文章目录提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
- 前言
- 一、头文件
- 二、cpp文件
- 效果展示
前言
自己写的简易柱状图
搭配QT5 加上 VS2015
注意:.pro文件中一定要加入QT +=charts
头文件里也必须加上:
QT_CHARTS_USE_NAMESPACE
下面直接上代码,注释全在文中
提示:以下是本篇文章正文内容,下面案例可供参考
#ifndef CDRAWAHISTOGRAM_H
#define CDRAWAHISTOGRAM_H
#include
#include
#include
#include
#include
#include
#pragma execution_character_set(“utf-8”)
#include
QT_CHARTS_USE_NAMESPACE
namespace Ui {
class CDrawAHistogram;
}
class CDrawAHistogram : public QWidget
{
Q_OBJECT
public:
explicit CDrawAHistogram(QWidget *parent = 0);
~CDrawAHistogram();
private:
Ui::CDrawAHistogram *ui;
};
#endif // CDRAWAHISTOGRAM_H
二、cpp文件#include “cdrawahistogram.h”
#include “ui_cdrawahistogram.h”
CDrawAHistogram::CDrawAHistogram(QWidget *parent) :
QWidget(parent),
ui(new Ui::CDrawAHistogram)
{
ui->setupUi(this);
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"); //定义各个成员
for(int i=0;i<6;i++)
{
set0->append(i);
}
for(int i=1;i<7;i++)
{
set1->append(i);
}
for(int i=2;i<8;i++)
{
set2->append(i);
}
for(int i=3;i<9;i++)
{
set3->append(i);
}
for(int i=4;i<10;i++)
{
set4->append(i);
}
QBarSeries *series = new QBarSeries(); //定义一个系列类
series->append(set0);
series->append(set1);
series->append(set2);
series->append(set3);
series->append(set4); //把上面5个成员的 6个月的值放到 series 待会显示
// QValueAxis *xAxis = new QValueAxis();
QValueAxis *yAxis = new QValueAxis();
// xAxis->setRange(-1, 6);
yAxis->setRange(0, 25);
QChart *chart = new QChart(); // 实例化QChart
chart->addSeries(series); //往Chart里添加series
chart->setTitle("图例显示"); //设置表格头
chart->setAnimationOptions(QChart::SeriesAnimations);
QStringList categories; //创建字符串List
categories.append("Jan");
categories.append("Feb");
categories.append("Mar");
categories.append("Apr");
categories.append("May");
categories.append("Jun"); //建立横坐标
QBarCategoryAxis *axis = new QBarCategoryAxis(); //实例化坐标轴文字标识轴
axis->append(categories); //将字符串列表append到文字标识轴里
// chart->createDefaultAxes();//创建默认的坐标轴(笛卡尔坐标),一定要有这一句
chart->setAxisX(axis, series);//设置坐标轴
chart->legend()->setVisible(true); //设置图例为显示状态
//显示容器 QChartView
QChartView *chartView = new QChartView(chart); //添加Chart到QChartView上
chartView->setRenderHint(QPainter::Antialiasing);
// chart->addAxis(xAxis, Qt::AlignBottom);
chart->addAxis(yAxis, Qt::AlignLeft); //把坐标轴连接上去
// series->attachAxis(xAxis);
series->attachAxis(yAxis); //把数据和线连接上去
ui->horizontalLayout->addWidget(chartView);
}
CDrawAHistogram::~CDrawAHistogram()
{
delete ui;
}
。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)