##// END OF EJS Templates
added example for stacked bar chart.
sauimone -
r95:9f20814aa978
parent child
Show More
@@ -1,5 +1,6
1 1 TEMPLATE = subdirs
2 2 SUBDIRS += linechart \
3 3 zoomlinechart \
4 4 colorlinechart \
5 barchart
5 barchart \
6 stackedbarchart
@@ -1,49 +1,45
1 1 #ifndef BARCHARTSERIES_H
2 2 #define BARCHARTSERIES_H
3 3
4 4 #include <QList>
5 //#include <QRectF>
6 5 #include <QAbstractItemModel>
7 6 #include "qchartseries.h"
8 7 #include "qchartglobal.h"
9 8
10 9 // TODO: Can this class be combined with series?
11 10 class BarGroup;
12 11
13 12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
14 13
15 14 // Container for series
16 15 class QTCOMMERCIALCHART_EXPORT BarChartSeries : public QChartSeries
17 16 {
18 17 Q_OBJECT
19 18 public:
20 19 BarChartSeries(QObject* parent=0);
21 20
22 21 // from QChartSeries
23 22 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; }
24 23
25 24 // TODO: Better data model?
26 25 virtual bool setData(QAbstractItemModel* model);
27 26
28 27 // Methods to find out minimum and maximum values of data
29 28 int min();
30 29 int max();
31 30 int countSeries();
32 31 int countColumns(); // Count items in one series.
33 32 int countTotalItems();
34 33 int valueAt(int row, int column);
35 34
36 35 public Q_SLOTS:
37 36
38 // TODO: wrong place for this... series don't know anything about layout
39 // void chartSizeChanged(QRectF rect);
40
41 37 private:
42 38
43 39 QAbstractItemModel* mModel;
44 40 BarGroup* mBarGroup;
45 41 };
46 42
47 43 QTCOMMERCIALCHART_END_NAMESPACE
48 44
49 45 #endif // BARCHARTSERIES_H
@@ -1,404 +1,427
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 "qxychartseries.h"
7 7 #include "qchartaxis.h"
8 8 #include "barchartseries.h"
9 9 #include "bargroup.h"
10 #include "stackedbarchartseries.h"
11 #include "stackedbargroup.h"
10 12
11 13 #include "xylinechartitem_p.h"
12 14 #include "plotdomain_p.h"
13 15 #include "axisitem_p.h"
14 16 #include <QGraphicsScene>
15 17 #include <QDebug>
16 18
17 19 QTCOMMERCIALCHART_BEGIN_NAMESPACE
18 20
19 21 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
20 22 m_backgroundItem(0),
21 23 m_titleItem(0),
22 24 m_axisXItem(new AxisItem(AxisItem::X_AXIS,this)),
23 25 m_plotDataIndex(0),
24 26 m_marginSize(0)
25 27 {
26 28 // TODO: the default theme?
27 29 setTheme(QChart::ChartThemeDefault);
28 30
29 31 PlotDomain domain;
30 32 m_plotDomainList<<domain;
31 33 m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this);
32 34 m_chartItems<<m_axisXItem;
33 35 m_chartItems<<m_axisYItem.at(0);
34 36 }
35 37
36 38 QChart::~QChart(){}
37 39
38 40 QRectF QChart::boundingRect() const
39 41 {
40 42 return m_rect;
41 43 }
42 44
43 45 void QChart::addSeries(QChartSeries* series)
44 46 {
45 47 // TODO: we should check the series not already added
46 48
47 49 m_chartSeries << series;
48 50
49 51 switch(series->type())
50 52 {
51 53 case QChartSeries::SeriesTypeLine: {
52 54
53 55 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
54 56 // Use color defined by theme in case the series does not define a custom color
55 57
56 58 if (!xyseries->pen().color().isValid() && m_themeColors.count()) //TODO: wtf
57 59 xyseries->setPen(nextColor());
58 60
59 61 m_plotDataIndex = 0 ;
60 62 m_plotDomainList.resize(1);
61 63
62 64 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
63 65
64 66 for (int i = 0 ; i < xyseries->count() ; i++)
65 67 {
66 68 qreal x = xyseries->x(i);
67 69 qreal y = xyseries->y(i);
68 70 domain.m_minX = qMin(domain.m_minX,x);
69 71 domain.m_minY = qMin(domain.m_minY,y);
70 72 domain.m_maxX = qMax(domain.m_maxX,x);
71 73 domain.m_maxY = qMax(domain.m_maxY,y);
72 74 }
73 75
74 76 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
75 77 m_chartItems<<item;
76 78
77 79 foreach(ChartItem* i ,m_chartItems)
78 80 i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
79 81
80 82 break;
81 83 }
82 84 case QChartSeries::SeriesTypeBar: {
83 85
84 86 qDebug() << "barSeries added";
85 87 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
86 88 BarGroup* barGroup = new BarGroup(*barSeries,this);
87 89
88 90 // Add some fugly colors for 5 fist series...
89 91 barGroup->addColor(QColor(255,0,0,128));
90 92 barGroup->addColor(QColor(255,255,0,128));
91 93 barGroup->addColor(QColor(0,255,0,128));
92 94 barGroup->addColor(QColor(0,0,255,128));
93 95 barGroup->addColor(QColor(255,128,0,128));
94 96
95 97 m_chartItems<<barGroup;
96 98 childItems().append(barGroup);
97 99 break;
98 100 }
101 case QChartSeries::SeriesTypeStackedBar: {
102
103 qDebug() << "barSeries added";
104 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
105 StackedBarGroup* stackedBarGroup = new StackedBarGroup(*stackedBarSeries,this);
106
107 // Add some fugly colors for 5 fist series...
108 stackedBarGroup->addColor(QColor(255,0,0,128));
109 stackedBarGroup->addColor(QColor(255,255,0,128));
110 stackedBarGroup->addColor(QColor(0,255,0,128));
111 stackedBarGroup->addColor(QColor(0,0,255,128));
112 stackedBarGroup->addColor(QColor(255,128,0,128));
113
114 m_chartItems<<stackedBarGroup;
115 childItems().append(stackedBarGroup);
116 break;
117 }
99 118 case QChartSeries::SeriesTypeScatter: {
100 119 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
101 120 scatterSeries->d->setParentItem(this);
102 121 // Set pre-defined colors in case the series has no colors defined
103 122 if (!scatterSeries->markerColor().isValid())
104 123 scatterSeries->setMarkerColor(nextColor());
105 124 connect(this, SIGNAL(sizeChanged(QRectF)),
106 125 scatterSeries, SLOT(chartSizeChanged(QRectF)));
107 126 // QColor nextColor = m_themeColors.takeFirst();
108 127 // nextColor.setAlpha(150); // TODO: default opacity?
109 128 // scatterSeries->setMarkerColor(nextColor);
110 129 break;
111 130 }
112 131 case QChartSeries::SeriesTypePie: {
113 132 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
114 133 for (int i(0); i < pieSeries->sliceCount(); i++) {
115 134 if (!pieSeries->sliceColor(i).isValid())
116 135 pieSeries->setSliceColor(i, nextColor());
117 136 }
118 137 connect(this, SIGNAL(sizeChanged(QRectF)),
119 138 pieSeries, SLOT(chartSizeChanged(QRectF)));
120 139
121 140 // Set pre-defined colors in case the series has no colors defined
122 141 // TODO: how to define the color for all the slices of a pie?
123 142 // for (int (i); i < pieSeries.sliceCount(); i++)
124 143 break;
125 144 }
126 145 }
127 146 }
128 147
129 148 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
130 149 {
131 150 // TODO: support also other types; not only scatter and pie
132 151
133 152 QChartSeries *series(0);
134 153
135 154 switch (type) {
136 155 case QChartSeries::SeriesTypeLine: {
137 156 series = QXYChartSeries::create();
138 157 break;
139 158 }
140 159 case QChartSeries::SeriesTypeBar: {
141 160 series = new BarChartSeries(this);
142 161 break;
143 162 }
163 case QChartSeries::SeriesTypeStackedBar: {
164 series = new StackedBarChartSeries(this);
165 break;
166 }
144 167 case QChartSeries::SeriesTypeScatter: {
145 168 series = new QScatterSeries(this);
146 169 break;
147 170 }
148 171 case QChartSeries::SeriesTypePie: {
149 172 series = new QPieSeries(this);
150 173 break;
151 174 }
152 175 default:
153 176 Q_ASSERT(false);
154 177 break;
155 178 }
156 179
157 180 addSeries(series);
158 181 return series;
159 182 }
160 183
161 184 void QChart::setSize(const QSize& size)
162 185 {
163 186 m_rect = QRect(QPoint(0,0),size);
164 187 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
165 188
166 189 //recaculate title
167 190 if(m_titleItem){
168 191 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
169 192 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
170 193
171 194 }
172 195
173 196 //recalculate background gradient
174 197 if(m_backgroundItem){
175 198 m_backgroundItem->setRect(rect);
176 199 if(m_bacgroundOrinetation==HorizonatlGradientOrientation)
177 200 m_backgroundGradient.setFinalStop(m_backgroundItem->rect().width(),0);
178 201 else
179 202 m_backgroundGradient.setFinalStop(0,m_backgroundItem->rect().height());
180 203
181 204 m_backgroundItem->setBrush(m_backgroundGradient);
182 205 }
183 206
184 207 //resize elements
185 208 foreach (ChartItem* item ,m_chartItems) {
186 209 item->setPos(rect.topLeft());
187 210 item->setSize(rect.size());
188 211
189 212 }
190 213 // TODO: TTD for setting scale
191 214 //emit scaleChanged(100, 100);
192 215 // TODO: calculate the origo
193 216 // TODO: not sure if emitting a signal here is the best from performance point of view
194 217 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
195 218
196 219 update();
197 220 }
198 221
199 222 void QChart::setBackground(const QColor& startColor, const QColor& endColor, GradientOrientation orientation)
200 223 {
201 224
202 225 if(!m_backgroundItem){
203 226 m_backgroundItem = new QGraphicsRectItem(this);
204 227 m_backgroundItem->setZValue(-1);
205 228 }
206 229
207 230 m_bacgroundOrinetation = orientation;
208 231 m_backgroundGradient.setColorAt( 0.0, startColor);
209 232 m_backgroundGradient.setColorAt( 1.0, endColor);
210 233 m_backgroundGradient.setStart(0,0);
211 234
212 235 if(orientation == VerticalGradientOrientation){
213 236 m_backgroundGradient.setFinalStop(0,m_rect.height());
214 237 }else{
215 238 m_backgroundGradient.setFinalStop(m_rect.width(),0);
216 239 }
217 240
218 241 m_backgroundItem->setBrush(m_backgroundGradient);
219 242 m_backgroundItem->setPen(Qt::NoPen);
220 243 m_backgroundItem->update();
221 244 }
222 245
223 246 void QChart::setTitle(const QString& title,const QFont& font)
224 247 {
225 248 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
226 249 m_titleItem->setPlainText(title);
227 250 m_titleItem->setFont(font);
228 251 }
229 252
230 253 int QChart::margin() const
231 254 {
232 255 return m_marginSize;
233 256 }
234 257
235 258 void QChart::setMargin(int margin)
236 259 {
237 260 m_marginSize = margin;
238 261 }
239 262
240 263 void QChart::setTheme(QChart::ChartThemeId theme)
241 264 {
242 265 // if (theme != m_currentTheme) {
243 266 m_themeColors.clear();
244 267
245 268 // TODO: define color themes
246 269 switch (theme) {
247 270 case QChart::ChartThemeDefault:
248 271 // TODO: define the default theme based on the OS
249 272 m_themeColors.append(QColor(QRgb(0xff000000)));
250 273 m_themeColors.append(QColor(QRgb(0xff707070)));
251 274 setBackground(QColor(QRgb(0xffffffff)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
252 275 break;
253 276 case QChart::ChartThemeVanilla:
254 277 m_themeColors.append(QColor(217, 197, 116));
255 278 m_themeColors.append(QColor(214, 168, 150));
256 279 m_themeColors.append(QColor(160, 160, 113));
257 280 m_themeColors.append(QColor(210, 210, 52));
258 281 m_themeColors.append(QColor(136, 114, 58));
259 282
260 283 setBackground(QColor(QRgb(0xff9d844d)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
261 284 break;
262 285 case QChart::ChartThemeIcy:
263 286 m_themeColors.append(QColor(0, 3, 165));
264 287 m_themeColors.append(QColor(49, 52, 123));
265 288 m_themeColors.append(QColor(71, 114, 187));
266 289 m_themeColors.append(QColor(48, 97, 87));
267 290 m_themeColors.append(QColor(19, 71, 90));
268 291 m_themeColors.append(QColor(110, 70, 228));
269 292
270 293 setBackground(QColor(QRgb(0xffe4ffff)), QColor(QRgb(0xffe4ffff)), VerticalGradientOrientation);
271 294 break;
272 295 case QChart::ChartThemeGrayscale:
273 296 m_themeColors.append(QColor(0, 0, 0));
274 297 m_themeColors.append(QColor(50, 50, 50));
275 298 m_themeColors.append(QColor(100, 100, 100));
276 299 m_themeColors.append(QColor(140, 140, 140));
277 300 m_themeColors.append(QColor(180, 180, 180));
278 301
279 302 setBackground(QColor(QRgb(0xffffffff)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
280 303 break;
281 304 case QChart::ChartThemeUnnamed1:
282 305 m_themeColors.append(QColor(QRgb(0xff3fa9f5)));
283 306 m_themeColors.append(QColor(QRgb(0xff7AC943)));
284 307 m_themeColors.append(QColor(QRgb(0xffFF931E)));
285 308 m_themeColors.append(QColor(QRgb(0xffFF1D25)));
286 309 m_themeColors.append(QColor(QRgb(0xffFF7BAC)));
287 310
288 311 setBackground(QColor(QRgb(0xfff3dc9e)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
289 312 break;
290 313 default:
291 314 Q_ASSERT(false);
292 315 break;
293 316 }
294 317
295 318 if(m_backgroundItem){
296 319 m_backgroundItem->setBrush(m_backgroundGradient);
297 320 m_backgroundItem->setPen(Qt::NoPen);
298 321 }
299 322
300 323 foreach(QChartSeries* series, m_chartSeries) {
301 324 // TODO: other series interested on themes?
302 325 if (series->type() == QChartSeries::SeriesTypeLine) {
303 326 QXYChartSeries *lineseries = reinterpret_cast<QXYChartSeries *>(series);
304 327 lineseries->setPen(nextColor());
305 328 } else if (series->type() == QChartSeries::SeriesTypeScatter) {
306 329 QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series);
307 330 scatter->setMarkerColor(nextColor());
308 331 } else if (series->type() == QChartSeries::SeriesTypePie) {
309 332 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
310 333 for (int i(0); i < pieSeries->sliceCount(); i++)
311 334 pieSeries->setSliceColor(i, nextColor());
312 335 }
313 336 }
314 337 update();
315 338 }
316 339
317 340 QColor QChart::nextColor()
318 341 {
319 342 QColor nextColor = m_themeColors.first();
320 343 m_themeColors.move(0, m_themeColors.size() - 1);
321 344 return nextColor;
322 345 }
323 346
324 347 void QChart::zoomInToRect(const QRect& rectangle)
325 348 {
326 349
327 350 if(!rectangle.isValid()) return;
328 351
329 352 qreal margin = this->margin();
330 353
331 354 QRect rect = rectangle.normalized();
332 355 rect.translate(-margin, -margin);
333 356
334 357 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
335 358
336 359 PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin);
337 360
338 361 m_plotDomainList.resize(m_plotDataIndex + 1);
339 362 m_plotDomainList<<domain;
340 363 m_plotDataIndex++;
341 364
342 365 foreach (ChartItem* item ,m_chartItems)
343 366 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
344 367 update();
345 368 }
346 369
347 370 void QChart::zoomIn()
348 371 {
349 372 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
350 373 m_plotDataIndex++;
351 374 foreach (ChartItem* item ,m_chartItems)
352 375 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
353 376 update();
354 377 } else {
355 378 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
356 379 rect.setWidth(rect.width()/2);
357 380 rect.setHeight(rect.height()/2);
358 381 rect.moveCenter(m_rect.center());
359 382 zoomInToRect(rect);
360 383 }
361 384 }
362 385
363 386 void QChart::zoomOut()
364 387 {
365 388 if (m_plotDataIndex > 0) {
366 389 m_plotDataIndex--;
367 390 foreach (ChartItem* item ,m_chartItems)
368 391 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
369 392 update();
370 393 }
371 394 }
372 395
373 396 void QChart::zoomReset()
374 397 {
375 398 if (m_plotDataIndex > 0) {
376 399 m_plotDataIndex = 0;
377 400 foreach (ChartItem* item ,m_chartItems)
378 401 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
379 402 update();
380 403 }
381 404 }
382 405
383 406 void QChart::setAxisX(const QChartAxis& axis)
384 407 {
385 408 setAxis(m_axisXItem,axis);
386 409 }
387 410 void QChart::setAxisY(const QChartAxis& axis)
388 411 {
389 412 setAxis(m_axisYItem.at(0),axis);
390 413 }
391 414
392 415 void QChart::setAxisY(const QList<QChartAxis>& axis)
393 416 {
394 417 //TODO not implemented
395 418 }
396 419
397 420 void QChart::setAxis(AxisItem *item, const QChartAxis& axis)
398 421 {
399 422 item->setVisible(axis.isAxisVisible());
400 423 }
401 424
402 425 #include "moc_qchart.cpp"
403 426
404 427 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,95 +1,95
1 1 !include( ../common.pri ) {
2 2 error( Couldn't find the common.pri file! )
3 3 }
4 4
5 5 TARGET = QtCommercialChart
6 6 DESTDIR = $$CHART_BUILD_LIB_DIR
7 7 TEMPLATE = lib
8 8 QT += core \
9 9 gui
10 10
11 11 CONFIG += debug_and_release
12 12 CONFIG(debug, debug|release):TARGET = QtCommercialChartd
13 13
14 14 SOURCES += \
15 15 barchart/barchartseries.cpp \
16 16 barchart/bargroup.cpp \
17 17 barchart/bar.cpp \
18 barchart/stackedbarchartseries.cpp \
19 barchart/stackedbargroup.cpp \
18 20 xylinechart/qxychartseries.cpp \
19 21 xylinechart/xylinechartitem.cpp \
20 22 plotdomain.cpp \
21 23 qscatterseries.cpp \
22 24 qpieseries.cpp \
23 25 qchart.cpp \
24 26 axisitem.cpp \
25 27 qchartwidget.cpp \
26 28 pieslice.cpp \
27 29 qchartview.cpp \
28 30 qchartseries.cpp \
29 qchartaxis.cpp \
30 barchart/stackedbarchartseries.cpp \
31 barchart/stackedbargroup.cpp
31 qchartaxis.cpp
32 32
33 33 PRIVATE_HEADERS += \
34 34 xylinechart/xylinechartitem_p.h \
35 35 plotdomain_p.h \
36 36 qscatterseries_p.h \
37 37 pieslice.h \
38 38 axisitem_p.h \
39 39 chartitem_p.h
40 40
41 41 PUBLIC_HEADERS += \
42 42 qchartseries.h \
43 43 qscatterseries.h \
44 44 qpieseries.h \
45 45 qchart.h \
46 46 qchartwidget.h \
47 47 qchartglobal.h \
48 48 xylinechart/qxychartseries.h \
49 49 barchart/barchartseries.h \
50 50 barchart/bargroup.h \
51 barchart/stackedbarchartseries.h \
52 barchart/stackedbargroup.h \
51 53 qchartview.h \
52 54 qchartaxis.h
53 55
54 HEADERS += $$PUBLIC_HEADERS \
55 barchart/stackedbarchartseries.h \
56 barchart/stackedbargroup.h
56 HEADERS += $$PUBLIC_HEADERS
57 57 HEADERS += $$PRIVATE_HEADERS
58 58
59 59 INCLUDEPATH += xylinechart \
60 60 barchart \
61 61 .
62 62
63 63 OBJECTS_DIR = $$CHART_BUILD_DIR/lib
64 64 MOC_DIR = $$CHART_BUILD_DIR/lib
65 65 UI_DIR = $$CHART_BUILD_DIR/lib
66 66 RCC_DIR = $$CHART_BUILD_DIR/lib
67 67
68 68
69 69 DEFINES += QTCOMMERCIALCHART_LIBRARY
70 70
71 71 public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart
72 72 public_headers.files = $$PUBLIC_HEADERS
73 73 target.path = $$[QT_INSTALL_LIBS]
74 74 INSTALLS += target \
75 75 public_headers
76 76
77 77
78 78 install_build_headers.name = bild_headers
79 79 install_build_headers.output = $$CHART_BUILD_HEADER_DIR/${QMAKE_FILE_BASE}.h
80 80 install_build_headers.input = PUBLIC_HEADERS
81 81 install_build_headers.commands = $$QMAKE_COPY ${QMAKE_FILE_NAME} $$CHART_BUILD_HEADER_DIR
82 82 install_build_headers.CONFIG += target_predeps no_link
83 83 QMAKE_EXTRA_COMPILERS += install_build_headers
84 84
85 85 chartversion.target = qchartversion_p.h
86 86 chartversion.commands = @echo "build_time" > $$chartversion.target;
87 87 chartversion.depends = $$HEADERS $$SOURCES
88 88 PRE_TARGETDEPS += qchartversion_p.h
89 89 QMAKE_CLEAN+= qchartversion_p.h
90 90 QMAKE_EXTRA_TARGETS += chartversion
91 91
92 92 unix:QMAKE_DISTCLEAN += -r $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR
93 93 win32:QMAKE_DISTCLEAN += /Q $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR
94 94
95 95
General Comments 0
You need to be logged in to leave comments. Login now