@@ -0,0 +1,126 | |||||
|
1 | #include <QtGui/QApplication> | |||
|
2 | #include <QMainWindow> | |||
|
3 | #include <qchartglobal.h> | |||
|
4 | #include <qchartview.h> | |||
|
5 | #include <qpieseries.h> | |||
|
6 | #include <qpieslice.h> | |||
|
7 | #include <QTime> | |||
|
8 | ||||
|
9 | QTCOMMERCIALCHART_USE_NAMESPACE | |||
|
10 | ||||
|
11 | class DrilldownSlice : public QPieSlice | |||
|
12 | { | |||
|
13 | Q_OBJECT | |||
|
14 | ||||
|
15 | public: | |||
|
16 | DrilldownSlice(qreal value, QString prefix, QSeries* drilldownSeries) | |||
|
17 | :m_drilldownSeries(drilldownSeries), | |||
|
18 | m_prefix(prefix) | |||
|
19 | { | |||
|
20 | setValue(value); | |||
|
21 | setLabelVisible(true); | |||
|
22 | updateLabel(); | |||
|
23 | connect(this, SIGNAL(changed()), this, SLOT(updateLabel())); | |||
|
24 | } | |||
|
25 | ||||
|
26 | QSeries* drilldownSeries() const { return m_drilldownSeries; } | |||
|
27 | ||||
|
28 | public Q_SLOTS: | |||
|
29 | void updateLabel() | |||
|
30 | { | |||
|
31 | QString label = m_prefix; | |||
|
32 | label += " - " + QString::number(this->value())+ "e ("; | |||
|
33 | label += QString::number(this->percentage()*100, 'f', 1) + "%)"; | |||
|
34 | qDebug() << "updateLabel" << label; | |||
|
35 | setLabel(label); | |||
|
36 | } | |||
|
37 | ||||
|
38 | private: | |||
|
39 | QSeries* m_drilldownSeries; | |||
|
40 | QString m_prefix; | |||
|
41 | }; | |||
|
42 | ||||
|
43 | class DrilldownChart : public QChartView | |||
|
44 | { | |||
|
45 | Q_OBJECT | |||
|
46 | public: | |||
|
47 | explicit DrilldownChart(QWidget *parent = 0):QChartView(parent), m_currentSeries(0) {} | |||
|
48 | ||||
|
49 | void changeSeries(QSeries* series) | |||
|
50 | { | |||
|
51 | if (m_currentSeries) | |||
|
52 | removeSeries(m_currentSeries); | |||
|
53 | m_currentSeries = series; | |||
|
54 | addSeries(series); | |||
|
55 | setChartTitle(series->title()); | |||
|
56 | } | |||
|
57 | ||||
|
58 | public Q_SLOTS: | |||
|
59 | void handleSliceClicked(QPieSlice* slice) | |||
|
60 | { | |||
|
61 | DrilldownSlice* drilldownSlice = static_cast<DrilldownSlice*>(slice); | |||
|
62 | changeSeries(drilldownSlice->drilldownSeries()); | |||
|
63 | } | |||
|
64 | ||||
|
65 | private: | |||
|
66 | QSeries* m_currentSeries; | |||
|
67 | }; | |||
|
68 | ||||
|
69 | int main(int argc, char *argv[]) | |||
|
70 | { | |||
|
71 | QApplication a(argc, argv); | |||
|
72 | ||||
|
73 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); | |||
|
74 | ||||
|
75 | QMainWindow window; | |||
|
76 | ||||
|
77 | DrilldownChart* drilldownChart = new DrilldownChart(&window); | |||
|
78 | drilldownChart->setRenderHint(QPainter::Antialiasing); | |||
|
79 | drilldownChart->setChartTheme(QChart::ChartThemeVanilla); | |||
|
80 | ||||
|
81 | QPieSeries* yearSeries = new QPieSeries(drilldownChart); | |||
|
82 | yearSeries->setTitle("Sales by year - All"); | |||
|
83 | yearSeries->setHoverHighlighting(); | |||
|
84 | ||||
|
85 | QList<QString> months; | |||
|
86 | months << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; | |||
|
87 | ||||
|
88 | int max = 1000; | |||
|
89 | ||||
|
90 | QPieSeries* monthSeriesJane = new QPieSeries(drilldownChart); | |||
|
91 | monthSeriesJane->setTitle("Sales by month - Jane"); | |||
|
92 | monthSeriesJane->setHoverHighlighting(); | |||
|
93 | foreach (QString month, months) | |||
|
94 | *monthSeriesJane << new DrilldownSlice(qrand() % max, month, yearSeries); | |||
|
95 | ||||
|
96 | QPieSeries* monthSeriesJohn = new QPieSeries(drilldownChart); | |||
|
97 | monthSeriesJohn->setTitle("Sales by month - John"); | |||
|
98 | monthSeriesJohn->setHoverHighlighting(); | |||
|
99 | foreach (QString month, months) | |||
|
100 | *monthSeriesJohn << new DrilldownSlice(qrand() % max, month, yearSeries); | |||
|
101 | ||||
|
102 | QPieSeries* monthSeriesAxel = new QPieSeries(drilldownChart); | |||
|
103 | monthSeriesAxel->setTitle("Sales by month - Axel"); | |||
|
104 | monthSeriesAxel->setHoverHighlighting(); | |||
|
105 | foreach (QString month, months) | |||
|
106 | *monthSeriesAxel << new DrilldownSlice(qrand() % max, month, yearSeries); | |||
|
107 | ||||
|
108 | *yearSeries << new DrilldownSlice(monthSeriesJane->total(), "Jane", monthSeriesJane); | |||
|
109 | *yearSeries << new DrilldownSlice(monthSeriesJohn->total(), "John", monthSeriesJohn); | |||
|
110 | *yearSeries << new DrilldownSlice(monthSeriesAxel->total(), "Axel", monthSeriesAxel); | |||
|
111 | ||||
|
112 | QObject::connect(monthSeriesJane, SIGNAL(clicked(QPieSlice*)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); | |||
|
113 | QObject::connect(monthSeriesJohn, SIGNAL(clicked(QPieSlice*)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); | |||
|
114 | QObject::connect(monthSeriesAxel, SIGNAL(clicked(QPieSlice*)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); | |||
|
115 | QObject::connect(yearSeries, SIGNAL(clicked(QPieSlice*)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); | |||
|
116 | ||||
|
117 | drilldownChart->changeSeries(yearSeries); | |||
|
118 | ||||
|
119 | window.setCentralWidget(drilldownChart); | |||
|
120 | window.resize(600, 600); | |||
|
121 | window.show(); | |||
|
122 | ||||
|
123 | return a.exec(); | |||
|
124 | } | |||
|
125 | ||||
|
126 | #include "main.moc" |
@@ -0,0 +1,9 | |||||
|
1 | !include( ../example.pri ) { | |||
|
2 | error( "Couldn't find the example.pri file!" ) | |||
|
3 | } | |||
|
4 | TARGET = piechartdrilldown | |||
|
5 | SOURCES += main.cpp | |||
|
6 | HEADERS += | |||
|
7 | ||||
|
8 | ||||
|
9 | MOC_DIR = $$PWD/moc |
@@ -7,6 +7,7 SUBDIRS += linechart \ | |||||
7 | percentbarchart \ |
|
7 | percentbarchart \ | |
8 | scatter \ |
|
8 | scatter \ | |
9 | piechart \ |
|
9 | piechart \ | |
|
10 | piechartdrilldown \ | |||
10 | dynamiclinechart \ |
|
11 | dynamiclinechart \ | |
11 | axischart \ |
|
12 | axischart \ | |
12 | multichart \ |
|
13 | multichart \ |
@@ -188,6 +188,12 void QPieSeries::add(QPieSlice* slice) | |||||
188 | add(QList<QPieSlice*>() << slice); |
|
188 | add(QList<QPieSlice*>() << slice); | |
189 | } |
|
189 | } | |
190 |
|
190 | |||
|
191 | QPieSeries& QPieSeries::operator << (QPieSlice* slice) | |||
|
192 | { | |||
|
193 | add(slice); | |||
|
194 | return *this; | |||
|
195 | } | |||
|
196 | ||||
191 |
|
197 | |||
192 | /*! |
|
198 | /*! | |
193 | Adds a single slice to the series with give \a value and \a name. |
|
199 | Adds a single slice to the series with give \a value and \a name. | |
@@ -348,10 +354,7 void QPieSeries::setClickExplodes(bool enable) | |||||
348 | Convenience method for highlighting a slice when user hovers over the slice. |
|
354 | Convenience method for highlighting a slice when user hovers over the slice. | |
349 | It changes the slice color to be lighter and shows the label of the slice. |
|
355 | It changes the slice color to be lighter and shows the label of the slice. | |
350 | Set \a enable to true to highlight a slice when user hovers on top of it. |
|
356 | Set \a enable to true to highlight a slice when user hovers on top of it. | |
351 |
|
||||
352 | \sa QPieSlice::isExploded(), QPieSlice::setExploded() |
|
|||
353 | */ |
|
357 | */ | |
354 |
|
||||
355 | void QPieSeries::setHoverHighlighting(bool enable) |
|
358 | void QPieSeries::setHoverHighlighting(bool enable) | |
356 | { |
|
359 | { | |
357 | if (enable) { |
|
360 | if (enable) { | |
@@ -364,6 +367,16 void QPieSeries::setHoverHighlighting(bool enable) | |||||
364 | } |
|
367 | } | |
365 |
|
368 | |||
366 | /*! |
|
369 | /*! | |
|
370 | Returns the sum of all slice values in this series. | |||
|
371 | ||||
|
372 | \sa QPieSlice::value(), QPieSlice::setValue() | |||
|
373 | */ | |||
|
374 | qreal QPieSeries::total() const | |||
|
375 | { | |||
|
376 | return m_total; | |||
|
377 | } | |||
|
378 | ||||
|
379 | /*! | |||
367 | \fn void QPieSeries::changed(const QPieSeries::ChangeSet& changeSet) |
|
380 | \fn void QPieSeries::changed(const QPieSeries::ChangeSet& changeSet) | |
368 |
|
381 | |||
369 | This signal emitted when something has changed in the series. |
|
382 | This signal emitted when something has changed in the series. | |
@@ -456,7 +469,6 void QPieSeries::highlightOn(QPieSlice* slice) | |||||
456 | Q_ASSERT(slice); |
|
469 | Q_ASSERT(slice); | |
457 | QColor c = slice->brush().color().lighter(); |
|
470 | QColor c = slice->brush().color().lighter(); | |
458 | slice->setBrush(c); |
|
471 | slice->setBrush(c); | |
459 | slice->setLabelVisible(true); |
|
|||
460 | } |
|
472 | } | |
461 |
|
473 | |||
462 | void QPieSeries::highlightOff(QPieSlice* slice) |
|
474 | void QPieSeries::highlightOff(QPieSlice* slice) | |
@@ -464,7 +476,6 void QPieSeries::highlightOff(QPieSlice* slice) | |||||
464 | Q_ASSERT(slice); |
|
476 | Q_ASSERT(slice); | |
465 | QColor c = slice->brush().color().darker(150); |
|
477 | QColor c = slice->brush().color().darker(150); | |
466 | slice->setBrush(c); |
|
478 | slice->setBrush(c); | |
467 | slice->setLabelVisible(false); |
|
|||
468 | } |
|
479 | } | |
469 |
|
480 | |||
470 | void QPieSeries::updateDerivativeData() |
|
481 | void QPieSeries::updateDerivativeData() | |
@@ -480,7 +491,10 void QPieSeries::updateDerivativeData() | |||||
480 | m_total += s->value(); |
|
491 | m_total += s->value(); | |
481 |
|
492 | |||
482 | // we must have some values |
|
493 | // we must have some values | |
|
494 | if (m_total == 0) { | |||
|
495 | qDebug() << "QPieSeries::updateDerivativeData() total == 0"; | |||
483 | Q_ASSERT(m_total > 0); // TODO: is this the correct way to handle this? |
|
496 | Q_ASSERT(m_total > 0); // TODO: is this the correct way to handle this? | |
|
497 | } | |||
484 |
|
498 | |||
485 | // update slice attributes |
|
499 | // update slice attributes | |
486 | qreal sliceAngle = m_pieStartAngle; |
|
500 | qreal sliceAngle = m_pieStartAngle; |
@@ -62,6 +62,7 public: | |||||
62 | void add(QList<QPieSlice*> slices); |
|
62 | void add(QList<QPieSlice*> slices); | |
63 | void add(QPieSlice* slice); |
|
63 | void add(QPieSlice* slice); | |
64 | QPieSlice* add(qreal value, QString name); |
|
64 | QPieSlice* add(qreal value, QString name); | |
|
65 | QPieSeries& operator << (QPieSlice* slice); | |||
65 | void remove(QPieSlice* slice); |
|
66 | void remove(QPieSlice* slice); | |
66 | void clear(); |
|
67 | void clear(); | |
67 |
|
68 | |||
@@ -78,6 +79,8 public: | |||||
78 | void setClickExplodes(bool enable = true); |
|
79 | void setClickExplodes(bool enable = true); | |
79 | void setHoverHighlighting(bool enable = true); |
|
80 | void setHoverHighlighting(bool enable = true); | |
80 |
|
81 | |||
|
82 | qreal total() const; | |||
|
83 | ||||
81 | // TODO: find slices? |
|
84 | // TODO: find slices? | |
82 | // QList<QPieSlice*> findByValue(qreal value); |
|
85 | // QList<QPieSlice*> findByValue(qreal value); | |
83 | // ... |
|
86 | // ... |
General Comments 0
You need to be logged in to leave comments.
Login now