##// END OF EJS Templates
enablers for tooltip and floating values, bug fixing, updated examples. tidying up the code
sauimone -
r296:8254aab7233d
parent child
Show More
@@ -1,12 +1,10
1 !include( ../example.pri ) {
1 !include( ../example.pri ) {
2 error( "Couldn't find the example.pri file!" )
2 error( "Couldn't find the example.pri file!" )
3 }
3 }
4
4
5 TARGET = barchart
5 TARGET = barchart
6 SOURCES += main.cpp \
6 SOURCES += main.cpp \
7 chartwidget.cpp \
7 chartwidget.cpp
8 custombarset.cpp
9 HEADERS += \
8 HEADERS += \
10 chartwidget.h \
9 chartwidget.h
11 custombarset.h
12
10
@@ -1,53 +1,54
1 #include <QApplication>
1 #include <QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <qchartview.h>
3 #include <qchartview.h>
4 #include <qbarchartseries.h>
4 #include <qbarchartseries.h>
5 #include <qbarset.h>
5 #include <qbarset.h>
6 #include <qbarcategory.h>
6 #include <qbarcategory.h>
7
7
8 #include "custombarset.h"
9
10 QTCOMMERCIALCHART_USE_NAMESPACE
8 QTCOMMERCIALCHART_USE_NAMESPACE
11
9
12 int main(int argc, char *argv[])
10 int main(int argc, char *argv[])
13 {
11 {
14 QApplication a(argc, argv);
12 QApplication a(argc, argv);
15 QMainWindow window;
13 QMainWindow window;
16
14
17 QBarCategory *category = new QBarCategory;
15 QBarCategory *category = new QBarCategory;
18 *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
16 *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
19
17
20 QBarChartSeries* series= new QBarChartSeries(category);
18 QBarChartSeries* series= new QBarChartSeries(category);
21
19
22 // We use custom set, which connects some signals. Could use QBarSet here if we don't need signals
20 // We use custom set, which connects some signals. Could use QBarSet here if we don't need signals
23 CustomBarSet *set0 = new CustomBarSet("Bub");
21 QBarSet *set0 = new QBarSet("Bub");
24 CustomBarSet *set1 = new CustomBarSet("Bob");
22 QBarSet *set1 = new QBarSet("Bob");
25 CustomBarSet *set2 = new CustomBarSet("Guybrush");
23 QBarSet *set2 = new QBarSet("Guybrush");
26 CustomBarSet *set3 = new CustomBarSet("Larry");
24 QBarSet *set3 = new QBarSet("Larry");
27 CustomBarSet *set4 = new CustomBarSet("Zak");
25 QBarSet *set4 = new QBarSet("Zak");
28
26
29 // Create some test data to chart
27 // Create some test data to chart
30 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
28 *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;
29 *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;
30 *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;
31 *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;
32 *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6;
35
33
36 series->addBarSet(set0);
34 series->addBarSet(set0);
37 series->addBarSet(set1);
35 series->addBarSet(set1);
38 series->addBarSet(set2);
36 series->addBarSet(set2);
39 series->addBarSet(set3);
37 series->addBarSet(set3);
40 series->addBarSet(set4);
38 series->addBarSet(set4);
41
39
40 series->enableToolTip(true);
41 series->enableFloatingValues(true);
42
42 QChartView* chartView = new QChartView(&window);
43 QChartView* chartView = new QChartView(&window);
43 chartView->addSeries(series);
44 chartView->addSeries(series);
44 chartView->setChartTitle("simple stacked barchart");
45 chartView->setChartTitle("simple stacked barchart");
45 chartView->setChartTheme(QChart::ChartThemeIcy);
46 chartView->setChartTheme(QChart::ChartThemeIcy);
46
47
47 window.setCentralWidget(chartView);
48 window.setCentralWidget(chartView);
48 window.resize(400, 300);
49 window.resize(400, 300);
49 window.show();
50 window.show();
50
51
51 return a.exec();
52 return a.exec();
52 }
53 }
53
54
@@ -1,53 +1,54
1 #include <QApplication>
1 #include <QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <QStandardItemModel>
3 #include <QStandardItemModel>
4 #include <qpercentbarchartseries.h>
4 #include <qpercentbarchartseries.h>
5 #include <qbarcategory.h>
5 #include <qbarcategory.h>
6 #include <qchartview.h>
6 #include <qchartview.h>
7
7 #include <qbarset.h>
8 #include "custombarset.h"
9
8
10 QTCOMMERCIALCHART_USE_NAMESPACE
9 QTCOMMERCIALCHART_USE_NAMESPACE
11
10
12 int main(int argc, char *argv[])
11 int main(int argc, char *argv[])
13 {
12 {
14 QApplication a(argc, argv);
13 QApplication a(argc, argv);
15 QMainWindow window;
14 QMainWindow window;
16
15
17 QBarCategory *category = new QBarCategory;
16 QBarCategory *category = new QBarCategory;
18 *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
17 *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
19
18
20 QPercentBarChartSeries* series = new QPercentBarChartSeries(category);
19 QPercentBarChartSeries* series = new QPercentBarChartSeries(category);
21
20
22 // We use custom set, which connects some signals. Could use QBarSet here if we don't need signals
21 QBarSet *set0 = new QBarSet("Bub");
23 CustomBarSet *set0 = new CustomBarSet("Bub");
22 QBarSet *set1 = new QBarSet("Bob");
24 CustomBarSet *set1 = new CustomBarSet("Bob");
23 QBarSet *set2 = new QBarSet("Guybrush");
25 CustomBarSet *set2 = new CustomBarSet("Guybrush");
24 QBarSet *set3 = new QBarSet("Larry");
26 CustomBarSet *set3 = new CustomBarSet("Larry");
25 QBarSet *set4 = new QBarSet("Zak");
27 CustomBarSet *set4 = new CustomBarSet("Zak");
28
26
29 // Create some test data to chart
27 // Create some test data to chart
30 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
28 *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;
29 *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;
30 *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;
31 *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;
32 *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6;
35
33
36 series->addBarSet(set0);
34 series->addBarSet(set0);
37 series->addBarSet(set1);
35 series->addBarSet(set1);
38 series->addBarSet(set2);
36 series->addBarSet(set2);
39 series->addBarSet(set3);
37 series->addBarSet(set3);
40 series->addBarSet(set4);
38 series->addBarSet(set4);
41
39
40 series->enableToolTip(true);
41 series->enableFloatingValues(true);
42
42 QChartView* chartView = new QChartView(&window);
43 QChartView* chartView = new QChartView(&window);
43 chartView->addSeries(series);
44 chartView->addSeries(series);
44 chartView->setChartTitle("simple percent barchart");
45 chartView->setChartTitle("simple percent barchart");
45 chartView->setChartTheme(QChart::ChartThemeIcy);
46 chartView->setChartTheme(QChart::ChartThemeIcy);
46
47
47 window.setCentralWidget(chartView);
48 window.setCentralWidget(chartView);
48 window.resize(400, 300);
49 window.resize(400, 300);
49 window.show();
50 window.show();
50
51
51 return a.exec();
52 return a.exec();
52 }
53 }
53
54
@@ -1,12 +1,10
1 !include( ../example.pri ) {
1 !include( ../example.pri ) {
2 error( "Couldn't find the example.pri file!" )
2 error( "Couldn't find the example.pri file!" )
3 }
3 }
4
4
5 TARGET = percentbarchart
5 TARGET = percentbarchart
6 SOURCES += main.cpp \
6 SOURCES += main.cpp \
7 chartwidget.cpp \
7 chartwidget.cpp
8 custombarset.cpp
9 HEADERS += \
8 HEADERS += \
10 chartwidget.h \
9 chartwidget.h
11 custombarset.h
12
10
@@ -1,53 +1,53
1 #include <QApplication>
1 #include <QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <qchartview.h>
3 #include <qchartview.h>
4 #include <qstackedbarchartseries.h>
4 #include <qstackedbarchartseries.h>
5 #include <qbarset.h>
5 #include <qbarset.h>
6 #include <qbarcategory.h>
6 #include <qbarcategory.h>
7
7
8 #include "custombarset.h"
9
10 QTCOMMERCIALCHART_USE_NAMESPACE
8 QTCOMMERCIALCHART_USE_NAMESPACE
11
9
12 int main(int argc, char *argv[])
10 int main(int argc, char *argv[])
13 {
11 {
14 QApplication a(argc, argv);
12 QApplication a(argc, argv);
15 QMainWindow window;
13 QMainWindow window;
16
14
17 QBarCategory *category = new QBarCategory;
15 QBarCategory *category = new QBarCategory;
18 *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
16 *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
19
17
20 QStackedBarChartSeries* series = new QStackedBarChartSeries(category);
18 QStackedBarChartSeries* series = new QStackedBarChartSeries(category);
21
19
22 // We use custom set, which connects some signals. Could use QBarSet here if we don't need signals
20 QBarSet *set0 = new QBarSet("Bub");
23 CustomBarSet *set0 = new CustomBarSet("Bub");
21 QBarSet *set1 = new QBarSet("Bob");
24 CustomBarSet *set1 = new CustomBarSet("Bob");
22 QBarSet *set2 = new QBarSet("Guybrush");
25 CustomBarSet *set2 = new CustomBarSet("Guybrush");
23 QBarSet *set3 = new QBarSet("Larry");
26 CustomBarSet *set3 = new CustomBarSet("Larry");
24 QBarSet *set4 = new QBarSet("Zak");
27 CustomBarSet *set4 = new CustomBarSet("Zak");
28
25
29 // Create some test data to chart
26 // Create some test data to chart
30 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
27 *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;
28 *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;
29 *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;
30 *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;
31 *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6;
35
32
36 series->addBarSet(set0);
33 series->addBarSet(set0);
37 series->addBarSet(set1);
34 series->addBarSet(set1);
38 series->addBarSet(set2);
35 series->addBarSet(set2);
39 series->addBarSet(set3);
36 series->addBarSet(set3);
40 series->addBarSet(set4);
37 series->addBarSet(set4);
41
38
39 series->enableToolTip(true);
40 series->enableFloatingValues(true);
41
42 QChartView* chartView = new QChartView(&window);
42 QChartView* chartView = new QChartView(&window);
43 chartView->addSeries(series);
43 chartView->addSeries(series);
44 chartView->setChartTitle("simple stacked barchart");
44 chartView->setChartTitle("simple stacked barchart");
45 chartView->setChartTheme(QChart::ChartThemeIcy);
45 chartView->setChartTheme(QChart::ChartThemeIcy);
46
46
47 window.setCentralWidget(chartView);
47 window.setCentralWidget(chartView);
48 window.resize(400, 300);
48 window.resize(400, 300);
49 window.show();
49 window.show();
50
50
51 return a.exec();
51 return a.exec();
52 }
52 }
53
53
@@ -1,11 +1,9
1 !include( ../example.pri ) {
1 !include( ../example.pri ) {
2 error( "Couldn't find the example.pri file!" )
2 error( "Couldn't find the example.pri file!" )
3 }
3 }
4 TARGET = stackedbarchart
4 TARGET = stackedbarchart
5 SOURCES += main.cpp \
5 SOURCES += main.cpp \
6 chartwidget.cpp \
6 chartwidget.cpp
7 custombarset.cpp
8 HEADERS += \
7 HEADERS += \
9 chartwidget.h \
8 chartwidget.h
10 custombarset.h
11
9
@@ -1,171 +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 "qbarchartseries.h"
7 #include "qbarchartseries.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(QBarChartSeries *series, QGraphicsItem *parent)
13 BarPresenterBase::BarPresenterBase(QBarChartSeries *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 ,mSeries(series)
19 ,mSeries(series)
19 {
20 {
20 connect(series,SIGNAL(floatingValuesEnabled(bool)),this,SLOT(enableFloatingValues(bool)));
21 connect(series,SIGNAL(toolTipEnabled(bool)),this,SLOT(enableToolTip(bool)));
22 connect(series,SIGNAL(separatorsEnabled(bool)),this,SLOT(enableSeparators(bool)));
23 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)));
24 dataChanged();
23 dataChanged();
25 }
24 }
26
25
27 BarPresenterBase::~BarPresenterBase()
26 BarPresenterBase::~BarPresenterBase()
28 {
27 {
29 disconnect(this,SLOT(enableFloatingValues(bool)));
30 disconnect(this,SLOT(enableToolTip(bool)));
31 disconnect(this,SLOT(enableSeparators(bool)));
32 disconnect(this,SLOT(showToolTip(QPoint,QString)));
28 disconnect(this,SLOT(showToolTip(QPoint,QString)));
33 delete mSeries;
29 delete mSeries;
34 }
30 }
35
31
36 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
32 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
37 {
33 {
38 if (!mLayoutSet) {
34 if (!mLayoutSet) {
39 qDebug() << "BarPresenterBase::paint called without layout set. Aborting.";
35 qDebug() << "BarPresenterBase::paint called without layout set. Aborting.";
40 return;
36 return;
41 }
37 }
42 // if (mLayoutDirty) {
38 // if (mLayoutDirty) {
43 // Layout or data has changed. Need to redraw.
39 // Layout or data has changed. Need to redraw.
44 foreach(QGraphicsItem* i, childItems()) {
40 foreach(QGraphicsItem* i, childItems()) {
45 i->paint(painter,option,widget);
41 i->paint(painter,option,widget);
46 }
42 }
47 // }
43 // }
48 }
44 }
49
45
50 QRectF BarPresenterBase::boundingRect() const
46 QRectF BarPresenterBase::boundingRect() const
51 {
47 {
52 return QRectF(0,0,mWidth,mHeight);
48 return QRectF(0,0,mWidth,mHeight);
53 }
49 }
54
50
55 void BarPresenterBase::setBarWidth( int w )
51 void BarPresenterBase::setBarWidth( int w )
56 {
52 {
57 mBarDefaultWidth = w;
53 mBarDefaultWidth = w;
58 }
54 }
59
55
60 void BarPresenterBase::dataChanged()
56 void BarPresenterBase::dataChanged()
61 {
57 {
62 // 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?
63 qDebug() << "datachanged";
59 qDebug() << "datachanged";
64 // Delete old bars
60 // Delete old bars
65 foreach (QGraphicsItem* item, childItems()) {
61 foreach (QGraphicsItem* item, childItems()) {
66 delete item;
62 delete item;
67 }
63 }
68
64
69 mBars.clear();
65 mBars.clear();
70 mLabels.clear();
66 mLabels.clear();
71 mSeparators.clear();
67 mSeparators.clear();
72 mFloatingValues.clear();
68 mFloatingValues.clear();
73
69
74 // Create new graphic items for bars
70 // Create new graphic items for bars
75 for (int c=0; c<mSeries->countCategories(); c++) {
71 for (int c=0; c<mSeries->countCategories(); c++) {
76 for (int s=0; s<mSeries->countSets(); s++) {
72 for (int s=0; s<mSeries->countSets(); s++) {
77 QBarSet *set = mSeries->setAt(s);
73 QBarSet *set = mSeries->setAt(s);
78 Bar *bar = new Bar(this);
74 Bar *bar = new Bar(this);
79 childItems().append(bar);
75 childItems().append(bar);
80 mBars.append(bar);
76 mBars.append(bar);
81 connect(bar,SIGNAL(clicked()),set,SLOT(barClicked()));
77 connect(bar,SIGNAL(clicked()),set,SLOT(barClicked()));
82 // TODO: should the event be passed to set or not?
83 connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEntered(QPoint)));
78 connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEntered(QPoint)));
84 connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaved()));
79 connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaved()));
85 }
80 }
86 }
81 }
87
82
88 // Create labels
83 // Create labels
89 int count = mSeries->countCategories();
84 int count = mSeries->countCategories();
90 for (int i=0; i<count; i++) {
85 for (int i=0; i<count; i++) {
91 BarLabel* label = new BarLabel(this);
86 BarLabel* label = new BarLabel(this);
92 label->set(mSeries->label(i));
87 label->set(mSeries->label(i));
93 childItems().append(label);
88 childItems().append(label);
94 mLabels.append(label);
89 mLabels.append(label);
95 }
90 }
96
91
97 // Create separators
92 // Create separators
98 count = mSeries->countCategories() - 1; // There is one less separator than columns
93 count = mSeries->countCategories() - 1; // There is one less separator than columns
99 for (int i=0; i<count; i++) {
94 for (int i=0; i<count; i++) {
100 Separator* sep = new Separator(this);
95 Separator* sep = new Separator(this);
101 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);
102 childItems().append(sep);
98 childItems().append(sep);
103 mSeparators.append(sep);
99 mSeparators.append(sep);
104 }
100 }
105
101
106 // Create floating values
102 // Create floating values
107 for (int category=0; category<mSeries->countCategories(); category++) {
103 for (int category=0; category<mSeries->countCategories(); category++) {
108 for (int s=0; s<mSeries->countSets(); s++) {
104 for (int s=0; s<mSeries->countSets(); s++) {
109 QBarSet *set = mSeries->setAt(s);
105 QBarSet *set = mSeries->setAt(s);
110 BarValue *value = new BarValue(*set, this);
106 BarValue *value = new BarValue(*set, this);
111 childItems().append(value);
107 childItems().append(value);
112 mFloatingValues.append(value);
108 mFloatingValues.append(value);
113 connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible()));
109 connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible()));
114 }
110 }
115 }
111 }
116
112
117 // TODO: if (autolayout) { layoutChanged() } or something
113 // TODO: if (autolayout) { layoutChanged() } or something
118 mLayoutDirty = true;
114 mLayoutDirty = true;
119 }
115 }
120
116
121 //handlers
117 //handlers
122
118
123 void BarPresenterBase::handleModelChanged(int index)
119 void BarPresenterBase::handleModelChanged(int index)
124 {
120 {
125 // qDebug() << "BarPresenterBase::handleModelChanged" << index;
121 // qDebug() << "BarPresenterBase::handleModelChanged" << index;
126 dataChanged();
122 dataChanged();
127 }
123 }
128
124
129 void BarPresenterBase::handleDomainChanged(const Domain& domain)
125 void BarPresenterBase::handleDomainChanged(const Domain& domain)
130 {
126 {
131 // qDebug() << "BarPresenterBase::handleDomainChanged";
127 // qDebug() << "BarPresenterBase::handleDomainChanged";
132 // TODO: Figure out the use case for this.
128 // TODO: Figure out the use case for this.
133 // Affects the size of visible item, so layout is changed.
129 // Affects the size of visible item, so layout is changed.
134 // layoutChanged();
130 // layoutChanged();
135 }
131 }
136
132
137 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
133 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
138 {
134 {
139 mWidth = rect.width();
135 mWidth = rect.width();
140 mHeight = rect.height();
136 mHeight = rect.height();
141 layoutChanged();
137 layoutChanged();
142 mLayoutSet = true;
138 mLayoutSet = true;
143 setPos(rect.topLeft());
139 setPos(rect.topLeft());
144 }
140 }
145
141
146 void BarPresenterBase::enableFloatingValues(bool enabled)
142 void BarPresenterBase::showToolTip(QPoint pos, QString tip)
147 {
148 mFloatingValuesEnabled = enabled;
149 }
150
151 void BarPresenterBase::enableToolTip(bool enabled)
152 {
143 {
153 mToolTipEnabled = enabled;
144 // TODO: cool tooltip instead of default
145 QToolTip::showText(pos,tip);
154 }
146 }
155
147
156 void BarPresenterBase::enableSeparators(bool enabled)
148 void BarPresenterBase::enableSeparators(bool enabled)
157 {
149 {
158 mSeparatorsEnabled = enabled;
150 for (int i=0; i<mSeparators.count(); i++) {
159 }
151 mSeparators.at(i)->setVisible(enabled);
160
161 void BarPresenterBase::showToolTip(QPoint pos, QString tip)
162 {
163 if (mToolTipEnabled) {
164 // TODO: cool tooltip instead of default
165 QToolTip::showText(pos,tip);
166 }
152 }
153 mSeparatorsEnabled = enabled;
167 }
154 }
168
155
169 #include "moc_barpresenterbase.cpp"
156 #include "moc_barpresenterbase.cpp"
170
157
171 QTCOMMERCIALCHART_END_NAMESPACE
158 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,76 +1,72
1 #ifndef BARPRESENTERBASE_H
1 #ifndef BARPRESENTERBASE_H
2 #define BARPRESENTERBASE_H
2 #define BARPRESENTERBASE_H
3
3
4 #include "chartitem_p.h"
4 #include "chartitem_p.h"
5 #include "qbarchartseries.h"
5 #include "qbarchartseries.h"
6 #include <QPen>
6 #include <QPen>
7 #include <QBrush>
7 #include <QBrush>
8 #include <QGraphicsItem>
8 #include <QGraphicsItem>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class Bar;
12 class Bar;
13 class BarLabel;
13 class BarLabel;
14 class Separator;
14 class Separator;
15 class BarValue;
15 class BarValue;
16
16
17 // Common implemantation of different presenters. Not to be instantiated.
17 // Common implemantation of different presenters. Not to be instantiated.
18 // TODO: combine this with BarPresenter and derive other presenters from it?
18 // TODO: combine this with BarPresenter and derive other presenters from it?
19 class BarPresenterBase : public QObject, public ChartItem
19 class BarPresenterBase : public QObject, public ChartItem
20 {
20 {
21 Q_OBJECT
21 Q_OBJECT
22 public:
22 public:
23 BarPresenterBase(QBarChartSeries *series, QGraphicsItem *parent = 0);
23 BarPresenterBase(QBarChartSeries *series, QGraphicsItem *parent = 0);
24 ~BarPresenterBase();
24 ~BarPresenterBase();
25
25
26 public:
26 public:
27 // From QGraphicsItem
27 // From QGraphicsItem
28 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
28 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
29 QRectF boundingRect() const;
29 QRectF boundingRect() const;
30
30
31 // TODO: these may change with layout awarness.
31 // TODO: these may change with layout awarness.
32 void setBarWidth( int w );
32 void setBarWidth( int w );
33
33
34 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
34 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
35 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
35 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
36 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
36 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
37
37
38 protected slots:
38 protected slots:
39 void handleModelChanged(int index);
39 void handleModelChanged(int index);
40 void handleDomainChanged(const Domain& domain);
40 void handleDomainChanged(const Domain& domain);
41 void handleGeometryChanged(const QRectF& size);
41 void handleGeometryChanged(const QRectF& size);
42
42
43 // Internal slots
43 // Internal slots
44 void enableFloatingValues(bool enabled=true); // enables floating values on top of bars
45 void enableToolTip(bool enabled=true); // enables tooltips
46 void enableSeparators(bool enabled=true); // enables separators between categories
47 void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled)
44 void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled)
45 void enableSeparators(bool enabled);
48
46
49 protected:
47 protected:
50
48
51 // TODO: consider these.
49 // TODO: consider these.
52 int mHeight; // Layout spesific
50 int mHeight; // Layout spesific
53 int mWidth;
51 int mWidth;
54 int mBarDefaultWidth;
52 int mBarDefaultWidth;
55
53
56 bool mLayoutSet; // True, if component has been laid out.
54 bool mLayoutSet; // True, if component has been laid out.
57 bool mLayoutDirty;
55 bool mLayoutDirty;
58
56
59 bool mFloatingValuesEnabled;
60 bool mToolTipEnabled;
61 bool mSeparatorsEnabled;
57 bool mSeparatorsEnabled;
62
58
63 // Owned
59 // Owned
64 QBarChartSeries* mSeries;
60 QBarChartSeries* mSeries;
65
61
66 // Not owned.
62 // Not owned.
67 QList<Bar*> mBars;
63 QList<Bar*> mBars;
68 QList<BarLabel*> mLabels;
64 QList<BarLabel*> mLabels;
69 QList<Separator*> mSeparators;
65 QList<Separator*> mSeparators;
70 QList<BarValue*> mFloatingValues;
66 QList<BarValue*> mFloatingValues;
71
67
72 };
68 };
73
69
74 QTCOMMERCIALCHART_END_NAMESPACE
70 QTCOMMERCIALCHART_END_NAMESPACE
75
71
76 #endif // BARPRESENTERBASE_H
72 #endif // BARPRESENTERBASE_H
@@ -1,116 +1,128
1 #include <QDebug>
1 #include <QDebug>
2 #include "qbarchartseries.h"
2 #include "qbarchartseries.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 QBarChartSeries::QBarChartSeries(QBarCategory *category, QObject *parent)
10 QBarChartSeries::QBarChartSeries(QBarCategory *category, QObject *parent)
11 : QChartSeries(parent)
11 : QChartSeries(parent)
12 ,mModel(new BarChartModel(category, this))
12 ,mModel(new BarChartModel(category, this))
13 {
13 {
14 }
14 }
15
15
16 void QBarChartSeries::addBarSet(QBarSet *set)
16 void QBarChartSeries::addBarSet(QBarSet *set)
17 {
17 {
18 connect(this,SIGNAL(floatingValuesEnabled(bool)),set,SLOT(enableFloatingValues(bool)));
19 connect(this,SIGNAL(separatorsEnabled(bool)),set,SLOT(enableSeparators(bool)));
20 connect(this,SIGNAL(toolTipEnabled(bool)),set,SLOT(enableToolTip(bool)));
21 connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
22 mModel->addBarSet(set);
18 mModel->addBarSet(set);
23 }
19 }
24
20
25 void QBarChartSeries::removeBarSet(QBarSet *set)
21 void QBarChartSeries::removeBarSet(QBarSet *set)
26 {
22 {
27 disconnect(set,SLOT(enableFloatingValues(bool)));
28 disconnect(set,SLOT(enableSeparators(bool)));
29 disconnect(set,SLOT(enableToolTip(bool)));
30 mModel->removeBarSet(set);
23 mModel->removeBarSet(set);
31 }
24 }
32
25
33 int QBarChartSeries::countSets()
26 int QBarChartSeries::countSets()
34 {
27 {
35 return mModel->countSets();
28 return mModel->countSets();
36 }
29 }
37
30
38 QBarSet* QBarChartSeries::nextSet(bool getFirst)
31 QBarSet* QBarChartSeries::nextSet(bool getFirst)
39 {
32 {
40 return mModel->nextSet(getFirst);
33 return mModel->nextSet(getFirst);
41 }
34 }
42
35
43 QBarSet* QBarChartSeries::setAt(int index)
36 QBarSet* QBarChartSeries::setAt(int index)
44 {
37 {
45 return mModel->setAt(index);
38 return mModel->setAt(index);
46 }
39 }
47
40
48 QList<QString> QBarChartSeries::legend()
41 QList<QString> QBarChartSeries::legend()
49 {
42 {
50 return mModel->legend();
43 return mModel->legend();
51 }
44 }
52
45
53 QString QBarChartSeries::label(int category)
46 QString QBarChartSeries::label(int category)
54 {
47 {
55 return mModel->label(category);
48 return mModel->label(category);
56 }
49 }
57
50
58 void QBarChartSeries::enableFloatingValues(bool enabled)
51 void QBarChartSeries::enableFloatingValues(bool enabled)
59 {
52 {
60 emit floatingValuesEnabled(enabled);
53 if (enabled) {
54 for (int i=0; i<mModel->countSets(); i++) {
55 QBarSet *set = mModel->setAt(i);
56 connect(set,SIGNAL(clicked()),set,SIGNAL(toggleFloatingValues()));
57 }
58 } else {
59 for (int i=0; i<mModel->countSets(); i++) {
60 QBarSet *set = mModel->setAt(i);
61 disconnect(set,SIGNAL(clicked()),set,SIGNAL(toggleFloatingValues()));
62 }
63 }
61 }
64 }
62
65
63 void QBarChartSeries::enableToolTip(bool enabled)
66 void QBarChartSeries::enableToolTip(bool enabled)
64 {
67 {
65 emit toolTipEnabled(enabled);
68 if (enabled) {
69 for (int i=0; i<mModel->countSets(); i++) {
70 QBarSet *set = mModel->setAt(i);
71 connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
72 }
73 } else {
74 for (int i=0; i<mModel->countSets(); i++) {
75 QBarSet *set = mModel->setAt(i);
76 disconnect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
77 }
78 }
66 }
79 }
67
80
68 void QBarChartSeries::enableSeparators(bool enabled)
81 void QBarChartSeries::enableSeparators(bool enabled)
69 {
82 {
70 emit separatorsEnabled(enabled);
83 emit separatorsEnabled(enabled);
71 }
84 }
72
85
73 int QBarChartSeries::countCategories()
86 int QBarChartSeries::countCategories()
74 {
87 {
75 return mModel->countCategories();
88 return mModel->countCategories();
76 }
89 }
77
90
78 qreal QBarChartSeries::min()
91 qreal QBarChartSeries::min()
79 {
92 {
80 return mModel->min();
93 return mModel->min();
81 }
94 }
82
95
83 qreal QBarChartSeries::max()
96 qreal QBarChartSeries::max()
84 {
97 {
85 return mModel->max();
98 return mModel->max();
86 }
99 }
87
100
88 qreal QBarChartSeries::valueAt(int set, int category)
101 qreal QBarChartSeries::valueAt(int set, int category)
89 {
102 {
90 return mModel->valueAt(set,category);
103 return mModel->valueAt(set,category);
91 }
104 }
92
105
93 qreal QBarChartSeries::percentageAt(int set, int category)
106 qreal QBarChartSeries::percentageAt(int set, int category)
94 {
107 {
95 return mModel->percentageAt(set,category);
108 return mModel->percentageAt(set,category);
96 }
109 }
97
110
98 qreal QBarChartSeries::categorySum(int category)
111 qreal QBarChartSeries::categorySum(int category)
99 {
112 {
100 return mModel->categorySum(category);
113 return mModel->categorySum(category);
101 }
114 }
102
115
103 qreal QBarChartSeries::maxCategorySum()
116 qreal QBarChartSeries::maxCategorySum()
104 {
117 {
105 return mModel->maxCategorySum();
118 return mModel->maxCategorySum();
106 }
119 }
107
120
108 BarChartModel& QBarChartSeries::model()
121 BarChartModel& QBarChartSeries::model()
109 {
122 {
110 return *mModel;
123 return *mModel;
111 }
124 }
112
125
113
114 #include "moc_qbarchartseries.cpp"
126 #include "moc_qbarchartseries.cpp"
115
127
116 QTCOMMERCIALCHART_END_NAMESPACE
128 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,66 +1,66
1 #ifndef BARCHARTSERIES_H
1 #ifndef BARCHARTSERIES_H
2 #define BARCHARTSERIES_H
2 #define BARCHARTSERIES_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 QBarChartSeries : public QChartSeries
13 class QTCOMMERCIALCHART_EXPORT QBarChartSeries : public QChartSeries
14 {
14 {
15 Q_OBJECT
15 Q_OBJECT
16 public:
16 public:
17 QBarChartSeries(QBarCategory *category, QObject* parent=0);
17 QBarChartSeries(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 QBarSet* nextSet(bool getFirst=false); // Returns first set, if called with true
24 QBarSet* nextSet(bool getFirst=false); // Returns first set, if called with true
25 QBarSet *setAt(int index);
25 QBarSet *setAt(int index);
26
26
27 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)
28 QString label(int category);
28 QString label(int category);
29
29
30 public Q_SLOTS:
31 // Disabled by default. Call these to change behavior.
32 void enableFloatingValues(bool enabled=true); // enables floating values on top of bars
33 void enableToolTip(bool enabled=true); // enables tooltips
34 void enableSeparators(bool enabled=true); // enables separators between categories
35
36 public:
30 public:
37 // TODO: Functions below this are not part of api and will be moved
31 // TODO: Functions below this are not part of api and will be moved
38 // to private implementation, when we start using it (not part of api)
32 // to private implementation, when we start using it
33 // TODO: TO PIMPL --->
39 int countCategories();
34 int countCategories();
40 qreal min();
35 qreal min();
41 qreal max();
36 qreal max();
42 qreal valueAt(int set, int category);
37 qreal valueAt(int set, int category);
43 qreal percentageAt(int set, int category);
38 qreal percentageAt(int set, int category);
44
45 qreal categorySum(int category);
39 qreal categorySum(int category);
46 qreal maxCategorySum();
40 qreal maxCategorySum();
47
48 BarChartModel& model();
41 BarChartModel& model();
42 // <--- TO PIMPL
49
43
50 signals:
44 signals:
51 void changed(int index);
45 void changed(int index);
52
46
53 // TODO: internal signals, these to private implementation.
47 // TODO: internal signals, these to private implementation.
48 // TODO: TO PIMPL --->
54 void floatingValuesEnabled(bool enabled);
49 void floatingValuesEnabled(bool enabled);
55 void toolTipEnabled(bool enabled);
50 void toolTipEnabled(bool enabled);
56 void separatorsEnabled(bool enabled);
51 void separatorsEnabled(bool enabled);
57 void showToolTip(QPoint pos, QString tip);
52 void showToolTip(QPoint pos, QString tip);
53 // <--- TO PIMPL
54
55 public Q_SLOTS:
56 void enableFloatingValues(bool enabled=true); // enables floating values on top of bars
57 void enableToolTip(bool enabled=true); // enables tooltips
58 void enableSeparators(bool enabled=true); // enables separators between categories
58
59
59 protected:
60 protected:
60 BarChartModel* mModel;
61 BarChartModel* mModel;
61
62 };
62 };
63
63
64 QTCOMMERCIALCHART_END_NAMESPACE
64 QTCOMMERCIALCHART_END_NAMESPACE
65
65
66 #endif // BARCHARTSERIES_H
66 #endif // BARCHARTSERIES_H
@@ -1,111 +1,88
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 QBarSet::QBarSet(QString name, QObject *parent)
7 QBarSet::QBarSet(QString name, QObject *parent)
8 : QObject(parent)
8 : QObject(parent)
9 ,mName(name)
9 ,mName(name)
10 ,mToolTipEnabled(true) // TODO: these 2 as false by default, when implementation is ready
11 ,mFloatingValuesEnabled(true)
12 {
10 {
13 }
11 }
14
12
15 void QBarSet::setName(QString name)
13 void QBarSet::setName(QString name)
16 {
14 {
17 mName = name;
15 mName = name;
18 }
16 }
19 QString QBarSet::name()
17 QString QBarSet::name()
20 {
18 {
21 return mName;
19 return mName;
22 }
20 }
23
21
24 QBarSet& QBarSet::operator << (const qreal &value)
22 QBarSet& QBarSet::operator << (const qreal &value)
25 {
23 {
26 mValues.append(value);
24 mValues.append(value);
27 return *this;
25 return *this;
28 }
26 }
29
27
30 int QBarSet::count()
28 int QBarSet::count()
31 {
29 {
32 return mValues.count();
30 return mValues.count();
33 }
31 }
34
32
35 qreal QBarSet::valueAt(int index)
33 qreal QBarSet::valueAt(int index)
36 {
34 {
37 return mValues.at(index);
35 return mValues.at(index);
38 }
36 }
39
37
40 void QBarSet::setValue(int index, qreal value)
38 void QBarSet::setValue(int index, qreal value)
41 {
39 {
42 mValues.replace(index,value);
40 mValues.replace(index,value);
43 }
41 }
44
42
45 void QBarSet::setPen(const QPen& pen)
43 void QBarSet::setPen(const QPen& pen)
46 {
44 {
47 mPen = pen;
45 mPen = pen;
48 }
46 }
49
47
50 const QPen& QBarSet::pen() const
48 const QPen& QBarSet::pen() const
51 {
49 {
52 return mPen;
50 return mPen;
53 }
51 }
54
52
55 void QBarSet::setBrush(const QBrush& brush)
53 void QBarSet::setBrush(const QBrush& brush)
56 {
54 {
57 mBrush = brush;
55 mBrush = brush;
58 }
56 }
59
57
60 const QBrush& QBarSet::brush() const
58 const QBrush& QBarSet::brush() const
61 {
59 {
62 return mBrush;
60 return mBrush;
63 }
61 }
64
62
65 void QBarSet::enableFloatingValues(bool enabled)
66 {
67 qDebug() << "QBarSet::enableFloatingValues";
68 mFloatingValuesEnabled = enabled;
69 }
70
71 void QBarSet::enableToolTip(bool enabled)
72 {
73 qDebug() << "QBarSet::enableToolTip";
74 mToolTipEnabled = enabled;
75 }
76
77 void QBarSet::enableSeparators(bool enabled)
78 {
79 qDebug() << "QBarSet::enableSeparators";
80 mSeparatorsEnabled = enabled;
81 }
82
83 void QBarSet::barClicked()
63 void QBarSet::barClicked()
84 {
64 {
85 // qDebug() << "QBarset::barClicked" << this;
65 // qDebug() << "QBarset::barClicked" << this;
86 // Some bar of this set has been clicked
66 // Some bar of this set has been clicked
87 // TODO: What happens then?
67 // TODO: What happens then?
88 emit clicked(); // Notify that set has been clicked
68 emit clicked(); // Notify that set has been clicked
89 }
69 }
90
70
91 void QBarSet::barHoverEntered(QPoint pos)
71 void QBarSet::barHoverEntered(QPoint pos)
92 {
72 {
93 if (mToolTipEnabled) {
73 emit showToolTip(pos, mName);
94 emit showToolTip(pos, mName);
95 }
96 // Emit signal to user of charts
97 emit hoverEnter(pos);
74 emit hoverEnter(pos);
98 }
75 }
99
76
100 void QBarSet::barHoverLeaved()
77 void QBarSet::barHoverLeaved()
101 {
78 {
102 // qDebug() << "QBarset::barHoverLeaved" << this;
79 // qDebug() << "QBarset::barHoverLeaved" << this;
103 // if (mToolTipEnabled) {
80 // if (mToolTipEnabled) {
104 // TODO: do what?
81 // TODO: do what?
105 // }
82 // }
106 // Emit signal to user of charts
83 // Emit signal to user of charts
107 emit hoverLeave();
84 emit hoverLeave();
108 }
85 }
109
86
110 #include "moc_qbarset.cpp"
87 #include "moc_qbarset.cpp"
111 QTCOMMERCIALCHART_END_NAMESPACE
88 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,63 +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(const QPen& pen);
25 const QPen& pen() const;
25 const QPen& pen() const;
26
26
27 void setBrush(const QBrush& brush);
27 void setBrush(const QBrush& brush);
28 const QBrush& brush() const;
28 const QBrush& brush() const;
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 void toggleFloatingValues(); // Private signal, TODO: move to private impl
35 void showToolTip(QPoint pos, QString tip); // Private signal, TODO: move to private impl
36
34
37 public Q_SLOTS:
35 // TODO: Expose this to user or not?
38 void enableFloatingValues(bool enabled); // enables floating values on top of bars
36 // TODO: TO PIMPL --->
39 void enableToolTip(bool enabled); // enables tooltips
37 void toggleFloatingValues();
40 void enableSeparators(bool enabled); // enables separators between categories
38 void showToolTip(QPoint pos, QString tip); // Private signal
39 // <--- TO PIMPL
41
40
42 // TODO: these slots belong to private implementation.
41 public Q_SLOTS:
43 // These are for single bars to notify set about internal events
42 // These are for internal communication
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
48
48 private:
49 private:
49
50
50 QString mName;
51 QString mName;
51 QList<qreal> mValues;
52 QList<qreal> mValues;
52 QPen mPen;
53 QPen mPen;
53 QBrush mBrush;
54 QBrush mBrush;
54
55
55 // TODO: to pimpl
56 bool mFloatingValuesEnabled;
57 bool mToolTipEnabled;
58 bool mSeparatorsEnabled;
59 };
56 };
60
57
61 QTCOMMERCIALCHART_END_NAMESPACE
58 QTCOMMERCIALCHART_END_NAMESPACE
62
59
63 #endif // QBARSET_H
60 #endif // QBARSET_H
@@ -1,44 +1,45
1 #include "separator_p.h"
1 #include "separator_p.h"
2 #include <QDebug>
2 #include <QDebug>
3 #include <QPainter>
3 #include <QPainter>
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 Separator::Separator(QGraphicsItem *parent)
7 Separator::Separator(QGraphicsItem *parent)
8 : ChartItem(parent)
8 : ChartItem(parent)
9 {
9 {
10 }
10 }
11
11
12 void Separator::setPos(qreal x, qreal y)
12 void Separator::setPos(qreal x, qreal y)
13 {
13 {
14 mXpos = x;
14 mXpos = x;
15 mYpos = y;
15 mYpos = y;
16 }
16 }
17
17
18 void Separator::setColor(QColor color)
18 void Separator::setColor(QColor color)
19 {
19 {
20 mColor = color;
20 mColor = color;
21 }
21 }
22
22
23 void Separator::setSize(const QSizeF &size)
23 void Separator::setSize(const QSizeF &size)
24 {
24 {
25 mWidth = size.width();
25 mWidth = size.width();
26 mHeight = size.height();
26 mHeight = size.height();
27 }
27 }
28
28
29 void Separator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
29 void Separator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
30 {
30 {
31 // TODO: real pen
31 if (isVisible()) {
32 QPen pen(mColor);
32 QPen pen(mColor);
33 painter->setPen(pen);
33 painter->setPen(pen);
34 painter->drawLine(mXpos,mYpos,mXpos,mHeight);
34 painter->drawLine(mXpos,mYpos,mXpos,mHeight);
35 }
35 }
36 }
36
37
37 QRectF Separator::boundingRect() const
38 QRectF Separator::boundingRect() const
38 {
39 {
39 QRectF r(mXpos,mYpos,mWidth,mHeight);
40 QRectF r(mXpos,mYpos,mWidth,mHeight);
40 return r;
41 return r;
41 }
42 }
42
43
43
44
44 QTCOMMERCIALCHART_END_NAMESPACE
45 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,369 +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 <qlinechartseries.h>
6 #include <qlinechartseries.h>
7 #include <qbarset.h>
7 #include <qbarset.h>
8 #include <qbarcategory.h>
8 #include <qbarcategory.h>
9 #include <qbarchartseries.h>
9 #include <qbarchartseries.h>
10 #include <qstackedbarchartseries.h>
10 #include <qstackedbarchartseries.h>
11 #include <qpercentbarchartseries.h>
11 #include <qpercentbarchartseries.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 QLineChartSeries *series = new QLineChartSeries();
222 QLineChartSeries *series = new QLineChartSeries();
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 QBarChartSeries* series = 0;
258 QBarChartSeries* series = 0;
259 if (seriesName == "Bar")
259 if (seriesName == "Bar")
260 series = new QBarChartSeries(category, this);
260 series = new QBarChartSeries(category, this);
261 else if (seriesName == "Stacked bar")
261 else if (seriesName == "Stacked bar")
262 series = new QStackedBarChartSeries(category, this);
262 series = new QStackedBarChartSeries(category, this);
263 else
263 else
264 series = new QPercentBarChartSeries(category, this);
264 series = new QPercentBarChartSeries(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();
275 series->enableToolTip();
276 series->enableSeparators(false);
274 m_chartWidget->addSeries(series);
277 m_chartWidget->addSeries(series);
275 setCurrentSeries(series);
278 setCurrentSeries(series);
276 }
279 }
277
280
278 // TODO: spline and area
281 // TODO: spline and area
279 }
282 }
280
283
281 void MainWidget::setCurrentSeries(QChartSeries *series)
284 void MainWidget::setCurrentSeries(QChartSeries *series)
282 {
285 {
283 if (series) {
286 if (series) {
284 m_currentSeries = series;
287 m_currentSeries = series;
285 switch (m_currentSeries->type()) {
288 switch (m_currentSeries->type()) {
286 case QChartSeries::SeriesTypeLine:
289 case QChartSeries::SeriesTypeLine:
287 break;
290 break;
288 case QChartSeries::SeriesTypeScatter:
291 case QChartSeries::SeriesTypeScatter:
289 break;
292 break;
290 case QChartSeries::SeriesTypePie:
293 case QChartSeries::SeriesTypePie:
291 break;
294 break;
292 case QChartSeries::SeriesTypeBar:
295 case QChartSeries::SeriesTypeBar:
293 qDebug() << "setCurrentSeries (bar)";
296 qDebug() << "setCurrentSeries (bar)";
294 break;
297 break;
295 case QChartSeries::SeriesTypeStackedBar:
298 case QChartSeries::SeriesTypeStackedBar:
296 qDebug() << "setCurrentSeries (Stackedbar)";
299 qDebug() << "setCurrentSeries (Stackedbar)";
297 break;
300 break;
298 case QChartSeries::SeriesTypePercentBar:
301 case QChartSeries::SeriesTypePercentBar:
299 qDebug() << "setCurrentSeries (Percentbar)";
302 qDebug() << "setCurrentSeries (Percentbar)";
300 break;
303 break;
301 default:
304 default:
302 Q_ASSERT(false);
305 Q_ASSERT(false);
303 break;
306 break;
304 }
307 }
305 }
308 }
306 }
309 }
307
310
308 void MainWidget::backgroundChanged(int itemIndex)
311 void MainWidget::backgroundChanged(int itemIndex)
309 {
312 {
310 qDebug() << "backgroundChanged: " << itemIndex;
313 qDebug() << "backgroundChanged: " << itemIndex;
311 }
314 }
312
315
313 void MainWidget::autoScaleChanged(int value)
316 void MainWidget::autoScaleChanged(int value)
314 {
317 {
315 if (value) {
318 if (value) {
316 // TODO: enable auto scaling
319 // TODO: enable auto scaling
317 } else {
320 } else {
318 // TODO: set scaling manually (and disable auto scaling)
321 // TODO: set scaling manually (and disable auto scaling)
319 }
322 }
320
323
321 m_xMinSpin->setEnabled(!value);
324 m_xMinSpin->setEnabled(!value);
322 m_xMaxSpin->setEnabled(!value);
325 m_xMaxSpin->setEnabled(!value);
323 m_yMinSpin->setEnabled(!value);
326 m_yMinSpin->setEnabled(!value);
324 m_yMaxSpin->setEnabled(!value);
327 m_yMaxSpin->setEnabled(!value);
325 }
328 }
326
329
327 void MainWidget::xMinChanged(int value)
330 void MainWidget::xMinChanged(int value)
328 {
331 {
329 qDebug() << "xMinChanged: " << value;
332 qDebug() << "xMinChanged: " << value;
330 }
333 }
331
334
332 void MainWidget::xMaxChanged(int value)
335 void MainWidget::xMaxChanged(int value)
333 {
336 {
334 qDebug() << "xMaxChanged: " << value;
337 qDebug() << "xMaxChanged: " << value;
335 }
338 }
336
339
337 void MainWidget::yMinChanged(int value)
340 void MainWidget::yMinChanged(int value)
338 {
341 {
339 qDebug() << "yMinChanged: " << value;
342 qDebug() << "yMinChanged: " << value;
340 }
343 }
341
344
342 void MainWidget::yMaxChanged(int value)
345 void MainWidget::yMaxChanged(int value)
343 {
346 {
344 qDebug() << "yMaxChanged: " << value;
347 qDebug() << "yMaxChanged: " << value;
345 }
348 }
346
349
347 void MainWidget::changeChartTheme(int themeIndex)
350 void MainWidget::changeChartTheme(int themeIndex)
348 {
351 {
349 qDebug() << "changeChartTheme: " << themeIndex;
352 qDebug() << "changeChartTheme: " << themeIndex;
350 m_chartWidget->setChartTheme((QChart::ChartTheme) themeIndex);
353 m_chartWidget->setChartTheme((QChart::ChartTheme) themeIndex);
351 //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.
352 QSize s = size();
355 QSize s = size();
353 s.setWidth(s.width()+1);
356 s.setWidth(s.width()+1);
354 resize(s);
357 resize(s);
355 }
358 }
356
359
357 void MainWidget::setPieSizeFactor(double size)
360 void MainWidget::setPieSizeFactor(double size)
358 {
361 {
359 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
362 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
360 if (pie)
363 if (pie)
361 pie->setSizeFactor(qreal(size));
364 pie->setSizeFactor(qreal(size));
362 }
365 }
363
366
364 void MainWidget::setPiePosition(int position)
367 void MainWidget::setPiePosition(int position)
365 {
368 {
366 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
369 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
367 if (pie)
370 if (pie)
368 pie->setPosition((QPieSeries::PiePosition) position);
371 pie->setPosition((QPieSeries::PiePosition) position);
369 }
372 }
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now