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