点击上方"蓝字"关注我们
01、QBarSet
1. 首先,需要创建一个名为QBarSet的类。
2. 在QBarSet类中,定义所需的属性和方法。
3. 属性可能包括条形的名称、颜色、值等。
4. 方法可能包括添加条形、删除条形、计算总和等。
5. 确保QBarSet类能够与QBar类协同工作,以便在图表中显示数据。
02、QStackedBarSeries
1. 首先,需要创建一个名为QStackedBarSeries的类。
2. 在QStackedBarSeries类中,定义所需的属性和方法。
3. 属性可能包括系列的名称、颜色、数据集合等。
4. 方法可能包括添加数据、删除数据、计算总和等。
5. 确保QStackedBarSeries类能够与其他图表元素协同工作,以便在图表中显示堆叠条形图。
03、QBarCategoryAxis
1. 首先,需要创建一个名为QBarCategoryAxis的类。
2. 在QBarCategoryAxis类中,定义所需的属性和方法。
3. 属性可能包括轴的名称、颜色、刻度标签等。
4. 方法可能包括设置刻度标签、计算类别宽度等。
5. 确保QBarCategoryAxis类能够与其他图表元素协同工作,以便在图表中显示条形图的类别轴。
04、QValueAxis
1. 首先,需要创建一个名为QValueAxis的类。
2. 在QValueAxis类中,定义所需的属性和方法。
3. 属性可能包括轴的名称、颜色、最小值、最大值等。
4. 方法可能包括设置最小值、设置最大值、计算刻度间隔等。
5. 确保QValueAxis类能够与其他图表元素协同工作,以便在图表中显示条形图的数值轴。
05、简单的堆叠条形图示例 main.cpp
代码语言:javascript复制#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QStackedBarSeries>
#include <QtCharts/QBarSet>
#include <QtCharts/QLegend>
#include <QtCharts/QBarCategoryAxis>
#include <QtCharts/QValueAxis>
//QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//![1]
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");
*set0 << 1 << 2 << 3 << 4 << 5 << 6;
*set1 << 5 << 0 << 0 << 4 << 0 << 7;
*set2 << 3 << 5 << 8 << 13 << 8 << 5;
*set3 << 5 << 6 << 7 << 3 << 4 << 5;
*set4 << 9 << 7 << 5 << 3 << 1 << 2;
//![1]
//![2]
QStackedBarSeries *series = new QStackedBarSeries();
series->append(set0);
series->append(set1);
series->append(set2);
series->append(set3);
series->append(set4);
//![2]
//![3]
QChart *chart = new QChart();
chart->addSeries(series);
chart->setTitle("简单的堆叠条形图示例");
chart->setAnimationOptions(QChart::SeriesAnimations);
//![3]
//![4]
QStringList categories;
categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
QBarCategoryAxis *axisX = new QBarCategoryAxis();
axisX->append(categories);
chart->addAxis(axisX, Qt::AlignBottom);
series->attachAxis(axisX);
QValueAxis *axisY = new QValueAxis();
chart->addAxis(axisY, Qt::AlignLeft);
series->attachAxis(axisY);
//![4]
//![5]
chart->legend()->setVisible(true);
chart->legend()->setAlignment(Qt::AlignBottom);
//![5]
//![6]
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
//![6]
//![7]
QMainWindow window;
window.setWindowTitle("公众号:Qt历险记");
window.setCentralWidget(chartView);
window.resize(420, 300);
window.show();
//![7]
return a.exec();
}
06、演示
07、示例2 main.cpp
代码语言:javascript复制#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QBarSeries>
#include <QtCharts/QBarSet>
#include <QtCharts/QLegend>
#include <QtCharts/QBarCategoryAxis>
#include <QtCharts/QStackedBarSeries>
#include <QtCharts/QValueAxis>
//QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//![1]
QBarSet *low = new QBarSet("Min");
QBarSet *high = new QBarSet("Max");
*low << -52 << -50 << -45.3 << -37.0 << -25.6 << -8.0
<< -6.0 << -11.8 << -19.7 << -32.8 << -43.0 << -48.0;
*high << 11.9 << 12.8 << 18.5 << 26.5 << 32.0 << 34.8
<< 38.2 << 34.8 << 29.8 << 20.4 << 15.1 << 11.8;
//![1]
//![2]
QStackedBarSeries *series = new QStackedBarSeries();
series->append(low);
series->append(high);
//![2]
//![3]
QChart *chart = new QChart();
chart->addSeries(series);
chart->setTitle("温度记录(摄氏度)");
chart->setAnimationOptions(QChart::SeriesAnimations);
//![3]
//![4]
QStringList categories = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
QBarCategoryAxis *axisX = new QBarCategoryAxis();
axisX->append(categories);
axisX->setTitleText("月份");
chart->addAxis(axisX, Qt::AlignBottom);
QValueAxis *axisY = new QValueAxis();
axisY->setRange(-52, 52);
axisY->setTitleText("Temperature [°C]");
chart->addAxis(axisY, Qt::AlignLeft);
series->attachAxis(axisX);
series->attachAxis(axisY);
//![4]
//![5]
chart->legend()->setVisible(true);
chart->legend()->setAlignment(Qt::AlignBottom);
//![5]
//![6]
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
//![6]
//![7]
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(600, 300);
window.show();
//![7]
return a.exec();
}
08、演示
09、QPercentBarSeries
1. 首先,需要创建一个名为QPercentBarSeries的类。
2. 在QPercentBarSeries类中,定义所需的属性和方法。
3. 属性可能包括系列的名称、颜色、数据集合等。
4. 方法可能包括添加数据、删除数据、计算总和等。
5. 确保QPercentBarSeries类能够与其他图表元素协同工作,以便在图表中显示堆叠百分比条形图。
10、示例 main.cpp
代码语言:javascript复制#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QPercentBarSeries>
#include <QtCharts/QBarSet>
#include <QtCharts/QLegend>
#include <QtCharts/QBarCategoryAxis>
#include <QtCharts/QValueAxis>
//QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//![1]
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");
*set0 << 1 << 2 << 3 << 4 << 5 << 6;
*set1 << 5 << 0 << 0 << 4 << 0 << 7;
*set2 << 3 << 5 << 8 << 13 << 8 << 5;
*set3 << 5 << 6 << 7 << 3 << 4 << 5;
*set4 << 9 << 7 << 5 << 3 << 1 << 2;
//![1]
//![2]
QPercentBarSeries *series = new QPercentBarSeries();
series->append(set0);
series->append(set1);
series->append(set2);
series->append(set3);
series->append(set4);
//![2]
//![3]
QChart *chart = new QChart();
chart->addSeries(series);
chart->setTitle("Simple percentbarchart example");
chart->setAnimationOptions(QChart::SeriesAnimations);
//![3]
//![4]
QStringList categories;
categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
QBarCategoryAxis *axisX = new QBarCategoryAxis();
axisX->append(categories);
chart->addAxis(axisX, Qt::AlignBottom);
series->attachAxis(axisX);
QValueAxis *axisY = new QValueAxis();
chart->addAxis(axisY, Qt::AlignLeft);
series->attachAxis(axisY);
//![4]
//![5]
chart->legend()->setVisible(true);
chart->legend()->setAlignment(Qt::AlignBottom);
//![5]
//![6]
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
//![6]
//![7]
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(420, 300);
window.show();
//![7]
return a.exec();
}