引用QT官方例子,如下图
基本思路是:
1、先画先QLineSeries,给线加上点QLineSeries
2、再把QLineSeries加到区域序列QAreaSeries里面来
3、把QAreaSeries加到QChart里
4、把QChart加到QChartView里,完工
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//![1]初始化线对象
QLineSeries *series0 = new QLineSeries();
QLineSeries *series1 = new QLineSeries();
//![1]
//![2]给线添加数据
*series0 << QPointF(1, 5) << QPointF(3, 7) << QPointF(7, 6) << QPointF(9, 7) << QPointF(12, 6)
<< QPointF(16, 7) << QPointF(18, 5);
*series1 << QPointF(1, 3) << QPointF(3, 4) << QPointF(7, 3) << QPointF(8, 2) << QPointF(12, 3)
<< QPointF(16, 4) << QPointF(18, 3);
//![2]
//![3]把线添加到区域,并设置样式
QAreaSeries *series = new QAreaSeries(series0, series1);
series->setName("Batman");//设置名字
QPen pen(0x059605);//设置画笔
pen.setWidth(3);//画笔宽带
series->setPen(pen);
series->setPointsVisible(true);//设置点可见
series->setPointLabelsVisible(true);//设置点边缘可见
QLinearGradient gradient(QPointF(0, 0), QPointF(0, 1));
gradient.setColorAt(0.0, 0x3cc63c);
gradient.setColorAt(1.0, 0x26f626);
gradient.setCoordinateMode(QGradient::ObjectBoundingMode);
series->setBrush(gradient);
//![3]
//![4]
QChart *chart = new QChart();
chart->addSeries(series);
chart->setTitle("Simple areachart example");
chart->createDefaultAxes();
chart->axes(Qt::Horizontal).first()->setRange(0, 20);
chart->axes(Qt::Vertical).first()->setRange(0, 10);
//![4]
//![5]
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
//![5]
//![6]
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(400, 300);
window.show();
//![6]
return a.exec();
}