QLineSeries折线图绘制

QLineSeries类以折线图的形式显示数据。
  折线图用于将信息显示为由直线连接的一系列数据点。
  创建基本折线图:

// 方式一:逐一添加,大批量数据较慢
QLineSeries* series = new QLineSeries();
series->append(0, 6);
series->append(2, 4);
...
chart->addSeries(series);

坐标轴上绘制一条直线:

void MainWindow::CategoryAxisDraw()
{
    QChart* _pChart = new QChart;
    QChartView* chartView = new QChartView(_pChart, this);
    QLineSeries* series = new QLineSeries;
    series->setPen(QPen(Qt::blue));
    for(quint32 i = 0; i < 100; i++)
    {
        series->append(i, 1);
    }
    _pChart->addSeries(series);

    _pChart->createDefaultAxes();

    QValueAxis* axisY = dynamic_cast<QValueAxis*>(_pChart->axisY());
    axisY->setRange(0, 5);
    axisY->setTickCount(6);
    axisY->setLabelFormat("%.1f");
    axisY->setLinePen(QPen(Qt::black, 1));


    QValueAxis* axisX = dynamic_cast<QValueAxis*>(_pChart->axisX());
    axisX->setRange(0, 100);
    axisX->setTickCount(11);
    axisX->setLabelFormat("%i");
    series->attachAxis(axisX);


    chartView->setRenderHint(QPainter::Antialiasing);
    setCentralWidget(chartView);
}

运行结果
QLineSeries折线图绘制_第1张图片
绘制sin值折线图

void MainWindow::LineSeriesDraw()
{
    QChart* _pChart = new QChart;
    QChartView* chartView = new QChartView(_pChart, this);
    QLineSeries* serials = new QLineSeries;
    serials->setPen(QPen(Qt::blue));
    for(quint32 i = 0; i < 100; i++)
    {
        serials->append(i, sin(static_cast<double>(0.6f*i)));
    }
    _pChart->addSeries(serials);
    _pChart->createDefaultAxes();
    chartView->setRenderHint(QPainter::Antialiasing);
    setCentralWidget(chartView);
}

QLineSeries折线图绘制_第2张图片

你可能感兴趣的:(qt,c++,qt,python,开发语言)