summaryrefslogtreecommitdiffstats
path: root/examples/datavisualization/customproxy/rainfallgraph.cpp
blob: 9287e738c66c0332b34f9bec42b99ac3d2d4f750 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd
** All rights reserved.
** For any questions to The Qt Company, please use contact form at http://qt.io
**
** This file is part of the Qt Data Visualization module.
**
** Licensees holding valid commercial license for Qt may use this file in
** accordance with the Qt License Agreement provided with the Software
** or, alternatively, in accordance with the terms contained in a written
** agreement between you and The Qt Company.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.io
**
****************************************************************************/

#include "rainfallgraph.h"
#include <QtDataVisualization/qcategory3daxis.h>
#include <QtDataVisualization/qvalue3daxis.h>
#include <QtDataVisualization/q3dscene.h>
#include <QtDataVisualization/q3dcamera.h>
#include <QtDataVisualization/qbar3dseries.h>
#include <QtDataVisualization/q3dtheme.h>
#include <QtGui/QGuiApplication>
#include <QtGui/QFont>
#include <QtCore/QDebug>
#include <QtCore/QTextStream>
#include <QtCore/QFile>

using namespace QtDataVisualization;

RainfallGraph::RainfallGraph(Q3DBars *rainfall)
    : m_graph(rainfall)
{
    // In data file the months are in numeric format, so create custom list
    for (int i = 1; i <= 12; i++)
        m_numericMonths << QString::number(i);

    m_columnCount = m_numericMonths.size();

    m_proxy = new VariantBarDataProxy;
    QBar3DSeries *series = new QBar3DSeries(m_proxy);
    m_graph->addSeries(series);

    updateYearsList(2000, 2012);

    // Set up bar specifications; make the bars as wide as they are deep,
    // and add a small space between the bars
    m_graph->setBarThickness(1.0f);
    m_graph->setBarSpacing(QSizeF(1.1, 1.1));

    // Set axis labels and titles
    QStringList months;
    months << "January" << "February" << "March" << "April" << "May" << "June" << "July" << "August" << "September" << "October" << "November" << "December";
    m_graph->rowAxis()->setTitle("Year");
    m_graph->columnAxis()->setTitle("Month");
    m_graph->valueAxis()->setTitle("rainfall");
    m_graph->valueAxis()->setLabelFormat("%d mm");
    m_graph->valueAxis()->setSegmentCount(5);
    m_graph->rowAxis()->setLabels(m_years);
    m_graph->columnAxis()->setLabels(months);

    // Set bar type to cylinder
    series->setMesh(QAbstract3DSeries::MeshCylinder);

    // Set shadows to medium
    m_graph->setShadowQuality(QAbstract3DGraph::ShadowQualityMedium);

    // Set selection mode to bar and column
    m_graph->setSelectionMode(QAbstract3DGraph::SelectionItemAndColumn | QAbstract3DGraph::SelectionSlice);

    // Set theme
    m_graph->activeTheme()->setType(Q3DTheme::ThemeArmyBlue);

    // Override font in theme
    m_graph->activeTheme()->setFont(QFont("Century Gothic", 30));

    // Override label background for theme
    m_graph->activeTheme()->setLabelBackgroundEnabled(false);

    // Set camera position and zoom
    m_graph->scene()->activeCamera()->setCameraPreset(Q3DCamera::CameraPresetIsometricRightHigh);

    // Set window title
    m_graph->setTitle(QStringLiteral("Monthly rainfall in Northern Finland"));

    // Set reflections on
    m_graph->setReflection(true);
}

RainfallGraph::~RainfallGraph()
{
    delete m_mapping;
    delete m_dataSet;
    delete m_graph;
}

void RainfallGraph::start()
{
    addDataSet();
}

void RainfallGraph::updateYearsList(int start, int end)
{
    m_years.clear();
    for (int i = start; i <= end; i++)
        m_years << QString::number(i);

    m_rowCount = m_years.size();
}

//! [0]
void RainfallGraph::addDataSet()
{
    // Create a new variant data set and data item list
    m_dataSet =  new VariantDataSet;
    VariantDataItemList *itemList = new VariantDataItemList;

    // Read data from a data file into the data item list
    QTextStream stream;
    QFile dataFile(":/data/raindata.txt");
    if (dataFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
        stream.setDevice(&dataFile);
        while (!stream.atEnd()) {
            QString line = stream.readLine();
            if (line.startsWith("#")) // Ignore comments
                continue;
            QStringList strList = line.split(",", QString::SkipEmptyParts);
            // Each line has three data items: Year, month, and rainfall value
            if (strList.size() < 3) {
                qWarning() << "Invalid row read from data:" << line;
                continue;
            }
            // Store year and month as strings, and rainfall value as double
            // into a variant data item and add the item to the item list.
            VariantDataItem *newItem = new VariantDataItem;
            for (int i = 0; i < 2; i++)
                newItem->append(strList.at(i).trimmed());
            newItem->append(strList.at(2).trimmed().toDouble());
            itemList->append(newItem);
        }
    } else {
        qWarning() << "Unable to open data file:" << dataFile.fileName();
    }

    //! [1]
    // Add items to the data set and set it to the proxy
    m_dataSet->addItems(itemList);
    m_proxy->setDataSet(m_dataSet);

    // Create new mapping for the data and set it to the proxy
    m_mapping =  new VariantBarDataMapping(0, 1, 2, m_years, m_numericMonths);
    m_proxy->setMapping(m_mapping);
    //! [1]
}
//! [0]