设为首页 加入收藏

TOP

基于QCustomPlot绘图,鼠标跟随动态显示曲线上的点的值(四)
2018-12-11 16:09:42 】 浏览:1274
Tags:基于 QCustomPlot 绘图 鼠标 跟随 动态 显示 曲线
or
(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);//直线 m_lineTracer->updatePosition(x_val, y_val); this->replot();//曲线重绘 } }

 

3 使用自定义图表类XCustomPlot

在需要绘图的地方使用,代码如下:

XCustomPlot *customPlot = new XCustomPlot(); customPlot->showTracer(true); // add title layout element:
customPlot->plotLayout()->insertRow(0); customPlot->plotLayout()->addElement(0, 0, new QCPTextElement(customPlot, "标题", QFont("黑体", 12, QFont::Bold))); customPlot->legend->setVisible(true); QFont legendFont = font();  // start out with MainWindow's font..
legendFont.setPointSize(9); // and make a bit smaller for legend
customPlot->legend->setFont(legendFont); customPlot->legend->setBrush(QBrush(QColor(255,255,255,230))); // by default, the legend is in the inset layout of the main axis rect. So this is how we access it to change legend placement:
customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop|Qt::AlignCenter); // make left and bottom axes always transfer their ranges to right and top axes:
connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange))); connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange))); // Allow user to drag axis ranges with mouse, zoom with mouse wheel and select graphs by clicking:
customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables); // generate some data:
int nCount = 100; QVector<double> x(nCount), y0(nCount), y1(nCount); // initialize with entries 0..100
for (int i = 0; i < nCount; ++i) { x[i] = i/50.0 - 1; // x goes from -1 to 1
    y0[i] = x[i]*x[i]; // let's plot a quadratic function
    y1[i] = x[i]*x[i] - x[i]; // let's plot a quadratic function
} // create graph and assign data to it:
QPen pen; int i = 1; QCPGraph *pGraph = customPlot->addGraph(); // customPlot->graph(0)->setData(x, y0);
pGraph->setName("曲线"); pGraph->setData(x,y0); pen.setColor(QColor(qSin(i * 1 + 1.2) * 80 + 80, qSin(i*0.3 + 0) * 80 + 80, qSin(i*0.3 + 1.5) * 80 + 80)); pGraph->setPen(pen); // give the axes some labels:
customPlot->xAxis->setLabel("x"); customPlot->yAxis->setLabel("y"); // set axes ranges, so we see all data:
customPlot->xAxis->setRange(-1, 1); customPlot->yAxis->setRange(0, 1); customPlot->repl
首页 上一页 1 2 3 4 5 下一页 尾页 4/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇[bzoj3524][Couriers] 下一篇[luogu3810][bzoj3262][陌上花开]

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目