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