QtChart可用于绘制折线图,在折线图中,数据点通过直线连接。要想绘制折线图至少要用到QLineSeries、QChart、QChartView这三个类。QLineSeries类用于创建有由一系列点组成的折线;QChart类用于控制整个图表界面的设置,例如坐标轴、主题、背景色、标题、legend等;QChartView类用于显示图表,将图表显示到QWidget或者QGraphicsWidget上。
打开Qt,新建一个Qt Widgets Application项目,使用QtChart绘制图表需要在pro文件中添加QT += charts,并且还需要在编写代码文件中添加名称空间QT_CHARTS_USE_NAMESPACE,一般放在头文件的后面,Pro文件如下所示:
#-------------------------------------------------
#
# Project created by QtCreator 2018-12-10T12:52:02
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
QT += charts
TARGET = LineChart
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
然后在main.cpp文件中添加如下代码,代码已经注释的很详细了:
#include "mainwindow.h"
#include
#include //显示图表
#include //线系列
#include
QT_CHARTS_USE_NAMESPACE//QtChart名空间
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
QList m_series;
QLineSeries *series1 = new QLineSeries();//实例化一个QLineSeries对象
QLineSeries *series2 = new QLineSeries();//实例化一个QLineSeries对象
m_series.append(series1);
m_series.append(series2);
//设置线条名称
series1->setName(QString("line " + QString::number(1)));
series2->setName(QString("line " + QString::number(2)));
//series->setName(QString("line " + QString::number(m_series.count())));
//设置线条颜色,如果不设置会给默认颜色
series1->setColor(QColor(255,0,255));
series2->setColor(QColor(0,255,255));
//series1->setBrush(QColor(255,0,255));
//series1->setPen(QColor(255,0,255));
//设置是否线条可视
series1->setVisible(true);
series2->setVisible(true);
//点标签是否可视
series1->setPointLabelsVisible(true);
series2->setPointLabelsVisible(true);
//点标签颜色
series1->setPointLabelsColor(QColor(255,255,255));
series2->setPointLabelsColor(QColor(255,255,255));
//点标签字体
series1->setPointLabelsFont(QFont("微软雅黑"));
series2->setPointLabelsFont(QFont("微软雅黑"));
//设置点标签显示格式
series1->setPointLabelsFormat("(@xPoint,@yPoint)");
series2->setPointLabelsFormat("(@xPoint,@yPoint)");
//是否切割边缘点标签,默认为true
series1->setPointLabelsClipping(false);
series2->setPointLabelsClipping(true);
//设置点标签是否可视
series1->setPointsVisible(true);
series2->setPointsVisible(true);
//添加坐标点
series1->append(0, 6);
series1->append(2, 4);
series1->append(3, 8);
series1->append(7, 4);
series1->append(10, 5);
*series1 << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2);
series2->append(0, 8);
series2->append(2, 5);
series2->append(3, 3);
series2->append(7, 9);
series2->append(10, 2);
*series2 << QPointF(11, 3) << QPointF(13, 8) << QPointF(17, 2) << QPointF(18, 5) << QPointF(20, 7);
QChart *chart = new QChart();
chart->setTheme(QChart::ChartThemeBlueCerulean);//设置系统主题
chart->setAnimationOptions(QChart::AllAnimations);//设置启用或禁用动画
//chart->setBackgroundBrush(QBrush(QColor(170,170,255)));//设置背景色,主题和背景二选一
//chart->setDropShadowEnabled(true);//是否背景阴影
chart->setLocalizeNumbers(true);//数字是否本地化
//chart->legend()->show();//legend是否显示,show和hide
chart->addSeries(series1);//添加系列到QChart上
chart->addSeries(series2);//添加系列到QChart上
chart->createDefaultAxes();//创建默认轴
chart->setTitle("Simple line chart example");//设置标题
chart->setTitleBrush(QBrush(QColor(255,170,255)));//设置标题Brush
chart->setTitleFont(QFont("微软雅黑"));//设置标题字体
chart->legend()->setVisible(true);
chart->legend()->setAlignment(Qt::AlignBottom);//底部对齐
//chart->legend()->detachFromChart();
chart->legend()->setBackgroundVisible(true);//设置背景是否可视
//chart->legend()->setAutoFillBackground(true);//设置背景自动填充
//chart->legend()->setColor(QColor(255,128,128,128));//设置颜色
//chart->legend()->setContentsMargins(10,10,10,10);//设置边距left,top,right,bottom
chart->legend()->setLabelColor(QColor(255,128,255));//设置标签颜色
chart->legend()->setVisible(true);//设置是否可视
//chart->legend()->setMaximumHeight(50);
//chart->legend()->setMaximumWidth(120);
//chart->legend()->setMaximumSize(10000);
//chart->legend()->setGeometry(50,50,120,50);//设置几何位置x,y,w,h
//chart->legend()->setBrush(QBrush(QColor(128,128,128,128)));
//chart->legend()->setPen(QPen(QColor(192,192, 192,192)));
chart->legend()->setBorderColor(QColor(255,255,170,185));//设置边框颜色
QFont font = chart->legend()->font();
font.setItalic(!font.italic());
chart->legend()->setFont(font);//设置字体为斜体
font.setPointSizeF(12);
chart->legend()->setFont(font);//设置字体大小
chart->legend()->setFont(QFont("微软雅黑"));//设置字体类型
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
w.setCentralWidget(chartView);
w.resize(960, 720);
w.show();
return a.exec();
}
CSDN下载:https://download.csdn.net/download/mars_xiaolei/10840847
百度网盘链接:https://pan.baidu.com/s/1sYzagekYPpEz41cHRfctQg
提取码:ptui