##// END OF EJS Templates
review fix: Removed iterator from barseries. Remove const from brush and pen. Renamed setters for tooltip, floating values and separators
sauimone -
r357:82b904eaae07
parent child
Show More
@@ -1,69 +1,69
1 #include <QApplication>
1 #include <QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <qchartview.h>
3 #include <qchartview.h>
4 #include <qbarseries.h>
4 #include <qbarseries.h>
5 #include <qbarset.h>
5 #include <qbarset.h>
6 #include <qbarcategory.h>
6 #include <qbarcategory.h>
7
7
8 QTCOMMERCIALCHART_USE_NAMESPACE
8 QTCOMMERCIALCHART_USE_NAMESPACE
9
9
10 int main(int argc, char *argv[])
10 int main(int argc, char *argv[])
11 {
11 {
12 QApplication a(argc, argv);
12 QApplication a(argc, argv);
13 QMainWindow window;
13 QMainWindow window;
14
14
15 //! [1]
15 //! [1]
16 // Create category
16 // Create category
17 QBarCategory *category = new QBarCategory;
17 QBarCategory *category = new QBarCategory;
18 *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
18 *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
19 //! [1]
19 //! [1]
20
20
21 //! [2]
21 //! [2]
22 // Create some test sets for chat
22 // Create some test sets for chat
23
23
24 QBarSet *set0 = new QBarSet("Bub");
24 QBarSet *set0 = new QBarSet("Bub");
25 QBarSet *set1 = new QBarSet("Bob");
25 QBarSet *set1 = new QBarSet("Bob");
26 QBarSet *set2 = new QBarSet("Guybrush");
26 QBarSet *set2 = new QBarSet("Guybrush");
27 QBarSet *set3 = new QBarSet("Larry");
27 QBarSet *set3 = new QBarSet("Larry");
28 QBarSet *set4 = new QBarSet("Zak");
28 QBarSet *set4 = new QBarSet("Zak");
29
29
30 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
30 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
31 *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2;
31 *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2;
32 *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5;
32 *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5;
33 *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7;
33 *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7;
34 *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6;
34 *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6;
35 //! [2]
35 //! [2]
36
36
37 //! [3]
37 //! [3]
38 // Create series and add sets to it
38 // Create series and add sets to it
39 QBarSeries* series= new QBarSeries(category);
39 QBarSeries* series= new QBarSeries(category);
40
40
41 series->addBarSet(set0);
41 series->addBarSet(set0);
42 series->addBarSet(set1);
42 series->addBarSet(set1);
43 series->addBarSet(set2);
43 series->addBarSet(set2);
44 series->addBarSet(set3);
44 series->addBarSet(set3);
45 series->addBarSet(set4);
45 series->addBarSet(set4);
46 //! [3]
46 //! [3]
47
47
48 //! [4]
48 //! [4]
49 // Enable some features
49 // Enable some features
50 series->enableToolTip();
50 series->setToolTipEnabled();
51 series->enableFloatingValues();
51 series->enableFloatingValues();
52 //! [4]
52 //! [4]
53
53
54 //! [5]
54 //! [5]
55 // Create view for chart and add series to it. Apply theme.
55 // Create view for chart and add series to it. Apply theme.
56
56
57 QChartView* chartView = new QChartView(&window);
57 QChartView* chartView = new QChartView(&window);
58 chartView->addSeries(series);
58 chartView->addSeries(series);
59 chartView->setChartTitle("simple stacked barchart");
59 chartView->setChartTitle("simple stacked barchart");
60 chartView->setChartTheme(QChart::ChartThemeIcy);
60 chartView->setChartTheme(QChart::ChartThemeIcy);
61 //! [5]
61 //! [5]
62
62
63 window.setCentralWidget(chartView);
63 window.setCentralWidget(chartView);
64 window.resize(600, 300);
64 window.resize(600, 300);
65 window.show();
65 window.show();
66
66
67 return a.exec();
67 return a.exec();
68 }
68 }
69
69
@@ -1,69 +1,69
1 #include <QApplication>
1 #include <QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <QStandardItemModel>
3 #include <QStandardItemModel>
4 #include <qpercentbarseries.h>
4 #include <qpercentbarseries.h>
5 #include <qbarcategory.h>
5 #include <qbarcategory.h>
6 #include <qchartview.h>
6 #include <qchartview.h>
7 #include <qbarset.h>
7 #include <qbarset.h>
8
8
9 QTCOMMERCIALCHART_USE_NAMESPACE
9 QTCOMMERCIALCHART_USE_NAMESPACE
10
10
11 int main(int argc, char *argv[])
11 int main(int argc, char *argv[])
12 {
12 {
13 QApplication a(argc, argv);
13 QApplication a(argc, argv);
14 QMainWindow window;
14 QMainWindow window;
15
15
16 //! [1]
16 //! [1]
17 // Create category
17 // Create category
18 QBarCategory *category = new QBarCategory;
18 QBarCategory *category = new QBarCategory;
19 *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
19 *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
20 //! [1]
20 //! [1]
21
21
22 //! [2]
22 //! [2]
23 // Create some test sets for chat
23 // Create some test sets for chat
24 QBarSet *set0 = new QBarSet("Bub");
24 QBarSet *set0 = new QBarSet("Bub");
25 QBarSet *set1 = new QBarSet("Bob");
25 QBarSet *set1 = new QBarSet("Bob");
26 QBarSet *set2 = new QBarSet("Guybrush");
26 QBarSet *set2 = new QBarSet("Guybrush");
27 QBarSet *set3 = new QBarSet("Larry");
27 QBarSet *set3 = new QBarSet("Larry");
28 QBarSet *set4 = new QBarSet("Zak");
28 QBarSet *set4 = new QBarSet("Zak");
29
29
30 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
30 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
31 *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2;
31 *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2;
32 *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5;
32 *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5;
33 *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7;
33 *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7;
34 *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6;
34 *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6;
35 //! [2]
35 //! [2]
36
36
37 //! [3]
37 //! [3]
38 // Create series and add sets to it
38 // Create series and add sets to it
39 QPercentBarSeries* series = new QPercentBarSeries(category);
39 QPercentBarSeries* series = new QPercentBarSeries(category);
40
40
41 series->addBarSet(set0);
41 series->addBarSet(set0);
42 series->addBarSet(set1);
42 series->addBarSet(set1);
43 series->addBarSet(set2);
43 series->addBarSet(set2);
44 series->addBarSet(set3);
44 series->addBarSet(set3);
45 series->addBarSet(set4);
45 series->addBarSet(set4);
46 //! [3]
46 //! [3]
47
47
48 //! [4]
48 //! [4]
49 // Enable features
49 // Enable features
50 series->enableToolTip();
50 series->setToolTipEnabled();
51 series->enableFloatingValues();
51 series->enableFloatingValues();
52 //! [4]
52 //! [4]
53
53
54 //! [5]
54 //! [5]
55 // Create view for chart and add series to it. Apply theme.
55 // Create view for chart and add series to it. Apply theme.
56
56
57 QChartView* chartView = new QChartView(&window);
57 QChartView* chartView = new QChartView(&window);
58 chartView->addSeries(series);
58 chartView->addSeries(series);
59 chartView->setChartTitle("simple percent barchart");
59 chartView->setChartTitle("simple percent barchart");
60 chartView->setChartTheme(QChart::ChartThemeIcy);
60 chartView->setChartTheme(QChart::ChartThemeIcy);
61 //! [5]
61 //! [5]
62
62
63 window.setCentralWidget(chartView);
63 window.setCentralWidget(chartView);
64 window.resize(400, 300);
64 window.resize(400, 300);
65 window.show();
65 window.show();
66
66
67 return a.exec();
67 return a.exec();
68 }
68 }
69
69
@@ -1,68 +1,68
1 #include <QApplication>
1 #include <QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <qchartview.h>
3 #include <qchartview.h>
4 #include <qstackedbarseries.h>
4 #include <qstackedbarseries.h>
5 #include <qbarset.h>
5 #include <qbarset.h>
6 #include <qbarcategory.h>
6 #include <qbarcategory.h>
7
7
8 QTCOMMERCIALCHART_USE_NAMESPACE
8 QTCOMMERCIALCHART_USE_NAMESPACE
9
9
10 int main(int argc, char *argv[])
10 int main(int argc, char *argv[])
11 {
11 {
12 QApplication a(argc, argv);
12 QApplication a(argc, argv);
13 QMainWindow window;
13 QMainWindow window;
14
14
15 //! [1]
15 //! [1]
16 // Create category
16 // Create category
17 QBarCategory *category = new QBarCategory;
17 QBarCategory *category = new QBarCategory;
18 *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
18 *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
19 //! [1]
19 //! [1]
20
20
21 //! [2]
21 //! [2]
22 // Create some test sets for chat
22 // Create some test sets for chat
23 QBarSet *set0 = new QBarSet("Bub");
23 QBarSet *set0 = new QBarSet("Bub");
24 QBarSet *set1 = new QBarSet("Bob");
24 QBarSet *set1 = new QBarSet("Bob");
25 QBarSet *set2 = new QBarSet("Guybrush");
25 QBarSet *set2 = new QBarSet("Guybrush");
26 QBarSet *set3 = new QBarSet("Larry");
26 QBarSet *set3 = new QBarSet("Larry");
27 QBarSet *set4 = new QBarSet("Zak");
27 QBarSet *set4 = new QBarSet("Zak");
28
28
29 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
29 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
30 *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2;
30 *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2;
31 *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5;
31 *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5;
32 *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7;
32 *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7;
33 *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6;
33 *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6;
34 //! [2]
34 //! [2]
35
35
36 //! [3]
36 //! [3]
37 // Create series and add sets to it
37 // Create series and add sets to it
38 QStackedBarSeries* series = new QStackedBarSeries(category);
38 QStackedBarSeries* series = new QStackedBarSeries(category);
39
39
40 series->addBarSet(set0);
40 series->addBarSet(set0);
41 series->addBarSet(set1);
41 series->addBarSet(set1);
42 series->addBarSet(set2);
42 series->addBarSet(set2);
43 series->addBarSet(set3);
43 series->addBarSet(set3);
44 series->addBarSet(set4);
44 series->addBarSet(set4);
45 //! [3]
45 //! [3]
46
46
47 //! [4]
47 //! [4]
48 // Enable features
48 // Enable features
49 series->enableToolTip();
49 series->setToolTipEnabled();
50 series->enableFloatingValues();
50 series->enableFloatingValues();
51 //! [4]
51 //! [4]
52
52
53 //! [5]
53 //! [5]
54 // Create view for chart and add series to it. Apply theme.
54 // Create view for chart and add series to it. Apply theme.
55
55
56 QChartView* chartView = new QChartView(&window);
56 QChartView* chartView = new QChartView(&window);
57 chartView->addSeries(series);
57 chartView->addSeries(series);
58 chartView->setChartTitle("simple stacked barchart");
58 chartView->setChartTitle("simple stacked barchart");
59 chartView->setChartTheme(QChart::ChartThemeIcy);
59 chartView->setChartTheme(QChart::ChartThemeIcy);
60 //! [5]
60 //! [5]
61
61
62 window.setCentralWidget(chartView);
62 window.setCentralWidget(chartView);
63 window.resize(400, 300);
63 window.resize(400, 300);
64 window.show();
64 window.show();
65
65
66 return a.exec();
66 return a.exec();
67 }
67 }
68
68
@@ -1,200 +1,190
1 #include <limits.h>
1 #include <limits.h>
2 #include <QVector>
2 #include <QVector>
3 #include <QDebug>
3 #include <QDebug>
4 #include "barchartmodel_p.h"
4 #include "barchartmodel_p.h"
5 #include "qbarcategory.h"
5 #include "qbarcategory.h"
6 #include "qbarset.h"
6 #include "qbarset.h"
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 BarChartModel::BarChartModel(QBarCategory *category, QObject *parent) :
10 BarChartModel::BarChartModel(QBarCategory *category, QObject *parent) :
11 QObject(parent)
11 QObject(parent)
12 ,mCategory(category)
12 ,mCategory(category)
13 {
13 {
14 }
14 }
15
15
16 BarChartModel::~BarChartModel()
16 BarChartModel::~BarChartModel()
17 {
17 {
18 delete mCategory;
18 delete mCategory;
19 }
19 }
20
20
21
21
22 QBarCategory& BarChartModel::category()
22 QBarCategory& BarChartModel::category()
23 {
23 {
24 return *mCategory;
24 return *mCategory;
25 }
25 }
26
26
27 void BarChartModel::addBarSet(QBarSet *set)
27 void BarChartModel::addBarSet(QBarSet *set)
28 {
28 {
29 mDataModel.append(set);
29 mDataModel.append(set);
30 }
30 }
31
31
32 void BarChartModel::removeBarSet(QBarSet *set)
32 void BarChartModel::removeBarSet(QBarSet *set)
33 {
33 {
34 if (mDataModel.contains(set)) {
34 if (mDataModel.contains(set)) {
35 mDataModel.removeOne(set);
35 mDataModel.removeOne(set);
36 }
36 }
37 }
37 }
38
38
39 QBarSet* BarChartModel::nextSet(bool getFirst)
39 QBarSet* BarChartModel::setAt(int index)
40 {
40 {
41 if (getFirst) {
41 return mDataModel.at(index);
42 mCurrentSet = 0;
43 }
44
45 if ((mDataModel.count() <= 0) || (mDataModel.count() <= mCurrentSet)) {
46 return 0;
47 }
48
49 QBarSet* set = mDataModel.at(mCurrentSet);
50 mCurrentSet++;
51 return set;
52 }
42 }
53
43
54 QBarSet* BarChartModel::setAt(int index)
44 QList<QBarSet*> BarChartModel::barSets()
55 {
45 {
56 return mDataModel.at(index);
46 return mDataModel;
57 }
47 }
58
48
59 QList<QString> BarChartModel::legend()
49 QList<QString> BarChartModel::legend()
60 {
50 {
61 QList<QString> legend;
51 QList<QString> legend;
62
52
63 for (int i=0; i<mDataModel.count(); i++) {
53 for (int i=0; i<mDataModel.count(); i++) {
64 legend.append(mDataModel.at(i)->name());
54 legend.append(mDataModel.at(i)->name());
65 }
55 }
66 return legend;
56 return legend;
67 }
57 }
68
58
69 int BarChartModel::countSets()
59 int BarChartModel::countSets()
70 {
60 {
71 return mDataModel.count();
61 return mDataModel.count();
72 }
62 }
73
63
74 int BarChartModel::countCategories()
64 int BarChartModel::countCategories()
75 {
65 {
76 int count(0);
66 int count(0);
77 for (int i=0; i<mDataModel.count(); i++){
67 for (int i=0; i<mDataModel.count(); i++){
78 // TODO: can we assume that all series have same number of values? If not. then which values are empty?
68 // TODO: can we assume that all series have same number of values? If not. then which values are empty?
79 int temp = mDataModel.at(i)->count();
69 int temp = mDataModel.at(i)->count();
80 if (temp > count) {
70 if (temp > count) {
81 count = temp;
71 count = temp;
82 }
72 }
83 }
73 }
84 return count;
74 return count;
85 }
75 }
86
76
87 int BarChartModel::countTotalItems()
77 int BarChartModel::countTotalItems()
88 {
78 {
89 int total = mDataModel.count() * countCategories();
79 int total = mDataModel.count() * countCategories();
90 return total;
80 return total;
91 }
81 }
92
82
93 qreal BarChartModel::min()
83 qreal BarChartModel::min()
94 {
84 {
95 Q_ASSERT(mDataModel.count() > 0);
85 Q_ASSERT(mDataModel.count() > 0);
96 // TODO: make min and max members and update them when data changes.
86 // TODO: make min and max members and update them when data changes.
97 // This is slower since they are checked every time, even if data is same since previous call.
87 // This is slower since they are checked every time, even if data is same since previous call.
98 qreal min = INT_MAX;
88 qreal min = INT_MAX;
99
89
100 for (int i=0; i <mDataModel.count(); i++) {
90 for (int i=0; i <mDataModel.count(); i++) {
101 int itemCount = mDataModel.at(i)->count();
91 int itemCount = mDataModel.at(i)->count();
102 for (int j=0; j<itemCount; j++) {
92 for (int j=0; j<itemCount; j++) {
103 qreal temp = mDataModel.at(i)->valueAt(j);
93 qreal temp = mDataModel.at(i)->valueAt(j);
104 if (temp < min) {
94 if (temp < min) {
105 min = temp;
95 min = temp;
106 }
96 }
107 }
97 }
108 }
98 }
109 return min;
99 return min;
110 }
100 }
111
101
112 qreal BarChartModel::max()
102 qreal BarChartModel::max()
113 {
103 {
114 Q_ASSERT(mDataModel.count() > 0);
104 Q_ASSERT(mDataModel.count() > 0);
115
105
116 // TODO: make min and max members and update them when data changes.
106 // TODO: make min and max members and update them when data changes.
117 // This is slower since they are checked every time, even if data is same since previous call.
107 // This is slower since they are checked every time, even if data is same since previous call.
118 qreal max = INT_MIN;
108 qreal max = INT_MIN;
119
109
120 for (int i=0; i <mDataModel.count(); i++) {
110 for (int i=0; i <mDataModel.count(); i++) {
121 int itemCount = mDataModel.at(i)->count();
111 int itemCount = mDataModel.at(i)->count();
122 for (int j=0; j<itemCount; j++) {
112 for (int j=0; j<itemCount; j++) {
123 qreal temp = mDataModel.at(i)->valueAt(j);
113 qreal temp = mDataModel.at(i)->valueAt(j);
124 if (temp > max) {
114 if (temp > max) {
125 max = temp;
115 max = temp;
126 }
116 }
127 }
117 }
128 }
118 }
129
119
130 return max;
120 return max;
131 }
121 }
132
122
133 qreal BarChartModel::valueAt(int set, int category)
123 qreal BarChartModel::valueAt(int set, int category)
134 {
124 {
135 if ((set < 0) || (set >= mDataModel.count())) {
125 if ((set < 0) || (set >= mDataModel.count())) {
136 // No set, no value.
126 // No set, no value.
137 return 0;
127 return 0;
138 } else if ((category < 0) || (category >= mDataModel.at(set)->count())) {
128 } else if ((category < 0) || (category >= mDataModel.at(set)->count())) {
139 // No category, no value.
129 // No category, no value.
140 return 0;
130 return 0;
141 }
131 }
142
132
143 return mDataModel.at(set)->valueAt(category);
133 return mDataModel.at(set)->valueAt(category);
144 }
134 }
145
135
146 qreal BarChartModel::percentageAt(int set, int category)
136 qreal BarChartModel::percentageAt(int set, int category)
147 {
137 {
148 if ((set < 0) || (set >= mDataModel.count())) {
138 if ((set < 0) || (set >= mDataModel.count())) {
149 // No set, no value.
139 // No set, no value.
150 return 0;
140 return 0;
151 } else if ((category < 0) || (category >= mDataModel.at(set)->count())) {
141 } else if ((category < 0) || (category >= mDataModel.at(set)->count())) {
152 // No category, no value.
142 // No category, no value.
153 return 0;
143 return 0;
154 }
144 }
155
145
156 qreal value = mDataModel.at(set)->valueAt(category);
146 qreal value = mDataModel.at(set)->valueAt(category);
157 qreal total = categorySum(category);
147 qreal total = categorySum(category);
158 if (0 == total) {
148 if (0 == total) {
159 return 100.0;
149 return 100.0;
160 }
150 }
161
151
162 return value / total;
152 return value / total;
163 }
153 }
164
154
165
155
166 qreal BarChartModel::categorySum(int category)
156 qreal BarChartModel::categorySum(int category)
167 {
157 {
168 qreal sum(0);
158 qreal sum(0);
169 int count = mDataModel.count(); // Count sets
159 int count = mDataModel.count(); // Count sets
170
160
171 for (int set = 0; set < count; set++) {
161 for (int set = 0; set < count; set++) {
172 if (category < mDataModel.at(set)->count()) {
162 if (category < mDataModel.at(set)->count()) {
173 sum += mDataModel.at(set)->valueAt(category);
163 sum += mDataModel.at(set)->valueAt(category);
174 }
164 }
175 }
165 }
176 return sum;
166 return sum;
177 }
167 }
178
168
179 qreal BarChartModel::maxCategorySum()
169 qreal BarChartModel::maxCategorySum()
180 {
170 {
181 qreal max = INT_MIN;
171 qreal max = INT_MIN;
182 int count = countCategories();
172 int count = countCategories();
183
173
184 for (int col=0; col<count; col++) {
174 for (int col=0; col<count; col++) {
185 qreal sum = categorySum(col);
175 qreal sum = categorySum(col);
186 if (sum > max) {
176 if (sum > max) {
187 max = sum;
177 max = sum;
188 }
178 }
189 }
179 }
190 return max;
180 return max;
191 }
181 }
192
182
193 QString BarChartModel::label(int category)
183 QString BarChartModel::label(int category)
194 {
184 {
195 return mCategory->label(category);
185 return mCategory->label(category);
196 }
186 }
197
187
198 #include "moc_barchartmodel_p.cpp"
188 #include "moc_barchartmodel_p.cpp"
199
189
200 QTCOMMERCIALCHART_END_NAMESPACE
190 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,59 +1,60
1 #ifndef BARCHARTMODEL_H
1 #ifndef BARCHARTMODEL_H
2 #define BARCHARTMODEL_H
2 #define BARCHARTMODEL_H
3
3
4 #include <QObject>
4 #include <QObject>
5 #include "qchartglobal.h"
5 #include "qchartglobal.h"
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 // Model for bar chart. Internal class.
9 // Model for bar chart. Internal class.
10 // TODO: Implement as QAbstractItemModel?
10 // TODO: Implement as QAbstractItemModel?
11
11
12 class QBarSet;
12 class QBarSet;
13 class QBarCategory;
13 class QBarCategory;
14
14
15 class BarChartModel : public QObject //, public QAbstractItemModel
15 class BarChartModel : public QObject //, public QAbstractItemModel
16 {
16 {
17 Q_OBJECT
17 Q_OBJECT
18 public:
18 public:
19 explicit BarChartModel(QBarCategory *category, QObject *parent = 0);
19 explicit BarChartModel(QBarCategory *category, QObject *parent = 0);
20 ~BarChartModel();
20 ~BarChartModel();
21
21
22 QBarCategory& category();
22 QBarCategory& category();
23 void addBarSet(QBarSet *set);
23 void addBarSet(QBarSet *set);
24 void removeBarSet(QBarSet *set);
24 void removeBarSet(QBarSet *set);
25 QBarSet* nextSet(bool getFirst);
26 QBarSet *setAt(int index);
25 QBarSet *setAt(int index);
26 QList<QBarSet*> barSets();
27
27 QList<QString> legend();
28 QList<QString> legend();
28
29
29 int countSets(); // Number of sets in model
30 int countSets(); // Number of sets in model
30 int countCategories(); // Number of categories
31 int countCategories(); // Number of categories
31 int countTotalItems(); // Total items in all sets. Includes empty items.
32 int countTotalItems(); // Total items in all sets. Includes empty items.
32
33
33 qreal max(); // Maximum value of all sets
34 qreal max(); // Maximum value of all sets
34 qreal min(); // Minimum value of all sets
35 qreal min(); // Minimum value of all sets
35 qreal valueAt(int set, int category);
36 qreal valueAt(int set, int category);
36 qreal percentageAt(int set, int category);
37 qreal percentageAt(int set, int category);
37
38
38 qreal categorySum(int category);
39 qreal categorySum(int category);
39 qreal maxCategorySum(); // returns maximum sum of sets in all categories.
40 qreal maxCategorySum(); // returns maximum sum of sets in all categories.
40
41
41 QString label(int category);
42 QString label(int category);
42
43
43 signals:
44 signals:
44 void modelUpdated();
45 void modelUpdated();
45
46
46 public slots:
47 public slots:
47
48
48 private:
49 private:
49
50
50 QList<QBarSet*> mDataModel;
51 QList<QBarSet*> mDataModel;
51 QBarCategory* mCategory; // Owned
52 QBarCategory* mCategory; // Owned
52
53
53 int mCurrentSet;
54 int mCurrentSet;
54
55
55 };
56 };
56
57
57 QTCOMMERCIALCHART_END_NAMESPACE
58 QTCOMMERCIALCHART_END_NAMESPACE
58
59
59 #endif // BARCHARTMODEL_H
60 #endif // BARCHARTMODEL_H
@@ -1,97 +1,97
1 #include "barpresenter.h"
1 #include "barpresenter.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barlabel_p.h"
3 #include "barlabel_p.h"
4 #include "barvalue_p.h"
4 #include "barvalue_p.h"
5 #include "qbarset.h"
5 #include "qbarset.h"
6 #include <QDebug>
6 #include <QDebug>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 BarPresenter::BarPresenter(QBarSeries *series, QGraphicsItem *parent) :
10 BarPresenter::BarPresenter(QBarSeries *series, QGraphicsItem *parent) :
11 BarPresenterBase(series, parent)
11 BarPresenterBase(series, parent)
12 {
12 {
13 mBarDefaultWidth = 15;
13 mBarDefaultWidth = 15;
14 }
14 }
15
15
16 void BarPresenter::layoutChanged()
16 void BarPresenter::layoutChanged()
17 {
17 {
18 // Scale bars to new layout
18 // Scale bars to new layout
19 // Layout for bars:
19 // Layout for bars:
20 if (mSeries->countSets() <= 0) {
20 if (mSeries->countSets() <= 0) {
21 qDebug() << "No sets in model!";
21 qDebug() << "No sets in model!";
22 return;
22 return;
23 }
23 }
24
24
25 if (childItems().count() == 0) {
25 if (childItems().count() == 0) {
26 qDebug() << "WARNING: BarPresenter::layoutChanged called before graphics items are created!";
26 qDebug() << "WARNING: BarPresenter::layoutChanged called before graphics items are created!";
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 int categoryCount = mSeries->countCategories();
32 int categoryCount = mSeries->countCategories();
33 int setCount = mSeries->countSets();
33 int setCount = mSeries->countSets();
34
34
35 qreal tW = mWidth;
35 qreal tW = mWidth;
36 qreal tH = mHeight;
36 qreal tH = mHeight;
37 qreal tM = mSeries->max();
37 qreal tM = mSeries->max();
38 qreal scale = (tH/tM);
38 qreal scale = (tH/tM);
39 qreal tC = categoryCount+1;
39 qreal tC = categoryCount+1;
40 qreal xStepPerSet = (tW/tC);
40 qreal xStepPerSet = (tW/tC);
41
41
42 // Scaling.
42 // Scaling.
43 int itemIndex(0);
43 int itemIndex(0);
44 int labelIndex(0);
44 int labelIndex(0);
45
45
46 for (int category=0; category < categoryCount; category++) {
46 for (int category=0; category < categoryCount; category++) {
47 qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
47 qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
48 qreal yPos = mHeight;
48 qreal yPos = mHeight;
49 for (int set = 0; set < setCount; set++) {
49 for (int set = 0; set < setCount; set++) {
50 qreal barHeight = mSeries->valueAt(set,category) * scale;
50 qreal barHeight = mSeries->valueAt(set,category) * scale;
51 Bar* bar = mBars.at(itemIndex);
51 Bar* bar = mBars.at(itemIndex);
52
52
53 // TODO: width settable per bar?
53 // TODO: width settable per bar?
54 bar->resize(mBarDefaultWidth, barHeight);
54 bar->resize(mBarDefaultWidth, barHeight);
55 bar->setBrush(mSeries->setAt(set)->brush());
55 bar->setBrush(mSeries->barsetAt(set)->brush());
56 bar->setPos(xPos, yPos-barHeight);
56 bar->setPos(xPos, yPos-barHeight);
57 itemIndex++;
57 itemIndex++;
58 xPos += mBarDefaultWidth;
58 xPos += mBarDefaultWidth;
59 }
59 }
60
60
61 // TODO: Layout for labels, remove magic number
61 // TODO: Layout for labels, remove magic number
62 xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
62 xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
63 BarLabel* label = mLabels.at(labelIndex);
63 BarLabel* label = mLabels.at(labelIndex);
64 label->setPos(xPos, mHeight + 20);
64 label->setPos(xPos, mHeight + 20);
65 labelIndex++;
65 labelIndex++;
66 }
66 }
67
67
68 // Position floating values
68 // Position floating values
69 itemIndex = 0;
69 itemIndex = 0;
70 for (int category=0; category < mSeries->countCategories(); category++) {
70 for (int category=0; category < mSeries->countCategories(); category++) {
71 qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
71 qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
72 qreal yPos = mHeight;
72 qreal yPos = mHeight;
73 for (int set=0; set < mSeries->countSets(); set++) {
73 for (int set=0; set < mSeries->countSets(); set++) {
74 qreal barHeight = mSeries->valueAt(set,category) * scale;
74 qreal barHeight = mSeries->valueAt(set,category) * scale;
75 BarValue* value = mFloatingValues.at(itemIndex);
75 BarValue* value = mFloatingValues.at(itemIndex);
76
76
77 // TODO: remove hard coding, apply layout
77 // TODO: remove hard coding, apply layout
78 value->resize(100,50);
78 value->resize(100,50);
79 value->setPos(xPos, yPos-barHeight/2);
79 value->setPos(xPos, yPos-barHeight/2);
80 value->setPen(QPen(QColor(255,255,255,255)));
80 value->setPen(QPen(QColor(255,255,255,255)));
81
81
82 if (mSeries->valueAt(set,category) != 0) {
82 if (mSeries->valueAt(set,category) != 0) {
83 value->setValueString(QString::number(mSeries->valueAt(set,category)));
83 value->setValueString(QString::number(mSeries->valueAt(set,category)));
84 } else {
84 } else {
85 value->setValueString(QString(""));
85 value->setValueString(QString(""));
86 }
86 }
87
87
88 itemIndex++;
88 itemIndex++;
89 xPos += mBarDefaultWidth;
89 xPos += mBarDefaultWidth;
90 }
90 }
91 }
91 }
92 mLayoutDirty = true;
92 mLayoutDirty = true;
93 }
93 }
94
94
95 #include "moc_barpresenter.cpp"
95 #include "moc_barpresenter.cpp"
96
96
97 QTCOMMERCIALCHART_END_NAMESPACE
97 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,158 +1,158
1 #include "barpresenterbase.h"
1 #include "barpresenterbase.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barvalue_p.h"
3 #include "barvalue_p.h"
4 #include "barlabel_p.h"
4 #include "barlabel_p.h"
5 #include "separator_p.h"
5 #include "separator_p.h"
6 #include "qbarset.h"
6 #include "qbarset.h"
7 #include "qbarseries.h"
7 #include "qbarseries.h"
8 #include <QDebug>
8 #include <QDebug>
9 #include <QToolTip>
9 #include <QToolTip>
10
10
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
12
13 BarPresenterBase::BarPresenterBase(QBarSeries *series, QGraphicsItem *parent)
13 BarPresenterBase::BarPresenterBase(QBarSeries *series, QGraphicsItem *parent)
14 : ChartItem(parent)
14 : ChartItem(parent)
15 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
15 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
16 ,mLayoutSet(false)
16 ,mLayoutSet(false)
17 ,mLayoutDirty(true)
17 ,mLayoutDirty(true)
18 ,mSeparatorsEnabled(false)
18 ,mSeparatorsEnabled(false)
19 ,mSeries(series)
19 ,mSeries(series)
20 {
20 {
21 connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString)));
21 connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString)));
22 connect(series,SIGNAL(separatorsEnabled(bool)),this,SLOT(enableSeparators(bool)));
22 connect(series,SIGNAL(separatorsEnabled(bool)),this,SLOT(enableSeparators(bool)));
23 dataChanged();
23 dataChanged();
24 }
24 }
25
25
26 BarPresenterBase::~BarPresenterBase()
26 BarPresenterBase::~BarPresenterBase()
27 {
27 {
28 disconnect(this,SLOT(showToolTip(QPoint,QString)));
28 disconnect(this,SLOT(showToolTip(QPoint,QString)));
29 delete mSeries;
29 delete mSeries;
30 }
30 }
31
31
32 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
32 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
33 {
33 {
34 if (!mLayoutSet) {
34 if (!mLayoutSet) {
35 qDebug() << "BarPresenterBase::paint called without layout set. Aborting.";
35 qDebug() << "BarPresenterBase::paint called without layout set. Aborting.";
36 return;
36 return;
37 }
37 }
38 // if (mLayoutDirty) {
38 // if (mLayoutDirty) {
39 // Layout or data has changed. Need to redraw.
39 // Layout or data has changed. Need to redraw.
40 foreach(QGraphicsItem* i, childItems()) {
40 foreach(QGraphicsItem* i, childItems()) {
41 i->paint(painter,option,widget);
41 i->paint(painter,option,widget);
42 }
42 }
43 // }
43 // }
44 }
44 }
45
45
46 QRectF BarPresenterBase::boundingRect() const
46 QRectF BarPresenterBase::boundingRect() const
47 {
47 {
48 return QRectF(0,0,mWidth,mHeight);
48 return QRectF(0,0,mWidth,mHeight);
49 }
49 }
50
50
51 void BarPresenterBase::setBarWidth( int w )
51 void BarPresenterBase::setBarWidth( int w )
52 {
52 {
53 mBarDefaultWidth = w;
53 mBarDefaultWidth = w;
54 }
54 }
55
55
56 void BarPresenterBase::dataChanged()
56 void BarPresenterBase::dataChanged()
57 {
57 {
58 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
58 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
59 qDebug() << "datachanged";
59 qDebug() << "datachanged";
60 // Delete old bars
60 // Delete old bars
61 foreach (QGraphicsItem* item, childItems()) {
61 foreach (QGraphicsItem* item, childItems()) {
62 delete item;
62 delete item;
63 }
63 }
64
64
65 mBars.clear();
65 mBars.clear();
66 mLabels.clear();
66 mLabels.clear();
67 mSeparators.clear();
67 mSeparators.clear();
68 mFloatingValues.clear();
68 mFloatingValues.clear();
69
69
70 // Create new graphic items for bars
70 // Create new graphic items for bars
71 for (int c=0; c<mSeries->countCategories(); c++) {
71 for (int c=0; c<mSeries->countCategories(); c++) {
72 for (int s=0; s<mSeries->countSets(); s++) {
72 for (int s=0; s<mSeries->countSets(); s++) {
73 QBarSet *set = mSeries->setAt(s);
73 QBarSet *set = mSeries->barsetAt(s);
74 Bar *bar = new Bar(this);
74 Bar *bar = new Bar(this);
75 childItems().append(bar);
75 childItems().append(bar);
76 mBars.append(bar);
76 mBars.append(bar);
77 connect(bar,SIGNAL(clicked()),set,SLOT(barClicked()));
77 connect(bar,SIGNAL(clicked()),set,SLOT(barClicked()));
78 connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEntered(QPoint)));
78 connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEntered(QPoint)));
79 connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaved()));
79 connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaved()));
80 }
80 }
81 }
81 }
82
82
83 // Create labels
83 // Create labels
84 int count = mSeries->countCategories();
84 int count = mSeries->countCategories();
85 for (int i=0; i<count; i++) {
85 for (int i=0; i<count; i++) {
86 BarLabel* label = new BarLabel(this);
86 BarLabel* label = new BarLabel(this);
87 label->set(mSeries->label(i));
87 label->set(mSeries->label(i));
88 childItems().append(label);
88 childItems().append(label);
89 mLabels.append(label);
89 mLabels.append(label);
90 }
90 }
91
91
92 // Create separators
92 // Create separators
93 count = mSeries->countCategories() - 1; // There is one less separator than columns
93 count = mSeries->countCategories() - 1; // There is one less separator than columns
94 for (int i=0; i<count; i++) {
94 for (int i=0; i<count; i++) {
95 Separator* sep = new Separator(this);
95 Separator* sep = new Separator(this);
96 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
97 sep->setVisible(mSeparatorsEnabled);
97 sep->setVisible(mSeparatorsEnabled);
98 childItems().append(sep);
98 childItems().append(sep);
99 mSeparators.append(sep);
99 mSeparators.append(sep);
100 }
100 }
101
101
102 // Create floating values
102 // Create floating values
103 for (int category=0; category<mSeries->countCategories(); category++) {
103 for (int category=0; category<mSeries->countCategories(); category++) {
104 for (int s=0; s<mSeries->countSets(); s++) {
104 for (int s=0; s<mSeries->countSets(); s++) {
105 QBarSet *set = mSeries->setAt(s);
105 QBarSet *set = mSeries->barsetAt(s);
106 BarValue *value = new BarValue(*set, this);
106 BarValue *value = new BarValue(*set, this);
107 childItems().append(value);
107 childItems().append(value);
108 mFloatingValues.append(value);
108 mFloatingValues.append(value);
109 connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible()));
109 connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible()));
110 }
110 }
111 }
111 }
112
112
113 // TODO: if (autolayout) { layoutChanged() } or something
113 // TODO: if (autolayout) { layoutChanged() } or something
114 mLayoutDirty = true;
114 mLayoutDirty = true;
115 }
115 }
116
116
117 //handlers
117 //handlers
118
118
119 void BarPresenterBase::handleModelChanged(int index)
119 void BarPresenterBase::handleModelChanged(int index)
120 {
120 {
121 // qDebug() << "BarPresenterBase::handleModelChanged" << index;
121 // qDebug() << "BarPresenterBase::handleModelChanged" << index;
122 dataChanged();
122 dataChanged();
123 }
123 }
124
124
125 void BarPresenterBase::handleDomainChanged(const Domain& domain)
125 void BarPresenterBase::handleDomainChanged(const Domain& domain)
126 {
126 {
127 // qDebug() << "BarPresenterBase::handleDomainChanged";
127 // qDebug() << "BarPresenterBase::handleDomainChanged";
128 // TODO: Figure out the use case for this.
128 // TODO: Figure out the use case for this.
129 // Affects the size of visible item, so layout is changed.
129 // Affects the size of visible item, so layout is changed.
130 // layoutChanged();
130 // layoutChanged();
131 }
131 }
132
132
133 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
133 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
134 {
134 {
135 mWidth = rect.width();
135 mWidth = rect.width();
136 mHeight = rect.height();
136 mHeight = rect.height();
137 layoutChanged();
137 layoutChanged();
138 mLayoutSet = true;
138 mLayoutSet = true;
139 setPos(rect.topLeft());
139 setPos(rect.topLeft());
140 }
140 }
141
141
142 void BarPresenterBase::showToolTip(QPoint pos, QString tip)
142 void BarPresenterBase::showToolTip(QPoint pos, QString tip)
143 {
143 {
144 // TODO: cool tooltip instead of default
144 // TODO: cool tooltip instead of default
145 QToolTip::showText(pos,tip);
145 QToolTip::showText(pos,tip);
146 }
146 }
147
147
148 void BarPresenterBase::enableSeparators(bool enabled)
148 void BarPresenterBase::enableSeparators(bool enabled)
149 {
149 {
150 for (int i=0; i<mSeparators.count(); i++) {
150 for (int i=0; i<mSeparators.count(); i++) {
151 mSeparators.at(i)->setVisible(enabled);
151 mSeparators.at(i)->setVisible(enabled);
152 }
152 }
153 mSeparatorsEnabled = enabled;
153 mSeparatorsEnabled = enabled;
154 }
154 }
155
155
156 #include "moc_barpresenterbase.cpp"
156 #include "moc_barpresenterbase.cpp"
157
157
158 QTCOMMERCIALCHART_END_NAMESPACE
158 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,112 +1,112
1 #include "percentbarpresenter.h"
1 #include "percentbarpresenter.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barlabel_p.h"
3 #include "barlabel_p.h"
4 #include "barvalue_p.h"
4 #include "barvalue_p.h"
5 #include "separator_p.h"
5 #include "separator_p.h"
6 #include "qbarset.h"
6 #include "qbarset.h"
7 #include <QDebug>
7 #include <QDebug>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11
11
12 PercentBarPresenter::PercentBarPresenter(QBarSeries *series, QGraphicsItem *parent) :
12 PercentBarPresenter::PercentBarPresenter(QBarSeries *series, QGraphicsItem *parent) :
13 BarPresenterBase(series, parent)
13 BarPresenterBase(series, parent)
14 {
14 {
15 }
15 }
16
16
17 void PercentBarPresenter::layoutChanged()
17 void PercentBarPresenter::layoutChanged()
18 {
18 {
19 // Scale bars to new layout
19 // Scale bars to new layout
20 // Layout for bars:
20 // Layout for bars:
21 if (mSeries->countSets() <= 0) {
21 if (mSeries->countSets() <= 0) {
22 qDebug() << "No sets in model!";
22 qDebug() << "No sets in model!";
23 // Nothing to do.
23 // Nothing to do.
24 return;
24 return;
25 }
25 }
26
26
27 if (childItems().count() == 0) {
27 if (childItems().count() == 0) {
28 qDebug() << "WARNING: PercentBarPresenter::layoutChanged called before graphics items are created!";
28 qDebug() << "WARNING: PercentBarPresenter::layoutChanged called before graphics items are created!";
29 return;
29 return;
30 }
30 }
31
31
32 // TODO: better way to auto-layout
32 // TODO: better way to auto-layout
33 // Use reals for accurancy (we might get some compiler warnings... :)
33 // Use reals for accurancy (we might get some compiler warnings... :)
34 int count = mSeries->countCategories();
34 int count = mSeries->countCategories();
35 int itemIndex(0);
35 int itemIndex(0);
36 int labelIndex(0);
36 int labelIndex(0);
37 qreal tW = mWidth;
37 qreal tW = mWidth;
38 qreal tC = count+1;
38 qreal tC = count+1;
39 qreal xStep = (tW/tC);
39 qreal xStep = (tW/tC);
40 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
40 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
41 qreal h = mHeight;
41 qreal h = mHeight;
42
42
43 for (int category = 0; category < mSeries->countCategories(); category++) {
43 for (int category = 0; category < mSeries->countCategories(); category++) {
44 qreal colSum = mSeries->categorySum(category);
44 qreal colSum = mSeries->categorySum(category);
45 qreal scale = (h / colSum);
45 qreal scale = (h / colSum);
46 qreal yPos = h;
46 qreal yPos = h;
47 for (int set=0; set < mSeries->countSets(); set++) {
47 for (int set=0; set < mSeries->countSets(); set++) {
48 qreal barHeight = mSeries->valueAt(set, category) * scale;
48 qreal barHeight = mSeries->valueAt(set, category) * scale;
49 Bar* bar = mBars.at(itemIndex);
49 Bar* bar = mBars.at(itemIndex);
50
50
51 // TODO: width settable per bar?
51 // TODO: width settable per bar?
52 bar->resize(mBarDefaultWidth, barHeight);
52 bar->resize(mBarDefaultWidth, barHeight);
53 bar->setBrush(mSeries->setAt(set)->brush());
53 bar->setBrush(mSeries->barsetAt(set)->brush());
54 bar->setPos(xPos, yPos-barHeight);
54 bar->setPos(xPos, yPos-barHeight);
55 itemIndex++;
55 itemIndex++;
56 yPos -= barHeight;
56 yPos -= barHeight;
57 }
57 }
58
58
59 // TODO: Layout for labels, remove magic number
59 // TODO: Layout for labels, remove magic number
60 BarLabel* label = mLabels.at(labelIndex);
60 BarLabel* label = mLabels.at(labelIndex);
61 label->setPos(xPos, mHeight + 20);
61 label->setPos(xPos, mHeight + 20);
62 labelIndex++;
62 labelIndex++;
63 xPos += xStep;
63 xPos += xStep;
64 }
64 }
65
65
66 // Position separators
66 // Position separators
67 xPos = xStep + xStep/2;
67 xPos = xStep + xStep/2;
68 for (int s=0; s < mSeries->countCategories() - 1; s++) {
68 for (int s=0; s < mSeries->countCategories() - 1; s++) {
69 Separator* sep = mSeparators.at(s);
69 Separator* sep = mSeparators.at(s);
70 sep->setPos(xPos,0);
70 sep->setPos(xPos,0);
71 sep->setSize(QSizeF(1,mHeight));
71 sep->setSize(QSizeF(1,mHeight));
72 xPos += xStep;
72 xPos += xStep;
73 }
73 }
74
74
75 // Position floating values
75 // Position floating values
76 itemIndex = 0;
76 itemIndex = 0;
77 xPos = ((tW/tC) - mBarDefaultWidth / 2);
77 xPos = ((tW/tC) - mBarDefaultWidth / 2);
78 for (int category=0; category < mSeries->countCategories(); category++) {
78 for (int category=0; category < mSeries->countCategories(); category++) {
79 qreal yPos = h;
79 qreal yPos = h;
80 qreal colSum = mSeries->categorySum(category);
80 qreal colSum = mSeries->categorySum(category);
81 qreal scale = (h / colSum);
81 qreal scale = (h / colSum);
82 for (int set=0; set < mSeries->countSets(); set++) {
82 for (int set=0; set < mSeries->countSets(); set++) {
83 qreal barHeight = mSeries->valueAt(set,category) * scale;
83 qreal barHeight = mSeries->valueAt(set,category) * scale;
84 BarValue* value = mFloatingValues.at(itemIndex);
84 BarValue* value = mFloatingValues.at(itemIndex);
85
85
86 // TODO: remove hard coding, apply layout
86 // TODO: remove hard coding, apply layout
87 value->resize(100,50);
87 value->resize(100,50);
88 value->setPos(xPos, yPos-barHeight/2);
88 value->setPos(xPos, yPos-barHeight/2);
89 value->setPen(QPen(QColor(255,255,255,255)));
89 value->setPen(QPen(QColor(255,255,255,255)));
90
90
91 if (mSeries->valueAt(set,category) != 0) {
91 if (mSeries->valueAt(set,category) != 0) {
92 int p = mSeries->percentageAt(set,category) * 100;
92 int p = mSeries->percentageAt(set,category) * 100;
93 QString vString(QString::number(p));
93 QString vString(QString::number(p));
94 vString.truncate(3);
94 vString.truncate(3);
95 vString.append("%");
95 vString.append("%");
96 value->setValueString(vString);
96 value->setValueString(vString);
97 } else {
97 } else {
98 value->setValueString(QString(""));
98 value->setValueString(QString(""));
99 }
99 }
100
100
101 itemIndex++;
101 itemIndex++;
102 yPos -= barHeight;
102 yPos -= barHeight;
103 }
103 }
104 xPos += xStep;
104 xPos += xStep;
105 }
105 }
106
106
107 mLayoutDirty = true;
107 mLayoutDirty = true;
108 }
108 }
109
109
110 #include "moc_percentbarpresenter.cpp"
110 #include "moc_percentbarpresenter.cpp"
111
111
112 QTCOMMERCIALCHART_END_NAMESPACE
112 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,252 +1,232
1 #include <QDebug>
1 #include <QDebug>
2 #include "qbarseries.h"
2 #include "qbarseries.h"
3 #include "qbarcategory.h"
3 #include "qbarcategory.h"
4 #include "qbarset.h"
4 #include "qbarset.h"
5 #include "barchartmodel_p.h"
5 #include "barchartmodel_p.h"
6
6
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 /*!
10 /*!
11 \class QBarSeries
11 \class QBarSeries
12 \brief part of QtCommercial chart API.
12 \brief part of QtCommercial chart API.
13
13
14 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multible
14 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multible
15 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
15 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
16 by QBarCategory class.
16 by QBarCategory class.
17
17
18 \mainclass
18 \mainclass
19
19
20 Example on how to create category:
21 \snippet ../example/barchart/main.cpp 1
22
23 Example on how to create sets of data:
24 \snippet ../example/barchart/main.cpp 2
25
26 Example on how to add sets to bar chart:
27 \snippet ../example/barchart/main.cpp 3
28
29 Example on how to enable tooltip and floating values:
30 \snippet ../example/barchart/main.cpp 4
31
32 Example on how to create view and apply theme:
33 \snippet ../example/barchart/main.cpp 5
34
35 \sa QBarCategory, QBarSet, QStackedBarSeries, QPercentBarSeries
20 \sa QBarCategory, QBarSet, QStackedBarSeries, QPercentBarSeries
36 */
21 */
37
22
38 /*!
23 /*!
39 \fn virtual QChartSeriesType QBarSeries::type() const
24 \fn virtual QChartSeriesType QBarSeries::type() const
40 \brief Returns type of series.
25 \brief Returns type of series.
41 \sa QChartSeries, QChartSeriesType
26 \sa QChartSeries, QChartSeriesType
42 */
27 */
43 /*!
28 /*!
44 \fn void QBarSeries::changed(int index)
29 \fn void QBarSeries::changed(int index)
45 \brief \internal \a index
30 \brief \internal \a index
46 */
31 */
47 /*!
32 /*!
48 \fn void QBarSeries::floatingValuesEnabled(bool enabled)
33 \fn void QBarSeries::floatingValuesEnabled(bool enabled)
49 \brief \internal \a enabled
34 \brief \internal \a enabled
50 */
35 */
51 /*!
36 /*!
52 \fn void QBarSeries::toolTipEnabled(bool enabled)
37 \fn void QBarSeries::toolTipEnabled(bool enabled)
53 \brief \internal \a enabled
38 \brief \internal \a enabled
54 */
39 */
55 /*!
40 /*!
56 \fn void QBarSeries::separatorsEnabled(bool enabled)
41 \fn void QBarSeries::separatorsEnabled(bool enabled)
57 \brief \internal \a enabled
42 \brief \internal \a enabled
58 */
43 */
59 /*!
44 /*!
60 \fn void QBarSeries::showToolTip(QPoint pos, QString tip)
45 \fn void QBarSeries::showToolTip(QPoint pos, QString tip)
61 \brief \internal \a pos \a tip
46 \brief \internal \a pos \a tip
62 */
47 */
63
48
64 /*!
49 /*!
65 Constructs empty QBarSeries. Parameter \a category defines the categories for chart.
50 Constructs empty QBarSeries. Parameter \a category defines the categories for chart.
66 Takes ownership of \a category.
51 Takes ownership of \a category.
67 QBarSeries is QObject which is a child of a \a parent.
52 QBarSeries is QObject which is a child of a \a parent.
68 */
53 */
69 QBarSeries::QBarSeries(QBarCategory *category, QObject *parent)
54 QBarSeries::QBarSeries(QBarCategory *category, QObject *parent)
70 : QChartSeries(parent)
55 : QChartSeries(parent)
71 ,mModel(new BarChartModel(category, this))
56 ,mModel(new BarChartModel(category, this))
72 {
57 {
73 }
58 }
74
59
75 /*!
60 /*!
76 Adds a set of bars to series. Takes ownership of \a set
61 Adds a set of bars to series. Takes ownership of \a set
77 */
62 */
78 void QBarSeries::addBarSet(QBarSet *set)
63 void QBarSeries::addBarSet(QBarSet *set)
79 {
64 {
80 mModel->addBarSet(set);
65 mModel->addBarSet(set);
81 }
66 }
82
67
83 /*!
68 /*!
84 Removes a set of bars from series. Releases ownership of \a set. Doesnt delete \a set.
69 Removes a set of bars from series. Releases ownership of \a set. Doesnt delete \a set.
85 */
70 */
86 void QBarSeries::removeBarSet(QBarSet *set)
71 void QBarSeries::removeBarSet(QBarSet *set)
87 {
72 {
88 mModel->removeBarSet(set);
73 mModel->removeBarSet(set);
89 }
74 }
90
75
91 /*!
76 /*!
92 Returns number of sets in series.
77 Returns number of sets in series.
93 */
78 */
94 int QBarSeries::countSets()
79 int QBarSeries::countSets()
95 {
80 {
96 return mModel->countSets();
81 return mModel->countSets();
97 }
82 }
98
83
99 /*!
84 /*!
100 Returns number of categories in series
85 Returns number of categories in series
101 */
86 */
102 int QBarSeries::countCategories()
87 int QBarSeries::countCategories()
103 {
88 {
104 return mModel->countCategories();
89 return mModel->countCategories();
105 }
90 }
106
91
107 /*!
92 /*!
108 Simple iterator for set. Returns pointer to next set in series.
93 Returns a list of sets in series. Keeps ownership of sets.
109 Returns first set, if parameter \a getFirst is true.
94 */
110 If series is empty, returns 0.
95 QList<QBarSet*> QBarSeries::barSets()
111 Returns 0 after last set.
112 */
113 QBarSet* QBarSeries::nextSet(bool getFirst)
114 {
96 {
115 return mModel->nextSet(getFirst);
97 return mModel->barSets();
116 }
98 }
117
99
118 /*!
100 /*!
119 Returns set indexed by \a index. Doesn't check for index bounds.
101 \internal \a index
120 Assumes that \a index is between 0 and number of sets. Use countSets() to get valid index bound.
121 \sa countSets()
122 */
102 */
123 QBarSet* QBarSeries::setAt(int index)
103 QBarSet* QBarSeries::barsetAt(int index)
124 {
104 {
125 return mModel->setAt(index);
105 return mModel->setAt(index);
126 }
106 }
127
107
128 /*!
108 /*!
129 Returns legend of series. Legend is a list of set names in series.
109 Returns legend of series. Legend is a list of set names in series.
130 */
110 */
131 QList<QString> QBarSeries::legend()
111 QList<QString> QBarSeries::legend()
132 {
112 {
133 return mModel->legend();
113 return mModel->legend();
134 }
114 }
135
115
136 /*!
116 /*!
137 \internal \a category
117 \internal \a category
138 */
118 */
139 QString QBarSeries::label(int category)
119 QString QBarSeries::label(int category)
140 {
120 {
141 return mModel->label(category);
121 return mModel->label(category);
142 }
122 }
143
123
144 /*!
124 /*!
145 Enables or disables floating values depending on parameter \a enabled.
125 Enables or disables floating values depending on parameter \a enabled.
146 Floating values are bar values, that are displayed on top of each bar.
126 Floating values are bar values, that are displayed on top of each bar.
147 Calling without parameter \a enabled, enables the floating values
127 Calling without parameter \a enabled, enables the floating values
148 */
128 */
149 void QBarSeries::enableFloatingValues(bool enabled)
129 void QBarSeries::setFloatingValuesEnabled(bool enabled)
150 {
130 {
151 if (enabled) {
131 if (enabled) {
152 for (int i=0; i<mModel->countSets(); i++) {
132 for (int i=0; i<mModel->countSets(); i++) {
153 QBarSet *set = mModel->setAt(i);
133 QBarSet *set = mModel->setAt(i);
154 connect(set,SIGNAL(clicked()),set,SIGNAL(toggleFloatingValues()));
134 connect(set,SIGNAL(clicked()),set,SIGNAL(toggleFloatingValues()));
155 }
135 }
156 } else {
136 } else {
157 for (int i=0; i<mModel->countSets(); i++) {
137 for (int i=0; i<mModel->countSets(); i++) {
158 QBarSet *set = mModel->setAt(i);
138 QBarSet *set = mModel->setAt(i);
159 disconnect(set,SIGNAL(clicked()),set,SIGNAL(toggleFloatingValues()));
139 disconnect(set,SIGNAL(clicked()),set,SIGNAL(toggleFloatingValues()));
160 }
140 }
161 }
141 }
162 }
142 }
163
143
164 /*!
144 /*!
165 Enables or disables tooltip depending on parameter \a enabled.
145 Enables or disables tooltip depending on parameter \a enabled.
166 Tooltip shows the name of set, when mouse is hovering on top of bar.
146 Tooltip shows the name of set, when mouse is hovering on top of bar.
167 Calling without parameter \a enabled, enables the tooltip
147 Calling without parameter \a enabled, enables the tooltip
168 */
148 */
169 void QBarSeries::enableToolTip(bool enabled)
149 void QBarSeries::setToolTipEnabled(bool enabled)
170 {
150 {
171 if (enabled) {
151 if (enabled) {
172 for (int i=0; i<mModel->countSets(); i++) {
152 for (int i=0; i<mModel->countSets(); i++) {
173 QBarSet *set = mModel->setAt(i);
153 QBarSet *set = mModel->setAt(i);
174 connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
154 connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
175 }
155 }
176 } else {
156 } else {
177 for (int i=0; i<mModel->countSets(); i++) {
157 for (int i=0; i<mModel->countSets(); i++) {
178 QBarSet *set = mModel->setAt(i);
158 QBarSet *set = mModel->setAt(i);
179 disconnect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
159 disconnect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
180 }
160 }
181 }
161 }
182 }
162 }
183
163
184 /*!
164 /*!
185 Enables or disables separators depending on parameter \a enabled.
165 Enables or disables separators depending on parameter \a enabled.
186 Separators are visual elements that are drawn between categories.
166 Separators are visual elements that are drawn between categories.
187 Calling without parameter \a enabled, enables the separators
167 Calling without parameter \a enabled, enables the separators
188 */
168 */
189 void QBarSeries::enableSeparators(bool enabled)
169 void QBarSeries::setSeparatorsEnabled(bool enabled)
190 {
170 {
191 emit separatorsEnabled(enabled);
171 emit separatorsEnabled(enabled);
192 }
172 }
193
173
194 /*!
174 /*!
195 \internal
175 \internal
196 */
176 */
197 qreal QBarSeries::min()
177 qreal QBarSeries::min()
198 {
178 {
199 return mModel->min();
179 return mModel->min();
200 }
180 }
201
181
202 /*!
182 /*!
203 \internal
183 \internal
204 */
184 */
205 qreal QBarSeries::max()
185 qreal QBarSeries::max()
206 {
186 {
207 return mModel->max();
187 return mModel->max();
208 }
188 }
209
189
210 /*!
190 /*!
211 \internal \a set \a category
191 \internal \a set \a category
212 */
192 */
213 qreal QBarSeries::valueAt(int set, int category)
193 qreal QBarSeries::valueAt(int set, int category)
214 {
194 {
215 return mModel->valueAt(set,category);
195 return mModel->valueAt(set,category);
216 }
196 }
217
197
218 /*!
198 /*!
219 \internal \a set \a category
199 \internal \a set \a category
220 */
200 */
221 qreal QBarSeries::percentageAt(int set, int category)
201 qreal QBarSeries::percentageAt(int set, int category)
222 {
202 {
223 return mModel->percentageAt(set,category);
203 return mModel->percentageAt(set,category);
224 }
204 }
225
205
226 /*!
206 /*!
227 \internal \a category
207 \internal \a category
228 */
208 */
229 qreal QBarSeries::categorySum(int category)
209 qreal QBarSeries::categorySum(int category)
230 {
210 {
231 return mModel->categorySum(category);
211 return mModel->categorySum(category);
232 }
212 }
233
213
234 /*!
214 /*!
235 \internal
215 \internal
236 */
216 */
237 qreal QBarSeries::maxCategorySum()
217 qreal QBarSeries::maxCategorySum()
238 {
218 {
239 return mModel->maxCategorySum();
219 return mModel->maxCategorySum();
240 }
220 }
241
221
242 /*!
222 /*!
243 \internal
223 \internal
244 */
224 */
245 BarChartModel& QBarSeries::model()
225 BarChartModel& QBarSeries::model()
246 {
226 {
247 return *mModel;
227 return *mModel;
248 }
228 }
249
229
250 #include "moc_qbarseries.cpp"
230 #include "moc_qbarseries.cpp"
251
231
252 QTCOMMERCIALCHART_END_NAMESPACE
232 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,66 +1,66
1 #ifndef BARSERIES_H
1 #ifndef BARSERIES_H
2 #define BARSERIES_H
2 #define BARSERIES_H
3
3
4 #include "qchartseries.h"
4 #include "qchartseries.h"
5
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
7
8 class QBarCategory;
8 class QBarCategory;
9 class QBarSet;
9 class QBarSet;
10 class BarChartModel;
10 class BarChartModel;
11
11
12 // Container for series
12 // Container for series
13 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QChartSeries
13 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QChartSeries
14 {
14 {
15 Q_OBJECT
15 Q_OBJECT
16 public:
16 public:
17 QBarSeries(QBarCategory *category, QObject* parent=0);
17 QBarSeries(QBarCategory *category, QObject* parent=0);
18
18
19 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; }
19 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; }
20
20
21 void addBarSet(QBarSet *set); // Takes ownership of set
21 void addBarSet(QBarSet *set); // Takes ownership of set
22 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
22 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
23 int countSets();
23 int countSets();
24 int countCategories();
24 int countCategories();
25 QBarSet* nextSet(bool getFirst=false); // Returns first set, if called with true
25 QList<QBarSet*> barSets();
26 QBarSet *setAt(int index);
27
26
28 QList<QString> legend(); // Returns legend of series (ie. names of all sets in series)
27 QList<QString> legend(); // Returns legend of series (ie. names of all sets in series)
29
28
30 public:
29 public:
31 // TODO: Functions below this are not part of api and will be moved
30 // TODO: Functions below this are not part of api and will be moved
32 // to private implementation, when we start using it
31 // to private implementation, when we start using it
33 // TODO: TO PIMPL --->
32 // TODO: TO PIMPL --->
33 QBarSet *barsetAt(int index);
34 QString label(int category);
34 QString label(int category);
35 qreal min();
35 qreal min();
36 qreal max();
36 qreal max();
37 qreal valueAt(int set, int category);
37 qreal valueAt(int set, int category);
38 qreal percentageAt(int set, int category);
38 qreal percentageAt(int set, int category);
39 qreal categorySum(int category);
39 qreal categorySum(int category);
40 qreal maxCategorySum();
40 qreal maxCategorySum();
41 BarChartModel& model();
41 BarChartModel& model();
42 // <--- TO PIMPL
42 // <--- TO PIMPL
43
43
44 signals:
44 signals:
45 void changed(int index);
45 void changed(int index);
46
46
47 // TODO: internal signals, these to private implementation.
47 // TODO: internal signals, these to private implementation.
48 // TODO: TO PIMPL --->
48 // TODO: TO PIMPL --->
49 void floatingValuesEnabled(bool enabled);
49 void floatingValuesEnabled(bool enabled);
50 void toolTipEnabled(bool enabled);
50 void toolTipEnabled(bool enabled);
51 void separatorsEnabled(bool enabled);
51 void separatorsEnabled(bool enabled);
52 void showToolTip(QPoint pos, QString tip);
52 void showToolTip(QPoint pos, QString tip);
53 // <--- TO PIMPL
53 // <--- TO PIMPL
54
54
55 public Q_SLOTS:
55 public Q_SLOTS:
56 void enableFloatingValues(bool enabled=true); // enables floating values on top of bars
56 void setFloatingValuesEnabled(bool enabled=true); // enables floating values on top of bars
57 void enableToolTip(bool enabled=true); // enables tooltips
57 void setToolTipEnabled(bool enabled=true); // enables tooltips
58 void enableSeparators(bool enabled=true); // enables separators between categories
58 void setSeparatorsEnabled(bool enabled=true); // enables separators between categories
59
59
60 protected:
60 protected:
61 BarChartModel* mModel;
61 BarChartModel* mModel;
62 };
62 };
63
63
64 QTCOMMERCIALCHART_END_NAMESPACE
64 QTCOMMERCIALCHART_END_NAMESPACE
65
65
66 #endif // BARSERIES_H
66 #endif // BARSERIES_H
@@ -1,170 +1,170
1 #include "qbarset.h"
1 #include "qbarset.h"
2 #include <QDebug>
2 #include <QDebug>
3 #include <QToolTip>
3 #include <QToolTip>
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 /*!
7 /*!
8 \class QBarSet
8 \class QBarSet
9 \brief part of QtCommercial chart API.
9 \brief part of QtCommercial chart API.
10
10
11 QBarSet represents one set of bars. Set of bars contains one data value for each category.
11 QBarSet represents one set of bars. Set of bars contains one data value for each category.
12 First value of set is assumed to belong to first category, second to second category and so on.
12 First value of set is assumed to belong to first category, second to second category and so on.
13 If set has fewer values than there are categories, then the missing values are assumed to be
13 If set has fewer values than there are categories, then the missing values are assumed to be
14 at the end of set. For missing values in middle of a set, numerical value of zero is used.
14 at the end of set. For missing values in middle of a set, numerical value of zero is used.
15
15
16 \mainclass
16 \mainclass
17
17
18 Example on how to create sets of data:
18 Example on how to create sets of data:
19 \snippet ../example/barchart/main.cpp 2
19 \snippet ../example/barchart/main.cpp 2
20
20
21 \sa QBarCategory, QBarSeries, QStackedBarSeries, QPercentBarSeries
21 \sa QBarCategory, QBarSeries, QStackedBarSeries, QPercentBarSeries
22 */
22 */
23
23
24 /*!
24 /*!
25 \fn void QBarSet::clicked()
25 \fn void QBarSet::clicked()
26 \brief signals that set has been clicked
26 \brief signals that set has been clicked
27 */
27 */
28 /*!
28 /*!
29 \fn void QBarSet::hoverEnter(QPoint pos)
29 \fn void QBarSet::hoverEnter(QPoint pos)
30 \brief signals that mouse has entered over the set at position \a pos.
30 \brief signals that mouse has entered over the set at position \a pos.
31 */
31 */
32 /*!
32 /*!
33 \fn void QBarSet::hoverLeave()
33 \fn void QBarSet::hoverLeave()
34 \brief signals that mouse has left from the set.
34 \brief signals that mouse has left from the set.
35 */
35 */
36 /*!
36 /*!
37 \fn void QBarSet::toggleFloatingValues()
37 \fn void QBarSet::toggleFloatingValues()
38 \brief \internal
38 \brief \internal
39 */
39 */
40 /*!
40 /*!
41 \fn void QBarSet::showToolTip(QPoint pos, QString tip)
41 \fn void QBarSet::showToolTip(QPoint pos, QString tip)
42 \brief \internal \a pos \a tip
42 \brief \internal \a pos \a tip
43 */
43 */
44
44
45
45
46 /*!
46 /*!
47 Constructs QBarSet with a name of \a name and with parent of \a parent
47 Constructs QBarSet with a name of \a name and with parent of \a parent
48 */
48 */
49 QBarSet::QBarSet(QString name, QObject *parent)
49 QBarSet::QBarSet(QString name, QObject *parent)
50 : QObject(parent)
50 : QObject(parent)
51 ,mName(name)
51 ,mName(name)
52 {
52 {
53 }
53 }
54
54
55 /*!
55 /*!
56 Sets new \a name for set.
56 Sets new \a name for set.
57 */
57 */
58 void QBarSet::setName(QString name)
58 void QBarSet::setName(QString name)
59 {
59 {
60 mName = name;
60 mName = name;
61 }
61 }
62
62
63 /*!
63 /*!
64 Returns name of the set.
64 Returns name of the set.
65 */
65 */
66 QString QBarSet::name()
66 QString QBarSet::name()
67 {
67 {
68 return mName;
68 return mName;
69 }
69 }
70
70
71 /*!
71 /*!
72 Appends new value \a value to the end of set.
72 Appends new value \a value to the end of set.
73 */
73 */
74 QBarSet& QBarSet::operator << (const qreal &value)
74 QBarSet& QBarSet::operator << (const qreal &value)
75 {
75 {
76 mValues.append(value);
76 mValues.append(value);
77 return *this;
77 return *this;
78 }
78 }
79
79
80 /*!
80 /*!
81 Returns count of values in set.
81 Returns count of values in set.
82 */
82 */
83 int QBarSet::count()
83 int QBarSet::count()
84 {
84 {
85 return mValues.count();
85 return mValues.count();
86 }
86 }
87
87
88 /*!
88 /*!
89 Returns value of set indexed by \a index
89 Returns value of set indexed by \a index
90 */
90 */
91 qreal QBarSet::valueAt(int index)
91 qreal QBarSet::valueAt(int index)
92 {
92 {
93 return mValues.at(index);
93 return mValues.at(index);
94 }
94 }
95
95
96 /*!
96 /*!
97 Sets a new value \a value to set, indexed by \a index
97 Sets a new value \a value to set, indexed by \a index
98 */
98 */
99 void QBarSet::setValue(int index, qreal value)
99 void QBarSet::setValue(int index, qreal value)
100 {
100 {
101 mValues.replace(index,value);
101 mValues.replace(index,value);
102 }
102 }
103
103
104 /*!
104 /*!
105 Sets pen for set. Bars of this set are drawn using \a pen
105 Sets pen for set. Bars of this set are drawn using \a pen
106 */
106 */
107 void QBarSet::setPen(const QPen& pen)
107 void QBarSet::setPen(QPen pen)
108 {
108 {
109 mPen = pen;
109 mPen = pen;
110 }
110 }
111
111
112 /*!
112 /*!
113 Returns pen of the set.
113 Returns pen of the set.
114 */
114 */
115 const QPen& QBarSet::pen() const
115 QPen QBarSet::pen()
116 {
116 {
117 return mPen;
117 return mPen;
118 }
118 }
119
119
120 /*!
120 /*!
121 Sets brush for the set. Bars of this set are drawn using \a brush
121 Sets brush for the set. Bars of this set are drawn using \a brush
122 */
122 */
123 void QBarSet::setBrush(const QBrush& brush)
123 void QBarSet::setBrush(QBrush brush)
124 {
124 {
125 mBrush = brush;
125 mBrush = brush;
126 }
126 }
127
127
128 /*!
128 /*!
129 Returns brush of the set.
129 Returns brush of the set.
130 */
130 */
131 const QBrush& QBarSet::brush() const
131 QBrush QBarSet::brush()
132 {
132 {
133 return mBrush;
133 return mBrush;
134 }
134 }
135
135
136 /*!
136 /*!
137 \internal
137 \internal
138 */
138 */
139 void QBarSet::barClicked()
139 void QBarSet::barClicked()
140 {
140 {
141 // qDebug() << "QBarset::barClicked" << this;
141 // qDebug() << "QBarset::barClicked" << this;
142 // Some bar of this set has been clicked
142 // Some bar of this set has been clicked
143 // TODO: What happens then?
143 // TODO: What happens then?
144 emit clicked(); // Notify that set has been clicked
144 emit clicked(); // Notify that set has been clicked
145 }
145 }
146
146
147 /*!
147 /*!
148 \internal \a pos
148 \internal \a pos
149 */
149 */
150 void QBarSet::barHoverEntered(QPoint pos)
150 void QBarSet::barHoverEntered(QPoint pos)
151 {
151 {
152 emit showToolTip(pos, mName);
152 emit showToolTip(pos, mName);
153 emit hoverEnter(pos);
153 emit hoverEnter(pos);
154 }
154 }
155
155
156 /*!
156 /*!
157 \internal
157 \internal
158 */
158 */
159 void QBarSet::barHoverLeaved()
159 void QBarSet::barHoverLeaved()
160 {
160 {
161 // qDebug() << "QBarset::barHoverLeaved" << this;
161 // qDebug() << "QBarset::barHoverLeaved" << this;
162 // if (mToolTipEnabled) {
162 // if (mToolTipEnabled) {
163 // TODO: do what?
163 // TODO: do what?
164 // }
164 // }
165 // Emit signal to user of charts
165 // Emit signal to user of charts
166 emit hoverLeave();
166 emit hoverLeave();
167 }
167 }
168
168
169 #include "moc_qbarset.cpp"
169 #include "moc_qbarset.cpp"
170 QTCOMMERCIALCHART_END_NAMESPACE
170 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,60 +1,60
1 #ifndef QBARSET_H
1 #ifndef QBARSET_H
2 #define QBARSET_H
2 #define QBARSET_H
3
3
4 #include <qchartglobal.h>
4 #include <qchartglobal.h>
5 #include <QPen>
5 #include <QPen>
6 #include <QBrush>
6 #include <QBrush>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
10 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
11 {
11 {
12 Q_OBJECT
12 Q_OBJECT
13 public:
13 public:
14 QBarSet(QString name, QObject *parent = 0);
14 QBarSet(QString name, QObject *parent = 0);
15
15
16 void setName(QString name);
16 void setName(QString name);
17 QString name();
17 QString name();
18 QBarSet& operator << (const qreal &value); // appends new value to set
18 QBarSet& operator << (const qreal &value); // appends new value to set
19
19
20 int count(); // count of values in set
20 int count(); // count of values in set
21 qreal valueAt(int index); // for modifying individual values
21 qreal valueAt(int index); // for modifying individual values
22 void setValue(int index, qreal value); // setter for individual value
22 void setValue(int index, qreal value); // setter for individual value
23
23
24 void setPen(const QPen& pen);
24 void setPen(QPen pen);
25 const QPen& pen() const;
25 QPen pen();
26
26
27 void setBrush(const QBrush& brush);
27 void setBrush(QBrush brush);
28 const QBrush& brush() const;
28 QBrush brush();
29
29
30 Q_SIGNALS:
30 Q_SIGNALS:
31 void clicked(); // Clicked and hover signals exposed to user
31 void clicked(); // Clicked and hover signals exposed to user
32 void hoverEnter(QPoint pos);
32 void hoverEnter(QPoint pos);
33 void hoverLeave();
33 void hoverLeave();
34
34
35 // TODO: Expose this to user or not?
35 // TODO: Expose this to user or not?
36 // TODO: TO PIMPL --->
36 // TODO: TO PIMPL --->
37 void toggleFloatingValues();
37 void toggleFloatingValues();
38 void showToolTip(QPoint pos, QString tip); // Private signal
38 void showToolTip(QPoint pos, QString tip); // Private signal
39 // <--- TO PIMPL
39 // <--- TO PIMPL
40
40
41 public Q_SLOTS:
41 public Q_SLOTS:
42 // These are for internal communication
42 // These are for internal communication
43 // TODO: TO PIMPL --->
43 // TODO: TO PIMPL --->
44 void barClicked();
44 void barClicked();
45 void barHoverEntered(QPoint pos);
45 void barHoverEntered(QPoint pos);
46 void barHoverLeaved();
46 void barHoverLeaved();
47 // <--- TO PIMPL
47 // <--- TO PIMPL
48
48
49 private:
49 private:
50
50
51 QString mName;
51 QString mName;
52 QList<qreal> mValues;
52 QList<qreal> mValues;
53 QPen mPen;
53 QPen mPen;
54 QBrush mBrush;
54 QBrush mBrush;
55
55
56 };
56 };
57
57
58 QTCOMMERCIALCHART_END_NAMESPACE
58 QTCOMMERCIALCHART_END_NAMESPACE
59
59
60 #endif // QBARSET_H
60 #endif // QBARSET_H
@@ -1,111 +1,111
1 #include "stackedbarpresenter.h"
1 #include "stackedbarpresenter.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barlabel_p.h"
3 #include "barlabel_p.h"
4 #include "barvalue_p.h"
4 #include "barvalue_p.h"
5 #include "separator_p.h"
5 #include "separator_p.h"
6 #include "qbarset.h"
6 #include "qbarset.h"
7 #include <QDebug>
7 #include <QDebug>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 StackedBarPresenter::StackedBarPresenter(QBarSeries *series, QGraphicsItem *parent) :
11 StackedBarPresenter::StackedBarPresenter(QBarSeries *series, QGraphicsItem *parent) :
12 BarPresenterBase(series,parent)
12 BarPresenterBase(series,parent)
13 {
13 {
14 }
14 }
15
15
16 void StackedBarPresenter::layoutChanged()
16 void StackedBarPresenter::layoutChanged()
17 {
17 {
18 // Scale bars to new layout
18 // Scale bars to new layout
19 // Layout for bars:
19 // Layout for bars:
20 if (mSeries->countSets() <= 0) {
20 if (mSeries->countSets() <= 0) {
21 qDebug() << "No sets in model!";
21 qDebug() << "No sets in model!";
22 // Nothing to do.
22 // Nothing to do.
23 return;
23 return;
24 }
24 }
25
25
26 if (mSeries->countCategories() == 0) {
26 if (mSeries->countCategories() == 0) {
27 qDebug() << "No categories in model!";
27 qDebug() << "No categories in model!";
28 // Nothing to do
28 // Nothing to do
29 return;
29 return;
30 }
30 }
31
31
32 if (childItems().count() == 0) {
32 if (childItems().count() == 0) {
33 qDebug() << "WARNING: StackedBarPresenter::layoutChanged called before graphics items are created!";
33 qDebug() << "WARNING: StackedBarPresenter::layoutChanged called before graphics items are created!";
34 return;
34 return;
35 }
35 }
36
36
37 // TODO: better way to auto-layout
37 // TODO: better way to auto-layout
38 // Use reals for accurancy (we might get some compiler warnings... :)
38 // Use reals for accurancy (we might get some compiler warnings... :)
39 // TODO: use temp variable for category count...
39 // TODO: use temp variable for category count...
40 qreal maxSum = mSeries->maxCategorySum();
40 qreal maxSum = mSeries->maxCategorySum();
41 qreal h = mHeight;
41 qreal h = mHeight;
42 qreal scale = (h / maxSum);
42 qreal scale = (h / maxSum);
43
43
44 int itemIndex(0);
44 int itemIndex(0);
45 int labelIndex(0);
45 int labelIndex(0);
46 qreal tW = mWidth;
46 qreal tW = mWidth;
47 qreal tC = mSeries->countCategories() + 1;
47 qreal tC = mSeries->countCategories() + 1;
48 qreal xStep = (tW/tC);
48 qreal xStep = (tW/tC);
49 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
49 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
50
50
51 for (int category = 0; category < mSeries->countCategories(); category++) {
51 for (int category = 0; category < mSeries->countCategories(); category++) {
52 qreal yPos = h;
52 qreal yPos = h;
53 for (int set=0; set < mSeries->countSets(); set++) {
53 for (int set=0; set < mSeries->countSets(); set++) {
54 qreal barHeight = mSeries->valueAt(set, category) * scale;
54 qreal barHeight = mSeries->valueAt(set, category) * scale;
55 Bar* bar = mBars.at(itemIndex);
55 Bar* bar = mBars.at(itemIndex);
56
56
57 bar->resize(mBarDefaultWidth, barHeight);
57 bar->resize(mBarDefaultWidth, barHeight);
58 bar->setBrush(mSeries->setAt(set)->brush());
58 bar->setBrush(mSeries->barsetAt(set)->brush());
59 bar->setPos(xPos, yPos-barHeight);
59 bar->setPos(xPos, yPos-barHeight);
60 itemIndex++;
60 itemIndex++;
61 yPos -= barHeight;
61 yPos -= barHeight;
62 }
62 }
63
63
64 // TODO: Layout for labels, remove magic number
64 // TODO: Layout for labels, remove magic number
65 BarLabel* label = mLabels.at(labelIndex);
65 BarLabel* label = mLabels.at(labelIndex);
66 label->setPos(xPos, mHeight + 20);
66 label->setPos(xPos, mHeight + 20);
67 labelIndex++;
67 labelIndex++;
68 xPos += xStep;
68 xPos += xStep;
69 }
69 }
70
70
71 // Position separators
71 // Position separators
72 xPos = xStep + xStep/2;
72 xPos = xStep + xStep/2;
73 for (int s=0; s < mSeries->countCategories() - 1; s++) {
73 for (int s=0; s < mSeries->countCategories() - 1; s++) {
74 Separator* sep = mSeparators.at(s);
74 Separator* sep = mSeparators.at(s);
75 sep->setPos(xPos,0);
75 sep->setPos(xPos,0);
76 sep->setSize(QSizeF(1,mHeight));
76 sep->setSize(QSizeF(1,mHeight));
77 xPos += xStep;
77 xPos += xStep;
78 }
78 }
79
79
80 // Position floating values
80 // Position floating values
81 itemIndex = 0;
81 itemIndex = 0;
82 xPos = ((tW/tC) - mBarDefaultWidth / 2);
82 xPos = ((tW/tC) - mBarDefaultWidth / 2);
83 for (int category=0; category < mSeries->countCategories(); category++) {
83 for (int category=0; category < mSeries->countCategories(); category++) {
84 qreal yPos = h;
84 qreal yPos = h;
85 for (int set=0; set < mSeries->countSets(); set++) {
85 for (int set=0; set < mSeries->countSets(); set++) {
86 qreal barHeight = mSeries->valueAt(set,category) * scale;
86 qreal barHeight = mSeries->valueAt(set,category) * scale;
87 BarValue* value = mFloatingValues.at(itemIndex);
87 BarValue* value = mFloatingValues.at(itemIndex);
88
88
89 // TODO: remove hard coding, apply layout
89 // TODO: remove hard coding, apply layout
90 value->resize(100,50);
90 value->resize(100,50);
91 value->setPos(xPos, yPos-barHeight/2);
91 value->setPos(xPos, yPos-barHeight/2);
92 value->setPen(QPen(QColor(255,255,255,255)));
92 value->setPen(QPen(QColor(255,255,255,255)));
93
93
94 if (mSeries->valueAt(set,category) != 0) {
94 if (mSeries->valueAt(set,category) != 0) {
95 value->setValueString(QString::number(mSeries->valueAt(set,category)));
95 value->setValueString(QString::number(mSeries->valueAt(set,category)));
96 } else {
96 } else {
97 value->setValueString(QString(""));
97 value->setValueString(QString(""));
98 }
98 }
99
99
100 itemIndex++;
100 itemIndex++;
101 yPos -= barHeight;
101 yPos -= barHeight;
102 }
102 }
103 xPos += xStep;
103 xPos += xStep;
104 }
104 }
105
105
106 mLayoutDirty = true;
106 mLayoutDirty = true;
107 }
107 }
108
108
109 #include "moc_stackedbarpresenter.cpp"
109 #include "moc_stackedbarpresenter.cpp"
110
110
111 QTCOMMERCIALCHART_END_NAMESPACE
111 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,267 +1,270
1 #include "charttheme_p.h"
1 #include "charttheme_p.h"
2 #include "qchart.h"
2 #include "qchart.h"
3 #include "qchartaxis.h"
3 #include "qchartaxis.h"
4 #include <QTime>
4 #include <QTime>
5
5
6 //series
6 //series
7 #include "qbarset.h"
7 #include "qbarset.h"
8 #include "qbarseries.h"
8 #include "qbarseries.h"
9 #include "qstackedbarseries.h"
9 #include "qstackedbarseries.h"
10 #include "qpercentbarseries.h"
10 #include "qpercentbarseries.h"
11 #include "qlineseries.h"
11 #include "qlineseries.h"
12 #include "qscatterseries.h"
12 #include "qscatterseries.h"
13 #include "qpieseries.h"
13 #include "qpieseries.h"
14 #include "qpieslice.h"
14 #include "qpieslice.h"
15
15
16 //items
16 //items
17 #include "axisitem_p.h"
17 #include "axisitem_p.h"
18 #include "barpresenter.h"
18 #include "barpresenter.h"
19 #include "stackedbarpresenter.h"
19 #include "stackedbarpresenter.h"
20 #include "linechartitem_p.h"
20 #include "linechartitem_p.h"
21 #include "percentbarpresenter.h"
21 #include "percentbarpresenter.h"
22 #include "scatterpresenter_p.h"
22 #include "scatterpresenter_p.h"
23 #include "piepresenter_p.h"
23 #include "piepresenter_p.h"
24
24
25 //themes
25 //themes
26 #include "chartthemevanilla_p.h"
26 #include "chartthemevanilla_p.h"
27 #include "chartthemeicy_p.h"
27 #include "chartthemeicy_p.h"
28 #include "chartthemegrayscale_p.h"
28 #include "chartthemegrayscale_p.h"
29 #include "chartthemescientific_p.h"
29 #include "chartthemescientific_p.h"
30
30
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 /* TODO
34 /* TODO
35 case QChart::ChartThemeUnnamed1:
35 case QChart::ChartThemeUnnamed1:
36 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2));
36 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2));
37 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2));
37 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2));
38 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2));
38 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2));
39 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2));
39 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2));
40 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2));
40 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2));
41
41
42 m_gradientStartColor = QColor(QRgb(0xfff3dc9e));
42 m_gradientStartColor = QColor(QRgb(0xfff3dc9e));
43 m_gradientEndColor = QColor(QRgb(0xffafafaf));
43 m_gradientEndColor = QColor(QRgb(0xffafafaf));
44 */
44 */
45
45
46 ChartTheme::ChartTheme(QChart::ChartTheme id)
46 ChartTheme::ChartTheme(QChart::ChartTheme id)
47 {
47 {
48 m_id = id;
48 m_id = id;
49 m_seriesColor.append(QRgb(0xff000000));
49 m_seriesColor.append(QRgb(0xff000000));
50 m_seriesColor.append(QRgb(0xff707070));
50 m_seriesColor.append(QRgb(0xff707070));
51 m_gradientStartColor = QColor(QRgb(0xffffffff));
51 m_gradientStartColor = QColor(QRgb(0xffffffff));
52 m_gradientEndColor = QColor(QRgb(0xffafafaf));
52 m_gradientEndColor = QColor(QRgb(0xffafafaf));
53
53
54 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
54 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
55 }
55 }
56
56
57
57
58 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
58 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
59 {
59 {
60 switch(theme) {
60 switch(theme) {
61 case QChart::ChartThemeDefault:
61 case QChart::ChartThemeDefault:
62 return new ChartThemeIcy();
62 return new ChartThemeIcy();
63 case QChart::ChartThemeVanilla:
63 case QChart::ChartThemeVanilla:
64 return new ChartThemeVanilla();
64 return new ChartThemeVanilla();
65 case QChart::ChartThemeIcy:
65 case QChart::ChartThemeIcy:
66 return new ChartThemeIcy();
66 return new ChartThemeIcy();
67 case QChart::ChartThemeGrayscale:
67 case QChart::ChartThemeGrayscale:
68 return new ChartThemeGrayscale();
68 return new ChartThemeGrayscale();
69 case QChart::ChartThemeScientific:
69 case QChart::ChartThemeScientific:
70 return new ChartThemeScientific();
70 return new ChartThemeScientific();
71 }
71 }
72 }
72 }
73
73
74 void ChartTheme::decorate(QChart* chart)
74 void ChartTheme::decorate(QChart* chart)
75 {
75 {
76 QLinearGradient backgroundGradient;
76 QLinearGradient backgroundGradient;
77 backgroundGradient.setColorAt(0.0, m_gradientStartColor);
77 backgroundGradient.setColorAt(0.0, m_gradientStartColor);
78 backgroundGradient.setColorAt(1.0, m_gradientEndColor);
78 backgroundGradient.setColorAt(1.0, m_gradientEndColor);
79 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
79 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
80 chart->setChartBackgroundBrush(backgroundGradient);
80 chart->setChartBackgroundBrush(backgroundGradient);
81 }
81 }
82 //TODO helper to by removed later
82 //TODO helper to by removed later
83 void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count)
83 void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count)
84 {
84 {
85 switch(series->type())
85 switch(series->type())
86 {
86 {
87 case QChartSeries::SeriesTypeLine: {
87 case QChartSeries::SeriesTypeLine: {
88 QLineSeries* s = static_cast<QLineSeries*>(series);
88 QLineSeries* s = static_cast<QLineSeries*>(series);
89 LineChartItem* i = static_cast<LineChartItem*>(item);
89 LineChartItem* i = static_cast<LineChartItem*>(item);
90 decorate(i,s,count);
90 decorate(i,s,count);
91 break;
91 break;
92 }
92 }
93 case QChartSeries::SeriesTypeBar: {
93 case QChartSeries::SeriesTypeBar: {
94 QBarSeries* b = static_cast<QBarSeries*>(series);
94 QBarSeries* b = static_cast<QBarSeries*>(series);
95 BarPresenter* i = static_cast<BarPresenter*>(item);
95 BarPresenter* i = static_cast<BarPresenter*>(item);
96 decorate(i,b,count);
96 decorate(i,b,count);
97 break;
97 break;
98 }
98 }
99 case QChartSeries::SeriesTypeStackedBar: {
99 case QChartSeries::SeriesTypeStackedBar: {
100 QStackedBarSeries* s = static_cast<QStackedBarSeries*>(series);
100 QStackedBarSeries* s = static_cast<QStackedBarSeries*>(series);
101 StackedBarPresenter* i = static_cast<StackedBarPresenter*>(item);
101 StackedBarPresenter* i = static_cast<StackedBarPresenter*>(item);
102 decorate(i,s,count);
102 decorate(i,s,count);
103 break;
103 break;
104 }
104 }
105 case QChartSeries::SeriesTypePercentBar: {
105 case QChartSeries::SeriesTypePercentBar: {
106 QPercentBarSeries* s = static_cast<QPercentBarSeries*>(series);
106 QPercentBarSeries* s = static_cast<QPercentBarSeries*>(series);
107 PercentBarPresenter* i = static_cast<PercentBarPresenter*>(item);
107 PercentBarPresenter* i = static_cast<PercentBarPresenter*>(item);
108 decorate(i,s,count);
108 decorate(i,s,count);
109 break;
109 break;
110 }
110 }
111 case QChartSeries::SeriesTypeScatter: {
111 case QChartSeries::SeriesTypeScatter: {
112 QScatterSeries* s = qobject_cast<QScatterSeries*>(series);
112 QScatterSeries* s = qobject_cast<QScatterSeries*>(series);
113 Q_ASSERT(s);
113 Q_ASSERT(s);
114 ScatterPresenter* i = static_cast<ScatterPresenter*>(item);
114 ScatterPresenter* i = static_cast<ScatterPresenter*>(item);
115 Q_ASSERT(i);
115 Q_ASSERT(i);
116 decorate(i, s, count);
116 decorate(i, s, count);
117 break;
117 break;
118 }
118 }
119 case QChartSeries::SeriesTypePie: {
119 case QChartSeries::SeriesTypePie: {
120 QPieSeries* s = static_cast<QPieSeries*>(series);
120 QPieSeries* s = static_cast<QPieSeries*>(series);
121 PiePresenter* i = static_cast<PiePresenter*>(item);
121 PiePresenter* i = static_cast<PiePresenter*>(item);
122 decorate(i,s,count);
122 decorate(i,s,count);
123 break;
123 break;
124 }
124 }
125 default:
125 default:
126 qDebug()<<"Wrong item to be decorated by theme";
126 qDebug()<<"Wrong item to be decorated by theme";
127 break;
127 break;
128 }
128 }
129
129
130 }
130 }
131
131
132 void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int count)
132 void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int count)
133 {
133 {
134 QPen pen;
134 QPen pen;
135 if(pen != series->pen()){
135 if(pen != series->pen()){
136 item->setPen(series->pen());
136 item->setPen(series->pen());
137 return;
137 return;
138 }
138 }
139 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
139 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
140 pen.setWidthF(2);
140 pen.setWidthF(2);
141 item->setPen(pen);
141 item->setPen(pen);
142 }
142 }
143
143
144 void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int count)
144 void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int count)
145 {
145 {
146 QList<QBarSet*> sets = series->barSets();
146 for (int i=0; i<series->countSets(); i++) {
147 for (int i=0; i<series->countSets(); i++) {
147 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
148 sets.at(i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
148 }
149 }
149 }
150 }
150
151
151 void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int count)
152 void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int count)
152 {
153 {
154 QList<QBarSet*> sets = series->barSets();
153 for (int i=0; i<series->countSets(); i++) {
155 for (int i=0; i<series->countSets(); i++) {
154 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
156 sets.at(i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
155 }
157 }
156 }
158 }
157
159
158 void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int count)
160 void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int count)
159 {
161 {
162 QList<QBarSet*> sets = series->barSets();
160 for (int i=0; i<series->countSets(); i++) {
163 for (int i=0; i<series->countSets(); i++) {
161 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
164 sets.at(i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
162 }
165 }
163 }
166 }
164
167
165 void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count)
168 void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count)
166 {
169 {
167 Q_ASSERT(presenter);
170 Q_ASSERT(presenter);
168 Q_ASSERT(series);
171 Q_ASSERT(series);
169
172
170 QColor color = m_seriesColor.at(count % m_seriesColor.size());
173 QColor color = m_seriesColor.at(count % m_seriesColor.size());
171 // TODO: define alpha in the theme? or in the series?
174 // TODO: define alpha in the theme? or in the series?
172 //color.setAlpha(120);
175 //color.setAlpha(120);
173
176
174 QBrush brush(color, Qt::SolidPattern);
177 QBrush brush(color, Qt::SolidPattern);
175 presenter->m_markerBrush = brush;
178 presenter->m_markerBrush = brush;
176
179
177 QPen pen(brush, 3);
180 QPen pen(brush, 3);
178 pen.setColor(color);
181 pen.setColor(color);
179 presenter->m_markerPen = pen;
182 presenter->m_markerPen = pen;
180 }
183 }
181
184
182 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
185 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
183 {
186 {
184 // create a list of slice colors based on current theme
187 // create a list of slice colors based on current theme
185 int i = 0;
188 int i = 0;
186 QList<QColor> colors;
189 QList<QColor> colors;
187 while (colors.count() < series->count()) {
190 while (colors.count() < series->count()) {
188
191
189 // get base color
192 // get base color
190 QColor c = m_seriesColor[i++];
193 QColor c = m_seriesColor[i++];
191 i = i % m_seriesColor.count();
194 i = i % m_seriesColor.count();
192
195
193 // dont use black colors... looks bad
196 // dont use black colors... looks bad
194 if (c == Qt::black)
197 if (c == Qt::black)
195 continue;
198 continue;
196
199
197 // by default use the "raw" theme color
200 // by default use the "raw" theme color
198 if (!colors.contains(c)) {
201 if (!colors.contains(c)) {
199 colors << c;
202 colors << c;
200 continue;
203 continue;
201 }
204 }
202 // ...ok we need to generate something that looks like the same color
205 // ...ok we need to generate something that looks like the same color
203 // but different lightness
206 // but different lightness
204
207
205 int tryCount = 0;
208 int tryCount = 0;
206 while (tryCount++ < 100) {
209 while (tryCount++ < 100) {
207
210
208 // find maximum value we can raise the lightness
211 // find maximum value we can raise the lightness
209 int lMax = 255;
212 int lMax = 255;
210 if (lMax > 255 - c.red())
213 if (lMax > 255 - c.red())
211 lMax = 255 - c.red();
214 lMax = 255 - c.red();
212 if (lMax > 255 - c.green())
215 if (lMax > 255 - c.green())
213 lMax = 255 - c.green();
216 lMax = 255 - c.green();
214 if (lMax > 255 - c.blue())
217 if (lMax > 255 - c.blue())
215 lMax = 255 - c.blue();
218 lMax = 255 - c.blue();
216
219
217 // find maximum value we can make it darker
220 // find maximum value we can make it darker
218 int dMax = 255;
221 int dMax = 255;
219 if (dMax > c.red())
222 if (dMax > c.red())
220 dMax = c.red();
223 dMax = c.red();
221 if (dMax > c.green())
224 if (dMax > c.green())
222 dMax = c.green();
225 dMax = c.green();
223 if (dMax > c.blue())
226 if (dMax > c.blue())
224 dMax = c.blue();
227 dMax = c.blue();
225
228
226 int max = dMax + lMax;
229 int max = dMax + lMax;
227 if (max == 0) {
230 if (max == 0) {
228 // no room to make color lighter or darker...
231 // no room to make color lighter or darker...
229 qDebug() << "cannot generate a color for pie!";
232 qDebug() << "cannot generate a color for pie!";
230 break;
233 break;
231 }
234 }
232
235
233 // generate random color
236 // generate random color
234 int r = c.red() - dMax;
237 int r = c.red() - dMax;
235 int g = c.green() - dMax;
238 int g = c.green() - dMax;
236 int b = c.blue() - dMax;
239 int b = c.blue() - dMax;
237 int d = qrand() % max;
240 int d = qrand() % max;
238 c.setRgb(r+d, g+d, b+d);
241 c.setRgb(r+d, g+d, b+d);
239
242
240 // found a unique color?
243 // found a unique color?
241 if (!colors.contains(c))
244 if (!colors.contains(c))
242 break;
245 break;
243 }
246 }
244
247
245 qDebug() << "generated a color for pie" << c;
248 qDebug() << "generated a color for pie" << c;
246 colors << c;
249 colors << c;
247 }
250 }
248
251
249 // finally update colors
252 // finally update colors
250 foreach (QPieSlice* s, series->slices()) {
253 foreach (QPieSlice* s, series->slices()) {
251 QColor c = colors.takeFirst();
254 QColor c = colors.takeFirst();
252 s->setPen(c);
255 s->setPen(c);
253 s->setBrush(c);
256 s->setBrush(c);
254 }
257 }
255 }
258 }
256
259
257
260
258 void ChartTheme::decorate(QChartAxis* axis,AxisItem* item)
261 void ChartTheme::decorate(QChartAxis* axis,AxisItem* item)
259 {
262 {
260 //TODO: dummy defults for now
263 //TODO: dummy defults for now
261 axis->setLabelsBrush(Qt::black);
264 axis->setLabelsBrush(Qt::black);
262 axis->setLabelsPen(Qt::NoPen);
265 axis->setLabelsPen(Qt::NoPen);
263 axis->setShadesPen(Qt::NoPen);
266 axis->setShadesPen(Qt::NoPen);
264 axis->setShadesOpacity(0.5);
267 axis->setShadesOpacity(0.5);
265 }
268 }
266
269
267 QTCOMMERCIALCHART_END_NAMESPACE
270 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,372 +1,372
1 #include "mainwidget.h"
1 #include "mainwidget.h"
2 #include "dataseriedialog.h"
2 #include "dataseriedialog.h"
3 #include "qchartseries.h"
3 #include "qchartseries.h"
4 #include "qpieseries.h"
4 #include "qpieseries.h"
5 #include "qscatterseries.h"
5 #include "qscatterseries.h"
6 #include <qlineseries.h>
6 #include <qlineseries.h>
7 #include <qbarset.h>
7 #include <qbarset.h>
8 #include <qbarcategory.h>
8 #include <qbarcategory.h>
9 #include <qbarseries.h>
9 #include <qbarseries.h>
10 #include <qstackedbarseries.h>
10 #include <qstackedbarseries.h>
11 #include <qpercentbarseries.h>
11 #include <qpercentbarseries.h>
12 #include <QPushButton>
12 #include <QPushButton>
13 #include <QComboBox>
13 #include <QComboBox>
14 #include <QSpinBox>
14 #include <QSpinBox>
15 #include <QCheckBox>
15 #include <QCheckBox>
16 #include <QGridLayout>
16 #include <QGridLayout>
17 #include <QHBoxLayout>
17 #include <QHBoxLayout>
18 #include <QLabel>
18 #include <QLabel>
19 #include <QSpacerItem>
19 #include <QSpacerItem>
20 #include <QMessageBox>
20 #include <QMessageBox>
21 #include <cmath>
21 #include <cmath>
22 #include <QDebug>
22 #include <QDebug>
23 #include <QStandardItemModel>
23 #include <QStandardItemModel>
24
24
25
25
26 QTCOMMERCIALCHART_USE_NAMESPACE
26 QTCOMMERCIALCHART_USE_NAMESPACE
27
27
28 MainWidget::MainWidget(QWidget *parent) :
28 MainWidget::MainWidget(QWidget *parent) :
29 QWidget(parent)
29 QWidget(parent)
30 {
30 {
31 m_chartWidget = new QChartView(this);
31 m_chartWidget = new QChartView(this);
32 m_chartWidget->setRubberBandPolicy(QChartView::HorizonalRubberBand);
32 m_chartWidget->setRubberBandPolicy(QChartView::HorizonalRubberBand);
33
33
34 // Grid layout for the controls for configuring the chart widget
34 // Grid layout for the controls for configuring the chart widget
35 QGridLayout *grid = new QGridLayout();
35 QGridLayout *grid = new QGridLayout();
36 QPushButton *addSeriesButton = new QPushButton("Add series");
36 QPushButton *addSeriesButton = new QPushButton("Add series");
37 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
37 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
38 grid->addWidget(addSeriesButton, 0, 1);
38 grid->addWidget(addSeriesButton, 0, 1);
39 initBackroundCombo(grid);
39 initBackroundCombo(grid);
40 initScaleControls(grid);
40 initScaleControls(grid);
41 initThemeCombo(grid);
41 initThemeCombo(grid);
42 QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom");
42 QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom");
43 connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartWidget, SLOT(setZoomEnabled(bool)));
43 connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartWidget, SLOT(setZoomEnabled(bool)));
44 zoomCheckBox->setChecked(true);
44 zoomCheckBox->setChecked(true);
45 grid->addWidget(zoomCheckBox, grid->rowCount(), 0);
45 grid->addWidget(zoomCheckBox, grid->rowCount(), 0);
46 // add row with empty label to make all the other rows static
46 // add row with empty label to make all the other rows static
47 grid->addWidget(new QLabel(""), grid->rowCount(), 0);
47 grid->addWidget(new QLabel(""), grid->rowCount(), 0);
48 grid->setRowStretch(grid->rowCount() - 1, 1);
48 grid->setRowStretch(grid->rowCount() - 1, 1);
49
49
50 // Another grid layout as a main layout
50 // Another grid layout as a main layout
51 QGridLayout *mainLayout = new QGridLayout();
51 QGridLayout *mainLayout = new QGridLayout();
52 mainLayout->addLayout(grid, 0, 0);
52 mainLayout->addLayout(grid, 0, 0);
53
53
54 // Init series type specific controls
54 // Init series type specific controls
55 initPieControls();
55 initPieControls();
56 mainLayout->addLayout(m_pieLayout, 2, 0);
56 mainLayout->addLayout(m_pieLayout, 2, 0);
57 // Scatter series specific settings
57 // Scatter series specific settings
58 // m_scatterLayout = new QGridLayout();
58 // m_scatterLayout = new QGridLayout();
59 // m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0);
59 // m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0);
60 // m_scatterLayout->setEnabled(false);
60 // m_scatterLayout->setEnabled(false);
61 // mainLayout->addLayout(m_scatterLayout, 1, 0);
61 // mainLayout->addLayout(m_scatterLayout, 1, 0);
62
62
63 // Add layouts and the chart widget to the main layout
63 // Add layouts and the chart widget to the main layout
64 mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1);
64 mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1);
65 setLayout(mainLayout);
65 setLayout(mainLayout);
66 }
66 }
67
67
68 // Combo box for selecting the chart's background
68 // Combo box for selecting the chart's background
69 void MainWidget::initBackroundCombo(QGridLayout *grid)
69 void MainWidget::initBackroundCombo(QGridLayout *grid)
70 {
70 {
71 QComboBox *backgroundCombo = new QComboBox(this);
71 QComboBox *backgroundCombo = new QComboBox(this);
72 backgroundCombo->addItem("Color");
72 backgroundCombo->addItem("Color");
73 backgroundCombo->addItem("Gradient");
73 backgroundCombo->addItem("Gradient");
74 backgroundCombo->addItem("Image");
74 backgroundCombo->addItem("Image");
75 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
75 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
76 this, SLOT(backgroundChanged(int)));
76 this, SLOT(backgroundChanged(int)));
77
77
78 grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0);
78 grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0);
79 grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1);
79 grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1);
80 }
80 }
81
81
82 // Scale related controls (auto-scale vs. manual min-max values)
82 // Scale related controls (auto-scale vs. manual min-max values)
83 void MainWidget::initScaleControls(QGridLayout *grid)
83 void MainWidget::initScaleControls(QGridLayout *grid)
84 {
84 {
85 m_autoScaleCheck = new QCheckBox("Automatic scaling");
85 m_autoScaleCheck = new QCheckBox("Automatic scaling");
86 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
86 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
87 // Allow setting also non-sense values (like -2147483648 and 2147483647)
87 // Allow setting also non-sense values (like -2147483648 and 2147483647)
88 m_xMinSpin = new QSpinBox();
88 m_xMinSpin = new QSpinBox();
89 m_xMinSpin->setMinimum(INT_MIN);
89 m_xMinSpin->setMinimum(INT_MIN);
90 m_xMinSpin->setMaximum(INT_MAX);
90 m_xMinSpin->setMaximum(INT_MAX);
91 m_xMinSpin->setValue(0);
91 m_xMinSpin->setValue(0);
92 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
92 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
93 m_xMaxSpin = new QSpinBox();
93 m_xMaxSpin = new QSpinBox();
94 m_xMaxSpin->setMinimum(INT_MIN);
94 m_xMaxSpin->setMinimum(INT_MIN);
95 m_xMaxSpin->setMaximum(INT_MAX);
95 m_xMaxSpin->setMaximum(INT_MAX);
96 m_xMaxSpin->setValue(10);
96 m_xMaxSpin->setValue(10);
97 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
97 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
98 m_yMinSpin = new QSpinBox();
98 m_yMinSpin = new QSpinBox();
99 m_yMinSpin->setMinimum(INT_MIN);
99 m_yMinSpin->setMinimum(INT_MIN);
100 m_yMinSpin->setMaximum(INT_MAX);
100 m_yMinSpin->setMaximum(INT_MAX);
101 m_yMinSpin->setValue(0);
101 m_yMinSpin->setValue(0);
102 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
102 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
103 m_yMaxSpin = new QSpinBox();
103 m_yMaxSpin = new QSpinBox();
104 m_yMaxSpin->setMinimum(INT_MIN);
104 m_yMaxSpin->setMinimum(INT_MIN);
105 m_yMaxSpin->setMaximum(INT_MAX);
105 m_yMaxSpin->setMaximum(INT_MAX);
106 m_yMaxSpin->setValue(10);
106 m_yMaxSpin->setValue(10);
107 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
107 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
108
108
109 grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0);
109 grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0);
110 grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0);
110 grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0);
111 grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1);
111 grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1);
112 grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0);
112 grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0);
113 grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1);
113 grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1);
114 grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0);
114 grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0);
115 grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1);
115 grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1);
116 grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0);
116 grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0);
117 grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1);
117 grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1);
118
118
119 m_autoScaleCheck->setChecked(true);
119 m_autoScaleCheck->setChecked(true);
120 }
120 }
121
121
122 // Combo box for selecting theme
122 // Combo box for selecting theme
123 void MainWidget::initThemeCombo(QGridLayout *grid)
123 void MainWidget::initThemeCombo(QGridLayout *grid)
124 {
124 {
125 QComboBox *chartTheme = new QComboBox();
125 QComboBox *chartTheme = new QComboBox();
126 chartTheme->addItem("Default");
126 chartTheme->addItem("Default");
127 chartTheme->addItem("Vanilla");
127 chartTheme->addItem("Vanilla");
128 chartTheme->addItem("Icy");
128 chartTheme->addItem("Icy");
129 chartTheme->addItem("Grayscale");
129 chartTheme->addItem("Grayscale");
130 chartTheme->addItem("Scientific");
130 chartTheme->addItem("Scientific");
131 chartTheme->addItem("Unnamed1");
131 chartTheme->addItem("Unnamed1");
132 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
132 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
133 this, SLOT(changeChartTheme(int)));
133 this, SLOT(changeChartTheme(int)));
134 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
134 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
135 grid->addWidget(chartTheme, 8, 1);
135 grid->addWidget(chartTheme, 8, 1);
136 }
136 }
137
137
138 void MainWidget::initPieControls()
138 void MainWidget::initPieControls()
139 {
139 {
140 // Pie series specific settings
140 // Pie series specific settings
141 // Pie size factory
141 // Pie size factory
142 QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox();
142 QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox();
143 pieSizeSpin->setMinimum(LONG_MIN);
143 pieSizeSpin->setMinimum(LONG_MIN);
144 pieSizeSpin->setMaximum(LONG_MAX);
144 pieSizeSpin->setMaximum(LONG_MAX);
145 pieSizeSpin->setValue(1.0);
145 pieSizeSpin->setValue(1.0);
146 pieSizeSpin->setSingleStep(0.1);
146 pieSizeSpin->setSingleStep(0.1);
147 connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double)));
147 connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double)));
148 // Pie position
148 // Pie position
149 QComboBox *piePosCombo = new QComboBox(this);
149 QComboBox *piePosCombo = new QComboBox(this);
150 piePosCombo->addItem("Maximized");
150 piePosCombo->addItem("Maximized");
151 piePosCombo->addItem("Top left");
151 piePosCombo->addItem("Top left");
152 piePosCombo->addItem("Top right");
152 piePosCombo->addItem("Top right");
153 piePosCombo->addItem("Bottom left");
153 piePosCombo->addItem("Bottom left");
154 piePosCombo->addItem("Bottom right");
154 piePosCombo->addItem("Bottom right");
155 connect(piePosCombo, SIGNAL(currentIndexChanged(int)),
155 connect(piePosCombo, SIGNAL(currentIndexChanged(int)),
156 this, SLOT(setPiePosition(int)));
156 this, SLOT(setPiePosition(int)));
157 m_pieLayout = new QGridLayout();
157 m_pieLayout = new QGridLayout();
158 m_pieLayout->setEnabled(false);
158 m_pieLayout->setEnabled(false);
159 m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0);
159 m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0);
160 m_pieLayout->addWidget(pieSizeSpin, 0, 1);
160 m_pieLayout->addWidget(pieSizeSpin, 0, 1);
161 m_pieLayout->addWidget(new QLabel("Pie position"), 1, 0);
161 m_pieLayout->addWidget(new QLabel("Pie position"), 1, 0);
162 m_pieLayout->addWidget(piePosCombo, 1, 1);
162 m_pieLayout->addWidget(piePosCombo, 1, 1);
163 }
163 }
164
164
165 void MainWidget::addSeries()
165 void MainWidget::addSeries()
166 {
166 {
167 DataSerieDialog dialog(m_defaultSeriesName, this);
167 DataSerieDialog dialog(m_defaultSeriesName, this);
168 connect(&dialog, SIGNAL(accepted(QString, int, int, QString, bool)),
168 connect(&dialog, SIGNAL(accepted(QString, int, int, QString, bool)),
169 this, SLOT(addSeries(QString, int, int, QString, bool)));
169 this, SLOT(addSeries(QString, int, int, QString, bool)));
170 dialog.exec();
170 dialog.exec();
171 }
171 }
172
172
173 QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics)
173 QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics)
174 {
174 {
175 // TODO: dataCharacteristics
175 // TODO: dataCharacteristics
176 QList<RealList> testData;
176 QList<RealList> testData;
177 for (int j(0); j < columnCount; j++) {
177 for (int j(0); j < columnCount; j++) {
178 QList <qreal> newColumn;
178 QList <qreal> newColumn;
179 for (int i(0); i < rowCount; i++) {
179 for (int i(0); i < rowCount; i++) {
180 if (dataCharacteristics == "Sin") {
180 if (dataCharacteristics == "Sin") {
181 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100));
181 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100));
182 } else if (dataCharacteristics == "Sin + random") {
182 } else if (dataCharacteristics == "Sin + random") {
183 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
183 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
184 } else if (dataCharacteristics == "Random") {
184 } else if (dataCharacteristics == "Random") {
185 newColumn.append(rand() % 5);
185 newColumn.append(rand() % 5);
186 } else if (dataCharacteristics == "Linear") {
186 } else if (dataCharacteristics == "Linear") {
187 //newColumn.append(i * (j + 1.0));
187 //newColumn.append(i * (j + 1.0));
188 // TODO: temporary hack to make pie work; prevent zero values:
188 // TODO: temporary hack to make pie work; prevent zero values:
189 newColumn.append(i * (j + 1.0) + 0.1);
189 newColumn.append(i * (j + 1.0) + 0.1);
190 } else { // "constant"
190 } else { // "constant"
191 newColumn.append((j + 1.0));
191 newColumn.append((j + 1.0));
192 }
192 }
193 }
193 }
194 testData.append(newColumn);
194 testData.append(newColumn);
195 }
195 }
196 return testData;
196 return testData;
197 }
197 }
198
198
199 QStringList MainWidget::generateLabels(int count)
199 QStringList MainWidget::generateLabels(int count)
200 {
200 {
201 QStringList result;
201 QStringList result;
202 for (int i(0); i < count; i++)
202 for (int i(0); i < count; i++)
203 result.append("label" + QString::number(i));
203 result.append("label" + QString::number(i));
204 return result;
204 return result;
205 }
205 }
206
206
207 void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled)
207 void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled)
208 {
208 {
209 qDebug() << "addSeries: " << seriesName
209 qDebug() << "addSeries: " << seriesName
210 << " columnCount: " << columnCount
210 << " columnCount: " << columnCount
211 << " rowCount: " << rowCount
211 << " rowCount: " << rowCount
212 << " dataCharacteristics: " << dataCharacteristics
212 << " dataCharacteristics: " << dataCharacteristics
213 << " labels enabled: " << labelsEnabled;
213 << " labels enabled: " << labelsEnabled;
214 m_defaultSeriesName = seriesName;
214 m_defaultSeriesName = seriesName;
215
215
216 QList<RealList> data = generateTestData(columnCount, rowCount, dataCharacteristics);
216 QList<RealList> data = generateTestData(columnCount, rowCount, dataCharacteristics);
217
217
218 // Line series and scatter series use similar data
218 // Line series and scatter series use similar data
219 if (seriesName.contains("line", Qt::CaseInsensitive)) {
219 if (seriesName.contains("line", Qt::CaseInsensitive)) {
220 for (int j(0); j < data.count(); j ++) {
220 for (int j(0); j < data.count(); j ++) {
221 QList<qreal> column = data.at(j);
221 QList<qreal> column = data.at(j);
222 QLineSeries *series = new QLineSeries();
222 QLineSeries *series = new QLineSeries();
223 for (int i(0); i < column.count(); i++) {
223 for (int i(0); i < column.count(); i++) {
224 series->add(i, column.at(i));
224 series->add(i, column.at(i));
225 }
225 }
226 m_chartWidget->addSeries(series);
226 m_chartWidget->addSeries(series);
227 setCurrentSeries(series);
227 setCurrentSeries(series);
228 }
228 }
229 } else if (seriesName.contains("scatter", Qt::CaseInsensitive)) {
229 } else if (seriesName.contains("scatter", Qt::CaseInsensitive)) {
230 for (int j(0); j < data.count(); j++) {
230 for (int j(0); j < data.count(); j++) {
231 QList<qreal> column = data.at(j);
231 QList<qreal> column = data.at(j);
232 QScatterSeries *series = new QScatterSeries();
232 QScatterSeries *series = new QScatterSeries();
233 for (int i(0); i < column.count(); i++) {
233 for (int i(0); i < column.count(); i++) {
234 (*series) << QPointF(i, column.at(i));
234 (*series) << QPointF(i, column.at(i));
235 }
235 }
236 m_chartWidget->addSeries(series);
236 m_chartWidget->addSeries(series);
237 setCurrentSeries(series);
237 setCurrentSeries(series);
238 }
238 }
239 } else if (seriesName.contains("pie", Qt::CaseInsensitive)) {
239 } else if (seriesName.contains("pie", Qt::CaseInsensitive)) {
240 QStringList labels = generateLabels(rowCount);
240 QStringList labels = generateLabels(rowCount);
241 for (int j(0); j < data.count(); j++) {
241 for (int j(0); j < data.count(); j++) {
242 QPieSeries *series = new QPieSeries();
242 QPieSeries *series = new QPieSeries();
243 QList<qreal> column = data.at(j);
243 QList<qreal> column = data.at(j);
244 for (int i(0); i < column.count(); i++) {
244 for (int i(0); i < column.count(); i++) {
245 series->add(column.at(i), labels.at(i));
245 series->add(column.at(i), labels.at(i));
246 }
246 }
247 m_chartWidget->addSeries(series);
247 m_chartWidget->addSeries(series);
248 setCurrentSeries(series);
248 setCurrentSeries(series);
249 }
249 }
250 } else if (seriesName == "Bar"
250 } else if (seriesName == "Bar"
251 || seriesName == "Stacked bar"
251 || seriesName == "Stacked bar"
252 || seriesName == "Percent bar") {
252 || seriesName == "Percent bar") {
253 // TODO: replace QBarCategory with QStringList?
253 // TODO: replace QBarCategory with QStringList?
254 QBarCategory *category = new QBarCategory;
254 QBarCategory *category = new QBarCategory;
255 QStringList labels = generateLabels(rowCount);
255 QStringList labels = generateLabels(rowCount);
256 foreach(QString label, labels)
256 foreach(QString label, labels)
257 *category << label;
257 *category << label;
258 QBarSeries* series = 0;
258 QBarSeries* series = 0;
259 if (seriesName == "Bar")
259 if (seriesName == "Bar")
260 series = new QBarSeries(category, this);
260 series = new QBarSeries(category, this);
261 else if (seriesName == "Stacked bar")
261 else if (seriesName == "Stacked bar")
262 series = new QStackedBarSeries(category, this);
262 series = new QStackedBarSeries(category, this);
263 else
263 else
264 series = new QPercentBarSeries(category, this);
264 series = new QPercentBarSeries(category, this);
265
265
266 for (int j(0); j < data.count(); j++) {
266 for (int j(0); j < data.count(); j++) {
267 QList<qreal> column = data.at(j);
267 QList<qreal> column = data.at(j);
268 QBarSet *set = new QBarSet("set" + QString::number(j));
268 QBarSet *set = new QBarSet("set" + QString::number(j));
269 for (int i(0); i < column.count(); i++) {
269 for (int i(0); i < column.count(); i++) {
270 *set << column.at(i);
270 *set << column.at(i);
271 }
271 }
272 series->addBarSet(set);
272 series->addBarSet(set);
273 }
273 }
274 series->enableFloatingValues();
274 series->enableFloatingValues();
275 series->enableToolTip();
275 series->setToolTipEnabled();
276 series->enableSeparators(false);
276 series->enableSeparators(false);
277 m_chartWidget->addSeries(series);
277 m_chartWidget->addSeries(series);
278 setCurrentSeries(series);
278 setCurrentSeries(series);
279 }
279 }
280
280
281 // TODO: spline and area
281 // TODO: spline and area
282 }
282 }
283
283
284 void MainWidget::setCurrentSeries(QChartSeries *series)
284 void MainWidget::setCurrentSeries(QChartSeries *series)
285 {
285 {
286 if (series) {
286 if (series) {
287 m_currentSeries = series;
287 m_currentSeries = series;
288 switch (m_currentSeries->type()) {
288 switch (m_currentSeries->type()) {
289 case QChartSeries::SeriesTypeLine:
289 case QChartSeries::SeriesTypeLine:
290 break;
290 break;
291 case QChartSeries::SeriesTypeScatter:
291 case QChartSeries::SeriesTypeScatter:
292 break;
292 break;
293 case QChartSeries::SeriesTypePie:
293 case QChartSeries::SeriesTypePie:
294 break;
294 break;
295 case QChartSeries::SeriesTypeBar:
295 case QChartSeries::SeriesTypeBar:
296 qDebug() << "setCurrentSeries (bar)";
296 qDebug() << "setCurrentSeries (bar)";
297 break;
297 break;
298 case QChartSeries::SeriesTypeStackedBar:
298 case QChartSeries::SeriesTypeStackedBar:
299 qDebug() << "setCurrentSeries (Stackedbar)";
299 qDebug() << "setCurrentSeries (Stackedbar)";
300 break;
300 break;
301 case QChartSeries::SeriesTypePercentBar:
301 case QChartSeries::SeriesTypePercentBar:
302 qDebug() << "setCurrentSeries (Percentbar)";
302 qDebug() << "setCurrentSeries (Percentbar)";
303 break;
303 break;
304 default:
304 default:
305 Q_ASSERT(false);
305 Q_ASSERT(false);
306 break;
306 break;
307 }
307 }
308 }
308 }
309 }
309 }
310
310
311 void MainWidget::backgroundChanged(int itemIndex)
311 void MainWidget::backgroundChanged(int itemIndex)
312 {
312 {
313 qDebug() << "backgroundChanged: " << itemIndex;
313 qDebug() << "backgroundChanged: " << itemIndex;
314 }
314 }
315
315
316 void MainWidget::autoScaleChanged(int value)
316 void MainWidget::autoScaleChanged(int value)
317 {
317 {
318 if (value) {
318 if (value) {
319 // TODO: enable auto scaling
319 // TODO: enable auto scaling
320 } else {
320 } else {
321 // TODO: set scaling manually (and disable auto scaling)
321 // TODO: set scaling manually (and disable auto scaling)
322 }
322 }
323
323
324 m_xMinSpin->setEnabled(!value);
324 m_xMinSpin->setEnabled(!value);
325 m_xMaxSpin->setEnabled(!value);
325 m_xMaxSpin->setEnabled(!value);
326 m_yMinSpin->setEnabled(!value);
326 m_yMinSpin->setEnabled(!value);
327 m_yMaxSpin->setEnabled(!value);
327 m_yMaxSpin->setEnabled(!value);
328 }
328 }
329
329
330 void MainWidget::xMinChanged(int value)
330 void MainWidget::xMinChanged(int value)
331 {
331 {
332 qDebug() << "xMinChanged: " << value;
332 qDebug() << "xMinChanged: " << value;
333 }
333 }
334
334
335 void MainWidget::xMaxChanged(int value)
335 void MainWidget::xMaxChanged(int value)
336 {
336 {
337 qDebug() << "xMaxChanged: " << value;
337 qDebug() << "xMaxChanged: " << value;
338 }
338 }
339
339
340 void MainWidget::yMinChanged(int value)
340 void MainWidget::yMinChanged(int value)
341 {
341 {
342 qDebug() << "yMinChanged: " << value;
342 qDebug() << "yMinChanged: " << value;
343 }
343 }
344
344
345 void MainWidget::yMaxChanged(int value)
345 void MainWidget::yMaxChanged(int value)
346 {
346 {
347 qDebug() << "yMaxChanged: " << value;
347 qDebug() << "yMaxChanged: " << value;
348 }
348 }
349
349
350 void MainWidget::changeChartTheme(int themeIndex)
350 void MainWidget::changeChartTheme(int themeIndex)
351 {
351 {
352 qDebug() << "changeChartTheme: " << themeIndex;
352 qDebug() << "changeChartTheme: " << themeIndex;
353 m_chartWidget->setChartTheme((QChart::ChartTheme) themeIndex);
353 m_chartWidget->setChartTheme((QChart::ChartTheme) themeIndex);
354 //TODO: remove this hack. This is just to make it so that theme change is seen immediately.
354 //TODO: remove this hack. This is just to make it so that theme change is seen immediately.
355 QSize s = size();
355 QSize s = size();
356 s.setWidth(s.width()+1);
356 s.setWidth(s.width()+1);
357 resize(s);
357 resize(s);
358 }
358 }
359
359
360 void MainWidget::setPieSizeFactor(double size)
360 void MainWidget::setPieSizeFactor(double size)
361 {
361 {
362 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
362 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
363 if (pie)
363 if (pie)
364 pie->setSizeFactor(qreal(size));
364 pie->setSizeFactor(qreal(size));
365 }
365 }
366
366
367 void MainWidget::setPiePosition(int position)
367 void MainWidget::setPiePosition(int position)
368 {
368 {
369 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
369 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
370 if (pie)
370 if (pie)
371 pie->setPosition((QPieSeries::PiePosition) position);
371 pie->setPosition((QPieSeries::PiePosition) position);
372 }
372 }
General Comments 0
You need to be logged in to leave comments. Login now