##// END OF EJS Templates
Add horizontal charts to chartviewer
Michal Klocek -
r1755:1a6e0b1256e1
parent child
Show More
@@ -0,0 +1,60
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "charts.h"
22 #include "qchart.h"
23 #include "qhorizontalbarseries.h"
24 #include "qbarset.h"
25
26 class HorizontalBarChart: public Chart
27 {
28 public:
29
30 HorizontalBarChart(){
31 initialize();
32 }
33
34 QString name() { return QObject::tr("HorizontalBarChart"); }
35 QString category() { return QObject::tr("BarSeries"); }
36 QString subCategory() { return QObject::tr("Vertical"); }
37
38 QChart* createChart(const DataTable& table)
39 {
40
41 QChart* chart = new QChart();
42
43 chart->setTitle("Horizontal bar chart");
44
45 QHorizontalBarSeries* series = new QHorizontalBarSeries(chart);
46 for (int i(0); i < table.count(); i++) {
47 QBarSet *set = new QBarSet("Bar set " + QString::number(i));
48 foreach (Data data, table[i])
49 *set << data.first.y();
50 series->append(set);
51 }
52 chart->addSeries(series);
53 chart->createDefaultAxes();
54 return chart;
55 }
56
57 };
58
59 DECLARE_CHART(HorizontalBarChart)
60
@@ -0,0 +1,60
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "charts.h"
22 #include "qchart.h"
23 #include "qhorizontalstackedbarseries.h"
24 #include "qbarset.h"
25
26 class HorizontalStackedBarChart: public Chart
27 {
28 public:
29
30 HorizontalStackedBarChart(){
31 initialize();
32 }
33
34 QString name() { return QObject::tr("HorizontalStackedBarChart"); }
35 QString category() { return QObject::tr("BarSeries"); }
36 QString subCategory() { return QObject::tr("Vertical"); }
37
38 QChart* createChart(const DataTable& table)
39 {
40
41 QChart* chart = new QChart();
42
43 chart->setTitle("Horizontal stacked chart");
44
45 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries(chart);
46 for (int i(0); i < table.count(); i++) {
47 QBarSet *set = new QBarSet("Bar set " + QString::number(i));
48 foreach (Data data, table[i])
49 *set << data.first.y();
50 series->append(set);
51 }
52 chart->addSeries(series);
53 chart->createDefaultAxes();
54 return chart;
55 }
56
57 };
58
59 DECLARE_CHART(HorizontalStackedBarChart)
60
@@ -0,0 +1,60
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "charts.h"
22 #include "qchart.h"
23 #include "qbarseries.h"
24 #include "qbarset.h"
25
26 class VerticalBarChart: public Chart
27 {
28 public:
29
30 VerticalBarChart(){
31 initialize();
32 }
33
34 QString name() { return QObject::tr("VerticalBarChart"); }
35 QString category() { return QObject::tr("BarSeries"); }
36 QString subCategory() { return QObject::tr("Vertical"); }
37
38 QChart* createChart(const DataTable& table)
39 {
40
41 QChart* chart = new QChart();
42
43 chart->setTitle("Vertical bar chart");
44
45 QBarSeries* series = new QBarSeries(chart);
46 for (int i(0); i < table.count(); i++) {
47 QBarSet *set = new QBarSet("Bar set " + QString::number(i));
48 foreach (Data data, table[i])
49 *set << data.first.y();
50 series->append(set);
51 }
52 chart->addSeries(series);
53 chart->createDefaultAxes();
54 return chart;
55 }
56
57 };
58
59 DECLARE_CHART(VerticalBarChart)
60
@@ -0,0 +1,67
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #ifndef MODEL_H_
22 #define MODEL_H_
23
24 #include <QList>
25 #include <QPair>
26 #include <QPointF>
27 #include <QTime>
28
29 typedef QPair<QPointF, QString> Data;
30 typedef QList<Data> DataList;
31 typedef QList<DataList> DataTable;
32
33
34 class Model
35 {
36 private:
37 Model(){}
38
39 public:
40 static DataTable generateRandomData(int listCount, int valueMax, int valueCount)
41 {
42 DataTable dataTable;
43
44 // set seed for random stuff
45 qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
46
47 // generate random data
48 for (int i(0); i < listCount; i++) {
49 DataList dataList;
50 qreal yValue(0);
51 for (int j(0); j < valueCount; j++) {
52 yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount;
53 QPointF value(
54 (j + (qreal) qrand() / (qreal) RAND_MAX)
55 * ((qreal) valueMax / (qreal) valueCount), yValue);
56 QString label = "Slice " + QString::number(i) + ":" + QString::number(j);
57 dataList << Data(value, label);
58 }
59 dataTable << dataList;
60 }
61
62 return dataTable;
63 }
64
65 };
66
67 #endif
@@ -21,7 +21,7
21
21
22 #ifndef CHARTS_H
22 #ifndef CHARTS_H
23 #define CHARTS_H
23 #define CHARTS_H
24 #include "window.h"
24 #include "model.h"
25 #include <QList>
25 #include <QList>
26 #include <QString>
26 #include <QString>
27 #include <qchartglobal.h>
27 #include <qchartglobal.h>
@@ -1,4 +1,13
1 INCLUDEPATH += $$PWD
1 INCLUDEPATH += $$PWD
2 DEPENDPATH += $$PWD
2 DEPENDPATH += $$PWD
3
3
4 SOURCES+= linechart.cpp scatterchart.cpp splinechart.cpp piechart.cpp barchart.cpp areachart.cpp No newline at end of file
4 SOURCES+= \
5 linechart.cpp \
6 scatterchart.cpp \
7 splinechart.cpp \
8 piechart.cpp \
9 verticalstackedbarchart.cpp \
10 horizontalstackedbarchart.cpp \
11 verticalbarchart.cpp \
12 horizontalbarchart.cpp \
13 areachart.cpp No newline at end of file
@@ -23,11 +23,11
23 #include "qstackedbarseries.h"
23 #include "qstackedbarseries.h"
24 #include "qbarset.h"
24 #include "qbarset.h"
25
25
26 class BarChart: public Chart
26 class VerticalStackedBarChart: public Chart
27 {
27 {
28 public:
28 public:
29
29
30 BarChart(){
30 VerticalStackedBarChart(){
31 initialize();
31 initialize();
32 }
32 }
33
33
@@ -40,7 +40,7 public:
40
40
41 QChart* chart = new QChart();
41 QChart* chart = new QChart();
42
42
43 chart->setTitle("Bar chart");
43 chart->setTitle("Stacked bar chart");
44
44
45 QStackedBarSeries* series = new QStackedBarSeries(chart);
45 QStackedBarSeries* series = new QStackedBarSeries(chart);
46 for (int i(0); i < table.count(); i++) {
46 for (int i(0); i < table.count(); i++) {
@@ -56,5 +56,5 public:
56
56
57 };
57 };
58
58
59 DECLARE_CHART(BarChart)
59 DECLARE_CHART(VerticalStackedBarChart)
60
60
@@ -3,5 +3,5 include(charts/charts.pri)
3 TARGET = chartviewer
3 TARGET = chartviewer
4 QT += opengl
4 QT += opengl
5 SOURCES += main.cpp window.cpp view.cpp charts.cpp
5 SOURCES += main.cpp window.cpp view.cpp charts.cpp
6 HEADERS += window.h view.h charts.h
6 HEADERS += window.h view.h charts.h model.h
7
7
@@ -21,17 +21,7
21 #include "window.h"
21 #include "window.h"
22 #include "view.h"
22 #include "view.h"
23 #include "charts.h"
23 #include "charts.h"
24
25 #include <QChartView>
24 #include <QChartView>
26 #include <QPieSeries>
27 #include <QPieSlice>
28 #include <QPercentBarSeries>
29 #include <QStackedBarSeries>
30 #include <QBarSeries>
31 #include <QBarSet>
32 #include <QLineSeries>
33 #include <QSplineSeries>
34 #include <QScatterSeries>
35 #include <QAreaSeries>
25 #include <QAreaSeries>
36 #include <QLegend>
26 #include <QLegend>
37 #include <QGridLayout>
27 #include <QGridLayout>
@@ -41,8 +31,6
41 #include <QCheckBox>
31 #include <QCheckBox>
42 #include <QGroupBox>
32 #include <QGroupBox>
43 #include <QLabel>
33 #include <QLabel>
44 #include <QTime>
45 #include <QBarCategoriesAxis>
46 #include <QGraphicsScene>
34 #include <QGraphicsScene>
47 #include <QGraphicsGridLayout>
35 #include <QGraphicsGridLayout>
48 #include <QGraphicsLinearLayout>
36 #include <QGraphicsLinearLayout>
@@ -58,7 +46,7 Window::Window(QWidget* parent) :
58 m_valueCount(7),
46 m_valueCount(7),
59 m_scene(new QGraphicsScene(this)),
47 m_scene(new QGraphicsScene(this)),
60 m_view(0),
48 m_view(0),
61 m_dataTable(generateRandomData(m_listCount, m_valueMax, m_valueCount)),
49 m_dataTable(Model::generateRandomData(m_listCount, m_valueMax, m_valueCount)),
62 m_form(0),
50 m_form(0),
63 m_themeComboBox(0),
51 m_themeComboBox(0),
64 m_antialiasCheckBox(0),
52 m_antialiasCheckBox(0),
@@ -97,9 +85,8 Window::Window(QWidget* parent) :
97
85
98 Charts::ChartList list = Charts::chartList();
86 Charts::ChartList list = Charts::chartList();
99
87
100 for(int i = 0 ; i < 6 ; ++i)
88 for(int i = 0 ; i < 9 && i<list.size() ; ++i)
101 {
89 {
102 if(!(i<list.size()) || list.isEmpty()) break;
103 QChart* chart = list.at(i)->createChart(m_dataTable);
90 QChart* chart = list.at(i)->createChart(m_dataTable);
104 baseLayout->addItem(chart, i/3, i%3);
91 baseLayout->addItem(chart, i/3, i%3);
105 m_chartList << chart;
92 m_chartList << chart;
@@ -135,31 +122,6 void Window::connectSignals()
135 QObject::connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
122 QObject::connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
136 }
123 }
137
124
138 DataTable Window::generateRandomData(int listCount, int valueMax, int valueCount) const
139 {
140 DataTable dataTable;
141
142 // set seed for random stuff
143 qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
144
145 // generate random data
146 for (int i(0); i < listCount; i++) {
147 DataList dataList;
148 qreal yValue(0);
149 for (int j(0); j < valueCount; j++) {
150 yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount;
151 QPointF value(
152 (j + (qreal) qrand() / (qreal) RAND_MAX)
153 * ((qreal) m_valueMax / (qreal) valueCount), yValue);
154 QString label = "Slice " + QString::number(i) + ":" + QString::number(j);
155 dataList << Data(value, label);
156 }
157 dataTable << dataList;
158 }
159
160 return dataTable;
161 }
162
163 void Window::createProxyWidgets()
125 void Window::createProxyWidgets()
164 {
126 {
165 m_themeComboBox = createThemeBox();
127 m_themeComboBox = createThemeBox();
@@ -20,6 +20,7
20
20
21 #ifndef WINDOW_H_
21 #ifndef WINDOW_H_
22 #define WINDOW_H_
22 #define WINDOW_H_
23 #include "model.h"
23 #include <QMainWindow>
24 #include <QMainWindow>
24 #include <QChartGlobal>
25 #include <QChartGlobal>
25 #include <QHash>
26 #include <QHash>
@@ -35,10 +36,6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 class QChart;
36 class QChart;
36 QTCOMMERCIALCHART_END_NAMESPACE
37 QTCOMMERCIALCHART_END_NAMESPACE
37
38
38 typedef QPair<QPointF, QString> Data;
39 typedef QList<Data> DataList;
40 typedef QList<DataList> DataTable;
41
42 QTCOMMERCIALCHART_USE_NAMESPACE
39 QTCOMMERCIALCHART_USE_NAMESPACE
43
40
44
41
@@ -51,10 +48,7 public:
51
48
52 private Q_SLOTS:
49 private Q_SLOTS:
53 void updateUI();
50 void updateUI();
54
55
56 private:
51 private:
57 DataTable generateRandomData(int listCount,int valueMax,int valueCount) const;
58 QComboBox* createThemeBox();
52 QComboBox* createThemeBox();
59 QComboBox* createAnimationBox();
53 QComboBox* createAnimationBox();
60 QComboBox* createLegendBox();
54 QComboBox* createLegendBox();
General Comments 0
You need to be logged in to leave comments. Login now