天天看點

Qt開發技術:QCharts(三)QCharts樣條曲線圖介紹、Demo以及代碼詳解

​​Qt開發專欄:開發技術(點選傳送門)​​

上一篇:《​​Qt開發技術:QCharts(二)QCharts折線圖介紹、Demo以及代碼詳解​​》

下一篇:《​​Qt開發技術:QCharts(四)QCharts面積圖介紹、Demo以及代碼詳解​​》

前言

  紅胖子,來也!

  按照順序,本章為樣條曲線圖。

補充

  QCharts所有的圖表都依賴《​​Qt開發技術:QCharts(一)QCharts基本介紹以及圖表架構詳解​​》中的QChart、QChartView、QLegend、QValueAxis。

Demo

  

Qt開發技術:QCharts(三)QCharts樣條曲線圖介紹、Demo以及代碼詳解

  

Qt開發技術:QCharts(三)QCharts樣條曲線圖介紹、Demo以及代碼詳解

Demo下載下傳位址

  CSDN:​​javascript:void(0)​​

  QQ群:1047134658

樣條曲線圖

概述

  折線圖和樣條曲線圖将資料表示為一系列由直線連接配接的資料點。在折線圖中,資料點由直線連接配接,而在樣條曲線圖中,資料點由樣條曲線連接配接。樣條曲線是使用QPainterPath繪制的。

  樣條曲線圖是通過使用繼承QLineSeries的QSplineSeries類或繼承LineSeries的SplineSeries類型實作的。

  

Qt開發技術:QCharts(三)QCharts樣條曲線圖介紹、Demo以及代碼詳解

QSplineSeries(樣條曲線圖)

概述

  QSplineSeries類将資料顯示為樣條曲線圖。

  樣條曲線系列存儲QPainterPath繪制樣條曲線所需的資料點和段控制點。當資料發生變化時,自動計算控制點。該算法計算點以便繪制法向樣條曲線。

  建立樣條曲線圖:

// 方式一:逐一添加,大批量資料較慢
QSplineSeries* pSeries = new QSplineSeries();
pSeries->append(0, 6);
pSeries->append(2, 4);
...
chart->addSeries(pSeries);      

  效率更高的方式為:

// 方式二:逐一添加,大批量資料插入
_pSplineSeries = new QSplineSeries;
_pSplineSeries2 = new QSplineSeries;
_pSplineSeries3 = new QSplineSeries;
_pSplineSeries4 = new QSplineSeries;

QList<QSplineSeries *> listLine;
listLine.append(_pSplineSeries);
listLine.append(_pSplineSeries2);
listLine.append(_pSplineSeries3);
listLine.append(_pSplineSeries4);
for(int index = 1; index < 4; index++)
{
    QList<QPointF> listPointF;
    for(int index = 0; index < 11; index++)
    {
        listPointF << QPointF(index, qrand()%11);
    }
    listLine.at(index)->append(listPointF);
    listLine.at(index)->setName(QString("通道%1").arg(index+1));
    listLine.at(index)->setPen(QPen(QColor(qrand()%256, qrand()%256, qrand()%256), 2));
    // 通用:将資料插入到圖表中
    _pChart->addSeries(listLine.at(index));
}      

  注意:樣條曲線圖的QSplineSeries類的使用請參考QXYSeries的使用。

  QSplineSeries類以樣條曲線圖的形式顯示資料。

QXYSeries(折線圖、樣條曲線圖、散點圖的基類)

概述

  QXYSeries類是折線圖、樣條曲線圖、散點圖的基類,QSplineSeries、QLineSeries、QScatterSeries都參照該類使用。

屬性

  

Qt開發技術:QCharts(三)QCharts樣條曲線圖介紹、Demo以及代碼詳解

Demo核心代碼解析

建立QChart的顯示架構

SplineChartWidget::SplineChartWidget(QWidget *parent) :
    QWidget(parent),
    _pChartView(0),
    _pChart(0),
    _pXValueAxis(0),
    _pYValueAxis(0),
    _pLegend(0),
    _pLineSeries(0),
    _pLineSeries2(0),
    _pLineSeries3(0),
    _pLineSeries4(0)
{
    _pChartView = new QChartView(this);
    _pChart = new QChart();
    initData();
}
void SplineChartWidget::resizeEvent(QResizeEvent *event)
{
    if(_pChartView)
    {
        _pChartView->setGeometry(rect());
    }
}      

(本文章部落格位址:​​javascript:void(0)​​)

初始化資料

void SplineChartWidget::initData()
{
    _pSplineSeries = new QSplineSeries;
    _pSplineSeries2 = new QSplineSeries;
    _pSplineSeries3 = new QSplineSeries;
    _pSplineSeries4 = new QSplineSeries;
    // 方式一:逐一添加,大批量資料較慢
    _pSplineSeries->append(0, qrand()%11);
    _pSplineSeries->append(1, qrand()%11);
    _pSplineSeries->append(2, qrand()%11);
    _pSplineSeries->append(3, qrand()%11);
    _pSplineSeries->append(4, qrand()%11);
    _pSplineSeries->append(5, qrand()%11);
    _pSplineSeries->append(6, qrand()%11);
    _pSplineSeries->append(7, qrand()%11);
    _pSplineSeries->append(8, qrand()%11);
    _pSplineSeries->append(9, qrand()%11);
    _pSplineSeries->append(10, qrand()%11);
    _pSplineSeries->setName("通道1");
    _pSplineSeries->setPen(QPen(QColor(qrand()%256, qrand()%256, qrand()%256), 2));
    // 通用:将資料插入到圖表中
    _pChart->addSeries(_pSplineSeries);

    // 方式二:逐一添加,大批量資料較慢
    QList<QSplineSeries *> listLine;
    listLine.append(_pSplineSeries);
    listLine.append(_pSplineSeries2);
    listLine.append(_pSplineSeries3);
    listLine.append(_pSplineSeries4);
    for(int index = 1; index < 4; index++)
    {
        QList<QPointF> listPointF;
        for(int index = 0; index < 11; index++)
        {
            listPointF << QPointF(index, qrand()%11);
        }
        listLine.at(index)->append(listPointF);
        listLine.at(index)->setName(QString("通道%1").arg(index+1));
        listLine.at(index)->setPen(QPen(QColor(qrand()%256, qrand()%256, qrand()%256), 2));
        // 通用:将資料插入到圖表中
        _pChart->addSeries(listLine.at(index));
    }

    // 通用: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 SplineChartWidget::setDataVisible(int index, bool visible)
{
    if(index < 0 || index > 3)
    {
        return;
    }
    QList<QSplineSeries *> listLine;
    listLine.append(_pSplineSeries);
    listLine.append(_pSplineSeries2);
    listLine.append(_pSplineSeries3);
    listLine.append(_pSplineSeries4);
    listLine.at(index)->setVisible(visible);
}      

設定主題樣式

void SplineChartWidget::setTheme(QChart::ChartTheme theme)
{
    _pChart->setTheme(theme);
}      

設定動畫模式

void SplineChartWidget::setAnimationOptions(QChart::AnimationOption option)
{
    _pChart->setAnimationOptions(option);
}      

設定标簽顯示位置

void SplineChartWidget::setAlignment(Qt::Alignment align)
{
    _pLegend->setAlignment(align);
}      

設定标簽是否可見

void SplineChartWidget::setLegendVisible(bool visible)
{
    _pLegend->setVisible(visible);
    _pChartView->setRenderHint(QPainter::Antialiasing);
}      

設定是否繪制平滑

void SplineChartWidget::setAntialiasing(bool antialiasing)
{
    _pChartView->setRenderHint(QPainter::Antialiasing, antialiasing);
}      

設定是否有陰影

void SplineChartWidget::setShadow(bool shadow)
{
    _pChart->setDropShadowEnabled(shadow);
}      

重置随機資料

void SplineChartWidget::resetData()
{
    _pChart->removeAllSeries();
    _pSplineSeries = new QSplineSeries;
    _pSplineSeries2 = new QSplineSeries;
    _pSplineSeries3 = new QSplineSeries;
    _pSplineSeries4 = new QSplineSeries;
    QList<QSplineSeries *> listLine;
    listLine.append(_pSplineSeries);
    listLine.append(_pSplineSeries2);
    listLine.append(_pSplineSeries3);
    listLine.append(_pSplineSeries4);
    for(int index = 0; index < 4; index++)
    {
        QList<QPointF> listPointF;
        for(int index = 0; index < 11; index++)
        {
            listPointF << QPointF(index, qrand()%11);
        }
        listLine.at(index)->append(listPointF);
        listLine.at(index)->setName(QString("通道%1").arg(index+1));
        listLine.at(index)->setPen(QPen(QColor(qrand()%256, qrand()%256, qrand()%256), 2));
        _pChart->addSeries(listLine.at(index));
    }
    resetColor();
}      

重置随機顔色

void SplineChartWidget::resetColor()
{
    QList<QLineSeries *> listLine;
    listLine.append(_pSplineSeries);
    listLine.append(_pSplineSeries2);
    listLine.append(_pSplineSeries3);
    listLine.append(_pSplineSeries4);
    for(int index = 0; index < listLine.size(); index++)
    {
        listLine.at(index)->setColor(QColor(qrand()%256, qrand()%256, qrand()%256));
    }
}      

工程模闆:對應版本号v1.0.0

  對應版本号v1.0.0