#include "EVChartView.h"
#include <QtDebug>
EVChartView::EVChartView():MyChart(nullptr),
x_line(nullptr),
y_line(nullptr)
{
resize(width(),height());
setRenderHints(QPainter::Antialiasing );//设置抗锯齿| QPainter::SmoothPixmapTransform
// Mychartview->setAttribute(Qt::WA_TransparentForMouseEvents, true); //设置控件不接受鼠标事件
// setMouseTracking(true);
init();
}
EVChartView::~EVChartView()
{
}
void EVChartView::repaceData(QVector<QPointF> &Pointf)
{
if(Pointf.size())
{
MyPointf = Pointf;
MyLineSeries.replace(Pointf);
if(MyLineSeries.attachedAxes().size() == 0)
{
MyLineSeries.attachAxis(&MyAxisX);
MyLineSeries.attachAxis(&MyAxisY);
}
}
}
void EVChartView::clearData()
{
MyLineSeries.clear();
}
void EVChartView::setAxisX(double xmin, double xmax)
{
MyAxisX.setMin(xmin);
MyAxisX.setMax(xmax);
}
void EVChartView::setAxisY(double ymin, double ymax)
{
MyAxisY.setMin(ymin);
MyAxisY.setMax(ymax);
}
QString htmldata;
void EVChartView::mouseMoveEvent(QMouseEvent *e)
{
// qDebug()<< this->chart()->mapToValue(e->pos());//把鼠标坐标值转化为画出的图中的坐标
//以防父类的实例接收不到mouseMoveEvent事件。
// QChartView::mouseMoveEvent(e);//调用父类的重写方法。本行程序不同于e->ignore(),ignor是把事件继续向父控件(一般其父控件就是ui界面)传递,而不是向父类传递
int nX = this->chart()->mapToValue(e->pos()).x();
int nY = this->chart()->mapToValue(e->pos()).y();
if(MyAxisX.max()>= nX && MyAxisX.min() <= nX && MyAxisY.min() <= nY && MyAxisY.max()>= nY)
{
x_line->setLine(e->x(),0,e->x(),this->height());
y_line->setLine(0,e->y(),this->width(),e->y());
htmldata = QString("当前坐标位置 [%1,%2]").arg(nX).arg(nY);
currentPointText->setHtml(htmldata);
currentPointText->setX(e->x()+15);
currentPointText->setY(e->y());
x_line->setVisible(true);
y_line->setVisible(true);
currentPointText->setVisible(true);
}else{
x_line->setVisible(false);
y_line->setVisible(false);
currentPointText->setVisible(false);
}
}
//void EVChartView::enterEvent(QEvent *event)
//{
// x_line->setVisible(true);
// y_line->setVisible(true);
// currentPointText->setVisible(true);
//}
void EVChartView::leaveEvent(QEvent *event)
{
x_line->setVisible(false);
y_line->setVisible(false);
currentPointText->setVisible(false);
}
void EVChartView::init()
{
//设置图表样式
MyChart = new QChart();
MyChart->setBackgroundBrush(QColor("#243B59")); //设置背景颜色
// MyChart->legend()->setColor(QColor("#ffffff"));
// MyChart->legend()->setAlignment(Qt::AlignLeft);
// MyChart->legend()->setLabelColor("#ffffff");
MyChart->legend()->setVisible(false);
QFont font("微软雅黑");
font.setBold(true);
font.setPointSize(10);
MyChart->legend()->setFont(font);
MyAxisX.setLabelsColor(QColor("#ffffff"));
MyAxisX.setGridLineColor(QColor("#45658D"));
MyAxisX.setLineVisible(false);
MyAxisX.setLabelsFont(font);
MyAxisX.setTickCount(5);
MyAxisY.setLabelsColor(QColor("#ffffff"));
MyAxisY.setGridLineColor(QColor("#45658D"));
MyAxisY.setLineVisible(false);
MyAxisY.setLabelsFont(font);
MyAxisY.setTickCount(5);
MyAxisX.setMin(0);
MyAxisX.setMax(50);
MyAxisY.setMin(0);
MyAxisY.setMax(50);
MyChart->addAxis(&MyAxisY,Qt::AlignLeft);
MyChart->addAxis(&MyAxisX,Qt::AlignBottom);
// MyLineSeries.setName("Voltage.V");
MyLineSeries.setColor(QColor("#36D2FF"));
// MyLineSeries.setUseOpenGL(true);
MyChart->addSeries(&MyLineSeries);
setChart(MyChart);
//添加线条
y_line = new QGraphicsLineItem(); // 定义一个直线图元
x_line = new QGraphicsLineItem();
y_line->setLine(QLineF(10, 0, 10,height()));
y_line->setPen(QPen(QColor("#36D2FF")));
y_line->setEnabled(false);
y_line->setVisible(false);
x_line->setLine(QLineF(10, 0, 10,height()));
x_line->setPen(QPen(QColor("#36D2FF")));
x_line->setEnabled(false);
x_line->setVisible(false);
scene()->addItem(x_line);
scene()->addItem(y_line);
currentPointText=new QGraphicsTextItem();
currentPointText->setDefaultTextColor(QColor("#ffffff"));
currentPointText->setHtml("大家好");
currentPointText->setEnabled(false);
currentPointText->setVisible(false);
scene()->addItem(currentPointText);
}