##// END OF EJS Templates
updated barchart examples. minor fixes
sauimone -
r276:d062a7f38647
parent child
Show More
@@ -0,0 +1,8
1 #include "custombarset.h"
2
3 CustomBarSet::CustomBarSet(QObject *parent) :
4 QBarSet(parent)
5 {
6 // Using signal to signal connection here.
7 connect(this,SIGNAL(clicked()),this,SIGNAL(toggleFloatingValues()));
8 }
@@ -0,0 +1,20
1 #ifndef CUSTOMBARSET_H
2 #define CUSTOMBARSET_H
3
4 #include <qbarset.h>
5
6 QTCOMMERCIALCHART_USE_NAMESPACE
7
8 class CustomBarSet : public QBarSet
9 {
10 Q_OBJECT
11 public:
12 explicit CustomBarSet(QObject *parent = 0);
13
14 signals:
15
16 public slots:
17
18 };
19
20 #endif // CUSTOMBARSET_H
@@ -0,0 +1,8
1 #include "custombarset.h"
2
3 CustomBarSet::CustomBarSet(QObject *parent) :
4 QBarSet(parent)
5 {
6 // Using signal to signal connection here.
7 connect(this,SIGNAL(clicked()),this,SIGNAL(toggleFloatingValues()));
8 }
@@ -0,0 +1,20
1 #ifndef CUSTOMBARSET_H
2 #define CUSTOMBARSET_H
3
4 #include <qbarset.h>
5
6 QTCOMMERCIALCHART_USE_NAMESPACE
7
8 class CustomBarSet : public QBarSet
9 {
10 Q_OBJECT
11 public:
12 explicit CustomBarSet(QObject *parent = 0);
13
14 signals:
15
16 public slots:
17
18 };
19
20 #endif // CUSTOMBARSET_H
@@ -0,0 +1,8
1 #include "custombarset.h"
2
3 CustomBarSet::CustomBarSet(QObject *parent) :
4 QBarSet(parent)
5 {
6 // Using signal to signal connection here.
7 connect(this,SIGNAL(clicked()),this,SIGNAL(toggleFloatingValues()));
8 }
@@ -0,0 +1,20
1 #ifndef CUSTOMBARSET_H
2 #define CUSTOMBARSET_H
3
4 #include <qbarset.h>
5
6 QTCOMMERCIALCHART_USE_NAMESPACE
7
8 class CustomBarSet : public QBarSet
9 {
10 Q_OBJECT
11 public:
12 explicit CustomBarSet(QObject *parent = 0);
13
14 signals:
15
16 public slots:
17
18 };
19
20 #endif // CUSTOMBARSET_H
@@ -1,10 +1,12
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
8 HEADERS += \
9 HEADERS += \
9 chartwidget.h
10 chartwidget.h \
11 custombarset.h
10
12
@@ -1,49 +1,53
1 #include <QApplication>
1 #include <QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <QStandardItemModel>
3 #include <qchartview.h>
4 #include <qbarchartseries.h>
4 #include <qbarchartseries.h>
5 #include <qbarcategory.h>
6 #include <qbarset.h>
5 #include <qbarset.h>
7 #include "chartwidget.h"
6 #include <qbarcategory.h>
7
8 #include "custombarset.h"
8
9
9 QTCOMMERCIALCHART_USE_NAMESPACE
10 QTCOMMERCIALCHART_USE_NAMESPACE
10
11
11 int main(int argc, char *argv[])
12 int main(int argc, char *argv[])
12 {
13 {
13 QApplication a(argc, argv);
14 QApplication a(argc, argv);
14 QMainWindow window;
15 QMainWindow window;
15
16
16 QBarCategory *category = new QBarCategory;
17 QBarCategory *category = new QBarCategory;
17 *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";
18
19
19 QBarChartSeries* series0 = new QBarChartSeries(category);
20 QBarChartSeries* series= new QBarChartSeries(category);
20
21
21 QBarSet *set0 = new QBarSet;
22 // We use custom set, which connects some signals. Could use QBarSet here if we don't need signals
22 QBarSet *set1 = new QBarSet;
23 CustomBarSet *set0 = new CustomBarSet;
23 QBarSet *set2 = new QBarSet;
24 CustomBarSet *set1 = new CustomBarSet;
24 QBarSet *set3 = new QBarSet;
25 CustomBarSet *set2 = new CustomBarSet;
25 QBarSet *set4 = new QBarSet;
26 CustomBarSet *set3 = new CustomBarSet;
27 CustomBarSet *set4 = new CustomBarSet;
26
28
27 // Create some test data to chart
29 // Create some test data to chart
28 *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;
29 *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;
30 *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;
31 *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;
32 *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;
33
35
34 series0->addBarSet(set0);
36 series->addBarSet(set0);
35 series0->addBarSet(set1);
37 series->addBarSet(set1);
36 series0->addBarSet(set2);
38 series->addBarSet(set2);
37 series0->addBarSet(set3);
39 series->addBarSet(set3);
38 series0->addBarSet(set4);
40 series->addBarSet(set4);
39
41
40 ChartWidget* chartWidget = new ChartWidget(&window);
42 QChartView* chartView = new QChartView(&window);
41 chartWidget->addSeries(series0);
43 chartView->addSeries(series);
44 chartView->setChartTitle("simple stacked barchart");
45 chartView->setChartTheme(QChart::ChartThemeIcy);
42
46
43 window.setCentralWidget(chartWidget);
47 window.setCentralWidget(chartView);
44 window.resize(400, 300);
48 window.resize(400, 300);
45 window.show();
49 window.show();
46
50
47 return a.exec();
51 return a.exec();
48 }
52 }
49
53
@@ -1,49 +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 "chartwidget.h"
6 #include <qbarcategory.h>
5 #include <qbarcategory.h>
7 #include <qbarset.h>
6 #include <qchartview.h>
7
8 //#include <qbarset.h>
9 #include "custombarset.h"
8
10
9 QTCOMMERCIALCHART_USE_NAMESPACE
11 QTCOMMERCIALCHART_USE_NAMESPACE
10
12
11 int main(int argc, char *argv[])
13 int main(int argc, char *argv[])
12 {
14 {
13 QApplication a(argc, argv);
15 QApplication a(argc, argv);
14 QMainWindow window;
16 QMainWindow window;
15
17
16 QBarCategory *category = new QBarCategory;
18 QBarCategory *category = new QBarCategory;
17 *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";
18
20
19 QPercentBarChartSeries* series0 = new QPercentBarChartSeries(category);
21 QPercentBarChartSeries* series = new QPercentBarChartSeries(category);
20
22
21 QBarSet *set0 = new QBarSet;
23 // We use custom set, which connects some signals. Could use QBarSet here if we don't need signals
22 QBarSet *set1 = new QBarSet;
24 CustomBarSet *set0 = new CustomBarSet;
23 QBarSet *set2 = new QBarSet;
25 CustomBarSet *set1 = new CustomBarSet;
24 QBarSet *set3 = new QBarSet;
26 CustomBarSet *set2 = new CustomBarSet;
25 QBarSet *set4 = new QBarSet;
27 CustomBarSet *set3 = new CustomBarSet;
28 CustomBarSet *set4 = new CustomBarSet;
26
29
27 // Create some test data to chart
30 // Create some test data to chart
28 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
31 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
29 *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2;
32 *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2;
30 *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5;
33 *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5;
31 *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7;
34 *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7;
32 *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6;
35 *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6;
33
36
34 series0->addBarSet(set0);
37 series->addBarSet(set0);
35 series0->addBarSet(set1);
38 series->addBarSet(set1);
36 series0->addBarSet(set2);
39 series->addBarSet(set2);
37 series0->addBarSet(set3);
40 series->addBarSet(set3);
38 series0->addBarSet(set4);
41 series->addBarSet(set4);
39
42
40 ChartWidget* chartWidget = new ChartWidget(&window);
43 QChartView* chartView = new QChartView(&window);
41 chartWidget->addSeries(series0);
44 chartView->addSeries(series);
45 chartView->setChartTitle("simple percent barchart");
46 chartView->setChartTheme(QChart::ChartThemeIcy);
42
47
43 window.setCentralWidget(chartWidget);
48 window.setCentralWidget(chartView);
44 window.resize(400, 300);
49 window.resize(400, 300);
45 window.show();
50 window.show();
46
51
47 return a.exec();
52 return a.exec();
48 }
53 }
49
54
@@ -1,10 +1,12
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
8 HEADERS += \
9 HEADERS += \
9 chartwidget.h
10 chartwidget.h \
11 custombarset.h
10
12
@@ -1,49 +1,53
1 #include <QApplication>
1 #include <QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <QStandardItemModel>
3 #include <qchartview.h>
4 #include <qstackedbarchartseries.h>
4 #include <qstackedbarchartseries.h>
5 #include "chartwidget.h"
6 #include <qbarset.h>
5 #include <qbarset.h>
7 #include <qbarcategory.h>
6 #include <qbarcategory.h>
8
7
8 #include "custombarset.h"
9
9 QTCOMMERCIALCHART_USE_NAMESPACE
10 QTCOMMERCIALCHART_USE_NAMESPACE
10
11
11 int main(int argc, char *argv[])
12 int main(int argc, char *argv[])
12 {
13 {
13 QApplication a(argc, argv);
14 QApplication a(argc, argv);
14 QMainWindow window;
15 QMainWindow window;
15
16
16 QBarCategory *category = new QBarCategory;
17 QBarCategory *category = new QBarCategory;
17 *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";
18
19
19 QStackedBarChartSeries* series0 = new QStackedBarChartSeries(category);
20 QStackedBarChartSeries* series = new QStackedBarChartSeries(category);
20
21
21 QBarSet *set0 = new QBarSet;
22 // We use custom set, which connects some signals. Could use QBarSet here if we don't need signals
22 QBarSet *set1 = new QBarSet;
23 CustomBarSet *set0 = new CustomBarSet;
23 QBarSet *set2 = new QBarSet;
24 CustomBarSet *set1 = new CustomBarSet;
24 QBarSet *set3 = new QBarSet;
25 CustomBarSet *set2 = new CustomBarSet;
25 QBarSet *set4 = new QBarSet;
26 CustomBarSet *set3 = new CustomBarSet;
27 CustomBarSet *set4 = new CustomBarSet;
26
28
27 // Create some test data to chart
29 // Create some test data to chart
28 *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;
29 // *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;
30 // *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;
31 // *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;
32 *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;
33
35
34 series0->addBarSet(set0);
36 series->addBarSet(set0);
35 // series0->addBarSet(set1);
37 series->addBarSet(set1);
36 // series0->addBarSet(set2);
38 series->addBarSet(set2);
37 // series0->addBarSet(set3);
39 series->addBarSet(set3);
38 series0->addBarSet(set4);
40 series->addBarSet(set4);
39
41
40 ChartWidget* chartWidget = new ChartWidget(&window);
42 QChartView* chartView = new QChartView(&window);
41 chartWidget->addSeries(series0);
43 chartView->addSeries(series);
44 chartView->setChartTitle("simple stacked barchart");
45 chartView->setChartTheme(QChart::ChartThemeIcy);
42
46
43 window.setCentralWidget(chartWidget);
47 window.setCentralWidget(chartView);
44 window.resize(400, 300);
48 window.resize(400, 300);
45 window.show();
49 window.show();
46
50
47 return a.exec();
51 return a.exec();
48 }
52 }
49
53
@@ -1,9 +1,11
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
7 HEADERS += \
8 HEADERS += \
8 chartwidget.h
9 chartwidget.h \
10 custombarset.h
9
11
@@ -1,77 +1,87
1 #include "bar_p.h"
1 #include "bar_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 Bar::Bar(QGraphicsItem *parent)
7 Bar::Bar(QGraphicsItem *parent)
8 : QGraphicsObject(parent)
8 : QGraphicsObject(parent)
9 {
9 {
10 setAcceptedMouseButtons(Qt::LeftButton);
10 setAcceptedMouseButtons(Qt::LeftButton);
11 }
11 }
12
12
13 void Bar::setSize(const QSizeF& size)
13 void Bar::setSize(const QSizeF& size)
14 {
14 {
15 mWidth = size.width();
15 mWidth = size.width();
16 mHeight = size.height();
16 mHeight = size.height();
17 }
17 }
18
18
19
19
20 void Bar::resize( qreal w, qreal h )
20 void Bar::resize( qreal w, qreal h )
21 {
21 {
22 mWidth = w;
22 mWidth = w;
23 mHeight = h;
23 mHeight = h;
24 }
24 }
25
25
26 void Bar::setColor( QColor col )
26 void Bar::setColor( QColor col )
27 {
27 {
28 mColor = col;
28 mColor = col;
29 }
29 }
30
30
31 void Bar::setPos(qreal x, qreal y)
31 void Bar::setPos(qreal x, qreal y)
32 {
32 {
33 mXpos = x;
33 mXpos = x;
34 mYpos = y;
34 mYpos = y;
35 }
35 }
36
36
37 void Bar::setPen(QPen pen)
37 void Bar::setPen(QPen pen)
38 {
38 {
39 mPen = pen;
39 mPen = pen;
40 }
40 }
41
41
42 void Bar::setBrush(QBrush brush)
42 void Bar::setBrush(QBrush brush)
43 {
43 {
44 mBrush = brush;
44 mBrush = brush;
45 }
45 }
46
46
47 void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
47 void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
48 {
48 {
49 if (0 == mHeight) {
49 if (0 == mHeight) {
50 return;
50 return;
51 }
51 }
52 painter->setBrush(mBrush);
52 painter->setBrush(mBrush);
53
53
54 // This compensates for rounding errors. drawRect takes ints and cumulative error of pos + size may be over 1.
54 // This compensates for rounding errors. drawRect takes ints and cumulative error of pos + size may be over 1.
55 int x0 = mXpos;
55 int x0 = mXpos;
56 int x1 = (mXpos + mWidth);
56 int x1 = (mXpos + mWidth);
57 int w = x1-x0;
57 int w = x1-x0;
58 int y0 = mYpos;
58 int y0 = mYpos;
59 int y1 = (mYpos + mHeight);
59 int y1 = (mYpos + mHeight);
60 int h = y1-y0;
60 int h = y1-y0;
61 painter->drawRect(x0, y0 ,w ,h);
61 painter->drawRect(x0, y0 ,w ,h);
62 }
62 }
63
63
64 QRectF Bar::boundingRect() const
64 QRectF Bar::boundingRect() const
65 {
65 {
66 QRectF r(mXpos, mYpos, mWidth, mHeight);
66 QRectF r(mXpos, mYpos, mWidth, mHeight);
67 return r;
67 return r;
68 }
68 }
69
69
70 void Bar::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/)
70 void Bar::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/)
71 {
71 {
72 emit clicked();
72 emit clicked();
73 }
73 }
74
74
75 void Bar::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/)
76 {
77 emit hoverEntered();
78 }
79
80 void Bar::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/)
81 {
82 emit hoverLeaved();
83 }
84
75 #include "moc_bar_p.cpp"
85 #include "moc_bar_p.cpp"
76
86
77 QTCOMMERCIALCHART_END_NAMESPACE
87 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,53 +1,56
1 #ifndef BAR_H
1 #ifndef BAR_H
2 #define BAR_H
2 #define BAR_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include <QGraphicsObject>
5 #include <QGraphicsObject>
6 #include <QPen>
6 #include <QPen>
7 #include <QBrush>
7 #include <QBrush>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 // Single bar item of chart
11 // Single bar item of chart
12 class Bar : public QGraphicsObject
12 class Bar : public QGraphicsObject
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 public:
15 public:
16 Bar(QGraphicsItem *parent=0);
16 Bar(QGraphicsItem *parent=0);
17
17
18 public: // from ChartItem
18 public: // from ChartItem
19 void setSize(const QSizeF &size);
19 void setSize(const QSizeF &size);
20
20
21 // Layout Stuff
21 // Layout Stuff
22 void resize(qreal w, qreal h); // Size of bar.
22 void resize(qreal w, qreal h); // Size of bar.
23 void setPos(qreal x, qreal y);
23 void setPos(qreal x, qreal y);
24 void setPen(QPen pen);
24 void setPen(QPen pen);
25 void setBrush(QBrush brush);
25 void setBrush(QBrush brush);
26 void setColor( QColor col); // deprecated
26 void setColor( QColor col); // deprecated
27
27
28 public:
28 public:
29 // From QGraphicsItem
29 // From QGraphicsItem
30
31 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
30 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
32 QRectF boundingRect() const;
31 QRectF boundingRect() const;
33 void mousePressEvent(QGraphicsSceneMouseEvent *event);
32 void mousePressEvent(QGraphicsSceneMouseEvent *event);
33 void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
34 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
34
35
35 Q_SIGNALS:
36 Q_SIGNALS:
36 void clicked();
37 void clicked();
38 void hoverEntered();
39 void hoverLeaved();
37
40
38 private:
41 private:
39
42
40 qreal mHeight;
43 qreal mHeight;
41 qreal mWidth;
44 qreal mWidth;
42 qreal mXpos;
45 qreal mXpos;
43 qreal mYpos;
46 qreal mYpos;
44 QColor mColor;
47 QColor mColor;
45
48
46 QBrush mBrush;
49 QBrush mBrush;
47 QPen mPen;
50 QPen mPen;
48
51
49 };
52 };
50
53
51 QTCOMMERCIALCHART_END_NAMESPACE
54 QTCOMMERCIALCHART_END_NAMESPACE
52
55
53 #endif // BAR_H
56 #endif // BAR_H
@@ -1,92 +1,95
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(BarChartModel& model, QGraphicsItem *parent) :
10 BarPresenter::BarPresenter(BarChartModel& model, QGraphicsItem *parent) :
11 BarPresenterBase(model,parent)
11 BarPresenterBase(model,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 (mModel.countSets() <= 0) {
20 if (mModel.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 = mModel.countCategories();
32 int categoryCount = mModel.countCategories();
33 int setCount = mModel.countSets();
33 int setCount = mModel.countSets();
34
34
35 qreal tW = mWidth;
35 qreal tW = mWidth;
36 qreal tH = mHeight;
36 qreal tH = mHeight;
37 qreal tM = mModel.max();
37 qreal tM = mModel.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 = mModel.valueAt(set, category) * scale;
50 qreal barHeight = mModel.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(mModel.setAt(set)->brush());
55 bar->setBrush(mModel.setAt(set)->brush());
56 bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + set * mBarDefaultWidth, mHeight);
56 bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + set * mBarDefaultWidth, mHeight);
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 < mModel.countCategories(); category++) {
70 for (int category=0; category < mModel.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 < mModel.countSets(); set++) {
73 for (int set=0; set < mModel.countSets(); set++) {
74 qreal barHeight = mModel.valueAt(set,category) * scale;
74 qreal barHeight = mModel.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 + mBarDefaultWidth/2, 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 QString vString(QString::number(mModel.valueAt(set,category)));
82 if (mModel.valueAt(set,category) != 0) {
83 value->setValueString(vString);
83 value->setValueString(QString::number(mModel.valueAt(set,category)));
84 } else {
85 value->setValueString(QString(""));
86 }
84
87
85 itemIndex++;
88 itemIndex++;
86 xPos += mBarDefaultWidth;
89 xPos += mBarDefaultWidth;
87 }
90 }
88 }
91 }
89 mLayoutDirty = true;
92 mLayoutDirty = true;
90 }
93 }
91
94
92 QTCOMMERCIALCHART_END_NAMESPACE
95 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,136 +1,138
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 <QDebug>
7 #include <QDebug>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 BarPresenterBase::BarPresenterBase(BarChartModel& model, QGraphicsItem *parent)
11 BarPresenterBase::BarPresenterBase(BarChartModel& model, QGraphicsItem *parent)
12 : ChartItem(parent)
12 : ChartItem(parent)
13 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
13 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
14 ,mLayoutSet(false)
14 ,mLayoutSet(false)
15 ,mLayoutDirty(true)
15 ,mLayoutDirty(true)
16 ,mSeparatorsVisible(false)
16 ,mSeparatorsVisible(false)
17 ,mModel(model)
17 ,mModel(model)
18 {
18 {
19 dataChanged();
19 dataChanged();
20 }
20 }
21
21
22 void BarPresenterBase::setSeparatorsVisible(bool visible)
22 void BarPresenterBase::setSeparatorsVisible(bool visible)
23 {
23 {
24 mSeparatorsVisible = visible;
24 mSeparatorsVisible = visible;
25 }
25 }
26
26
27 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
27 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
28 {
28 {
29 if (!mLayoutSet) {
29 if (!mLayoutSet) {
30 qDebug() << "BarPresenterBase::paint called without layout set. Aborting.";
30 qDebug() << "BarPresenterBase::paint called without layout set. Aborting.";
31 return;
31 return;
32 }
32 }
33 // if (mLayoutDirty) {
33 // if (mLayoutDirty) {
34 // Layout or data has changed. Need to redraw.
34 // Layout or data has changed. Need to redraw.
35 foreach(QGraphicsItem* i, childItems()) {
35 foreach(QGraphicsItem* i, childItems()) {
36 i->paint(painter,option,widget);
36 i->paint(painter,option,widget);
37 }
37 }
38 // }
38 // }
39 }
39 }
40
40
41 QRectF BarPresenterBase::boundingRect() const
41 QRectF BarPresenterBase::boundingRect() const
42 {
42 {
43 return QRectF(0,0,mWidth,mHeight);
43 return QRectF(0,0,mWidth,mHeight);
44 }
44 }
45
45
46 void BarPresenterBase::setBarWidth( int w )
46 void BarPresenterBase::setBarWidth( int w )
47 {
47 {
48 mBarDefaultWidth = w;
48 mBarDefaultWidth = w;
49 }
49 }
50
50
51 void BarPresenterBase::dataChanged()
51 void BarPresenterBase::dataChanged()
52 {
52 {
53 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
53 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
54 qDebug() << "datachanged";
54 qDebug() << "datachanged";
55 // Delete old bars
55 // Delete old bars
56 foreach (QGraphicsItem* item, childItems()) {
56 foreach (QGraphicsItem* item, childItems()) {
57 delete item;
57 delete item;
58 }
58 }
59
59
60 mBars.clear();
60 mBars.clear();
61 mLabels.clear();
61 mLabels.clear();
62 mSeparators.clear();
62 mSeparators.clear();
63 mFloatingValues.clear();
63 mFloatingValues.clear();
64
64
65 // Create new graphic items for bars
65 // Create new graphic items for bars
66 for (int c=0; c<mModel.countCategories(); c++) {
66 for (int c=0; c<mModel.countCategories(); c++) {
67 for (int s=0; s<mModel.countSets(); s++) {
67 for (int s=0; s<mModel.countSets(); s++) {
68 QBarSet *set = mModel.setAt(s);
68 QBarSet *set = mModel.setAt(s);
69 Bar *bar = new Bar(this);
69 Bar *bar = new Bar(this);
70 childItems().append(bar);
70 childItems().append(bar);
71 mBars.append(bar);
71 mBars.append(bar);
72 connect(bar,SIGNAL(clicked()),set,SLOT(barClicked()));
72 connect(bar,SIGNAL(clicked()),set,SLOT(barClicked()));
73 connect(bar,SIGNAL(hoverEntered()),set,SLOT(barHoverEntered()));
74 connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaved()));
73 }
75 }
74 }
76 }
75
77
76 // Create labels
78 // Create labels
77 int count = mModel.countCategories();
79 int count = mModel.countCategories();
78 for (int i=0; i<count; i++) {
80 for (int i=0; i<count; i++) {
79 BarLabel* label = new BarLabel(this);
81 BarLabel* label = new BarLabel(this);
80 label->set(mModel.label(i));
82 label->set(mModel.label(i));
81 childItems().append(label);
83 childItems().append(label);
82 mLabels.append(label);
84 mLabels.append(label);
83 }
85 }
84
86
85 // Create separators
87 // Create separators
86 count = mModel.countCategories() - 1; // There is one less separator than columns
88 count = mModel.countCategories() - 1; // There is one less separator than columns
87 for (int i=0; i<count; i++) {
89 for (int i=0; i<count; i++) {
88 Separator* sep = new Separator(this);
90 Separator* sep = new Separator(this);
89 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
91 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
90 childItems().append(sep);
92 childItems().append(sep);
91 mSeparators.append(sep);
93 mSeparators.append(sep);
92 }
94 }
93
95
94 // Create floating values
96 // Create floating values
95 for (int category=0; category<mModel.countCategories(); category++) {
97 for (int category=0; category<mModel.countCategories(); category++) {
96 for (int s=0; s<mModel.countSets(); s++) {
98 for (int s=0; s<mModel.countSets(); s++) {
97 QBarSet *set = mModel.setAt(s);
99 QBarSet *set = mModel.setAt(s);
98 BarValue *value = new BarValue(*set, this);
100 BarValue *value = new BarValue(*set, this);
99 childItems().append(value);
101 childItems().append(value);
100 mFloatingValues.append(value);
102 mFloatingValues.append(value);
101 connect(set,SIGNAL(clicked()),value,SLOT(toggleVisible()));
103 connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible()));
102 }
104 }
103 }
105 }
104
106
105 // TODO: if (autolayout) { layoutChanged() } or something
107 // TODO: if (autolayout) { layoutChanged() } or something
106 mLayoutDirty = true;
108 mLayoutDirty = true;
107 }
109 }
108
110
109 //handlers
111 //handlers
110
112
111 void BarPresenterBase::handleModelChanged(int index)
113 void BarPresenterBase::handleModelChanged(int index)
112 {
114 {
113 // qDebug() << "BarPresenterBase::handleModelChanged" << index;
115 // qDebug() << "BarPresenterBase::handleModelChanged" << index;
114 dataChanged();
116 dataChanged();
115 }
117 }
116
118
117 void BarPresenterBase::handleDomainChanged(const Domain& domain)
119 void BarPresenterBase::handleDomainChanged(const Domain& domain)
118 {
120 {
119 // qDebug() << "BarPresenterBase::handleDomainChanged";
121 // qDebug() << "BarPresenterBase::handleDomainChanged";
120 // TODO: Figure out the use case for this.
122 // TODO: Figure out the use case for this.
121 // Affects the size of visible item, so layout is changed.
123 // Affects the size of visible item, so layout is changed.
122 // layoutChanged();
124 // layoutChanged();
123 }
125 }
124
126
125 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
127 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
126 {
128 {
127 mWidth = rect.width();
129 mWidth = rect.width();
128 mHeight = rect.height();
130 mHeight = rect.height();
129 layoutChanged();
131 layoutChanged();
130 mLayoutSet = true;
132 mLayoutSet = true;
131 setPos(rect.topLeft());
133 setPos(rect.topLeft());
132 }
134 }
133
135
134 #include "moc_barpresenterbase.cpp"
136 #include "moc_barpresenterbase.cpp"
135
137
136 QTCOMMERCIALCHART_END_NAMESPACE
138 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,103 +1,107
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(BarChartModel& model, QGraphicsItem *parent) :
12 PercentBarPresenter::PercentBarPresenter(BarChartModel& model, QGraphicsItem *parent) :
13 BarPresenterBase(model, parent)
13 BarPresenterBase(model, 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 (mModel.countSets() <= 0) {
21 if (mModel.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 = mModel.countCategories();
34 int count = mModel.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 < mModel.countCategories(); category++) {
43 for (int category = 0; category < mModel.countCategories(); category++) {
44 qreal colSum = mModel.categorySum(category);
44 qreal colSum = mModel.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 < mModel.countSets(); set++) {
47 for (int set=0; set < mModel.countSets(); set++) {
48 qreal barHeight = mModel.valueAt(set, category) * scale;
48 qreal barHeight = mModel.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(mModel.setAt(set)->brush());
53 bar->setBrush(mModel.setAt(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 < mModel.countCategories() - 1; s++) {
68 for (int s=0; s < mModel.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 < mModel.countCategories(); category++) {
78 for (int category=0; category < mModel.countCategories(); category++) {
79 qreal yPos = h;
79 qreal yPos = h;
80 qreal colSum = mModel.categorySum(category);
80 qreal colSum = mModel.categorySum(category);
81 qreal scale = (h / colSum);
81 qreal scale = (h / colSum);
82 for (int set=0; set < mModel.countSets(); set++) {
82 for (int set=0; set < mModel.countSets(); set++) {
83 qreal barHeight = mModel.valueAt(set,category) * scale;
83 qreal barHeight = mModel.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->setPos(xPos + mBarDefaultWidth/2, yPos-barHeight/2);
87 value->setPos(xPos, yPos-barHeight/2);
88 value->setPen(QPen(QColor(255,255,255,255)));
88 value->setPen(QPen(QColor(255,255,255,255)));
89
89
90 QString vString(QString::number(mModel.percentageAt(set,category) * 100));
90 if (mModel.valueAt(set,category) != 0) {
91 vString.append("%");
91 QString vString(QString::number(mModel.percentageAt(set,category) * 100));
92 value->setValueString(vString);
92 vString.append("%");
93 value->setValueString(vString);
94 } else {
95 value->setValueString(QString(""));
96 }
93
97
94 itemIndex++;
98 itemIndex++;
95 yPos -= barHeight;
99 yPos -= barHeight;
96 }
100 }
97 xPos += xStep;
101 xPos += xStep;
98 }
102 }
99
103
100 mLayoutDirty = true;
104 mLayoutDirty = true;
101 }
105 }
102
106
103 QTCOMMERCIALCHART_END_NAMESPACE
107 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,75 +1,88
1 #include "qbarset.h"
1 #include "qbarset.h"
2 #include <QDebug>
2 #include <QDebug>
3
3
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5
5
6 QBarSet::QBarSet()
6 QBarSet::QBarSet(QObject *parent)
7 : QObject(parent)
7 {
8 {
8 mFloatingValuesVisible = false;
9 mFloatingValuesVisible = false;
9 }
10 }
10
11
11 void QBarSet::setName(QString name)
12 void QBarSet::setName(QString name)
12 {
13 {
13 mName = name;
14 mName = name;
14 }
15 }
15 QString QBarSet::name()
16 QString QBarSet::name()
16 {
17 {
17 return mName;
18 return mName;
18 }
19 }
19
20
20 QBarSet& QBarSet::operator << (const qreal &value)
21 QBarSet& QBarSet::operator << (const qreal &value)
21 {
22 {
22 mValues.append(value);
23 mValues.append(value);
23 return *this;
24 return *this;
24 }
25 }
25
26
26 int QBarSet::count()
27 int QBarSet::count()
27 {
28 {
28 return mValues.count();
29 return mValues.count();
29 }
30 }
30
31
31 qreal QBarSet::valueAt(int index)
32 qreal QBarSet::valueAt(int index)
32 {
33 {
33 return mValues.at(index);
34 return mValues.at(index);
34 }
35 }
35
36
36 void QBarSet::setValue(int index, qreal value)
37 void QBarSet::setValue(int index, qreal value)
37 {
38 {
38 mValues.replace(index,value);
39 mValues.replace(index,value);
39 }
40 }
40
41
41 void QBarSet::setPen(const QPen& pen)
42 void QBarSet::setPen(const QPen& pen)
42 {
43 {
43 mPen = pen;
44 mPen = pen;
44 }
45 }
45
46
46 const QPen& QBarSet::pen() const
47 const QPen& QBarSet::pen() const
47 {
48 {
48 return mPen;
49 return mPen;
49 }
50 }
50
51
51 void QBarSet::setBrush(const QBrush& brush)
52 void QBarSet::setBrush(const QBrush& brush)
52 {
53 {
53 mBrush = brush;
54 mBrush = brush;
54 }
55 }
55
56
56 const QBrush& QBarSet::brush() const
57 const QBrush& QBarSet::brush() const
57 {
58 {
58 return mBrush;
59 return mBrush;
59 }
60 }
60
61
61 bool QBarSet::isFloatingValuesVisible()
62 bool QBarSet::isFloatingValuesVisible()
62 {
63 {
63 return mFloatingValuesVisible;
64 return mFloatingValuesVisible;
64 }
65 }
65
66
66 void QBarSet::barClicked()
67 void QBarSet::barClicked()
67 {
68 {
68 qDebug() << "QBarset::barClicked" << this;
69 qDebug() << "QBarset::barClicked" << this;
69 // Some bar of this set has been clicked
70 // Some bar of this set has been clicked
70 // TODO: What happens then?
71 // TODO: What happens then?
71 emit clicked(); // Notify that set has been clicked
72 emit clicked(); // Notify that set has been clicked
72 }
73 }
73
74
75 void QBarSet::barHoverEntered()
76 {
77 qDebug() << "QBarset::barHoverEntered" << this;
78 emit hoverEnter();
79 }
80
81 void QBarSet::barHoverLeaved()
82 {
83 qDebug() << "QBarset::barHoverLeaved" << this;
84 emit hoverLeave();
85 }
86
74 #include "moc_qbarset.cpp"
87 #include "moc_qbarset.cpp"
75 QTCOMMERCIALCHART_END_NAMESPACE
88 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,54 +1,55
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();
14 QBarSet(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); //
22 void setValue(int index, qreal 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 bool isFloatingValuesVisible();
30 bool isFloatingValuesVisible();
31
31
32 Q_SIGNALS:
32 Q_SIGNALS:
33 void clicked();
33 void clicked();
34 /*
35 void hoverEnter();
34 void hoverEnter();
36 void hoverLeave();
35 void hoverLeave();
37 */
36 void toggleFloatingValues();
38
37
39 public Q_SLOTS:
38 public Q_SLOTS:
40 void barClicked();
39 void barClicked();
40 void barHoverEntered();
41 void barHoverLeaved();
41
42
42 private:
43 private:
43
44
44 QString mName;
45 QString mName;
45 QList<qreal> mValues;
46 QList<qreal> mValues;
46 QPen mPen;
47 QPen mPen;
47 QBrush mBrush;
48 QBrush mBrush;
48
49
49 bool mFloatingValuesVisible;
50 bool mFloatingValuesVisible;
50 };
51 };
51
52
52 QTCOMMERCIALCHART_END_NAMESPACE
53 QTCOMMERCIALCHART_END_NAMESPACE
53
54
54 #endif // QBARSET_H
55 #endif // QBARSET_H
@@ -1,52 +1,52
1 #ifndef STACKEDBARCHARTSERIES_H
1 #ifndef STACKEDBARCHARTSERIES_H
2 #define STACKEDBARCHARTSERIES_H
2 #define STACKEDBARCHARTSERIES_H
3
3
4 #include <QList>
4 #include <QList>
5 #include <QAbstractItemModel>
5 #include <QAbstractItemModel>
6 #include "qchartseries.h"
6 #include "qchartseries.h"
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QBarCategory;
10 class QBarCategory;
11 class QBarSet;
11 class QBarSet;
12 class BarChartModel;
12 class BarChartModel;
13
13
14 class QTCOMMERCIALCHART_EXPORT QStackedBarChartSeries : public QChartSeries
14 class QTCOMMERCIALCHART_EXPORT QStackedBarChartSeries : public QChartSeries
15 {
15 {
16 Q_OBJECT
16 Q_OBJECT
17 public:
17 public:
18 QStackedBarChartSeries(QBarCategory *category, QObject* parent=0);
18 QStackedBarChartSeries(QBarCategory *category, QObject* parent=0);
19
19
20 // from QChartSeries
20 // from QChartSeries
21 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeStackedBar; }
21 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeStackedBar; }
22
22
23 // Set handling
23 void addBarSet(QBarSet *set); // Takes ownership
24 void addBarSet(QBarSet *set); // Takes ownership
24 void removeBarSet(QBarSet *set); // Also deletes the set, if set is owned.
25 void removeBarSet(QBarSet *set); // Also deletes the set, if set is owned.
25 int countSets();
26 int countSets();
26 QBarSet* nextSet(bool first=false); // Returns first set, if called with true
27 QBarSet* nextSet(bool first=false); // Returns first set, if called with true
27
28
28 //TODO:
29 //QList<QString> legend(); // Returns legend of series (ie. names of all sets in series)
29 //QList<QString> legend(); // Returns legend of series (ie. names of all sets in series)
30
30
31 // 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
32 // to private implementation, when we start using it (not part of api)
32 // to private implementation, when we start using it (not part of api)
33 int countCategories();
33 int countCategories();
34 qreal min();
34 qreal min();
35 qreal max();
35 qreal max();
36 qreal valueAt(int set, int category);
36 qreal valueAt(int set, int category);
37 qreal maxCategorySum();
37 qreal maxCategorySum();
38
38
39 BarChartModel& model();
39 BarChartModel& model();
40
40
41 signals:
41 signals:
42 void changed(int index);
42 void changed(int index);
43
43
44 public Q_SLOTS:
44 public Q_SLOTS:
45
45
46 private:
46 private:
47 BarChartModel* mModel;
47 BarChartModel* mModel;
48 };
48 };
49
49
50 QTCOMMERCIALCHART_END_NAMESPACE
50 QTCOMMERCIALCHART_END_NAMESPACE
51
51
52 #endif // STACKEDBARCHARTSERIES_H
52 #endif // STACKEDBARCHARTSERIES_H
@@ -1,106 +1,109
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(BarChartModel& model, QGraphicsItem *parent) :
11 StackedBarPresenter::StackedBarPresenter(BarChartModel& model, QGraphicsItem *parent) :
12 BarPresenterBase(model,parent)
12 BarPresenterBase(model,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 (mModel.countSets() <= 0) {
20 if (mModel.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 (mModel.countCategories() == 0) {
26 if (mModel.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 = mModel.maxCategorySum();
40 qreal maxSum = mModel.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 = mModel.countCategories() + 1;
47 qreal tC = mModel.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 < mModel.countCategories(); category++) {
51 for (int category = 0; category < mModel.countCategories(); category++) {
52 qreal yPos = h;
52 qreal yPos = h;
53 for (int set=0; set < mModel.countSets(); set++) {
53 for (int set=0; set < mModel.countSets(); set++) {
54 qreal barHeight = mModel.valueAt(set, category) * scale;
54 qreal barHeight = mModel.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(mModel.setAt(set)->brush());
58 bar->setBrush(mModel.setAt(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 < mModel.countCategories() - 1; s++) {
73 for (int s=0; s < mModel.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 < mModel.countCategories(); category++) {
83 for (int category=0; category < mModel.countCategories(); category++) {
84 qreal yPos = h;
84 qreal yPos = h;
85 for (int set=0; set < mModel.countSets(); set++) {
85 for (int set=0; set < mModel.countSets(); set++) {
86 qreal barHeight = mModel.valueAt(set,category) * scale;
86 qreal barHeight = mModel.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 + mBarDefaultWidth/2, 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 QString vString(QString::number(mModel.valueAt(set,category)));
94 if (mModel.valueAt(set,category) != 0) {
95 value->setValueString(vString);
95 value->setValueString(QString::number(mModel.valueAt(set,category)));
96 } else {
97 value->setValueString(QString(""));
98 }
96
99
97 itemIndex++;
100 itemIndex++;
98 yPos -= barHeight;
101 yPos -= barHeight;
99 }
102 }
100 xPos += xStep;
103 xPos += xStep;
101 }
104 }
102
105
103 mLayoutDirty = true;
106 mLayoutDirty = true;
104 }
107 }
105
108
106 QTCOMMERCIALCHART_END_NAMESPACE
109 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now