@@ -1,275 +1,281 | |||||
1 | #include "barchartitem_p.h" |
|
1 | #include "barchartitem_p.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barvalue_p.h" |
|
3 | #include "barvalue_p.h" | |
4 | #include "qbarset.h" |
|
4 | #include "qbarset.h" | |
5 | #include "qbarseries.h" |
|
5 | #include "qbarseries.h" | |
6 | #include "qchart.h" |
|
6 | #include "qchart.h" | |
7 | #include "qchartaxis.h" |
|
7 | #include "qchartaxis.h" | |
8 | #include "qchartaxiscategories.h" |
|
8 | #include "qchartaxiscategories.h" | |
9 | #include "chartpresenter_p.h" |
|
9 | #include "chartpresenter_p.h" | |
10 | #include "chartanimator_p.h" |
|
10 | #include "chartanimator_p.h" | |
11 | #include <QDebug> |
|
11 | #include <QDebug> | |
12 | #include <QToolTip> |
|
12 | #include <QToolTip> | |
13 |
|
13 | |||
14 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
14 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
15 |
|
15 | |||
16 | BarChartItem::BarChartItem(QBarSeries *series, QChart *parent) : |
|
16 | BarChartItem::BarChartItem(QBarSeries *series, QChart *parent) : | |
17 | ChartItem(parent), |
|
17 | ChartItem(parent), | |
18 | mHeight(0), |
|
18 | mHeight(0), | |
19 | mWidth(0), |
|
19 | mWidth(0), | |
20 | mLayoutSet(false), |
|
20 | mLayoutSet(false), | |
21 | mSeries(series), |
|
21 | mSeries(series), | |
22 | mChart(parent) |
|
22 | mChart(parent) | |
23 | { |
|
23 | { | |
24 | connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString))); |
|
24 | connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString))); | |
25 | connect(series, SIGNAL(updatedBars()), this, SLOT(layoutChanged())); |
|
25 | connect(series, SIGNAL(updatedBars()), this, SLOT(layoutChanged())); | |
26 | //TODO: connect(series,SIGNAL("position or size has changed"), this, SLOT(handleLayoutChanged())); |
|
26 | //TODO: connect(series,SIGNAL("position or size has changed"), this, SLOT(handleLayoutChanged())); | |
27 | connect(series, SIGNAL(restructuredBar(int)), this, SLOT(handleModelChanged(int))); |
|
27 | connect(series, SIGNAL(restructuredBar(int)), this, SLOT(handleModelChanged(int))); | |
28 | setZValue(ChartPresenter::BarSeriesZValue); |
|
28 | setZValue(ChartPresenter::BarSeriesZValue); | |
29 | initAxisLabels(); |
|
29 | initAxisLabels(); | |
30 | dataChanged(); |
|
30 | dataChanged(); | |
31 | } |
|
31 | } | |
32 |
|
32 | |||
33 | BarChartItem::~BarChartItem() |
|
33 | BarChartItem::~BarChartItem() | |
34 | { |
|
34 | { | |
35 | disconnect(this,SLOT(showToolTip(QPoint,QString))); |
|
35 | disconnect(this,SLOT(showToolTip(QPoint,QString))); | |
36 | } |
|
36 | } | |
37 |
|
37 | |||
38 | void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
38 | void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
39 | { |
|
39 | { | |
40 | if (!mLayoutSet) { |
|
40 | if (!mLayoutSet) { | |
41 | qDebug() << "BarChartItem::paint called without layout set. Aborting."; |
|
41 | qDebug() << "BarChartItem::paint called without layout set. Aborting."; | |
42 | return; |
|
42 | return; | |
43 | } |
|
43 | } | |
44 | foreach(QGraphicsItem* i, childItems()) { |
|
44 | foreach(QGraphicsItem* i, childItems()) { | |
45 | i->paint(painter,option,widget); |
|
45 | i->paint(painter,option,widget); | |
46 | } |
|
46 | } | |
47 | } |
|
47 | } | |
48 |
|
48 | |||
49 | QRectF BarChartItem::boundingRect() const |
|
49 | QRectF BarChartItem::boundingRect() const | |
50 | { |
|
50 | { | |
51 | return QRectF(0, 0, mWidth, mHeight); |
|
51 | return QRectF(0, 0, mWidth, mHeight); | |
52 | } |
|
52 | } | |
53 |
|
53 | |||
54 | void BarChartItem::dataChanged() |
|
54 | void BarChartItem::dataChanged() | |
55 | { |
|
55 | { | |
56 | // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? |
|
56 | // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? | |
57 | // Delete old bars |
|
57 | // Delete old bars | |
58 | foreach (QGraphicsItem* item, childItems()) { |
|
58 | foreach (QGraphicsItem* item, childItems()) { | |
59 | delete item; |
|
59 | delete item; | |
60 | } |
|
60 | } | |
61 |
|
61 | |||
62 | mBars.clear(); |
|
62 | mBars.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<mSeries->categoryCount(); c++) { |
|
66 | for (int c=0; c<mSeries->categoryCount(); c++) { | |
67 | QString category = mSeries->categoryName(c); |
|
67 | QString category = mSeries->categoryName(c); | |
68 | for (int s=0; s<mSeries->barsetCount(); s++) { |
|
68 | for (int s=0; s<mSeries->barsetCount(); s++) { | |
69 | QBarSet *set = mSeries->barsetAt(s); |
|
69 | QBarSet *set = mSeries->barsetAt(s); | |
70 | Bar *bar = new Bar(category,this); |
|
70 | Bar *bar = new Bar(category,this); | |
71 | childItems().append(bar); |
|
71 | childItems().append(bar); | |
72 | mBars.append(bar); |
|
72 | mBars.append(bar); | |
73 | connect(bar,SIGNAL(clicked(QString)),set,SIGNAL(clicked(QString))); |
|
73 | connect(bar,SIGNAL(clicked(QString)),set,SIGNAL(clicked(QString))); | |
74 | connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString))); |
|
74 | connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString))); | |
75 | connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint))); |
|
75 | connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint))); | |
76 | connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent())); |
|
76 | connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent())); | |
77 | } |
|
77 | } | |
78 | } |
|
78 | } | |
79 |
|
79 | |||
80 | // Create floating values |
|
80 | // Create floating values | |
81 | for (int category=0; category<mSeries->categoryCount(); category++) { |
|
81 | for (int category=0; category<mSeries->categoryCount(); category++) { | |
82 | for (int s=0; s<mSeries->barsetCount(); s++) { |
|
82 | for (int s=0; s<mSeries->barsetCount(); s++) { | |
83 | QBarSet *set = mSeries->barsetAt(s); |
|
83 | QBarSet *set = mSeries->barsetAt(s); | |
84 | BarValue *value = new BarValue(*set, this); |
|
84 | BarValue *value = new BarValue(*set, this); | |
85 | childItems().append(value); |
|
85 | childItems().append(value); | |
86 | mFloatingValues.append(value); |
|
86 | mFloatingValues.append(value); | |
87 | connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible())); |
|
87 | connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible())); | |
88 | } |
|
88 | } | |
89 | } |
|
89 | } | |
90 | } |
|
90 | } | |
91 |
|
91 | |||
92 | void BarChartItem::layoutChanged() |
|
92 | void BarChartItem::layoutChanged() | |
93 | { |
|
93 | { | |
94 | // Scale bars to new layout |
|
94 | // Scale bars to new layout | |
95 | // Layout for bars: |
|
95 | // Layout for bars: | |
96 | if (mSeries->barsetCount() <= 0) { |
|
96 | if (mSeries->barsetCount() <= 0) { | |
97 | qDebug() << "No sets in model!"; |
|
97 | qDebug() << "No sets in model!"; | |
98 | return; |
|
98 | return; | |
99 | } |
|
99 | } | |
100 |
|
100 | |||
101 | if (childItems().count() == 0) { |
|
101 | if (childItems().count() == 0) { | |
102 | qDebug() << "WARNING: BarChartitem::layoutChanged called before graphics items are created!"; |
|
102 | qDebug() << "WARNING: BarChartitem::layoutChanged called before graphics items are created!"; | |
103 | return; |
|
103 | return; | |
104 | } |
|
104 | } | |
105 |
|
105 | |||
106 | // Use temporary qreals for accurancy (we might get some compiler warnings... :) |
|
106 | // Use temporary qreals for accurancy (we might get some compiler warnings... :) | |
107 | int categoryCount = mSeries->categoryCount(); |
|
107 | int categoryCount = mSeries->categoryCount(); | |
108 | int setCount = mSeries->barsetCount(); |
|
108 | int setCount = mSeries->barsetCount(); | |
109 |
|
109 | |||
110 | qreal tW = mWidth; |
|
110 | qreal tW = mWidth; | |
111 | qreal tH = mHeight; |
|
111 | qreal tH = mHeight; | |
112 | qreal tM = mSeries->max(); |
|
112 | qreal tM = mSeries->max(); | |
|
113 | ||||
|
114 | // Domain: | |||
|
115 | if (mDomainMaxY > tM) { | |||
|
116 | tM = mDomainMaxY; | |||
|
117 | } | |||
|
118 | ||||
113 | qreal scale = (tH/tM); |
|
119 | qreal scale = (tH/tM); | |
114 | qreal tC = categoryCount + 1; |
|
120 | qreal tC = categoryCount + 1; | |
115 | qreal categoryWidth = tW/tC; |
|
121 | qreal categoryWidth = tW/tC; | |
116 | mBarWidth = categoryWidth / (setCount+1); |
|
122 | mBarWidth = categoryWidth / (setCount+1); | |
117 |
|
123 | |||
118 | int itemIndex(0); |
|
124 | int itemIndex(0); | |
119 | for (int category=0; category < categoryCount; category++) { |
|
125 | for (int category=0; category < categoryCount; category++) { | |
120 | qreal xPos = categoryWidth * category + categoryWidth /2 + mBarWidth/2; |
|
126 | qreal xPos = categoryWidth * category + categoryWidth /2 + mBarWidth/2; | |
121 | qreal yPos = mHeight; |
|
127 | qreal yPos = mHeight; | |
122 | for (int set = 0; set < setCount; set++) { |
|
128 | for (int set = 0; set < setCount; set++) { | |
123 | qreal barHeight = mSeries->valueAt(set,category) * scale; |
|
129 | qreal barHeight = mSeries->valueAt(set,category) * scale; | |
124 | Bar* bar = mBars.at(itemIndex); |
|
130 | Bar* bar = mBars.at(itemIndex); | |
125 |
|
131 | |||
126 | // TODO: width settable per bar? |
|
132 | // TODO: width settable per bar? | |
127 | bar->resize(mBarWidth, barHeight); |
|
133 | bar->resize(mBarWidth, barHeight); | |
128 | bar->setPen(mSeries->barsetAt(set)->pen()); |
|
134 | bar->setPen(mSeries->barsetAt(set)->pen()); | |
129 | bar->setBrush(mSeries->barsetAt(set)->brush()); |
|
135 | bar->setBrush(mSeries->barsetAt(set)->brush()); | |
130 | bar->setPos(xPos, yPos-barHeight); |
|
136 | bar->setPos(xPos, yPos-barHeight); | |
131 | itemIndex++; |
|
137 | itemIndex++; | |
132 | xPos += mBarWidth; |
|
138 | xPos += mBarWidth; | |
133 | } |
|
139 | } | |
134 | } |
|
140 | } | |
135 |
|
141 | |||
136 | // Position floating values |
|
142 | // Position floating values | |
137 | itemIndex = 0; |
|
143 | itemIndex = 0; | |
138 | for (int category=0; category < mSeries->categoryCount(); category++) { |
|
144 | for (int category=0; category < mSeries->categoryCount(); category++) { | |
139 | qreal xPos = categoryWidth * category + categoryWidth/2 + mBarWidth; |
|
145 | qreal xPos = categoryWidth * category + categoryWidth/2 + mBarWidth; | |
140 | qreal yPos = mHeight; |
|
146 | qreal yPos = mHeight; | |
141 | for (int set=0; set < mSeries->barsetCount(); set++) { |
|
147 | for (int set=0; set < mSeries->barsetCount(); set++) { | |
142 | qreal barHeight = mSeries->valueAt(set,category) * scale; |
|
148 | qreal barHeight = mSeries->valueAt(set,category) * scale; | |
143 | BarValue* value = mFloatingValues.at(itemIndex); |
|
149 | BarValue* value = mFloatingValues.at(itemIndex); | |
144 |
|
150 | |||
145 | QBarSet* barSet = mSeries->barsetAt(set); |
|
151 | QBarSet* barSet = mSeries->barsetAt(set); | |
146 | value->resize(100,50); // TODO: proper layout for this. |
|
152 | value->resize(100,50); // TODO: proper layout for this. | |
147 | value->setPos(xPos, yPos-barHeight/2); |
|
153 | value->setPos(xPos, yPos-barHeight/2); | |
148 | value->setPen(barSet->floatingValuePen()); |
|
154 | value->setPen(barSet->floatingValuePen()); | |
149 |
|
155 | |||
150 | if (mSeries->valueAt(set,category) != 0) { |
|
156 | if (mSeries->valueAt(set,category) != 0) { | |
151 | value->setValueString(QString::number(mSeries->valueAt(set,category))); |
|
157 | value->setValueString(QString::number(mSeries->valueAt(set,category))); | |
152 | } else { |
|
158 | } else { | |
153 | value->setValueString(QString("")); |
|
159 | value->setValueString(QString("")); | |
154 | } |
|
160 | } | |
155 |
|
161 | |||
156 | itemIndex++; |
|
162 | itemIndex++; | |
157 | xPos += mBarWidth; |
|
163 | xPos += mBarWidth; | |
158 | } |
|
164 | } | |
159 | } |
|
165 | } | |
160 | update(); |
|
166 | update(); | |
161 | } |
|
167 | } | |
162 |
|
168 | |||
163 | BarLayout BarChartItem::calculateLayout() |
|
169 | BarLayout BarChartItem::calculateLayout() | |
164 | { |
|
170 | { | |
165 | BarLayout layout; |
|
171 | BarLayout layout; | |
166 | foreach(Bar* bar, mBars) { |
|
172 | foreach(Bar* bar, mBars) { | |
167 | layout.insert(bar,bar->boundingRect().size()); |
|
173 | layout.insert(bar,bar->boundingRect().size()); | |
168 | } |
|
174 | } | |
169 |
|
175 | |||
170 | return layout; |
|
176 | return layout; | |
171 | } |
|
177 | } | |
172 |
|
178 | |||
173 | void BarChartItem::applyLayout(const BarLayout &layout) |
|
179 | void BarChartItem::applyLayout(const BarLayout &layout) | |
174 | { |
|
180 | { | |
175 | if (m_animator) |
|
181 | if (m_animator) | |
176 | m_animator->updateLayout(this, layout); |
|
182 | m_animator->updateLayout(this, layout); | |
177 | else |
|
183 | else | |
178 | setLayout(layout); |
|
184 | setLayout(layout); | |
179 | } |
|
185 | } | |
180 |
|
186 | |||
181 | void BarChartItem::setLayout(const BarLayout &layout) |
|
187 | void BarChartItem::setLayout(const BarLayout &layout) | |
182 | { |
|
188 | { | |
183 | foreach (Bar *bar, layout.keys()) { |
|
189 | foreach (Bar *bar, layout.keys()) { | |
184 | bar->setSize(layout.value(bar)); |
|
190 | bar->setSize(layout.value(bar)); | |
185 | } |
|
191 | } | |
186 | update(); |
|
192 | update(); | |
187 | } |
|
193 | } | |
188 |
|
194 | |||
189 | void BarChartItem::initAxisLabels() |
|
195 | void BarChartItem::initAxisLabels() | |
190 | { |
|
196 | { | |
191 | int count = mSeries->categoryCount(); |
|
197 | int count = mSeries->categoryCount(); | |
192 | if (0 == count) { |
|
198 | if (0 == count) { | |
193 | return; |
|
199 | return; | |
194 | } |
|
200 | } | |
195 |
|
201 | |||
196 | mChart->axisX()->setTicksCount(count+2); |
|
202 | mChart->axisX()->setTicksCount(count+2); | |
197 |
|
203 | |||
198 | qreal min = 0; |
|
204 | qreal min = 0; | |
199 | qreal max = count+1; |
|
205 | qreal max = count+1; | |
200 |
|
206 | |||
201 | mChart->axisX()->setMin(min); |
|
207 | mChart->axisX()->setMin(min); | |
202 | mChart->axisX()->setMax(max); |
|
208 | mChart->axisX()->setMax(max); | |
203 |
|
209 | |||
204 | QChartAxisCategories* categories = mChart->axisX()->categories(); |
|
210 | QChartAxisCategories* categories = mChart->axisX()->categories(); | |
205 | categories->clear(); |
|
211 | categories->clear(); | |
206 | for (int i=0; i<count; i++) { |
|
212 | for (int i=0; i<count; i++) { | |
207 | categories->insert(i+1,mSeries->categoryName(i)); |
|
213 | categories->insert(i+1,mSeries->categoryName(i)); | |
208 | } |
|
214 | } | |
209 |
|
215 | |||
210 |
|
216 | |||
211 |
|
217 | |||
212 | mChart->axisX()->setLabelsVisible(true); |
|
218 | mChart->axisX()->setLabelsVisible(true); | |
213 | } |
|
219 | } | |
214 |
|
220 | |||
215 | //handlers |
|
221 | //handlers | |
216 |
|
222 | |||
217 | void BarChartItem::handleModelChanged(int index) |
|
223 | void BarChartItem::handleModelChanged(int index) | |
218 | { |
|
224 | { | |
219 | Q_UNUSED(index) |
|
225 | Q_UNUSED(index) | |
220 | dataChanged(); |
|
226 | dataChanged(); | |
221 | } |
|
227 | } | |
222 |
|
228 | |||
223 | void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) |
|
229 | void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) | |
224 | { |
|
230 | { | |
225 | // TODO: |
|
231 | mDomainMinX = minX; | |
226 | Q_UNUSED(minX) |
|
232 | mDomainMaxX = maxX; | |
227 | Q_UNUSED(maxX) |
|
233 | mDomainMinY = minY; | |
228 | Q_UNUSED(minY) |
|
234 | mDomainMaxY = maxY; | |
229 | Q_UNUSED(maxY) |
|
235 | layoutChanged(); | |
230 |
|
236 | |||
231 | /* |
|
237 | /* | |
232 | int count = mSeries->categoryCount(); |
|
238 | int count = mSeries->categoryCount(); | |
233 | if (0 == count) { |
|
239 | if (0 == count) { | |
234 | return; |
|
240 | return; | |
235 | } |
|
241 | } | |
236 |
|
242 | |||
237 | // Position labels to domain |
|
243 | // Position labels to domain | |
238 | qreal min = domain.minX(); |
|
244 | qreal min = domain.minX(); | |
239 | qreal max = domain.maxX(); |
|
245 | qreal max = domain.maxX(); | |
240 | qreal step = (max-min)/count; |
|
246 | qreal step = (max-min)/count; | |
241 | QChartAxisCategories& categories = mChart->axisX()->categories(); |
|
247 | QChartAxisCategories& categories = mChart->axisX()->categories(); | |
242 | categories.clear(); |
|
248 | categories.clear(); | |
243 | for (int i=0; i<count; i++) { |
|
249 | for (int i=0; i<count; i++) { | |
244 | categories.insert(min,mSeries->categoryName(i)); |
|
250 | categories.insert(min,mSeries->categoryName(i)); | |
245 | min += step; |
|
251 | min += step; | |
246 | } |
|
252 | } | |
247 | */ |
|
253 | */ | |
248 | } |
|
254 | } | |
249 |
|
255 | |||
250 | void BarChartItem::handleGeometryChanged(const QRectF& rect) |
|
256 | void BarChartItem::handleGeometryChanged(const QRectF& rect) | |
251 | { |
|
257 | { | |
252 | mWidth = rect.width(); |
|
258 | mWidth = rect.width(); | |
253 | mHeight = rect.height(); |
|
259 | mHeight = rect.height(); | |
254 | layoutChanged(); |
|
260 | layoutChanged(); | |
255 | mLayoutSet = true; |
|
261 | mLayoutSet = true; | |
256 | setPos(rect.topLeft()); |
|
262 | setPos(rect.topLeft()); | |
257 | } |
|
263 | } | |
258 |
|
264 | |||
259 | void BarChartItem::handleLayoutChanged() |
|
265 | void BarChartItem::handleLayoutChanged() | |
260 | { |
|
266 | { | |
261 | BarLayout layout = calculateLayout(); |
|
267 | BarLayout layout = calculateLayout(); | |
262 | applyLayout(layout); |
|
268 | applyLayout(layout); | |
263 | update(); |
|
269 | update(); | |
264 | } |
|
270 | } | |
265 |
|
271 | |||
266 |
|
272 | |||
267 | void BarChartItem::showToolTip(QPoint pos, QString tip) |
|
273 | void BarChartItem::showToolTip(QPoint pos, QString tip) | |
268 | { |
|
274 | { | |
269 | // TODO: cool tooltip instead of default |
|
275 | // TODO: cool tooltip instead of default | |
270 | QToolTip::showText(pos,tip); |
|
276 | QToolTip::showText(pos,tip); | |
271 | } |
|
277 | } | |
272 |
|
278 | |||
273 | #include "moc_barchartitem_p.cpp" |
|
279 | #include "moc_barchartitem_p.cpp" | |
274 |
|
280 | |||
275 | QTCOMMERCIALCHART_END_NAMESPACE |
|
281 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,71 +1,76 | |||||
1 | #ifndef BARCHARTITEM_H |
|
1 | #ifndef BARCHARTITEM_H | |
2 | #define BARCHARTITEM_H |
|
2 | #define BARCHARTITEM_H | |
3 |
|
3 | |||
4 | #include "chartitem_p.h" |
|
4 | #include "chartitem_p.h" | |
5 | #include "qbarseries.h" |
|
5 | #include "qbarseries.h" | |
6 | #include <QPen> |
|
6 | #include <QPen> | |
7 | #include <QBrush> |
|
7 | #include <QBrush> | |
8 | #include <QGraphicsItem> |
|
8 | #include <QGraphicsItem> | |
9 |
|
9 | |||
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
11 |
|
11 | |||
12 | class Bar; |
|
12 | class Bar; | |
13 | class BarValue; |
|
13 | class BarValue; | |
14 | class QChartAxisCategories; |
|
14 | class QChartAxisCategories; | |
15 | class QChart; |
|
15 | class QChart; | |
16 |
|
16 | |||
17 | typedef QHash<Bar*, QSizeF> BarLayout; |
|
17 | typedef QHash<Bar*, QSizeF> BarLayout; | |
18 |
|
18 | |||
19 | class BarChartItem : public QObject, public ChartItem |
|
19 | class BarChartItem : public QObject, public ChartItem | |
20 | { |
|
20 | { | |
21 | Q_OBJECT |
|
21 | Q_OBJECT | |
22 | public: |
|
22 | public: | |
23 | BarChartItem(QBarSeries *series, QChart *parent = 0); |
|
23 | BarChartItem(QBarSeries *series, QChart *parent = 0); | |
24 | virtual ~BarChartItem(); |
|
24 | virtual ~BarChartItem(); | |
25 |
|
25 | |||
26 | public: |
|
26 | public: | |
27 | // From QGraphicsItem |
|
27 | // From QGraphicsItem | |
28 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
28 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |
29 | QRectF boundingRect() const; |
|
29 | QRectF boundingRect() const; | |
30 |
|
30 | |||
31 | // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it |
|
31 | // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it | |
32 | virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes |
|
32 | virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes | |
33 | private slots: |
|
33 | private slots: | |
34 | virtual void layoutChanged(); // layout has changed -> need to recalculate bar sizes |
|
34 | virtual void layoutChanged(); // layout has changed -> need to recalculate bar sizes | |
35 |
|
35 | |||
36 | public: |
|
36 | public: | |
37 | BarLayout calculateLayout(); |
|
37 | BarLayout calculateLayout(); | |
38 | void applyLayout(const BarLayout &layout); |
|
38 | void applyLayout(const BarLayout &layout); | |
39 | void setLayout(const BarLayout &layout); |
|
39 | void setLayout(const BarLayout &layout); | |
40 |
|
40 | |||
41 | protected: |
|
41 | protected: | |
42 | void initAxisLabels(); |
|
42 | void initAxisLabels(); | |
43 |
|
43 | |||
44 | public slots: |
|
44 | public slots: | |
45 | void handleModelChanged(int index); |
|
45 | void handleModelChanged(int index); | |
46 | void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY); |
|
46 | void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY); | |
47 | void handleGeometryChanged(const QRectF& size); |
|
47 | void handleGeometryChanged(const QRectF& size); | |
48 | void handleLayoutChanged(); |
|
48 | void handleLayoutChanged(); | |
49 |
|
49 | |||
50 | // Internal slots |
|
50 | // Internal slots | |
51 | void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled) |
|
51 | void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled) | |
52 |
|
52 | |||
53 | protected: |
|
53 | protected: | |
54 |
|
54 | |||
55 | // TODO: consider these. |
|
55 | // TODO: consider these. | |
56 | int mHeight; // Layout spesific |
|
56 | int mHeight; // Layout spesific | |
57 | int mWidth; |
|
57 | int mWidth; | |
58 | qreal mBarWidth; |
|
58 | qreal mBarWidth; | |
59 |
|
59 | |||
|
60 | qreal mDomainMinX; | |||
|
61 | qreal mDomainMaxX; | |||
|
62 | qreal mDomainMinY; | |||
|
63 | qreal mDomainMaxY; | |||
|
64 | ||||
60 | bool mLayoutSet; // True, if component has been laid out. |
|
65 | bool mLayoutSet; // True, if component has been laid out. | |
61 |
|
66 | |||
62 | // Not owned. |
|
67 | // Not owned. | |
63 | QBarSeries* mSeries; |
|
68 | QBarSeries* mSeries; | |
64 | QList<Bar*> mBars; |
|
69 | QList<Bar*> mBars; | |
65 | QList<BarValue*> mFloatingValues; |
|
70 | QList<BarValue*> mFloatingValues; | |
66 | QChart* mChart; |
|
71 | QChart* mChart; | |
67 | }; |
|
72 | }; | |
68 |
|
73 | |||
69 | QTCOMMERCIALCHART_END_NAMESPACE |
|
74 | QTCOMMERCIALCHART_END_NAMESPACE | |
70 |
|
75 | |||
71 | #endif // BARCHARTITEM_H |
|
76 | #endif // BARCHARTITEM_H |
@@ -1,97 +1,102 | |||||
1 | #include "stackedbarchartitem_p.h" |
|
1 | #include "stackedbarchartitem_p.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barvalue_p.h" |
|
3 | #include "barvalue_p.h" | |
4 | #include "qbarset.h" |
|
4 | #include "qbarset.h" | |
5 | #include <QDebug> |
|
5 | #include <QDebug> | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 | StackedBarChartItem::StackedBarChartItem(QBarSeries *series, QChart *parent) : |
|
9 | StackedBarChartItem::StackedBarChartItem(QBarSeries *series, QChart *parent) : | |
10 | BarChartItem(series,parent) |
|
10 | BarChartItem(series,parent) | |
11 | { |
|
11 | { | |
12 | } |
|
12 | } | |
13 |
|
13 | |||
14 | StackedBarChartItem::~StackedBarChartItem() |
|
14 | StackedBarChartItem::~StackedBarChartItem() | |
15 | { |
|
15 | { | |
16 | } |
|
16 | } | |
17 |
|
17 | |||
18 |
|
18 | |||
19 | void StackedBarChartItem::layoutChanged() |
|
19 | void StackedBarChartItem::layoutChanged() | |
20 | { |
|
20 | { | |
21 | // Scale bars to new layout |
|
21 | // Scale bars to new layout | |
22 | // Layout for bars: |
|
22 | // Layout for bars: | |
23 | if (mSeries->barsetCount() <= 0) { |
|
23 | if (mSeries->barsetCount() <= 0) { | |
24 | qDebug() << "No sets in model!"; |
|
24 | qDebug() << "No sets in model!"; | |
25 | // Nothing to do. |
|
25 | // Nothing to do. | |
26 | return; |
|
26 | return; | |
27 | } |
|
27 | } | |
28 |
|
28 | |||
29 | if (mSeries->categoryCount() == 0) { |
|
29 | if (mSeries->categoryCount() == 0) { | |
30 | qDebug() << "No categories in model!"; |
|
30 | qDebug() << "No categories in model!"; | |
31 | // Nothing to do |
|
31 | // Nothing to do | |
32 | return; |
|
32 | return; | |
33 | } |
|
33 | } | |
34 |
|
34 | |||
35 | if (childItems().count() == 0) { |
|
35 | if (childItems().count() == 0) { | |
36 | qDebug() << "WARNING: StackedBarChartItem::layoutChanged called before graphics items are created!"; |
|
36 | qDebug() << "WARNING: StackedBarChartItem::layoutChanged called before graphics items are created!"; | |
37 | return; |
|
37 | return; | |
38 | } |
|
38 | } | |
39 |
|
39 | |||
40 | // Use temporary qreals for accurancy (we might get some compiler warnings... :) |
|
40 | // Use temporary qreals for accurancy (we might get some compiler warnings... :) | |
41 | qreal maxSum = mSeries->maxCategorySum(); |
|
41 | qreal maxSum = mSeries->maxCategorySum(); | |
|
42 | // Domain: | |||
|
43 | if (mDomainMaxY > maxSum) { | |||
|
44 | maxSum = mDomainMaxY; | |||
|
45 | } | |||
|
46 | ||||
42 | qreal h = mHeight; |
|
47 | qreal h = mHeight; | |
43 | qreal scale = (h / maxSum); |
|
48 | qreal scale = (h / maxSum); | |
44 | qreal tW = mWidth; |
|
49 | qreal tW = mWidth; | |
45 | qreal tC = mSeries->categoryCount() + 1; |
|
50 | qreal tC = mSeries->categoryCount() + 1; | |
46 | qreal cC = mSeries->categoryCount() * 2 + 1; |
|
51 | qreal cC = mSeries->categoryCount() * 2 + 1; | |
47 | mBarWidth = tW / cC; |
|
52 | mBarWidth = tW / cC; | |
48 | qreal xStep = (tW/tC); |
|
53 | qreal xStep = (tW/tC); | |
49 | qreal xPos = ((tW/tC) - mBarWidth / 2); |
|
54 | qreal xPos = ((tW/tC) - mBarWidth / 2); | |
50 |
|
55 | |||
51 | int itemIndex(0); |
|
56 | int itemIndex(0); | |
52 | for (int category = 0; category < mSeries->categoryCount(); category++) { |
|
57 | for (int category = 0; category < mSeries->categoryCount(); category++) { | |
53 | qreal yPos = h; |
|
58 | qreal yPos = h; | |
54 | for (int set=0; set < mSeries->barsetCount(); set++) { |
|
59 | for (int set=0; set < mSeries->barsetCount(); set++) { | |
55 | qreal barHeight = mSeries->valueAt(set, category) * scale; |
|
60 | qreal barHeight = mSeries->valueAt(set, category) * scale; | |
56 | Bar* bar = mBars.at(itemIndex); |
|
61 | Bar* bar = mBars.at(itemIndex); | |
57 |
|
62 | |||
58 | bar->resize(mBarWidth, barHeight); |
|
63 | bar->resize(mBarWidth, barHeight); | |
59 | bar->setPen(mSeries->barsetAt(set)->pen()); |
|
64 | bar->setPen(mSeries->barsetAt(set)->pen()); | |
60 | bar->setBrush(mSeries->barsetAt(set)->brush()); |
|
65 | bar->setBrush(mSeries->barsetAt(set)->brush()); | |
61 | bar->setPos(xPos, yPos-barHeight); |
|
66 | bar->setPos(xPos, yPos-barHeight); | |
62 | itemIndex++; |
|
67 | itemIndex++; | |
63 | yPos -= barHeight; |
|
68 | yPos -= barHeight; | |
64 | } |
|
69 | } | |
65 | xPos += xStep; |
|
70 | xPos += xStep; | |
66 | } |
|
71 | } | |
67 |
|
72 | |||
68 | // Position floating values |
|
73 | // Position floating values | |
69 | itemIndex = 0; |
|
74 | itemIndex = 0; | |
70 | xPos = (tW/tC); |
|
75 | xPos = (tW/tC); | |
71 | for (int category=0; category < mSeries->categoryCount(); category++) { |
|
76 | for (int category=0; category < mSeries->categoryCount(); category++) { | |
72 | qreal yPos = h; |
|
77 | qreal yPos = h; | |
73 | for (int set=0; set < mSeries->barsetCount(); set++) { |
|
78 | for (int set=0; set < mSeries->barsetCount(); set++) { | |
74 | qreal barHeight = mSeries->valueAt(set,category) * scale; |
|
79 | qreal barHeight = mSeries->valueAt(set,category) * scale; | |
75 | BarValue* value = mFloatingValues.at(itemIndex); |
|
80 | BarValue* value = mFloatingValues.at(itemIndex); | |
76 |
|
81 | |||
77 | QBarSet* barSet = mSeries->barsetAt(set); |
|
82 | QBarSet* barSet = mSeries->barsetAt(set); | |
78 | value->resize(100,50); // TODO: proper layout for this. |
|
83 | value->resize(100,50); // TODO: proper layout for this. | |
79 | value->setPos(xPos, yPos-barHeight/2); |
|
84 | value->setPos(xPos, yPos-barHeight/2); | |
80 | value->setPen(barSet->floatingValuePen()); |
|
85 | value->setPen(barSet->floatingValuePen()); | |
81 |
|
86 | |||
82 | if (mSeries->valueAt(set,category) != 0) { |
|
87 | if (mSeries->valueAt(set,category) != 0) { | |
83 | value->setValueString(QString::number(mSeries->valueAt(set,category))); |
|
88 | value->setValueString(QString::number(mSeries->valueAt(set,category))); | |
84 | } else { |
|
89 | } else { | |
85 | value->setValueString(QString("")); |
|
90 | value->setValueString(QString("")); | |
86 | } |
|
91 | } | |
87 |
|
92 | |||
88 | itemIndex++; |
|
93 | itemIndex++; | |
89 | yPos -= barHeight; |
|
94 | yPos -= barHeight; | |
90 | } |
|
95 | } | |
91 | xPos += xStep; |
|
96 | xPos += xStep; | |
92 | } |
|
97 | } | |
93 | } |
|
98 | } | |
94 |
|
99 | |||
95 | #include "moc_stackedbarchartitem_p.cpp" |
|
100 | #include "moc_stackedbarchartitem_p.cpp" | |
96 |
|
101 | |||
97 | QTCOMMERCIALCHART_END_NAMESPACE |
|
102 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now