若該文爲原創文章,未經容許不得轉載 原博主博客地址:https://blog.csdn.net/qq21497936
原博主博客導航:http://www.javashuo.com/article/p-wxwjppoc-mo.html
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/108240696 各位讀者,知識無窮而人力有窮,要麼改需求,要麼找專業人士,要麼本身研究
紅胖子(紅模仿)的博文大全:開發技術集合(包含Qt實用技術、樹莓派、三維、OpenCV、OpenGL、ffmpeg、OSG、單片機、軟硬結合等等)持續更新中…(點擊傳送門)app
Qt開發專欄:開發技術(點擊傳送門)
上一篇:《Qt開發技術:QCharts(三)QCharts樣條曲線圖介紹、Demo以及代碼詳解》 下一篇: 敬請期待...框架
<br>函數
前言
紅胖子,來也! 按照順序,本章爲面積圖。動畫
<br>this
補充
QCharts全部的圖表都依賴《Qt開發技術:QCharts(一)QCharts基本介紹以及圖表框架詳解》中的QChart、QChartView、QLegend、QValueAxis。.net
<br>code
Demo
<br>orm
Demo下載地址
當前爲v1.1.0版本 CSDN:https://download.csdn.net/download/qq21497936/12753524 QQ羣:1047134658(點擊「文件」搜索「qChartsTools」,羣內與博文同步更新)blog
<br>圖片
面積圖
概述
面積圖是使用QAreaSeries類或AreaSeries QML類型實現的。默認狀況下,X軸被用做一個邊界和QLineSeries或線列做爲另外一個。可是,能夠使用QLineSeries或LineSeries做爲兩個邊界。
<br>
QAreaSeries(面積圖類)
概述
QAreaSeries類在面積圖中顯示數據。 面積序列用於顯示定量數據。它是基於一系列線,用顏色強調邊界線之間的區域。因爲區域序列基於行序列,QAreaSeries構造函數須要一個QLineSeries實例,該實例定義區域的上邊界。默認狀況下,使用繪圖區域底部做爲下邊界繪製面積圖。下邊界能夠由另外一條線指定,而不是繪圖區域的底部。在這種狀況下,QAReaSeries應該用兩個QLineSeries實例初始化。 注意:當下邊界值大於上邊界值時,術語上下邊界可能會產生誤導。重點是這兩條邊界線之間的區域將被填充。 下面的代碼片斷說明了如何建立基本面積圖:
_pLineSeries = new QLineSeries; _pLineSeries2 = new QLineSeries; _pLineSeries3 = new QLineSeries; _pLineSeries4 = new QLineSeries; // 方式一:逐一添加,大批量數據較慢 _pLineSeries->append(0, qrand()%11); _pLineSeries->append(1, qrand()%11); _pLineSeries->append(2, qrand()%11); _pLineSeries->append(3, qrand()%11); _pLineSeries->append(4, qrand()%11); _pLineSeries->append(5, qrand()%11); _pLineSeries->append(6, qrand()%11); _pLineSeries->append(7, qrand()%11); _pLineSeries->append(8, qrand()%11); _pLineSeries->append(9, qrand()%11); _pLineSeries->append(10, qrand()%11); _pLineSeries->setName("通道1"); _pLineSeries->setPen(QPen(QColor(qrand()%256, qrand()%256, qrand()%256), 2)); // 通用:將數據插入到圖表中 _pAreaSeries = new QAreaSeries(); _pAreaSeries->setName("面積1"); _pLineSeries->setPointsVisible(true); _pLineSeries->setPointLabelsFormat("(@xPoint,@yPoint)"); _pAreaSeries->setUpperSeries(_pLineSeries); _pChart->addSeries(_pAreaSeries);
效率更高的方式爲:
// 方式二:逐一添加,大批量數據插入 QList<QLineSeries *> list; list.append(_pLineSeries); list.append(_pLineSeries2); list.append(_pLineSeries3); list.append(_pLineSeries4); for(int index = 0; index < 4; index++) { QList<QPointF> listPointF; for(int index = 0; index < 11; index++) { listPointF << QPointF(index, qrand()%11); } list.at(index)->append(listPointF); list.at(index)->setName(QString("通道%1").arg(index+1)); list.at(index)->setPen(QPen(QColor(qrand()%256, qrand()%256, qrand()%256), 2)); } // 通用:將數據插入到圖表中 _pAreaSeries = new QAreaSeries(); _pAreaSeries->setName("面積1"); _pLineSeries->setPointsVisible(true); _pLineSeries->setPointLabelsFormat("(@xPoint,@yPoint)"); _pAreaSeries->setUpperSeries(_pLineSeries); _pChart->addSeries(_pAreaSeries); _pAreaSeries2 = new QAreaSeries(); _pAreaSeries2->setName("面積2"); _pAreaSeries2->setUpperSeries(_pLineSeries2); _pChart->addSeries(_pAreaSeries2); _pAreaSeries3 = new QAreaSeries(); _pAreaSeries3->setName("面積3"); _pAreaSeries3->setUpperSeries(_pLineSeries3); _pAreaSeries3->setLowerSeries(_pLineSeries2); _pChart->addSeries(_pAreaSeries3); _pAreaSeries4 = new QAreaSeries(); _pAreaSeries4->setName("面積4"); _pAreaSeries4->setUpperSeries(_pLineSeries4); _pAreaSeries4->setLowerSeries(_pLineSeries3); _pChart->addSeries(_pAreaSeries4);
<br>
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/108240696
Demo核心代碼解析
創建QChart顯示框架
AreaChartWidget::AreaChartWidget(QWidget *parent) : QWidget(parent), _pChartView(0), _pChart(0), _pXValueAxis(0), _pYValueAxis(0), _pLegend(0), _pLineSeries(0), _pLineSeries2(0), _pLineSeries3(0), _pLineSeries4(0), _pAreaSeries(0), _pAreaSeries2(0), _pAreaSeries3(0), _pAreaSeries4(0) { _pChartView = new QChartView(this); _pChart = new QChart(); initData(); }
初始化數據
void AreaChartWidget::initData() { _pLineSeries = new QLineSeries; _pLineSeries2 = new QLineSeries; _pLineSeries3 = new QLineSeries; _pLineSeries4 = new QLineSeries; // 方式一:逐一添加,大批量數據較慢 _pLineSeries->append(0, qrand()%11); _pLineSeries->append(1, qrand()%11); _pLineSeries->append(2, qrand()%11); _pLineSeries->append(3, qrand()%11); _pLineSeries->append(4, qrand()%11); _pLineSeries->append(5, qrand()%11); _pLineSeries->append(6, qrand()%11); _pLineSeries->append(7, qrand()%11); _pLineSeries->append(8, qrand()%11); _pLineSeries->append(9, qrand()%11); _pLineSeries->append(10, qrand()%11); _pLineSeries->setName("通道1"); _pLineSeries->setPen(QPen(QColor(qrand()%256, qrand()%256, qrand()%256), 2)); // 通用:將數據插入到圖表中 _pAreaSeries = new QAreaSeries(); _pAreaSeries->setName("面積1"); _pLineSeries->setPointsVisible(true); _pLineSeries->setPointLabelsFormat("(@xPoint,@yPoint)"); _pAreaSeries->setUpperSeries(_pLineSeries); _pChart->addSeries(_pAreaSeries); // 方式二:逐一添加,大批量數據插入 QList<QLineSeries *> list; list.append(_pLineSeries); list.append(_pLineSeries2); list.append(_pLineSeries3); list.append(_pLineSeries4); for(int index = 1; index < 4; index++) { QList<QPointF> listPointF; for(int index = 0; index < 11; index++) { listPointF << QPointF(index, qrand()%11); } list.at(index)->append(listPointF); list.at(index)->setName(QString("通道%1").arg(index+1)); list.at(index)->setPen(QPen(QColor(qrand()%256, qrand()%256, qrand()%256), 2)); } // 通用:將數據插入到圖表中 _pAreaSeries2 = new QAreaSeries(); _pAreaSeries2->setName("面積2"); _pAreaSeries2->setUpperSeries(_pLineSeries2); _pChart->addSeries(_pAreaSeries2); _pAreaSeries3 = new QAreaSeries(); _pAreaSeries3->setName("面積3"); _pAreaSeries3->setUpperSeries(_pLineSeries3); _pAreaSeries3->setLowerSeries(_pLineSeries2); _pChart->addSeries(_pAreaSeries3); _pAreaSeries4 = new QAreaSeries(); _pAreaSeries4->setName("面積4"); _pAreaSeries4->setUpperSeries(_pLineSeries4); _pAreaSeries4->setLowerSeries(_pLineSeries3); _pChart->addSeries(_pAreaSeries4); // 通用:X軸和Y軸的處理(先插入數據再處理軸,不然不會有軸) _pChart->createDefaultAxes(); _pYValueAxis = dynamic_cast<QValueAxis *>(_pChart->axisY()); // _pYValueAxis = new QValueAxis(_pChart); _pYValueAxis->setRange(0, 10); _pYValueAxis->setLinePen(QPen(Qt::black, 1)); // tick _pYValueAxis->setTickCount(5); _pYValueAxis->setGridLinePen(QPen(Qt::gray, 1)); _pYValueAxis->setGridLineVisible(true); // subTick _pYValueAxis->setMinorTickCount(4); _pYValueAxis->setMinorGridLineVisible(true); _pYValueAxis->setLabelFormat("%d"); // _pChart->addAxis(_pYValueAxis, Qt::AlignLeft); _pXValueAxis = dynamic_cast<QValueAxis *>(_pChart->axisX()); // _pXValueAxis = new QValueAxis(_pChart); _pXValueAxis->setRange(0, 10); _pXValueAxis->setLinePen(QPen(Qt::black, 1)); // tick _pXValueAxis->setTickCount(5); _pXValueAxis->setGridLinePen(QPen(Qt::gray, 1)); _pXValueAxis->setGridLineVisible(true); // subTick _pXValueAxis->setMinorTickCount(4); // 相反 _pXValueAxis->setMinorGridLineVisible(true); _pXValueAxis->setLabelFormat("%d s"); // _pChart->addAxis(_pXValueAxis, Qt::AlignBottom); // 通用:視圖顯示設置爲圖表 _pChartView->setRubberBand(QChartView::NoRubberBand); // 不縮放 _pChartView->setDragMode(QChartView::NoDrag); // 拽拖:須要本身重寫QCharView _pChartView->setChart(_pChart); // 標識 _pLegend = _pChart->legend(); _pLegend->setAlignment(Qt::AlignRight); // 平滑 _pChartView->setRenderHint(QPainter::Antialiasing, true); // 陰影 _pChart->setDropShadowEnabled(true); }
設置數據線是否顯示(標籤顯示會同步)
void AreaChartWidget::setDataVisible(int index, bool visible) { if(index < 0 || index > 3) { return; } QList<QAreaSeries *> list; list.append(_pAreaSeries); list.append(_pAreaSeries2); list.append(_pAreaSeries3); list.append(_pAreaSeries4); list.at(index)->setVisible(visible); }
設置主題樣式
void AreaChartWidget::setTheme(QChart::ChartTheme theme) { _pChart->setTheme(theme); }
設置動畫模式
void AreaChartWidget::setAnimationOptions(QChart::AnimationOption option) { _pChart->setAnimationOptions(option); }
設置標籤顯示位置
void AreaChartWidget::setAlignment(Qt::Alignment align) { _pLegend->setAlignment(align); }
設置標籤是否可見
void AreaChartWidget::setLegendVisible(bool visible) { _pLegend->setVisible(visible); _pChartView->setRenderHint(QPainter::Antialiasing); }
設置是否繪製平滑
void AreaChartWidget::setAntialiasing(bool antialiasing) { _pChartView->setRenderHint(QPainter::Antialiasing, antialiasing); }
設置是否有陰影
void AreaChartWidget::setShadow(bool shadow) { _pChart->setDropShadowEnabled(shadow); }
是否顯示數據點(新增)
void AreaChartWidget::setPointVisible(bool visible) { _pAreaSeries->setPointsVisible(visible); _pAreaSeries2->setPointsVisible(visible); _pAreaSeries3->setPointsVisible(visible); _pAreaSeries4->setPointsVisible(visible); }
是否顯示數據點座標(新增)
void AreaChartWidget::setPointLabelVisible(bool visible) { _pAreaSeries->setPointLabelsFormat("(@xPoint,@yPoint)"); _pAreaSeries->setPointLabelsVisible(visible); _pAreaSeries2->setPointLabelsFormat("(@xPoint,@yPoint)"); _pAreaSeries2->setPointLabelsVisible(visible); _pAreaSeries3->setPointLabelsFormat("(@xPoint,@yPoint)"); _pAreaSeries3->setPointLabelsVisible(visible); _pAreaSeries4->setPointLabelsFormat("(@xPoint,@yPoint)"); _pAreaSeries4->setPointLabelsVisible(visible); }
重置隨機數據
void AreaChartWidget::resetData() { _pChart->removeAllSeries(); _pLineSeries = new QLineSeries; _pLineSeries2 = new QLineSeries; _pLineSeries3 = new QLineSeries; _pLineSeries4 = new QLineSeries; QList<QLineSeries *> list; list.append(_pLineSeries); list.append(_pLineSeries2); list.append(_pLineSeries3); list.append(_pLineSeries4); for(int index = 0; index < 4; index++) { QList<QPointF> listPointF; for(int index = 0; index < 11; index++) { listPointF << QPointF(index, qrand()%11); } list.at(index)->append(listPointF); list.at(index)->setName(QString("通道%1").arg(index+1)); list.at(index)->setPen(QPen(QColor(qrand()%256, qrand()%256, qrand()%256), 2)); } _pAreaSeries = new QAreaSeries(); _pAreaSeries->setName("面積1"); _pAreaSeries->setUpperSeries(_pLineSeries); _pAreaSeries2 = new QAreaSeries(); _pAreaSeries->setName("面積2"); _pAreaSeries2->setUpperSeries(_pLineSeries2); _pChart->addSeries(_pAreaSeries2); _pAreaSeries3 = new QAreaSeries(); _pAreaSeries3->setName("面積3"); _pAreaSeries3->setUpperSeries(_pLineSeries3); _pAreaSeries3->setLowerSeries(_pLineSeries2); _pChart->addSeries(_pAreaSeries3); _pAreaSeries4 = new QAreaSeries(); _pAreaSeries4->setName("面積4"); _pAreaSeries4->setUpperSeries(_pLineSeries3); _pAreaSeries4->setLowerSeries(_pLineSeries4); _pChart->addSeries(_pAreaSeries4); _pChart->addSeries(_pAreaSeries); resetColor(); }
初始化顏色
void AreaChartWidget::resetColor() { QList<QLineSeries *> list; list.append(_pLineSeries); list.append(_pLineSeries2); list.append(_pLineSeries3); list.append(_pLineSeries4); for(int index = 0; index < list.size(); index++) { list.at(index)->setColor(QColor(qrand()%256, qrand()%256, qrand()%256)); } }
<br>
工程模板:對應版本號v1.1.0
對應版本號v1.1.0 增長面積圖 對摺線圖、曲線圖和麪積圖增長數據點顯示、數據點標籤顯示
<br>
上一篇:《Qt開發技術:QCharts(三)QCharts樣條曲線圖介紹、Demo以及代碼詳解》 下一篇: 敬請期待...