1. Preparation
In pro added QT + = charts
Then add the header files in the interface header file and declare a namespace, add:
#include <QtCharts>
QT_CHARTS_USE_NAMESPACE
The graph 2.QChart
FIG plotted need to use the three categories
- QSplineSeries: used to create a curve with a series of data consisting There are similar QPieSeries (pie data) QLineSeries (data polyline).
- QChart: Chart interface for content control chart, color, size, etc.
- QChartView: responsible for displaying QChart
Results are as follows:
Code as follows:
m_chart = new QChart(); QSplineSeries *series1 = new QSplineSeries();//实例化一个QLineSeries对象 series1->setColor(QColor(0,100,255)); series1->append(QPointF(0,qrand()%200)) ; series1->append(QPointF(30,qrand()%200)) ; series1->append(QPointF(60,qrand()%200)) ; series1->append(QPointF(90,qrand()%200)) ; series1->append(QPointF(120,qrand()%200)) ; series1->setName("线条1"); series1->setVisible(true); series1->setPointLabelsFormat("(@xPoint,@yPoint)"); series1->setPointLabelsVisible(true); m_chart->setTheme(QChart::ChartThemeLight);//设置白色主题 m_chart->setDropShadowEnabled(true);//背景阴影 m_chart->addSeries(series1);//Adding to the series QChart m_chart -> setTitleBrush (QBrush (QColor ( 0 , 0 , 255 ))); // set the title Brush m_chart -> setTitleFont (QFont ( " Microsoft elegant black " )); // set title font m_chart - > setTitle ( " graph " ); // Create X and Y axes QValueAxis * = AxisX new new QValueAxis; AxisX -> setRange ( 0 , 150 ); // to setRange (0,1); the dynamic graph calculating the size of AxisX -> setLabelFormat ( "dS% " ); QValueAxis * = AxisY new new QValueAxis; AxisY -> setRange ( 0 , 250 ); // to setRange (0,1); the graph is dynamically calculated size AxisY -> setTitleText ( " value value " ); m_chart -> setAxisX (AxisX, Series1); m_chart -> setAxisY (AxisY, Series1); // m_chart-> createDefaultAxes (); // or create default axis // modify the description style m_chart -> Legend () -> setVisible ( to true ); m_chart-> Legend () -> setAlignment (the Qt :: AlignBottom); // Align bottom m_chart -> Legend () -> setBackgroundVisible ( to true ); // set the background are visible m_chart -> Legend () -> setAutoFillBackground ( to true ); // set the background to automatically fill m_chart -> Legend () -> the setColor (a QColor ( 222 , 233 , 251 )); // set the color m_chart -> Legend () -> setLabelColor (a QColor ( 0 , 100 , 255 ) ); // set the label color m_chart -> Legend () -> setMaximumHeight ( 50 ); QChartView *chartView = new QChartView(m_chart); chartView->setRenderHint(QPainter::Antialiasing); QVBoxLayout *pVLayout = new QVBoxLayout(this); pVLayout->addWidget(chartView); resize(960, 720);
3.QChart of the pie
Draw pie need to use three categories
- QSplineSeries: pie chart data consisting of a series of data used to create there
- QChart: Chart interface for content control chart, color, size, etc.
- QChartView: responsible for displaying QChart
Results are as follows:
code show as below:
m_chart = new QChart(); QPieSeries *series = new QPieSeries(); series->append("水果:30%",3); //添加标签"水果:30%" 和 百分值30% series->append("零食:20%",2); series->append("主食:50%",5); series->setLabelsVisible(true); series->setUseOpenGL(true); series->slices().at(0)->setColor(QColor(13,128,217)); //设置颜色 series->slices().at(0)->setLabelColor(QColor(13,128,217)); series->slices().at(1)->setColor(QColor(69,13,217)); series->slices().at(1)->setLabelColor(QColor(69,13,217));
series->slices().at(2)->setColor(QColor(13,217,152)); series->slices().at(2)->setLabelColor(QColor(13,217,152)); m_chart->setTheme(QChart::ChartThemeLight);//设置白色主题 m_chart->setDropShadowEnabled(true);//背景阴影 m_chart->addSeries(series);//添加系列到QChart上 m_chart->setTitleBrush(QBrush(QColor(0,0,255)));//设置标题Brush m_chart->setTitleFont(QFont("微软雅黑"));//设置标题字体 m_chart->setTitle("饼状图"); //修改说明样式 m_chart->legend()->setVisible(true); m_chart->legend()->setAlignment(Qt::AlignBottom);//底部对齐 m_chart->legend()->setBackgroundVisible(true);//设置背景是否可视 m_chart->legend()->setAutoFillBackground(true);//设置背景自动填充 m_chart->legend()->setColor(QColor(222,233,251));//设置颜色 m_chart->legend()->setLabelColor(QColor(0,100,255));//设置标签颜色 m_chart->legend()->setMaximumHeight(50); QChartView *chartView = new QChartView(m_chart); chartView->setRenderHint(QPainter::Antialiasing); QVBoxLayout *pVLayout = new QVBoxLayout(this); pVLayout->addWidget(chartView); resize(960, 720);
4. QChart之条形图
绘制条形图需要用到4个类
- QBarSet: 一个条形集合
- QBarSeries: 用来封装多个QBarSet的条形数据
- QChart: 图表界面,用来管理图表内容,颜色,大小等
- QChartView: 负责显示QChart
效果如下:
代码如下:
m_chart = new QChart(); //创建3个条线数据 QBarSet *set0 = new QBarSet("零食"); QBarSet *set1 = new QBarSet("水果"); QBarSet *set2 = new QBarSet("主食"); *set0 << 158 << 685 << 458 << 260 << 354; //向零食数据添加这4个月的销售数据 *set1 << 350 << 725 << 602 << 523 << 458; *set2 << 222 << 350 << 598 << 480 << 687; set0->setLabelColor(QColor(0,0,255)); //设置条形数据颜色 set1->setLabelColor(QColor(0,0,255)); set2->setLabelColor(QColor(0,0,255)); QBarSeries *series = new QBarSeries(); series->append(set0); series->append(set1); series->append(set2); series->setVisible(true); series->setLabelsVisible(true); m_chart->setTheme(QChart::ChartThemeLight);//设置白色主题 m_chart->setDropShadowEnabled(true);//背景阴影 m_chart->addSeries(series);//添加系列到QChart上 m_chart->setTitleBrush(QBrush(QColor(0,0,255)));//设置标题Brush m_chart->setTitleFont(QFont("微软雅黑"));//设置标题字体 m_chart->setTitle("超市销售条形图"); //创建X轴和Y轴 QBarCategoryAxis *axisX = new QBarCategoryAxis; axisX->append("一月"); axisX->append("二月"); axisX->append("三月"); axisX->append("四月"); axisX->append("五月"); axisX->setLabelsColor(QColor(7,28,96)); QValueAxis *axisY = new QValueAxis; axisY->setRange(0,1000); //改为setRange(0,1);则表示坐标为动态计算大小的 axisY->setTitleText("价格"); axisY->setLabelFormat("%d$"); m_chart->setAxisX(axisX,series); m_chart->setAxisY(axisY,series); //修改说明样式 m_chart->legend()->setVisible(true); m_chart->legend()->setAlignment(Qt::AlignBottom);//底部对齐 m_chart->legend()->setBackgroundVisible(true);//设置背景是否可视 m_chart->legend()->setAutoFillBackground(true);//设置背景自动填充 m_chart->legend()->setColor(QColor(222,233,251));//设置颜色 m_chart->legend()->setLabelColor(QColor(0,100,255));//设置标签颜色 m_chart->legend()->setMaximumHeight(50); QChartView *chartView = new QChartView(m_chart); chartView->setRenderHint(QPainter::Antialiasing); QVBoxLayout *pVLayout = new QVBoxLayout(this); pVLayout->addWidget(chartView); resize(960, 720);