柱状图显示数据(需要在.pro文件引入 “QT += core gui sql charts”)
QBarSet *set0 = new QBarSet("对象0");
QBarSet *set1 = new QBarSet("对象1");
//输入数据
*set0 <<1102<< 1054<< 1013<< 989<< 941<< 920<< 893<< 951<< 980<< 1016<< 1056 <<1088;
*set1 <<936<< 895<< 861<< 841<< 800<< 782<< 759<< 809<< 833<< 864<< 898<< 925;
QBarSeries *series = new QBarSeries();
series->append(set0);
series->append(set1);
QChart *chart = new QChart();
chart->addSeries(series);
chart->setTitle("xxx"); //xxx为图表名称会在图中显示,不同于界面名称
chart->setAnimationOptions(QChart::SeriesAnimations);
//设置x,y轴坐标轴名字
QValueAxis *axisx = new QValueAxis;
axisx->setTitleText("X轴");//名称任意设置
chart->setAxisX(axisx, series);
QValueAxis *axisy = new QValueAxis;
axisy->setTitleText("Y轴");//名称任意设置
chart->setAxisY(axisy,series);
//设置横轴刻度线
QStringList categories;
categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "July" << "Aug" << "Sept" << "Ocr" <<"Nove" << "Dec";
QBarCategoryAxis *axis = new QBarCategoryAxis();
axis->append(categories);
chart->createDefaultAxes();//创建默认的左侧的坐标轴(根据 QBarSet 设置的值)
chart->setAxisX(axis, series);//设置坐标轴
chart->legend()->setVisible(true); //设置图例为显示状态
chart->legend()->setAlignment(Qt::AlignBottom);//设置图例的显示位置在底部
chart->setTheme(QChart::ChartThemeBlueCerulean);//设置背景,背景有几种默认选择
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
this->setCentralWidget(chartView);
this->resize(800, 600); //调整界面大小
曲线显示数据(需要在.pro文件引入 “QT += core gui sql charts”)
QChartView * _chart_view = new QChartView;
this->setCentralWidget(_chart_view);
//自定义背景
//_chart_view->chart()->setBackgroundVisible(false);
//setStyleSheet(R"(QGraphicsView{ background-image:url(:/tubiao/33.png);})");
/* 以下代码完成曲线添加 */
QLineSeries *seriesx = new QLineSeries();
for(int i=1;i<=5223;i=i+1) {
seriesx->append(i,a[i]); //数组a中即为要显示的数据值,个数与i的最大范围统一
}
_chart_view->chart()->addSeries(seriesx);
//chart的背景和ui文件的背景不同
_chart_view->chart()->setTheme(QChart::ChartThemeBlueCerulean);
//_chart_view->chart()->setTheme(QChart::Ch)
seriesx->setName("xxx");//xxx为图中显示的点名称
_chart_view->chart()->setTitle("xxxx"); //xxxx为显示曲线的名称
//设置x,y轴坐标轴名字
QValueAxis *axisx = new QValueAxis;
axisx->setTitleText("xxxx"); //xxxx为x轴名称
_chart_view->chart()->setAxisX(axisx, seriesx);
QValueAxis *axisy = new QValueAxis;
axisy->setTitleText("xxxx"); //xxxx为y轴名称
_chart_view->chart()->setAxisY(axisy,seriesx);
_chart_view->chart()->legend()->setVisible(true); //使曲线标题可视
_chart_view->chart()->legend()->setAlignment(Qt::AlignBottom);//将线名放于底部,参数可调
this->resize(800, 600);