| 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 <QtWidgets/QApplication> |
| 31 | #include <QtWidgets/QMainWindow> |
| 32 | #include <QtCharts/QChartView> |
| 33 | #include <QtCharts/QLineSeries> |
| 34 | #include <QtCharts/QCategoryAxis> |
| 35 | |
| 36 | QT_CHARTS_USE_NAMESPACE |
| 37 | |
| 38 | int main(int argc, char *argv[]) |
| 39 | { |
| 40 | QApplication a(argc, argv); |
| 41 | |
| 42 | //![1] |
| 43 | QLineSeries *series = new QLineSeries(); |
| 44 | *series << QPointF(0, 6) << QPointF(9, 4) << QPointF(15, 20) << QPointF(25, 12) << QPointF(29, 26); |
| 45 | QChart *chart = new QChart(); |
| 46 | chart->legend()->hide(); |
| 47 | chart->addSeries(series); |
| 48 | //![1] |
| 49 | |
| 50 | //![2] |
| 51 | // Customize series |
| 52 | QPen pen(QRgb(0xfdb157)); |
| 53 | pen.setWidth(5); |
| 54 | series->setPen(pen); |
| 55 | |
| 56 | // Customize chart title |
| 57 | QFont font; |
| 58 | font.setPixelSize(18); |
| 59 | chart->setTitleFont(font); |
| 60 | chart->setTitleBrush(QBrush(Qt::white)); |
| 61 | chart->setTitle("Customchart example" ); |
| 62 | |
| 63 | // Customize chart background |
| 64 | QLinearGradient backgroundGradient; |
| 65 | backgroundGradient.setStart(QPointF(0, 0)); |
| 66 | backgroundGradient.setFinalStop(QPointF(0, 1)); |
| 67 | backgroundGradient.setColorAt(pos: 0.0, color: QRgb(0xd2d0d1)); |
| 68 | backgroundGradient.setColorAt(pos: 1.0, color: QRgb(0x4c4547)); |
| 69 | backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); |
| 70 | chart->setBackgroundBrush(backgroundGradient); |
| 71 | |
| 72 | // Customize plot area background |
| 73 | QLinearGradient plotAreaGradient; |
| 74 | plotAreaGradient.setStart(QPointF(0, 1)); |
| 75 | plotAreaGradient.setFinalStop(QPointF(1, 0)); |
| 76 | plotAreaGradient.setColorAt(pos: 0.0, color: QRgb(0x555555)); |
| 77 | plotAreaGradient.setColorAt(pos: 1.0, color: QRgb(0x55aa55)); |
| 78 | plotAreaGradient.setCoordinateMode(QGradient::ObjectBoundingMode); |
| 79 | chart->setPlotAreaBackgroundBrush(plotAreaGradient); |
| 80 | chart->setPlotAreaBackgroundVisible(true); |
| 81 | //![2] |
| 82 | |
| 83 | //![3] |
| 84 | QCategoryAxis *axisX = new QCategoryAxis(); |
| 85 | QCategoryAxis *axisY = new QCategoryAxis(); |
| 86 | |
| 87 | // Customize axis label font |
| 88 | QFont labelsFont; |
| 89 | labelsFont.setPixelSize(12); |
| 90 | axisX->setLabelsFont(labelsFont); |
| 91 | axisY->setLabelsFont(labelsFont); |
| 92 | |
| 93 | // Customize axis colors |
| 94 | QPen axisPen(QRgb(0xd18952)); |
| 95 | axisPen.setWidth(2); |
| 96 | axisX->setLinePen(axisPen); |
| 97 | axisY->setLinePen(axisPen); |
| 98 | |
| 99 | // Customize axis label colors |
| 100 | QBrush axisBrush(Qt::white); |
| 101 | axisX->setLabelsBrush(axisBrush); |
| 102 | axisY->setLabelsBrush(axisBrush); |
| 103 | |
| 104 | // Customize grid lines and shades |
| 105 | axisX->setGridLineVisible(false); |
| 106 | axisY->setGridLineVisible(false); |
| 107 | axisY->setShadesPen(Qt::NoPen); |
| 108 | axisY->setShadesBrush(QBrush(QColor(0x99, 0xcc, 0xcc, 0x55))); |
| 109 | axisY->setShadesVisible(true); |
| 110 | //![3] |
| 111 | |
| 112 | //![4] |
| 113 | axisX->append(label: "low" , categoryEndValue: 10); |
| 114 | axisX->append(label: "optimal" , categoryEndValue: 20); |
| 115 | axisX->append(label: "high" , categoryEndValue: 30); |
| 116 | axisX->setRange(min: 0, max: 30); |
| 117 | |
| 118 | axisY->append(label: "slow" , categoryEndValue: 10); |
| 119 | axisY->append(label: "med" , categoryEndValue: 20); |
| 120 | axisY->append(label: "fast" , categoryEndValue: 30); |
| 121 | axisY->setRange(min: 0, max: 30); |
| 122 | |
| 123 | chart->addAxis(axis: axisX, alignment: Qt::AlignBottom); |
| 124 | chart->addAxis(axis: axisY, alignment: Qt::AlignLeft); |
| 125 | series->attachAxis(axis: axisX); |
| 126 | series->attachAxis(axis: axisY); |
| 127 | //![4] |
| 128 | |
| 129 | //![5] |
| 130 | QChartView *chartView = new QChartView(chart); |
| 131 | chartView->setRenderHint(hint: QPainter::Antialiasing); |
| 132 | //![5] |
| 133 | |
| 134 | //![6] |
| 135 | QMainWindow window; |
| 136 | window.setCentralWidget(chartView); |
| 137 | window.resize(w: 400, h: 300); |
| 138 | window.show(); |
| 139 | //![6] |
| 140 | |
| 141 | return a.exec(); |
| 142 | } |
| 143 | |