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