tft每日頭條

 > 生活

 > cqb基礎知識圖解

cqb基礎知識圖解

生活 更新时间:2024-10-06 00:18:42

開源的圖表控件QCustomPlot很經典,作者至少是八星鬥聖級别,在曲線數據展示這塊性能彪悍,總結了一些容易忽略的經驗要點。

- 可以将XY軸對調,然後形成橫向的效果,無論是曲線圖還是柱狀圖,分組圖、堆積圖等,都支持這個特性。

- 不需要的提示圖例可以調用 legend->removeItem 進行移除。

- 兩條曲線可以調用 setChannelFillGraph 設置合并為一個面積區域。

- 可以關閉抗鋸齒 setAntialiased 加快繪制速度。

- 可以設置不同的線條樣式(setLineStyle)、數據樣式(setScatterStyle)。

- 坐标軸的箭頭樣式可更換 setUpperEnding。

- 可以用 QCPBarsGroup 實現柱狀分組圖,這個類在官方demo中沒有,所以非常容易忽略。

- V2.0開始支持數據排序設置,默認是交給QCustomPlot排序,也可以設置setData第三個參數為true表示已經排序過,這樣可以繪制往回走的曲線。

- 頻繁繪制數據可以設置排隊繪制參數 replot(QCustomPlot::rpQueuedReplot),可以避免重複的replot和提高性能。如果不開啟很可能繪制出錯。

//對調XY軸,在最前面設置 QCPAxis *yAxis = customPlot->yAxis; QCPAxis *xAxis = customPlot->xAxis; customPlot->xAxis = yAxis; customPlot->yAxis = xAxis; //移除圖例 customPlot->legend->removeItem(1); //合并兩個曲線畫布形成封閉區域 customPlot->graph(0)->setChannelFillGraph(customPlot->graph(1)); //關閉抗鋸齒以及設置拖動的時候不啟用抗鋸齒 customPlot->graph()->setAntialiased(false); customPlot->setNoAntialiasingOnDrag(true); //多種設置數據的方法 customPlot->graph(0)->setData(); customPlot->graph(0)->data()->set(); //設置不同的線條樣式、數據樣式 customPlot->graph()->setLineStyle(QCPGraph::lsLine); customPlot->graph()->setScatterStyle(QCPScatterStyle::ssDot); customPlot->graph()->setScatterStyle(QCPScatterStyle(shapes.at(i), 10)); //還可以設置為圖片或者自定義形狀 customPlot->graph()->setScatterStyle(QCPScatterStyle(QPixmap("./sun.png"))); QPainterPath customScatterPath; for (int i = 0; i < 3; i) { customScatterPath.cubicTo(qCos(2 * M_PI * i / 3.0) * 9, qSin(2 * M_PI * i / 3.0) * 9, qCos(2 * M_PI * (i 0.9) / 3.0) * 9, qSin(2 * M_PI * (i 0.9) / 3.0) * 9, 0, 0); } customPlot->graph()->setScatterStyle(QCPScatterStyle(customScatterPath, QPen(Qt::black, 0), QColor(40, 70, 255, 50), 10)); //更換坐标軸的箭頭樣式 customPlot->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow); customPlot->yAxis->setUpperEnding(QCPLineEnding::esSpikeArrow); //設置背景圖片 customPlot->axisRect()->setBackground(QPixmap("./solarpanels.jpg")); //畫布也可以設置背景圖片 customPlot->graph(0)->setBrush(QBrush(QPixmap("./balboa.jpg"))); //整體可以設置填充顔色或者圖片 customPlot->setBackground(QBrush(gradient)); //設置零點線條顔色 customPlot->xAxis->grid()->setZeroLinePen(Qt::NoPen); //控制是否鼠标滾輪縮放拖動等交互形式 customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables); //柱狀分組圖 QCPBarsGroup *group = new QCPBarsGroup(customPlot); QList<QCPBars*> bars; bars << fossil << nuclear << regen; foreach (QCPBars *bar, bars) { //設置柱狀圖的寬度大小 bar->setWidth(bar->width() / bars.size()); group->append(bar); } //設置分組之間的間隔 group->setSpacing(2); //繪制往回走的曲線 QVector<double> keys, values; keys << 0 << 1 << 2 << 3 << 4 << 5 << 4 << 3; values << 5 << 4 << 6 << 7 << 7 << 6 << 5 << 4; customPlot->graph(0)->setData(keys, values, true); //頻繁繪制數據開啟排隊繪制可以提高性能 customPlot->replot(QCustomPlot::rpQueuedReplot); QCPAxis *axis = customPlot->xAxis; double lower = axis->range().lower; double upper = axis->range().upper; double origin = (upper - lower) / 2; //設置刻度線按照設置優先而不是可讀性優先 axis->ticker()->setTickStepStrategy(QCPAxisTicker::tssMeetTickCount); //設置原點值為範圍值的中心點 axis->ticker()->setTickOrigin(origin);

cqb基礎知識圖解(關于QCustomPlot超級圖表的一些冷知識點)1

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关生活资讯推荐

热门生活资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved