##// END OF EJS Templates
Floating values to bar charts
sauimone -
r263:b057123ac417
parent child
Show More
@@ -0,0 +1,58
1 #include "barvalue_p.h"
2 #include <QPainter>
3 #include <QPen>
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
7 BarValue::BarValue(QGraphicsItem *parent)
8 : QGraphicsItem(parent)
9 {
10 // setVisible(false);
11 }
12
13 void BarValue::setValueString(QString str)
14 {
15 mValueString = str;
16 }
17
18 QString BarValue::valueString()
19 {
20 return mValueString;
21 }
22
23 void BarValue::setPen(const QPen& pen)
24 {
25 mPen = pen;
26 }
27
28 const QPen& BarValue::pen()
29 {
30 return mPen;
31 }
32
33 void BarValue::resize(qreal w, qreal h)
34 {
35 mWidth = w;
36 mHeight = h;
37 }
38
39 void BarValue::setPos(qreal x, qreal y)
40 {
41 mXpos = x;
42 mYpos = y;
43 }
44
45 void BarValue::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
46 {
47 painter->setPen(mPen);
48 painter->drawText(boundingRect(),mValueString);
49 }
50
51 QRectF BarValue::boundingRect() const
52 {
53 QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight);
54 return r;
55 }
56
57
58 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,45
1 #ifndef BARVALUE_P_H
2 #define BARVALUE_P_H
3
4 #include "qchartglobal.h"
5 #include <QGraphicsItem>
6 #include <QPen>
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10
11 // Visual class for floating bar values
12 // TODO: fonts, colors etc.
13 // By default these are not visible.
14 class BarValue : public QGraphicsItem
15 {
16 public:
17 BarValue(QGraphicsItem *parent = 0);
18
19 void setValueString(QString str);
20 QString valueString();
21
22 void setPen(const QPen& pen);
23 const QPen& pen();
24
25 void resize(qreal w, qreal h);
26 void setPos(qreal x, qreal y);
27
28 // From QGraphicsItem
29 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
30 QRectF boundingRect() const;
31
32 private:
33
34 QPen mPen;
35 QString mValueString;
36
37 qreal mXpos;
38 qreal mYpos;
39 qreal mWidth;
40 qreal mHeight;
41 };
42
43 QTCOMMERCIALCHART_END_NAMESPACE
44
45 #endif // BARVALUE_P_H
@@ -14,7 +14,8 SOURCES += \
14 $$PWD/qpercentbarchartseries.cpp \
14 $$PWD/qpercentbarchartseries.cpp \
15 $$PWD/qstackedbarchartseries.cpp \
15 $$PWD/qstackedbarchartseries.cpp \
16 $$PWD/separator.cpp \
16 $$PWD/separator.cpp \
17 $$PWD/stackedbarpresenter.cpp
17 $$PWD/stackedbarpresenter.cpp \
18 $$PWD/barvalue.cpp
18
19
19 PRIVATE_HEADERS += \
20 PRIVATE_HEADERS += \
20 $$PWD/bar_p.h \
21 $$PWD/bar_p.h \
@@ -24,12 +25,14 PRIVATE_HEADERS += \
24 $$PWD/barpresenterbase.h \
25 $$PWD/barpresenterbase.h \
25 $$PWD/percentbarpresenter.h \
26 $$PWD/percentbarpresenter.h \
26 $$PWD/separator_p.h \
27 $$PWD/separator_p.h \
27 $$PWD/stackedbarpresenter.h
28 $$PWD/stackedbarpresenter.h \
28
29 $$PWD/barvalue_p.h
30
29 PUBLIC_HEADERS += \
31 PUBLIC_HEADERS += \
30 $$PWD/qbarcategory.h \
32 $$PWD/qbarcategory.h \
31 $$PWD/qbarchartseries.h \
33 $$PWD/qbarchartseries.h \
32 $$PWD/qbarset.h \
34 $$PWD/qbarset.h \
33 $$PWD/qpercentbarchartseries.h \
35 $$PWD/qpercentbarchartseries.h \
34 $$PWD/qstackedbarchartseries.h
36 $$PWD/qstackedbarchartseries.h
35 No newline at end of file
37
38
@@ -134,14 +134,34 qreal BarChartModel::valueAt(int set, int category)
134 return mDataModel.at(set)->valueAt(category);
134 return mDataModel.at(set)->valueAt(category);
135 }
135 }
136
136
137 qreal BarChartModel::categorySum(int column)
137 qreal BarChartModel::percentageAt(int set, int category)
138 {
139 if ((set < 0) || (set >= mDataModel.count())) {
140 // No set, no value.
141 return 0;
142 } else if ((category < 0) || (category >= mDataModel.at(set)->count())) {
143 // No category, no value.
144 return 0;
145 }
146
147 qreal value = mDataModel.at(set)->valueAt(category);
148 qreal total = categorySum(category);
149 if (0 == total) {
150 return 100.0;
151 }
152
153 return value / total;
154 }
155
156
157 qreal BarChartModel::categorySum(int category)
138 {
158 {
139 qreal sum(0);
159 qreal sum(0);
140 int count = mDataModel.count(); // Count rows
160 int count = mDataModel.count(); // Count sets
141
161
142 for (int row = 0; row < count; row++) {
162 for (int set = 0; set < count; set++) {
143 if (column < mDataModel.at(row)->count()) {
163 if (category < mDataModel.at(set)->count()) {
144 sum += mDataModel.at(row)->valueAt(column);
164 sum += mDataModel.at(set)->valueAt(category);
145 }
165 }
146 }
166 }
147 return sum;
167 return sum;
@@ -33,8 +33,9 public:
33 qreal max(); // Maximum value of all sets
33 qreal max(); // Maximum value of all sets
34 qreal min(); // Minimum value of all sets
34 qreal min(); // Minimum value of all sets
35 qreal valueAt(int set, int category);
35 qreal valueAt(int set, int category);
36 qreal percentageAt(int set, int category);
36
37
37 qreal categorySum(int column);
38 qreal categorySum(int category);
38 qreal maxCategorySum(); // returns maximum sum of sets in all categories.
39 qreal maxCategorySum(); // returns maximum sum of sets in all categories.
39
40
40 QString label(int category);
41 QString label(int category);
@@ -1,6 +1,7
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 "qbarset.h"
5 #include "qbarset.h"
5 #include <QDebug>
6 #include <QDebug>
6
7
@@ -9,7 +10,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 BarPresenter::BarPresenter(BarChartModel& model, QGraphicsItem *parent) :
10 BarPresenter::BarPresenter(BarChartModel& model, QGraphicsItem *parent) :
10 BarPresenterBase(model,parent)
11 BarPresenterBase(model,parent)
11 {
12 {
12 mBarDefaultWidth = 5;
13 mBarDefaultWidth = 15;
13 }
14 }
14
15
15 void BarPresenter::layoutChanged()
16 void BarPresenter::layoutChanged()
@@ -28,25 +29,25 void BarPresenter::layoutChanged()
28
29
29 // TODO: better way to auto-layout?
30 // TODO: better way to auto-layout?
30 // Use reals for accurancy (we might get some compiler warnings... :)
31 // Use reals for accurancy (we might get some compiler warnings... :)
31 int itemCount = mModel.countCategories();
32 int categoryCount = mModel.countCategories();
32 int setCount = mModel.countSets();
33 int setCount = mModel.countSets();
33
34
34 qreal tW = mWidth;
35 qreal tW = mWidth;
35 qreal tH = mHeight;
36 qreal tH = mHeight;
36 qreal tM = mModel.max();
37 qreal tM = mModel.max();
37 qreal scale = (tH/tM);
38 qreal scale = (tH/tM);
38 qreal tC = itemCount+1;
39 qreal tC = categoryCount+1;
39 qreal xStepPerSet = (tW/tC);
40 qreal xStepPerSet = (tW/tC);
40
41
41 // Scaling.
42 // Scaling.
42 int itemIndex(0);
43 int itemIndex(0);
43 int labelIndex(0);
44 int labelIndex(0);
44
45
45 for (int item=0; item < itemCount; item++) {
46 for (int category=0; category < categoryCount; category++) {
46 qreal xPos = xStepPerSet * item + ((tW + mBarDefaultWidth*setCount)/(itemCount*2));
47 qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
47 qreal yPos = mHeight;
48 qreal yPos = mHeight;
48 for (int set = 0; set < setCount; set++) {
49 for (int set = 0; set < setCount; set++) {
49 qreal barHeight = mModel.valueAt(set, item) * scale;
50 qreal barHeight = mModel.valueAt(set, category) * scale;
50 Bar* bar = mBars.at(itemIndex);
51 Bar* bar = mBars.at(itemIndex);
51
52
52 // TODO: width settable per bar?
53 // TODO: width settable per bar?
@@ -58,12 +59,33 void BarPresenter::layoutChanged()
58 }
59 }
59
60
60 // TODO: Layout for labels, remove magic number
61 // TODO: Layout for labels, remove magic number
61 xPos = xStepPerSet * item + ((tW + mBarDefaultWidth*setCount)/(itemCount*2));
62 xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
62 BarLabel* label = mLabels.at(labelIndex);
63 BarLabel* label = mLabels.at(labelIndex);
63 label->setPos(xPos, mHeight + 20);
64 label->setPos(xPos, mHeight + 20);
64 labelIndex++;
65 labelIndex++;
65 }
66 }
66
67
68 // Position floating values
69 itemIndex = 0;
70 for (int category=0; category < mModel.countCategories(); category++) {
71 qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
72 qreal yPos = mHeight;
73 for (int set=0; set < mModel.countSets(); set++) {
74 qreal barHeight = mModel.valueAt(set,category) * scale;
75 BarValue* value = mFloatingValues.at(itemIndex);
76
77 // TODO: remove hard coding, apply layout
78 value->resize(100,50);
79 value->setPos(xPos + mBarDefaultWidth/2, yPos-barHeight/2);
80 value->setPen(QPen(QColor(255,255,255,255)));
81
82 QString vString(QString::number(mModel.valueAt(set,category)));
83 value->setValueString(vString);
84
85 itemIndex++;
86 xPos += mBarDefaultWidth;
87 }
88 }
67 mLayoutDirty = true;
89 mLayoutDirty = true;
68 }
90 }
69
91
@@ -1,5 +1,6
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 "barlabel_p.h"
4 #include "barlabel_p.h"
4 #include "separator_p.h"
5 #include "separator_p.h"
5 #include "qbarset.h"
6 #include "qbarset.h"
@@ -12,7 +13,7 BarPresenterBase::BarPresenterBase(BarChartModel& model, QGraphicsItem *parent)
12 ,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
13 ,mLayoutSet(false)
14 ,mLayoutSet(false)
14 ,mLayoutDirty(true)
15 ,mLayoutDirty(true)
15 ,mSeparatorsVisible(true)
16 ,mSeparatorsVisible(false)
16 ,mModel(model)
17 ,mModel(model)
17 {
18 {
18 dataChanged();
19 dataChanged();
@@ -59,6 +60,7 void BarPresenterBase::dataChanged()
59 mBars.clear();
60 mBars.clear();
60 mLabels.clear();
61 mLabels.clear();
61 mSeparators.clear();
62 mSeparators.clear();
63 mFloatingValues.clear();
62
64
63 // Create new graphic items for bars
65 // Create new graphic items for bars
64 for (int s=0; s<mModel.countSets(); s++) {
66 for (int s=0; s<mModel.countSets(); s++) {
@@ -71,6 +73,7 void BarPresenterBase::dataChanged()
71 }
73 }
72 }
74 }
73
75
76 // Create labels
74 int count = mModel.countCategories();
77 int count = mModel.countCategories();
75 for (int i=0; i<count; i++) {
78 for (int i=0; i<count; i++) {
76 BarLabel* label = new BarLabel(this);
79 BarLabel* label = new BarLabel(this);
@@ -79,6 +82,7 void BarPresenterBase::dataChanged()
79 mLabels.append(label);
82 mLabels.append(label);
80 }
83 }
81
84
85 // Create separators
82 count = mModel.countCategories() - 1; // There is one less separator than columns
86 count = mModel.countCategories() - 1; // There is one less separator than columns
83 for (int i=0; i<count; i++) {
87 for (int i=0; i<count; i++) {
84 Separator* sep = new Separator(this);
88 Separator* sep = new Separator(this);
@@ -87,6 +91,15 void BarPresenterBase::dataChanged()
87 mSeparators.append(sep);
91 mSeparators.append(sep);
88 }
92 }
89
93
94 // Create floating values
95 for (int s=0; s<mModel.countSets(); s++) {
96 for (int category=0; category<mModel.countCategories(); category++) {
97 BarValue *value = new BarValue(this);
98 childItems().append(value);
99 mFloatingValues.append(value);
100 }
101 }
102
90 // TODO: if (autolayout) { layoutChanged() } or something
103 // TODO: if (autolayout) { layoutChanged() } or something
91 mLayoutDirty = true;
104 mLayoutDirty = true;
92 }
105 }
@@ -12,6 +12,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 class Bar;
12 class Bar;
13 class BarLabel;
13 class BarLabel;
14 class Separator;
14 class Separator;
15 class BarValue;
15
16
16 // Common implemantation of different presenters. Not to be instantiated.
17 // Common implemantation of different presenters. Not to be instantiated.
17 class BarPresenterBase : public QObject, public ChartItem
18 class BarPresenterBase : public QObject, public ChartItem
@@ -62,6 +63,7 protected:
62 QList<Bar*> mBars;
63 QList<Bar*> mBars;
63 QList<BarLabel*> mLabels;
64 QList<BarLabel*> mLabels;
64 QList<Separator*> mSeparators;
65 QList<Separator*> mSeparators;
66 QList<BarValue*> mFloatingValues;
65
67
66 QPen mPen;
68 QPen mPen;
67 };
69 };
@@ -1,6 +1,7
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 "separator_p.h"
5 #include "separator_p.h"
5 #include "qbarset.h"
6 #include "qbarset.h"
6 #include <QDebug>
7 #include <QDebug>
@@ -37,10 +38,10 void PercentBarPresenter::layoutChanged()
37 qreal tC = count+1;
38 qreal tC = count+1;
38 qreal xStep = (tW/tC);
39 qreal xStep = (tW/tC);
39 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
40 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
41 qreal h = mHeight;
40
42
41 for (int category = 0; category < mModel.countCategories(); category++) {
43 for (int category = 0; category < mModel.countCategories(); category++) {
42 qreal colSum = mModel.categorySum(category);
44 qreal colSum = mModel.categorySum(category);
43 qreal h = mHeight;
44 qreal scale = (h / colSum);
45 qreal scale = (h / colSum);
45 qreal yPos = h;
46 qreal yPos = h;
46 for (int set=0; set < mModel.countSets(); set++) {
47 for (int set=0; set < mModel.countSets(); set++) {
@@ -63,14 +64,37 void PercentBarPresenter::layoutChanged()
63 }
64 }
64
65
65 // Position separators
66 // Position separators
66 int separatorIndex(0);
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(separatorIndex);
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 separatorIndex++;
73 }
74
75 // Position floating values
76 itemIndex = 0;
77 xPos = ((tW/tC) - mBarDefaultWidth / 2);
78 for (int category=0; category < mModel.countCategories(); category++) {
79 qreal yPos = h;
80 qreal colSum = mModel.categorySum(category);
81 qreal scale = (h / colSum);
82 for (int set=0; set < mModel.countSets(); set++) {
83 qreal barHeight = mModel.valueAt(set,category) * scale;
84 BarValue* value = mFloatingValues.at(itemIndex);
85
86 // TODO: remove hard coding, apply layout
87 value->setPos(xPos + mBarDefaultWidth/2, yPos-barHeight/2);
88 value->setPen(QPen(QColor(255,255,255,255)));
89
90 QString vString(QString::number(mModel.percentageAt(set,category) * 100));
91 vString.append("%");
92 value->setValueString(vString);
93
94 itemIndex++;
95 yPos -= barHeight;
96 }
97 xPos += xStep;
74 }
98 }
75
99
76 mLayoutDirty = true;
100 mLayoutDirty = true;
@@ -59,9 +59,10 const QBrush& QBarSet::brush() const
59
59
60 void QBarSet::barClicked()
60 void QBarSet::barClicked()
61 {
61 {
62 qDebug() << "QBarset::barClicked";
62 // Some bar of this set has been clicked
63 // Some bar of this set has been clicked
63 // TODO: What happens then?
64 // TODO: What happens then?
64 qDebug() << "bar Clicked";
65 emit clicked(); // Notify that set has been clicked
65 }
66 }
66
67
67
68
@@ -27,7 +27,8 public:
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 // void clicked();
30 Q_SIGNALS:
31 void clicked();
31 /*
32 /*
32 void hoverEnter();
33 void hoverEnter();
33 void hoverLeave();
34 void hoverLeave();
@@ -1,6 +1,7
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 "separator_p.h"
5 #include "separator_p.h"
5 #include "qbarset.h"
6 #include "qbarset.h"
6 #include <QDebug>
7 #include <QDebug>
@@ -68,14 +69,35 void StackedBarPresenter::layoutChanged()
68 }
69 }
69
70
70 // Position separators
71 // Position separators
71 int separatorIndex(0);
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(separatorIndex);
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 separatorIndex++;
78 }
79
80 // Position floating values
81 itemIndex = 0;
82 xPos = ((tW/tC) - mBarDefaultWidth / 2);
83 for (int category=0; category < mModel.countCategories(); category++) {
84 qreal yPos = h;
85 for (int set=0; set < mModel.countSets(); set++) {
86 qreal barHeight = mModel.valueAt(set,category) * scale;
87 BarValue* value = mFloatingValues.at(itemIndex);
88
89 // TODO: remove hard coding, apply layout
90 value->resize(100,50);
91 value->setPos(xPos + mBarDefaultWidth/2, yPos-barHeight/2);
92 value->setPen(QPen(QColor(255,255,255,255)));
93
94 QString vString(QString::number(mModel.valueAt(set,category)));
95 value->setValueString(vString);
96
97 itemIndex++;
98 yPos -= barHeight;
99 }
100 xPos += xStep;
79 }
101 }
80
102
81 mLayoutDirty = true;
103 mLayoutDirty = true;
General Comments 0
You need to be logged in to leave comments. Login now