生活随笔
收集整理的這篇文章主要介紹了
QCustomplot基础应用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、基本畫圖
首先,給個簡單的例子:
[cpp]?view plaincopy
?? QVector<double>?x(101),?y(101);??? for?(int?i=0;?i<101;?++i)?? {?? ??x[i]?=?i/50.0?-?1;??? ??y[i]?=?x[i]*x[i];??? }?? ?? customPlot->addGraph();??
[cpp]?view plaincopy
?? ?? customPlot->graph(0)->setData(x,?y);?? ?? customPlot->xAxis->setLabel("x");?? customPlot->yAxis->setLabel("y");?? ?? customPlot->xAxis->setRange(-1,?1);?? customPlot->yAxis->setRange(0,?1);??
[cpp]?view plaincopy
?? customPlot->replot();??
上面代碼生成的結(jié)果大致是這樣的:
[cpp]?view plaincopy
外觀??
[cpp]?view plaincopy
QCustomPlot的外觀由很多方面特性組成,都可以改變:??
[cpp]?view plaincopy
坐標(biāo)軸:??
[cpp]?view plaincopy
QCustomPlot有四個QCPAxis成員變量,分別代表四個坐標(biāo)軸:xAxis(下)yAxis(左)xAxis2(上)yAxis2(右)??
[cpp]?view plaincopy
QCPAxis有相應(yīng)的函數(shù)可以設(shè)置坐標(biāo)軸的刻度、間距、范圍等:??
[cpp]?view plaincopy
setTickStep(double?step);?? setTickVector(const?QVector<double>?&vec);?? setAutoTickStep(bool?on);?? setAutoTicks(bool?on);?? setAutoTickCount(int?approximateCount);??
[cpp]?view plaincopy
還有setBasePen、setTickPen、setTickLength、setSubTickLength、setSubTickPen、setTickLabelFont、setLabelFont、setTickLabelPadding、setLabelPadding、setRangeReversed等??
[cpp]?view plaincopy
等后面專門講QCPAxis的時候再詳細介紹??
曲線風(fēng)格:
[cpp]?view plaincopy
QCPGraph::setPen(const?QPen?&pen);??
曲線畫筆:
[cpp]?view plaincopy
QCPGraph::setLineStyle(LineStyle?ls);??
可以設(shè)置顏色、寬度、實虛等
曲線形狀:
[cpp]?view plaincopy
QCPGraph::setScatterStyle(QCPScatterStyle?&style);??
曲線形狀像*、+、x、o等等
填充曲線方式:
[cpp]?view plaincopy
QCPGraph::setBrush(const?QBrush?&brush);??
[cpp]?view plaincopy
QCPGraph::setChannelFillGraph(otherGraph);??
[cpp]?view plaincopy
QCPGraph::setBrush(Qt::NoBrush);??
以上會等到專門將QCPGraph和QCPScatterStyle類的時候再細講網(wǎng)格:
[cpp]?view plaincopy
customPlot->yAxis->grid();??
[cpp]?view plaincopy
setPen、setZeroLinePen、setSubGridVisible等??
[cpp]?view plaincopy
等講QCPGrid類再細講??
二、高級畫圖
[cpp]?view plaincopy
1、多曲線與多風(fēng)格??
[cpp]?view plaincopy
<pre?name="code"?class="cpp">customPlot->setLocale(QLocale(QLocale::English,?QLocale::UnitedKingdom));??? customPlot->legend->setVisible(true);?? QFont?legendFont?=?font();???? legendFont.setPointSize(9);??? customPlot->legend->setFont(legendFont);?? customPlot->legend->setBrush(QBrush(QColor(255,255,255,230)));?? ?? customPlot->axisRect()->insetLayout()->setInsetAlignment(0,?Qt::AlignBottom|Qt::AlignRight);?? ??? ?? ?? customPlot->addGraph(customPlot->yAxis,?customPlot->xAxis);?? customPlot->graph(0)->setPen(QPen(QColor(255,?100,?0)));?? customPlot->graph(0)->setBrush(QBrush(QPixmap("./dali.png")));??? customPlot->graph(0)->setLineStyle(QCPGraph::lsLine);?? customPlot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc,?5));?? customPlot->graph(0)->setName("Left?maxwell?function");?? ??? ?? ?? customPlot->addGraph();?? customPlot->graph(1)->setPen(QPen(Qt::red));?? customPlot->graph(1)->setBrush(QBrush(QPixmap("./dali.png")));??? customPlot->graph(1)->setLineStyle(QCPGraph::lsStepCenter);?? customPlot->graph(1)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle,?Qt::red,?Qt::white,?7));?? customPlot->graph(1)->setErrorType(QCPGraph::etValue);?? customPlot->graph(1)->setName("Bottom?maxwell?function");?? ??? ?? ?? customPlot->addGraph(customPlot->xAxis2,?customPlot->yAxis2);?? customPlot->graph(2)->setPen(QPen(Qt::blue));?? customPlot->graph(2)->setName("High?frequency?sine");?? ??? ?? ?? customPlot->addGraph(customPlot->xAxis2,?customPlot->yAxis2);?? QPen?blueDotPen;?? blueDotPen.setColor(QColor(30,?40,?255,?150));?? blueDotPen.setStyle(Qt::DotLine);?? blueDotPen.setWidthF(4);?? customPlot->graph(3)->setPen(blueDotPen);?? customPlot->graph(3)->setName("Sine?envelope");?? ??? ?? ?? customPlot->addGraph(customPlot->yAxis2,?customPlot->xAxis2);?? customPlot->graph(4)->setPen(QColor(50,?50,?50,?255));?? customPlot->graph(4)->setLineStyle(QCPGraph::lsNone);?? customPlot->graph(4)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle,?4));?? customPlot->graph(4)->setName("Some?random?data?around\na?quadratic?function");?? ??? ?? QVector<double>?x0(25),?y0(25);?? QVector<double>?x1(15),?y1(15),?y1err(15);?? QVector<double>?x2(250),?y2(250);?? QVector<double>?x3(250),?y3(250);?? QVector<double>?x4(250),?y4(250);?? for?(int?i=0;?i<25;?++i)??? {?? ??x0[i]?=?3*i/25.0;?? ??y0[i]?=?exp(-x0[i]*x0[i]*0.8)*(x0[i]*x0[i]+x0[i]);?? }?? for?(int?i=0;?i<15;?++i)??? {?? ??x1[i]?=?3*i/15.0;;?? ??y1[i]?=?exp(-x1[i]*x1[i])*(x1[i]*x1[i])*2.6;?? ??y1err[i]?=?y1[i]*0.25;?? }?? for?(int?i=0;?i<250;?++i)??? {?? ??x2[i]?=?i/250.0*3*M_PI;?? ??x3[i]?=?x2[i];?? ??x4[i]?=?i/250.0*100-50;?? ??y2[i]?=?sin(x2[i]*12)*cos(x2[i])*10;?? ??y3[i]?=?cos(x3[i])*10;?? ??y4[i]?=?0.01*x4[i]*x4[i]?+?1.5*(rand()/(double)RAND_MAX-0.5)?+?1.5*M_PI;?? }?? ??? ?? customPlot->graph(0)->setData(x0,?y0);?? customPlot->graph(1)->setDataValueError(x1,?y1,?y1err);?? customPlot->graph(2)->setData(x2,?y2);?? customPlot->graph(3)->setData(x3,?y3);?? customPlot->graph(4)->setData(x4,?y4);?? ?? customPlot->xAxis2->setVisible(true);?? customPlot->yAxis2->setVisible(true);?? ?? customPlot->xAxis->setRange(0,?2.7);?? customPlot->yAxis->setRange(0,?2.6);?? customPlot->xAxis2->setRange(0,?3.0*M_PI);?? customPlot->yAxis2->setRange(-70,?35);?? ?? QVector<double>?piTicks;?? QVector<QString>?piLabels;?? piTicks?<<?0??<<?0.5*M_PI?<<?M_PI?<<?1.5*M_PI?<<?2*M_PI?<<?2.5*M_PI?<<?3*M_PI;?? piLabels?<<?"0"?<<?QString::fromUtf8("?π")?<<?QString::fromUtf8("π")?<<?QString::fromUtf8("1?π")?<<?QString::fromUtf8("2π")?<<?QString::fromUtf8("2?π")?<<?QString::fromUtf8("3π");?? customPlot->xAxis2->setAutoTicks(false);?? customPlot->xAxis2->setAutoTickLabels(false);?? customPlot->xAxis2->setTickVector(piTicks);?? customPlot->xAxis2->setTickVectorLabels(piLabels);?? ?? customPlot->plotLayout()->insertRow(0);?? customPlot->plotLayout()->addElement(0,?0,?new?QCPPlotTitle(customPlot,?"Way?too?many?graphs?in?one?plot"));?? ?? customPlot->xAxis->setLabel("Bottom?axis?with?outward?ticks");?? customPlot->yAxis->setLabel("Left?axis?label");?? customPlot->xAxis2->setLabel("Top?axis?label");?? customPlot->yAxis2->setLabel("Right?axis?label");?? ?? customPlot->xAxis->setTickLength(0,?5);?? customPlot->xAxis->setSubTickLength(0,?3);?? ?? customPlot->yAxis2->setTickLength(3,?3);?? customPlot->yAxis2->setSubTickLength(1,?1);??
效果圖:
2、日期和時間數(shù)據(jù)曲線
[cpp]?view plaincopy
?? ??customPlot->setLocale(QLocale(QLocale::English,?QLocale::UnitedKingdom));?? ???? ??double?now?=?QDateTime::currentDateTime().toTime_t();?? ??srand(8);??? ???? ??for?(int?gi=0;?gi<5;?++gi)?? ??{?? ????customPlot->addGraph();?? ????QPen?pen;?? ????pen.setColor(QColor(0,?0,?255,?200));?? ????customPlot->graph()->setLineStyle(QCPGraph::lsLine);?? ????customPlot->graph()->setPen(pen);?? ????customPlot->graph()->setBrush(QBrush(QColor(255/4.0*gi,160,50,150)));?? ?????? ????QVector<double>?time(250),?value(250);?? ????for?(int?i=0;?i<250;?++i)?? ????{?? ??????time[i]?=?now?+?24*3600*i;?? ??????if?(i?==?0)?? ????????value[i]?=?(i/50.0+1)*(rand()/(double)RAND_MAX-0.5);?? ??????else?? ????????value[i]?=?fabs(value[i-1])*(1+0.02/4.0*(4-gi))?+?(i/50.0+1)*(rand()/(double)RAND_MAX-0.5);?? ????}?? ????customPlot->graph()->setData(time,?value);?? ??}?? ???? ??customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);?? ??customPlot->xAxis->setDateTimeFormat("MMMM\nyyyy");?? ???? ??customPlot->xAxis->setTickLabelFont(QFont(QFont().family(),?8));?? ??customPlot->yAxis->setTickLabelFont(QFont(QFont().family(),?8));?? ???? ??customPlot->xAxis->setAutoTickStep(false);?? ??customPlot->xAxis->setTickStep(2628000);??? ??customPlot->xAxis->setSubTickCount(3);?? ???? ??customPlot->yAxis->setAutoTicks(false);?? ??customPlot->yAxis->setAutoTickLabels(false);?? ??customPlot->yAxis->setTickVector(QVector<double>()?<<?5?<<?55);?? ??customPlot->yAxis->setTickVectorLabels(QVector<QString>()?<<?"Not?so\nhigh"?<<?"Very\nhigh");?? ???? ??customPlot->xAxis->setLabel("Date");?? ??customPlot->yAxis->setLabel("Random?wobbly?lines?value");?? ???? ??customPlot->xAxis2->setVisible(true);?? ??customPlot->yAxis2->setVisible(true);?? ??customPlot->xAxis2->setTicks(false);?? ??customPlot->yAxis2->setTicks(false);?? ??customPlot->xAxis2->setTickLabels(false);?? ??customPlot->yAxis2->setTickLabels(false);?? ???? ??customPlot->xAxis->setRange(now,?now+24*3600*249);?? ??customPlot->yAxis->setRange(0,?60);?? ???? ??customPlot->legend->setVisible(true);??
效果圖:
三、曲線、柱形圖、統(tǒng)計圖...
到目前為止,我們?yōu)閳D像添加曲線都是使用
[cpp]?view plaincopy
QCustomPlot::addGraph();?? QCustomPlot::graph();??
其實,除了
QCPGraph ,QCustomPlot 還提供了多個畫圖類:
QCPCurve:與QCPGraph 類似,差別在于它是用于展示參數(shù)化曲線,可以有循環(huán)。
QCPBars:柱形圖,如果有多個QCPBars ,可以依次重疊。
QCPStatisticalBox、QCPColorMap、QCPFinancial
與QCPGraph 不同的是,這些畫圖類在添加到QCustomPlot 的時候需要使用new創(chuàng)建一個實例,而不能直接
[cpp]?view plaincopy
<span?style="color:?rgb(53,?53,?53);?font-family:?monospace;?line-height:?19.5px;?background-color:?rgb(240,?240,?240);">addPlottable</span>();??
簡單例子如下:
[cpp]?view plaincopy
QCPBars?*myBars?=?new?QCPBars(customPlot->xAxis,?customPlot->yAxis);?? customPlot->addPlottable(myBars);?? ?? myBars->setName("Bars?Series?1");?? QVector<double>?keyData;?? QVector<double>?valueData;?? keyData?<<?1?<<?2?<<?3;?? valueData?<<?2?<<?4?<<?8;?? myBars->setData(keyData,?valueData);?? customPlot->rescaleAxes();?? customPlot->replot();??
好吧,這篇就到這里。水平有限,如有出錯,敬請指出,互相學(xué)習(xí)。
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖
總結(jié)
以上是生活随笔為你收集整理的QCustomplot基础应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。