@@ -1,88 +1,43 | |||||
1 | #include <QApplication> |
|
1 | #include <QApplication> | |
2 | #include <QMainWindow> |
|
2 | #include <QMainWindow> | |
3 | #include <QStandardItemModel> |
|
3 | #include <QStandardItemModel> | |
4 | #include <barchartseries.h> |
|
4 | #include <barchartseries.h> | |
5 | #include "chartwidget.h" |
|
5 | #include "chartwidget.h" | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
7 | QTCOMMERCIALCHART_USE_NAMESPACE | |
8 |
|
8 | |||
9 | int main(int argc, char *argv[]) |
|
9 | int main(int argc, char *argv[]) | |
10 | { |
|
10 | { | |
11 | QApplication a(argc, argv); |
|
11 | QApplication a(argc, argv); | |
12 | QMainWindow window; |
|
12 | QMainWindow window; | |
13 |
|
13 | |||
14 | BarChartSeries* series0 = new BarChartSeries(); |
|
14 | BarChartSeries* series0 = new BarChartSeries(); | |
15 |
|
15 | |||
16 | // Create some test data to chart |
|
16 | // Create some test data to chart | |
17 | QStandardItemModel dataModel(5,10); |
|
17 | QList<qreal> data0; | |
18 | QModelIndex index; |
|
18 | data0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10; | |
19 | index = dataModel.index(0,0); |
|
19 | QList<qreal> data1; | |
20 |
|
20 | data1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0; | ||
21 | // Series 0 |
|
21 | QList<qreal> data2; | |
22 | dataModel.setData(dataModel.index(0,0),1); |
|
22 | data2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1; | |
23 | dataModel.setData(dataModel.index(0,1),2); |
|
23 | QList<qreal> data3; | |
24 | dataModel.setData(dataModel.index(0,2),3); |
|
24 | data3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5; | |
25 | dataModel.setData(dataModel.index(0,3),4); |
|
25 | QList<qreal> data4; | |
26 | dataModel.setData(dataModel.index(0,4),5); |
|
26 | data4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10; | |
27 | dataModel.setData(dataModel.index(0,5),6); |
|
27 | ||
28 | dataModel.setData(dataModel.index(0,6),7); |
|
28 | series0->addData(data0); | |
29 | dataModel.setData(dataModel.index(0,7),8); |
|
29 | series0->addData(data1); | |
30 | dataModel.setData(dataModel.index(0,8),9); |
|
30 | series0->addData(data2); | |
31 | dataModel.setData(dataModel.index(0,9),10); |
|
31 | series0->addData(data3); | |
32 |
|
32 | series0->addData(data4); | ||
33 | // Series 1, some other items missing |
|
|||
34 | dataModel.setData(dataModel.index(1,0),5); |
|
|||
35 | dataModel.setData(dataModel.index(1,3),4); |
|
|||
36 | dataModel.setData(dataModel.index(1,5),7); |
|
|||
37 | dataModel.setData(dataModel.index(1,6),8); |
|
|||
38 | dataModel.setData(dataModel.index(1,8),9); |
|
|||
39 | dataModel.setData(dataModel.index(1,9),9); |
|
|||
40 |
|
||||
41 | // Series 2 |
|
|||
42 | dataModel.setData(dataModel.index(2,0),3); |
|
|||
43 | dataModel.setData(dataModel.index(2,1),5); |
|
|||
44 | dataModel.setData(dataModel.index(2,2),8); |
|
|||
45 | dataModel.setData(dataModel.index(2,3),13); |
|
|||
46 | dataModel.setData(dataModel.index(2,4),8); |
|
|||
47 | dataModel.setData(dataModel.index(2,5),5); |
|
|||
48 | dataModel.setData(dataModel.index(2,6),3); |
|
|||
49 | dataModel.setData(dataModel.index(2,7),2); |
|
|||
50 | dataModel.setData(dataModel.index(2,8),1); |
|
|||
51 | dataModel.setData(dataModel.index(2,9),1); |
|
|||
52 |
|
||||
53 | // Series 3 |
|
|||
54 | dataModel.setData(dataModel.index(3,0),5); |
|
|||
55 | dataModel.setData(dataModel.index(3,1),6); |
|
|||
56 | dataModel.setData(dataModel.index(3,2),7); |
|
|||
57 | dataModel.setData(dataModel.index(3,3),3); |
|
|||
58 | dataModel.setData(dataModel.index(3,4),4); |
|
|||
59 | dataModel.setData(dataModel.index(3,5),5); |
|
|||
60 | dataModel.setData(dataModel.index(3,6),8); |
|
|||
61 | dataModel.setData(dataModel.index(3,7),9); |
|
|||
62 | dataModel.setData(dataModel.index(3,8),10); |
|
|||
63 | dataModel.setData(dataModel.index(3,9),5); |
|
|||
64 |
|
||||
65 | // Series 4 |
|
|||
66 | dataModel.setData(dataModel.index(4,0),9); |
|
|||
67 | dataModel.setData(dataModel.index(4,1),7); |
|
|||
68 | dataModel.setData(dataModel.index(4,2),5); |
|
|||
69 | dataModel.setData(dataModel.index(4,3),3); |
|
|||
70 | dataModel.setData(dataModel.index(4,4),1); |
|
|||
71 | dataModel.setData(dataModel.index(4,5),2); |
|
|||
72 | dataModel.setData(dataModel.index(4,6),4); |
|
|||
73 | dataModel.setData(dataModel.index(4,7),6); |
|
|||
74 | dataModel.setData(dataModel.index(4,8),8); |
|
|||
75 | dataModel.setData(dataModel.index(4,9),10); |
|
|||
76 |
|
||||
77 | series0->setData(&dataModel); |
|
|||
78 |
|
33 | |||
79 | ChartWidget* chartWidget = new ChartWidget(&window); |
|
34 | ChartWidget* chartWidget = new ChartWidget(&window); | |
80 | chartWidget->addSeries(series0); |
|
35 | chartWidget->addSeries(series0); | |
81 |
|
36 | |||
82 | window.setCentralWidget(chartWidget); |
|
37 | window.setCentralWidget(chartWidget); | |
83 | window.resize(400, 300); |
|
38 | window.resize(400, 300); | |
84 | window.show(); |
|
39 | window.show(); | |
85 |
|
40 | |||
86 | return a.exec(); |
|
41 | return a.exec(); | |
87 | } |
|
42 | } | |
88 |
|
43 |
@@ -1,88 +1,43 | |||||
1 | #include <QApplication> |
|
1 | #include <QApplication> | |
2 | #include <QMainWindow> |
|
2 | #include <QMainWindow> | |
3 | #include <QStandardItemModel> |
|
3 | #include <QStandardItemModel> | |
4 | #include <percentbarchartseries.h> |
|
4 | #include <percentbarchartseries.h> | |
5 | #include "chartwidget.h" |
|
5 | #include "chartwidget.h" | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
7 | QTCOMMERCIALCHART_USE_NAMESPACE | |
8 |
|
8 | |||
9 | int main(int argc, char *argv[]) |
|
9 | int main(int argc, char *argv[]) | |
10 | { |
|
10 | { | |
11 | QApplication a(argc, argv); |
|
11 | QApplication a(argc, argv); | |
12 | QMainWindow window; |
|
12 | QMainWindow window; | |
13 |
|
13 | |||
14 | PercentBarChartSeries* series0 = new PercentBarChartSeries(); |
|
14 | PercentBarChartSeries* series0 = new PercentBarChartSeries(); | |
15 |
|
15 | |||
16 | // Create some test data to chart |
|
16 | // Create some test data to chart | |
17 | QStandardItemModel dataModel(5,10); |
|
17 | QList<qreal> data0; | |
18 | QModelIndex index; |
|
18 | data0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10; | |
19 | index = dataModel.index(0,0); |
|
19 | QList<qreal> data1; | |
20 |
|
20 | data1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0; | ||
21 | // Series 0 |
|
21 | QList<qreal> data2; | |
22 | dataModel.setData(dataModel.index(0,0),1); |
|
22 | data2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1; | |
23 | dataModel.setData(dataModel.index(0,1),2); |
|
23 | QList<qreal> data3; | |
24 | dataModel.setData(dataModel.index(0,2),3); |
|
24 | data3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5; | |
25 | dataModel.setData(dataModel.index(0,3),4); |
|
25 | QList<qreal> data4; | |
26 | dataModel.setData(dataModel.index(0,4),5); |
|
26 | data4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10; | |
27 | dataModel.setData(dataModel.index(0,5),6); |
|
27 | ||
28 | dataModel.setData(dataModel.index(0,6),7); |
|
28 | series0->addData(data0); | |
29 | dataModel.setData(dataModel.index(0,7),8); |
|
29 | series0->addData(data1); | |
30 | dataModel.setData(dataModel.index(0,8),9); |
|
30 | series0->addData(data2); | |
31 | dataModel.setData(dataModel.index(0,9),10); |
|
31 | series0->addData(data3); | |
32 |
|
32 | series0->addData(data4); | ||
33 | // Series 1, some other items missing |
|
|||
34 | dataModel.setData(dataModel.index(1,0),5); |
|
|||
35 | dataModel.setData(dataModel.index(1,3),4); |
|
|||
36 | dataModel.setData(dataModel.index(1,5),7); |
|
|||
37 | dataModel.setData(dataModel.index(1,6),8); |
|
|||
38 | dataModel.setData(dataModel.index(1,8),9); |
|
|||
39 | dataModel.setData(dataModel.index(1,9),9); |
|
|||
40 |
|
||||
41 | // Series 2 |
|
|||
42 | dataModel.setData(dataModel.index(2,0),3); |
|
|||
43 | dataModel.setData(dataModel.index(2,1),5); |
|
|||
44 | dataModel.setData(dataModel.index(2,2),8); |
|
|||
45 | dataModel.setData(dataModel.index(2,3),13); |
|
|||
46 | dataModel.setData(dataModel.index(2,4),8); |
|
|||
47 | dataModel.setData(dataModel.index(2,5),5); |
|
|||
48 | dataModel.setData(dataModel.index(2,6),3); |
|
|||
49 | dataModel.setData(dataModel.index(2,7),2); |
|
|||
50 | dataModel.setData(dataModel.index(2,8),1); |
|
|||
51 | dataModel.setData(dataModel.index(2,9),1); |
|
|||
52 |
|
||||
53 | // Series 3 |
|
|||
54 | dataModel.setData(dataModel.index(3,0),5); |
|
|||
55 | dataModel.setData(dataModel.index(3,1),6); |
|
|||
56 | dataModel.setData(dataModel.index(3,2),7); |
|
|||
57 | dataModel.setData(dataModel.index(3,3),3); |
|
|||
58 | dataModel.setData(dataModel.index(3,4),4); |
|
|||
59 | dataModel.setData(dataModel.index(3,5),5); |
|
|||
60 | dataModel.setData(dataModel.index(3,6),8); |
|
|||
61 | dataModel.setData(dataModel.index(3,7),9); |
|
|||
62 | dataModel.setData(dataModel.index(3,8),10); |
|
|||
63 | dataModel.setData(dataModel.index(3,9),5); |
|
|||
64 |
|
||||
65 | // Series 4 |
|
|||
66 | dataModel.setData(dataModel.index(4,0),9); |
|
|||
67 | dataModel.setData(dataModel.index(4,1),7); |
|
|||
68 | dataModel.setData(dataModel.index(4,2),5); |
|
|||
69 | dataModel.setData(dataModel.index(4,3),3); |
|
|||
70 | dataModel.setData(dataModel.index(4,4),1); |
|
|||
71 | dataModel.setData(dataModel.index(4,5),2); |
|
|||
72 | dataModel.setData(dataModel.index(4,6),4); |
|
|||
73 | dataModel.setData(dataModel.index(4,7),6); |
|
|||
74 | dataModel.setData(dataModel.index(4,8),8); |
|
|||
75 | dataModel.setData(dataModel.index(4,9),10); |
|
|||
76 |
|
||||
77 | series0->setData(&dataModel); |
|
|||
78 |
|
33 | |||
79 | ChartWidget* chartWidget = new ChartWidget(&window); |
|
34 | ChartWidget* chartWidget = new ChartWidget(&window); | |
80 | chartWidget->addSeries(series0); |
|
35 | chartWidget->addSeries(series0); | |
81 |
|
36 | |||
82 | window.setCentralWidget(chartWidget); |
|
37 | window.setCentralWidget(chartWidget); | |
83 | window.resize(400, 300); |
|
38 | window.resize(400, 300); | |
84 | window.show(); |
|
39 | window.show(); | |
85 |
|
40 | |||
86 | return a.exec(); |
|
41 | return a.exec(); | |
87 | } |
|
42 | } | |
88 |
|
43 |
@@ -1,88 +1,43 | |||||
1 | #include <QApplication> |
|
1 | #include <QApplication> | |
2 | #include <QMainWindow> |
|
2 | #include <QMainWindow> | |
3 | #include <QStandardItemModel> |
|
3 | #include <QStandardItemModel> | |
4 | #include <stackedbarchartseries.h> |
|
4 | #include <stackedbarchartseries.h> | |
5 | #include "chartwidget.h" |
|
5 | #include "chartwidget.h" | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
7 | QTCOMMERCIALCHART_USE_NAMESPACE | |
8 |
|
8 | |||
9 | int main(int argc, char *argv[]) |
|
9 | int main(int argc, char *argv[]) | |
10 | { |
|
10 | { | |
11 | QApplication a(argc, argv); |
|
11 | QApplication a(argc, argv); | |
12 | QMainWindow window; |
|
12 | QMainWindow window; | |
13 |
|
13 | |||
14 | StackedBarChartSeries* series0 = new StackedBarChartSeries(); |
|
14 | StackedBarChartSeries* series0 = new StackedBarChartSeries(); | |
15 |
|
15 | |||
16 | // Create some test data to chart |
|
16 | // Create some test data to chart | |
17 | QStandardItemModel dataModel(5,10); |
|
17 | QList<qreal> data0; | |
18 | QModelIndex index; |
|
18 | data0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10; | |
19 | index = dataModel.index(0,0); |
|
19 | QList<qreal> data1; | |
20 |
|
20 | data1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0; | ||
21 | // Series 0 |
|
21 | QList<qreal> data2; | |
22 | dataModel.setData(dataModel.index(0,0),1); |
|
22 | data2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1; | |
23 | dataModel.setData(dataModel.index(0,1),2); |
|
23 | QList<qreal> data3; | |
24 | dataModel.setData(dataModel.index(0,2),3); |
|
24 | data3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5; | |
25 | dataModel.setData(dataModel.index(0,3),4); |
|
25 | QList<qreal> data4; | |
26 | dataModel.setData(dataModel.index(0,4),5); |
|
26 | data4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10; | |
27 | dataModel.setData(dataModel.index(0,5),6); |
|
27 | ||
28 | dataModel.setData(dataModel.index(0,6),7); |
|
28 | series0->addData(data0); | |
29 | dataModel.setData(dataModel.index(0,7),8); |
|
29 | series0->addData(data1); | |
30 | dataModel.setData(dataModel.index(0,8),9); |
|
30 | series0->addData(data2); | |
31 | dataModel.setData(dataModel.index(0,9),10); |
|
31 | series0->addData(data3); | |
32 |
|
32 | series0->addData(data4); | ||
33 | // Series 1, some other items missing |
|
|||
34 | dataModel.setData(dataModel.index(1,0),5); |
|
|||
35 | dataModel.setData(dataModel.index(1,3),4); |
|
|||
36 | dataModel.setData(dataModel.index(1,5),7); |
|
|||
37 | dataModel.setData(dataModel.index(1,6),8); |
|
|||
38 | dataModel.setData(dataModel.index(1,8),9); |
|
|||
39 | dataModel.setData(dataModel.index(1,9),9); |
|
|||
40 |
|
||||
41 | // Series 2 |
|
|||
42 | dataModel.setData(dataModel.index(2,0),3); |
|
|||
43 | dataModel.setData(dataModel.index(2,1),5); |
|
|||
44 | dataModel.setData(dataModel.index(2,2),8); |
|
|||
45 | dataModel.setData(dataModel.index(2,3),13); |
|
|||
46 | dataModel.setData(dataModel.index(2,4),8); |
|
|||
47 | dataModel.setData(dataModel.index(2,5),5); |
|
|||
48 | dataModel.setData(dataModel.index(2,6),3); |
|
|||
49 | dataModel.setData(dataModel.index(2,7),2); |
|
|||
50 | dataModel.setData(dataModel.index(2,8),1); |
|
|||
51 | dataModel.setData(dataModel.index(2,9),1); |
|
|||
52 |
|
||||
53 | // Series 3 |
|
|||
54 | dataModel.setData(dataModel.index(3,0),5); |
|
|||
55 | dataModel.setData(dataModel.index(3,1),6); |
|
|||
56 | dataModel.setData(dataModel.index(3,2),7); |
|
|||
57 | dataModel.setData(dataModel.index(3,3),3); |
|
|||
58 | dataModel.setData(dataModel.index(3,4),4); |
|
|||
59 | dataModel.setData(dataModel.index(3,5),5); |
|
|||
60 | dataModel.setData(dataModel.index(3,6),8); |
|
|||
61 | dataModel.setData(dataModel.index(3,7),9); |
|
|||
62 | dataModel.setData(dataModel.index(3,8),10); |
|
|||
63 | dataModel.setData(dataModel.index(3,9),5); |
|
|||
64 |
|
||||
65 | // Series 4 |
|
|||
66 | dataModel.setData(dataModel.index(4,0),9); |
|
|||
67 | dataModel.setData(dataModel.index(4,1),7); |
|
|||
68 | dataModel.setData(dataModel.index(4,2),5); |
|
|||
69 | dataModel.setData(dataModel.index(4,3),3); |
|
|||
70 | dataModel.setData(dataModel.index(4,4),1); |
|
|||
71 | dataModel.setData(dataModel.index(4,5),2); |
|
|||
72 | dataModel.setData(dataModel.index(4,6),4); |
|
|||
73 | dataModel.setData(dataModel.index(4,7),6); |
|
|||
74 | dataModel.setData(dataModel.index(4,8),8); |
|
|||
75 | dataModel.setData(dataModel.index(4,9),10); |
|
|||
76 |
|
||||
77 | series0->setData(&dataModel); |
|
|||
78 |
|
33 | |||
79 | ChartWidget* chartWidget = new ChartWidget(&window); |
|
34 | ChartWidget* chartWidget = new ChartWidget(&window); | |
80 | chartWidget->addSeries(series0); |
|
35 | chartWidget->addSeries(series0); | |
81 |
|
36 | |||
82 | window.setCentralWidget(chartWidget); |
|
37 | window.setCentralWidget(chartWidget); | |
83 | window.resize(400, 300); |
|
38 | window.resize(400, 300); | |
84 | window.show(); |
|
39 | window.show(); | |
85 |
|
40 | |||
86 | return a.exec(); |
|
41 | return a.exec(); | |
87 | } |
|
42 | } | |
88 |
|
43 |
@@ -1,99 +1,149 | |||||
1 | #include <limits.h> |
|
1 | #include <limits.h> | |
|
2 | #include <QVector> | |||
|
3 | #include <QDebug> | |||
2 | #include "barchartmodel_p.h" |
|
4 | #include "barchartmodel_p.h" | |
3 |
|
5 | |||
4 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
5 |
|
7 | |||
6 | BarChartModel::BarChartModel(QObject *parent) : |
|
8 | BarChartModel::BarChartModel(QObject *parent) : | |
7 | QObject(parent) |
|
9 | QObject(parent) | |
|
10 | ,mRunningId(1) | |||
8 | { |
|
11 | { | |
9 | } |
|
12 | } | |
10 |
|
13 | |||
11 | void BarChartModel::addSeries(BarChartSeriesBase& series) |
|
14 | BarChartModel::~BarChartModel() | |
12 | { |
|
15 | { | |
13 | mSeries.append(&series); |
|
16 | qDebug() << "BarChartModel::~BarChartModel"; | |
14 | emit modelUpdated(); |
|
17 | foreach (DataContainer* c, mDataModel) { | |
|
18 | delete c; | |||
|
19 | } | |||
15 | } |
|
20 | } | |
16 |
|
21 | |||
17 | void BarChartModel::removeSeries(BarChartSeriesBase& series) |
|
22 | int BarChartModel::addData(QList<qreal> data) | |
18 | { |
|
23 | { | |
19 | int i = mSeries.indexOf(&series); |
|
24 | qDebug() << "BarChartModel::addData" << data.count(); | |
20 | if (-1 == i) { |
|
25 | DataContainer* c = new DataContainer(data,mRunningId); | |
21 | return; |
|
26 | mDataModel.append(c); | |
|
27 | mRunningId++; | |||
|
28 | return mRunningId-1; | |||
|
29 | } | |||
|
30 | ||||
|
31 | void BarChartModel::removeData(int id) | |||
|
32 | { | |||
|
33 | foreach(DataContainer* c, mDataModel) { | |||
|
34 | if (c->mId == id) { | |||
|
35 | mDataModel.removeOne(c); | |||
|
36 | delete c; | |||
|
37 | } | |||
22 | } |
|
38 | } | |
23 | mSeries.removeAt(i); |
|
|||
24 | emit modelUpdated(); |
|
|||
25 | } |
|
39 | } | |
26 |
|
40 | |||
27 |
int BarChartModel::count |
|
41 | int BarChartModel::countRows() | |
28 | { |
|
42 | { | |
29 |
return m |
|
43 | return mDataModel.count(); | |
30 | } |
|
44 | } | |
31 |
|
45 | |||
32 |
int BarChartModel::count |
|
46 | int BarChartModel::countColumns() | |
33 | { |
|
47 | { | |
34 | int count(0); |
|
48 | int count(0); | |
35 |
for (int i=0; i<m |
|
49 | for (int i=0; i<mDataModel.count(); i++){ | |
36 | // TODO: can we assume that all series have same number of values? If not. then which values are empty? |
|
50 | // TODO: can we assume that all series have same number of values? If not. then which values are empty? | |
37 |
int temp = m |
|
51 | int temp = mDataModel.at(i)->countColumns(); | |
38 | if (temp > count) { |
|
52 | if (temp > count) { | |
39 | count = temp; |
|
53 | count = temp; | |
40 | } |
|
54 | } | |
41 | } |
|
55 | } | |
42 | return count; |
|
56 | return count; | |
43 | } |
|
57 | } | |
44 |
|
58 | |||
45 | int BarChartModel::countTotalItems() |
|
59 | int BarChartModel::countTotalItems() | |
46 | { |
|
60 | { | |
47 |
int total = m |
|
61 | int total = mDataModel.count() * countColumns(); | |
|
62 | qDebug() << "BarChartModel::countTotalItems datamodel count" << mDataModel.count(); | |||
|
63 | qDebug() << "BarChartModel::countTotalItems countColumns count" << countColumns(); | |||
48 | return total; |
|
64 | return total; | |
49 | } |
|
65 | } | |
50 |
|
66 | |||
51 | int BarChartModel::min() |
|
67 | int BarChartModel::min() | |
52 | { |
|
68 | { | |
53 |
Q_ASSERT(m |
|
69 | Q_ASSERT(mDataModel.count() > 0); | |
54 | // TODO: make min and max members and update them when data changes. |
|
70 | // TODO: make min and max members and update them when data changes. | |
55 | // This is slower since they are checked every time, even if data is same since previous call. |
|
71 | // This is slower since they are checked every time, even if data is same since previous call. | |
56 | int min = INT_MAX; |
|
72 | int min = INT_MAX; | |
57 |
|
73 | |||
58 |
for (int i=0; i <m |
|
74 | for (int i=0; i <mDataModel.count(); i++) { | |
59 |
int |
|
75 | int itemCount = mDataModel.at(i)->countColumns(); | |
60 | if (temp < min) { |
|
76 | for (int j=0; j<itemCount; j++) { | |
61 | min = temp; |
|
77 | int temp = mDataModel.at(i)->valueAt(j); | |
|
78 | if (temp < min) { | |||
|
79 | min = temp; | |||
|
80 | } | |||
62 | } |
|
81 | } | |
63 | } |
|
82 | } | |
64 | return min; |
|
83 | return min; | |
65 | } |
|
84 | } | |
66 |
|
85 | |||
67 | int BarChartModel::max() |
|
86 | int BarChartModel::max() | |
68 | { |
|
87 | { | |
69 |
Q_ASSERT(m |
|
88 | Q_ASSERT(mDataModel.count() > 0); | |
70 |
|
89 | |||
71 | // TODO: make min and max members and update them when data changes. |
|
90 | // TODO: make min and max members and update them when data changes. | |
72 | // This is slower since they are checked every time, even if data is same since previous call. |
|
91 | // This is slower since they are checked every time, even if data is same since previous call. | |
73 | int max = INT_MIN; |
|
92 | int max = INT_MIN; | |
74 |
|
93 | |||
75 |
for (int i=0; i <m |
|
94 | for (int i=0; i <mDataModel.count(); i++) { | |
76 |
int |
|
95 | int itemCount = mDataModel.at(i)->countColumns(); | |
77 | if (temp > max) { |
|
96 | for (int j=0; j<itemCount; j++) { | |
78 | max = temp; |
|
97 | int temp = mDataModel.at(i)->valueAt(j); | |
|
98 | if (temp > max) { | |||
|
99 | max = temp; | |||
|
100 | } | |||
79 | } |
|
101 | } | |
80 | } |
|
102 | } | |
|
103 | ||||
81 | return max; |
|
104 | return max; | |
82 | } |
|
105 | } | |
83 |
|
106 | |||
84 | qreal BarChartModel::valueAt(int series, int item) |
|
107 | qreal BarChartModel::valueAt(int series, int item) | |
85 | { |
|
108 | { | |
86 |
if ((series < 0) || (series >= m |
|
109 | if ((series < 0) || (series >= mDataModel.count())) { | |
87 | // No series, no value. |
|
110 | // No series, no value. | |
88 | return 0; |
|
111 | return 0; | |
89 |
} else if ((item < 0) || (item >= m |
|
112 | } else if ((item < 0) || (item >= mDataModel.at(series)->countColumns())) { | |
90 | // No item, no value. |
|
113 | // No item, no value. | |
91 | return 0; |
|
114 | return 0; | |
92 | } |
|
115 | } | |
93 |
|
116 | |||
94 |
return m |
|
117 | return mDataModel.at(series)->valueAt(item); | |
|
118 | } | |||
|
119 | ||||
|
120 | qreal BarChartModel::columnSum(int column) | |||
|
121 | { | |||
|
122 | int sum(0); | |||
|
123 | int count = mDataModel.count(); // Count rows | |||
|
124 | ||||
|
125 | for (int row = 0; row < count; row++) { | |||
|
126 | if (column < mDataModel.at(row)->countColumns()) { | |||
|
127 | sum += mDataModel.at(row)->valueAt(column); | |||
|
128 | } | |||
|
129 | } | |||
|
130 | return sum; | |||
|
131 | } | |||
|
132 | ||||
|
133 | qreal BarChartModel::maxColumnSum() | |||
|
134 | { | |||
|
135 | int max = INT_MIN; | |||
|
136 | int count = countColumns(); | |||
|
137 | ||||
|
138 | for (int col=0; col<count; col++) { | |||
|
139 | int sum = columnSum(col); | |||
|
140 | if (sum > max) { | |||
|
141 | max = sum; | |||
|
142 | } | |||
|
143 | } | |||
|
144 | return max; | |||
95 | } |
|
145 | } | |
96 |
|
146 | |||
97 | #include "moc_barchartmodel_p.cpp" |
|
147 | #include "moc_barchartmodel_p.cpp" | |
98 |
|
148 | |||
99 | QTCOMMERCIALCHART_END_NAMESPACE |
|
149 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,45 +1,63 | |||||
1 | #ifndef BARCHARTMODEL_H |
|
1 | #ifndef BARCHARTMODEL_H | |
2 | #define BARCHARTMODEL_H |
|
2 | #define BARCHARTMODEL_H | |
3 |
|
3 | |||
4 | #include <QObject> |
|
4 | #include <QObject> | |
5 |
#include " |
|
5 | #include "qchartglobal.h" | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 | // Model for bar chart. |
|
9 | // Model for bar chart. Internal class. | |
10 | // TODO: Implement as QAbstractItemModel? |
|
10 | // TODO: Implement as QAbstractItemModel? | |
11 |
|
11 | |||
12 | class BarChartModel : public QObject //, public QAbstractItemModel |
|
12 | class BarChartModel : public QObject //, public QAbstractItemModel | |
13 | { |
|
13 | { | |
14 | Q_OBJECT |
|
14 | Q_OBJECT | |
15 | public: |
|
15 | public: | |
16 | explicit BarChartModel(QObject *parent = 0); |
|
16 | explicit BarChartModel(QObject *parent = 0); | |
|
17 | ~BarChartModel(); | |||
17 |
|
18 | |||
18 | // Takes reference, Series are owned by QChart or model? |
|
19 | // Adds data to model. returns id. | |
19 | void addSeries(BarChartSeriesBase& series); |
|
20 | int addData(QList<qreal> data); | |
20 | void removeSeries(BarChartSeriesBase& series); |
|
21 | void removeData(int id); | |
21 |
|
22 | |||
22 |
int count |
|
23 | int countRows(); // Number of series in model | |
23 |
int count |
|
24 | int countColumns(); // Maximum number of items in series | |
24 | int countTotalItems(); // Total items in all series. Includes empty items. |
|
25 | int countTotalItems(); // Total items in all series. Includes empty items. | |
25 |
|
26 | |||
|
27 | // TODO: qreal these | |||
26 | int max(); // Maximum value of all series |
|
28 | int max(); // Maximum value of all series | |
27 | int min(); // Minimum value of all series |
|
29 | int min(); // Minimum value of all series | |
28 | qreal valueAt(int series, int item); |
|
30 | qreal valueAt(int series, int item); | |
29 |
|
31 | |||
|
32 | qreal columnSum(int column); | |||
|
33 | qreal maxColumnSum(); // returns maximum sum of items in all columns. | |||
|
34 | ||||
30 | signals: |
|
35 | signals: | |
31 | void modelUpdated(); |
|
36 | void modelUpdated(); | |
32 |
|
37 | |||
33 | public slots: |
|
38 | public slots: | |
34 |
|
39 | |||
35 | private: |
|
40 | private: | |
36 |
|
41 | |||
37 | // Data |
|
42 | // Little helper class. | |
38 | QList<BarChartSeriesBase*> mSeries; |
|
43 | class DataContainer { | |
|
44 | public: | |||
|
45 | DataContainer(QList<qreal> data, int id) : mId(id), mData(data) {} | |||
|
46 | int countColumns() { return mData.count(); } | |||
|
47 | qreal valueAt(int item) { return mData.at(item); } | |||
|
48 | ||||
|
49 | int mId; // TODO: Is this needed? | |||
|
50 | private: | |||
|
51 | QList<qreal> mData; | |||
|
52 | }; | |||
|
53 | ||||
|
54 | // Owned. N series. each has a list of values. | |||
|
55 | QList<DataContainer*> mDataModel; | |||
|
56 | int mRunningId; | |||
|
57 | int mMaxColumns; // longest series in datamodel | |||
39 |
|
58 | |||
40 | BarChartModel* mSingle; |
|
|||
41 | }; |
|
59 | }; | |
42 |
|
60 | |||
43 | QTCOMMERCIALCHART_END_NAMESPACE |
|
61 | QTCOMMERCIALCHART_END_NAMESPACE | |
44 |
|
62 | |||
45 | #endif // BARCHARTMODEL_H |
|
63 | #endif // BARCHARTMODEL_H |
@@ -1,30 +1,29 | |||||
1 | #ifndef BARCHARTSERIES_H |
|
1 | #ifndef BARCHARTSERIES_H | |
2 | #define BARCHARTSERIES_H |
|
2 | #define BARCHARTSERIES_H | |
3 |
|
3 | |||
4 | #include <QList> |
|
4 | #include <QList> | |
5 | #include <QAbstractItemModel> |
|
5 | #include <QAbstractItemModel> | |
6 | #include "barchartseriesbase.h" |
|
6 | #include "barchartseriesbase.h" | |
7 |
|
7 | |||
8 | class BarGroup; |
|
8 | class BarGroup; | |
9 |
|
9 | |||
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
11 |
|
11 | |||
12 | // Container for series |
|
12 | // Container for series | |
13 | class QTCOMMERCIALCHART_EXPORT BarChartSeries : public BarChartSeriesBase |
|
13 | class QTCOMMERCIALCHART_EXPORT BarChartSeries : public BarChartSeriesBase | |
14 | { |
|
14 | { | |
15 | Q_OBJECT |
|
15 | Q_OBJECT | |
16 | public: |
|
16 | public: | |
17 | BarChartSeries(QObject* parent=0); |
|
17 | BarChartSeries(QObject* parent=0); | |
18 |
|
18 | |||
19 | // from BarChartSeriesBase |
|
19 | // from BarChartSeriesBase | |
20 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; } |
|
20 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; } | |
21 |
|
21 | |||
22 | private: |
|
22 | private: | |
23 |
|
23 | |||
24 | QAbstractItemModel* mModel; |
|
|||
25 | BarGroup* mBarGroup; |
|
24 | BarGroup* mBarGroup; | |
26 | }; |
|
25 | }; | |
27 |
|
26 | |||
28 | QTCOMMERCIALCHART_END_NAMESPACE |
|
27 | QTCOMMERCIALCHART_END_NAMESPACE | |
29 |
|
28 | |||
30 | #endif // BARCHARTSERIES_H |
|
29 | #endif // BARCHARTSERIES_H |
@@ -1,158 +1,187 | |||||
1 | #include <limits.h> |
|
1 | #include <limits.h> | |
2 | #include <QDebug> |
|
2 | #include <QDebug> | |
3 | #include "barchartseriesbase.h" |
|
3 | #include "barchartseriesbase.h" | |
4 | #include "bargroup.h" |
|
4 | #include "bargroup.h" | |
|
5 | #include "barchartmodel_p.h" | |||
5 |
|
6 | |||
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
7 |
|
8 | |||
8 | BarChartSeriesBase::BarChartSeriesBase(QObject *parent) |
|
9 | BarChartSeriesBase::BarChartSeriesBase(QObject *parent) | |
9 | : QChartSeries(parent) |
|
10 | : QChartSeries(parent) | |
10 | ,mData(0) |
|
11 | ,mModel(*(new BarChartModel(this))) // TODO: is this ok? | |
11 | { |
|
12 | { | |
|
13 | qDebug() << "BarChartSeriesBase::BarChartSeriesBase"; | |||
12 | } |
|
14 | } | |
13 | /* |
|
15 | /* | |
14 | bool BarChartSeriesBase::setModel(QAbstractItemModel* model) |
|
16 | bool BarChartSeriesBase::setModel(QAbstractItemModel* model) | |
15 | { |
|
17 | { | |
16 | mModel = model; |
|
18 | mModel = model; | |
17 | return true; |
|
19 | return true; | |
18 | } |
|
20 | } | |
19 | */ |
|
21 | */ | |
|
22 | /* | |||
20 | bool BarChartSeriesBase::setData(QList<qreal>& data) |
|
23 | bool BarChartSeriesBase::setData(QList<qreal>& data) | |
21 |
|
|
24 | { | |
22 | mData = &data; |
|
25 | mData = &data; | |
23 | return true; |
|
26 | return true; | |
24 |
|
|
27 | } | |
|
28 | */ | |||
25 | /* |
|
29 | /* | |
26 | int BarChartSeriesBase::min() |
|
30 | int BarChartSeriesBase::min() | |
27 | { |
|
31 | { | |
28 | Q_ASSERT(mModel->rowCount() > 0); |
|
32 | Q_ASSERT(mModel->rowCount() > 0); | |
29 | Q_ASSERT(mModel->columnCount() > 0); |
|
33 | Q_ASSERT(mModel->columnCount() > 0); | |
30 |
|
34 | |||
31 | // TODO: make min and max members and update them when data changes. |
|
35 | // TODO: make min and max members and update them when data changes. | |
32 | // This is slower since they are checked every time, even if data is same since previous call. |
|
36 | // This is slower since they are checked every time, even if data is same since previous call. | |
33 | int min = INT_MAX; |
|
37 | int min = INT_MAX; | |
34 |
|
38 | |||
35 | for (int i=0; i <mModel->rowCount(); i++) { |
|
39 | for (int i=0; i <mModel->rowCount(); i++) { | |
36 | for(int j=0; j<mModel->columnCount(); j++) { |
|
40 | for(int j=0; j<mModel->columnCount(); j++) { | |
37 | int temp = mModel->data(mModel->index(i,j)).toInt(); |
|
41 | int temp = mModel->data(mModel->index(i,j)).toInt(); | |
38 | if (temp < min) { |
|
42 | if (temp < min) { | |
39 | min = temp; |
|
43 | min = temp; | |
40 | } |
|
44 | } | |
41 | } |
|
45 | } | |
42 | } |
|
46 | } | |
43 | return min; |
|
47 | return min; | |
44 | } |
|
48 | } | |
45 |
|
49 | |||
46 | int BarChartSeriesBase::max() |
|
50 | int BarChartSeriesBase::max() | |
47 | { |
|
51 | { | |
48 | Q_ASSERT(mModel->rowCount() > 0); |
|
52 | Q_ASSERT(mModel->rowCount() > 0); | |
49 | Q_ASSERT(mModel->columnCount() > 0); |
|
53 | Q_ASSERT(mModel->columnCount() > 0); | |
50 |
|
54 | |||
51 | // TODO: make min and max members and update them when data changes. |
|
55 | // TODO: make min and max members and update them when data changes. | |
52 | // This is slower since they are checked every time, even if data is same since previous call. |
|
56 | // This is slower since they are checked every time, even if data is same since previous call. | |
53 | int max = INT_MIN; |
|
57 | int max = INT_MIN; | |
54 |
|
58 | |||
55 | for (int i=0; i <mModel->rowCount(); i++) { |
|
59 | for (int i=0; i <mModel->rowCount(); i++) { | |
56 | for(int j=0; j<mModel->columnCount(); j++) { |
|
60 | for(int j=0; j<mModel->columnCount(); j++) { | |
57 | int temp = mModel->data(mModel->index(i,j)).toInt(); |
|
61 | int temp = mModel->data(mModel->index(i,j)).toInt(); | |
58 | if (temp > max) { |
|
62 | if (temp > max) { | |
59 | max = temp; |
|
63 | max = temp; | |
60 | } |
|
64 | } | |
61 | } |
|
65 | } | |
62 | } |
|
66 | } | |
63 | return max; |
|
67 | return max; | |
64 | } |
|
68 | } | |
65 |
|
69 | |||
66 | int BarChartSeriesBase::maxColumnSum() |
|
70 | int BarChartSeriesBase::maxColumnSum() | |
67 | { |
|
71 | { | |
68 | Q_ASSERT(mModel->rowCount() > 0); |
|
72 | Q_ASSERT(mModel->rowCount() > 0); | |
69 | Q_ASSERT(mModel->columnCount() > 0); |
|
73 | Q_ASSERT(mModel->columnCount() > 0); | |
70 |
|
74 | |||
71 | int max = INT_MIN; |
|
75 | int max = INT_MIN; | |
72 |
|
76 | |||
73 | for (int col=0; col <mModel->columnCount(); col++) { |
|
77 | for (int col=0; col <mModel->columnCount(); col++) { | |
74 | int sum = columnSum(col); |
|
78 | int sum = columnSum(col); | |
75 | if (sum > max) { |
|
79 | if (sum > max) { | |
76 | max = sum; |
|
80 | max = sum; | |
77 | } |
|
81 | } | |
78 | } |
|
82 | } | |
79 | return max; |
|
83 | return max; | |
80 | } |
|
84 | } | |
81 |
|
85 | |||
82 | int BarChartSeriesBase::countRows() |
|
86 | int BarChartSeriesBase::countRows() | |
83 | { |
|
87 | { | |
84 | return mModel->rowCount(); |
|
88 | return mModel->rowCount(); | |
85 | } |
|
89 | } | |
86 |
|
90 | |||
87 | int BarChartSeriesBase::countColumns() |
|
91 | int BarChartSeriesBase::countColumns() | |
88 | { |
|
92 | { | |
89 | return mModel->columnCount(); |
|
93 | return mModel->columnCount(); | |
90 | } |
|
94 | } | |
91 |
|
95 | |||
92 | int BarChartSeriesBase::countTotalItems() |
|
96 | int BarChartSeriesBase::countTotalItems() | |
93 | { |
|
97 | { | |
94 | return mModel->rowCount() * mModel->columnCount(); |
|
98 | return mModel->rowCount() * mModel->columnCount(); | |
95 | } |
|
99 | } | |
96 |
|
100 | |||
97 | int BarChartSeriesBase::valueAt(int row, int column) |
|
101 | int BarChartSeriesBase::valueAt(int row, int column) | |
98 | { |
|
102 | { | |
99 | QModelIndex index = mModel->index(row,column); |
|
103 | QModelIndex index = mModel->index(row,column); | |
100 | return mModel->data(index).toInt(); |
|
104 | return mModel->data(index).toInt(); | |
101 | } |
|
105 | } | |
102 |
|
106 | |||
103 | int BarChartSeriesBase::columnSum(int column) |
|
107 | int BarChartSeriesBase::columnSum(int column) | |
104 | { |
|
108 | { | |
105 | int sum(0); |
|
109 | int sum(0); | |
106 | int count = mModel->rowCount(); |
|
110 | int count = mModel->rowCount(); | |
107 |
|
111 | |||
108 | for (int row = 0; row < count; row++) { |
|
112 | for (int row = 0; row < count; row++) { | |
109 | sum += mModel->data(mModel->index(row,column)).toInt(); |
|
113 | sum += mModel->data(mModel->index(row,column)).toInt(); | |
110 | } |
|
114 | } | |
111 | return sum; |
|
115 | return sum; | |
112 | } |
|
116 | } | |
113 | */ |
|
117 | */ | |
114 | qreal BarChartSeriesBase::min() |
|
118 | ||
|
119 | int BarChartSeriesBase::addData(QList<qreal> data) | |||
115 | { |
|
120 | { | |
116 | Q_ASSERT(mData != 0); |
|
121 | qDebug() << "BarChartSeriesBase::addData"; | |
|
122 | return mModel.addData(data); | |||
|
123 | } | |||
117 |
|
124 | |||
|
125 | void BarChartSeriesBase::removeData(int id) | |||
|
126 | { | |||
|
127 | mModel.removeData(id); | |||
|
128 | } | |||
|
129 | ||||
|
130 | qreal BarChartSeriesBase::min() | |||
|
131 | { | |||
|
132 | return mModel.min(); | |||
|
133 | /* Delegated to model | |||
118 | int count = mData->count(); |
|
134 | int count = mData->count(); | |
119 | int min = INT_MAX; |
|
135 | int min = INT_MAX; | |
120 |
|
|
136 | ||
121 | for (int i=0; i<count; i++) { |
|
137 | for (int i=0; i<count; i++) { | |
122 | if (mData->at(i) < min) { |
|
138 | if (mData->at(i) < min) { | |
123 | min = mData->at(i); |
|
139 | min = mData->at(i); | |
124 | } |
|
140 | } | |
125 | } |
|
141 | } | |
126 | return min; |
|
142 | return min; | |
|
143 | */ | |||
127 | } |
|
144 | } | |
128 |
|
145 | |||
129 | qreal BarChartSeriesBase::max() |
|
146 | qreal BarChartSeriesBase::max() | |
130 | { |
|
147 | { | |
131 | Q_ASSERT(mData != 0); |
|
148 | return mModel.max(); | |
132 |
|
149 | /* Delegated to model | ||
133 | int count = mData->count(); |
|
150 | int count = mData->count(); | |
134 | int max = INT_MIN; |
|
151 | int max = INT_MIN; | |
135 |
|
|
152 | ||
136 | for (int i=0; i<count; i++) { |
|
153 | for (int i=0; i<count; i++) { | |
137 | if (mData->at(i) > max) { |
|
154 | if (mData->at(i) > max) { | |
138 | max = mData->at(i); |
|
155 | max = mData->at(i); | |
139 | } |
|
156 | } | |
140 | } |
|
157 | } | |
141 | return max; |
|
158 | return max; | |
|
159 | */ | |||
|
160 | } | |||
|
161 | ||||
|
162 | int BarChartSeriesBase::countColumns() | |||
|
163 | { | |||
|
164 | return mModel.countColumns(); | |||
|
165 | // return mData->count(); | |||
|
166 | } | |||
|
167 | ||||
|
168 | qreal BarChartSeriesBase::valueAt(int series, int item) | |||
|
169 | { | |||
|
170 | return mModel.valueAt(series,item); | |||
|
171 | // return mData->at(item); | |||
142 | } |
|
172 | } | |
143 |
|
173 | |||
144 |
|
|
174 | qreal BarChartSeriesBase::maxColumnSum() | |
145 | { |
|
175 | { | |
146 | Q_ASSERT(mData != 0); |
|
176 | qDebug() << "BarChartSeriesBase::maxColumnSum" << mModel.maxColumnSum(); | |
147 |
return m |
|
177 | return mModel.maxColumnSum(); | |
148 | } |
|
178 | } | |
149 |
|
179 | |||
150 |
|
|
180 | BarChartModel& BarChartSeriesBase::model() | |
151 | { |
|
181 | { | |
152 | Q_ASSERT(mData != 0); |
|
182 | return mModel; | |
153 | return mData->at(item); |
|
|||
154 | } |
|
183 | } | |
155 |
|
184 | |||
156 | #include "moc_barchartseriesbase.cpp" |
|
185 | #include "moc_barchartseriesbase.cpp" | |
157 |
|
186 | |||
158 | QTCOMMERCIALCHART_END_NAMESPACE |
|
187 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,58 +1,52 | |||||
1 | #ifndef BARCHARTSERIESBASE_H |
|
1 | #ifndef BARCHARTSERIESBASE_H | |
2 | #define BARCHARTSERIESBASE_H |
|
2 | #define BARCHARTSERIESBASE_H | |
3 |
|
3 | |||
4 | #include <QList> |
|
4 | #include <QList> | |
5 | #include <QAbstractItemModel> |
|
5 | #include <QAbstractItemModel> | |
6 | #include "qchartseries.h" |
|
6 | #include "qchartseries.h" | |
7 | #include "qchartglobal.h" |
|
7 | #include "qchartglobal.h" | |
8 |
|
8 | |||
9 | class BarGroupBase; |
|
|||
10 |
|
||||
11 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
12 |
|
10 | |||
|
11 | class BarGroupBase; | |||
|
12 | class BarChartModel; | |||
|
13 | ||||
13 | // Container for series |
|
14 | // Container for series | |
14 | class QTCOMMERCIALCHART_EXPORT BarChartSeriesBase : public QChartSeries |
|
15 | class QTCOMMERCIALCHART_EXPORT BarChartSeriesBase : public QChartSeries | |
15 | { |
|
16 | { | |
16 | Q_OBJECT |
|
17 | Q_OBJECT | |
17 | protected: |
|
18 | protected: | |
18 | BarChartSeriesBase(QObject* parent=0); |
|
19 | BarChartSeriesBase(QObject* parent=0); | |
19 |
|
20 | |||
20 | public: |
|
21 | public: | |
21 | // from QChartSeries |
|
22 | // from QChartSeries | |
22 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeInvalid; } |
|
23 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeInvalid; } | |
23 |
|
24 | |||
24 | // TODO: Better data model? |
|
25 | // TODO: << operator for convinience | |
25 | // virtual bool setModel(QAbstractItemModel* model); |
|
26 | // Returns id for vector. | |
26 |
|
|
27 | int addData(QList<qreal> data); | |
27 |
|
28 | void removeData(int id); | ||
28 | // Methods to find out minimum and maximum values of data |
|
|||
29 | // int min(); // TODO: remove |
|
|||
30 | // int max(); // TODO: remove |
|
|||
31 | // int maxColumnSum(); // TODO: move to model. returns maximum sum of items in all columns. |
|
|||
32 |
|
||||
33 | // int countRows(); // TODO: remove. |
|
|||
34 | // int countColumns(); // TODO: remove. Count items in one series. |
|
|||
35 | // int countTotalItems(); // TODO: move to model |
|
|||
36 | // int valueAt(int row, int column); // TODO: move to model |
|
|||
37 |
|
||||
38 | // int columnSum(int column); // TODO: move to model |
|
|||
39 |
|
29 | |||
40 | qreal min(); |
|
30 | qreal min(); | |
41 | qreal max(); |
|
31 | qreal max(); | |
42 | int countValues(); |
|
32 | int countColumns(); // Count items in one series. | |
43 | qreal valueAt(int item); |
|
33 | qreal valueAt(int series, int item); | |
|
34 | qreal maxColumnSum(); | |||
|
35 | ||||
|
36 | BarChartModel& model(); | |||
|
37 | ||||
|
38 | signals: | |||
|
39 | void changed(int index); | |||
44 |
|
40 | |||
45 | public Q_SLOTS: |
|
41 | public Q_SLOTS: | |
46 |
|
42 | |||
47 | private: |
|
43 | private: | |
48 |
|
44 | |||
49 |
|
|
45 | BarChartModel& mModel; | |
50 | BarGroupBase* mBarGroup; |
|
46 | BarGroupBase* mBarGroup; | |
51 |
|
47 | |||
52 | QList<qreal>* mData; |
|
|||
53 |
|
||||
54 | }; |
|
48 | }; | |
55 |
|
49 | |||
56 | QTCOMMERCIALCHART_END_NAMESPACE |
|
50 | QTCOMMERCIALCHART_END_NAMESPACE | |
57 |
|
51 | |||
58 | #endif // BARCHARTSERIESBASE_H |
|
52 | #endif // BARCHARTSERIESBASE_H |
@@ -1,70 +1,65 | |||||
1 | #include "bargroup.h" |
|
1 | #include "bargroup.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barlabel_p.h" |
|
3 | #include "barlabel_p.h" | |
4 | #include <QDebug> |
|
4 | #include <QDebug> | |
5 |
|
5 | |||
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
7 |
|
7 | |||
8 | BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) : |
|
8 | BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) : | |
9 | BarGroupBase(series,parent) |
|
9 | BarGroupBase(series,parent) | |
10 | { |
|
10 | { | |
11 | mBarDefaultWidth = 10; |
|
11 | mBarDefaultWidth = 10; | |
12 | } |
|
12 | } | |
13 |
|
13 | |||
14 | void BarGroup::layoutChanged() |
|
14 | void BarGroup::layoutChanged() | |
15 | { |
|
15 | { | |
|
16 | qDebug() << "BarGroup::layoutChanged"; | |||
16 | // Scale bars to new layout |
|
17 | // Scale bars to new layout | |
17 | // Layout for bars: |
|
18 | // Layout for bars: | |
18 | /* |
|
19 | if (mModel.countRows() <= 0) { | |
19 | if (mSeries.countRows() <= 0) { |
|
|||
20 | // Nothing to do. |
|
|||
21 | return; |
|
|||
22 | } |
|
|||
23 | */ |
|
|||
24 | if (mModel.countSeries() <= 0) { |
|
|||
25 | return; |
|
20 | return; | |
26 | } |
|
21 | } | |
27 |
|
22 | |||
28 | // TODO: better way to auto-layout? |
|
23 | // TODO: better way to auto-layout? | |
29 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
24 | // Use reals for accurancy (we might get some compiler warnings... :) | |
30 |
int itemCount = mModel.count |
|
25 | int itemCount = mModel.countColumns(); | |
31 |
int seriesCount = mModel.count |
|
26 | int seriesCount = mModel.countRows(); | |
32 |
|
27 | |||
33 | qreal tW = mWidth; |
|
28 | qreal tW = mWidth; | |
34 | qreal tH = mHeight; |
|
29 | qreal tH = mHeight; | |
35 | qreal tM = mMax; |
|
30 | qreal tM = mMax; | |
36 | qreal scale = (tH/tM); |
|
31 | qreal scale = (tH/tM); | |
37 |
|
32 | |||
38 | qreal tC = itemCount+1; |
|
33 | qreal tC = itemCount+1; | |
39 | qreal xStepPerSeries = (tW/tC); |
|
34 | qreal xStepPerSeries = (tW/tC); | |
40 |
|
35 | |||
41 | // Scaling. |
|
36 | // Scaling. | |
42 | int itemIndex(0); |
|
37 | int itemIndex(0); | |
43 | int labelIndex = itemCount * seriesCount; |
|
38 | int labelIndex = itemCount * seriesCount; | |
44 |
|
39 | |||
45 | for (int item=0; item < itemCount; item++) { |
|
40 | for (int item=0; item < itemCount; item++) { | |
46 | qreal xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2)); |
|
41 | qreal xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2)); | |
47 | qreal yPos = mHeight; |
|
42 | qreal yPos = mHeight; | |
48 | for (int series = 0; series < seriesCount; series++) { |
|
43 | for (int series = 0; series < seriesCount; series++) { | |
49 | qreal barHeight = mModel.valueAt(series, item) * scale; |
|
44 | qreal barHeight = mModel.valueAt(series, item) * scale; | |
50 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
45 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
51 |
|
46 | |||
52 | // TODO: width settable per bar? |
|
47 | // TODO: width settable per bar? | |
53 | bar->resize(mBarDefaultWidth, barHeight); |
|
48 | bar->resize(mBarDefaultWidth, barHeight); | |
54 | bar->setColor(mColors.at(series)); |
|
49 | bar->setColor(mColors.at(series)); | |
55 | bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight); |
|
50 | bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight); | |
56 | itemIndex++; |
|
51 | itemIndex++; | |
57 | xPos += mBarDefaultWidth; |
|
52 | xPos += mBarDefaultWidth; | |
58 | } |
|
53 | } | |
59 |
|
54 | |||
60 | // TODO: Layout for labels, remove magic number |
|
55 | // TODO: Layout for labels, remove magic number | |
61 | xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2)); |
|
56 | xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2)); | |
62 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); |
|
57 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); | |
63 | label->setPos(xPos, mHeight + 20); |
|
58 | label->setPos(xPos, mHeight + 20); | |
64 | labelIndex++; |
|
59 | labelIndex++; | |
65 | } |
|
60 | } | |
66 |
|
61 | |||
67 | mLayoutDirty = true; |
|
62 | mLayoutDirty = true; | |
68 | } |
|
63 | } | |
69 |
|
64 | |||
70 | QTCOMMERCIALCHART_END_NAMESPACE |
|
65 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,133 +1,129 | |||||
1 | #include "bargroupbase.h" |
|
1 | #include "bargroupbase.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barlabel_p.h" |
|
3 | #include "barlabel_p.h" | |
4 | #include "separator_p.h" |
|
4 | #include "separator_p.h" | |
5 | #include "barchartseriesbase.h" |
|
5 | #include "barchartseriesbase.h" | |
6 | #include <QDebug> |
|
6 | #include <QDebug> | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | BarGroupBase::BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent) |
|
10 | BarGroupBase::BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent) | |
11 | : ChartItem(parent) |
|
11 | : ChartItem(parent) | |
12 | // ,mSeries(series) |
|
|||
13 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready |
|
12 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready | |
14 | ,mLayoutSet(false) |
|
13 | ,mLayoutSet(false) | |
15 | ,mLayoutDirty(true) |
|
14 | ,mLayoutDirty(true) | |
16 | ,mTheme(0) |
|
15 | ,mTheme(0) | |
17 | ,mSeparatorsVisible(true) |
|
16 | ,mSeparatorsVisible(true) | |
|
17 | ,mModel(series.model()) | |||
18 | { |
|
18 | { | |
19 | mModel.addSeries(series); |
|
|||
20 | dataChanged(); |
|
|||
21 | } |
|
19 | } | |
22 |
|
20 | |||
23 | void BarGroupBase::setSeparatorsVisible(bool visible) |
|
21 | void BarGroupBase::setSeparatorsVisible(bool visible) | |
24 | { |
|
22 | { | |
25 | mSeparatorsVisible = visible; |
|
23 | mSeparatorsVisible = visible; | |
26 | } |
|
24 | } | |
27 |
|
25 | |||
28 | void BarGroupBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
26 | void BarGroupBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
29 | { |
|
27 | { | |
|
28 | qDebug() << "BarGroupBase::paint" << childItems().count(); | |||
30 | if (!mLayoutSet) { |
|
29 | if (!mLayoutSet) { | |
31 | qDebug() << "BarGroupBase::paint called without layout set. Aborting."; |
|
30 | qDebug() << "BarGroupBase::paint called without layout set. Aborting."; | |
32 | return; |
|
31 | return; | |
33 | } |
|
32 | } | |
34 | if (mLayoutDirty) { |
|
33 | // if (mLayoutDirty) { | |
35 | // Layout or data has changed. Need to redraw. |
|
34 | // Layout or data has changed. Need to redraw. | |
36 | foreach(QGraphicsItem* i, childItems()) { |
|
35 | foreach(QGraphicsItem* i, childItems()) { | |
37 | i->paint(painter,option,widget); |
|
36 | i->paint(painter,option,widget); | |
38 | } |
|
37 | } | |
39 | } |
|
38 | // } | |
40 | } |
|
39 | } | |
41 |
|
40 | |||
42 | QRectF BarGroupBase::boundingRect() const |
|
41 | QRectF BarGroupBase::boundingRect() const | |
43 | { |
|
42 | { | |
44 | return QRectF(0,0,mWidth,mHeight); |
|
43 | return QRectF(0,0,mWidth,mHeight); | |
45 | } |
|
44 | } | |
46 |
|
45 | |||
47 | void BarGroupBase::themeChanged(ChartTheme *theme) |
|
46 | void BarGroupBase::themeChanged(ChartTheme *theme) | |
48 | { |
|
47 | { | |
49 | mTheme = theme; |
|
48 | mTheme = theme; | |
50 | } |
|
49 | } | |
51 |
|
50 | |||
52 | void BarGroupBase::setBarWidth( int w ) |
|
51 | void BarGroupBase::setBarWidth( int w ) | |
53 | { |
|
52 | { | |
54 | mBarDefaultWidth = w; |
|
53 | mBarDefaultWidth = w; | |
55 | } |
|
54 | } | |
56 |
|
55 | |||
57 | int BarGroupBase::addColor( QColor color ) |
|
56 | int BarGroupBase::addColor( QColor color ) | |
58 | { |
|
57 | { | |
59 | int colorIndex = mColors.count(); |
|
58 | int colorIndex = mColors.count(); | |
60 | mColors.append(color); |
|
59 | mColors.append(color); | |
61 | return colorIndex; |
|
60 | return colorIndex; | |
62 | } |
|
61 | } | |
63 |
|
62 | |||
64 | void BarGroupBase::resetColors() |
|
63 | void BarGroupBase::resetColors() | |
65 | { |
|
64 | { | |
66 | mColors.clear(); |
|
65 | mColors.clear(); | |
67 | } |
|
66 | } | |
68 |
|
67 | |||
69 | void BarGroupBase::dataChanged() |
|
68 | void BarGroupBase::dataChanged() | |
70 | { |
|
69 | { | |
71 | qDebug() << "BarGroupBase::dataChanged"; |
|
70 | qDebug() << "BarGroupBase::dataChanged"; | |
72 |
|
71 | |||
73 | // Find out maximum and minimum of all series. |
|
|||
74 | // TODO: is this actually needed? |
|
|||
75 | // mMax = mModel.max(); |
|
|||
76 | // mMin = mModel.min(); |
|
|||
77 |
|
||||
78 | // Delete old bars |
|
72 | // Delete old bars | |
79 | foreach (QGraphicsItem* item, childItems()) { |
|
73 | foreach (QGraphicsItem* item, childItems()) { | |
80 | delete item; |
|
74 | delete item; | |
81 | } |
|
75 | } | |
82 |
|
76 | |||
83 | // Create new graphic items for bars |
|
77 | // Create new graphic items for bars | |
84 | int totalItems = mModel.countTotalItems(); // mSeries.countTotalItems(); |
|
78 | int totalItems = mModel.countTotalItems(); // mSeries.countTotalItems(); | |
85 | for (int i=0; i<totalItems; i++) { |
|
79 | for (int i=0; i<totalItems; i++) { | |
86 | Bar *bar = new Bar(this); |
|
80 | Bar *bar = new Bar(this); | |
87 | childItems().append(bar); |
|
81 | childItems().append(bar); | |
88 | } |
|
82 | } | |
89 |
|
83 | |||
90 | // TODO: labels from series. This creates just some example labels |
|
84 | // TODO: labels from series. This creates just some example labels | |
91 |
int count = mModel.count |
|
85 | int count = mModel.countColumns(); // mSeries.countColumns(); | |
92 | for (int i=0; i<count; i++) { |
|
86 | for (int i=0; i<count; i++) { | |
93 | BarLabel* label = new BarLabel(this); |
|
87 | BarLabel* label = new BarLabel(this); | |
94 | QString text("Label " + QString::number(i)); |
|
88 | QString text("Label " + QString::number(i)); | |
95 | label->set(text); |
|
89 | label->set(text); | |
96 | childItems().append(label); |
|
90 | childItems().append(label); | |
97 | } |
|
91 | } | |
98 |
|
92 | |||
99 |
count = mModel.count |
|
93 | count = mModel.countColumns() - 1; // mSeries.countColumns() - 1; // There is one less separator than columns | |
100 | for (int i=0; i<count; i++) { |
|
94 | for (int i=0; i<count; i++) { | |
101 | Separator* sep = new Separator(this); |
|
95 | Separator* sep = new Separator(this); | |
102 | sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme |
|
96 | sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme | |
103 | childItems().append(sep); |
|
97 | childItems().append(sep); | |
104 | } |
|
98 | } | |
105 |
|
99 | |||
106 | // TODO: if (autolayout) { layoutChanged() } or something |
|
100 | // TODO: if (autolayout) { layoutChanged() } or something | |
107 | mLayoutDirty = true; |
|
101 | mLayoutDirty = true; | |
108 | } |
|
102 | } | |
109 |
|
103 | |||
110 | //handlers |
|
104 | //handlers | |
111 |
|
105 | |||
112 | void BarGroupBase::handleModelChanged(int index) |
|
106 | void BarGroupBase::handleModelChanged(int index) | |
113 | { |
|
107 | { | |
114 | qDebug() << "BarGroupBase::handleModelChanged"; |
|
108 | qDebug() << "BarGroupBase::handleModelChanged PUUH" << index; | |
|
109 | dataChanged(); | |||
115 | } |
|
110 | } | |
116 |
|
111 | |||
117 | void BarGroupBase::handleDomainChanged(const Domain& domain) |
|
112 | void BarGroupBase::handleDomainChanged(const Domain& domain) | |
118 | { |
|
113 | { | |
119 |
qDebug() << "BarGroupBase::handle |
|
114 | qDebug() << "BarGroupBase::handleDomainChanged"; | |
|
115 | dataChanged(); | |||
120 | } |
|
116 | } | |
121 |
|
117 | |||
122 | void BarGroupBase::handleGeometryChanged(const QRectF& rect) |
|
118 | void BarGroupBase::handleGeometryChanged(const QRectF& rect) | |
123 | { |
|
119 | { | |
124 | mWidth = rect.width(); |
|
120 | mWidth = rect.width(); | |
125 | mHeight = rect.height(); |
|
121 | mHeight = rect.height(); | |
126 | layoutChanged(); |
|
122 | layoutChanged(); | |
127 | mLayoutSet = true; |
|
123 | mLayoutSet = true; | |
128 | setPos(rect.topLeft()); |
|
124 | setPos(rect.topLeft()); | |
129 | } |
|
125 | } | |
130 |
|
126 | |||
131 | #include "moc_bargroupbase.cpp" |
|
127 | #include "moc_bargroupbase.cpp" | |
132 |
|
128 | |||
133 | QTCOMMERCIALCHART_END_NAMESPACE |
|
129 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,74 +1,71 | |||||
1 | #ifndef BARGROUPBASE_H |
|
1 | #ifndef BARGROUPBASE_H | |
2 | #define BARGROUPBASE_H |
|
2 | #define BARGROUPBASE_H | |
3 |
|
3 | |||
4 | #include "charttheme_p.h" |
|
4 | #include "charttheme_p.h" | |
5 | #include "chartitem_p.h" |
|
5 | #include "chartitem_p.h" | |
6 | //#include "barlabel_p.h" |
|
6 | //#include "barlabel_p.h" | |
7 | //#include "bar_p.h" |
|
7 | //#include "bar_p.h" | |
8 | #include "barchartseriesbase.h" |
|
8 | #include "barchartseriesbase.h" | |
9 | #include "barchartmodel_p.h" |
|
9 | #include "barchartmodel_p.h" | |
10 | #include <QGraphicsItem> |
|
10 | #include <QGraphicsItem> | |
11 |
|
11 | |||
12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
13 |
|
13 | |||
14 | // Base Class for bar groups. Common implemantation of different groups. Not to be instantiated. |
|
14 | // Base Class for bar groups. Common implemantation of different groups. Not to be instantiated. | |
15 | class BarGroupBase : public QObject, public ChartItem |
|
15 | class BarGroupBase : public QObject, public ChartItem | |
16 | { |
|
16 | { | |
17 | Q_OBJECT |
|
17 | Q_OBJECT | |
18 | public: |
|
18 | public: | |
19 | BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent = 0); |
|
19 | BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent = 0); | |
20 | // BarGroupBase(BarChartModel& model, QGraphicsItem *parent = 0); |
|
|||
21 | void setSeparatorsVisible(bool visible = true); |
|
20 | void setSeparatorsVisible(bool visible = true); | |
22 |
|
21 | |||
23 | public: // From ChartItem |
|
22 | public: // From ChartItem | |
24 | void setSize(const QSizeF &size){}; |
|
23 | void setSize(const QSizeF &size){}; | |
25 | void setPlotDomain(const PlotDomain& data){}; |
|
24 | void setPlotDomain(const PlotDomain& data){}; | |
26 |
|
25 | |||
27 | // From QGraphicsItem |
|
26 | // From QGraphicsItem | |
28 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
27 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |
29 | QRectF boundingRect() const; |
|
28 | QRectF boundingRect() const; | |
30 |
|
29 | |||
31 | // From ChartThemeObserver |
|
30 | // From ChartThemeObserver | |
32 | void themeChanged(ChartTheme *theme); |
|
31 | void themeChanged(ChartTheme *theme); | |
33 |
|
32 | |||
34 | // TODO: these may change with layout awarness. |
|
33 | // TODO: these may change with layout awarness. | |
35 | void setBarWidth( int w ); |
|
34 | void setBarWidth( int w ); | |
36 | int addColor( QColor color ); |
|
35 | int addColor( QColor color ); | |
37 | void resetColors(); |
|
36 | void resetColors(); | |
38 |
|
37 | |||
39 | virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes |
|
38 | virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes | |
40 | virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes |
|
39 | virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes | |
41 |
|
40 | |||
42 | protected slots: |
|
41 | protected slots: | |
43 | void handleModelChanged(int index); |
|
42 | void handleModelChanged(int index); | |
44 | void handleDomainChanged(const Domain& domain); |
|
43 | void handleDomainChanged(const Domain& domain); | |
45 | void handleGeometryChanged(const QRectF& size); |
|
44 | void handleGeometryChanged(const QRectF& size); | |
46 |
|
45 | |||
47 |
|
46 | |||
48 | protected: |
|
47 | protected: | |
49 |
|
48 | |||
50 | //BarChartSeriesBase& mSeries; |
|
|||
51 |
|
||||
52 | // TODO: consider these. |
|
49 | // TODO: consider these. | |
53 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) |
|
50 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) | |
54 | int mMax; |
|
51 | int mMax; | |
55 |
|
52 | |||
56 | int mHeight; // Layout spesific |
|
53 | int mHeight; // Layout spesific | |
57 | int mWidth; |
|
54 | int mWidth; | |
58 | int mBarDefaultWidth; |
|
55 | int mBarDefaultWidth; | |
59 |
|
56 | |||
60 | bool mLayoutSet; // True, if component has been laid out. |
|
57 | bool mLayoutSet; // True, if component has been laid out. | |
61 | bool mLayoutDirty; |
|
58 | bool mLayoutDirty; | |
62 |
|
59 | |||
63 | QList<QColor> mColors; // List of colors for series for now |
|
60 | QList<QColor> mColors; // List of colors for series for now | |
64 |
|
61 | |||
65 | ChartTheme* mTheme; |
|
62 | ChartTheme* mTheme; | |
66 | bool mSeparatorsVisible; |
|
63 | bool mSeparatorsVisible; | |
67 |
|
64 | |||
68 | BarChartModel mModel; |
|
65 | BarChartModel& mModel; | |
69 |
|
66 | |||
70 | }; |
|
67 | }; | |
71 |
|
68 | |||
72 | QTCOMMERCIALCHART_END_NAMESPACE |
|
69 | QTCOMMERCIALCHART_END_NAMESPACE | |
73 |
|
70 | |||
74 | #endif // BARGROUPBASE_H |
|
71 | #endif // BARGROUPBASE_H |
@@ -1,32 +1,31 | |||||
1 | #ifndef PERCENTBARCHARTSERIES_H |
|
1 | #ifndef PERCENTBARCHARTSERIES_H | |
2 | #define PERCENTBARCHARTSERIES_H |
|
2 | #define PERCENTBARCHARTSERIES_H | |
3 |
|
3 | |||
4 | #include <QList> |
|
4 | #include <QList> | |
5 | #include <QAbstractItemModel> |
|
5 | #include <QAbstractItemModel> | |
6 | #include "barchartseriesbase.h" |
|
6 | #include "barchartseriesbase.h" | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | class PercentBarGroup; |
|
10 | class PercentBarGroup; | |
11 |
|
11 | |||
12 | class QTCOMMERCIALCHART_EXPORT PercentBarChartSeries : public BarChartSeriesBase |
|
12 | class QTCOMMERCIALCHART_EXPORT PercentBarChartSeries : public BarChartSeriesBase | |
13 | { |
|
13 | { | |
14 | Q_OBJECT |
|
14 | Q_OBJECT | |
15 | public: |
|
15 | public: | |
16 | PercentBarChartSeries(QObject* parent=0); |
|
16 | PercentBarChartSeries(QObject* parent=0); | |
17 |
|
17 | |||
18 | // from BarChartSeriesBase |
|
18 | // from BarChartSeriesBase | |
19 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypePercentBar; } |
|
19 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypePercentBar; } | |
20 |
|
20 | |||
21 | public Q_SLOTS: |
|
21 | public Q_SLOTS: | |
22 |
|
22 | |||
23 | private: |
|
23 | private: | |
24 |
|
24 | |||
25 | QAbstractItemModel* mModel; |
|
|||
26 | PercentBarGroup* mPercentBarGroup; |
|
25 | PercentBarGroup* mPercentBarGroup; | |
27 | }; |
|
26 | }; | |
28 |
|
27 | |||
29 | QTCOMMERCIALCHART_END_NAMESPACE |
|
28 | QTCOMMERCIALCHART_END_NAMESPACE | |
30 |
|
29 | |||
31 |
|
30 | |||
32 | #endif // PERCENTBARCHARTSERIES_H |
|
31 | #endif // PERCENTBARCHARTSERIES_H |
@@ -1,74 +1,73 | |||||
1 | #include "percentbargroup.h" |
|
1 | #include "percentbargroup.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barlabel_p.h" |
|
3 | #include "barlabel_p.h" | |
4 | #include "separator_p.h" |
|
4 | #include "separator_p.h" | |
5 | #include <QDebug> |
|
5 | #include <QDebug> | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 |
|
9 | |||
10 | PercentBarGroup::PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent) : |
|
10 | PercentBarGroup::PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent) : | |
11 | BarGroupBase(series, parent) |
|
11 | BarGroupBase(series, parent) | |
12 | { |
|
12 | { | |
13 | } |
|
13 | } | |
14 |
|
14 | |||
15 | void PercentBarGroup::layoutChanged() |
|
15 | void PercentBarGroup::layoutChanged() | |
16 | { |
|
16 | { | |
17 | /* |
|
|||
18 | // Scale bars to new layout |
|
17 | // Scale bars to new layout | |
19 | // Layout for bars: |
|
18 | // Layout for bars: | |
20 |
if (m |
|
19 | if (mModel.countRows() <= 0) { | |
21 | // Nothing to do. |
|
20 | // Nothing to do. | |
22 | return; |
|
21 | return; | |
23 | } |
|
22 | } | |
24 |
|
23 | |||
25 | // TODO: better way to auto-layout |
|
24 | // TODO: better way to auto-layout | |
26 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
25 | // Use reals for accurancy (we might get some compiler warnings... :) | |
27 |
int count = m |
|
26 | int count = mModel.countColumns(); | |
28 | int itemIndex(0); |
|
27 | int itemIndex(0); | |
29 | qreal tW = mWidth; |
|
28 | qreal tW = mWidth; | |
30 | qreal tC = count+1; |
|
29 | qreal tC = count+1; | |
31 | qreal xStep = (tW/tC); |
|
30 | qreal xStep = (tW/tC); | |
32 | // qreal xPos = ((tW/tC) + mBarDefaultWidth / 2); |
|
31 | // qreal xPos = ((tW/tC) + mBarDefaultWidth / 2); | |
33 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); |
|
32 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); | |
34 |
int labelIndex = m |
|
33 | int labelIndex = mModel.countColumns() * mModel.countRows(); | |
35 |
|
34 | |||
36 |
for (int column = 0; column < m |
|
35 | for (int column = 0; column < mModel.countColumns(); column++) { | |
37 |
qreal colSum = m |
|
36 | qreal colSum = mModel.columnSum(column); | |
38 | qreal h = mHeight; |
|
37 | qreal h = mHeight; | |
39 | qreal scale = (h / colSum); |
|
38 | qreal scale = (h / colSum); | |
40 | qreal yPos = h; |
|
39 | qreal yPos = h; | |
41 |
for (int row=0; row < m |
|
40 | for (int row=0; row < mModel.countRows(); row++) { | |
42 |
qreal barHeight = m |
|
41 | qreal barHeight = mModel.valueAt(row, column) * scale; | |
43 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
42 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
44 |
|
43 | |||
45 | // TODO: width settable per bar? |
|
44 | // TODO: width settable per bar? | |
46 | bar->resize(mBarDefaultWidth, barHeight); |
|
45 | bar->resize(mBarDefaultWidth, barHeight); | |
47 | bar->setColor(mColors.at(row)); |
|
46 | bar->setColor(mColors.at(row)); | |
48 | bar->setPos(xPos, yPos-barHeight); |
|
47 | bar->setPos(xPos, yPos-barHeight); | |
49 | itemIndex++; |
|
48 | itemIndex++; | |
50 | yPos -= barHeight; |
|
49 | yPos -= barHeight; | |
51 | } |
|
50 | } | |
52 |
|
51 | |||
53 | // TODO: Layout for labels, remove magic number |
|
52 | // TODO: Layout for labels, remove magic number | |
54 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); |
|
53 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); | |
55 | label->setPos(xPos, mHeight + 20); |
|
54 | label->setPos(xPos, mHeight + 20); | |
56 | labelIndex++; |
|
55 | labelIndex++; | |
57 | xPos += xStep; |
|
56 | xPos += xStep; | |
58 | } |
|
57 | } | |
59 |
|
58 | |||
60 | // Position separators |
|
59 | // Position separators | |
61 | int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? |
|
60 | int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? | |
62 | xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. |
|
61 | xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. | |
63 |
for (int s=0; s < m |
|
62 | for (int s=0; s < mModel.countColumns() - 1; s++) { | |
64 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); |
|
63 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); | |
65 | sep->setPos(xPos,0); |
|
64 | sep->setPos(xPos,0); | |
66 | sep->setSize(QSizeF(1,mHeight)); |
|
65 | sep->setSize(QSizeF(1,mHeight)); | |
67 | xPos += xStep; |
|
66 | xPos += xStep; | |
68 | separatorIndex++; |
|
67 | separatorIndex++; | |
69 | } |
|
68 | } | |
70 | */ |
|
69 | ||
71 | mLayoutDirty = true; |
|
70 | mLayoutDirty = true; | |
72 | } |
|
71 | } | |
73 |
|
72 | |||
74 | QTCOMMERCIALCHART_END_NAMESPACE |
|
73 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,44 +1,44 | |||||
1 | #include "separator_p.h" |
|
1 | #include "separator_p.h" | |
2 | #include <QDebug> |
|
2 | #include <QDebug> | |
3 | #include <QPainter> |
|
3 | #include <QPainter> | |
4 |
|
4 | |||
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
6 |
|
6 | |||
7 | Separator::Separator(QGraphicsItem *parent) |
|
7 | Separator::Separator(QGraphicsItem *parent) | |
8 | : ChartItem(parent) |
|
8 | : ChartItem(parent) | |
9 | { |
|
9 | { | |
10 | } |
|
10 | } | |
11 |
|
11 | |||
12 | void Separator::setPos(qreal x, qreal y) |
|
12 | void Separator::setPos(qreal x, qreal y) | |
13 | { |
|
13 | { | |
14 | mXpos = x; |
|
14 | mXpos = x; | |
15 | mYpos = y; |
|
15 | mYpos = y; | |
16 | } |
|
16 | } | |
17 |
|
17 | |||
18 | void Separator::setColor(QColor color) |
|
18 | void Separator::setColor(QColor color) | |
19 | { |
|
19 | { | |
20 | mColor = color; |
|
20 | mColor = color; | |
21 | } |
|
21 | } | |
22 |
|
22 | |||
23 | void Separator::setSize(const QSizeF &size) |
|
23 | void Separator::setSize(const QSizeF &size) | |
24 | { |
|
24 | { | |
25 | mWidth = size.width(); |
|
25 | mWidth = size.width(); | |
26 | mHeight = size.height(); |
|
26 | mHeight = size.height(); | |
27 | } |
|
27 | } | |
28 |
|
28 | |||
29 | void Separator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
29 | void Separator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
30 | { |
|
30 | { | |
31 | qDebug() << "separator::paint" << boundingRect(); |
|
31 | // qDebug() << "separator::paint" << boundingRect(); | |
32 | QPen pen(mColor); |
|
32 | QPen pen(mColor); | |
33 | painter->setPen(pen); |
|
33 | painter->setPen(pen); | |
34 | painter->drawLine(mXpos,mYpos,mXpos,mHeight); |
|
34 | painter->drawLine(mXpos,mYpos,mXpos,mHeight); | |
35 | } |
|
35 | } | |
36 |
|
36 | |||
37 | QRectF Separator::boundingRect() const |
|
37 | QRectF Separator::boundingRect() const | |
38 | { |
|
38 | { | |
39 | QRectF r(mXpos,mYpos,mWidth,mHeight); |
|
39 | QRectF r(mXpos,mYpos,mWidth,mHeight); | |
40 | return r; |
|
40 | return r; | |
41 | } |
|
41 | } | |
42 |
|
42 | |||
43 |
|
43 | |||
44 | QTCOMMERCIALCHART_END_NAMESPACE |
|
44 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,31 +1,30 | |||||
1 | #ifndef STACKEDBARCHARTSERIES_H |
|
1 | #ifndef STACKEDBARCHARTSERIES_H | |
2 | #define STACKEDBARCHARTSERIES_H |
|
2 | #define STACKEDBARCHARTSERIES_H | |
3 |
|
3 | |||
4 | #include <QList> |
|
4 | #include <QList> | |
5 | #include <QAbstractItemModel> |
|
5 | #include <QAbstractItemModel> | |
6 | #include "barchartseriesbase.h" |
|
6 | #include "barchartseriesbase.h" | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | class StackedBarGroup; |
|
10 | class StackedBarGroup; | |
11 |
|
11 | |||
12 | class QTCOMMERCIALCHART_EXPORT StackedBarChartSeries : public BarChartSeriesBase |
|
12 | class QTCOMMERCIALCHART_EXPORT StackedBarChartSeries : public BarChartSeriesBase | |
13 | { |
|
13 | { | |
14 | Q_OBJECT |
|
14 | Q_OBJECT | |
15 | public: |
|
15 | public: | |
16 | StackedBarChartSeries(QObject* parent=0); |
|
16 | StackedBarChartSeries(QObject* parent=0); | |
17 |
|
17 | |||
18 | // from QChartSeries |
|
18 | // from QChartSeries | |
19 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeStackedBar; } |
|
19 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeStackedBar; } | |
20 |
|
20 | |||
21 | public Q_SLOTS: |
|
21 | public Q_SLOTS: | |
22 |
|
22 | |||
23 | private: |
|
23 | private: | |
24 |
|
24 | |||
25 | QAbstractItemModel* mModel; |
|
|||
26 | StackedBarGroup* mStackedBarGroup; |
|
25 | StackedBarGroup* mStackedBarGroup; | |
27 | }; |
|
26 | }; | |
28 |
|
27 | |||
29 | QTCOMMERCIALCHART_END_NAMESPACE |
|
28 | QTCOMMERCIALCHART_END_NAMESPACE | |
30 |
|
29 | |||
31 | #endif // STACKEDBARCHARTSERIES_H |
|
30 | #endif // STACKEDBARCHARTSERIES_H |
@@ -1,82 +1,82 | |||||
1 | #include "stackedbargroup.h" |
|
1 | #include "stackedbargroup.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barlabel_p.h" |
|
3 | #include "barlabel_p.h" | |
4 | #include "separator_p.h" |
|
4 | #include "separator_p.h" | |
5 | #include <QDebug> |
|
5 | #include <QDebug> | |
6 | #include <QPainter> |
|
6 | //#include <QPainter> | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) : |
|
10 | StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) : | |
11 | BarGroupBase(series,parent) |
|
11 | BarGroupBase(series,parent) | |
12 | { |
|
12 | { | |
13 | } |
|
13 | } | |
14 |
|
14 | |||
15 | void StackedBarGroup::layoutChanged() |
|
15 | void StackedBarGroup::layoutChanged() | |
16 | { |
|
16 | { | |
17 | /* |
|
17 | ||
18 | // Scale bars to new layout |
|
18 | // Scale bars to new layout | |
19 | // Layout for bars: |
|
19 | // Layout for bars: | |
20 |
if (m |
|
20 | if (mModel.countRows() <= 0) { | |
21 | // Nothing to do. |
|
21 | // Nothing to do. | |
22 | return; |
|
22 | return; | |
23 | } |
|
23 | } | |
24 |
|
24 | |||
25 |
if (m |
|
25 | if (mModel.countColumns() == 0) { | |
26 | // Nothing to do |
|
26 | // Nothing to do | |
27 | return; |
|
27 | return; | |
28 | } |
|
28 | } | |
29 |
|
29 | |||
30 | // TODO: better way to auto-layout |
|
30 | // TODO: better way to auto-layout | |
31 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
31 | // Use reals for accurancy (we might get some compiler warnings... :) | |
32 | // TODO: use temp variable for column count... |
|
32 | // TODO: use temp variable for column count... | |
33 |
qreal maxSum = m |
|
33 | qreal maxSum = mModel.maxColumnSum(); | |
34 | qreal h = mHeight; |
|
34 | qreal h = mHeight; | |
35 | qreal scale = (h / maxSum); |
|
35 | qreal scale = (h / maxSum); | |
36 |
|
36 | |||
37 | int itemIndex(0); |
|
37 | int itemIndex(0); | |
38 | qreal tW = mWidth; |
|
38 | qreal tW = mWidth; | |
39 |
qreal tC = m |
|
39 | qreal tC = mModel.countColumns() + 1; | |
40 | qreal xStep = (tW/tC); |
|
40 | qreal xStep = (tW/tC); | |
41 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); |
|
41 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); | |
42 |
int labelIndex = m |
|
42 | int labelIndex = mModel.countRows() * mModel.countColumns(); | |
43 |
|
43 | |||
44 |
for (int column = 0; column < m |
|
44 | for (int column = 0; column < mModel.countColumns(); column++) { | |
45 | qreal yPos = h; |
|
45 | qreal yPos = h; | |
46 |
for (int row=0; row < m |
|
46 | for (int row=0; row < mModel.countRows(); row++) { | |
47 |
qreal barHeight = m |
|
47 | qreal barHeight = mModel.valueAt(row, column) * scale; | |
48 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
48 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
49 |
|
49 | |||
50 | // TODO: width settable per bar? |
|
50 | // TODO: width settable per bar? | |
51 | // TODO: how to get color for series(x) from theme? |
|
51 | // TODO: how to get color for series(x) from theme? | |
52 | // mTheme->themeForSeries(); |
|
52 | // mTheme->themeForSeries(); | |
53 | bar->resize(mBarDefaultWidth, barHeight); |
|
53 | bar->resize(mBarDefaultWidth, barHeight); | |
54 | bar->setColor(mColors.at(row)); |
|
54 | bar->setColor(mColors.at(row)); | |
55 | bar->setPos(xPos, yPos-barHeight); |
|
55 | bar->setPos(xPos, yPos-barHeight); | |
56 | itemIndex++; |
|
56 | itemIndex++; | |
57 | yPos -= barHeight; |
|
57 | yPos -= barHeight; | |
58 | } |
|
58 | } | |
59 |
|
59 | |||
60 | // TODO: Layout for labels, remove magic number |
|
60 | // TODO: Layout for labels, remove magic number | |
61 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); |
|
61 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); | |
62 | label->setPos(xPos, mHeight + 20); |
|
62 | label->setPos(xPos, mHeight + 20); | |
63 | labelIndex++; |
|
63 | labelIndex++; | |
64 | xPos += xStep; |
|
64 | xPos += xStep; | |
65 | } |
|
65 | } | |
66 |
|
66 | |||
67 |
|
67 | |||
68 | // Position separators |
|
68 | // Position separators | |
69 | int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? |
|
69 | int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? | |
70 | xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. |
|
70 | xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. | |
71 |
for (int s=0; s < m |
|
71 | for (int s=0; s < mModel.countColumns() - 1; s++) { | |
72 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); |
|
72 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); | |
73 | sep->setPos(xPos,0); |
|
73 | sep->setPos(xPos,0); | |
74 | sep->setSize(QSizeF(1,mHeight)); |
|
74 | sep->setSize(QSizeF(1,mHeight)); | |
75 | xPos += xStep; |
|
75 | xPos += xStep; | |
76 | separatorIndex++; |
|
76 | separatorIndex++; | |
77 | } |
|
77 | } | |
78 | */ |
|
78 | ||
79 | mLayoutDirty = true; |
|
79 | mLayoutDirty = true; | |
80 | } |
|
80 | } | |
81 |
|
81 | |||
82 | QTCOMMERCIALCHART_END_NAMESPACE |
|
82 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,165 +1,160 | |||||
1 | #include "chartdataset_p.h" |
|
1 | #include "chartdataset_p.h" | |
2 | //series |
|
2 | //series | |
3 | #include "qlinechartseries.h" |
|
3 | #include "qlinechartseries.h" | |
4 | #include "barchartseries.h" |
|
4 | #include "barchartseries.h" | |
5 | #include "stackedbarchartseries.h" |
|
5 | #include "stackedbarchartseries.h" | |
6 | #include "percentbarchartseries.h" |
|
6 | #include "percentbarchartseries.h" | |
7 | #include "qpieseries.h" |
|
7 | #include "qpieseries.h" | |
8 | #include "qscatterseries.h" |
|
8 | #include "qscatterseries.h" | |
9 |
|
9 | |||
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
11 |
|
11 | |||
12 | ChartDataSet::ChartDataSet(QObject *parent):QObject(parent) |
|
12 | ChartDataSet::ChartDataSet(QObject *parent):QObject(parent) | |
13 | { |
|
13 | { | |
14 | Domain domain; |
|
14 | Domain domain; | |
15 | m_domains<<domain; |
|
15 | m_domains<<domain; | |
16 | } |
|
16 | } | |
17 |
|
17 | |||
18 | ChartDataSet::~ChartDataSet() |
|
18 | ChartDataSet::~ChartDataSet() | |
19 | { |
|
19 | { | |
20 | // TODO Auto-generated destructor stub |
|
20 | // TODO Auto-generated destructor stub | |
21 | } |
|
21 | } | |
22 |
|
22 | |||
23 | const Domain& ChartDataSet::domain() const |
|
23 | const Domain& ChartDataSet::domain() const | |
24 | { |
|
24 | { | |
25 | return m_domains[m_domainIndex]; |
|
25 | return m_domains[m_domainIndex]; | |
26 | } |
|
26 | } | |
27 |
|
27 | |||
28 | void ChartDataSet::addSeries(QChartSeries* series) |
|
28 | void ChartDataSet::addSeries(QChartSeries* series) | |
29 | { |
|
29 | { | |
30 | // TODO: we should check the series not already added |
|
30 | // TODO: we should check the series not already added | |
31 | m_chartSeries << series; |
|
31 | m_chartSeries << series; | |
32 | m_domainIndex = 0; |
|
32 | m_domainIndex = 0; | |
33 | m_domains.resize(1); |
|
33 | m_domains.resize(1); | |
34 |
|
34 | |||
35 | Domain& domain = m_domains[m_domainIndex]; |
|
35 | Domain& domain = m_domains[m_domainIndex]; | |
36 |
|
36 | |||
37 | switch(series->type()) |
|
37 | switch(series->type()) | |
38 | { |
|
38 | { | |
39 | case QChartSeries::SeriesTypeLine: { |
|
39 | case QChartSeries::SeriesTypeLine: { | |
40 |
|
40 | |||
41 | QLineChartSeries* xyseries = static_cast<QLineChartSeries*>(series); |
|
41 | QLineChartSeries* xyseries = static_cast<QLineChartSeries*>(series); | |
42 |
|
42 | |||
43 | for (int i = 0; i < xyseries->count(); i++) |
|
43 | for (int i = 0; i < xyseries->count(); i++) | |
44 | { |
|
44 | { | |
45 | qreal x = xyseries->x(i); |
|
45 | qreal x = xyseries->x(i); | |
46 | qreal y = xyseries->y(i); |
|
46 | qreal y = xyseries->y(i); | |
47 | domain.m_minX = qMin(domain.m_minX,x); |
|
47 | domain.m_minX = qMin(domain.m_minX,x); | |
48 | domain.m_minY = qMin(domain.m_minY,y); |
|
48 | domain.m_minY = qMin(domain.m_minY,y); | |
49 | domain.m_maxX = qMax(domain.m_maxX,x); |
|
49 | domain.m_maxX = qMax(domain.m_maxX,x); | |
50 | domain.m_maxY = qMax(domain.m_maxY,y); |
|
50 | domain.m_maxY = qMax(domain.m_maxY,y); | |
51 | } |
|
51 | } | |
52 | break; |
|
52 | break; | |
53 | } |
|
53 | } | |
54 | case QChartSeries::SeriesTypeBar: { |
|
54 | case QChartSeries::SeriesTypeBar: { | |
55 |
|
55 | |||
56 | BarChartSeries* barSeries = static_cast<BarChartSeries*>(series); |
|
56 | BarChartSeries* barSeries = static_cast<BarChartSeries*>(series); | |
57 |
qreal x = barSeries->count |
|
57 | qreal x = barSeries->countColumns(); | |
58 | qreal y = barSeries->max(); |
|
58 | qreal y = barSeries->max(); | |
59 | domain.m_minX = qMin(domain.m_minX,x); |
|
59 | domain.m_minX = qMin(domain.m_minX,x); | |
60 | domain.m_minY = qMin(domain.m_minY,y); |
|
60 | domain.m_minY = qMin(domain.m_minY,y); | |
61 | domain.m_maxX = qMax(domain.m_maxX,x); |
|
61 | domain.m_maxX = qMax(domain.m_maxX,x); | |
62 | domain.m_maxY = qMax(domain.m_maxY,y); |
|
62 | domain.m_maxY = qMax(domain.m_maxY,y); | |
63 |
|
||||
64 | } |
|
63 | } | |
65 | break; |
|
64 | break; | |
66 | case QChartSeries::SeriesTypeStackedBar: { |
|
65 | case QChartSeries::SeriesTypeStackedBar: { | |
67 |
|
66 | |||
68 | StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series); |
|
67 | StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series); | |
69 | /* |
|
|||
70 | qreal x = stackedBarSeries->countColumns(); |
|
68 | qreal x = stackedBarSeries->countColumns(); | |
71 | qreal y = stackedBarSeries->maxColumnSum(); |
|
69 | qreal y = stackedBarSeries->maxColumnSum(); | |
72 | domain.m_minX = qMin(domain.m_minX,x); |
|
70 | domain.m_minX = qMin(domain.m_minX,x); | |
73 | domain.m_minY = qMin(domain.m_minY,y); |
|
71 | domain.m_minY = qMin(domain.m_minY,y); | |
74 | domain.m_maxX = qMax(domain.m_maxX,x); |
|
72 | domain.m_maxX = qMax(domain.m_maxX,x); | |
75 | domain.m_maxY = qMax(domain.m_maxY,y); |
|
73 | domain.m_maxY = qMax(domain.m_maxY,y); | |
76 | */ |
|
|||
77 | } |
|
74 | } | |
78 | break; |
|
75 | break; | |
79 | case QChartSeries::SeriesTypePercentBar: { |
|
76 | case QChartSeries::SeriesTypePercentBar: { | |
80 |
|
77 | |||
81 | PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series); |
|
78 | PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series); | |
82 | /* |
|
|||
83 | qreal x = percentBarSeries->countColumns(); |
|
79 | qreal x = percentBarSeries->countColumns(); | |
84 | domain.m_minX = qMin(domain.m_minX,x); |
|
80 | domain.m_minX = qMin(domain.m_minX,x); | |
85 | domain.m_minY = 0; |
|
81 | domain.m_minY = 0; | |
86 | domain.m_maxX = qMax(domain.m_maxX,x); |
|
82 | domain.m_maxX = qMax(domain.m_maxX,x); | |
87 | domain.m_maxY = 100; |
|
83 | domain.m_maxY = 100; | |
88 | */ |
|
|||
89 | } |
|
84 | } | |
90 | break; |
|
85 | break; | |
91 |
|
86 | |||
92 | case QChartSeries::SeriesTypePie: { |
|
87 | case QChartSeries::SeriesTypePie: { | |
93 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
88 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | |
94 | // TODO: domain stuff |
|
89 | // TODO: domain stuff | |
95 | break; |
|
90 | break; | |
96 | } |
|
91 | } | |
97 |
|
92 | |||
98 | case QChartSeries::SeriesTypeScatter: { |
|
93 | case QChartSeries::SeriesTypeScatter: { | |
99 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); |
|
94 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); | |
100 | Q_ASSERT(scatterSeries); |
|
95 | Q_ASSERT(scatterSeries); | |
101 | foreach (QPointF point, scatterSeries->data()) { |
|
96 | foreach (QPointF point, scatterSeries->data()) { | |
102 | domain.m_minX = qMin(domain.m_minX, point.x()); |
|
97 | domain.m_minX = qMin(domain.m_minX, point.x()); | |
103 | domain.m_maxX = qMax(domain.m_maxX, point.x()); |
|
98 | domain.m_maxX = qMax(domain.m_maxX, point.x()); | |
104 | domain.m_minY = qMin(domain.m_minY, point.y()); |
|
99 | domain.m_minY = qMin(domain.m_minY, point.y()); | |
105 | domain.m_maxY = qMax(domain.m_maxY, point.y()); |
|
100 | domain.m_maxY = qMax(domain.m_maxY, point.y()); | |
106 | } |
|
101 | } | |
107 | break; |
|
102 | break; | |
108 | } |
|
103 | } | |
109 |
|
104 | |||
110 | default: { |
|
105 | default: { | |
111 | qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported"; |
|
106 | qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported"; | |
112 | return; |
|
107 | return; | |
113 | break; |
|
108 | break; | |
114 | } |
|
109 | } | |
115 |
|
110 | |||
116 | } |
|
111 | } | |
117 |
|
112 | |||
118 | emit seriesAdded(series); |
|
113 | emit seriesAdded(series); | |
119 | emit domainChanged(domain); |
|
114 | emit domainChanged(domain); | |
120 | } |
|
115 | } | |
121 |
|
116 | |||
122 | bool ChartDataSet::nextDomain() |
|
117 | bool ChartDataSet::nextDomain() | |
123 | { |
|
118 | { | |
124 | if (m_domainIndex < m_domains.count() - 1) { |
|
119 | if (m_domainIndex < m_domains.count() - 1) { | |
125 | m_domainIndex++; |
|
120 | m_domainIndex++; | |
126 | emit domainChanged(m_domains[m_domainIndex]); |
|
121 | emit domainChanged(m_domains[m_domainIndex]); | |
127 | return true; |
|
122 | return true; | |
128 | } |
|
123 | } | |
129 | else { |
|
124 | else { | |
130 | return false; |
|
125 | return false; | |
131 | } |
|
126 | } | |
132 | } |
|
127 | } | |
133 |
|
128 | |||
134 | bool ChartDataSet::previousDomain() |
|
129 | bool ChartDataSet::previousDomain() | |
135 | { |
|
130 | { | |
136 | if (m_domainIndex > 0) { |
|
131 | if (m_domainIndex > 0) { | |
137 | m_domainIndex--; |
|
132 | m_domainIndex--; | |
138 | emit domainChanged(m_domains[m_domainIndex]); |
|
133 | emit domainChanged(m_domains[m_domainIndex]); | |
139 | return true; |
|
134 | return true; | |
140 | } |
|
135 | } | |
141 | else { |
|
136 | else { | |
142 | return false; |
|
137 | return false; | |
143 | } |
|
138 | } | |
144 | } |
|
139 | } | |
145 |
|
140 | |||
146 | void ChartDataSet::clearDomains() |
|
141 | void ChartDataSet::clearDomains() | |
147 | { |
|
142 | { | |
148 | if (m_domainIndex > 0) { |
|
143 | if (m_domainIndex > 0) { | |
149 | m_domainIndex = 0; |
|
144 | m_domainIndex = 0; | |
150 | emit domainChanged(m_domains[m_domainIndex]); |
|
145 | emit domainChanged(m_domains[m_domainIndex]); | |
151 | } |
|
146 | } | |
152 | } |
|
147 | } | |
153 |
|
148 | |||
154 | void ChartDataSet::addDomain(const Domain& domain) |
|
149 | void ChartDataSet::addDomain(const Domain& domain) | |
155 | { |
|
150 | { | |
156 | m_domains.resize(m_domainIndex + 1); |
|
151 | m_domains.resize(m_domainIndex + 1); | |
157 | m_domains << domain; |
|
152 | m_domains << domain; | |
158 | m_domainIndex++; |
|
153 | m_domainIndex++; | |
159 |
|
154 | |||
160 | emit domainChanged(domain); |
|
155 | emit domainChanged(domain); | |
161 | } |
|
156 | } | |
162 |
|
157 | |||
163 | #include "moc_chartdataset_p.cpp" |
|
158 | #include "moc_chartdataset_p.cpp" | |
164 |
|
159 | |||
165 | QTCOMMERCIALCHART_END_NAMESPACE |
|
160 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now