QT5.14.2 官方例子 - Qt Charts 2: Chart Themes Example(图表主题)

示例展示了如何将QLineChart与QDateTimeAxis一起使用。

QT5.14.2 官方例子 - Qt Charts 2: Chart Themes Example(图表主题)_第1张图片

在图表中,我们将展示太阳黑子的数量随时间的变化。数据(来自太空天气预报中心)是从一个文本文件读取的。

 

具体讲解在:

https://doc.qt.io/qt-5/qtcharts-datetimeaxis-example.html

 

过程分析:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    //![1]
    //创建线性图指针
    QLineSeries *series = new QLineSeries();
    //![1]

    //![2]
    // data from http://www.swpc.noaa.gov/ftpdir/weekly/RecentIndices.txt
    // http://www.swpc.noaa.gov/ftpdir/weekly/README
    // http://www.weather.gov/disclaimer

    // 打开太阳黑点记录文件,
    //    在资源文件中,添加该文本:sun_spots.txt,别名为:sun
    QFile sunSpots(":sun");
    if (!sunSpots.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        return 1;
    }

    // 用文本流(QTextStream)去处理该文本
    QTextStream stream(&sunSpots);
    while (!stream.atEnd())
    {
        QString line = stream.readLine();
        if (line.startsWith("#") || line.startsWith(":"))
            continue;

        // 将字符串用空格分隔开,并逐条记录到字符列表中
        QStringList values = line.split(" ", QString::SkipEmptyParts);
        QDateTime momentInTime;
        momentInTime.setDate(QDate(values[0].toInt(), values[1].toInt() , 15));

        // 添加横,纵坐标
        series->append(momentInTime.toMSecsSinceEpoch(), values[2].toDouble());
    }
    sunSpots.close();
    //![2]

    //![3]
    // 添加图表,为该图表添加线性图
    QChart *chart = new QChart();
    chart->addSeries(series);
    chart->legend()->hide();
    chart->setTitle("Sunspots count (by Space Weather Prediction Center)");
    //![3]

    //![4]
    // QDateTimeAxis类将日期和时间添加到图表的轴上
    QDateTimeAxis *axisX = new QDateTimeAxis;
    axisX->setTickCount(10);
    axisX->setFormat("MMM yyyy");
    axisX->setTitleText("Date");
    // 为图表添加该轴为下方的轴
    chart->addAxis(axisX, Qt::AlignBottom);
    series->attachAxis(axisX);

    QValueAxis *axisY = new QValueAxis;
    axisY->setLabelFormat("%i");
    axisY->setTitleText("Sunspots count");
    // 为图表添加该轴为左方的轴
    chart->addAxis(axisY, Qt::AlignLeft);
    series->attachAxis(axisY);
    //![4]

    //![5]
    // 将该图表添加到图表视图中
    QChartView *chartView = new QChartView(chart);
    chartView->setRenderHint(QPainter::Antialiasing);
    //![5]

    //![6]
    QMainWindow window;
    window.setCentralWidget(chartView);
    window.resize(820, 600);
    window.show();
    //![6]

    return a.exec();
}

 

大致思路:

1.创建图类型(线型图,条形图,饼状图等等);

2.为类型图添加点坐标;

//![1]
    //创建线性图指针
    QLineSeries *series = new QLineSeries();
    //![1]

    //![2]
    // data from http://www.swpc.noaa.gov/ftpdir/weekly/RecentIndices.txt
    // http://www.swpc.noaa.gov/ftpdir/weekly/README
    // http://www.weather.gov/disclaimer

    // 打开太阳黑点记录文件,
    //    在资源文件中,添加该文本:sun_spots.txt,别名为:sun
    QFile sunSpots(":sun");
    if (!sunSpots.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        return 1;
    }

    // 用文本流(QTextStream)去处理该文本
    QTextStream stream(&sunSpots);
    while (!stream.atEnd())
    {
        QString line = stream.readLine();
        if (line.startsWith("#") || line.startsWith(":"))
            continue;

        // 将字符串用空格分隔开,并逐条记录到字符列表中
        QStringList values = line.split(" ", QString::SkipEmptyParts);
        QDateTime momentInTime;
        momentInTime.setDate(QDate(values[0].toInt(), values[1].toInt() , 15));

        // 添加横,纵坐标
        series->append(momentInTime.toMSecsSinceEpoch(), values[2].toDouble());
    }
    sunSpots.close();

3.创建图表,将类型图添加到图表上;

    // 添加图表,为该图表添加线性图
    QChart *chart = new QChart();
    chart->addSeries(series);
    chart->legend()->hide();
    chart->setTitle("Sunspots count (by Space Weather Prediction Center)");
    //![3]

4.创建x,y轴,添加到类型图上;

    // QDateTimeAxis类将日期和时间添加到图表的轴上
    QDateTimeAxis *axisX = new QDateTimeAxis;
    axisX->setTickCount(10);
    axisX->setFormat("MMM yyyy");
    axisX->setTitleText("Date");
    // 为图表添加该轴为下方的轴
    chart->addAxis(axisX, Qt::AlignBottom);
    series->attachAxis(axisX);

    QValueAxis *axisY = new QValueAxis;
    axisY->setLabelFormat("%i");
    axisY->setTitleText("Sunspots count");
    // 为图表添加该轴为左方的轴
    chart->addAxis(axisY, Qt::AlignLeft);
    series->attachAxis(axisY);

5.创建图表视图,添加图表;

    // 将该图表添加到图表视图中
    QChartView *chartView = new QChartView(chart);
    chartView->setRenderHint(QPainter::Antialiasing);

 

修改:将线性图修改了饼状图;

范围分为:0-67,67-135,135-202,202-270

待补充。。。

你可能感兴趣的:(QT,examples,qt)