From 1a6e0b1256e194ae9ce5e2996ee014a0e0aa7043 2012-07-30 15:34:50 From: Michal Klocek Date: 2012-07-30 15:34:50 Subject: [PATCH] Add horizontal charts to chartviewer --- diff --git a/demos/chartviewer/charts.h b/demos/chartviewer/charts.h index 399bd08..3f5ca7a 100644 --- a/demos/chartviewer/charts.h +++ b/demos/chartviewer/charts.h @@ -21,7 +21,7 @@ #ifndef CHARTS_H #define CHARTS_H -#include "window.h" +#include "model.h" #include #include #include diff --git a/demos/chartviewer/charts/charts.pri b/demos/chartviewer/charts/charts.pri index 1f98762..07e12e9 100644 --- a/demos/chartviewer/charts/charts.pri +++ b/demos/chartviewer/charts/charts.pri @@ -1,4 +1,13 @@ INCLUDEPATH += $$PWD DEPENDPATH += $$PWD -SOURCES+= linechart.cpp scatterchart.cpp splinechart.cpp piechart.cpp barchart.cpp areachart.cpp \ No newline at end of file +SOURCES+= \ + linechart.cpp \ + scatterchart.cpp \ + splinechart.cpp \ + piechart.cpp \ + verticalstackedbarchart.cpp \ + horizontalstackedbarchart.cpp \ + verticalbarchart.cpp \ + horizontalbarchart.cpp \ + areachart.cpp \ No newline at end of file diff --git a/demos/chartviewer/charts/horizontalbarchart.cpp b/demos/chartviewer/charts/horizontalbarchart.cpp new file mode 100644 index 0000000..15fe09e --- /dev/null +++ b/demos/chartviewer/charts/horizontalbarchart.cpp @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the Qt Commercial Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "charts.h" +#include "qchart.h" +#include "qhorizontalbarseries.h" +#include "qbarset.h" + +class HorizontalBarChart: public Chart +{ +public: + + HorizontalBarChart(){ + initialize(); + } + + QString name() { return QObject::tr("HorizontalBarChart"); } + QString category() { return QObject::tr("BarSeries"); } + QString subCategory() { return QObject::tr("Vertical"); } + + QChart* createChart(const DataTable& table) + { + + QChart* chart = new QChart(); + + chart->setTitle("Horizontal bar chart"); + + QHorizontalBarSeries* series = new QHorizontalBarSeries(chart); + for (int i(0); i < table.count(); i++) { + QBarSet *set = new QBarSet("Bar set " + QString::number(i)); + foreach (Data data, table[i]) + *set << data.first.y(); + series->append(set); + } + chart->addSeries(series); + chart->createDefaultAxes(); + return chart; + } + +}; + +DECLARE_CHART(HorizontalBarChart) + diff --git a/demos/chartviewer/charts/horizontalstackedbarchart.cpp b/demos/chartviewer/charts/horizontalstackedbarchart.cpp new file mode 100644 index 0000000..546a64e --- /dev/null +++ b/demos/chartviewer/charts/horizontalstackedbarchart.cpp @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the Qt Commercial Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "charts.h" +#include "qchart.h" +#include "qhorizontalstackedbarseries.h" +#include "qbarset.h" + +class HorizontalStackedBarChart: public Chart +{ +public: + + HorizontalStackedBarChart(){ + initialize(); + } + + QString name() { return QObject::tr("HorizontalStackedBarChart"); } + QString category() { return QObject::tr("BarSeries"); } + QString subCategory() { return QObject::tr("Vertical"); } + + QChart* createChart(const DataTable& table) + { + + QChart* chart = new QChart(); + + chart->setTitle("Horizontal stacked chart"); + + QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries(chart); + for (int i(0); i < table.count(); i++) { + QBarSet *set = new QBarSet("Bar set " + QString::number(i)); + foreach (Data data, table[i]) + *set << data.first.y(); + series->append(set); + } + chart->addSeries(series); + chart->createDefaultAxes(); + return chart; + } + +}; + +DECLARE_CHART(HorizontalStackedBarChart) + diff --git a/demos/chartviewer/charts/verticalbarchart.cpp b/demos/chartviewer/charts/verticalbarchart.cpp new file mode 100644 index 0000000..b566037 --- /dev/null +++ b/demos/chartviewer/charts/verticalbarchart.cpp @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the Qt Commercial Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "charts.h" +#include "qchart.h" +#include "qbarseries.h" +#include "qbarset.h" + +class VerticalBarChart: public Chart +{ +public: + + VerticalBarChart(){ + initialize(); + } + + QString name() { return QObject::tr("VerticalBarChart"); } + QString category() { return QObject::tr("BarSeries"); } + QString subCategory() { return QObject::tr("Vertical"); } + + QChart* createChart(const DataTable& table) + { + + QChart* chart = new QChart(); + + chart->setTitle("Vertical bar chart"); + + QBarSeries* series = new QBarSeries(chart); + for (int i(0); i < table.count(); i++) { + QBarSet *set = new QBarSet("Bar set " + QString::number(i)); + foreach (Data data, table[i]) + *set << data.first.y(); + series->append(set); + } + chart->addSeries(series); + chart->createDefaultAxes(); + return chart; + } + +}; + +DECLARE_CHART(VerticalBarChart) + diff --git a/demos/chartviewer/charts/barchart.cpp b/demos/chartviewer/charts/verticalstackedbarchart.cpp similarity index 91% rename from demos/chartviewer/charts/barchart.cpp rename to demos/chartviewer/charts/verticalstackedbarchart.cpp index 1bbb4bd..e207093 100644 --- a/demos/chartviewer/charts/barchart.cpp +++ b/demos/chartviewer/charts/verticalstackedbarchart.cpp @@ -23,11 +23,11 @@ #include "qstackedbarseries.h" #include "qbarset.h" -class BarChart: public Chart +class VerticalStackedBarChart: public Chart { public: - BarChart(){ + VerticalStackedBarChart(){ initialize(); } @@ -40,7 +40,7 @@ public: QChart* chart = new QChart(); - chart->setTitle("Bar chart"); + chart->setTitle("Stacked bar chart"); QStackedBarSeries* series = new QStackedBarSeries(chart); for (int i(0); i < table.count(); i++) { @@ -56,5 +56,5 @@ public: }; -DECLARE_CHART(BarChart) +DECLARE_CHART(VerticalStackedBarChart) diff --git a/demos/chartviewer/chartviewer.pro b/demos/chartviewer/chartviewer.pro index 3eed466..6e8c6fd 100644 --- a/demos/chartviewer/chartviewer.pro +++ b/demos/chartviewer/chartviewer.pro @@ -3,5 +3,5 @@ include(charts/charts.pri) TARGET = chartviewer QT += opengl SOURCES += main.cpp window.cpp view.cpp charts.cpp -HEADERS += window.h view.h charts.h +HEADERS += window.h view.h charts.h model.h diff --git a/demos/chartviewer/model.h b/demos/chartviewer/model.h new file mode 100644 index 0000000..a49deef --- /dev/null +++ b/demos/chartviewer/model.h @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the Qt Commercial Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MODEL_H_ +#define MODEL_H_ + +#include +#include +#include +#include + +typedef QPair Data; +typedef QList DataList; +typedef QList DataTable; + + +class Model +{ +private: + Model(){} + +public: + static DataTable generateRandomData(int listCount, int valueMax, int valueCount) + { + DataTable dataTable; + + // set seed for random stuff + qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); + + // generate random data + for (int i(0); i < listCount; i++) { + DataList dataList; + qreal yValue(0); + for (int j(0); j < valueCount; j++) { + yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount; + QPointF value( + (j + (qreal) qrand() / (qreal) RAND_MAX) + * ((qreal) valueMax / (qreal) valueCount), yValue); + QString label = "Slice " + QString::number(i) + ":" + QString::number(j); + dataList << Data(value, label); + } + dataTable << dataList; + } + + return dataTable; + } + +}; + +#endif diff --git a/demos/chartviewer/window.cpp b/demos/chartviewer/window.cpp index 7628f8e..2993cb2 100644 --- a/demos/chartviewer/window.cpp +++ b/demos/chartviewer/window.cpp @@ -21,17 +21,7 @@ #include "window.h" #include "view.h" #include "charts.h" - #include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include #include #include @@ -41,8 +31,6 @@ #include #include #include -#include -#include #include #include #include @@ -58,7 +46,7 @@ Window::Window(QWidget* parent) : m_valueCount(7), m_scene(new QGraphicsScene(this)), m_view(0), - m_dataTable(generateRandomData(m_listCount, m_valueMax, m_valueCount)), + m_dataTable(Model::generateRandomData(m_listCount, m_valueMax, m_valueCount)), m_form(0), m_themeComboBox(0), m_antialiasCheckBox(0), @@ -97,9 +85,8 @@ Window::Window(QWidget* parent) : Charts::ChartList list = Charts::chartList(); - for(int i = 0 ; i < 6 ; ++i) + for(int i = 0 ; i < 9 && icreateChart(m_dataTable); baseLayout->addItem(chart, i/3, i%3); m_chartList << chart; @@ -135,31 +122,6 @@ void Window::connectSignals() QObject::connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); } -DataTable Window::generateRandomData(int listCount, int valueMax, int valueCount) const -{ - DataTable dataTable; - - // set seed for random stuff - qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); - - // generate random data - for (int i(0); i < listCount; i++) { - DataList dataList; - qreal yValue(0); - for (int j(0); j < valueCount; j++) { - yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount; - QPointF value( - (j + (qreal) qrand() / (qreal) RAND_MAX) - * ((qreal) m_valueMax / (qreal) valueCount), yValue); - QString label = "Slice " + QString::number(i) + ":" + QString::number(j); - dataList << Data(value, label); - } - dataTable << dataList; - } - - return dataTable; -} - void Window::createProxyWidgets() { m_themeComboBox = createThemeBox(); diff --git a/demos/chartviewer/window.h b/demos/chartviewer/window.h index 7b0a593..9257552 100644 --- a/demos/chartviewer/window.h +++ b/demos/chartviewer/window.h @@ -20,6 +20,7 @@ #ifndef WINDOW_H_ #define WINDOW_H_ +#include "model.h" #include #include #include @@ -35,10 +36,6 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class QChart; QTCOMMERCIALCHART_END_NAMESPACE -typedef QPair Data; -typedef QList DataList; -typedef QList DataTable; - QTCOMMERCIALCHART_USE_NAMESPACE @@ -51,10 +48,7 @@ public: private Q_SLOTS: void updateUI(); - - private: - DataTable generateRandomData(int listCount,int valueMax,int valueCount) const; QComboBox* createThemeBox(); QComboBox* createAnimationBox(); QComboBox* createLegendBox();