@@ -1,433 +1,433 | |||
|
1 | 1 | #include "qchart.h" |
|
2 | 2 | #include "qchartseries.h" |
|
3 | 3 | #include "qscatterseries.h" |
|
4 | 4 | #include "qscatterseries_p.h" |
|
5 | 5 | #include "qpieseries.h" |
|
6 | 6 | #include "qpieseries_p.h" |
|
7 | 7 | #include "qxychartseries.h" |
|
8 | 8 | #include "qchartaxis.h" |
|
9 | 9 | #include "barchartseries.h" |
|
10 | 10 | #include "bargroup.h" |
|
11 | 11 | #include "stackedbarchartseries.h" |
|
12 | 12 | #include "stackedbargroup.h" |
|
13 | 13 | #include "percentbarchartseries.h" |
|
14 | 14 | #include "percentbargroup.h" |
|
15 | 15 | #include "charttheme_p.h" |
|
16 | 16 | #include "chartitem_p.h" |
|
17 | 17 | |
|
18 | 18 | #include "xylinechartitem_p.h" |
|
19 | 19 | #include "plotdomain_p.h" |
|
20 | 20 | #include "axisitem_p.h" |
|
21 | 21 | #include <QGraphicsScene> |
|
22 | 22 | #include <QGraphicsSceneResizeEvent> |
|
23 | 23 | #include <QDebug> |
|
24 | 24 | |
|
25 | 25 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
26 | 26 | |
|
27 | 27 | QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), |
|
28 | 28 | m_backgroundItem(0), |
|
29 | 29 | m_titleItem(0), |
|
30 | 30 | m_axisXItem(new AxisItem(AxisItem::X_AXIS, this)), |
|
31 | 31 | m_plotDataIndex(0), |
|
32 | 32 | m_marginSize(0), |
|
33 | 33 | m_chartTheme(new ChartTheme(this)) |
|
34 | 34 | { |
|
35 | 35 | // TODO: the default theme? |
|
36 | 36 | setTheme(QChart::ChartThemeDefault); |
|
37 | 37 | |
|
38 | 38 | PlotDomain domain; |
|
39 | 39 | m_plotDomainList << domain; |
|
40 | 40 | m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this); |
|
41 | 41 | m_chartItems << m_axisXItem; |
|
42 | 42 | m_chartItems << m_axisYItem.at(0); |
|
43 | 43 | } |
|
44 | 44 | |
|
45 | 45 | QChart::~QChart(){} |
|
46 | 46 | |
|
47 | 47 | void QChart::addSeries(QChartSeries* series) |
|
48 | 48 | { |
|
49 | 49 | // TODO: we should check the series not already added |
|
50 | 50 | Q_ASSERT(series); |
|
51 | 51 | Q_ASSERT(series->type() != QChartSeries::SeriesTypeInvalid); |
|
52 | 52 | |
|
53 | 53 | m_chartSeries << series; |
|
54 | 54 | |
|
55 | 55 | m_plotDataIndex = 0 ; |
|
56 | 56 | m_plotDomainList.resize(1); |
|
57 | 57 | PlotDomain& domain = m_plotDomainList[m_plotDataIndex]; |
|
58 | 58 | |
|
59 | 59 | switch(series->type()) |
|
60 | 60 | { |
|
61 | 61 | case QChartSeries::SeriesTypeLine: { |
|
62 | 62 | |
|
63 | 63 | QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series); |
|
64 | 64 | |
|
65 | 65 | for (int i = 0 ; i < xyseries->count() ; i++) { |
|
66 | 66 | qreal x = xyseries->x(i); |
|
67 | 67 | qreal y = xyseries->y(i); |
|
68 | 68 | domain.m_minX = qMin(domain.m_minX,x); |
|
69 | 69 | domain.m_minY = qMin(domain.m_minY,y); |
|
70 | 70 | domain.m_maxX = qMax(domain.m_maxX,x); |
|
71 | 71 | domain.m_maxY = qMax(domain.m_maxY,y); |
|
72 | 72 | } |
|
73 | 73 | |
|
74 |
XYLineChartItem* item = new XYLineChartItem(xyseries, |
|
|
74 | XYLineChartItem* item = new XYLineChartItem(xyseries, this); | |
|
75 | 75 | |
|
76 | 76 | m_chartItems << item; |
|
77 | 77 | // TODO: |
|
78 | 78 | //m_chartTheme->addObserver(xyseries); |
|
79 | 79 | |
|
80 | 80 | break; |
|
81 | 81 | } |
|
82 | 82 | case QChartSeries::SeriesTypeBar: { |
|
83 | 83 | |
|
84 | 84 | qDebug() << "barSeries added"; |
|
85 | 85 | BarChartSeries* barSeries = static_cast<BarChartSeries*>(series); |
|
86 | 86 | BarGroup* barGroup = new BarGroup(*barSeries,this); |
|
87 | 87 | |
|
88 | 88 | // Add some fugly colors for 5 fist series... |
|
89 | 89 | barGroup->addColor(QColor(255,0,0,128)); |
|
90 | 90 | barGroup->addColor(QColor(255,255,0,128)); |
|
91 | 91 | barGroup->addColor(QColor(0,255,0,128)); |
|
92 | 92 | barGroup->addColor(QColor(0,0,255,128)); |
|
93 | 93 | barGroup->addColor(QColor(255,128,0,128)); |
|
94 | 94 | |
|
95 | 95 | m_chartItems << barGroup; |
|
96 | 96 | childItems().append(barGroup); |
|
97 | 97 | |
|
98 | 98 | qreal x = barSeries->countColumns(); |
|
99 | 99 | qreal y = barSeries->max(); |
|
100 | 100 | domain.m_minX = qMin(domain.m_minX,x); |
|
101 | 101 | domain.m_minY = qMin(domain.m_minY,y); |
|
102 | 102 | domain.m_maxX = qMax(domain.m_maxX,x); |
|
103 | 103 | domain.m_maxY = qMax(domain.m_maxY,y); |
|
104 | 104 | m_axisXItem->setVisible(false); |
|
105 | 105 | break; |
|
106 | 106 | } |
|
107 | 107 | case QChartSeries::SeriesTypeStackedBar: { |
|
108 | 108 | |
|
109 | 109 | qDebug() << "barSeries added"; |
|
110 | 110 | StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series); |
|
111 | 111 | StackedBarGroup* stackedBarGroup = new StackedBarGroup(*stackedBarSeries,this); |
|
112 | 112 | |
|
113 | 113 | // Add some fugly colors for 5 fist series... |
|
114 | 114 | stackedBarGroup->addColor(QColor(255,0,0,128)); |
|
115 | 115 | stackedBarGroup->addColor(QColor(255,255,0,128)); |
|
116 | 116 | stackedBarGroup->addColor(QColor(0,255,0,128)); |
|
117 | 117 | stackedBarGroup->addColor(QColor(0,0,255,128)); |
|
118 | 118 | stackedBarGroup->addColor(QColor(255,128,0,128)); |
|
119 | 119 | |
|
120 | 120 | m_chartItems << stackedBarGroup; |
|
121 | 121 | childItems().append(stackedBarGroup); |
|
122 | 122 | |
|
123 | 123 | qreal x = stackedBarSeries->countColumns(); |
|
124 | 124 | qreal y = stackedBarSeries->maxColumnSum(); |
|
125 | 125 | domain.m_minX = qMin(domain.m_minX,x); |
|
126 | 126 | domain.m_minY = qMin(domain.m_minY,y); |
|
127 | 127 | domain.m_maxX = qMax(domain.m_maxX,x); |
|
128 | 128 | domain.m_maxY = qMax(domain.m_maxY,y); |
|
129 | 129 | m_axisXItem->setVisible(false); |
|
130 | 130 | break; |
|
131 | 131 | } |
|
132 | 132 | case QChartSeries::SeriesTypePercentBar: { |
|
133 | 133 | |
|
134 | 134 | qDebug() << "barSeries added"; |
|
135 | 135 | PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series); |
|
136 | 136 | PercentBarGroup* percentBarGroup = new PercentBarGroup(*percentBarSeries,this); |
|
137 | 137 | |
|
138 | 138 | // Add some fugly colors for 5 fist series... |
|
139 | 139 | percentBarGroup->addColor(QColor(255,0,0,128)); |
|
140 | 140 | percentBarGroup->addColor(QColor(255,255,0,128)); |
|
141 | 141 | percentBarGroup->addColor(QColor(0,255,0,128)); |
|
142 | 142 | percentBarGroup->addColor(QColor(0,0,255,128)); |
|
143 | 143 | percentBarGroup->addColor(QColor(255,128,0,128)); |
|
144 | 144 | |
|
145 | 145 | m_chartItems << percentBarGroup; |
|
146 | 146 | childItems().append(percentBarGroup); |
|
147 | 147 | |
|
148 | 148 | qreal x = percentBarSeries->countColumns(); |
|
149 | 149 | domain.m_minX = qMin(domain.m_minX,x); |
|
150 | 150 | domain.m_minY = 0; |
|
151 | 151 | domain.m_maxX = qMax(domain.m_maxX,x); |
|
152 | 152 | domain.m_maxY = 100; |
|
153 | 153 | m_axisXItem->setVisible(false); |
|
154 | 154 | break; |
|
155 | 155 | } |
|
156 | 156 | case QChartSeries::SeriesTypeScatter: { |
|
157 | 157 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); |
|
158 | 158 | scatterSeries->d->m_theme = m_chartTheme->themeForSeries(); |
|
159 | 159 | scatterSeries->d->setParentItem(this); |
|
160 | 160 | scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); |
|
161 | 161 | m_chartItems << scatterSeries->d; |
|
162 | 162 | m_chartTheme->addObserver(scatterSeries->d); |
|
163 | 163 | |
|
164 | 164 | foreach (qreal x, scatterSeries->d->m_x) { |
|
165 | 165 | domain.m_minX = qMin(domain.m_minX, x); |
|
166 | 166 | domain.m_maxX = qMax(domain.m_maxX, x); |
|
167 | 167 | } |
|
168 | 168 | foreach (qreal y, scatterSeries->d->m_y) { |
|
169 | 169 | domain.m_minY = qMin(domain.m_minY, y); |
|
170 | 170 | domain.m_maxY = qMax(domain.m_maxY, y); |
|
171 | 171 | } |
|
172 | 172 | |
|
173 | 173 | break; |
|
174 | 174 | } |
|
175 | 175 | case QChartSeries::SeriesTypePie: { |
|
176 | 176 | QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series); |
|
177 | 177 | pieSeries->d->setParentItem(this); |
|
178 | 178 | m_chartItems << pieSeries->d; |
|
179 | 179 | pieSeries->d->m_chartTheme = m_chartTheme; |
|
180 | 180 | m_chartTheme->addObserver(pieSeries->d); |
|
181 | 181 | break; |
|
182 | 182 | } |
|
183 | 183 | default: |
|
184 | 184 | break; |
|
185 | 185 | } |
|
186 | 186 | |
|
187 | 187 | // Update all the items to match the new visible area of the chart |
|
188 | 188 | foreach(ChartItem* i, m_chartItems) |
|
189 | 189 | i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex)); |
|
190 | 190 | } |
|
191 | 191 | |
|
192 | 192 | QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type) |
|
193 | 193 | { |
|
194 | 194 | // TODO: support also other types; not only scatter and pie |
|
195 | 195 | |
|
196 | 196 | QChartSeries *series(0); |
|
197 | 197 | |
|
198 | 198 | switch (type) { |
|
199 | 199 | case QChartSeries::SeriesTypeLine: { |
|
200 | 200 | series = QXYChartSeries::create(); |
|
201 | 201 | break; |
|
202 | 202 | } |
|
203 | 203 | case QChartSeries::SeriesTypeBar: { |
|
204 | 204 | series = new BarChartSeries(this); |
|
205 | 205 | break; |
|
206 | 206 | } |
|
207 | 207 | case QChartSeries::SeriesTypeStackedBar: { |
|
208 | 208 | series = new StackedBarChartSeries(this); |
|
209 | 209 | break; |
|
210 | 210 | } |
|
211 | 211 | case QChartSeries::SeriesTypePercentBar: { |
|
212 | 212 | series = new PercentBarChartSeries(this); |
|
213 | 213 | break; |
|
214 | 214 | } |
|
215 | 215 | case QChartSeries::SeriesTypeScatter: { |
|
216 | 216 | series = new QScatterSeries(this); |
|
217 | 217 | break; |
|
218 | 218 | } |
|
219 | 219 | case QChartSeries::SeriesTypePie: { |
|
220 | 220 | series = new QPieSeries(this); |
|
221 | 221 | break; |
|
222 | 222 | } |
|
223 | 223 | default: |
|
224 | 224 | Q_ASSERT(false); |
|
225 | 225 | break; |
|
226 | 226 | } |
|
227 | 227 | |
|
228 | 228 | addSeries(series); |
|
229 | 229 | return series; |
|
230 | 230 | } |
|
231 | 231 | |
|
232 | 232 | void QChart::setBackground(const QColor& startColor, const QColor& endColor, GradientOrientation orientation) |
|
233 | 233 | { |
|
234 | 234 | |
|
235 | 235 | if(!m_backgroundItem){ |
|
236 | 236 | m_backgroundItem = new QGraphicsRectItem(this); |
|
237 | 237 | m_backgroundItem->setZValue(-1); |
|
238 | 238 | } |
|
239 | 239 | |
|
240 | 240 | m_bacgroundOrinetation = orientation; |
|
241 | 241 | m_backgroundGradient.setColorAt(0.0, startColor); |
|
242 | 242 | m_backgroundGradient.setColorAt(1.0, endColor); |
|
243 | 243 | m_backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); |
|
244 | 244 | |
|
245 | 245 | if(orientation == VerticalGradientOrientation){ |
|
246 | 246 | m_backgroundGradient.setFinalStop(0,1); |
|
247 | 247 | }else{ |
|
248 | 248 | m_backgroundGradient.setFinalStop(1,0); |
|
249 | 249 | } |
|
250 | 250 | |
|
251 | 251 | m_backgroundItem->setBrush(m_backgroundGradient); |
|
252 | 252 | m_backgroundItem->setPen(Qt::NoPen); |
|
253 | 253 | m_backgroundItem->update(); |
|
254 | 254 | } |
|
255 | 255 | |
|
256 | 256 | void QChart::setChartBackgroundBrush(const QBrush& brush) |
|
257 | 257 | { |
|
258 | 258 | |
|
259 | 259 | if(!m_backgroundItem){ |
|
260 | 260 | m_backgroundItem = new QGraphicsRectItem(this); |
|
261 | 261 | m_backgroundItem->setZValue(-1); |
|
262 | 262 | } |
|
263 | 263 | |
|
264 | 264 | m_backgroundItem->setBrush(brush); |
|
265 | 265 | m_backgroundItem->update(); |
|
266 | 266 | } |
|
267 | 267 | |
|
268 | 268 | void QChart::setChartBackgroundPen(const QPen& pen) |
|
269 | 269 | { |
|
270 | 270 | |
|
271 | 271 | if(!m_backgroundItem){ |
|
272 | 272 | m_backgroundItem = new QGraphicsRectItem(this); |
|
273 | 273 | m_backgroundItem->setZValue(-1); |
|
274 | 274 | } |
|
275 | 275 | |
|
276 | 276 | m_backgroundItem->setPen(pen); |
|
277 | 277 | m_backgroundItem->update(); |
|
278 | 278 | } |
|
279 | 279 | |
|
280 | 280 | void QChart::setTitle(const QString& title,const QFont& font) |
|
281 | 281 | { |
|
282 | 282 | if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this); |
|
283 | 283 | m_titleItem->setPlainText(title); |
|
284 | 284 | m_titleItem->setFont(font); |
|
285 | 285 | } |
|
286 | 286 | |
|
287 | 287 | int QChart::margin() const |
|
288 | 288 | { |
|
289 | 289 | return m_marginSize; |
|
290 | 290 | } |
|
291 | 291 | |
|
292 | 292 | void QChart::setMargin(int margin) |
|
293 | 293 | { |
|
294 | 294 | m_marginSize = margin; |
|
295 | 295 | } |
|
296 | 296 | |
|
297 | 297 | void QChart::setTheme(QChart::ChartThemeId theme) |
|
298 | 298 | { |
|
299 | 299 | m_chartTheme->setTheme(theme); |
|
300 | 300 | setBackground(m_chartTheme->d->m_gradientStartColor, |
|
301 | 301 | m_chartTheme->d->m_gradientEndColor, |
|
302 | 302 | m_bacgroundOrinetation); |
|
303 | 303 | |
|
304 | 304 | // TODO: Move the controlling of the series presentations into private implementation of the |
|
305 | 305 | // series instead of QChart controlling themes for each |
|
306 | 306 | // In other words, the following should be used when creating xy series: |
|
307 | 307 | // m_chartTheme->addObserver(xyseries) |
|
308 | 308 | foreach (QChartSeries *series, m_chartSeries) { |
|
309 | 309 | if (series->type() == QChartSeries::SeriesTypeLine) { |
|
310 | 310 | QXYChartSeries *xyseries = static_cast<QXYChartSeries *>(series); |
|
311 | 311 | SeriesTheme seriesTheme = m_chartTheme->themeForSeries(); |
|
312 | 312 | xyseries->setPen(seriesTheme.linePen); |
|
313 | 313 | } |
|
314 | 314 | } |
|
315 | 315 | |
|
316 | 316 | update(); |
|
317 | 317 | } |
|
318 | 318 | |
|
319 | 319 | QChart::ChartThemeId QChart::theme() |
|
320 | 320 | { |
|
321 | 321 | return (QChart::ChartThemeId) m_chartTheme->d->m_currentTheme; |
|
322 | 322 | } |
|
323 | 323 | |
|
324 | 324 | void QChart::zoomInToRect(const QRectF& rectangle) |
|
325 | 325 | { |
|
326 | 326 | |
|
327 | 327 | if(!rectangle.isValid()) return; |
|
328 | 328 | |
|
329 | 329 | qreal margin = this->margin(); |
|
330 | 330 | |
|
331 | 331 | QRectF rect = rectangle.normalized(); |
|
332 | 332 | rect.translate(-margin, -margin); |
|
333 | 333 | |
|
334 | 334 | PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex]; |
|
335 | 335 | |
|
336 | 336 | PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin); |
|
337 | 337 | |
|
338 | 338 | m_plotDomainList.resize(m_plotDataIndex + 1); |
|
339 | 339 | m_plotDomainList<<domain; |
|
340 | 340 | m_plotDataIndex++; |
|
341 | 341 | |
|
342 | 342 | foreach (ChartItem* item, m_chartItems) |
|
343 | 343 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); |
|
344 | 344 | update(); |
|
345 | 345 | } |
|
346 | 346 | |
|
347 | 347 | void QChart::zoomIn() |
|
348 | 348 | { |
|
349 | 349 | if (m_plotDataIndex < m_plotDomainList.count() - 1) { |
|
350 | 350 | m_plotDataIndex++; |
|
351 | 351 | foreach (ChartItem* item, m_chartItems) |
|
352 | 352 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); |
|
353 | 353 | update(); |
|
354 | 354 | } else { |
|
355 | 355 | QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); |
|
356 | 356 | rect.setWidth(rect.width()/2); |
|
357 | 357 | rect.setHeight(rect.height()/2); |
|
358 | 358 | rect.moveCenter(m_rect.center()); |
|
359 | 359 | zoomInToRect(rect); |
|
360 | 360 | } |
|
361 | 361 | } |
|
362 | 362 | |
|
363 | 363 | void QChart::zoomOut() |
|
364 | 364 | { |
|
365 | 365 | if (m_plotDataIndex > 0) { |
|
366 | 366 | m_plotDataIndex--; |
|
367 | 367 | foreach (ChartItem* item, m_chartItems) |
|
368 | 368 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); |
|
369 | 369 | update(); |
|
370 | 370 | } |
|
371 | 371 | } |
|
372 | 372 | |
|
373 | 373 | void QChart::zoomReset() |
|
374 | 374 | { |
|
375 | 375 | if (m_plotDataIndex > 0) { |
|
376 | 376 | m_plotDataIndex = 0; |
|
377 | 377 | foreach (ChartItem* item, m_chartItems) |
|
378 | 378 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); |
|
379 | 379 | update(); |
|
380 | 380 | } |
|
381 | 381 | } |
|
382 | 382 | |
|
383 | 383 | void QChart::setAxisX(const QChartAxis& axis) |
|
384 | 384 | { |
|
385 | 385 | setAxis(m_axisXItem,axis); |
|
386 | 386 | } |
|
387 | 387 | void QChart::setAxisY(const QChartAxis& axis) |
|
388 | 388 | { |
|
389 | 389 | setAxis(m_axisYItem.at(0),axis); |
|
390 | 390 | } |
|
391 | 391 | |
|
392 | 392 | void QChart::setAxisY(const QList<QChartAxis>& axis) |
|
393 | 393 | { |
|
394 | 394 | //TODO not implemented |
|
395 | 395 | } |
|
396 | 396 | |
|
397 | 397 | void QChart::setAxis(AxisItem *item, const QChartAxis& axis) |
|
398 | 398 | { |
|
399 | 399 | item->setVisible(axis.isAxisVisible()); |
|
400 | 400 | } |
|
401 | 401 | |
|
402 | 402 | void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
403 | 403 | { |
|
404 | 404 | |
|
405 | 405 | m_rect = QRectF(QPoint(0,0),event->newSize()); |
|
406 | 406 | QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); |
|
407 | 407 | |
|
408 | 408 | // recalculate title position |
|
409 | 409 | if (m_titleItem) { |
|
410 | 410 | QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); |
|
411 | 411 | m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2); |
|
412 | 412 | } |
|
413 | 413 | |
|
414 | 414 | //recalculate background gradient |
|
415 | 415 | if (m_backgroundItem) { |
|
416 | 416 | m_backgroundItem->setRect(rect); |
|
417 | 417 | } |
|
418 | 418 | |
|
419 | 419 | // resize and reposition childs |
|
420 | 420 | foreach (ChartItem *item, m_chartItems) { |
|
421 | 421 | item->setPos(rect.topLeft()); |
|
422 | 422 | item->setSize(rect.size()); |
|
423 | 423 | } |
|
424 | 424 | |
|
425 | 425 | QGraphicsWidget::resizeEvent(event); |
|
426 | 426 | update(); |
|
427 | 427 | } |
|
428 | 428 | |
|
429 | 429 | |
|
430 | 430 | |
|
431 | 431 | #include "moc_qchart.cpp" |
|
432 | 432 | |
|
433 | 433 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now