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