1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| #include <QApplication> #include <QWidget> #include <QChartView> #include <QSplineSeries> #include <QScatterSeries> #include <QChart> #include <QHBoxLayout>
using namespace QtCharts;
int main(int argc, char *argv[]) { QApplication a(argc, argv);
QSplineSeries *splineSeries = new QSplineSeries(); splineSeries->setName("spline");
splineSeries->append(0, 6); splineSeries->append(2, 4); splineSeries->append(3, 8); splineSeries->append(7, 4); splineSeries->append(10, 5); *splineSeries << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2);
QScatterSeries *scatterSeries = new QScatterSeries(); scatterSeries->setMarkerSize(8); scatterSeries->append(0, 6); scatterSeries->append(2, 4); scatterSeries->append(3, 8); scatterSeries->append(7, 4); scatterSeries->append(10, 5); *scatterSeries << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2);
QChart *chart = new QChart(); chart->addSeries(splineSeries); chart->addSeries(scatterSeries); chart->legend()->hide(); chart->setTitle("平滑曲线"); chart->createDefaultAxes(); QList<QAbstractAxis *> yAxes = chart->axes(Qt::Vertical, splineSeries); yAxes[0]->setRange(0, 10);
QChartView *chartView = new QChartView(chart); chartView->setRenderHint(QPainter::Antialiasing);
QHBoxLayout *layout = new QHBoxLayout(); layout->setContentsMargins(0, 0, 0, 0); layout->addWidget(chartView);
QWidget w; w.setWindowTitle("QChart 实现的平滑曲线"); w.resize(600, 300); w.setLayout(layout); w.show();
return a.exec(); }
|