##// END OF EJS Templates
Refactors barchart axis hadnling...
Michal Klocek -
r679:2f2494d0880e
parent child
Show More
@@ -69,7 +69,7 int main(int argc, char *argv[])
69 69 //! [5]
70 70
71 71 //! [6]
72 chartView->axisX()->setAxisVisible(false);
72 //chartView->axisX()->setAxisVisible(false);
73 73 chartView->axisX()->setGridLineVisible(false);
74 74 chartView->axisX()->setLabelsVisible(false);
75 75 //! [6]
@@ -103,15 +103,17 protected:
103 103
104 104 QRectF boundingRect() const
105 105 {
106 return QGraphicsLineItem::boundingRect().adjusted(0,0,m_axis->axisType()!=Axis::X_AXIS?10:0,m_axis->axisType()!=Axis::Y_AXIS?10:0);
106 return shape().boundingRect();
107 107 }
108 108
109 109 QPainterPath shape() const
110 110 {
111 QPainterPath path;
112 path.addRect(boundingRect());
111 QPainterPath path = QGraphicsLineItem::shape();
112 QRectF rect = path.boundingRect();
113 path.addRect(rect.adjusted(0,0,m_axis->axisType()!=Axis::X_AXIS?8:0,m_axis->axisType()!=Axis::Y_AXIS?8:0));
113 114 return path;
114 115 }
116
115 117 private:
116 118 Axis* m_axis;
117 119
@@ -6,75 +6,13
6 6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 7
8 8 Bar::Bar(QString category, QGraphicsItem *parent)
9 : QGraphicsObject(parent),
10 mXpos(0),
11 mYpos(0),
12 mWidth(0),
13 mHeight(0),
14 mBrush(QBrush()),
15 mPen(QPen()),
9 : QGraphicsRectItem(parent),
16 10 mCategory(category)
17 11 {
18 12 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
19 13 setAcceptHoverEvents(true);
20 14 }
21 15
22 void Bar::setSize(const QSizeF& size)
23 {
24 mWidth = size.width();
25 mHeight = size.height();
26 }
27
28
29 void Bar::resize( qreal w, qreal h )
30 {
31 mWidth = w;
32 mHeight = h;
33 }
34
35 void Bar::setPos(qreal x, qreal y)
36 {
37 mXpos = x;
38 mYpos = y;
39 }
40
41 void Bar::setPen(QPen pen)
42 {
43 mPen = pen;
44 }
45
46 void Bar::setBrush(QBrush brush)
47 {
48 mBrush = brush;
49 }
50
51 void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
52 {
53 Q_UNUSED(option)
54 Q_UNUSED(widget)
55
56 if (0 == mHeight) {
57 return;
58 }
59 painter->setPen(mPen);
60 painter->setBrush(mBrush);
61
62 // This compensates for rounding errors. drawRect takes ints and cumulative error of pos + size may be over 1.
63 int x0 = mXpos;
64 int x1 = (mXpos + mWidth);
65 int w = x1-x0;
66 int y0 = mYpos;
67 int y1 = (mYpos + mHeight);
68 int h = y1-y0;
69 painter->drawRect(x0, y0 ,w ,h);
70 }
71
72 QRectF Bar::boundingRect() const
73 {
74 QRectF r(mXpos, mYpos, mWidth, mHeight);
75 return r;
76 }
77
78 16 void Bar::mousePressEvent(QGraphicsSceneMouseEvent* event)
79 17 {
80 18 if (event->button() == Qt::LeftButton) {
@@ -2,32 +2,18
2 2 #define BAR_H
3 3
4 4 #include "qchartglobal.h"
5 #include <QGraphicsObject>
6 #include <QPen>
7 #include <QBrush>
5 #include <QGraphicsRectItem>
8 6
9 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 8
11 9 // Single visual bar item of chart
12 class Bar : public QGraphicsObject
10 class Bar : public QObject, public QGraphicsRectItem
13 11 {
14 12 Q_OBJECT
15 13 public:
16 14 Bar(QString category, QGraphicsItem *parent=0);
17 15
18 public: // from ChartItem
19 void setSize(const QSizeF &size);
20
21 // Layout Stuff
22 void resize(qreal w, qreal h);
23 void setPos(qreal x, qreal y);
24 void setPen(QPen pen);
25 void setBrush(QBrush brush);
26
27 16 public:
28 // From QGraphicsItem
29 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
30 QRectF boundingRect() const;
31 17 void mousePressEvent(QGraphicsSceneMouseEvent *event);
32 18 void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
33 19 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
@@ -39,15 +25,6 Q_SIGNALS:
39 25 void hoverLeaved();
40 26
41 27 private:
42
43 qreal mXpos;
44 qreal mYpos;
45 qreal mWidth;
46 qreal mHeight;
47
48 QBrush mBrush;
49 QPen mPen;
50
51 28 QString mCategory;
52 29 };
53 30
@@ -8,6 +8,7
8 8 #include "qchartaxiscategories.h"
9 9 #include "chartpresenter_p.h"
10 10 #include "chartanimator_p.h"
11 #include "chartdataset_p.h"
11 12 #include <QDebug>
12 13 #include <QToolTip>
13 14
@@ -15,8 +16,6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
15 16
16 17 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
17 18 ChartItem(presenter),
18 mHeight(0),
19 mWidth(0),
20 19 mLayoutSet(false),
21 20 mSeries(series)
22 21 {
@@ -47,7 +46,7 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
47 46
48 47 QRectF BarChartItem::boundingRect() const
49 48 {
50 return QRectF(0, 0, mWidth, mHeight);
49 return m_rect;
51 50 }
52 51
53 52 void BarChartItem::dataChanged()
@@ -102,47 +101,46 void BarChartItem::layoutChanged()
102 101 return;
103 102 }
104 103
105 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
106 int categoryCount = mSeries->categoryCount();
107 int setCount = mSeries->barsetCount();
108 104
109 qreal tW = mWidth;
110 qreal tH = mHeight;
111 qreal tM = mSeries->max();
105 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
106 qreal categoryCount = mSeries->categoryCount();
107 qreal setCount = mSeries->barsetCount();
108 qreal max = mSeries->max();
112 109
113 110 // Domain:
114 if (mDomainMaxY > tM) {
115 tM = mDomainMaxY;
116 }
111 if (mDomainMaxY > max) {
112 max = mDomainMaxY;
113 }
117 114
118 qreal scale = (tH/tM);
119 qreal tC = categoryCount + 1;
120 qreal categoryWidth = tW/tC;
121 mBarWidth = categoryWidth / (setCount+1);
115 qreal width = geometry().width();
116 qreal height = geometry().height();
117 qreal scale = (height/max);
118 qreal categoryWidth = width/categoryCount;
119 qreal barWidth = categoryWidth / (setCount+1);
122 120
123 121 int itemIndex(0);
124 122 for (int category=0; category < categoryCount; category++) {
125 qreal xPos = categoryWidth * category + categoryWidth /2 + mBarWidth/2;
126 qreal yPos = mHeight;
123 qreal xPos = categoryWidth * category + barWidth/2;
124 qreal yPos = height;
127 125 for (int set = 0; set < setCount; set++) {
128 126 qreal barHeight = mSeries->valueAt(set,category) * scale;
129 127 Bar* bar = mBars.at(itemIndex);
130 128
131 129 // TODO: width settable per bar?
132 bar->resize(mBarWidth, barHeight);
130 bar->setRect(xPos, yPos-barHeight,barWidth, barHeight);
133 131 bar->setPen(mSeries->barsetAt(set)->pen());
134 132 bar->setBrush(mSeries->barsetAt(set)->brush());
135 bar->setPos(xPos, yPos-barHeight);
133
136 134 itemIndex++;
137 xPos += mBarWidth;
135 xPos += barWidth;
138 136 }
139 137 }
140 138
141 139 // Position floating values
142 140 itemIndex = 0;
143 141 for (int category=0; category < mSeries->categoryCount(); category++) {
144 qreal xPos = categoryWidth * category + categoryWidth/2 + mBarWidth;
145 qreal yPos = mHeight;
142 qreal xPos = categoryWidth * category + categoryWidth/2 + barWidth;
143 qreal yPos = height;
146 144 for (int set=0; set < mSeries->barsetCount(); set++) {
147 145 qreal barHeight = mSeries->valueAt(set,category) * scale;
148 146 BarValue* value = mFloatingValues.at(itemIndex);
@@ -159,7 +157,7 void BarChartItem::layoutChanged()
159 157 }
160 158
161 159 itemIndex++;
162 xPos += mBarWidth;
160 xPos += barWidth;
163 161 }
164 162 }
165 163 update();
@@ -169,7 +167,7 BarLayout BarChartItem::calculateLayout()
169 167 {
170 168 BarLayout layout;
171 169 foreach(Bar* bar, mBars) {
172 layout.insert(bar,bar->boundingRect().size());
170 layout.insert(bar,bar->boundingRect());
173 171 }
174 172
175 173 return layout;
@@ -177,8 +175,8 BarLayout BarChartItem::calculateLayout()
177 175
178 176 void BarChartItem::applyLayout(const BarLayout &layout)
179 177 {
180 if (m_animator)
181 m_animator->updateLayout(this, layout);
178 if (animator())
179 animator()->updateLayout(this, layout);
182 180 else
183 181 setLayout(layout);
184 182 }
@@ -186,7 +184,7 void BarChartItem::applyLayout(const BarLayout &layout)
186 184 void BarChartItem::setLayout(const BarLayout &layout)
187 185 {
188 186 foreach (Bar *bar, layout.keys()) {
189 bar->setSize(layout.value(bar));
187 bar->setRect(layout.value(bar));
190 188 }
191 189 update();
192 190 }
@@ -198,23 +196,12 void BarChartItem::initAxisLabels()
198 196 return;
199 197 }
200 198
201 mChart->axisX()->setTicksCount(count+2);
199 Domain* domain = presenter()->dataSet()->domain(mSeries);
202 200
203 201 qreal min = 0;
204 202 qreal max = count+1;
205 203
206 mChart->axisX()->setMin(min);
207 mChart->axisX()->setMax(max);
208
209 QChartAxisCategories* categories = mChart->axisX()->categories();
210 categories->clear();
211 for (int i=0; i<count; i++) {
212 categories->insert(i+1,mSeries->categoryName(i));
213 }
214
215
216
217 mChart->axisX()->setLabelsVisible(true);
204 domain->setRangeX(min,max,count+1);
218 205 }
219 206
220 207 //handlers
@@ -254,8 +241,7 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal
254 241
255 242 void BarChartItem::handleGeometryChanged(const QRectF& rect)
256 243 {
257 mWidth = rect.width();
258 mHeight = rect.height();
244 m_rect=rect;
259 245 layoutChanged();
260 246 mLayoutSet = true;
261 247 setPos(rect.topLeft());
@@ -14,9 +14,9 class BarValue;
14 14 class QChartAxisCategories;
15 15 class QChart;
16 16
17 typedef QHash<Bar*, QSizeF> BarLayout;
17 typedef QHash<Bar*, QRectF> BarLayout;
18 18
19 class BarChartItem : public QObject, public ChartItem
19 class BarChartItem : public ChartItem
20 20 {
21 21 Q_OBJECT
22 22 public:
@@ -41,6 +41,8 public:
41 41 void applyLayout(const BarLayout &layout);
42 42 void setLayout(const BarLayout &layout);
43 43
44 QRectF geometry() const { return m_rect;}
45
44 46 protected:
45 47 void initAxisLabels();
46 48
@@ -56,15 +58,12 public slots:
56 58 protected:
57 59
58 60 // TODO: consider these.
59 int mHeight; // Layout spesific
60 int mWidth;
61 qreal mBarWidth;
62
63 61 qreal mDomainMinX;
64 62 qreal mDomainMaxX;
65 63 qreal mDomainMinY;
66 64 qreal mDomainMaxY;
67 65
66 QRectF m_rect;
68 67 bool mLayoutSet; // True, if component has been laid out.
69 68
70 69 // Not owned.
@@ -27,28 +27,29 void PercentBarChartItem::layoutChanged()
27 27 }
28 28
29 29 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
30 qreal tW = mWidth;
31 qreal tC = mSeries->categoryCount() + 1;
32 qreal cC = mSeries->categoryCount() * 2 + 1;
33 mBarWidth = tW / cC;
34 qreal xStep = (tW/tC);
35 qreal xPos = ((tW/tC) - mBarWidth / 2);
36 qreal h = mHeight;
30 qreal width = geometry().width();
31 qreal height = geometry().height();
32
33 qreal categoryCount = mSeries->categoryCount();
34 qreal barWidth = width / (mSeries->categoryCount() * 2);
35 qreal xStep = width/categoryCount;
36 qreal xPos = xStep/2 - barWidth / 2;
37 37
38 38 int itemIndex(0);
39 for (int category = 0; category < mSeries->categoryCount(); category++) {
39 for (int category = 0; category < categoryCount ; category++) {
40 40 qreal colSum = mSeries->categorySum(category);
41 qreal scale = (h / colSum);
42 qreal yPos = h;
41 qreal scale = (height / colSum);
42 qreal yPos = height;
43 43 for (int set=0; set < mSeries->barsetCount(); set++) {
44 44 qreal barHeight = mSeries->valueAt(set, category) * scale;
45 45 Bar* bar = mBars.at(itemIndex);
46 46
47 47 // TODO: width settable per bar?
48 bar->resize(mBarWidth, barHeight);
48
49 49 bar->setPen(mSeries->barsetAt(set)->pen());
50 bar->setRect(xPos, yPos-barHeight,barWidth, barHeight);
50 51 bar->setBrush(mSeries->barsetAt(set)->brush());
51 bar->setPos(xPos, yPos-barHeight);
52
52 53 itemIndex++;
53 54 yPos -= barHeight;
54 55 }
@@ -57,11 +58,11 void PercentBarChartItem::layoutChanged()
57 58
58 59 // Position floating values
59 60 itemIndex = 0;
60 xPos = (tW/tC);
61 xPos = (width/categoryCount);
61 62 for (int category=0; category < mSeries->categoryCount(); category++) {
62 qreal yPos = h;
63 qreal yPos = height;
63 64 qreal colSum = mSeries->categorySum(category);
64 qreal scale = (h / colSum);
65 qreal scale = (height / colSum);
65 66 for (int set=0; set < mSeries->barsetCount(); set++) {
66 67 qreal barHeight = mSeries->valueAt(set,category) * scale;
67 68 BarValue* value = mFloatingValues.at(itemIndex);
@@ -12,7 +12,7 class PercentBarChartItem : public BarChartItem
12 12 {
13 13 Q_OBJECT
14 14 public:
15 PercentBarChartItem(QBarSeries *series, QChart *parent = 0);
15 PercentBarChartItem(QBarSeries *series, ChartPresenter *presenter);
16 16
17 17 private:
18 18
@@ -38,32 +38,31 void StackedBarChartItem::layoutChanged()
38 38 }
39 39
40 40 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
41
41 42 qreal maxSum = mSeries->maxCategorySum();
42 43 // Domain:
43 44 if (mDomainMaxY > maxSum) {
44 45 maxSum = mDomainMaxY;
45 46 }
46 47
47 qreal h = mHeight;
48 qreal scale = (h / maxSum);
49 qreal tW = mWidth;
50 qreal tC = mSeries->categoryCount() + 1;
51 qreal cC = mSeries->categoryCount() * 2 + 1;
52 mBarWidth = tW / cC;
53 qreal xStep = (tW/tC);
54 qreal xPos = ((tW/tC) - mBarWidth / 2);
48 qreal height = geometry().height();
49 qreal width = geometry().width();
50 qreal scale = (height / mSeries->maxCategorySum());
51 qreal categotyCount = mSeries->categoryCount();
52 qreal barWidth = width / (categotyCount *2);
53 qreal xStep = width/categotyCount;
54 qreal xPos = xStep/2 - barWidth/2;
55
55 56
56 57 int itemIndex(0);
57 for (int category = 0; category < mSeries->categoryCount(); category++) {
58 qreal yPos = h;
58 for (int category = 0; category < categotyCount; category++) {
59 qreal yPos = height;
59 60 for (int set=0; set < mSeries->barsetCount(); set++) {
60 61 qreal barHeight = mSeries->valueAt(set, category) * scale;
61 62 Bar* bar = mBars.at(itemIndex);
62
63 bar->resize(mBarWidth, barHeight);
64 63 bar->setPen(mSeries->barsetAt(set)->pen());
65 64 bar->setBrush(mSeries->barsetAt(set)->brush());
66 bar->setPos(xPos, yPos-barHeight);
65 bar->setRect(xPos, yPos-barHeight,barWidth, barHeight);
67 66 itemIndex++;
68 67 yPos -= barHeight;
69 68 }
@@ -72,9 +71,9 void StackedBarChartItem::layoutChanged()
72 71
73 72 // Position floating values
74 73 itemIndex = 0;
75 xPos = (tW/tC);
74 xPos = (width/categotyCount);
76 75 for (int category=0; category < mSeries->categoryCount(); category++) {
77 qreal yPos = h;
76 qreal yPos = height;
78 77 for (int set=0; set < mSeries->barsetCount(); set++) {
79 78 qreal barHeight = mSeries->valueAt(set,category) * scale;
80 79 BarValue* value = mFloatingValues.at(itemIndex);
@@ -11,7 +11,7 class StackedBarChartItem : public BarChartItem
11 11 {
12 12 Q_OBJECT
13 13 public:
14 StackedBarChartItem(QBarSeries *series, QChart *parent = 0);
14 StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter);
15 15 ~StackedBarChartItem();
16 16
17 17 private:
@@ -35,7 +35,7 void Domain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY,int tickXCo
35 35 }
36 36
37 37 if(m_tickXCount!=tickYCount) {
38 m_tickXCount=tickYCount;
38 m_tickYCount=tickYCount;
39 39 tickYChanged=true;
40 40 }
41 41
@@ -74,11 +74,22 void Domain::setRangeX(qreal min, qreal max)
74 74 {
75 75 setRange(min,max,m_minY, m_maxY);
76 76 }
77
78 void Domain::setRangeX(qreal min, qreal max, int tickCount)
79 {
80 setRange(min,max,m_minY, m_maxY,tickCount,m_tickYCount);
81 }
82
77 83 void Domain::setRangeY(qreal min, qreal max)
78 84 {
79 85 setRange(m_minX, m_maxX, min, max);
80 86 }
81 87
88 void Domain::setRangeY(qreal min, qreal max,int tickCount)
89 {
90 setRange(m_minX, m_maxX, min, max,m_tickXCount,tickCount);
91 }
92
82 93 void Domain::setMinX(qreal min)
83 94 {
84 95 setRange(min, m_maxX, m_minY, m_maxY);
@@ -239,7 +250,7 bool operator!= (const Domain &domain1, const Domain &domain2)
239 250
240 251 QDebug operator<<(QDebug dbg, const Domain &domain)
241 252 {
242 dbg.nospace() << "Domain("<<domain.m_minX<<','<<domain.m_maxX<<','<<domain.m_minY<<','<<domain.m_maxY<<')';
253 dbg.nospace() << "Domain("<<domain.m_minX<<','<<domain.m_maxX<<','<<domain.m_minY<<','<<domain.m_maxY<<')' << domain.m_tickXCount << "," << domain.m_tickYCount ;
243 254 return dbg.maybeSpace();
244 255 }
245 256
@@ -16,7 +16,9 public:
16 16 void setRange(qreal minX, qreal maxX, qreal minY, qreal maxY);
17 17 void setRange(qreal minX, qreal maxX, qreal minY, qreal maxY, int tickXCount, int tickYCount);
18 18 void setRangeX(qreal min, qreal max);
19 void setRangeX(qreal min, qreal max, int tickCount);
19 20 void setRangeY(qreal min, qreal max);
21 void setRangeY(qreal min, qreal max, int tickCount);
20 22 void setMinX(qreal min);
21 23 void setMaxX(qreal max);
22 24 void setMinY(qreal min);
@@ -63,8 +63,8 void PieChartItem::handleSlicesAdded(QList<QPieSlice*> slices)
63 63
64 64 PieSliceData data = sliceData(s);
65 65
66 if (m_animator)
67 m_animator->addAnimation(this, s, data, isEmpty);
66 if (animator())
67 animator()->addAnimation(this, s, data, isEmpty);
68 68 else
69 69 setLayout(s, data);
70 70 }
@@ -155,8 +155,8 void PieChartItem::applyLayout(const PieLayout &layout)
155 155
156 156 void PieChartItem::updateLayout(QPieSlice *slice, const PieSliceData &sliceData)
157 157 {
158 if (m_animator)
159 m_animator->updateLayout(this, slice, sliceData);
158 if (animator())
159 animator()->updateLayout(this, slice, sliceData);
160 160 else
161 161 setLayout(slice, sliceData);
162 162 }
General Comments 0
You need to be logged in to leave comments. Login now