上图
代码
#include <iostream>
#include <random>
#include <qcategoryaxis.h>void MainWindow::testLine()
{QChartView* view = new QChartView(this);QChart* chart = new QChart();view->setChart(chart);chart->setTitle("");chart->legend()->show();QDateTimeAxis* axisX = new QDateTimeAxis(this);axisX->setTitleText("");axisX->setRange(QDateTime(QDate(2000, 01, 01), QTime(0, 0, 0)),QDateTime(QDate(2000, 01, 10), QTime(0, 0, 0)));axisX->setTickCount(10);axisX->setFormat("MM.dd");axisX->setLabelsAngle(45);chart->addAxis(axisX, Qt::AlignBottom);QValueAxis* axisY = new QValueAxis(this);axisY->setRange(0, 100);axisY->setTickCount(11);axisY->setTitleText("Y");chart->addAxis(axisY, Qt::AlignLeft);QLineSeries* line = new QLineSeries();line->setName("tar");QLineSeries* line2 = new QLineSeries();line2->setName("reality");for (int i = 0; i < 7; i++){QString strDate = QString("%1.%2.%3").arg(2000).arg(1).arg(i + 1);{std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution<> dis(1, 100);line->append(QDateTime(QDate::fromString(strDate, "yyyy.M.d"), QTime(0, 0, 0)).toMSecsSinceEpoch(), dis(gen));}{std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution<> dis(1, 100);line2->append(QDateTime(QDate::fromString(strDate, "yyyy.M.d"), QTime(0, 0, 0)).toMSecsSinceEpoch(), dis(gen));}}line->setPointLabelsVisible(true);line2->setPointLabelsVisible(true);line->setPointLabelsFormat("(@yPoint)");line2->setPointLabelsFormat("(@yPoint)");line->setPointLabelsClipping(false);line2->setPointLabelsClipping(false);chart->addSeries(line);chart->addSeries(line2);line->attachAxis(axisX);line->attachAxis(axisY);line2->attachAxis(axisX);line2->attachAxis(axisY);setCentralWidget(view);
}