-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathXxwCustomPlot.cpp
77 lines (68 loc) · 2.46 KB
/
XxwCustomPlot.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "XxwCustomPlot.h"
XxwCustomPlot::XxwCustomPlot(QWidget *parent)
:QCustomPlot(parent)
,m_isShowTracer(false)
,m_xTracer(Q_NULLPTR)
,m_yTracer(Q_NULLPTR)
,m_dataTracers(QList<XxwTracer *>())
,m_lineTracer(Q_NULLPTR)
{
}
void XxwCustomPlot::mouseMoveEvent(QMouseEvent *event)
{
QCustomPlot::mouseMoveEvent(event);
if(m_isShowTracer)
{
//当前鼠标位置(像素坐标)
int x_pos = event->pos().x();
int y_pos = event->pos().y();
//像素坐标转成实际的x,y轴的坐标
float x_val = this->xAxis->pixelToCoord(x_pos);
float y_val = this->yAxis->pixelToCoord(y_pos);
if(Q_NULLPTR == m_xTracer)
m_xTracer = new XxwTracer(this, XxwTracer::XAxisTracer);//x轴
m_xTracer->updatePosition(x_val, y_val);
if(Q_NULLPTR == m_yTracer)
m_yTracer = new XxwTracer(this, XxwTracer::YAxisTracer);//y轴
m_yTracer->updatePosition(x_val, y_val);
int nTracerCount = m_dataTracers.count();
int nGraphCount = graphCount();
if(nTracerCount < nGraphCount)
{
for(int i = nTracerCount; i < nGraphCount; ++i)
{
XxwTracer *tracer = new XxwTracer(this, XxwTracer::DataTracer);
m_dataTracers.append(tracer);
}
}
else if(nTracerCount > nGraphCount)
{
for(int i = nGraphCount; i < nTracerCount; ++i)
{
XxwTracer *tracer = m_dataTracers[i];
if(tracer)
{
tracer->setVisible(false);
}
}
}
for (int i = 0; i < nGraphCount; ++i)
{
XxwTracer *tracer = m_dataTracers[i];
if(!tracer)
tracer = new XxwTracer(this, XxwTracer::DataTracer);
tracer->setVisible(true);
tracer->setPen(this->graph(i)->pen());
tracer->setBrush(Qt::NoBrush);
tracer->setLabelPen(this->graph(i)->pen());
auto iter = this->graph(i)->data()->findBegin(x_val);
double value = iter->mainValue();
// double value = this->graph(i)->data()->findBegin(x_val)->value;
tracer->updatePosition(x_val, value);
}
if(Q_NULLPTR == m_lineTracer)
m_lineTracer = new XxwTraceLine(this,XxwTraceLine::Both);//直线
m_lineTracer->updatePosition(x_val, y_val);
this->replot();//曲线重绘
}
}