##// END OF EJS Templates
added labels to barcharts
sauimone -
r114:a235f6009c27
parent child
Show More
@@ -0,0 +1,42
1 #include "barlabel_p.h"
2 #include <QPainter>
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
5 BarLabel::BarLabel(QGraphicsItem* parent) : ChartItem(parent)
6 {
7 }
8
9
10 void BarLabel::set(QString label)
11 {
12 mLabel = label;
13 }
14
15 void BarLabel::setPos(qreal x, qreal y)
16 {
17 mXpos = x;
18 mYpos = y;
19 }
20
21 void BarLabel::setSize(const QSize &size)
22 {
23 mSize = size;
24 }
25
26 void BarLabel::setPlotDomain(const PlotDomain& data)
27 {
28 mDomain = data;
29 }
30
31 void BarLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
32 {
33 painter->drawText(boundingRect(),mLabel);
34 }
35
36 QRectF BarLabel::boundingRect() const
37 {
38 QRectF r(mXpos, mYpos, mXpos + mSize.width(), mYpos + mSize.height());
39 return r;
40 }
41
42 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,36
1 #ifndef BARLABEL_H
2 #define BARLABEL_H
3
4 #include "chartitem_p.h"
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8 class BarLabel : public ChartItem
9 {
10 public:
11 BarLabel(QGraphicsItem* parent = 0);
12
13 void set(QString label);
14 void setPos(qreal x, qreal y);
15
16 // From ChartItem
17 void setSize(const QSize &size);
18 void setPlotDomain(const PlotDomain& data);
19
20 // From QGraphicsItem
21 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
22 QRectF boundingRect() const;
23
24 private:
25
26 PlotDomain mDomain;
27 QSize mSize;
28 QString mLabel;
29 qreal mXpos;
30 qreal mYpos;
31
32 };
33
34 QTCOMMERCIALCHART_END_NAMESPACE
35
36 #endif // BARLABEL_H
@@ -1,5 +1,6
1 #include "bargroup.h"
1 #include "bargroup.h"
2 #include "bar.h"
2 #include "bar.h"
3 #include "barlabel_p.h"
3 #include <QDebug>
4 #include <QDebug>
4
5
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -57,6 +58,7 void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
57 // Layout or data has changed. Need to redraw.
58 // Layout or data has changed. Need to redraw.
58 foreach(QGraphicsItem* i, childItems()) {
59 foreach(QGraphicsItem* i, childItems()) {
59 i->paint(painter,option,widget);
60 i->paint(painter,option,widget);
61 mLayoutDirty = false;
60 }
62 }
61 }
63 }
62 }
64 }
@@ -88,6 +90,16 void BarGroup::dataChanged()
88 childItems().append(bar);
90 childItems().append(bar);
89 }
91 }
90
92
93 // TODO: labels from series. This creates just some example labels
94 int count = mSeries.countColumns();
95 for (int i=0; i<count; i++) {
96 BarLabel* label = new BarLabel(this);
97 QString text("Label " + QString::number(i));
98 label->set(text);
99 childItems().append(label);
100 }
101
102 // TODO: if (autolayout) { layoutChanged() } or something
91 mLayoutDirty = true;
103 mLayoutDirty = true;
92 }
104 }
93
105
@@ -113,10 +125,10 void BarGroup::layoutChanged()
113 qreal tC = columnCount+1;
125 qreal tC = columnCount+1;
114 qreal xStepPerSeries = (tW/tC);
126 qreal xStepPerSeries = (tW/tC);
115
127
116 qDebug() << "XSTEP:" << xStepPerSeries;
117
118 // Scaling.
128 // Scaling.
119 int itemIndex(0);
129 int itemIndex(0);
130 int labelIndex = mSeries.countColumns() * mSeries.countRows();
131
120 for (int column=0; column < columnCount; column++) {
132 for (int column=0; column < columnCount; column++) {
121 qreal xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2));
133 qreal xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2));
122 qreal yPos = mHeight;
134 qreal yPos = mHeight;
@@ -131,6 +143,12 void BarGroup::layoutChanged()
131 itemIndex++;
143 itemIndex++;
132 xPos += mBarDefaultWidth;
144 xPos += mBarDefaultWidth;
133 }
145 }
146
147 // TODO: Layout for labels, remove magic number
148 xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2));
149 BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex));
150 label->setPos(xPos, mHeight + 20);
151 labelIndex++;
134 }
152 }
135
153
136 mLayoutDirty = true;
154 mLayoutDirty = true;
@@ -1,5 +1,6
1 #include "percentbargroup.h"
1 #include "percentbargroup.h"
2 #include "bar.h"
2 #include "bar.h"
3 #include "barlabel_p.h"
3 #include <QDebug>
4 #include <QDebug>
4
5
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -58,6 +59,7 void PercentBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *o
58 foreach(QGraphicsItem* i, childItems()) {
59 foreach(QGraphicsItem* i, childItems()) {
59 i->paint(painter,option,widget);
60 i->paint(painter,option,widget);
60 }
61 }
62 mLayoutDirty = false;
61 }
63 }
62 }
64 }
63
65
@@ -88,6 +90,16 void PercentBarGroup::dataChanged()
88 childItems().append(bar);
90 childItems().append(bar);
89 }
91 }
90
92
93 // TODO: labels from series. This creates just some example labels
94 int count = mSeries.countColumns();
95 for (int i=0; i<count; i++) {
96 BarLabel* label = new BarLabel(this);
97 QString text("Label " + QString::number(i));
98 label->set(text);
99 childItems().append(label);
100 }
101
102 // TODO: if (autolayout) { layoutChanged() } or something
91 mLayoutDirty = true;
103 mLayoutDirty = true;
92 }
104 }
93
105
@@ -107,7 +119,9 void PercentBarGroup::layoutChanged()
107 qreal tW = mWidth;
119 qreal tW = mWidth;
108 qreal tC = count+1;
120 qreal tC = count+1;
109 qreal xStep = (tW/tC);
121 qreal xStep = (tW/tC);
110 qreal xPos = ((tW/tC) + mBarDefaultWidth / 2);
122 // qreal xPos = ((tW/tC) + mBarDefaultWidth / 2);
123 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
124 int labelIndex = mSeries.countColumns() * mSeries.countRows();
111
125
112 for (int column = 0; column < mSeries.countColumns(); column++) {
126 for (int column = 0; column < mSeries.countColumns(); column++) {
113 qreal colSum = mSeries.columnSum(column);
127 qreal colSum = mSeries.columnSum(column);
@@ -125,6 +139,11 void PercentBarGroup::layoutChanged()
125 itemIndex++;
139 itemIndex++;
126 yPos -= barHeight;
140 yPos -= barHeight;
127 }
141 }
142
143 // TODO: Layout for labels, remove magic number
144 BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex));
145 label->setPos(xPos, mHeight + 20);
146 labelIndex++;
128 xPos += xStep;
147 xPos += xStep;
129 }
148 }
130 mLayoutDirty = true;
149 mLayoutDirty = true;
@@ -1,5 +1,6
1 #include "stackedbargroup.h"
1 #include "stackedbargroup.h"
2 #include "bar.h"
2 #include "bar.h"
3 #include "barlabel_p.h"
3 #include <QDebug>
4 #include <QDebug>
4
5
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -64,7 +65,10 void StackedBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *o
64 foreach(QGraphicsItem* i, childItems()) {
65 foreach(QGraphicsItem* i, childItems()) {
65 i->paint(painter,option,widget);
66 i->paint(painter,option,widget);
66 }
67 }
68 mLayoutDirty = false;
69 //TODO: draw labels.
67 }
70 }
71
68 }
72 }
69
73
70 QRectF StackedBarGroup::boundingRect() const
74 QRectF StackedBarGroup::boundingRect() const
@@ -94,6 +98,16 void StackedBarGroup::dataChanged()
94 childItems().append(bar);
98 childItems().append(bar);
95 }
99 }
96
100
101 // TODO: labels from series. This creates just some example labels
102 int count = mSeries.countColumns();
103 for (int i=0; i<count; i++) {
104 BarLabel* label = new BarLabel(this);
105 QString text("Label " + QString::number(i));
106 label->set(text);
107 childItems().append(label);
108 }
109
110 // TODO: if (autolayout) { layoutChanged() } or something
97 mLayoutDirty = true;
111 mLayoutDirty = true;
98 }
112 }
99
113
@@ -118,6 +132,7 void StackedBarGroup::layoutChanged()
118 qreal tC = count+1;
132 qreal tC = count+1;
119 qreal xStep = (tW/tC);
133 qreal xStep = (tW/tC);
120 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
134 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
135 int labelIndex = mSeries.countColumns() * mSeries.countRows();
121
136
122 for (int column = 0; column < mSeries.countColumns(); column++) {
137 for (int column = 0; column < mSeries.countColumns(); column++) {
123 qreal yPos = h;
138 qreal yPos = h;
@@ -134,8 +149,14 void StackedBarGroup::layoutChanged()
134 itemIndex++;
149 itemIndex++;
135 yPos -= barHeight;
150 yPos -= barHeight;
136 }
151 }
152
153 // TODO: Layout for labels, remove magic number
154 BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex));
155 label->setPos(xPos, mHeight + 20);
156 labelIndex++;
137 xPos += xStep;
157 xPos += xStep;
138 }
158 }
159
139 mLayoutDirty = true;
160 mLayoutDirty = true;
140 }
161 }
141
162
@@ -3,6 +3,7
3
3
4 #include "charttheme_p.h"
4 #include "charttheme_p.h"
5 #include "chartitem_p.h"
5 #include "chartitem_p.h"
6 #include "barlabel_p.h"
6 #include "bar.h"
7 #include "bar.h"
7 #include "stackedbarchartseries.h"
8 #include "stackedbarchartseries.h"
8 #include <QGraphicsItem>
9 #include <QGraphicsItem>
@@ -55,6 +56,7 private:
55 QList<QColor> mColors; // List of colors for series for now
56 QList<QColor> mColors; // List of colors for series for now
56
57
57 ChartTheme* mTheme;
58 ChartTheme* mTheme;
59 // QList<BarLabel*> mLabels;
58
60
59 };
61 };
60
62
@@ -19,6 +19,7 SOURCES += \
19 barchart/stackedbargroup.cpp \
19 barchart/stackedbargroup.cpp \
20 barchart/percentbarchartseries.cpp \
20 barchart/percentbarchartseries.cpp \
21 barchart/percentbargroup.cpp \
21 barchart/percentbargroup.cpp \
22 barchart/barlabel.cpp \
22 xylinechart/qxychartseries.cpp \
23 xylinechart/qxychartseries.cpp \
23 xylinechart/xylinechartitem.cpp \
24 xylinechart/xylinechartitem.cpp \
24 plotdomain.cpp \
25 plotdomain.cpp \
@@ -35,7 +36,8 SOURCES += \
35
36
36 PRIVATE_HEADERS += \
37 PRIVATE_HEADERS += \
37 xylinechart/xylinechartitem_p.h \
38 xylinechart/xylinechartitem_p.h \
38 plotdomain_p.h \
39 barchart/barlabel_p.h \
40 plotdomain_p.h \
39 qscatterseries_p.h \
41 qscatterseries_p.h \
40 qpieseries_p.h \
42 qpieseries_p.h \
41 pieslice.h \
43 pieslice.h \
General Comments 0
You need to be logged in to leave comments. Login now