##// END OF EJS Templates
proof of concept implementation for barset and barcategory
sauimone -
r169:1723c50daa1e
parent child
Show More
@@ -0,0 +1,26
1 #include "qbarcategory.h"
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
5 QBarCategory::QBarCategory()
6 {
7 }
8
9 QBarCategory& QBarCategory::operator << (const QString &label)
10 {
11 mList.append(label);
12 return *this;
13 }
14
15 int QBarCategory::count()
16 {
17 return mList.count();
18 }
19
20 QList<QString>& QBarCategory::items()
21 {
22 return mList;
23 }
24
25
26 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,27
1 #ifndef QBARCATEGORY_H
2 #define QBARCATEGORY_H
3
4 #include "qchartglobal.h"
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8 class QTCOMMERCIALCHART_EXPORT QBarCategory // : pubclic QObject // TODO: Need for this?
9 {
10 public:
11 QBarCategory();
12
13 QBarCategory& operator << (const QString &label);
14
15 // Number of items in category
16 int count();
17 QList<QString>& items();
18
19 public:
20
21 QList<QString> mList;
22
23 };
24
25 QTCOMMERCIALCHART_END_NAMESPACE
26
27 #endif // QBARCATEGORY_H
@@ -0,0 +1,20
1 #include "qbarset.h"
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
5 QBarSet::QBarSet()
6 {
7 }
8
9 void QBarSet::setName(QString name)
10 {
11 mName = name;
12 }
13
14 QBarSet& QBarSet::operator << (const qreal &value)
15 {
16 mValues.append(value);
17 return *this;
18 }
19
20 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,29
1 #ifndef QBARSET_H
2 #define QBARSET_H
3
4 #include "qchartglobal.h"
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8 class QTCOMMERCIALCHART_EXPORT QBarSet // : pubclic QObject // TODO: Need for this?
9 {
10 public:
11 QBarSet();
12
13 void setName(QString name);
14 // void setValues(QList<qreal> &values);
15
16 // TODO:
17 QBarSet& operator << (const qreal &value);
18
19 // TODO: Hide these from user of QBarSet. (but data model needs access to these)
20 public:
21
22 QString mName;
23 QList<qreal> mValues;
24
25 };
26
27 QTCOMMERCIALCHART_END_NAMESPACE
28
29 #endif // QBARSET_H
@@ -1,43 +1,49
1 #include <QApplication>
1 #include <QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <QStandardItemModel>
3 #include <QStandardItemModel>
4 #include <barchartseries.h>
4 #include <barchartseries.h>
5 #include <qbarcategory.h>
6 #include <qbarset.h>
5 #include "chartwidget.h"
7 #include "chartwidget.h"
6
8
7 QTCOMMERCIALCHART_USE_NAMESPACE
9 QTCOMMERCIALCHART_USE_NAMESPACE
8
10
9 int main(int argc, char *argv[])
11 int main(int argc, char *argv[])
10 {
12 {
11 QApplication a(argc, argv);
13 QApplication a(argc, argv);
12 QMainWindow window;
14 QMainWindow window;
13
15
14 BarChartSeries* series0 = new BarChartSeries();
16 QBarCategory category;
17 category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec";
18
19 BarChartSeries* series0 = new BarChartSeries(category);
20
21 QBarSet barSet0;
22 QBarSet barSet1;
23 QBarSet barSet2;
24 QBarSet barSet3;
25 QBarSet barSet4;
15
26
16 // Create some test data to chart
27 // Create some test data to chart
17 QList<qreal> data0;
28 barSet0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10;
18 data0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10;
29 barSet1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0;
19 QList<qreal> data1;
30 barSet2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1;
20 data1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0;
31 barSet3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5;
21 QList<qreal> data2;
32 barSet4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10;
22 data2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1;
33
23 QList<qreal> data3;
34 series0->addBarSet(barSet0);
24 data3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5;
35 series0->addBarSet(barSet1);
25 QList<qreal> data4;
36 series0->addBarSet(barSet2);
26 data4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10;
37 series0->addBarSet(barSet3);
27
38 series0->addBarSet(barSet4);
28 series0->addData(data0);
29 series0->addData(data1);
30 series0->addData(data2);
31 series0->addData(data3);
32 series0->addData(data4);
33
39
34 ChartWidget* chartWidget = new ChartWidget(&window);
40 ChartWidget* chartWidget = new ChartWidget(&window);
35 chartWidget->addSeries(series0);
41 chartWidget->addSeries(series0);
36
42
37 window.setCentralWidget(chartWidget);
43 window.setCentralWidget(chartWidget);
38 window.resize(400, 300);
44 window.resize(400, 300);
39 window.show();
45 window.show();
40
46
41 return a.exec();
47 return a.exec();
42 }
48 }
43
49
@@ -1,43 +1,47
1 #include <QApplication>
1 #include <QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <QStandardItemModel>
3 #include <QStandardItemModel>
4 #include <percentbarchartseries.h>
4 #include <percentbarchartseries.h>
5 #include "chartwidget.h"
5 #include "chartwidget.h"
6
6
7 QTCOMMERCIALCHART_USE_NAMESPACE
7 QTCOMMERCIALCHART_USE_NAMESPACE
8
8
9 int main(int argc, char *argv[])
9 int main(int argc, char *argv[])
10 {
10 {
11 QApplication a(argc, argv);
11 QApplication a(argc, argv);
12 QMainWindow window;
12 QMainWindow window;
13
13
14 PercentBarChartSeries* series0 = new PercentBarChartSeries();
14 PercentBarChartSeries* series0 = new PercentBarChartSeries();
15
15
16 // Create some test data to chart
16 // Create some test data to chart
17 QList<qreal> data0;
17 QList<qreal> data0;
18 data0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10;
18 data0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10;
19 QList<qreal> data1;
19 QList<qreal> data1;
20 data1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0;
20 data1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0;
21 QList<qreal> data2;
21 QList<qreal> data2;
22 data2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1;
22 data2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1;
23 QList<qreal> data3;
23 QList<qreal> data3;
24 data3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5;
24 data3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5;
25 QList<qreal> data4;
25 QList<qreal> data4;
26 data4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10;
26 data4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10;
27
27
28 series0->addData(data0);
28 series0->addData(data0);
29 series0->addData(data1);
29 series0->addData(data1);
30 series0->addData(data2);
30 series0->addData(data2);
31 series0->addData(data3);
31 series0->addData(data3);
32 series0->addData(data4);
32 series0->addData(data4);
33
33
34 QList<QString> labels;
35 labels << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec";
36 series0->setLabels(labels);
37
34 ChartWidget* chartWidget = new ChartWidget(&window);
38 ChartWidget* chartWidget = new ChartWidget(&window);
35 chartWidget->addSeries(series0);
39 chartWidget->addSeries(series0);
36
40
37 window.setCentralWidget(chartWidget);
41 window.setCentralWidget(chartWidget);
38 window.resize(400, 300);
42 window.resize(400, 300);
39 window.show();
43 window.show();
40
44
41 return a.exec();
45 return a.exec();
42 }
46 }
43
47
@@ -1,43 +1,47
1 #include <QApplication>
1 #include <QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <QStandardItemModel>
3 #include <QStandardItemModel>
4 #include <stackedbarchartseries.h>
4 #include <stackedbarchartseries.h>
5 #include "chartwidget.h"
5 #include "chartwidget.h"
6
6
7 QTCOMMERCIALCHART_USE_NAMESPACE
7 QTCOMMERCIALCHART_USE_NAMESPACE
8
8
9 int main(int argc, char *argv[])
9 int main(int argc, char *argv[])
10 {
10 {
11 QApplication a(argc, argv);
11 QApplication a(argc, argv);
12 QMainWindow window;
12 QMainWindow window;
13
13
14 StackedBarChartSeries* series0 = new StackedBarChartSeries();
14 StackedBarChartSeries* series0 = new StackedBarChartSeries();
15
15
16 // Create some test data to chart
16 // Create some test data to chart
17 QList<qreal> data0;
17 QList<qreal> data0;
18 data0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10;
18 data0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10;
19 QList<qreal> data1;
19 QList<qreal> data1;
20 data1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0;
20 data1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0;
21 QList<qreal> data2;
21 QList<qreal> data2;
22 data2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1;
22 data2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1;
23 QList<qreal> data3;
23 QList<qreal> data3;
24 data3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5;
24 data3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5;
25 QList<qreal> data4;
25 QList<qreal> data4;
26 data4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10;
26 data4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10;
27
27
28 series0->addData(data0);
28 series0->addData(data0);
29 series0->addData(data1);
29 series0->addData(data1);
30 series0->addData(data2);
30 series0->addData(data2);
31 series0->addData(data3);
31 series0->addData(data3);
32 series0->addData(data4);
32 series0->addData(data4);
33
33
34 QList<QString> labels;
35 labels << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec";
36 series0->setLabels(labels);
37
34 ChartWidget* chartWidget = new ChartWidget(&window);
38 ChartWidget* chartWidget = new ChartWidget(&window);
35 chartWidget->addSeries(series0);
39 chartWidget->addSeries(series0);
36
40
37 window.setCentralWidget(chartWidget);
41 window.setCentralWidget(chartWidget);
38 window.resize(400, 300);
42 window.resize(400, 300);
39 window.show();
43 window.show();
40
44
41 return a.exec();
45 return a.exec();
42 }
46 }
43
47
@@ -1,159 +1,175
1 #include <limits.h>
1 #include <limits.h>
2 #include <QVector>
2 #include <QVector>
3 #include <QDebug>
3 #include <QDebug>
4 #include "barchartmodel_p.h"
4 #include "barchartmodel_p.h"
5 #include "qbarcategory.h"
6 #include "qbarset.h"
5
7
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
9
8 BarChartModel::BarChartModel(QObject *parent) :
10 BarChartModel::BarChartModel(QBarCategory &category, QObject *parent) :
9 QObject(parent)
11 QObject(parent)
10 ,mRunningId(1)
12 ,mRunningId(1)
13 ,mCategory(category)
11 {
14 {
12 }
15 }
13
16
14 BarChartModel::~BarChartModel()
17 BarChartModel::~BarChartModel()
15 {
18 {
16 // qDebug() << "BarChartModel::~BarChartModel";
19 // qDebug() << "BarChartModel::~BarChartModel";
17 foreach (DataContainer* c, mDataModel) {
20 foreach (DataContainer* c, mDataModel) {
18 delete c;
21 delete c;
19 }
22 }
20 }
23 }
21
24
22 int BarChartModel::addData(QList<qreal> data)
25 int BarChartModel::addData(QList<qreal> data)
23 {
26 {
24 // qDebug() << "BarChartModel::addData" << data.count();
27 // qDebug() << "BarChartModel::addData" << data.count();
25 DataContainer* c = new DataContainer(data,mRunningId);
28 DataContainer* c = new DataContainer(data,mRunningId);
26 mDataModel.append(c);
29 mDataModel.append(c);
27 mRunningId++;
30 mRunningId++;
28 emit modelUpdated();
31 emit modelUpdated();
29 return mRunningId-1;
32 return mRunningId-1;
30 }
33 }
31
34
32 void BarChartModel::removeData(int id)
35 void BarChartModel::removeData(int id)
33 {
36 {
34 // qDebug() << "BarChartModel::removeData";
37 // qDebug() << "BarChartModel::removeData";
35 foreach(DataContainer* c, mDataModel) {
38 foreach(DataContainer* c, mDataModel) {
36 if (c->mId == id) {
39 if (c->mId == id) {
37 mDataModel.removeOne(c);
40 mDataModel.removeOne(c);
38 delete c;
41 delete c;
39 }
42 }
40 }
43 }
41 emit modelUpdated();
44 emit modelUpdated();
42 }
45 }
43
46
47 void BarChartModel::addBarSet(QBarSet &set)
48 {
49 DataContainer* c = new DataContainer(set.mValues,mRunningId);
50 mDataModel.append(c);
51 mRunningId++;
52 }
53
54 void BarChartModel::removeBarSet(QBarSet &set)
55 {
56 // TODO:
57 }
58
59
44 int BarChartModel::countRows()
60 int BarChartModel::countRows()
45 {
61 {
46 // qDebug() << "BarChartModel::countRows";
62 // qDebug() << "BarChartModel::countRows";
47 return mDataModel.count();
63 return mDataModel.count();
48 }
64 }
49
65
50 int BarChartModel::countColumns()
66 int BarChartModel::countColumns()
51 {
67 {
52 // qDebug() << "BarChartModel::countColumns";
68 // qDebug() << "BarChartModel::countColumns";
53 int count(0);
69 int count(0);
54 for (int i=0; i<mDataModel.count(); i++){
70 for (int i=0; i<mDataModel.count(); i++){
55 // TODO: can we assume that all series have same number of values? If not. then which values are empty?
71 // TODO: can we assume that all series have same number of values? If not. then which values are empty?
56 int temp = mDataModel.at(i)->countColumns();
72 int temp = mDataModel.at(i)->countColumns();
57 if (temp > count) {
73 if (temp > count) {
58 count = temp;
74 count = temp;
59 }
75 }
60 }
76 }
61 return count;
77 return count;
62 }
78 }
63
79
64 int BarChartModel::countTotalItems()
80 int BarChartModel::countTotalItems()
65 {
81 {
66 // qDebug() << "BarChartModel::countTotalItems";
82 // qDebug() << "BarChartModel::countTotalItems";
67 int total = mDataModel.count() * countColumns();
83 int total = mDataModel.count() * countColumns();
68 return total;
84 return total;
69 }
85 }
70
86
71 qreal BarChartModel::min()
87 qreal BarChartModel::min()
72 {
88 {
73 // qDebug() << "BarChartModel::min";
89 // qDebug() << "BarChartModel::min";
74 Q_ASSERT(mDataModel.count() > 0);
90 Q_ASSERT(mDataModel.count() > 0);
75 // TODO: make min and max members and update them when data changes.
91 // TODO: make min and max members and update them when data changes.
76 // This is slower since they are checked every time, even if data is same since previous call.
92 // This is slower since they are checked every time, even if data is same since previous call.
77 qreal min = INT_MAX;
93 qreal min = INT_MAX;
78
94
79 for (int i=0; i <mDataModel.count(); i++) {
95 for (int i=0; i <mDataModel.count(); i++) {
80 int itemCount = mDataModel.at(i)->countColumns();
96 int itemCount = mDataModel.at(i)->countColumns();
81 for (int j=0; j<itemCount; j++) {
97 for (int j=0; j<itemCount; j++) {
82 qreal temp = mDataModel.at(i)->valueAt(j);
98 qreal temp = mDataModel.at(i)->valueAt(j);
83 if (temp < min) {
99 if (temp < min) {
84 min = temp;
100 min = temp;
85 }
101 }
86 }
102 }
87 }
103 }
88 return min;
104 return min;
89 }
105 }
90
106
91 qreal BarChartModel::max()
107 qreal BarChartModel::max()
92 {
108 {
93 // qDebug() << "BarChartModel::max";
109 // qDebug() << "BarChartModel::max";
94 Q_ASSERT(mDataModel.count() > 0);
110 Q_ASSERT(mDataModel.count() > 0);
95
111
96 // TODO: make min and max members and update them when data changes.
112 // TODO: make min and max members and update them when data changes.
97 // This is slower since they are checked every time, even if data is same since previous call.
113 // This is slower since they are checked every time, even if data is same since previous call.
98 qreal max = INT_MIN;
114 qreal max = INT_MIN;
99
115
100 for (int i=0; i <mDataModel.count(); i++) {
116 for (int i=0; i <mDataModel.count(); i++) {
101 int itemCount = mDataModel.at(i)->countColumns();
117 int itemCount = mDataModel.at(i)->countColumns();
102 for (int j=0; j<itemCount; j++) {
118 for (int j=0; j<itemCount; j++) {
103 qreal temp = mDataModel.at(i)->valueAt(j);
119 qreal temp = mDataModel.at(i)->valueAt(j);
104 if (temp > max) {
120 if (temp > max) {
105 max = temp;
121 max = temp;
106 }
122 }
107 }
123 }
108 }
124 }
109
125
110 return max;
126 return max;
111 }
127 }
112
128
113 qreal BarChartModel::valueAt(int series, int item)
129 qreal BarChartModel::valueAt(int series, int item)
114 {
130 {
115 // qDebug() << "BarChartModel::valueAt" << series << item;
131 // qDebug() << "BarChartModel::valueAt" << series << item;
116 if ((series < 0) || (series >= mDataModel.count())) {
132 if ((series < 0) || (series >= mDataModel.count())) {
117 // No series, no value.
133 // No series, no value.
118 return 0;
134 return 0;
119 } else if ((item < 0) || (item >= mDataModel.at(series)->countColumns())) {
135 } else if ((item < 0) || (item >= mDataModel.at(series)->countColumns())) {
120 // No item, no value.
136 // No item, no value.
121 return 0;
137 return 0;
122 }
138 }
123
139
124 // qDebug() << "ValueAt" << series << item << "=" << mDataModel.at(series)->valueAt(item);
140 // qDebug() << "ValueAt" << series << item << "=" << mDataModel.at(series)->valueAt(item);
125 return mDataModel.at(series)->valueAt(item);
141 return mDataModel.at(series)->valueAt(item);
126 }
142 }
127
143
128 qreal BarChartModel::columnSum(int column)
144 qreal BarChartModel::columnSum(int column)
129 {
145 {
130 // qDebug() << "BarChartModel::columnSum";
146 // qDebug() << "BarChartModel::columnSum";
131 int sum(0);
147 int sum(0);
132 int count = mDataModel.count(); // Count rows
148 int count = mDataModel.count(); // Count rows
133
149
134 for (int row = 0; row < count; row++) {
150 for (int row = 0; row < count; row++) {
135 if (column < mDataModel.at(row)->countColumns()) {
151 if (column < mDataModel.at(row)->countColumns()) {
136 sum += mDataModel.at(row)->valueAt(column);
152 sum += mDataModel.at(row)->valueAt(column);
137 }
153 }
138 }
154 }
139 return sum;
155 return sum;
140 }
156 }
141
157
142 qreal BarChartModel::maxColumnSum()
158 qreal BarChartModel::maxColumnSum()
143 {
159 {
144 // qDebug() << "BarChartModel::maxColumnSum";
160 // qDebug() << "BarChartModel::maxColumnSum";
145 int max = INT_MIN;
161 int max = INT_MIN;
146 int count = countColumns();
162 int count = countColumns();
147
163
148 for (int col=0; col<count; col++) {
164 for (int col=0; col<count; col++) {
149 int sum = columnSum(col);
165 int sum = columnSum(col);
150 if (sum > max) {
166 if (sum > max) {
151 max = sum;
167 max = sum;
152 }
168 }
153 }
169 }
154 return max;
170 return max;
155 }
171 }
156
172
157 #include "moc_barchartmodel_p.cpp"
173 #include "moc_barchartmodel_p.cpp"
158
174
159 QTCOMMERCIALCHART_END_NAMESPACE
175 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,62 +1,69
1 #ifndef BARCHARTMODEL_H
1 #ifndef BARCHARTMODEL_H
2 #define BARCHARTMODEL_H
2 #define BARCHARTMODEL_H
3
3
4 #include <QObject>
4 #include <QObject>
5 #include "qchartglobal.h"
5 #include "qchartglobal.h"
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 // Model for bar chart. Internal class.
9 // Model for bar chart. Internal class.
10 // TODO: Implement as QAbstractItemModel?
10 // TODO: Implement as QAbstractItemModel?
11
11
12 class QBarSet;
13 class QBarCategory;
14
12 class BarChartModel : public QObject //, public QAbstractItemModel
15 class BarChartModel : public QObject //, public QAbstractItemModel
13 {
16 {
14 Q_OBJECT
17 Q_OBJECT
15 public:
18 public:
16 explicit BarChartModel(QObject *parent = 0);
19 explicit BarChartModel(QBarCategory &category, QObject *parent = 0);
17 ~BarChartModel();
20 ~BarChartModel();
18
21
19 // Adds data to model. returns id.
22 // TODO: remove these after add and remove QBarSet works.
20 int addData(QList<qreal> data);
23 int addData(QList<qreal> data);
21 void removeData(int id);
24 void removeData(int id);
22
25
26 void addBarSet(QBarSet &set);
27 void removeBarSet(QBarSet &set);
28
23 int countRows(); // Number of series in model
29 int countRows(); // Number of series in model
24 int countColumns(); // Maximum number of items in series
30 int countColumns(); // Maximum number of items in series
25 int countTotalItems(); // Total items in all series. Includes empty items.
31 int countTotalItems(); // Total items in all series. Includes empty items.
26
32
27 qreal max(); // Maximum value of all series
33 qreal max(); // Maximum value of all series
28 qreal min(); // Minimum value of all series
34 qreal min(); // Minimum value of all series
29 qreal valueAt(int series, int item);
35 qreal valueAt(int series, int item);
30
36
31 qreal columnSum(int column);
37 qreal columnSum(int column);
32 qreal maxColumnSum(); // returns maximum sum of items in all columns.
38 qreal maxColumnSum(); // returns maximum sum of items in all columns.
33
39
34 signals:
40 signals:
35 void modelUpdated();
41 void modelUpdated();
36
42
37 public slots:
43 public slots:
38
44
39 private:
45 private:
40
46
41 // Little helper class.
47 // Little helper class.
42 class DataContainer {
48 class DataContainer {
43 public:
49 public:
44 DataContainer(QList<qreal> data, int id) : mId(id), mData(data) {}
50 DataContainer(QList<qreal> data, int id) : mId(id), mData(data) {}
45 int countColumns() { return mData.count(); }
51 int countColumns() { return mData.count(); }
46 qreal valueAt(int item) { return mData.at(item); }
52 qreal valueAt(int item) { return mData.at(item); }
47
53
48 int mId; // TODO: Is this needed?
54 int mId; // TODO: Is this needed?
49 private:
55 private:
50 QList<qreal> mData;
56 QList<qreal> mData;
51 };
57 };
52
58
53 // Owned. N series. each has a list of values.
59 // Owned. N series. each has a list of values.
54 QList<DataContainer*> mDataModel;
60 QList<DataContainer*> mDataModel;
55 int mRunningId;
61 int mRunningId;
56 int mMaxColumns; // longest series in datamodel
62 int mMaxColumns; // longest series in datamodel
63 QBarCategory& mCategory;
57
64
58 };
65 };
59
66
60 QTCOMMERCIALCHART_END_NAMESPACE
67 QTCOMMERCIALCHART_END_NAMESPACE
61
68
62 #endif // BARCHARTMODEL_H
69 #endif // BARCHARTMODEL_H
@@ -1,13 +1,13
1 #include <QDebug>
1 #include <QDebug>
2 #include "barchartseries.h"
2 #include "barchartseries.h"
3
3
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5
5
6 BarChartSeries::BarChartSeries(QObject *parent)
6 BarChartSeries::BarChartSeries(QBarCategory &category, QObject *parent)
7 : BarChartSeriesBase(parent)
7 : BarChartSeriesBase(category, parent)
8 {
8 {
9 }
9 }
10
10
11 #include "moc_barchartseries.cpp"
11 #include "moc_barchartseries.cpp"
12
12
13 QTCOMMERCIALCHART_END_NAMESPACE
13 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,29 +1,29
1 #ifndef BARCHARTSERIES_H
1 #ifndef BARCHARTSERIES_H
2 #define BARCHARTSERIES_H
2 #define BARCHARTSERIES_H
3
3
4 #include <QList>
4 #include <QList>
5 #include <QAbstractItemModel>
5 #include <QAbstractItemModel>
6 #include "barchartseriesbase.h"
6 #include "barchartseriesbase.h"
7
7
8 class BarGroup;
8 class BarGroup;
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 // Container for series
12 // Container for series
13 class QTCOMMERCIALCHART_EXPORT BarChartSeries : public BarChartSeriesBase
13 class QTCOMMERCIALCHART_EXPORT BarChartSeries : public BarChartSeriesBase
14 {
14 {
15 Q_OBJECT
15 Q_OBJECT
16 public:
16 public:
17 BarChartSeries(QObject* parent=0);
17 BarChartSeries(QBarCategory &category, QObject* parent=0);
18
18
19 // from BarChartSeriesBase
19 // from BarChartSeriesBase
20 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; }
20 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; }
21
21
22 private:
22 private:
23
23
24 BarGroup* mBarGroup;
24 BarGroup* mBarGroup;
25 };
25 };
26
26
27 QTCOMMERCIALCHART_END_NAMESPACE
27 QTCOMMERCIALCHART_END_NAMESPACE
28
28
29 #endif // BARCHARTSERIES_H
29 #endif // BARCHARTSERIES_H
@@ -1,73 +1,84
1 #include <limits.h>
1 #include <limits.h>
2 #include <QDebug>
2 #include <QDebug>
3 #include "barchartseriesbase.h"
3 #include "barchartseriesbase.h"
4 #include "bargroup.h"
4 #include "bargroup.h"
5 #include "barchartmodel_p.h"
5 #include "barchartmodel_p.h"
6 #include "qbarset.h"
6
7
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
9
9 BarChartSeriesBase::BarChartSeriesBase(QObject *parent)
10 BarChartSeriesBase::BarChartSeriesBase(QBarCategory &category, QObject *parent)
10 : QChartSeries(parent)
11 : QChartSeries(parent)
11 ,mModel(new BarChartModel(this))
12 ,mModel(new BarChartModel(category, this))
12 {
13 {
13 }
14 }
14
15
15 int BarChartSeriesBase::addData(QList<qreal> data)
16 int BarChartSeriesBase::addData(QList<qreal> data)
16 {
17 {
17 return mModel->addData(data);
18 return mModel->addData(data);
18 }
19 }
19
20
20 void BarChartSeriesBase::removeData(int id)
21 void BarChartSeriesBase::removeData(int id)
21 {
22 {
22 mModel->removeData(id);
23 mModel->removeData(id);
23 }
24 }
24
25
25 void BarChartSeriesBase::setLabels(QList<QString> labels)
26 void BarChartSeriesBase::setLabels(QList<QString> labels)
26 {
27 {
27 mLabels = labels;
28 mLabels = labels;
28 }
29 }
29
30
31 void BarChartSeriesBase::addBarSet(QBarSet &set)
32 {
33 mModel->addBarSet(set);
34 }
35
36 void BarChartSeriesBase::removeBarSet(QBarSet &set)
37 {
38 mModel->removeBarSet(set);
39 }
40
30 qreal BarChartSeriesBase::min()
41 qreal BarChartSeriesBase::min()
31 {
42 {
32 return mModel->min();
43 return mModel->min();
33 }
44 }
34
45
35 qreal BarChartSeriesBase::max()
46 qreal BarChartSeriesBase::max()
36 {
47 {
37 return mModel->max();
48 return mModel->max();
38 }
49 }
39
50
40 int BarChartSeriesBase::countColumns()
51 int BarChartSeriesBase::countColumns()
41 {
52 {
42 return mModel->countColumns();
53 return mModel->countColumns();
43 }
54 }
44
55
45 qreal BarChartSeriesBase::valueAt(int series, int item)
56 qreal BarChartSeriesBase::valueAt(int series, int item)
46 {
57 {
47 // qDebug() << "BarChartSeriesBase::valueAt" << series << item;
58 // qDebug() << "BarChartSeriesBase::valueAt" << series << item;
48 return mModel->valueAt(series,item);
59 return mModel->valueAt(series,item);
49 }
60 }
50
61
51 qreal BarChartSeriesBase::maxColumnSum()
62 qreal BarChartSeriesBase::maxColumnSum()
52 {
63 {
53 // qDebug() << "BarChartSeriesBase::maxColumnSum" << mModel->maxColumnSum();
64 // qDebug() << "BarChartSeriesBase::maxColumnSum" << mModel->maxColumnSum();
54 return mModel->maxColumnSum();
65 return mModel->maxColumnSum();
55 }
66 }
56
67
57 BarChartModel& BarChartSeriesBase::model()
68 BarChartModel& BarChartSeriesBase::model()
58 {
69 {
59 return *mModel;
70 return *mModel;
60 }
71 }
61
72
62 QString BarChartSeriesBase::label(int item)
73 QString BarChartSeriesBase::label(int item)
63 {
74 {
64 if ((item>=0) && (item < mLabels.count())) {
75 if ((item>=0) && (item < mLabels.count())) {
65 return mLabels.at(item);
76 return mLabels.at(item);
66 }
77 }
67
78
68 return QString("");
79 return QString("");
69 }
80 }
70
81
71 #include "moc_barchartseriesbase.cpp"
82 #include "moc_barchartseriesbase.cpp"
72
83
73 QTCOMMERCIALCHART_END_NAMESPACE
84 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,55 +1,60
1 #ifndef BARCHARTSERIESBASE_H
1 #ifndef BARCHARTSERIESBASE_H
2 #define BARCHARTSERIESBASE_H
2 #define BARCHARTSERIESBASE_H
3
3
4 #include <QList>
4 #include <QList>
5 #include <QAbstractItemModel>
5 #include <QAbstractItemModel>
6 #include "qchartseries.h"
6 #include "qchartseries.h"
7 #include "qchartglobal.h"
7 #include "qchartglobal.h"
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 class BarGroupBase;
11 class BarGroupBase;
12 class BarChartModel;
12 class BarChartModel;
13 class QBarSet;
14 class QBarCategory;
13
15
14 // Container for series
16 // Container for series
15 class QTCOMMERCIALCHART_EXPORT BarChartSeriesBase : public QChartSeries
17 class QTCOMMERCIALCHART_EXPORT BarChartSeriesBase : public QChartSeries
16 {
18 {
17 Q_OBJECT
19 Q_OBJECT
18 protected:
20 protected:
19 BarChartSeriesBase(QObject* parent=0);
21 // BarChartSeriesBase(QObject* parent=0);
22 BarChartSeriesBase(QBarCategory &category, QObject* parent=0);
20
23
21 public:
24 public:
22 // from QChartSeries
25 // from QChartSeries
23 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeInvalid; }
26 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeInvalid; }
24
27
25 // TODO: << operator for convinience
28 // TODO: These 3 will be removed.
26 // Returns id for vector.
27 int addData(QList<qreal> data);
29 int addData(QList<qreal> data);
28 void removeData(int id);
30 void removeData(int id);
29 void setLabels(QList<QString> labels);
31 void setLabels(QList<QString> labels);
30
32
33 // TODO: Expose these to user in derived class instead of here? Common implementation for all bar charts, but not visible to user
34 void addBarSet(QBarSet &set); // Bob[1,5,6,2,15,4] first value goes to category 1 etc..
35 void removeBarSet(QBarSet &set); //
36
31 // These shouldn't be visible to chart series user. However, ChartDataSet needs to access them, and friends are evil.
37 // These shouldn't be visible to chart series user. However, ChartDataSet needs to access them, and friends are evil.
32 qreal min();
38 qreal min();
33 qreal max();
39 qreal max();
34 int countColumns(); // Count items in one series.
40 int countColumns(); // Count items in one series.
35 qreal valueAt(int series, int item);
41 qreal valueAt(int series, int item);
36 qreal maxColumnSum();
42 qreal maxColumnSum();
37
43
38 BarChartModel& model();
44 BarChartModel& model();
39 QString label(int item);
45 QString label(int item);
40
46
41 signals:
47 signals:
42 void changed(int index);
48 void changed(int index);
43
49
44 public Q_SLOTS:
50 public Q_SLOTS:
45
51
46 private:
52 private:
47
53
48 BarChartModel* mModel;
54 BarChartModel* mModel;
49
50 QList<QString> mLabels;
55 QList<QString> mLabels;
51 };
56 };
52
57
53 QTCOMMERCIALCHART_END_NAMESPACE
58 QTCOMMERCIALCHART_END_NAMESPACE
54
59
55 #endif // BARCHARTSERIESBASE_H
60 #endif // BARCHARTSERIESBASE_H
@@ -1,69 +1,69
1 #include "bargroup.h"
1 #include "bargroup.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barlabel_p.h"
3 #include "barlabel_p.h"
4 #include <QDebug>
4 #include <QDebug>
5
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
7
8 BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) :
8 BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) :
9 BarGroupBase(series,parent)
9 BarGroupBase(series,parent)
10 {
10 {
11 mBarDefaultWidth = 10;
11 mBarDefaultWidth = 10;
12 }
12 }
13
13
14 void BarGroup::layoutChanged()
14 void BarGroup::layoutChanged()
15 {
15 {
16 qDebug() << "BarGroup::layoutChanged";
16 // qDebug() << "BarGroup::layoutChanged";
17 // Scale bars to new layout
17 // Scale bars to new layout
18 // Layout for bars:
18 // Layout for bars:
19 if (mModel.countRows() <= 0) {
19 if (mModel.countRows() <= 0) {
20 return;
20 return;
21 }
21 }
22
22
23 if (childItems().count() == 0) {
23 if (childItems().count() == 0) {
24 qDebug() << "WARNING: BarGroup::layoutChanged called before graphics items are created!";
24 qDebug() << "WARNING: BarGroup::layoutChanged called before graphics items are created!";
25 return;
25 return;
26 }
26 }
27
27
28 // TODO: better way to auto-layout?
28 // TODO: better way to auto-layout?
29 // Use reals for accurancy (we might get some compiler warnings... :)
29 // Use reals for accurancy (we might get some compiler warnings... :)
30 int itemCount = mModel.countColumns();
30 int itemCount = mModel.countColumns();
31 int seriesCount = mModel.countRows();
31 int seriesCount = mModel.countRows();
32
32
33 qreal tW = mWidth;
33 qreal tW = mWidth;
34 qreal tH = mHeight;
34 qreal tH = mHeight;
35 qreal tM = mModel.max();
35 qreal tM = mModel.max();
36 qreal scale = (tH/tM);
36 qreal scale = (tH/tM);
37 qreal tC = itemCount+1;
37 qreal tC = itemCount+1;
38 qreal xStepPerSeries = (tW/tC);
38 qreal xStepPerSeries = (tW/tC);
39
39
40 // Scaling.
40 // Scaling.
41 int itemIndex(0);
41 int itemIndex(0);
42 int labelIndex = itemCount * seriesCount;
42 int labelIndex = itemCount * seriesCount;
43
43
44 for (int item=0; item < itemCount; item++) {
44 for (int item=0; item < itemCount; item++) {
45 qreal xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2));
45 qreal xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2));
46 qreal yPos = mHeight;
46 qreal yPos = mHeight;
47 for (int series = 0; series < seriesCount; series++) {
47 for (int series = 0; series < seriesCount; series++) {
48 qreal barHeight = mModel.valueAt(series, item) * scale;
48 qreal barHeight = mModel.valueAt(series, item) * scale;
49 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
49 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
50
50
51 // TODO: width settable per bar?
51 // TODO: width settable per bar?
52 bar->resize(mBarDefaultWidth, barHeight);
52 bar->resize(mBarDefaultWidth, barHeight);
53 bar->setColor(mColors.at(series));
53 bar->setColor(mColors.at(series));
54 bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight);
54 bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight);
55 itemIndex++;
55 itemIndex++;
56 xPos += mBarDefaultWidth;
56 xPos += mBarDefaultWidth;
57 }
57 }
58
58
59 // TODO: Layout for labels, remove magic number
59 // TODO: Layout for labels, remove magic number
60 xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2));
60 xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2));
61 BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex));
61 BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex));
62 label->setPos(xPos, mHeight + 20);
62 label->setPos(xPos, mHeight + 20);
63 labelIndex++;
63 labelIndex++;
64 }
64 }
65
65
66 mLayoutDirty = true;
66 mLayoutDirty = true;
67 }
67 }
68
68
69 QTCOMMERCIALCHART_END_NAMESPACE
69 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,17 +1,17
1 #include "percentbarchartseries.h"
1 #include "percentbarchartseries.h"
2
2
3 #include <limits.h>
3 #include <limits.h>
4 #include <QDebug>
4 #include <QDebug>
5 #include "percentbarchartseries.h"
5 #include "percentbarchartseries.h"
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 PercentBarChartSeries::PercentBarChartSeries(QObject *parent) :
9 PercentBarChartSeries::PercentBarChartSeries(QBarCategory &category, QObject *parent) :
10 BarChartSeriesBase(parent)
10 BarChartSeriesBase(category, parent)
11 {
11 {
12 }
12 }
13
13
14 #include "moc_percentbarchartseries.cpp"
14 #include "moc_percentbarchartseries.cpp"
15
15
16 QTCOMMERCIALCHART_END_NAMESPACE
16 QTCOMMERCIALCHART_END_NAMESPACE
17
17
@@ -1,31 +1,31
1 #ifndef PERCENTBARCHARTSERIES_H
1 #ifndef PERCENTBARCHARTSERIES_H
2 #define PERCENTBARCHARTSERIES_H
2 #define PERCENTBARCHARTSERIES_H
3
3
4 #include <QList>
4 #include <QList>
5 #include <QAbstractItemModel>
5 #include <QAbstractItemModel>
6 #include "barchartseriesbase.h"
6 #include "barchartseriesbase.h"
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class PercentBarGroup;
10 class PercentBarGroup;
11
11
12 class QTCOMMERCIALCHART_EXPORT PercentBarChartSeries : public BarChartSeriesBase
12 class QTCOMMERCIALCHART_EXPORT PercentBarChartSeries : public BarChartSeriesBase
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 public:
15 public:
16 PercentBarChartSeries(QObject* parent=0);
16 PercentBarChartSeries(QBarCategory &category, QObject* parent=0);
17
17
18 // from BarChartSeriesBase
18 // from BarChartSeriesBase
19 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypePercentBar; }
19 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypePercentBar; }
20
20
21 public Q_SLOTS:
21 public Q_SLOTS:
22
22
23 private:
23 private:
24
24
25 PercentBarGroup* mPercentBarGroup;
25 PercentBarGroup* mPercentBarGroup;
26 };
26 };
27
27
28 QTCOMMERCIALCHART_END_NAMESPACE
28 QTCOMMERCIALCHART_END_NAMESPACE
29
29
30
30
31 #endif // PERCENTBARCHARTSERIES_H
31 #endif // PERCENTBARCHARTSERIES_H
@@ -1,15 +1,15
1 #include <limits.h>
1 #include <limits.h>
2 #include <QDebug>
2 #include <QDebug>
3 #include "stackedbarchartseries.h"
3 #include "stackedbarchartseries.h"
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 StackedBarChartSeries::StackedBarChartSeries(QObject *parent) :
7 StackedBarChartSeries::StackedBarChartSeries(QBarCategory &category, QObject *parent) :
8 BarChartSeriesBase(parent)
8 BarChartSeriesBase(category, parent)
9 {
9 {
10 }
10 }
11
11
12 #include "moc_stackedbarchartseries.cpp"
12 #include "moc_stackedbarchartseries.cpp"
13
13
14 QTCOMMERCIALCHART_END_NAMESPACE
14 QTCOMMERCIALCHART_END_NAMESPACE
15
15
@@ -1,30 +1,30
1 #ifndef STACKEDBARCHARTSERIES_H
1 #ifndef STACKEDBARCHARTSERIES_H
2 #define STACKEDBARCHARTSERIES_H
2 #define STACKEDBARCHARTSERIES_H
3
3
4 #include <QList>
4 #include <QList>
5 #include <QAbstractItemModel>
5 #include <QAbstractItemModel>
6 #include "barchartseriesbase.h"
6 #include "barchartseriesbase.h"
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class StackedBarGroup;
10 class StackedBarGroup;
11
11
12 class QTCOMMERCIALCHART_EXPORT StackedBarChartSeries : public BarChartSeriesBase
12 class QTCOMMERCIALCHART_EXPORT StackedBarChartSeries : public BarChartSeriesBase
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 public:
15 public:
16 StackedBarChartSeries(QObject* parent=0);
16 StackedBarChartSeries(QBarCategory &category, QObject* parent=0);
17
17
18 // from QChartSeries
18 // from QChartSeries
19 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeStackedBar; }
19 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeStackedBar; }
20
20
21 public Q_SLOTS:
21 public Q_SLOTS:
22
22
23 private:
23 private:
24
24
25 StackedBarGroup* mStackedBarGroup;
25 StackedBarGroup* mStackedBarGroup;
26 };
26 };
27
27
28 QTCOMMERCIALCHART_END_NAMESPACE
28 QTCOMMERCIALCHART_END_NAMESPACE
29
29
30 #endif // STACKEDBARCHARTSERIES_H
30 #endif // STACKEDBARCHARTSERIES_H
@@ -1,83 +1,83
1 #include "stackedbargroup.h"
1 #include "stackedbargroup.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barlabel_p.h"
3 #include "barlabel_p.h"
4 #include "separator_p.h"
4 #include "separator_p.h"
5 #include <QDebug>
5 #include <QDebug>
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) :
9 StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) :
10 BarGroupBase(series,parent)
10 BarGroupBase(series,parent)
11 {
11 {
12 }
12 }
13
13
14 void StackedBarGroup::layoutChanged()
14 void StackedBarGroup::layoutChanged()
15 {
15 {
16 qDebug() << "StackedBarGroup::layoutChanged";
16 // qDebug() << "StackedBarGroup::layoutChanged";
17 // Scale bars to new layout
17 // Scale bars to new layout
18 // Layout for bars:
18 // Layout for bars:
19 if (mModel.countRows() <= 0) {
19 if (mModel.countRows() <= 0) {
20 // Nothing to do.
20 // Nothing to do.
21 return;
21 return;
22 }
22 }
23
23
24 if (mModel.countColumns() == 0) {
24 if (mModel.countColumns() == 0) {
25 // Nothing to do
25 // Nothing to do
26 return;
26 return;
27 }
27 }
28
28
29 if (childItems().count() == 0) {
29 if (childItems().count() == 0) {
30 qDebug() << "WARNING: StackedBarGroup::layoutChanged called before graphics items are created!";
30 qDebug() << "WARNING: StackedBarGroup::layoutChanged called before graphics items are created!";
31 return;
31 return;
32 }
32 }
33
33
34 // TODO: better way to auto-layout
34 // TODO: better way to auto-layout
35 // Use reals for accurancy (we might get some compiler warnings... :)
35 // Use reals for accurancy (we might get some compiler warnings... :)
36 // TODO: use temp variable for column count...
36 // TODO: use temp variable for column count...
37 qreal maxSum = mModel.maxColumnSum();
37 qreal maxSum = mModel.maxColumnSum();
38 qreal h = mHeight;
38 qreal h = mHeight;
39 qreal scale = (h / maxSum);
39 qreal scale = (h / maxSum);
40
40
41 int itemIndex(0);
41 int itemIndex(0);
42 qreal tW = mWidth;
42 qreal tW = mWidth;
43 qreal tC = mModel.countColumns() + 1;
43 qreal tC = mModel.countColumns() + 1;
44 qreal xStep = (tW/tC);
44 qreal xStep = (tW/tC);
45 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
45 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
46 int labelIndex = mModel.countRows() * mModel.countColumns();
46 int labelIndex = mModel.countRows() * mModel.countColumns();
47
47
48 for (int column = 0; column < mModel.countColumns(); column++) {
48 for (int column = 0; column < mModel.countColumns(); column++) {
49 qreal yPos = h;
49 qreal yPos = h;
50 for (int row=0; row < mModel.countRows(); row++) {
50 for (int row=0; row < mModel.countRows(); row++) {
51 qreal barHeight = mModel.valueAt(row, column) * scale;
51 qreal barHeight = mModel.valueAt(row, column) * scale;
52 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
52 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
53
53
54 bar->resize(mBarDefaultWidth, barHeight);
54 bar->resize(mBarDefaultWidth, barHeight);
55 bar->setColor(mColors.at(row));
55 bar->setColor(mColors.at(row));
56 bar->setPos(xPos, yPos-barHeight);
56 bar->setPos(xPos, yPos-barHeight);
57 itemIndex++;
57 itemIndex++;
58 yPos -= barHeight;
58 yPos -= barHeight;
59 }
59 }
60
60
61 // TODO: Layout for labels, remove magic number
61 // TODO: Layout for labels, remove magic number
62 BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex));
62 BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex));
63 label->setPos(xPos, mHeight + 20);
63 label->setPos(xPos, mHeight + 20);
64 labelIndex++;
64 labelIndex++;
65 xPos += xStep;
65 xPos += xStep;
66 }
66 }
67
67
68
68
69 // Position separators
69 // Position separators
70 int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these?
70 int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these?
71 xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left.
71 xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left.
72 for (int s=0; s < mModel.countColumns() - 1; s++) {
72 for (int s=0; s < mModel.countColumns() - 1; s++) {
73 Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex));
73 Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex));
74 sep->setPos(xPos,0);
74 sep->setPos(xPos,0);
75 sep->setSize(QSizeF(1,mHeight));
75 sep->setSize(QSizeF(1,mHeight));
76 xPos += xStep;
76 xPos += xStep;
77 separatorIndex++;
77 separatorIndex++;
78 }
78 }
79
79
80 mLayoutDirty = true;
80 mLayoutDirty = true;
81 }
81 }
82
82
83 QTCOMMERCIALCHART_END_NAMESPACE
83 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,194 +1,194
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qscatterseries.h"
2 #include "qscatterseries.h"
3 #include "qscatterseries_p.h"
3 #include "qscatterseries_p.h"
4 #include "qpieseries.h"
4 #include "qpieseries.h"
5 #include "qchartaxis.h"
5 #include "qchartaxis.h"
6 #include "chartpresenter_p.h"
6 #include "chartpresenter_p.h"
7 #include "chartdataset_p.h"
7 #include "chartdataset_p.h"
8
8
9 //series
9 //series
10 #include "barchartseries.h"
10 #include "barchartseries.h"
11 #include "stackedbarchartseries.h"
11 #include "stackedbarchartseries.h"
12 #include "percentbarchartseries.h"
12 #include "percentbarchartseries.h"
13 #include "qlinechartseries.h"
13 #include "qlinechartseries.h"
14
14
15 #include <QGraphicsScene>
15 #include <QGraphicsScene>
16 #include <QGraphicsSceneResizeEvent>
16 #include <QGraphicsSceneResizeEvent>
17 #include <QDebug>
17 #include <QDebug>
18
18
19 QTCOMMERCIALCHART_BEGIN_NAMESPACE
19 QTCOMMERCIALCHART_BEGIN_NAMESPACE
20
20
21 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
21 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
22 m_backgroundItem(0),
22 m_backgroundItem(0),
23 m_titleItem(0),
23 m_titleItem(0),
24 m_dataset(new ChartDataSet(this)),
24 m_dataset(new ChartDataSet(this)),
25 m_presenter(new ChartPresenter(this,m_dataset))
25 m_presenter(new ChartPresenter(this,m_dataset))
26 {
26 {
27 }
27 }
28
28
29 QChart::~QChart() {}
29 QChart::~QChart() {}
30
30
31 void QChart::addSeries(QChartSeries* series)
31 void QChart::addSeries(QChartSeries* series)
32 {
32 {
33 m_dataset->addSeries(series);
33 m_dataset->addSeries(series);
34 }
34 }
35
35
36 //TODO on review, is it really needed ??
36 //TODO on review, is it really needed ??
37 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
37 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
38 {
38 {
39 QChartSeries *series(0);
39 QChartSeries *series(0);
40
40
41 switch (type) {
41 switch (type) {
42 case QChartSeries::SeriesTypeLine: {
42 case QChartSeries::SeriesTypeLine: {
43 series = new QLineChartSeries(this);
43 series = new QLineChartSeries(this);
44 break;
44 break;
45 }
45 }
46 case QChartSeries::SeriesTypeBar: {
46 case QChartSeries::SeriesTypeBar: {
47 series = new BarChartSeries(this);
47 //series = new BarChartSeries(this);
48 break;
48 break;
49 }
49 }
50 case QChartSeries::SeriesTypeStackedBar: {
50 case QChartSeries::SeriesTypeStackedBar: {
51 series = new StackedBarChartSeries(this);
51 //series = new StackedBarChartSeries(this);
52 break;
52 break;
53 }
53 }
54 case QChartSeries::SeriesTypePercentBar: {
54 case QChartSeries::SeriesTypePercentBar: {
55 series = new PercentBarChartSeries(this);
55 //series = new PercentBarChartSeries(this);
56 break;
56 break;
57 }
57 }
58 case QChartSeries::SeriesTypeScatter: {
58 case QChartSeries::SeriesTypeScatter: {
59 series = new QScatterSeries(this);
59 series = new QScatterSeries(this);
60 break;
60 break;
61 }
61 }
62 case QChartSeries::SeriesTypePie: {
62 case QChartSeries::SeriesTypePie: {
63 series = new QPieSeries(this);
63 series = new QPieSeries(this);
64 break;
64 break;
65 }
65 }
66 default:
66 default:
67 Q_ASSERT(false);
67 Q_ASSERT(false);
68 break;
68 break;
69 }
69 }
70
70
71 addSeries(series);
71 addSeries(series);
72 return series;
72 return series;
73 }
73 }
74
74
75 void QChart::setChartBackgroundBrush(const QBrush& brush)
75 void QChart::setChartBackgroundBrush(const QBrush& brush)
76 {
76 {
77
77
78 if(!m_backgroundItem) {
78 if(!m_backgroundItem) {
79 m_backgroundItem = new QGraphicsRectItem(this);
79 m_backgroundItem = new QGraphicsRectItem(this);
80 m_backgroundItem->setZValue(-1);
80 m_backgroundItem->setZValue(-1);
81 }
81 }
82
82
83 m_backgroundItem->setBrush(brush);
83 m_backgroundItem->setBrush(brush);
84 m_backgroundItem->update();
84 m_backgroundItem->update();
85 }
85 }
86
86
87 void QChart::setChartBackgroundPen(const QPen& pen)
87 void QChart::setChartBackgroundPen(const QPen& pen)
88 {
88 {
89
89
90 if(!m_backgroundItem) {
90 if(!m_backgroundItem) {
91 m_backgroundItem = new QGraphicsRectItem(this);
91 m_backgroundItem = new QGraphicsRectItem(this);
92 m_backgroundItem->setZValue(-1);
92 m_backgroundItem->setZValue(-1);
93 }
93 }
94
94
95 m_backgroundItem->setPen(pen);
95 m_backgroundItem->setPen(pen);
96 m_backgroundItem->update();
96 m_backgroundItem->update();
97 }
97 }
98
98
99 void QChart::setTitle(const QString& title,const QFont& font)
99 void QChart::setTitle(const QString& title,const QFont& font)
100 {
100 {
101 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
101 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
102 m_titleItem->setPlainText(title);
102 m_titleItem->setPlainText(title);
103 m_titleItem->setFont(font);
103 m_titleItem->setFont(font);
104 }
104 }
105
105
106 int QChart::margin() const
106 int QChart::margin() const
107 {
107 {
108 return m_presenter->margin();
108 return m_presenter->margin();
109 }
109 }
110
110
111 void QChart::setMargin(int margin)
111 void QChart::setMargin(int margin)
112 {
112 {
113 m_presenter->setMargin(margin);
113 m_presenter->setMargin(margin);
114 }
114 }
115
115
116 void QChart::setChartTheme(QChart::ChartTheme theme)
116 void QChart::setChartTheme(QChart::ChartTheme theme)
117 {
117 {
118 m_presenter->setChartTheme(theme);
118 m_presenter->setChartTheme(theme);
119 }
119 }
120
120
121 QChart::ChartTheme QChart::chartTheme() const
121 QChart::ChartTheme QChart::chartTheme() const
122 {
122 {
123 return m_presenter->chartTheme();
123 return m_presenter->chartTheme();
124 }
124 }
125
125
126 void QChart::zoomInToRect(const QRectF& rectangle)
126 void QChart::zoomInToRect(const QRectF& rectangle)
127 {
127 {
128 m_presenter->zoomInToRect(rectangle);
128 m_presenter->zoomInToRect(rectangle);
129 }
129 }
130
130
131 void QChart::zoomIn()
131 void QChart::zoomIn()
132 {
132 {
133 m_presenter->zoomIn();
133 m_presenter->zoomIn();
134 }
134 }
135
135
136 void QChart::zoomOut()
136 void QChart::zoomOut()
137 {
137 {
138 m_presenter->zoomOut();
138 m_presenter->zoomOut();
139 }
139 }
140
140
141 void QChart::zoomReset()
141 void QChart::zoomReset()
142 {
142 {
143 m_presenter->zoomReset();
143 m_presenter->zoomReset();
144 }
144 }
145
145
146 QChartAxis* QChart::axisX()
146 QChartAxis* QChart::axisX()
147 {
147 {
148 return m_presenter->axisX();
148 return m_presenter->axisX();
149 }
149 }
150
150
151 QChartAxis* QChart::axisY()
151 QChartAxis* QChart::axisY()
152 {
152 {
153 return m_presenter->axisY();
153 return m_presenter->axisY();
154 }
154 }
155
155
156 QChartAxis* QChart::addAxisX()
156 QChartAxis* QChart::addAxisX()
157 {
157 {
158 return m_presenter->addAxisX();
158 return m_presenter->addAxisX();
159 }
159 }
160
160
161 QChartAxis* QChart::addAxisY()
161 QChartAxis* QChart::addAxisY()
162 {
162 {
163 return m_presenter->addAxisY();
163 return m_presenter->addAxisY();
164 }
164 }
165
165
166 void QChart::removeAxis(QChartAxis* axis)
166 void QChart::removeAxis(QChartAxis* axis)
167 {
167 {
168 m_presenter->removeAxis(axis);
168 m_presenter->removeAxis(axis);
169 }
169 }
170
170
171 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
171 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
172 {
172 {
173
173
174 m_rect = QRectF(QPoint(0,0),event->newSize());
174 m_rect = QRectF(QPoint(0,0),event->newSize());
175 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
175 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
176
176
177 // recalculate title position
177 // recalculate title position
178 if (m_titleItem) {
178 if (m_titleItem) {
179 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
179 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
180 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
180 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
181 }
181 }
182
182
183 //recalculate background gradient
183 //recalculate background gradient
184 if (m_backgroundItem) {
184 if (m_backgroundItem) {
185 m_backgroundItem->setRect(rect);
185 m_backgroundItem->setRect(rect);
186 }
186 }
187
187
188 QGraphicsWidget::resizeEvent(event);
188 QGraphicsWidget::resizeEvent(event);
189 update();
189 update();
190 }
190 }
191
191
192 #include "moc_qchart.cpp"
192 #include "moc_qchart.cpp"
193
193
194 QTCOMMERCIALCHART_END_NAMESPACE
194 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,114 +1,118
1 !include( ../common.pri ):error( Couldn't find the common.pri file! )
1 !include( ../common.pri ):error( Couldn't find the common.pri file! )
2 TARGET = QtCommercialChart
2 TARGET = QtCommercialChart
3 DESTDIR = $$CHART_BUILD_LIB_DIR
3 DESTDIR = $$CHART_BUILD_LIB_DIR
4 TEMPLATE = lib
4 TEMPLATE = lib
5 QT += core \
5 QT += core \
6 gui
6 gui
7 CONFIG += debug_and_release
7 CONFIG += debug_and_release
8 CONFIG(debug, debug|release):TARGET = QtCommercialChartd
8 CONFIG(debug, debug|release):TARGET = QtCommercialChartd
9 SOURCES += barchart/barchartseries.cpp \
9 SOURCES += barchart/barchartseries.cpp \
10 barchart/bargroup.cpp \
10 barchart/bargroup.cpp \
11 barchart/bar.cpp \
11 barchart/bar.cpp \
12 barchart/stackedbarchartseries.cpp \
12 barchart/stackedbarchartseries.cpp \
13 barchart/stackedbargroup.cpp \
13 barchart/stackedbargroup.cpp \
14 barchart/percentbarchartseries.cpp \
14 barchart/percentbarchartseries.cpp \
15 barchart/percentbargroup.cpp \
15 barchart/percentbargroup.cpp \
16 barchart/barlabel.cpp \
16 barchart/barlabel.cpp \
17 barchart/barchartmodel.cpp \
17 barchart/barchartmodel.cpp \
18 barchart/separator.cpp \
18 barchart/separator.cpp \
19 barchart/bargroupbase.cpp \
19 barchart/bargroupbase.cpp \
20 barchart/barchartseriesbase.cpp \
20 barchart/barchartseriesbase.cpp \
21 barchart/qbarset.cpp \
22 barchart/qbarcategory.cpp \
21 linechart/linechartanimationitem.cpp \
23 linechart/linechartanimationitem.cpp \
22 linechart/linechartitem.cpp \
24 linechart/linechartitem.cpp \
23 linechart/qlinechartseries.cpp \
25 linechart/qlinechartseries.cpp \
24 qscatterseries.cpp \
26 qscatterseries.cpp \
25 #scatterpresentation.cpp \
27 #scatterpresentation.cpp \
26 qchart.cpp \
28 qchart.cpp \
27 axisitem.cpp \
29 axisitem.cpp \
28 qchartview.cpp \
30 qchartview.cpp \
29 qchartseries.cpp \
31 qchartseries.cpp \
30 qchartaxis.cpp \
32 qchartaxis.cpp \
31 charttheme.cpp \
33 charttheme.cpp \
32 chartdataset.cpp \
34 chartdataset.cpp \
33 chartpresenter.cpp \
35 chartpresenter.cpp \
34 domain.cpp \
36 domain.cpp \
35 scatterpresenter.cpp
37 scatterpresenter.cpp
36 PRIVATE_HEADERS += linechart/linechartitem_p.h \
38 PRIVATE_HEADERS += linechart/linechartitem_p.h \
37 linechart/linechartanimationitem_p.h \
39 linechart/linechartanimationitem_p.h \
38 barchart/barlabel_p.h \
40 barchart/barlabel_p.h \
39 barchart/bar_p.h \
41 barchart/bar_p.h \
40 barchart/separator_p.h \
42 barchart/separator_p.h \
41 barchart/barchartmodel_p.h \
43 barchart/barchartmodel_p.h \
42 qscatterseries_p.h \
44 qscatterseries_p.h \
43 #scatterpresentation.h \
45 #scatterpresentation.h \
44 axisitem_p.h \
46 axisitem_p.h \
45 chartitem_p.h \
47 chartitem_p.h \
46 charttheme_p.h \
48 charttheme_p.h \
47 chartdataset_p.h \
49 chartdataset_p.h \
48 chartpresenter_p.h \
50 chartpresenter_p.h \
49 domain_p.h
51 domain_p.h
50 PUBLIC_HEADERS += linechart/qlinechartseries.h \
52 PUBLIC_HEADERS += linechart/qlinechartseries.h \
51 barchart/barchartseries.h \
53 barchart/barchartseries.h \
52 barchart/bargroup.h \
54 barchart/bargroup.h \
53 barchart/stackedbarchartseries.h \
55 barchart/stackedbarchartseries.h \
54 barchart/stackedbargroup.h \
56 barchart/stackedbargroup.h \
55 barchart/percentbarchartseries.h \
57 barchart/percentbarchartseries.h \
56 barchart/percentbargroup.h \
58 barchart/percentbargroup.h \
57 barchart/barchartseriesbase.h \
59 barchart/barchartseriesbase.h \
58 barchart/bargroupbase.h \
60 barchart/bargroupbase.h \
61 barchart/qbarset.h \
62 barchart/qbarcategory.h \
59 qchartseries.h \
63 qchartseries.h \
60 qscatterseries.h \
64 qscatterseries.h \
61 qchart.h \
65 qchart.h \
62 qchartglobal.h \
66 qchartglobal.h \
63 qchartview.h \
67 qchartview.h \
64 qchartaxis.h
68 qchartaxis.h
65
69
66 include(piechart/piechart.pri)
70 include(piechart/piechart.pri)
67
71
68 THEMES += themes/chartthemeicy_p.h \
72 THEMES += themes/chartthemeicy_p.h \
69 themes/chartthemegrayscale_p.h \
73 themes/chartthemegrayscale_p.h \
70 themes/chartthemescientific_p.h \
74 themes/chartthemescientific_p.h \
71 themes/chartthemevanilla_p.h
75 themes/chartthemevanilla_p.h
72 HEADERS += $$PUBLIC_HEADERS \
76 HEADERS += $$PUBLIC_HEADERS \
73 scatterpresenter.h
77 scatterpresenter.h
74 HEADERS += $$PRIVATE_HEADERS
78 HEADERS += $$PRIVATE_HEADERS
75 HEADERS += $$THEMES
79 HEADERS += $$THEMES
76 INCLUDEPATH += linechart \
80 INCLUDEPATH += linechart \
77 barchart \
81 barchart \
78 themes \
82 themes \
79 .
83 .
80 OBJECTS_DIR = $$CHART_BUILD_DIR/lib
84 OBJECTS_DIR = $$CHART_BUILD_DIR/lib
81 MOC_DIR = $$CHART_BUILD_DIR/lib
85 MOC_DIR = $$CHART_BUILD_DIR/lib
82 UI_DIR = $$CHART_BUILD_DIR/lib
86 UI_DIR = $$CHART_BUILD_DIR/lib
83 RCC_DIR = $$CHART_BUILD_DIR/lib
87 RCC_DIR = $$CHART_BUILD_DIR/lib
84 DEFINES += QTCOMMERCIALCHART_LIBRARY
88 DEFINES += QTCOMMERCIALCHART_LIBRARY
85 public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart
89 public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart
86 public_headers.files = $$PUBLIC_HEADERS
90 public_headers.files = $$PUBLIC_HEADERS
87 target.path = $$[QT_INSTALL_LIBS]
91 target.path = $$[QT_INSTALL_LIBS]
88 INSTALLS += target \
92 INSTALLS += target \
89 public_headers
93 public_headers
90 install_build_headers.name = bild_headers
94 install_build_headers.name = bild_headers
91 install_build_headers.output = $$CHART_BUILD_HEADER_DIR/${QMAKE_FILE_BASE}.h
95 install_build_headers.output = $$CHART_BUILD_HEADER_DIR/${QMAKE_FILE_BASE}.h
92 install_build_headers.input = PUBLIC_HEADERS
96 install_build_headers.input = PUBLIC_HEADERS
93 install_build_headers.commands = $$QMAKE_COPY \
97 install_build_headers.commands = $$QMAKE_COPY \
94 ${QMAKE_FILE_NAME} \
98 ${QMAKE_FILE_NAME} \
95 $$CHART_BUILD_HEADER_DIR
99 $$CHART_BUILD_HEADER_DIR
96 install_build_headers.CONFIG += target_predeps \
100 install_build_headers.CONFIG += target_predeps \
97 no_link
101 no_link
98 QMAKE_EXTRA_COMPILERS += install_build_headers
102 QMAKE_EXTRA_COMPILERS += install_build_headers
99 chartversion.target = qchartversion_p.h
103 chartversion.target = qchartversion_p.h
100 chartversion.commands = @echo \
104 chartversion.commands = @echo \
101 "build_time" \
105 "build_time" \
102 > \
106 > \
103 $$chartversion.target;
107 $$chartversion.target;
104 chartversion.depends = $$HEADERS \
108 chartversion.depends = $$HEADERS \
105 $$SOURCES
109 $$SOURCES
106 PRE_TARGETDEPS += qchartversion_p.h
110 PRE_TARGETDEPS += qchartversion_p.h
107 QMAKE_CLEAN += qchartversion_p.h
111 QMAKE_CLEAN += qchartversion_p.h
108 QMAKE_EXTRA_TARGETS += chartversion
112 QMAKE_EXTRA_TARGETS += chartversion
109 unix:QMAKE_DISTCLEAN += -r \
113 unix:QMAKE_DISTCLEAN += -r \
110 $$CHART_BUILD_HEADER_DIR \
114 $$CHART_BUILD_HEADER_DIR \
111 $$CHART_BUILD_LIB_DIR
115 $$CHART_BUILD_LIB_DIR
112 win32:QMAKE_DISTCLEAN += /Q \
116 win32:QMAKE_DISTCLEAN += /Q \
113 $$CHART_BUILD_HEADER_DIR \
117 $$CHART_BUILD_HEADER_DIR \
114 $$CHART_BUILD_LIB_DIR
118 $$CHART_BUILD_LIB_DIR
@@ -1,414 +1,414
1 #include "mainwidget.h"
1 #include "mainwidget.h"
2 #include "dataseriedialog.h"
2 #include "dataseriedialog.h"
3 #include "qchartseries.h"
3 #include "qchartseries.h"
4 #include "qpieseries.h"
4 #include "qpieseries.h"
5 #include <qlinechartseries.h>
5 #include <qlinechartseries.h>
6 #include <barchartseries.h>
6 #include <barchartseries.h>
7 #include <stackedbarchartseries.h>
7 #include <stackedbarchartseries.h>
8 #include <percentbarchartseries.h>
8 #include <percentbarchartseries.h>
9 #include <QPushButton>
9 #include <QPushButton>
10 #include <QComboBox>
10 #include <QComboBox>
11 #include <QSpinBox>
11 #include <QSpinBox>
12 #include <QCheckBox>
12 #include <QCheckBox>
13 #include <QGridLayout>
13 #include <QGridLayout>
14 #include <QHBoxLayout>
14 #include <QHBoxLayout>
15 #include <QLabel>
15 #include <QLabel>
16 #include <QSpacerItem>
16 #include <QSpacerItem>
17 #include <QMessageBox>
17 #include <QMessageBox>
18 #include <cmath>
18 #include <cmath>
19 #include <QDebug>
19 #include <QDebug>
20 #include <QStandardItemModel>
20 #include <QStandardItemModel>
21
21
22
22
23 QTCOMMERCIALCHART_USE_NAMESPACE
23 QTCOMMERCIALCHART_USE_NAMESPACE
24
24
25 MainWidget::MainWidget(QWidget *parent) :
25 MainWidget::MainWidget(QWidget *parent) :
26 QWidget(parent)
26 QWidget(parent)
27 {
27 {
28 m_chartWidget = new QChartView(this);
28 m_chartWidget = new QChartView(this);
29 m_chartWidget->setRubberBandPolicy(QChartView::HorizonalRubberBand);
29 m_chartWidget->setRubberBandPolicy(QChartView::HorizonalRubberBand);
30
30
31 // Grid layout for the controls for configuring the chart widget
31 // Grid layout for the controls for configuring the chart widget
32 QGridLayout *grid = new QGridLayout();
32 QGridLayout *grid = new QGridLayout();
33 QPushButton *addSeriesButton = new QPushButton("Add series");
33 QPushButton *addSeriesButton = new QPushButton("Add series");
34 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
34 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
35 grid->addWidget(addSeriesButton, 0, 1);
35 grid->addWidget(addSeriesButton, 0, 1);
36 initBackroundCombo(grid);
36 initBackroundCombo(grid);
37 initScaleControls(grid);
37 initScaleControls(grid);
38 initThemeCombo(grid);
38 initThemeCombo(grid);
39 QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom");
39 QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom");
40 connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartWidget, SLOT(setZoomEnabled(bool)));
40 connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartWidget, SLOT(setZoomEnabled(bool)));
41 zoomCheckBox->setChecked(true);
41 zoomCheckBox->setChecked(true);
42 grid->addWidget(zoomCheckBox, grid->rowCount(), 0);
42 grid->addWidget(zoomCheckBox, grid->rowCount(), 0);
43 // add row with empty label to make all the other rows static
43 // add row with empty label to make all the other rows static
44 grid->addWidget(new QLabel(""), grid->rowCount(), 0);
44 grid->addWidget(new QLabel(""), grid->rowCount(), 0);
45 grid->setRowStretch(grid->rowCount() - 1, 1);
45 grid->setRowStretch(grid->rowCount() - 1, 1);
46
46
47 // Another grid layout as a main layout
47 // Another grid layout as a main layout
48 QGridLayout *mainLayout = new QGridLayout();
48 QGridLayout *mainLayout = new QGridLayout();
49 mainLayout->addLayout(grid, 0, 0);
49 mainLayout->addLayout(grid, 0, 0);
50
50
51 // Init series type specific controls
51 // Init series type specific controls
52 initPieControls();
52 initPieControls();
53 mainLayout->addLayout(m_pieLayout, 2, 0);
53 mainLayout->addLayout(m_pieLayout, 2, 0);
54 // Scatter series specific settings
54 // Scatter series specific settings
55 // m_scatterLayout = new QGridLayout();
55 // m_scatterLayout = new QGridLayout();
56 // m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0);
56 // m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0);
57 // m_scatterLayout->setEnabled(false);
57 // m_scatterLayout->setEnabled(false);
58 // mainLayout->addLayout(m_scatterLayout, 1, 0);
58 // mainLayout->addLayout(m_scatterLayout, 1, 0);
59
59
60 // Add layouts and the chart widget to the main layout
60 // Add layouts and the chart widget to the main layout
61 mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1);
61 mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1);
62 setLayout(mainLayout);
62 setLayout(mainLayout);
63
63
64 // force an update to test data
64 // force an update to test data
65 testDataChanged(0);
65 testDataChanged(0);
66 }
66 }
67
67
68 // Combo box for selecting the chart's background
68 // Combo box for selecting the chart's background
69 void MainWidget::initBackroundCombo(QGridLayout *grid)
69 void MainWidget::initBackroundCombo(QGridLayout *grid)
70 {
70 {
71 QComboBox *backgroundCombo = new QComboBox(this);
71 QComboBox *backgroundCombo = new QComboBox(this);
72 backgroundCombo->addItem("Color");
72 backgroundCombo->addItem("Color");
73 backgroundCombo->addItem("Gradient");
73 backgroundCombo->addItem("Gradient");
74 backgroundCombo->addItem("Image");
74 backgroundCombo->addItem("Image");
75 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
75 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
76 this, SLOT(backgroundChanged(int)));
76 this, SLOT(backgroundChanged(int)));
77
77
78 grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0);
78 grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0);
79 grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1);
79 grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1);
80 }
80 }
81
81
82 // Scale related controls (auto-scale vs. manual min-max values)
82 // Scale related controls (auto-scale vs. manual min-max values)
83 void MainWidget::initScaleControls(QGridLayout *grid)
83 void MainWidget::initScaleControls(QGridLayout *grid)
84 {
84 {
85 m_autoScaleCheck = new QCheckBox("Automatic scaling");
85 m_autoScaleCheck = new QCheckBox("Automatic scaling");
86 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
86 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
87 // Allow setting also non-sense values (like -2147483648 and 2147483647)
87 // Allow setting also non-sense values (like -2147483648 and 2147483647)
88 m_xMinSpin = new QSpinBox();
88 m_xMinSpin = new QSpinBox();
89 m_xMinSpin->setMinimum(INT_MIN);
89 m_xMinSpin->setMinimum(INT_MIN);
90 m_xMinSpin->setMaximum(INT_MAX);
90 m_xMinSpin->setMaximum(INT_MAX);
91 m_xMinSpin->setValue(0);
91 m_xMinSpin->setValue(0);
92 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
92 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
93 m_xMaxSpin = new QSpinBox();
93 m_xMaxSpin = new QSpinBox();
94 m_xMaxSpin->setMinimum(INT_MIN);
94 m_xMaxSpin->setMinimum(INT_MIN);
95 m_xMaxSpin->setMaximum(INT_MAX);
95 m_xMaxSpin->setMaximum(INT_MAX);
96 m_xMaxSpin->setValue(10);
96 m_xMaxSpin->setValue(10);
97 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
97 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
98 m_yMinSpin = new QSpinBox();
98 m_yMinSpin = new QSpinBox();
99 m_yMinSpin->setMinimum(INT_MIN);
99 m_yMinSpin->setMinimum(INT_MIN);
100 m_yMinSpin->setMaximum(INT_MAX);
100 m_yMinSpin->setMaximum(INT_MAX);
101 m_yMinSpin->setValue(0);
101 m_yMinSpin->setValue(0);
102 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
102 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
103 m_yMaxSpin = new QSpinBox();
103 m_yMaxSpin = new QSpinBox();
104 m_yMaxSpin->setMinimum(INT_MIN);
104 m_yMaxSpin->setMinimum(INT_MIN);
105 m_yMaxSpin->setMaximum(INT_MAX);
105 m_yMaxSpin->setMaximum(INT_MAX);
106 m_yMaxSpin->setValue(10);
106 m_yMaxSpin->setValue(10);
107 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
107 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
108
108
109 grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0);
109 grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0);
110 grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0);
110 grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0);
111 grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1);
111 grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1);
112 grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0);
112 grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0);
113 grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1);
113 grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1);
114 grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0);
114 grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0);
115 grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1);
115 grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1);
116 grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0);
116 grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0);
117 grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1);
117 grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1);
118
118
119 m_autoScaleCheck->setChecked(true);
119 m_autoScaleCheck->setChecked(true);
120 }
120 }
121
121
122 // Combo box for selecting theme
122 // Combo box for selecting theme
123 void MainWidget::initThemeCombo(QGridLayout *grid)
123 void MainWidget::initThemeCombo(QGridLayout *grid)
124 {
124 {
125 QComboBox *chartTheme = new QComboBox();
125 QComboBox *chartTheme = new QComboBox();
126 chartTheme->addItem("Default");
126 chartTheme->addItem("Default");
127 chartTheme->addItem("Vanilla");
127 chartTheme->addItem("Vanilla");
128 chartTheme->addItem("Icy");
128 chartTheme->addItem("Icy");
129 chartTheme->addItem("Grayscale");
129 chartTheme->addItem("Grayscale");
130 chartTheme->addItem("Scientific");
130 chartTheme->addItem("Scientific");
131 chartTheme->addItem("Unnamed1");
131 chartTheme->addItem("Unnamed1");
132 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
132 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
133 this, SLOT(changeChartTheme(int)));
133 this, SLOT(changeChartTheme(int)));
134 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
134 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
135 grid->addWidget(chartTheme, 8, 1);
135 grid->addWidget(chartTheme, 8, 1);
136 }
136 }
137
137
138 void MainWidget::initPieControls()
138 void MainWidget::initPieControls()
139 {
139 {
140 // Pie series specific settings
140 // Pie series specific settings
141 // Pie size factory
141 // Pie size factory
142 QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox();
142 QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox();
143 pieSizeSpin->setMinimum(LONG_MIN);
143 pieSizeSpin->setMinimum(LONG_MIN);
144 pieSizeSpin->setMaximum(LONG_MAX);
144 pieSizeSpin->setMaximum(LONG_MAX);
145 pieSizeSpin->setValue(1.0);
145 pieSizeSpin->setValue(1.0);
146 pieSizeSpin->setSingleStep(0.1);
146 pieSizeSpin->setSingleStep(0.1);
147 connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double)));
147 connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double)));
148 // Pie position
148 // Pie position
149 QComboBox *piePosCombo = new QComboBox(this);
149 QComboBox *piePosCombo = new QComboBox(this);
150 piePosCombo->addItem("Maximized");
150 piePosCombo->addItem("Maximized");
151 piePosCombo->addItem("Top left");
151 piePosCombo->addItem("Top left");
152 piePosCombo->addItem("Top right");
152 piePosCombo->addItem("Top right");
153 piePosCombo->addItem("Bottom left");
153 piePosCombo->addItem("Bottom left");
154 piePosCombo->addItem("Bottom right");
154 piePosCombo->addItem("Bottom right");
155 connect(piePosCombo, SIGNAL(currentIndexChanged(int)),
155 connect(piePosCombo, SIGNAL(currentIndexChanged(int)),
156 this, SLOT(setPiePosition(int)));
156 this, SLOT(setPiePosition(int)));
157 m_pieLayout = new QGridLayout();
157 m_pieLayout = new QGridLayout();
158 m_pieLayout->setEnabled(false);
158 m_pieLayout->setEnabled(false);
159 m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0);
159 m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0);
160 m_pieLayout->addWidget(pieSizeSpin, 0, 1);
160 m_pieLayout->addWidget(pieSizeSpin, 0, 1);
161 m_pieLayout->addWidget(new QLabel("Pie position"), 1, 0);
161 m_pieLayout->addWidget(new QLabel("Pie position"), 1, 0);
162 m_pieLayout->addWidget(piePosCombo, 1, 1);
162 m_pieLayout->addWidget(piePosCombo, 1, 1);
163 }
163 }
164
164
165 void MainWidget::addSeries()
165 void MainWidget::addSeries()
166 {
166 {
167 DataSerieDialog dialog(m_defaultSeriesName, this);
167 DataSerieDialog dialog(m_defaultSeriesName, this);
168 connect(&dialog, SIGNAL(accepted(QString, QString)), this, SLOT(addSeries(QString, QString)));
168 connect(&dialog, SIGNAL(accepted(QString, QString)), this, SLOT(addSeries(QString, QString)));
169 dialog.exec();
169 dialog.exec();
170 }
170 }
171
171
172 void MainWidget::addSeries(QString series, QString data)
172 void MainWidget::addSeries(QString series, QString data)
173 {
173 {
174 qDebug() << "addSeries: " << series << " data: " << data;
174 qDebug() << "addSeries: " << series << " data: " << data;
175 m_defaultSeriesName = series;
175 m_defaultSeriesName = series;
176
176
177 // TODO: a dedicated data class for storing x and y values
177 // TODO: a dedicated data class for storing x and y values
178 QList<qreal> x;
178 QList<qreal> x;
179 QList<qreal> y;
179 QList<qreal> y;
180
180
181 QList<qreal> data0;
181 QList<qreal> data0;
182 QList<qreal> data1;
182 QList<qreal> data1;
183 QList<qreal> data2;
183 QList<qreal> data2;
184 QList<qreal> data3;
184 QList<qreal> data3;
185 QList<qreal> data4;
185 QList<qreal> data4;
186
186
187 if (data == "linear") {
187 if (data == "linear") {
188 for (int i = 0; i < 20; i++) {
188 for (int i = 0; i < 20; i++) {
189 x.append(i);
189 x.append(i);
190 y.append(i);
190 y.append(i);
191 }
191 }
192 } else if (data == "linear, 1M") {
192 } else if (data == "linear, 1M") {
193 // 1 million data points from 0.0001 to 100
193 // 1 million data points from 0.0001 to 100
194 // TODO: What is the requirement? Should we be able to show this kind of data with
194 // TODO: What is the requirement? Should we be able to show this kind of data with
195 // reasonable performance, or can we expect the application developer to do "data mining"
195 // reasonable performance, or can we expect the application developer to do "data mining"
196 // for us, so that the count of data points given to QtCommercial Chart is always
196 // for us, so that the count of data points given to QtCommercial Chart is always
197 // reasonable?
197 // reasonable?
198 for (qreal i = 0; i < 100; i += 0.0001) {
198 for (qreal i = 0; i < 100; i += 0.0001) {
199 x.append(i);
199 x.append(i);
200 y.append(20);
200 y.append(20);
201 }
201 }
202 } else if (data == "SIN") {
202 } else if (data == "SIN") {
203 for (int i = 0; i < 100; i++) {
203 for (int i = 0; i < 100; i++) {
204 x.append(i);
204 x.append(i);
205 y.append(abs(sin(3.14159265358979 / 50 * i) * 100));
205 y.append(abs(sin(3.14159265358979 / 50 * i) * 100));
206 }
206 }
207 } else if (data == "SIN + random") {
207 } else if (data == "SIN + random") {
208 for (qreal i = 0; i < 100; i += 0.1) {
208 for (qreal i = 0; i < 100; i += 0.1) {
209 x.append(i + (rand() % 5));
209 x.append(i + (rand() % 5));
210 y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
210 y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
211 }
211 }
212 } else if (data == "Table, 5 series"){
212 } else if (data == "Table, 5 series"){
213 // Create some test data to chart
213 // Create some test data to chart
214 data0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10;
214 data0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 5;
215 data1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0;
215 data1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4;
216 data2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1;
216 data2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3;
217 data3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5;
217 data3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2;
218 data4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10;
218 data4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1;
219 } else {
219 } else {
220 // TODO: check if data has a valid file name
220 // TODO: check if data has a valid file name
221 Q_ASSERT(false);
221 Q_ASSERT(false);
222 }
222 }
223
223
224 // TODO: color of the series
224 // TODO: color of the series
225 QChartSeries *newSeries = 0;
225 QChartSeries *newSeries = 0;
226 if (series == "Scatter") {
226 if (series == "Scatter") {
227 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeScatter);
227 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeScatter);
228 Q_ASSERT(newSeries->setData(x, y));
228 Q_ASSERT(newSeries->setData(x, y));
229 } else if (series == "Pie") {
229 } else if (series == "Pie") {
230 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie);
230 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie);
231 Q_ASSERT(newSeries->setData(y));
231 Q_ASSERT(newSeries->setData(y));
232 } else if (series == "Line") {
232 } else if (series == "Line") {
233 // TODO: adding data to an existing line series does not give any visuals for some reason
233 // TODO: adding data to an existing line series does not give any visuals for some reason
234 // newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeLine);
234 // newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeLine);
235 // QXYChartSeries *lineSeries = static_cast<QXYChartSeries *>(newSeries);
235 // QXYChartSeries *lineSeries = static_cast<QXYChartSeries *>(newSeries);
236 // lineSeries->setColor(Qt::blue);
236 // lineSeries->setColor(Qt::blue);
237 // for (int i(0); i < x.count() && i < y.count(); i++) {
237 // for (int i(0); i < x.count() && i < y.count(); i++) {
238 // lineSeries->add(x.at(i), y.at(i));
238 // lineSeries->add(x.at(i), y.at(i));
239 // }
239 // }
240 //Q_ASSERT(newSeries->setData(x, y));
240 //Q_ASSERT(newSeries->setData(x, y));
241 QLineChartSeries* series0 = new QLineChartSeries();
241 QLineChartSeries* series0 = new QLineChartSeries();
242 for (int i(0); i < x.count() && i < y.count(); i++)
242 for (int i(0); i < x.count() && i < y.count(); i++)
243 series0->add(x.at(i), y.at(i));
243 series0->add(x.at(i), y.at(i));
244 m_chartWidget->addSeries(series0);
244 m_chartWidget->addSeries(series0);
245 newSeries = series0;
245 newSeries = series0;
246 } else if (series == "Bar") {
246 } else if (series == "Bar") {
247 qDebug() << "Bar chart series";
247 qDebug() << "Bar chart series";
248 BarChartSeries* series0 = new BarChartSeries(this);
248 BarChartSeries* series0 = new BarChartSeries(this);
249 series0->addData(data0);
249 series0->addData(data0);
250 series0->addData(data1);
250 series0->addData(data1);
251 series0->addData(data2);
251 series0->addData(data2);
252 series0->addData(data3);
252 series0->addData(data3);
253 series0->addData(data4);
253 series0->addData(data4);
254 QList<QString> labels;
254 QList<QString> labels;
255 labels << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec";
255 labels << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec";
256 series0->setLabels(labels);
256 series0->setLabels(labels);
257 m_chartWidget->addSeries(series0);
257 m_chartWidget->addSeries(series0);
258 newSeries = series0;
258 newSeries = series0;
259 } else if (series == "StackedBar") {
259 } else if (series == "StackedBar") {
260 qDebug() << "Bar chart series";
260 qDebug() << "Bar chart series";
261 StackedBarChartSeries* series0 = new StackedBarChartSeries(this);
261 StackedBarChartSeries* series0 = new StackedBarChartSeries(this);
262 series0->addData(data0);
262 series0->addData(data0);
263 series0->addData(data1);
263 series0->addData(data1);
264 series0->addData(data2);
264 series0->addData(data2);
265 series0->addData(data3);
265 series0->addData(data3);
266 series0->addData(data4);
266 series0->addData(data4);
267 QList<QString> labels;
267 QList<QString> labels;
268 labels << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec";
268 labels << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec";
269 series0->setLabels(labels);
269 series0->setLabels(labels);
270 m_chartWidget->addSeries(series0);
270 m_chartWidget->addSeries(series0);
271 newSeries = series0;
271 newSeries = series0;
272 } else if (series == "PercentBar") {
272 } else if (series == "PercentBar") {
273 qDebug() << "Bar chart series";
273 qDebug() << "Bar chart series";
274 PercentBarChartSeries* series0 = new PercentBarChartSeries(this);
274 PercentBarChartSeries* series0 = new PercentBarChartSeries(this);
275 series0->addData(data0);
275 series0->addData(data0);
276 series0->addData(data1);
276 series0->addData(data1);
277 series0->addData(data2);
277 series0->addData(data2);
278 series0->addData(data3);
278 series0->addData(data3);
279 series0->addData(data4);
279 series0->addData(data4);
280 QList<QString> labels;
280 QList<QString> labels;
281 labels << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec";
281 labels << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec";
282 series0->setLabels(labels);
282 series0->setLabels(labels);
283 m_chartWidget->addSeries(series0);
283 m_chartWidget->addSeries(series0);
284 newSeries = series0;
284 newSeries = series0;
285 } else {
285 } else {
286 qDebug() << "Something weird going on in MainWidget::addSeries";
286 qDebug() << "Something weird going on in MainWidget::addSeries";
287 }
287 }
288
288
289 setCurrentSeries(newSeries);
289 setCurrentSeries(newSeries);
290 }
290 }
291
291
292 void MainWidget::setCurrentSeries(QChartSeries *series)
292 void MainWidget::setCurrentSeries(QChartSeries *series)
293 {
293 {
294 m_currentSeries = series;
294 m_currentSeries = series;
295 switch (m_currentSeries->type()) {
295 switch (m_currentSeries->type()) {
296 case QChartSeries::SeriesTypeLine:
296 case QChartSeries::SeriesTypeLine:
297 break;
297 break;
298 case QChartSeries::SeriesTypeScatter:
298 case QChartSeries::SeriesTypeScatter:
299 break;
299 break;
300 case QChartSeries::SeriesTypePie:
300 case QChartSeries::SeriesTypePie:
301 break;
301 break;
302 case QChartSeries::SeriesTypeBar:
302 case QChartSeries::SeriesTypeBar:
303 qDebug() << "setCurrentSeries (bar)";
303 qDebug() << "setCurrentSeries (bar)";
304 break;
304 break;
305 case QChartSeries::SeriesTypeStackedBar:
305 case QChartSeries::SeriesTypeStackedBar:
306 qDebug() << "setCurrentSeries (Stackedbar)";
306 qDebug() << "setCurrentSeries (Stackedbar)";
307 break;
307 break;
308 case QChartSeries::SeriesTypePercentBar:
308 case QChartSeries::SeriesTypePercentBar:
309 qDebug() << "setCurrentSeries (Percentbar)";
309 qDebug() << "setCurrentSeries (Percentbar)";
310 break;
310 break;
311 default:
311 default:
312 Q_ASSERT(false);
312 Q_ASSERT(false);
313 break;
313 break;
314 }
314 }
315 }
315 }
316
316
317 void MainWidget::testDataChanged(int itemIndex)
317 void MainWidget::testDataChanged(int itemIndex)
318 {
318 {
319 qDebug() << "testDataChanged: " << itemIndex;
319 qDebug() << "testDataChanged: " << itemIndex;
320
320
321 // switch (itemIndex) {
321 // switch (itemIndex) {
322 // case 0: {
322 // case 0: {
323 // QList<QChartDataPoint> data;
323 // QList<QChartDataPoint> data;
324 // for (int x = 0; x < 20; x++) {
324 // for (int x = 0; x < 20; x++) {
325 // data.append(QChartDataPoint() << x << x / 2);
325 // data.append(QChartDataPoint() << x << x / 2);
326 // }
326 // }
327 // m_chartWidget->setData(data);
327 // m_chartWidget->setData(data);
328 // break;
328 // break;
329 // }
329 // }
330 // case 1: {
330 // case 1: {
331 // QList<QChartDataPoint> data;
331 // QList<QChartDataPoint> data;
332 // for (int x = 0; x < 100; x++) {
332 // for (int x = 0; x < 100; x++) {
333 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100));
333 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100));
334 // }
334 // }
335 // m_chartWidget->setData(data);
335 // m_chartWidget->setData(data);
336 // break;
336 // break;
337 // }
337 // }
338 // case 2: {
338 // case 2: {
339 // QList<QChartDataPoint> data;
339 // QList<QChartDataPoint> data;
340 // for (int x = 0; x < 1000; x++) {
340 // for (int x = 0; x < 1000; x++) {
341 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
341 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
342 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
342 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
343 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
343 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
344 // }
344 // }
345 // m_chartWidget->setData(data);
345 // m_chartWidget->setData(data);
346 // break;
346 // break;
347 // }
347 // }
348 // default:
348 // default:
349 // break;
349 // break;
350 // }
350 // }
351 }
351 }
352
352
353 void MainWidget::backgroundChanged(int itemIndex)
353 void MainWidget::backgroundChanged(int itemIndex)
354 {
354 {
355 qDebug() << "backgroundChanged: " << itemIndex;
355 qDebug() << "backgroundChanged: " << itemIndex;
356 }
356 }
357
357
358 void MainWidget::autoScaleChanged(int value)
358 void MainWidget::autoScaleChanged(int value)
359 {
359 {
360 if (value) {
360 if (value) {
361 // TODO: enable auto scaling
361 // TODO: enable auto scaling
362 } else {
362 } else {
363 // TODO: set scaling manually (and disable auto scaling)
363 // TODO: set scaling manually (and disable auto scaling)
364 }
364 }
365
365
366 m_xMinSpin->setEnabled(!value);
366 m_xMinSpin->setEnabled(!value);
367 m_xMaxSpin->setEnabled(!value);
367 m_xMaxSpin->setEnabled(!value);
368 m_yMinSpin->setEnabled(!value);
368 m_yMinSpin->setEnabled(!value);
369 m_yMaxSpin->setEnabled(!value);
369 m_yMaxSpin->setEnabled(!value);
370 }
370 }
371
371
372 void MainWidget::xMinChanged(int value)
372 void MainWidget::xMinChanged(int value)
373 {
373 {
374 qDebug() << "xMinChanged: " << value;
374 qDebug() << "xMinChanged: " << value;
375 }
375 }
376
376
377 void MainWidget::xMaxChanged(int value)
377 void MainWidget::xMaxChanged(int value)
378 {
378 {
379 qDebug() << "xMaxChanged: " << value;
379 qDebug() << "xMaxChanged: " << value;
380 }
380 }
381
381
382 void MainWidget::yMinChanged(int value)
382 void MainWidget::yMinChanged(int value)
383 {
383 {
384 qDebug() << "yMinChanged: " << value;
384 qDebug() << "yMinChanged: " << value;
385 }
385 }
386
386
387 void MainWidget::yMaxChanged(int value)
387 void MainWidget::yMaxChanged(int value)
388 {
388 {
389 qDebug() << "yMaxChanged: " << value;
389 qDebug() << "yMaxChanged: " << value;
390 }
390 }
391
391
392 void MainWidget::changeChartTheme(int themeIndex)
392 void MainWidget::changeChartTheme(int themeIndex)
393 {
393 {
394 qDebug() << "changeChartTheme: " << themeIndex;
394 qDebug() << "changeChartTheme: " << themeIndex;
395 m_chartWidget->setChartTheme((QChart::ChartTheme) themeIndex);
395 m_chartWidget->setChartTheme((QChart::ChartTheme) themeIndex);
396 //TODO: remove this hack. This is just to make it so that theme change is seen immediately.
396 //TODO: remove this hack. This is just to make it so that theme change is seen immediately.
397 QSize s = size();
397 QSize s = size();
398 s.setWidth(s.width()+1);
398 s.setWidth(s.width()+1);
399 resize(s);
399 resize(s);
400 }
400 }
401
401
402 void MainWidget::setPieSizeFactor(double size)
402 void MainWidget::setPieSizeFactor(double size)
403 {
403 {
404 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
404 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
405 if (pie)
405 if (pie)
406 pie->setSizeFactor(qreal(size));
406 pie->setSizeFactor(qreal(size));
407 }
407 }
408
408
409 void MainWidget::setPiePosition(int position)
409 void MainWidget::setPiePosition(int position)
410 {
410 {
411 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
411 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
412 if (pie)
412 if (pie)
413 pie->setPosition((QPieSeries::PiePosition) position);
413 pie->setPosition((QPieSeries::PiePosition) position);
414 }
414 }
General Comments 0
You need to be logged in to leave comments. Login now