头文件:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtCharts>
#include <QtCharts/QPieSeries>
#include <QtCharts/QPieSlice>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
void fun_pie_chart();
void fun_bar_chart();
~MainWindow();
private slots:
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
.cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtPrintSupport/QPrinter>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
fun_pie_chart();
fun_bar_chart();
}
void MainWindow::fun_pie_chart()
{
// 构造两个饼状分区,A数据显示绿色占60%,B数据显示蓝色占40%
QPieSlice *slice_1 = new QPieSlice(QStringLiteral("A数据"), 0.6, this);
slice_1->setLabelVisible(true); // 显示饼状区对应的数据label
slice_1->setBrush(Qt::green);
QPieSlice *slice_2 = new QPieSlice(QStringLiteral("B数据"), 0.4, this);
slice_2->setLabelVisible(true);
slice_2->setBrush(Qt::blue);
// 将两个饼状分区加入series
QPieSeries *series = new QPieSeries(this);
series->append(slice_1);
series->append(slice_2);
QChart *chart = new QChart();
chart->addSeries(series);
//chart->setAnimationOptions(QChart::AllAnimations); // 设置显示时的动画效果
chart->setTheme(QChart::ChartThemeBlueCerulean);
QChartView *chartview = new QChartView(this);
chartview->setChart(chart);
chartview->show();
ui->verticalLayout_pie->insertWidget(0, chartview);
//ui->verticalLayoutWidget->hide();
QPixmap p = QPixmap::grabWidget(chartview);
QImage image=p.toImage();
image.save("E:\\qt\\wml\\chart_pie.png");
}
void MainWindow::fun_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");
//设置单个柱状条的大小
*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;
//柱状条添加到序列
QBarSeries *series = new QBarSeries();
series->append(set0);
// series->append(set1);
// series->append(set2);
// series->append(set3);
// series->append(set4);
//柱状条序列添加到QChart
QChart *chart = new QChart();
chart->addSeries(series);
chart->setTitle(" 柱状图表");
//chart->setAnimationOptions(QChart::SeriesAnimations); //动画效果
//X轴设置名称
QStringList categories;
categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
QBarCategoryAxis *axisX = new QBarCategoryAxis();
axisX->append(categories);
chart->addAxis(axisX, Qt::AlignBottom);
series->attachAxis(axisX);
//Y轴设置
QValueAxis *axisY = new QValueAxis();
axisY->setRange(0, 15);
chart->addAxis(axisY, Qt::AlignLeft);
series->attachAxis(axisY);
//设置图例
chart->legend()->setVisible(false);
chart->legend()->setAlignment(Qt::AlignBottom); //下对齐
chart->setTheme(QChart::ChartThemeBlueCerulean); //主题
//设置渲染模式
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing); //消除边缘
//显示QChart
ui->verticalLayout_bar->insertWidget(0,chartView);
//ui->verticalLayoutWidget_2->hide();
QPixmap p = QPixmap::grabWidget(chartView);
QImage image=p.toImage();
image.save("E:\\qt\\wml\\chart_bar.png");
}
MainWindow::~MainWindow()
{
delete ui;
}