Qt開發技術:QCharts(二)QCharts折線圖介紹、Demo以及代碼詳解

若該文爲原創文章,未經容許不得轉載
原博主博客地址:https://blog.csdn.net/qq21497936
原博主博客導航:http://www.javashuo.com/article/p-wxwjppoc-mo.html
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/107787359
各位讀者,知識無窮而人力有窮,要麼改需求,要麼找專業人士,要麼本身研究
紅胖子(紅模仿)的博文大全:開發技術集合(包含Qt實用技術、樹莓派、三維、OpenCV、OpenGL、ffmpeg、OSG、單片機、軟硬結合等等)持續更新中…(點擊傳送門)app

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

Qt開發技術:QCharts(一)QCharts基本介紹以及圖表框架詳解
Qt開發技術:QCharts(二)QCharts折線圖介紹、Demo以及代碼詳解
 敬請期待...框架


前言

  紅胖子,來也!
  介紹了總體框架,開始動手碼代碼,按照順序,從折線圖開始。動畫


補充

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


Demo

  在這裏插入圖片描述
  在這裏插入圖片描述

Demo下載地址

  CSDN:https://download.csdn.net/download/qq21497936/12685603
  QQ羣:1047134658(點擊「文件」搜索「qChartsTools」,羣內與博文同步更新)spa


折線圖

概述

  QLineSeries類以折線圖的形式顯示數據。
  折線圖用於將信息顯示爲由直線鏈接的一系列數據點。
  在這裏插入圖片描述.net

QLineSeries(折線圖)

概述

  QLineSeries類以折線圖的形式顯示數據。
  折線圖用於將信息顯示爲由直線鏈接的一系列數據點。
  建立基本折線圖:code

// 方式一:逐一添加,大批量數據較慢
QLineSeries* series = new QLineSeries();
series->append(0, 6);
series->append(2, 4);
...
chart->addSeries(series);

  效率更高的方式爲:orm

_pLineSeries = new QLineSeries;
_pLineSeries2 = new QLineSeries;
_pLineSeries3 = new QLineSeries;
_pLineSeries4 = new QLineSeries;
QList<QLineSeries *> listLine;
listLine.append(_pLineSeries);
listLine.append(_pLineSeries2);
listLine.append(_pLineSeries3);
listLine.append(_pLineSeries4);
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));
}

  注意:樣條曲線圖的QLineSeries類的使用請參考QXYSeries的使用。
  QLineSeries類以折線圖的形式顯示數據。
  折線圖用於將信息顯示爲由直線鏈接的一系列數據點。blog

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

概述

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

屬性

  在這裏插入圖片描述


Demo核心代碼解析

創建QChart的顯示框架

LineChartWidget::LineChartWidget(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 LineChartWidget::resizeEvent(QResizeEvent *event)
{
    if(_pChartView)
    {
        _pChartView->setGeometry(rect());
    }
}

初始化數據

void LineChartWidget::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));
    // 通用:將數據插入到圖表中
    _pChart->addSeries(_pLineSeries);
    // 方式二:逐一添加,大批量數據較慢
    QList<QLineSeries *> listLine;
    listLine.append(_pLineSeries);
    listLine.append(_pLineSeries2);
    listLine.append(_pLineSeries3);
    listLine.append(_pLineSeries4);
    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->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");

    _pXValueAxis = dynamic_cast<QValueAxis *>(_pChart->axisX());
    _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");

    // 通用:視圖顯示設置爲圖表
    _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 LineChartWidget::setDataVisible(int index, bool visible)
{
    if(index < 0 || index > 3)
    {
        return;
    }
    QList<QLineSeries *> listLine;
    listLine.append(_pLineSeries);
    listLine.append(_pLineSeries2);
    listLine.append(_pLineSeries3);
    listLine.append(_pLineSeries4);
    listLine.at(index)->setVisible(visible);
}

設置主題樣式

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

設置動畫模式

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

設置標籤顯示位置

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

設置標籤是否可見

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

設置是否繪製平滑

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

設置是否有陰影

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

重置隨機數據

void LineChartWidget::resetData()
{
    _pChart->removeAllSeries();
    _pLineSeries = new QLineSeries;
    _pLineSeries2 = new QLineSeries;
    _pLineSeries3 = new QLineSeries;
    _pLineSeries4 = new QLineSeries;
    QList<QLineSeries *> listLine;
    listLine.append(_pLineSeries);
    listLine.append(_pLineSeries2);
    listLine.append(_pLineSeries3);
    listLine.append(_pLineSeries4);
    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 LineChartWidget::resetColor()
{
    QList<QLineSeries *> listLine;
    listLine.append(_pLineSeries);
    listLine.append(_pLineSeries2);
    listLine.append(_pLineSeries3);
    listLine.append(_pLineSeries4);
    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


原博主博客地址:https://blog.csdn.net/qq21497936
原博主博客導航:http://www.javashuo.com/article/p-wxwjppoc-mo.html
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/107787359

相關文章
相關標籤/搜索