| 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 Data Visualization 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 "rainfallgraph.h" | 
| 31 | #include <QtDataVisualization/qcategory3daxis.h> | 
| 32 | #include <QtDataVisualization/qvalue3daxis.h> | 
| 33 | #include <QtDataVisualization/q3dscene.h> | 
| 34 | #include <QtDataVisualization/q3dcamera.h> | 
| 35 | #include <QtDataVisualization/qbar3dseries.h> | 
| 36 | #include <QtDataVisualization/q3dtheme.h> | 
| 37 | #include <QtGui/QGuiApplication> | 
| 38 | #include <QtGui/QFont> | 
| 39 | #include <QtCore/QDebug> | 
| 40 | #include <QtCore/QTextStream> | 
| 41 | #include <QtCore/QFile> | 
| 42 |  | 
| 43 | using namespace QtDataVisualization; | 
| 44 |  | 
| 45 | RainfallGraph::RainfallGraph(Q3DBars *rainfall) | 
| 46 |     : m_graph(rainfall) | 
| 47 | { | 
| 48 |     // In data file the months are in numeric format, so create custom list | 
| 49 |     for (int i = 1; i <= 12; i++) | 
| 50 |         m_numericMonths << QString::number(i); | 
| 51 |  | 
| 52 |     m_columnCount = m_numericMonths.size(); | 
| 53 |  | 
| 54 |     m_proxy = new VariantBarDataProxy; | 
| 55 |     QBar3DSeries *series = new QBar3DSeries(m_proxy); | 
| 56 |     m_graph->addSeries(series); | 
| 57 |  | 
| 58 |     updateYearsList(start: 2000, end: 2012); | 
| 59 |  | 
| 60 |     // Set up bar specifications; make the bars as wide as they are deep, | 
| 61 |     // and add a small space between the bars | 
| 62 |     m_graph->setBarThickness(1.0f); | 
| 63 |     m_graph->setBarSpacing(QSizeF(1.1, 1.1)); | 
| 64 |  | 
| 65 |     // Set axis labels and titles | 
| 66 |     QStringList months; | 
| 67 |     months << "January"  << "February"  << "March"  << "April"  << "May"  << "June"  << "July"  << "August"  << "September"  << "October"  << "November"  << "December" ; | 
| 68 |     m_graph->rowAxis()->setTitle("Year" ); | 
| 69 |     m_graph->columnAxis()->setTitle("Month" ); | 
| 70 |     m_graph->valueAxis()->setTitle("rainfall" ); | 
| 71 |     m_graph->valueAxis()->setLabelFormat("%d mm" ); | 
| 72 |     m_graph->valueAxis()->setSegmentCount(5); | 
| 73 |     m_graph->rowAxis()->setLabels(m_years); | 
| 74 |     m_graph->columnAxis()->setLabels(months); | 
| 75 |  | 
| 76 |     // Set bar type to cylinder | 
| 77 |     series->setMesh(QAbstract3DSeries::MeshCylinder); | 
| 78 |  | 
| 79 |     // Set shadows to medium | 
| 80 |     m_graph->setShadowQuality(QAbstract3DGraph::ShadowQualityMedium); | 
| 81 |  | 
| 82 |     // Set selection mode to bar and column | 
| 83 |     m_graph->setSelectionMode(QAbstract3DGraph::SelectionItemAndColumn | QAbstract3DGraph::SelectionSlice); | 
| 84 |  | 
| 85 |     // Set theme | 
| 86 |     m_graph->activeTheme()->setType(Q3DTheme::ThemeArmyBlue); | 
| 87 |  | 
| 88 |     // Override font in theme | 
| 89 |     m_graph->activeTheme()->setFont(QFont("Century Gothic" , 30)); | 
| 90 |  | 
| 91 |     // Override label background for theme | 
| 92 |     m_graph->activeTheme()->setLabelBackgroundEnabled(false); | 
| 93 |  | 
| 94 |     // Set camera position and zoom | 
| 95 |     m_graph->scene()->activeCamera()->setCameraPreset(Q3DCamera::CameraPresetIsometricRightHigh); | 
| 96 |  | 
| 97 |     // Set window title | 
| 98 |     m_graph->setTitle(QStringLiteral("Monthly rainfall in Northern Finland" )); | 
| 99 |  | 
| 100 |     // Set reflections on | 
| 101 |     m_graph->setReflection(true); | 
| 102 | } | 
| 103 |  | 
| 104 | RainfallGraph::~RainfallGraph() | 
| 105 | { | 
| 106 |     delete m_mapping; | 
| 107 |     delete m_dataSet; | 
| 108 |     delete m_graph; | 
| 109 | } | 
| 110 |  | 
| 111 | void RainfallGraph::start() | 
| 112 | { | 
| 113 |     addDataSet(); | 
| 114 | } | 
| 115 |  | 
| 116 | void RainfallGraph::updateYearsList(int start, int end) | 
| 117 | { | 
| 118 |     m_years.clear(); | 
| 119 |     for (int i = start; i <= end; i++) | 
| 120 |         m_years << QString::number(i); | 
| 121 |  | 
| 122 |     m_rowCount = m_years.size(); | 
| 123 | } | 
| 124 |  | 
| 125 | //! [0] | 
| 126 | void RainfallGraph::addDataSet() | 
| 127 | { | 
| 128 |     // Create a new variant data set and data item list | 
| 129 |     m_dataSet =  new VariantDataSet; | 
| 130 |     VariantDataItemList *itemList = new VariantDataItemList; | 
| 131 |  | 
| 132 |     // Read data from a data file into the data item list | 
| 133 |     QTextStream stream; | 
| 134 |     QFile dataFile(":/data/raindata.txt" ); | 
| 135 |     if (dataFile.open(flags: QIODevice::ReadOnly | QIODevice::Text)) { | 
| 136 |         stream.setDevice(&dataFile); | 
| 137 |         while (!stream.atEnd()) { | 
| 138 |             QString line = stream.readLine(); | 
| 139 |             if (line.startsWith(s: "#" )) // Ignore comments | 
| 140 |                 continue; | 
| 141 |             QStringList strList = line.split(sep: "," , behavior: Qt::SkipEmptyParts); | 
| 142 |             // Each line has three data items: Year, month, and rainfall value | 
| 143 |             if (strList.size() < 3) { | 
| 144 |                 qWarning() << "Invalid row read from data:"  << line; | 
| 145 |                 continue; | 
| 146 |             } | 
| 147 |             // Store year and month as strings, and rainfall value as double | 
| 148 |             // into a variant data item and add the item to the item list. | 
| 149 |             VariantDataItem *newItem = new VariantDataItem; | 
| 150 |             for (int i = 0; i < 2; i++) | 
| 151 |                 newItem->append(t: strList.at(i).trimmed()); | 
| 152 |             newItem->append(t: strList.at(i: 2).trimmed().toDouble()); | 
| 153 |             itemList->append(t: newItem); | 
| 154 |         } | 
| 155 |     } else { | 
| 156 |         qWarning() << "Unable to open data file:"  << dataFile.fileName(); | 
| 157 |     } | 
| 158 |  | 
| 159 |     //! [1] | 
| 160 |     // Add items to the data set and set it to the proxy | 
| 161 |     m_dataSet->addItems(itemList); | 
| 162 |     m_proxy->setDataSet(m_dataSet); | 
| 163 |  | 
| 164 |     // Create new mapping for the data and set it to the proxy | 
| 165 |     m_mapping =  new VariantBarDataMapping(0, 1, 2, m_years, m_numericMonths); | 
| 166 |     m_proxy->setMapping(m_mapping); | 
| 167 |     //! [1] | 
| 168 | } | 
| 169 | //! [0] | 
| 170 |  |