5. QtCharts curve beautification

Article directory

  • Effect
  • ui settings
  • dialog.h
  • dialog.cpp

Effect

ui settings

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QtCharts>
#include <QLineSeries>
#include <QGraphicsScene>
#include <QTimer>
#include <QSplineSeries>
QT_BEGIN_NAMESPACE
namespace Ui {<!-- --> class Dialog; }
QT_END_NAMESPACE

class Dialog : public QDialog
{<!-- -->
    Q_OBJECT

public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();

private:
    Ui::Dialog *ui;

private:
    /**
     * @brief Get data and internally simulate production change data
     * @param[in]x X coordinate
     * @return the data corresponding to x
     */
    qreal getData_1(qreal x);
    qreal getData_2(qreal x);
    /**
     * @brief set style
     *
     */
    void changeStyle();

    /**
     * @brief Set the form palette
     *
     */
    void setDialogPalette();

public:
    QChart* m_chart;//Build chart object
    QSplineSeries* m_splineSerise1;
    QSplineSeries* m_splineSerise2;
    QGraphicsScene* m_pScene;
    QTimer* m_timer;//Timer
    QValueAxis* m_axisX;//X coordinate axis
    QValueAxis* m_axisY;//Y coordinate axis
    qint64 m_tmLast;//The last time the timer entered
public slots:


};
#endif // DIALOG_H

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include <QString>

const quint32 c_MaxSize=1000;//Number of data

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
    ,m_tmLast(0)
    ,m_splineSerise1(NULL)
    ,m_splineSerise2(NULL)
{<!-- -->
    ui->setupUi(this);


    //setWindowFlags(Qt::FramelessWindowHint);

    //Construct curve series
    m_splineSerise1=new QSplineSeries(this);
    m_splineSerise2=new QSplineSeries(this);

    //Add data to the polyline, curve one
    qint32 i=0;
    qreal x=0.f;
    for (i=0;i<c_MaxSize;i + + )
    {<!-- -->
        x=i*1.f/c_MaxSize;
        m_splineSerise1->append(i,getData_1(x));
    }

     //Add data to the polyline, curve two
    for ( i=0;i<c_MaxSize;i + + )
    {<!-- -->
        x=i*1.f/c_MaxSize;
        m_splineSerise2->append(i,getData_2(x));
    }

    //Build icon object
    m_chart=new QChart();

    //Note: add it to the chart first and then create the coordinate axis, otherwise it will be invalid




    //Build the coordinate axis
    m_axisX = new QValueAxis();
    m_axisX->setRange(0,c_MaxSize);
    m_axisX->setTitleText(QString::fromLocal8Bit("Time"));//Set the title
    m_axisX->setLabelFormat("%g");//Set the format
    m_axisX->setTickCount(5);//Set the number of ticks

    m_axisY= new QValueAxis();
    m_axisY->setRange(-10,10);
    m_axisY->setTitleText(QString::fromLocal8Bit("T"));

    //Bind the coordinate axis
    m_chart->setAxisX(m_axisX,m_splineSerise1);
    m_chart->setAxisY(m_axisY,m_splineSerise1);

    m_chart->setAxisX(m_axisX,m_splineSerise2);
    m_chart->setAxisY(m_axisY,m_splineSerise2);

     //hide legend
     m_chart->legend()->hide();



    //Set icon theme
    m_chart->setTheme(QtCharts::QChart::ChartThemeBlueCerulean);
    //Set title
    m_chart->setTitle(QString("Chart 1"));
    //Set size
    m_chart->setGeometry(0,0,500,300);


    //Build the scene
    m_pScene =new QGraphicsScene(this);

    //Build the scene for the view
    ui->graphicsView->setScene(m_pScene);

    //Add the chart to the scene
    m_pScene->addItem(m_chart);

    //Set anti-aliasing
    ui->graphicsView->setRenderHint(QPainter::Antialiasing,true);

    //Set style
    changeStyle();

    //1. Add the line series to the chart
    m_chart->addSeries(m_splineSerise1);
    m_chart->addSeries(m_splineSerise2);
}

Dialog::~Dialog()
{<!-- -->
    delete ui;
}

qreal Dialog::getData_1(qreal x)
{<!-- -->
    return qSin(x*2*M_PI)*7;//Sine
}

qreal Dialog::getData_2(qreal x)
{<!-- -->
    return qCos(x*2*M_PI)*7;//Cosine
}

void Dialog::changeStyle()
{<!-- -->

    /**
     * Modify form
     *
     */
    //Set the color palette according to the chart's theme
    setDialogPalette();



    /**
     * Modify chart
     *
     */
    m_chart->setBackgroundVisible(true);
    //m_chart->setBackgroundBrush(Qt::transparent);//Set to transparent
    m_chart->setBackgroundBrush(Qt::lightGray);

     QPen penBackground;
     penBackground.setStyle(Qt::DotLine);
     penBackground.setColor(Qt::green);
     m_chart->setBackgroundPen(penBackground);
    /**
     * Modify drawing area
     *
     */
    m_chart->setPlotAreaBackgroundVisible(true);
    m_chart->setPlotAreaBackgroundBrush(Qt::gray);



    /**
     * Modify title
     *
     */

    QFont fontTitle;
    fontTitle.setFamily(QString::fromLocal8Bit("Chinese Amber"));
    fontTitle.setPointSizeF(20.f);
    m_chart->setTitleFont(fontTitle);
    //Set text color
    m_chart->setTitleBrush(Qt::black);


    /**
     * Modify scale
     *
     */

    //Set the scale
    QFont fontAxis;
    fontAxis.setFamily(QString::fromLocal8Bit("Microsoft Yahei"));
    fontAxis.setPointSizeF(12.f);
    m_axisX->setTitleFont(fontAxis);
    m_axisY->setTitleFont(fontAxis);
    //Set text color
    m_axisX->setTitleBrush(Qt::darkMagenta);
    m_axisY->setTitleBrush(Qt::darkMagenta);

    //Set whether to display tick marks
    m_axisX->setGridLineVisible(true);
    m_axisY->setGridLineVisible(true);
    //Set the font coordinate axis

    QFont fontLabel;
    fontLabel.setFamily(QStringLiteral("Microsoft Yahei"));
    fontLabel.setPixelSize(12);
    m_axisX->setLabelsFont(fontLabel);
    m_axisY->setLabelsFont(fontLabel);

    /**
     * Modify legend
     *
     */
    //Alignment
    m_chart->legend()->setAlignment(Qt::AlignLeft);

    /**
     * series
     *
     */

    QPen pn1(Qt::green,2.f);
    m_splineSerise1->setPen(pn1);

    QPen pn2(Qt::cyan,2.f);
    m_splineSerise2->setPen(pn2);

    /**
     * Turn on animation
     *
     */

    QChart::AnimationOptions aniOptions=QChart::AllAnimations;
    m_chart->setAnimationOptions(aniOptions);

}

void Dialog::setDialogPalette()
{<!-- -->
    QChart::ChartTheme theme=QChart::ChartThemeBlueIcy;
    m_chart->setTheme(theme);
    //Determine the Dialog's color palette based on the selected theme
    QPalette pal=window()->palette();
    switch(theme)
    {<!-- -->
       case QtCharts::QChart::ChartThemeLight:
        pal.setColor(QPalette::Window,QRgb(0xf0f0f0));
        pal.setColor(QPalette::WindowText,QRgb(0x404040));
        break;
    case QtCharts::QChart::ChartThemeBlueCerulean:
        pal.setColor(QPalette::Window,QRgb(0x121218));
        pal.setColor(QPalette::WindowText,QRgb(0x6d6d6));
        break;
    case QtCharts::QChart::ChartThemeDark:
        pal.setColor(QPalette::Window,QRgb(0xf0f0f0));
        pal.setColor(QPalette::WindowText,QRgb(0x404040));
        break;
    case QtCharts::QChart::ChartThemeBrownSand:
        pal.setColor(QPalette::Window,QRgb(0xf0f0f0));
        pal.setColor(QPalette::WindowText,QRgb(0x404040));
        break;
    case QtCharts::QChart::ChartThemeBlueNcs:
        pal.setColor(QPalette::Window,QRgb(0xf0f0f0));
        pal.setColor(QPalette::WindowText,QRgb(0x404040));
        break;
    case QtCharts::QChart::ChartThemeHighContrast:
        pal.setColor(QPalette::Window,QRgb(0xf0f0f0));
        pal.setColor(QPalette::WindowText,QRgb(0x404040));
        break;
    case QtCharts::QChart::ChartThemeBlueIcy:
        pal.setColor(QPalette::Window,QRgb(0xf0f0f0));
        pal.setColor(QPalette::WindowText,QRgb(0x404040));
        break;
    case QtCharts::QChart::ChartThemeQt:
        pal.setColor(QPalette::Window,QRgb(0xf0f0f0));
        pal.setColor(QPalette::WindowText,QRgb(0x404040));
        break;
    default:
        pal.setColor(QPalette::Window,QRgb(0xf0f0f0));
        pal.setColor(QPalette::WindowText,QRgb(0x404040));
        break;
    }
    window()->setPalette(pal);
}