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