rainfallgraph.cpp Example File
customproxy/rainfallgraph.cpp
#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)
{
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);
m_graph->setBarThickness(1.0f);
m_graph->setBarSpacing(QSizeF(1.1, 1.1));
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);
series->setMesh(QAbstract3DSeries::MeshCylinder);
m_graph->setShadowQuality(QAbstract3DGraph::ShadowQualityMedium);
m_graph->setSelectionMode(QAbstract3DGraph::SelectionItemAndColumn | QAbstract3DGraph::SelectionSlice);
m_graph->activeTheme()->setType(Q3DTheme::ThemeArmyBlue);
m_graph->activeTheme()->setFont(QFont("Century Gothic", 30));
m_graph->activeTheme()->setLabelBackgroundEnabled(false);
m_graph->scene()->activeCamera()->setCameraPreset(Q3DCamera::CameraPresetIsometricRightHigh);
m_graph->setTitle(QStringLiteral("Monthly rainfall in Northern Finland"));
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();
}
void RainfallGraph::addDataSet()
{
m_dataSet = new VariantDataSet;
VariantDataItemList *itemList = new VariantDataItemList;
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("#"))
continue;
QStringList strList = line.split(",", QString::SkipEmptyParts);
if (strList.size() < 3) {
qWarning() << "Invalid row read from data:" << line;
continue;
}
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();
}
m_dataSet->addItems(itemList);
m_proxy->setDataSet(m_dataSet);
m_mapping = new VariantBarDataMapping(0, 1, 2, m_years, m_numericMonths);
m_proxy->setMapping(m_mapping);
}