使用QCustomPlot绘图的基础
生活随笔
收集整理的這篇文章主要介紹了
使用QCustomPlot绘图的基础
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用QCustomPlot繪圖的基礎
首先你用QCustomPlot::addGraph創建一個曲線圖然后你給曲線圖賦一些數據點(一對QVector<double>為x,y的值)并且定義曲線圖的外觀(線型,分散圖案,顏色,線筆)子厚調用QCustomPlot::replot。注意replot應該被自動調用當widget被重新改變大小的時候或者內建的用戶交互被觸發的時候(拖拽坐標軸范圍用鼠標或者齒輪),莫ten情況下QCustomPlot有四個類型為QCPAxis的坐標軸:xAxis,yAxis,xAxis2,yAxis2。組成了下,左,上,右四個坐標軸。他們的范圍(QCPAxis::setRange)定義了Plot當前可見的部分。
這有一個小例子customPlot是一個QCustomPlot widget的指針。
[cpp] view plaincopyprint?//?generate?some?data:?? ????QVector<double>?x(101),?y(101);?//?initialize?with?entries?0..100?? ????for?(int?i=0;?i<101;?++i)?? ????{?? ??????x[i]?=?i/50.0?-?1;?//?x?goes?from?-1?to?1?? ??????y[i]?=?x[i]*x[i];?//?let's?plot?a?quadratic?function?? ????}?? ????//?create?graph?and?assign?data?to?it:?? ????customPlot->addGraph();?? ????customPlot->graph(0)->setData(x,?y);?? ????//?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->replot();??
輸出應該看起來跟下面一樣。注意刻度線的位置是被自動選擇的。然而乜可以完全控制刻度步長甚至單一刻度位置通過調用setTickStep或者setTickVector在各自的軸上。為了禁用和可用自動化,電泳setAutoTickStep或 [cpp] view plaincopyprint?</pre>者setAutoTicks。如果你想提供自己的刻度步長或者刻度vector,禁用自動化將是必須的。如果你只是想在可見的范圍里改變合適的刻度數,可以使用setAutoTickCount.你將看到坐標軸的刻度標簽沒有被裁剪甚至它們變寬了.這是因為自動化的邊緣計算由默認的數據決定。如果你不希望axis的margin自動決定,用過調用customPlot->axisRect()->setAutoMargins(QCP::msNone)來關閉。然后可以通過QCPAxisRect::setMargins來手動調整margins.改變外觀graph有很多影響外觀的因素,所有的都可以被修改。這有重要的幾點Line?style:調用QCPGraph::setLineStyle。對所有可能的Line?Style你可以查看LineStyle文檔。Line?Pen:繪圖框架的所有Pen都是可獲取的,solid,dashed,dotted,不同的寬度,顏色,透明度,等等。通過QCPGraph::setPen設置配置好的Pen.Scatter?symbol調用QCPGraph::setScatterStyle來改變分散點符號的外觀。所以可能的scatter?style可以查看QCPScatterStyle文檔。Fills?under?graph?or?between?two?graphs:繪圖框架的所有畫刷都可以被用來填充graph:solid,各種各樣的模式,紋理,漸變,顏色,透明度等等通過QCPGraph::setBrush來設置配置好的Brush.坐標軸的外觀可以被修改通過改變繪圖的畫筆和它們label的字體。看一看QCPAxis文檔就不言而喻了。這有一個很快的總結:setBasePen,setTickPen,setTickLength,setSubTickLength,setSubTickPen,setTickLabelFont,setLabelFont,setTickLabelPadding,setLabelPadding。你可以反轉一個坐標軸使用setRangeReversed.柵格可以被修改通過使用單獨的QCPGrid實例。每一個坐標軸都有自己的柵格實例QCPAxis::grid()因此改變水平的柵格線可以通過訪問customPlot->yAxis->grid()來實現。柵格線外觀的基礎地的畫筆可以使用QCPGrid::setPen來設置。0刻度可以使用不同的畫筆繪出來,使用QCPGrid::setZeroLinePen來配置。如果你不想畫0刻度用特殊的筆只需要設置為Qt::NoPen并且柵格線將會使用普通的畫筆繪制。Examples有兩個曲線圖的Plot這有一個例子創建一個衰減的cosin函數圖像.<span?style="white-space:pre"></span><pre?name="code"?class="cpp">//?add?two?new?graphs?and?set?their?look:?? customPlot->addGraph();?? customPlot->graph(0)->setPen(QPen(Qt::blue));?//?line?color?blue?for?first?graph?? customPlot->graph(0)->setBrush(QBrush(QColor(0,?0,?255,?20)));?//?first?graph?will?be?filled?with?translucent?blue?? customPlot->addGraph();?? customPlot->graph(1)->setPen(QPen(Qt::red));?//?line?color?red?for?second?graph?? //?generate?some?points?of?data?(y0?for?first,?y1?for?second?graph):?? QVector<double>?x(250),?y0(250),?y1(250);?? for?(int?i=0;?i<250;?++i)?? {?? ??x[i]?=?i;?? ??y0[i]?=?exp(-i/150.0)*cos(i/10.0);?//?exponentially?decaying?cosine?? ??y1[i]?=?exp(-i/150.0);?//?exponential?envelope?? }?? //?configure?right?and?top?axis?to?show?ticks?but?no?labels:?? //?(see?QCPAxisRect::setupFullAxesBox?for?a?quicker?method?to?do?this)?? customPlot->xAxis2->setVisible(true);?? customPlot->xAxis2->setTickLabels(false);?? customPlot->yAxis2->setVisible(true);?? customPlot->yAxis2->setTickLabels(false);?? //?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)));?? //?pass?data?points?to?graphs:?? customPlot->graph(0)->setData(x,?y0);?? customPlot->graph(1)->setData(x,?y1);?? //?let?the?ranges?scale?themselves?so?graph?0?fits?perfectly?in?the?visible?area:?? customPlot->graph(0)->rescaleAxes();?? //?same?thing?for?graph?1,?but?only?enlarge?ranges?(in?case?graph?1?is?smaller?than?graph?0):?? customPlot->graph(1)->rescaleAxes(true);?? //?Note:?we?could?have?also?just?called?customPlot->rescaleAxes();?instead?? //?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);??
正如你看到的,應用填充凸顯跟設置一個畫刷一樣容易但不是Qt::NoBrush.填充從圖像開始到與坐標軸平行的0值線。如果我們想要在一個graph和另一個graph之間填充的通道可以調用QCPGraph::setChannelFillGraph(otherGraph).為了刪除通道填充,只需要將零值線作為另一個圖形并且填充將可以達到所有的方式來把之前的作為零值線。為了完全移除填充調用QCPGraph::setBrush(Qt::NoBrush);
Plotting with multiple axes and more advanced styleing
繪制多軸和更高級的樣式
現在看一些復雜的例子為了創建演示截圖,在四個坐標軸上包括了五個graphs,紋理填充,垂直誤差,圖例,點作為小數分隔符等等。
[cpp] view plaincopyprint?ustomPlot->setLocale(QLocale(QLocale::English,?QLocale::UnitedKingdom));?//?period?as?decimal?separator?and?comma?as?thousand?separator?? 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::AlignBottom|Qt::AlignRight);?? ??? //?setup?for?graph?0:?key?axis?left,?value?axis?bottom?? //?will?contain?left?maxwell-like?function?? customPlot->addGraph(customPlot->yAxis,?customPlot->xAxis);?? customPlot->graph(0)->setPen(QPen(QColor(255,?100,?0)));?? customPlot->graph(0)->setBrush(QBrush(QPixmap("./dali.png")));?//?fill?with?texture?of?specified?png-image?? customPlot->graph(0)->setLineStyle(QCPGraph::lsLine);?? customPlot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc,?5));?? customPlot->graph(0)->setName("Left?maxwell?function");?? ??? //?setup?for?graph?1:?key?axis?bottom,?value?axis?left?(those?are?the?default?axes)?? //?will?contain?bottom?maxwell-like?function?? customPlot->addGraph();?? customPlot->graph(1)->setPen(QPen(Qt::red));?? customPlot->graph(1)->setBrush(QBrush(QPixmap("./dali.png")));?//?same?fill?as?we?used?for?graph?0?? 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");?? ??? //?setup?for?graph?2:?key?axis?top,?value?axis?right?? //?will?contain?high?frequency?sine?with?low?frequency?beating:?? customPlot->addGraph(customPlot->xAxis2,?customPlot->yAxis2);?? customPlot->graph(2)->setPen(QPen(Qt::blue));?? customPlot->graph(2)->setName("High?frequency?sine");?? ??? //?setup?for?graph?3:?same?axes?as?graph?2?? //?will?contain?low?frequency?beating?envelope?of?graph?2?? 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");?? ??? //?setup?for?graph?4:?key?axis?right,?value?axis?top?? //?will?contain?parabolically?distributed?data?points?with?some?random?perturbance?? 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");?? ??? //?generate?data,?just?playing?with?numbers,?not?much?to?learn?here:?? 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)?//?data?for?graph?0?? {?? ??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)?//?data?for?graph?1?? {?? ??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)?//?data?for?graphs?2,?3?and?4?? {?? ??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;?? }?? ??? //?pass?data?points?to?graphs:?? 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);?? //?activate?top?and?right?axes,?which?are?invisible?by?default:?? customPlot->xAxis2->setVisible(true);?? customPlot->yAxis2->setVisible(true);?? //?set?ranges?appropriate?to?show?data:?? 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);?? //?set?pi?ticks?on?top?axis:?? 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);?? //?add?title?layout?element:?? customPlot->plotLayout()->insertRow(0);?? customPlot->plotLayout()->addElement(0,?0,?new?QCPPlotTitle(customPlot,?"Way?too?many?graphs?in?one?plot"));?? //?set?labels:?? 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");?? //?make?ticks?on?bottom?axis?go?outward:?? customPlot->xAxis->setTickLength(0,?5);?? customPlot->xAxis->setSubTickLength(0,?3);?? //?make?ticks?on?right?axis?go?inward?and?outward:?? customPlot->yAxis2->setTickLength(3,?3);?? customPlot->yAxis2->setSubTickLength(1,?1);??
正如你所見,你可以自由的定義那個軸作為對graph起作用。例子中索引為0的graph使用了左軸作為它的key并且下軸作為它的值。因此graph是朝著左軸向上的。
為了對graph1應用誤差線,我們需要確保它們設置了QCPGraph::setErrorType().它常用來指定是否為這個值,鍵,或者兩者或者都不顯示誤差線。然后我們可以調用QCPGraph::setData函數將傳遞我們想要的參數。這有keys(x1),value(y1)和誤差值(ylerr)。為了進一步的解釋這個方法的使用可以查看文檔。
Plotting date and time data
下面,我們將看到如何繪制日期和時間關聯的數據。它基本可以歸結為兩個額外的函數調用來通知坐標軸,它應該輸出標簽作為日期或者時間。
[cpp] view plaincopyprint?//?set?locale?to?english,?so?we?get?english?month?names:?? customPlot->setLocale(QLocale(QLocale::English,?QLocale::UnitedKingdom));?? //?seconds?of?current?time,?we'll?use?it?as?starting?point?in?time?for?data:?? double?now?=?QDateTime::currentDateTime().toTime_t();?? srand(8);?//?set?the?random?seed,?so?we?always?get?the?same?random?data?? //?create?multiple?graphs:?? 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)));?? ??//?generate?random?walk?data:?? ??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);?? }?? //?configure?bottom?axis?to?show?date?and?time?instead?of?number:?? customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);?? customPlot->xAxis->setDateTimeFormat("MMMM\nyyyy");?? //?set?a?more?compact?font?size?for?bottom?and?left?axis?tick?labels:?? customPlot->xAxis->setTickLabelFont(QFont(QFont().family(),?8));?? customPlot->yAxis->setTickLabelFont(QFont(QFont().family(),?8));?? //?set?a?fixed?tick-step?to?one?tick?per?month:?? customPlot->xAxis->setAutoTickStep(false);?? customPlot->xAxis->setTickStep(2628000);?//?one?month?in?seconds?? customPlot->xAxis->setSubTickCount(3);?? //?apply?manual?tick?and?tick?label?for?left?axis:?? 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");?? //?set?axis?labels:?? customPlot->xAxis->setLabel("Date");?? customPlot->yAxis->setLabel("Random?wobbly?lines?value");?? //?make?top?and?right?axes?visible?but?without?ticks?and?labels:?? customPlot->xAxis2->setVisible(true);?? customPlot->yAxis2->setVisible(true);?? customPlot->xAxis2->setTicks(false);?? customPlot->yAxis2->setTicks(false);?? customPlot->xAxis2->setTickLabels(false);?? customPlot->yAxis2->setTickLabels(false);?? //?set?axis?ranges?to?show?all?data:?? customPlot->xAxis->setRange(now,?now+24*3600*249);?? customPlot->yAxis->setRange(0,?60);?? //?show?legend:?? customPlot->legend->setVisible(true);??
你可以給QCPAxis::setDateTimeFormate()傳遞一個字符串有相同的格式選項跟QDateTime::toString。所有的date/time都被作為秒來控制從1970.1.1 凌晨12點開始。這就是你使用的格式,當調用QDateTime::toTime_t或者setTime_t在Qt date/time類中。為了達到次秒級的精度,你可以使用QDateTime::toMSecsSinceEpoch()/1000.0結果是double值代表與toTime_t返回一樣的時間間隔,但是是毫秒級的精度。
Beyond Graphs:Curves,Bar Charts,Statistical Box Plot...
到目前位置我們只看了Graph.因為它們是這樣一個占主導地位的用例,QCustomPlot為他們提供一個特化的接口。我們始終使用它QCustomPlot::addGraph,QCustomPlot::graph.這不是故事的全部。QCustomPlot有更一般的接口來為不同的對象在Plot里繪制數據。我可以稱它們為Plottables.這個接口圍繞抽象基類QCPAbstractPlottable被創建。所有的Plottables派生自這個類并且跟QCPGraph這個類相似。QCustomPlot提供了很多其他的Plottable類。
QCPGraph
這是一個plottable類,顯示一些列數據點作為graph.
QCPCurve
跟QCPGraph類似用不同的參數化的曲線組成。不像函數graph它們有封閉的環形。
QCPBar
一個柱狀圖。帶有一系列的數據點和代表它們的柱狀圖。如果這有多個QCPBars plottables.他們可以堆疊在彼此頂方。
QCPStatisticalBox
一個統計框。帶著五個數字(minium,lower quartile,midian,upper quartile,maximum)。
QCPColorMap
一個2D map可視化第三維度的數據通過使用一個漸變顏色。
QCPFinancial
一個plottable可以被用來可視化像股票的開盤價,最高,最低,收盤價通過使用Candlesticks或者OHLC bars
不像graphs,其他的plottables需要通過使用外面的QCustomPlot來創建并且把它們添加到里面QCustomPlot::addPlottable.這意味著這沒有addCurver或者addBar函數。QCustomPlot傳遞plottable的所有權。已經存在的plottables可以通過QCustomPlot::plottable(int index)來訪問。plottable包括graph的數量可以使用QCustomPlot::plottableCount來檢索。這有一個很簡單的例子創建一個帶有三個柱狀的柱狀圖。
[cpp] view plaincopyprint?QCPBars?*myBars?=?new?QCPBars(customPlot->xAxis,?customPlot->yAxis);?? customPlot->addPlottable(myBars);?? //?now?we?can?modify?properties?of?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();??
關于其他plottables更多的細節可以在實例里面被發現。更進一步,每一個plottable類型都在文檔里有詳細的描述。
當然,這是絕對可以的編寫你自己的plottable來顯示你需要的任意數據。你應該查看QCPAbstractPlottable文檔來學習如何子類化它。你也可以查看已經存在的plottables來看他們如何工作。為了這個目的我建議從QCPBar或者QCPCurve開始。QCPGraph特征很多不適合作為一個開始。
首先你用QCustomPlot::addGraph創建一個曲線圖然后你給曲線圖賦一些數據點(一對QVector<double>為x,y的值)并且定義曲線圖的外觀(線型,分散圖案,顏色,線筆)子厚調用QCustomPlot::replot。注意replot應該被自動調用當widget被重新改變大小的時候或者內建的用戶交互被觸發的時候(拖拽坐標軸范圍用鼠標或者齒輪),莫ten情況下QCustomPlot有四個類型為QCPAxis的坐標軸:xAxis,yAxis,xAxis2,yAxis2。組成了下,左,上,右四個坐標軸。他們的范圍(QCPAxis::setRange)定義了Plot當前可見的部分。
這有一個小例子customPlot是一個QCustomPlot widget的指針。
[cpp] view plaincopyprint?
輸出應該看起來跟下面一樣。注意刻度線的位置是被自動選擇的。然而乜可以完全控制刻度步長甚至單一刻度位置通過調用setTickStep或者setTickVector在各自的軸上。為了禁用和可用自動化,電泳setAutoTickStep或 [cpp] view plaincopyprint?
正如你看到的,應用填充凸顯跟設置一個畫刷一樣容易但不是Qt::NoBrush.填充從圖像開始到與坐標軸平行的0值線。如果我們想要在一個graph和另一個graph之間填充的通道可以調用QCPGraph::setChannelFillGraph(otherGraph).為了刪除通道填充,只需要將零值線作為另一個圖形并且填充將可以達到所有的方式來把之前的作為零值線。為了完全移除填充調用QCPGraph::setBrush(Qt::NoBrush);
Plotting with multiple axes and more advanced styleing
繪制多軸和更高級的樣式
現在看一些復雜的例子為了創建演示截圖,在四個坐標軸上包括了五個graphs,紋理填充,垂直誤差,圖例,點作為小數分隔符等等。
[cpp] view plaincopyprint?
正如你所見,你可以自由的定義那個軸作為對graph起作用。例子中索引為0的graph使用了左軸作為它的key并且下軸作為它的值。因此graph是朝著左軸向上的。
為了對graph1應用誤差線,我們需要確保它們設置了QCPGraph::setErrorType().它常用來指定是否為這個值,鍵,或者兩者或者都不顯示誤差線。然后我們可以調用QCPGraph::setData函數將傳遞我們想要的參數。這有keys(x1),value(y1)和誤差值(ylerr)。為了進一步的解釋這個方法的使用可以查看文檔。
Plotting date and time data
下面,我們將看到如何繪制日期和時間關聯的數據。它基本可以歸結為兩個額外的函數調用來通知坐標軸,它應該輸出標簽作為日期或者時間。
[cpp] view plaincopyprint?
你可以給QCPAxis::setDateTimeFormate()傳遞一個字符串有相同的格式選項跟QDateTime::toString。所有的date/time都被作為秒來控制從1970.1.1 凌晨12點開始。這就是你使用的格式,當調用QDateTime::toTime_t或者setTime_t在Qt date/time類中。為了達到次秒級的精度,你可以使用QDateTime::toMSecsSinceEpoch()/1000.0結果是double值代表與toTime_t返回一樣的時間間隔,但是是毫秒級的精度。
Beyond Graphs:Curves,Bar Charts,Statistical Box Plot...
到目前位置我們只看了Graph.因為它們是這樣一個占主導地位的用例,QCustomPlot為他們提供一個特化的接口。我們始終使用它QCustomPlot::addGraph,QCustomPlot::graph.這不是故事的全部。QCustomPlot有更一般的接口來為不同的對象在Plot里繪制數據。我可以稱它們為Plottables.這個接口圍繞抽象基類QCPAbstractPlottable被創建。所有的Plottables派生自這個類并且跟QCPGraph這個類相似。QCustomPlot提供了很多其他的Plottable類。
QCPGraph
這是一個plottable類,顯示一些列數據點作為graph.
QCPCurve
跟QCPGraph類似用不同的參數化的曲線組成。不像函數graph它們有封閉的環形。
QCPBar
一個柱狀圖。帶有一系列的數據點和代表它們的柱狀圖。如果這有多個QCPBars plottables.他們可以堆疊在彼此頂方。
QCPStatisticalBox
一個統計框。帶著五個數字(minium,lower quartile,midian,upper quartile,maximum)。
QCPColorMap
一個2D map可視化第三維度的數據通過使用一個漸變顏色。
QCPFinancial
一個plottable可以被用來可視化像股票的開盤價,最高,最低,收盤價通過使用Candlesticks或者OHLC bars
不像graphs,其他的plottables需要通過使用外面的QCustomPlot來創建并且把它們添加到里面QCustomPlot::addPlottable.這意味著這沒有addCurver或者addBar函數。QCustomPlot傳遞plottable的所有權。已經存在的plottables可以通過QCustomPlot::plottable(int index)來訪問。plottable包括graph的數量可以使用QCustomPlot::plottableCount來檢索。這有一個很簡單的例子創建一個帶有三個柱狀的柱狀圖。
[cpp] view plaincopyprint?
關于其他plottables更多的細節可以在實例里面被發現。更進一步,每一個plottable類型都在文檔里有詳細的描述。
當然,這是絕對可以的編寫你自己的plottable來顯示你需要的任意數據。你應該查看QCPAbstractPlottable文檔來學習如何子類化它。你也可以查看已經存在的plottables來看他們如何工作。為了這個目的我建議從QCPBar或者QCPCurve開始。QCPGraph特征很多不適合作為一個開始。
總結
以上是生活随笔為你收集整理的使用QCustomPlot绘图的基础的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 塔拉 孤岛危机2_《孤岛危机》新作爆料!
- 下一篇: 最小二乘法幂函数C语言,跪求最小二乘法幂