创建样条曲线图

要创建样条曲线图,我们需要将数据输入QSplineSeriesQSplineSeries 会自动计算样条线段控制点,这些控制点是正确绘制样条曲线所必需的。

auto series = new QSplineSeries;
series->setName("Spline");

现在,让我们为系列添加一些数据点。

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 对象,并将数据序列添加到该对象中。我们还在 Y 轴上设置了标题和数值范围,这样图表的可视性就更好了。

auto chart = new QChart;
chart->legend()->hide();
chart->addSeries(series);
chart->setTitle("Simple Spline Chart");
chart->createDefaultAxes();
chart->axes(Qt::Vertical).first()->setRange(0, 10);

然后,我们创建一个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.