| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt Charts module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 or (at your option) any later version |
| 20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
| 21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
| 22 | ** included in the packaging of this file. Please review the following |
| 23 | ** information to ensure the GNU General Public License requirements will |
| 24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 25 | ** |
| 26 | ** $QT_END_LICENSE$ |
| 27 | ** |
| 28 | ****************************************************************************/ |
| 29 | |
| 30 | #include "datasource.h" |
| 31 | #include <QtWidgets/QApplication> |
| 32 | #include <QtWidgets/QMainWindow> |
| 33 | #include <QtCharts/QChartView> |
| 34 | #include <QtCharts/QLineSeries> |
| 35 | #include <QtCharts/QScatterSeries> |
| 36 | #include <QtCharts/QValueAxis> |
| 37 | #include <QtCharts/QLogValueAxis> |
| 38 | #include <QtWidgets/QLabel> |
| 39 | |
| 40 | // Uncomment to use logarithmic axes instead of regular value axes |
| 41 | //#define USE_LOG_AXIS |
| 42 | |
| 43 | // Uncomment to use regular series instead of OpenGL accelerated series |
| 44 | //#define DONT_USE_GL_SERIES |
| 45 | |
| 46 | // Uncomment to add a simple regular series (thick line) and a matching OpenGL series (thinner line) |
| 47 | // to verify the series have same visible geometry. |
| 48 | //#define ADD_SIMPLE_SERIES |
| 49 | |
| 50 | QT_CHARTS_USE_NAMESPACE |
| 51 | |
| 52 | int main(int argc, char *argv[]) |
| 53 | { |
| 54 | QApplication a(argc, argv); |
| 55 | QStringList colors; |
| 56 | colors << "red" << "blue" << "green" << "black" ; |
| 57 | |
| 58 | QChart *chart = new QChart(); |
| 59 | chart->legend()->hide(); |
| 60 | |
| 61 | #ifdef USE_LOG_AXIS |
| 62 | QLogValueAxis *axisX = new QLogValueAxis; |
| 63 | QLogValueAxis *axisY = new QLogValueAxis; |
| 64 | #else |
| 65 | QValueAxis *axisX = new QValueAxis; |
| 66 | QValueAxis *axisY = new QValueAxis; |
| 67 | #endif |
| 68 | |
| 69 | chart->addAxis(axis: axisX, alignment: Qt::AlignBottom); |
| 70 | chart->addAxis(axis: axisY, alignment: Qt::AlignLeft); |
| 71 | |
| 72 | const int seriesCount = 10; |
| 73 | #ifdef DONT_USE_GL_SERIES |
| 74 | const int pointCount = 100; |
| 75 | chart->setTitle("Unaccelerated Series" ); |
| 76 | #else |
| 77 | const int pointCount = 10000; |
| 78 | chart->setTitle("OpenGL Accelerated Series" ); |
| 79 | #endif |
| 80 | |
| 81 | QList<QXYSeries *> seriesList; |
| 82 | for (int i = 0; i < seriesCount; i++) { |
| 83 | QXYSeries *series = 0; |
| 84 | int colorIndex = i % colors.size(); |
| 85 | if (i % 2) { |
| 86 | series = new QScatterSeries; |
| 87 | QScatterSeries *scatter = static_cast<QScatterSeries *>(series); |
| 88 | scatter->setColor(QColor(colors.at(i: colorIndex))); |
| 89 | scatter->setMarkerSize(qreal(colorIndex + 2) / 2.0); |
| 90 | // Scatter pen doesn't have affect in OpenGL drawing, but if you disable OpenGL drawing |
| 91 | // this makes the marker border visible and gives comparable marker size to OpenGL |
| 92 | // scatter points. |
| 93 | scatter->setPen(QPen("black" )); |
| 94 | } else { |
| 95 | series = new QLineSeries; |
| 96 | series->setPen(QPen(QBrush(QColor(colors.at(i: colorIndex))), |
| 97 | qreal(colorIndex + 2) / 2.0)); |
| 98 | } |
| 99 | seriesList.append(t: series); |
| 100 | #ifdef DONT_USE_GL_SERIES |
| 101 | series->setUseOpenGL(false); |
| 102 | #else |
| 103 | //![1] |
| 104 | series->setUseOpenGL(true); |
| 105 | //![1] |
| 106 | #endif |
| 107 | chart->addSeries(series); |
| 108 | series->attachAxis(axis: axisX); |
| 109 | series->attachAxis(axis: axisY); |
| 110 | } |
| 111 | |
| 112 | if (axisX->type() == QAbstractAxis::AxisTypeLogValue) |
| 113 | axisX->setRange(min: 0.1, max: 20.0); |
| 114 | else |
| 115 | axisX->setRange(min: 0, max: 20.0); |
| 116 | |
| 117 | if (axisY->type() == QAbstractAxis::AxisTypeLogValue) |
| 118 | axisY->setRange(min: 0.1, max: 10.0); |
| 119 | else |
| 120 | axisY->setRange(min: 0, max: 10.0); |
| 121 | |
| 122 | #ifdef ADD_SIMPLE_SERIES |
| 123 | QLineSeries *simpleRasterSeries = new QLineSeries; |
| 124 | *simpleRasterSeries << QPointF(0.001, 0.001) |
| 125 | << QPointF(2.5, 8.0) |
| 126 | << QPointF(5.0, 4.0) |
| 127 | << QPointF(7.5, 9.0) |
| 128 | << QPointF(10.0, 0.001) |
| 129 | << QPointF(12.5, 2.0) |
| 130 | << QPointF(15.0, 1.0) |
| 131 | << QPointF(17.5, 6.0) |
| 132 | << QPointF(20.0, 10.0); |
| 133 | simpleRasterSeries->setUseOpenGL(false); |
| 134 | simpleRasterSeries->setPen(QPen(QBrush("magenta" ), 8)); |
| 135 | chart->addSeries(simpleRasterSeries); |
| 136 | simpleRasterSeries->attachAxis(axisX); |
| 137 | simpleRasterSeries->attachAxis(axisY); |
| 138 | |
| 139 | QLineSeries *simpleGLSeries = new QLineSeries; |
| 140 | simpleGLSeries->setUseOpenGL(true); |
| 141 | simpleGLSeries->setPen(QPen(QBrush("black" ), 2)); |
| 142 | simpleGLSeries->replace(simpleRasterSeries->points()); |
| 143 | chart->addSeries(simpleGLSeries); |
| 144 | simpleGLSeries->attachAxis(axisX); |
| 145 | simpleGLSeries->attachAxis(axisY); |
| 146 | #endif |
| 147 | |
| 148 | QChartView *chartView = new QChartView(chart); |
| 149 | |
| 150 | QMainWindow window; |
| 151 | window.setCentralWidget(chartView); |
| 152 | window.resize(w: 600, h: 400); |
| 153 | window.show(); |
| 154 | |
| 155 | DataSource dataSource; |
| 156 | dataSource.generateData(seriesCount, rowCount: 10, colCount: pointCount); |
| 157 | |
| 158 | QLabel *fpsLabel = new QLabel(&window); |
| 159 | QLabel *countLabel = new QLabel(&window); |
| 160 | QString countText = QStringLiteral("Total point count: %1" ); |
| 161 | countLabel->setText(countText.arg(a: pointCount * seriesCount)); |
| 162 | countLabel->adjustSize(); |
| 163 | fpsLabel->move(ax: 10, ay: 2); |
| 164 | fpsLabel->adjustSize(); |
| 165 | fpsLabel->raise(); |
| 166 | fpsLabel->show(); |
| 167 | countLabel->move(ax: 10, ay: fpsLabel->height()); |
| 168 | fpsLabel->raise(); |
| 169 | countLabel->show(); |
| 170 | |
| 171 | // We can get more than one changed event per frame, so do async update. |
| 172 | // This also allows the application to be responsive. |
| 173 | QObject::connect(sender: chart->scene(), signal: &QGraphicsScene::changed, |
| 174 | receiver: &dataSource, slot: &DataSource::handleSceneChanged); |
| 175 | |
| 176 | dataSource.startUpdates(seriesList, fpsLabel); |
| 177 | |
| 178 | return a.exec(); |
| 179 | } |
| 180 | |