创建折线图

注: 这是 "带 Widgets 图库的图表"示例的一部分。

要创建折线图,需要一个QLineSeries 实例。让我们创建一个。

auto series = new QLineSeries;

然后向系列中添加数据。我们可以使用 append() 成员函数或流操作符。

series->append(0, 6);
series->append(2, 4);
series->append(3, 8);
series->append(7, 4);
series->append(10, 5);
*series << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2);

要在图表中显示数据,我们需要一个QChart 实例。我们将系列添加到其中,创建默认坐标轴,并设置图表标题。

auto chart = new QChart;
chart->legend()->hide();
chart->addSeries(series);
chart->createDefaultAxes();
chart->setTitle("Simple Line Chart");

然后创建一个QChartView 对象,并将QChart 作为参数。这样我们就不需要自己创建QGraphicsView 场景了。

createDefaultChartView(chart);

图表就可以显示了。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.