How Qt creates and displays histograms

Create a simple histogram

Step 1: Create a QBarSet object; the QBarSet class represents a set of bars in a bar chart.

QBarSet *set0 = new QBarSet("Jane");
QBarSet *set1 = new QBarSet("John");
QBarSet *set2 = new QBarSet("Axel");
QBarSet *set3 = new QBarSet("Mary");
QBarSet *set4 = new QBarSet("Samantha");

  In this case, we have created five groups, that is to say, there will be five groups of data.

Step 2: Next add data for these five bar groups

*set0 << 1 << 2 << 3 << 4 << 13 << 6;
*set1 << 5 << 0 << 0 << 4 << 15 << 7;
*set2 << 3 << 5 << 8 << 13 << 8 << 5;
*set3 << 5 << 6 << 7 << 3 << 4 << 5;
*set4 << 9 << 7 << 5 << 3 << 1 << 2;

Step 3: Create a QBarSeries object. The QBarSeries class displays a series of data as vertical lines grouped by category.

QBarSeries *series = new QBarSeries();
series->append(set0);
series->append(set1);
series->append(set2);
series->append(set3);
series->append(set4);

Step 4: Create a QChart to hold QBarSeries objects; QChart is a QGraphicsWidget that can be displayed in QGraphicsScene.

QChart *chart = new QChart();
chart->addSeries(series);
chart->setTitle("Simple barchart example");
chart->setAnimationOptions(QChart::SeriesAnimations);

Step 5: Create a QChartView to display the table

QChartView *chartView = new QChartView(chart);

Step 6: Add QChartView to a main window and display it.

QMainWindow window;
window.setCentralWidget(chartView);
window.resize(420, 300);
window.show();

  Then we can see a histogram like the one below when we run it.

 

Histogram optimization

  Although we have successfully displayed a bar graph, it does not look very beautiful, and it will not achieve the desired effect in practical applications. Then we optimize it a little bit.

First add an axis to it

QStringList categories;
categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";  //保存横坐标字符串的列表
QBarCategoryAxis *axis = new QBarCategoryAxis();
axis->append(categories);
chart->createDefaultAxes();  //创建一个默认的坐标轴
chart->setAxisX(axis, series);  //设置X坐标轴

  Then let it display the label data

series->setLabelsPosition(QAbstractBarSeries::LabelsInsideEnd);  //设置标签显示的位置
series->setLabelsVisible(true);  //设置数据标签可见

  In this way, we get a histogram like the one below.

 

  Then we can set a theme for the table

chart->setTheme(QChart::ChartThemeBlueCerulean);

  You can set the position of the setting legend to the bottom

chart->legend()->setAlignment(Qt::AlignBottom);

  In the end, looking at the effect, it looks much better like this, right?


  Finally, how do we display the chart when we want to display it in our layout?

 

  When we tried to add QChartView to a QWidget, we found that we couldn't do it. Here we need to use QHBoxLayout.

  Like this:

//...
QWidget *widget = new QWidget();
QHBoxLayout m_pHLayout = new QHBoxLayout();
m_pHLayout->addWidget(chartView);
widget->setLayout(m_pHLayout);
//...

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

Guess you like

Origin blog.csdn.net/m0_73443478/article/details/130535440