##// END OF EJS Templates
Refactor piechartdrilldown example a bit
Jani Honkonen -
r408:e1c298a209aa
parent child
Show More
@@ -1,125 +1,113
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 label += " - " + QString::number(this->value())+ "e (";
32 label += " " + QString::number(this->value())+ "e (";
33 33 label += QString::number(this->percentage()*100, 'f', 1) + "%)";
34 34 setLabel(label);
35 35 }
36 36
37 37 private:
38 38 QSeries* m_drilldownSeries;
39 39 QString m_prefix;
40 40 };
41 41
42 42 class DrilldownChart : public QChartView
43 43 {
44 44 Q_OBJECT
45 45 public:
46 46 explicit DrilldownChart(QWidget *parent = 0):QChartView(parent), m_currentSeries(0) {}
47 47
48 48 void changeSeries(QSeries* series)
49 49 {
50 50 if (m_currentSeries)
51 51 removeSeries(m_currentSeries);
52 52 m_currentSeries = series;
53 53 addSeries(series);
54 54 setChartTitle(series->title());
55 55 }
56 56
57 57 public Q_SLOTS:
58 58 void handleSliceClicked(QPieSlice* slice)
59 59 {
60 60 DrilldownSlice* drilldownSlice = static_cast<DrilldownSlice*>(slice);
61 61 changeSeries(drilldownSlice->drilldownSeries());
62 62 }
63 63
64 64 private:
65 65 QSeries* m_currentSeries;
66 66 };
67 67
68 68 int main(int argc, char *argv[])
69 69 {
70 70 QApplication a(argc, argv);
71 71
72 72 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
73 73
74 74 QMainWindow window;
75 75
76 76 DrilldownChart* drilldownChart = new DrilldownChart(&window);
77 77 drilldownChart->setRenderHint(QPainter::Antialiasing);
78 78 drilldownChart->setChartTheme(QChart::ChartThemeVanilla);
79 79
80 80 QPieSeries* yearSeries = new QPieSeries(drilldownChart);
81 81 yearSeries->setTitle("Sales by year - All");
82 82 yearSeries->setHoverHighlighting();
83 83
84 84 QList<QString> months;
85 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 102 QObject::connect(yearSeries, SIGNAL(clicked(QPieSlice*)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*)));
115 103
116 104 drilldownChart->changeSeries(yearSeries);
117 105
118 106 window.setCentralWidget(drilldownChart);
119 107 window.resize(600, 600);
120 108 window.show();
121 109
122 110 return a.exec();
123 111 }
124 112
125 113 #include "main.moc"
General Comments 0
You need to be logged in to leave comments. Login now