1.折线图
- 1、QChart 类继承自 QGraphicsWidget,用于管理图表、图例和轴。
- 2、QValueAxis 类专门用来自定义图表中 X 和 Y 坐标轴。
- 3、QLineSeries 类专门用于折线图(曲线)的形式展示数据
//.pro
QT += core gui charts
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QtCharts>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
//自定义函数实现折线图
void myselfLineSeriesFunc();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
//.c
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//调用此函数
myselfLineSeriesFunc();
}
Widget::~Widget()
{
delete ui;
}
void Widget::myselfLineSeriesFunc()
{
//实例化图表QChart对象
QChart * chart = new QChart;
chart->setTitle("全球各国军事增长折线图");
ui->graphicsView->setChart(chart);
//设置X坐标
QValueAxis * axisX = new QValueAxis;
axisX->setTitleText("年份");
axisX->setRange(2017,2021);//设置范围
//axisX->setTickCount(6);//这只刻度个数
axisX->setLineVisible(true);
axisX->setGridLineVisible(true);//设置网格线是否可见
//设置Y坐标
QValueAxis * axisY = new QValueAxis;
axisY->setTitleText("军事(万亿:美元)");
axisY->setRange(2000,10000);//设置范围
axisY->setTickCount(5);//这只刻度个数
axisY->setLineVisible(true);
axisY->setGridLineVisible(true);//设置网格线是否可见
//绘制折线图,设置序列
QLineSeries * sris1 =new QLineSeries;
sris1->setName("中国");
sris1->setColor(QColor(255,0,0));//设置序列颜色值
//添加数据点到序列
sris1->append(2017,5000);
sris1->append(2018,6500);
sris1->append(2019,4900);
sris1->append(2020,8000);
sris1->append(2021,8900);
sris1->append(2022,9000);
QLineSeries * sris2 =new QLineSeries;
sris2->setName("美国");
sris2->setColor(QColor(0,255,0));//设置序列颜色值
//添加数据点到序列
sris2->append(2017,6500);
sris2->append(2018,7300);
sris2->append(2019,7500);
sris2->append(2020,8000);
sris2->append(2021,8700);
sris2->append(2022,9200);
QLineSeries * sris3 =new QLineSeries;
sris3->setName("俄罗斯");
sris3->setColor(QColor(0,0,255));//设置序列颜色值
//添加数据点到序列
sris3->append(2017,4000);
sris3->append(2018,8000);
sris3->append(2019,7900);
sris3->append(2020,8000);
sris3->append(2021,8200);
sris3->append(2022,9500);
//为QChart添加序列
chart->addSeries(sris1);
chart->addSeries(sris2);
chart->addSeries(sris3);
//将序列设置到坐标轴上
chart->setAxisX(axisX,sris1);
chart->setAxisY(axisY,sris1);
chart->setAxisX(axisX,sris2);
chart->setAxisY(axisY,sris2);
chart->setAxisX(axisX,sris3);
chart->setAxisY(axisY,sris3);
}
2.柱形图
- 1、QBarSet 类专用于条形图中的一组条形集。
- 2、QStackedBarSeries 类在 QChart 中用于创建堆积柱状图。
- 3、QBarCategoryAxis 类用于提供柱状图的轴。
//.pro
QT += charts
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QChart>
using namespace QtCharts;
#include <QBarSet>
#include <QBarSeries>
#include <QBarCategoryAxis>
#include <QChartView>
#include <QValueAxis>
#include <QStackedBarSeries>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
//.c
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//创建条形集对象
QBarSet * ba = new QBarSet("bar");
*ba<<560<<710<<599<<688<<701;
QStackedBarSeries *sris = new QStackedBarSeries();
sris->append(ba);
QChart * chart = new QChart();
chart->addSeries(sris);
chart->setTitle("某某学校实验班高考成绩分数");
QStringList strlist = {"小王","小李","小吴","小张","小赵"};
//通过QBarCategoryAxis类将类型添加到图表的轴
QBarCategoryAxis * axisX = new QBarCategoryAxis();
axisX->append(strlist);
axisX->setTitleText("考生姓名");
chart->addAxis(axisX,Qt::AlignBottom);
//通过QBarCategoryAxis类将类型添加到图表的轴
QValueAxis * axisY = new QValueAxis();
axisY->setRange(0,750);
axisY->setTitleText("高考总分阶梯");
chart->addAxis(axisY,Qt::AlignLeft);
sris->attachAxis(axisX);
sris->attachAxis(axisY);
//图例展示
chart->legend()->setVisible(false);//设置绘图区与图表边界
chart->legend()->setAlignment(Qt::AlignBottom);
//将图标显示到图表视图区
QChartView * chartView = new QChartView(chart);
MainWindow w;
w.setWindowTitle("高考成绩--柱状图表展示");
w.setCentralWidget(chartView);
w.resize(800,500);
w.show();
return a.exec();
}
3.饼状图
- 1、QPieSeries 类专用于提供饼图形状。
- 2、QPieSlice 类设置颜色。
//.pro
QT += charts
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QChart>
#include <QtCharts/QPieSeries>
#include <QtCharts/QPieSlice>
#include <QtCharts/QChartView>
using namespace QtCharts;
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
//.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//创建饼状图对象
QPieSeries *sris = new QPieSeries();
//新建比例
sris->append("70分以下:30%",30);
sris->append("70分-85分:40%",40);
sris->append("86分-99分:25%",25);
sris->append("100分:5%",5);
sris->setLabelsVisible();//设置标签属性,可见
QPieSlice * sred = sris->slices().at(0);
QPieSlice * sgree = sris->slices().at(1);
QPieSlice * sblue = sris->slices().at(2);
QPieSlice * sblack = sris->slices().at(3);
sred->setColor(QColor(255,0,0,255));
sgree->setColor(QColor(0,255,0,255));
sblue->setColor(QColor(0,0,255,255));
sblack->setColor(QColor(0,0,0,255));
QChart * chart = new QChart();
chart->addSeries(sris);
chart->setTitle("饼图形状--班级考试百分制");
QChartView *chartview = new QChartView(chart);
MainWindow w;
w.setCentralWidget(chartview);
w.resize(600,600);
w.show();
return a.exec();
}