##// END OF EJS Templates
Fix piechartdrilldown bug. Chart was deleting the series from us.
Jani Honkonen -
r617:fd7ce4629ee8
parent child
Show More
@@ -1,111 +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 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 // NOTE: if the series is owned by the chart it will be deleted
51 // here the "window" owns the series...
50 52 if (m_currentSeries)
51 53 removeSeries(m_currentSeries);
52 54 m_currentSeries = series;
53 55 addSeries(series);
54 56 setChartTitle(series->title());
55 57 }
56 58
57 59 public Q_SLOTS:
58 60 void handleSliceClicked(QPieSlice* slice)
59 61 {
60 62 DrilldownSlice* drilldownSlice = static_cast<DrilldownSlice*>(slice);
61 63 changeSeries(drilldownSlice->drilldownSeries());
62 64 }
63 65
64 66 private:
65 67 QSeries* m_currentSeries;
66 68 };
67 69
68 70 int main(int argc, char *argv[])
69 71 {
70 72 QApplication a(argc, argv);
71 73
72 74 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
73 75
74 76 QMainWindow window;
75 77
76 78 DrilldownChart* drilldownChart = new DrilldownChart(&window);
77 79 drilldownChart->setRenderHint(QPainter::Antialiasing);
78 80 drilldownChart->setChartTheme(QChart::ChartThemeVanilla);
79 81
80 QPieSeries* yearSeries = new QPieSeries(drilldownChart);
82 QPieSeries* yearSeries = new QPieSeries(&window);
81 83 yearSeries->setTitle("Sales by year - All");
82 84
83 85 QList<QString> months;
84 86 months << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
85 87 QList<QString> names;
86 88 names << "Jane" << "John" << "Axel" << "Mary" << "Samantha" << "Bob";
87 89
88 90 foreach (QString name, names) {
89 QPieSeries* series = new QPieSeries(drilldownChart);
91 QPieSeries* series = new QPieSeries(&window);
90 92 series->setTitle("Sales by month - " + name);
91 93
92 94 foreach (QString month, months)
93 95 *series << new DrilldownSlice(qrand() % 1000, month, yearSeries);
94 96
95 97 QObject::connect(series, SIGNAL(clicked(QPieSlice*)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*)));
96 98
97 99 *yearSeries << new DrilldownSlice(series->total(), name, series);
98 100 }
99 101
100 102 QObject::connect(yearSeries, SIGNAL(clicked(QPieSlice*)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*)));
101 103
102 104 drilldownChart->changeSeries(yearSeries);
103 105
104 106 window.setCentralWidget(drilldownChart);
105 window.resize(600, 600);
107 window.resize(800, 600);
106 108 window.show();
107 109
108 110 return a.exec();
109 111 }
110 112
111 113 #include "main.moc"
General Comments 0
You need to be logged in to leave comments. Login now