@@ -1,126 +1,125 | |||
|
1 | 1 | #include <QtGui/QApplication> |
|
2 | 2 | #include <QMainWindow> |
|
3 | 3 | #include <qchartglobal.h> |
|
4 | 4 | #include <qchartview.h> |
|
5 | 5 | #include <qpieseries.h> |
|
6 | 6 | #include <qpieslice.h> |
|
7 | 7 | #include <QTime> |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
10 | 10 | |
|
11 | 11 | class DrilldownSlice : public QPieSlice |
|
12 | 12 | { |
|
13 | 13 | Q_OBJECT |
|
14 | 14 | |
|
15 | 15 | public: |
|
16 | 16 | DrilldownSlice(qreal value, QString prefix, QSeries* drilldownSeries) |
|
17 | 17 | :m_drilldownSeries(drilldownSeries), |
|
18 | 18 | m_prefix(prefix) |
|
19 | 19 | { |
|
20 | 20 | setValue(value); |
|
21 | 21 | setLabelVisible(true); |
|
22 | 22 | updateLabel(); |
|
23 | 23 | connect(this, SIGNAL(changed()), this, SLOT(updateLabel())); |
|
24 | 24 | } |
|
25 | 25 | |
|
26 | 26 | QSeries* drilldownSeries() const { return m_drilldownSeries; } |
|
27 | 27 | |
|
28 | 28 | public Q_SLOTS: |
|
29 | 29 | void updateLabel() |
|
30 | 30 | { |
|
31 | 31 | QString label = m_prefix; |
|
32 | 32 | label += " - " + QString::number(this->value())+ "e ("; |
|
33 | 33 | label += QString::number(this->percentage()*100, 'f', 1) + "%)"; |
|
34 | qDebug() << "updateLabel" << label; | |
|
35 | 34 | setLabel(label); |
|
36 | 35 | } |
|
37 | 36 | |
|
38 | 37 | private: |
|
39 | 38 | QSeries* m_drilldownSeries; |
|
40 | 39 | QString m_prefix; |
|
41 | 40 | }; |
|
42 | 41 | |
|
43 | 42 | class DrilldownChart : public QChartView |
|
44 | 43 | { |
|
45 | 44 | Q_OBJECT |
|
46 | 45 | public: |
|
47 | 46 | explicit DrilldownChart(QWidget *parent = 0):QChartView(parent), m_currentSeries(0) {} |
|
48 | 47 | |
|
49 | 48 | void changeSeries(QSeries* series) |
|
50 | 49 | { |
|
51 | 50 | if (m_currentSeries) |
|
52 | 51 | removeSeries(m_currentSeries); |
|
53 | 52 | m_currentSeries = series; |
|
54 | 53 | addSeries(series); |
|
55 | 54 | setChartTitle(series->title()); |
|
56 | 55 | } |
|
57 | 56 | |
|
58 | 57 | public Q_SLOTS: |
|
59 | 58 | void handleSliceClicked(QPieSlice* slice) |
|
60 | 59 | { |
|
61 | 60 | DrilldownSlice* drilldownSlice = static_cast<DrilldownSlice*>(slice); |
|
62 | 61 | changeSeries(drilldownSlice->drilldownSeries()); |
|
63 | 62 | } |
|
64 | 63 | |
|
65 | 64 | private: |
|
66 | 65 | QSeries* m_currentSeries; |
|
67 | 66 | }; |
|
68 | 67 | |
|
69 | 68 | int main(int argc, char *argv[]) |
|
70 | 69 | { |
|
71 | 70 | QApplication a(argc, argv); |
|
72 | 71 | |
|
73 | 72 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); |
|
74 | 73 | |
|
75 | 74 | QMainWindow window; |
|
76 | 75 | |
|
77 | 76 | DrilldownChart* drilldownChart = new DrilldownChart(&window); |
|
78 | 77 | drilldownChart->setRenderHint(QPainter::Antialiasing); |
|
79 | 78 | drilldownChart->setChartTheme(QChart::ChartThemeVanilla); |
|
80 | 79 | |
|
81 | 80 | QPieSeries* yearSeries = new QPieSeries(drilldownChart); |
|
82 | 81 | yearSeries->setTitle("Sales by year - All"); |
|
83 | 82 | yearSeries->setHoverHighlighting(); |
|
84 | 83 | |
|
85 | 84 | QList<QString> months; |
|
86 | 85 | months << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; |
|
87 | 86 | |
|
88 | 87 | int max = 1000; |
|
89 | 88 | |
|
90 | 89 | QPieSeries* monthSeriesJane = new QPieSeries(drilldownChart); |
|
91 | 90 | monthSeriesJane->setTitle("Sales by month - Jane"); |
|
92 | 91 | monthSeriesJane->setHoverHighlighting(); |
|
93 | 92 | foreach (QString month, months) |
|
94 | 93 | *monthSeriesJane << new DrilldownSlice(qrand() % max, month, yearSeries); |
|
95 | 94 | |
|
96 | 95 | QPieSeries* monthSeriesJohn = new QPieSeries(drilldownChart); |
|
97 | 96 | monthSeriesJohn->setTitle("Sales by month - John"); |
|
98 | 97 | monthSeriesJohn->setHoverHighlighting(); |
|
99 | 98 | foreach (QString month, months) |
|
100 | 99 | *monthSeriesJohn << new DrilldownSlice(qrand() % max, month, yearSeries); |
|
101 | 100 | |
|
102 | 101 | QPieSeries* monthSeriesAxel = new QPieSeries(drilldownChart); |
|
103 | 102 | monthSeriesAxel->setTitle("Sales by month - Axel"); |
|
104 | 103 | monthSeriesAxel->setHoverHighlighting(); |
|
105 | 104 | foreach (QString month, months) |
|
106 | 105 | *monthSeriesAxel << new DrilldownSlice(qrand() % max, month, yearSeries); |
|
107 | 106 | |
|
108 | 107 | *yearSeries << new DrilldownSlice(monthSeriesJane->total(), "Jane", monthSeriesJane); |
|
109 | 108 | *yearSeries << new DrilldownSlice(monthSeriesJohn->total(), "John", monthSeriesJohn); |
|
110 | 109 | *yearSeries << new DrilldownSlice(monthSeriesAxel->total(), "Axel", monthSeriesAxel); |
|
111 | 110 | |
|
112 | 111 | QObject::connect(monthSeriesJane, SIGNAL(clicked(QPieSlice*)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); |
|
113 | 112 | QObject::connect(monthSeriesJohn, SIGNAL(clicked(QPieSlice*)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); |
|
114 | 113 | QObject::connect(monthSeriesAxel, SIGNAL(clicked(QPieSlice*)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); |
|
115 | 114 | QObject::connect(yearSeries, SIGNAL(clicked(QPieSlice*)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); |
|
116 | 115 | |
|
117 | 116 | drilldownChart->changeSeries(yearSeries); |
|
118 | 117 | |
|
119 | 118 | window.setCentralWidget(drilldownChart); |
|
120 | 119 | window.resize(600, 600); |
|
121 | 120 | window.show(); |
|
122 | 121 | |
|
123 | 122 | return a.exec(); |
|
124 | 123 | } |
|
125 | 124 | |
|
126 | 125 | #include "main.moc" |
General Comments 0
You need to be logged in to leave comments.
Login now