@@ -1,135 +1,134 | |||||
1 | /**************************************************************************** |
|
1 | /**************************************************************************** | |
2 | ** |
|
2 | ** | |
3 | ** Copyright (C) 2012 Digia Plc |
|
3 | ** Copyright (C) 2012 Digia Plc | |
4 | ** All rights reserved. |
|
4 | ** All rights reserved. | |
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
6 | ** |
|
6 | ** | |
7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
8 | ** |
|
8 | ** | |
9 | ** $QT_BEGIN_LICENSE$ |
|
9 | ** $QT_BEGIN_LICENSE$ | |
10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
12 | ** Software or, alternatively, in accordance with the terms contained in |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
13 | ** a written agreement between you and Digia. |
|
13 | ** a written agreement between you and Digia. | |
14 | ** |
|
14 | ** | |
15 | ** If you have questions regarding the use of this file, please use |
|
15 | ** If you have questions regarding the use of this file, please use | |
16 | ** contact form at http://qt.digia.com |
|
16 | ** contact form at http://qt.digia.com | |
17 | ** $QT_END_LICENSE$ |
|
17 | ** $QT_END_LICENSE$ | |
18 | ** |
|
18 | ** | |
19 | ****************************************************************************/ |
|
19 | ****************************************************************************/ | |
20 |
|
20 | |||
21 | #include <QtGui/QApplication> |
|
21 | #include <QtGui/QApplication> | |
22 | #include <QMainWindow> |
|
22 | #include <QMainWindow> | |
23 | #include <qchartglobal.h> |
|
|||
24 | #include <qchartview.h> |
|
|||
25 | #include <qpieseries.h> |
|
|||
26 | #include <qpieslice.h> |
|
|||
27 | #include <QTime> |
|
23 | #include <QTime> | |
|
24 | #include <QChartView> | |||
|
25 | #include <QPieSeries> | |||
|
26 | #include <QPieSlice> | |||
28 |
|
27 | |||
29 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
28 | QTCOMMERCIALCHART_USE_NAMESPACE | |
30 |
|
29 | |||
31 | class DrilldownSlice : public QPieSlice |
|
30 | class DrilldownSlice : public QPieSlice | |
32 | { |
|
31 | { | |
33 | Q_OBJECT |
|
32 | Q_OBJECT | |
34 |
|
33 | |||
35 | public: |
|
34 | public: | |
36 | DrilldownSlice(qreal value, QString prefix, QSeries* drilldownSeries) |
|
35 | DrilldownSlice(qreal value, QString prefix, QSeries* drilldownSeries) | |
37 | :m_drilldownSeries(drilldownSeries), |
|
36 | :m_drilldownSeries(drilldownSeries), | |
38 | m_prefix(prefix) |
|
37 | m_prefix(prefix) | |
39 | { |
|
38 | { | |
40 | setValue(value); |
|
39 | setValue(value); | |
41 | setLabelVisible(true); |
|
40 | setLabelVisible(true); | |
42 | updateLabel(); |
|
41 | updateLabel(); | |
43 | connect(this, SIGNAL(changed()), this, SLOT(updateLabel())); |
|
42 | connect(this, SIGNAL(changed()), this, SLOT(updateLabel())); | |
44 | } |
|
43 | } | |
45 |
|
44 | |||
46 | QSeries* drilldownSeries() const { return m_drilldownSeries; } |
|
45 | QSeries* drilldownSeries() const { return m_drilldownSeries; } | |
47 |
|
46 | |||
48 | public Q_SLOTS: |
|
47 | public Q_SLOTS: | |
49 | void updateLabel() |
|
48 | void updateLabel() | |
50 | { |
|
49 | { | |
51 | QString label = m_prefix; |
|
50 | QString label = m_prefix; | |
52 | label += " " + QString::number(this->value())+ "e ("; |
|
51 | label += " " + QString::number(this->value())+ "e ("; | |
53 | label += QString::number(this->percentage()*100, 'f', 1) + "%)"; |
|
52 | label += QString::number(this->percentage()*100, 'f', 1) + "%)"; | |
54 | setLabel(label); |
|
53 | setLabel(label); | |
55 | } |
|
54 | } | |
56 |
|
55 | |||
57 | private: |
|
56 | private: | |
58 | QSeries* m_drilldownSeries; |
|
57 | QSeries* m_drilldownSeries; | |
59 | QString m_prefix; |
|
58 | QString m_prefix; | |
60 | }; |
|
59 | }; | |
61 |
|
60 | |||
62 | class DrilldownChart : public QChart |
|
61 | class DrilldownChart : public QChart | |
63 | { |
|
62 | { | |
64 | Q_OBJECT |
|
63 | Q_OBJECT | |
65 | public: |
|
64 | public: | |
66 | explicit DrilldownChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0):QChart(parent, wFlags), m_currentSeries(0) {} |
|
65 | explicit DrilldownChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0):QChart(parent, wFlags), m_currentSeries(0) {} | |
67 |
|
66 | |||
68 | void changeSeries(QSeries* series) |
|
67 | void changeSeries(QSeries* series) | |
69 | { |
|
68 | { | |
70 | // NOTE: if the series is owned by the chart it will be deleted |
|
69 | // NOTE: if the series is owned by the chart it will be deleted | |
71 | // here the "window" owns the series... |
|
70 | // here the "window" owns the series... | |
72 | if (m_currentSeries) |
|
71 | if (m_currentSeries) | |
73 | removeSeries(m_currentSeries); |
|
72 | removeSeries(m_currentSeries); | |
74 | m_currentSeries = series; |
|
73 | m_currentSeries = series; | |
75 | addSeries(series); |
|
74 | addSeries(series); | |
76 | setTitle(series->name()); |
|
75 | setTitle(series->name()); | |
77 | } |
|
76 | } | |
78 |
|
77 | |||
79 | public Q_SLOTS: |
|
78 | public Q_SLOTS: | |
80 | void handleSliceClicked(QPieSlice* slice) |
|
79 | void handleSliceClicked(QPieSlice* slice) | |
81 | { |
|
80 | { | |
82 | DrilldownSlice* drilldownSlice = static_cast<DrilldownSlice*>(slice); |
|
81 | DrilldownSlice* drilldownSlice = static_cast<DrilldownSlice*>(slice); | |
83 | changeSeries(drilldownSlice->drilldownSeries()); |
|
82 | changeSeries(drilldownSlice->drilldownSeries()); | |
84 | } |
|
83 | } | |
85 |
|
84 | |||
86 | private: |
|
85 | private: | |
87 | QSeries* m_currentSeries; |
|
86 | QSeries* m_currentSeries; | |
88 | }; |
|
87 | }; | |
89 |
|
88 | |||
90 | int main(int argc, char *argv[]) |
|
89 | int main(int argc, char *argv[]) | |
91 | { |
|
90 | { | |
92 | QApplication a(argc, argv); |
|
91 | QApplication a(argc, argv); | |
93 |
|
92 | |||
94 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); |
|
93 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); | |
95 |
|
94 | |||
96 | QMainWindow window; |
|
95 | QMainWindow window; | |
97 |
|
96 | |||
98 | DrilldownChart* drilldownChart = new DrilldownChart(); |
|
97 | DrilldownChart* drilldownChart = new DrilldownChart(); | |
99 | drilldownChart->setTheme(QChart::ChartThemeLight); |
|
98 | drilldownChart->setTheme(QChart::ChartThemeLight); | |
100 | drilldownChart->setAnimationOptions(QChart::AllAnimations); |
|
99 | drilldownChart->setAnimationOptions(QChart::AllAnimations); | |
101 |
|
100 | |||
102 | QPieSeries* yearSeries = new QPieSeries(&window); |
|
101 | QPieSeries* yearSeries = new QPieSeries(&window); | |
103 | yearSeries->setName("Sales by year - All"); |
|
102 | yearSeries->setName("Sales by year - All"); | |
104 |
|
103 | |||
105 | QList<QString> months; |
|
104 | QList<QString> months; | |
106 | months << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; |
|
105 | months << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; | |
107 | QList<QString> names; |
|
106 | QList<QString> names; | |
108 | names << "Jane" << "John" << "Axel" << "Mary" << "Samantha" << "Bob"; |
|
107 | names << "Jane" << "John" << "Axel" << "Mary" << "Samantha" << "Bob"; | |
109 |
|
108 | |||
110 | foreach (QString name, names) { |
|
109 | foreach (QString name, names) { | |
111 | QPieSeries* series = new QPieSeries(&window); |
|
110 | QPieSeries* series = new QPieSeries(&window); | |
112 | series->setName("Sales by month - " + name); |
|
111 | series->setName("Sales by month - " + name); | |
113 |
|
112 | |||
114 | foreach (QString month, months) |
|
113 | foreach (QString month, months) | |
115 | *series << new DrilldownSlice(qrand() % 1000, month, yearSeries); |
|
114 | *series << new DrilldownSlice(qrand() % 1000, month, yearSeries); | |
116 |
|
115 | |||
117 | QObject::connect(series, SIGNAL(clicked(QPieSlice*, Qt::MouseButtons)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); |
|
116 | QObject::connect(series, SIGNAL(clicked(QPieSlice*, Qt::MouseButtons)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); | |
118 |
|
117 | |||
119 | *yearSeries << new DrilldownSlice(series->total(), name, series); |
|
118 | *yearSeries << new DrilldownSlice(series->total(), name, series); | |
120 | } |
|
119 | } | |
121 |
|
120 | |||
122 | QObject::connect(yearSeries, SIGNAL(clicked(QPieSlice*, Qt::MouseButtons)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); |
|
121 | QObject::connect(yearSeries, SIGNAL(clicked(QPieSlice*, Qt::MouseButtons)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); | |
123 |
|
122 | |||
124 | drilldownChart->changeSeries(yearSeries); |
|
123 | drilldownChart->changeSeries(yearSeries); | |
125 |
|
124 | |||
126 | QChartView* chartView = new QChartView(drilldownChart); |
|
125 | QChartView* chartView = new QChartView(drilldownChart); | |
127 | chartView->setRenderHint(QPainter::Antialiasing); |
|
126 | chartView->setRenderHint(QPainter::Antialiasing); | |
128 | window.setCentralWidget(chartView); |
|
127 | window.setCentralWidget(chartView); | |
129 | window.resize(800, 600); |
|
128 | window.resize(800, 600); | |
130 | window.show(); |
|
129 | window.show(); | |
131 |
|
130 | |||
132 | return a.exec(); |
|
131 | return a.exec(); | |
133 | } |
|
132 | } | |
134 |
|
133 | |||
135 | #include "main.moc" |
|
134 | #include "main.moc" |
@@ -1,137 +1,135 | |||||
1 | /**************************************************************************** |
|
1 | /**************************************************************************** | |
2 | ** |
|
2 | ** | |
3 | ** Copyright (C) 2012 Digia Plc |
|
3 | ** Copyright (C) 2012 Digia Plc | |
4 | ** All rights reserved. |
|
4 | ** All rights reserved. | |
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
6 | ** |
|
6 | ** | |
7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
8 | ** |
|
8 | ** | |
9 | ** $QT_BEGIN_LICENSE$ |
|
9 | ** $QT_BEGIN_LICENSE$ | |
10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
12 | ** Software or, alternatively, in accordance with the terms contained in |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
13 | ** a written agreement between you and Digia. |
|
13 | ** a written agreement between you and Digia. | |
14 | ** |
|
14 | ** | |
15 | ** If you have questions regarding the use of this file, please use |
|
15 | ** If you have questions regarding the use of this file, please use | |
16 | ** contact form at http://qt.digia.com |
|
16 | ** contact form at http://qt.digia.com | |
17 | ** $QT_END_LICENSE$ |
|
17 | ** $QT_END_LICENSE$ | |
18 | ** |
|
18 | ** | |
19 | ****************************************************************************/ |
|
19 | ****************************************************************************/ | |
20 |
|
20 | |||
21 | #include "splinewidget.h" |
|
21 | #include "splinewidget.h" | |
22 | #include "qchartview.h" |
|
|||
23 | #include "qlineseries.h" |
|
|||
24 | #include <QGridLayout> |
|
22 | #include <QGridLayout> | |
25 | #include <QPushButton> |
|
23 | #include <QPushButton> | |
26 | #include "qchartaxis.h" |
|
|||
27 | #include <qmath.h> |
|
|||
28 | #include <QTime> |
|
24 | #include <QTime> | |
|
25 | #include <QChartView> | |||
|
26 | #include <QSplineSeries> | |||
29 |
|
27 | |||
30 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
28 | QTCOMMERCIALCHART_USE_NAMESPACE | |
31 |
|
29 | |||
32 | SplineWidget::SplineWidget(QWidget *parent) |
|
30 | SplineWidget::SplineWidget(QWidget *parent) | |
33 | : QWidget(parent) |
|
31 | : QWidget(parent) | |
34 | { |
|
32 | { | |
35 | // qsrand(time(NULL)); |
|
33 | // qsrand(time(NULL)); | |
36 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); |
|
34 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); | |
37 | //! [1] |
|
35 | //! [1] | |
38 | //create QSplineSeries |
|
36 | //create QSplineSeries | |
39 | series = new QSplineSeries(this); |
|
37 | series = new QSplineSeries(this); | |
40 | //! [1] |
|
38 | //! [1] | |
41 |
|
39 | |||
42 | //! [2] |
|
40 | //! [2] | |
43 | // customize the series presentation settings |
|
41 | // customize the series presentation settings | |
44 | QPen seriesPen(Qt::blue); |
|
42 | QPen seriesPen(Qt::blue); | |
45 | seriesPen.setWidth(3); |
|
43 | seriesPen.setWidth(3); | |
46 | // series->setPen(seriesPen); |
|
44 | // series->setPen(seriesPen); | |
47 | //! [2] |
|
45 | //! [2] | |
48 |
|
46 | |||
49 | //! [add points to series] |
|
47 | //! [add points to series] | |
50 | //add data points to the series |
|
48 | //add data points to the series | |
51 | series->append(QPointF(150, 100)); |
|
49 | series->append(QPointF(150, 100)); | |
52 | series->append(QPointF(200, 130)); |
|
50 | series->append(QPointF(200, 130)); | |
53 | series->append(QPointF(250, 120)); |
|
51 | series->append(QPointF(250, 120)); | |
54 | series->append(QPointF(300, 140)); |
|
52 | series->append(QPointF(300, 140)); | |
55 | series->append(QPointF(350, 160)); |
|
53 | series->append(QPointF(350, 160)); | |
56 | //! [add points to series] |
|
54 | //! [add points to series] | |
57 |
|
55 | |||
58 | QSplineSeries* series2 = new QSplineSeries; |
|
56 | QSplineSeries* series2 = new QSplineSeries; | |
59 |
|
57 | |||
60 | series2->append(QPointF(400, 120)); |
|
58 | series2->append(QPointF(400, 120)); | |
61 | series2->append(QPointF(450, 150)); |
|
59 | series2->append(QPointF(450, 150)); | |
62 | series2->append(QPointF(500, 145)); |
|
60 | series2->append(QPointF(500, 145)); | |
63 | series2->append(QPointF(550, 170)); |
|
61 | series2->append(QPointF(550, 170)); | |
64 |
|
62 | |||
65 | // series->append(QPointF(600, 190)); |
|
63 | // series->append(QPointF(600, 190)); | |
66 | // series->append(QPointF(650, 210)); |
|
64 | // series->append(QPointF(650, 210)); | |
67 | // series->append(QPointF(700, 190)); |
|
65 | // series->append(QPointF(700, 190)); | |
68 | // series->append(QPointF(750, 180)); |
|
66 | // series->append(QPointF(750, 180)); | |
69 | // series->append(QPointF(800, 170)); |
|
67 | // series->append(QPointF(800, 170)); | |
70 | QSplineSeries* series3 = new QSplineSeries; |
|
68 | QSplineSeries* series3 = new QSplineSeries; | |
71 | series3->append(QPointF(600, 190)); |
|
69 | series3->append(QPointF(600, 190)); | |
72 | series3->append(QPointF(650, 210)); |
|
70 | series3->append(QPointF(650, 210)); | |
73 | series3->append(QPointF(700, 190)); |
|
71 | series3->append(QPointF(700, 190)); | |
74 | series3->append(QPointF(750, 180)); |
|
72 | series3->append(QPointF(750, 180)); | |
75 | series3->append(QPointF(800, 170)); |
|
73 | series3->append(QPointF(800, 170)); | |
76 |
|
74 | |||
77 | //! [3] |
|
75 | //! [3] | |
78 | // create chart view |
|
76 | // create chart view | |
79 | QChart* chart = new QChart; |
|
77 | QChart* chart = new QChart; | |
80 | chart->addSeries(series); |
|
78 | chart->addSeries(series); | |
81 | chart->addSeries(series2); |
|
79 | chart->addSeries(series2); | |
82 | chart->addSeries(series3); |
|
80 | chart->addSeries(series3); | |
83 |
|
81 | |||
84 | chart->setTitle("Spline chart example"); |
|
82 | chart->setTitle("Spline chart example"); | |
85 | chart->axisX()->setMax(1500); |
|
83 | chart->axisX()->setMax(1500); | |
86 | chart->axisY()->setMax(500); |
|
84 | chart->axisY()->setMax(500); | |
87 |
|
85 | |||
88 | chart->setMinimumSize(800,600); |
|
86 | chart->setMinimumSize(800,600); | |
89 | //! [3] |
|
87 | //! [3] | |
90 |
|
88 | |||
91 | //! [4] |
|
89 | //! [4] | |
92 | //add new data point button |
|
90 | //add new data point button | |
93 | QPushButton* addButton = new QPushButton("Add new point"); |
|
91 | QPushButton* addButton = new QPushButton("Add new point"); | |
94 | connect(addButton, SIGNAL(clicked()), this, SLOT(addNewPoint())); |
|
92 | connect(addButton, SIGNAL(clicked()), this, SLOT(addNewPoint())); | |
95 |
|
93 | |||
96 | // remove the last data point in the series |
|
94 | // remove the last data point in the series | |
97 | QPushButton* removeButton = new QPushButton("Remove point"); |
|
95 | QPushButton* removeButton = new QPushButton("Remove point"); | |
98 | connect(removeButton, SIGNAL(clicked()), this, SLOT(removePoint())); |
|
96 | connect(removeButton, SIGNAL(clicked()), this, SLOT(removePoint())); | |
99 | //! [4] |
|
97 | //! [4] | |
100 |
|
98 | |||
101 | //! [5] |
|
99 | //! [5] | |
102 | //butttons layout |
|
100 | //butttons layout | |
103 | QVBoxLayout* buttonsLayout = new QVBoxLayout; |
|
101 | QVBoxLayout* buttonsLayout = new QVBoxLayout; | |
104 | buttonsLayout->addWidget(addButton); |
|
102 | buttonsLayout->addWidget(addButton); | |
105 | buttonsLayout->addWidget(removeButton); |
|
103 | buttonsLayout->addWidget(removeButton); | |
106 | buttonsLayout->addStretch(); |
|
104 | buttonsLayout->addStretch(); | |
107 |
|
105 | |||
108 | QGridLayout* mainLayout = new QGridLayout; |
|
106 | QGridLayout* mainLayout = new QGridLayout; | |
109 | QChartView *chartView = new QChartView(chart); |
|
107 | QChartView *chartView = new QChartView(chart); | |
110 | mainLayout->addWidget(chartView, 1, 0); |
|
108 | mainLayout->addWidget(chartView, 1, 0); | |
111 | mainLayout->addLayout(buttonsLayout, 1, 1); |
|
109 | mainLayout->addLayout(buttonsLayout, 1, 1); | |
112 | setLayout(mainLayout); |
|
110 | setLayout(mainLayout); | |
113 | //! [5] |
|
111 | //! [5] | |
114 | } |
|
112 | } | |
115 |
|
113 | |||
116 | //! [add point] |
|
114 | //! [add point] | |
117 | void SplineWidget::addNewPoint() |
|
115 | void SplineWidget::addNewPoint() | |
118 | { |
|
116 | { | |
119 | if (series->count() > 0) |
|
117 | if (series->count() > 0) | |
120 | series->append(QPointF(series->x(series->count() - 1) + 40 + qrand()%40, qAbs(series->y(series->count() - 1) - 50 + qrand()%100))); |
|
118 | series->append(QPointF(series->x(series->count() - 1) + 40 + qrand()%40, qAbs(series->y(series->count() - 1) - 50 + qrand()%100))); | |
121 | else |
|
119 | else | |
122 | series->append(QPointF(50, 50 + qrand()%50)); |
|
120 | series->append(QPointF(50, 50 + qrand()%50)); | |
123 | } |
|
121 | } | |
124 | //! [add point] |
|
122 | //! [add point] | |
125 |
|
123 | |||
126 | //! [remove point] |
|
124 | //! [remove point] | |
127 | void SplineWidget::removePoint() |
|
125 | void SplineWidget::removePoint() | |
128 | { |
|
126 | { | |
129 | if (series->count() > 0) |
|
127 | if (series->count() > 0) | |
130 | series->remove(QPointF(series->x(series->count() - 1), series->y(series->count() - 1))); |
|
128 | series->remove(QPointF(series->x(series->count() - 1), series->y(series->count() - 1))); | |
131 | } |
|
129 | } | |
132 | //! [remove point] |
|
130 | //! [remove point] | |
133 |
|
131 | |||
134 | SplineWidget::~SplineWidget() |
|
132 | SplineWidget::~SplineWidget() | |
135 | { |
|
133 | { | |
136 |
|
134 | |||
137 | } |
|
135 | } |
@@ -1,167 +1,170 | |||||
1 | /**************************************************************************** |
|
1 | /**************************************************************************** | |
2 | ** |
|
2 | ** | |
3 | ** Copyright (C) 2012 Digia Plc |
|
3 | ** Copyright (C) 2012 Digia Plc | |
4 | ** All rights reserved. |
|
4 | ** All rights reserved. | |
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
6 | ** |
|
6 | ** | |
7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
8 | ** |
|
8 | ** | |
9 | ** $QT_BEGIN_LICENSE$ |
|
9 | ** $QT_BEGIN_LICENSE$ | |
10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
12 | ** Software or, alternatively, in accordance with the terms contained in |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
13 | ** a written agreement between you and Digia. |
|
13 | ** a written agreement between you and Digia. | |
14 | ** |
|
14 | ** | |
15 | ** If you have questions regarding the use of this file, please use |
|
15 | ** If you have questions regarding the use of this file, please use | |
16 | ** contact form at http://qt.digia.com |
|
16 | ** contact form at http://qt.digia.com | |
17 | ** $QT_END_LICENSE$ |
|
17 | ** $QT_END_LICENSE$ | |
18 | ** |
|
18 | ** | |
19 | ****************************************************************************/ |
|
19 | ****************************************************************************/ | |
20 |
|
20 | |||
21 | #include <QtGui/QApplication> |
|
21 | #include <QtGui/QApplication> | |
22 | #include <QMainWindow> |
|
22 | #include <QMainWindow> | |
23 |
#include < |
|
23 | #include <QChartView> | |
24 |
#include < |
|
24 | #include <QStackedBarSeries> | |
25 | #include <qstackedbarseries.h> |
|
25 | #include <QBarSet> | |
26 | #include <qbarset.h> |
|
|||
27 | #include <qchartaxis.h> |
|
|||
28 | #include <QStringList> |
|
|||
29 | #include <QDebug> |
|
|||
30 |
|
26 | |||
31 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
27 | QTCOMMERCIALCHART_USE_NAMESPACE | |
32 |
|
28 | |||
33 | //! [1] |
|
29 | //! [1] | |
34 | class DrilldownBarSeries : public QStackedBarSeries |
|
30 | class DrilldownBarSeries : public QStackedBarSeries | |
35 | { |
|
31 | { | |
36 | Q_OBJECT |
|
32 | Q_OBJECT | |
37 | public: |
|
33 | public: | |
38 |
DrilldownBarSeries(QStringList categories, QObject *parent = 0) |
|
34 | DrilldownBarSeries(QStringList categories, QObject *parent = 0) | |
|
35 | :QStackedBarSeries(categories, parent) | |||
|
36 | { | |||
|
37 | ||||
|
38 | } | |||
39 |
|
39 | |||
40 | void mapDrilldownSeries(QString category, DrilldownBarSeries* drilldownSeries) |
|
40 | void mapDrilldownSeries(QString category, DrilldownBarSeries* drilldownSeries) | |
41 | { |
|
41 | { | |
42 | mDrilldownSeries[category] = drilldownSeries; |
|
42 | mDrilldownSeries[category] = drilldownSeries; | |
43 | } |
|
43 | } | |
44 |
|
44 | |||
45 | DrilldownBarSeries* drilldownSeries(QString category) |
|
45 | DrilldownBarSeries* drilldownSeries(QString category) | |
46 | { |
|
46 | { | |
47 | return mDrilldownSeries[category]; |
|
47 | return mDrilldownSeries[category]; | |
48 | } |
|
48 | } | |
49 |
|
49 | |||
50 | public Q_SLOTS: |
|
|||
51 |
|
||||
52 | private: |
|
50 | private: | |
53 |
|
||||
54 | QMap<QString, DrilldownBarSeries*> mDrilldownSeries; |
|
51 | QMap<QString, DrilldownBarSeries*> mDrilldownSeries; | |
55 | }; |
|
52 | }; | |
56 | //! [1] |
|
53 | //! [1] | |
57 |
|
54 | |||
58 | //! [2] |
|
55 | //! [2] | |
59 | class DrilldownChart : public QChart |
|
56 | class DrilldownChart : public QChart | |
60 | { |
|
57 | { | |
61 | Q_OBJECT |
|
58 | Q_OBJECT | |
62 | public: |
|
59 | public: | |
63 |
explicit DrilldownChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0) |
|
60 | explicit DrilldownChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0) | |
|
61 | :QChart(parent, wFlags), | |||
|
62 | m_currentSeries(0) | |||
|
63 | { | |||
|
64 | ||||
|
65 | } | |||
64 |
|
66 | |||
65 | void changeSeries(QSeries* series) |
|
67 | void changeSeries(QSeries* series) | |
66 | { |
|
68 | { | |
67 | if (m_currentSeries) |
|
69 | if (m_currentSeries) | |
68 | removeSeries(m_currentSeries); |
|
70 | removeSeries(m_currentSeries); | |
69 | m_currentSeries = series; |
|
71 | m_currentSeries = series; | |
70 | addSeries(series); |
|
72 | addSeries(series); | |
71 | setTitle(series->name()); |
|
73 | setTitle(series->name()); | |
72 | } |
|
74 | } | |
73 |
|
75 | |||
74 | public Q_SLOTS: |
|
76 | public Q_SLOTS: | |
75 | void handleClicked(QBarSet *barset, QString category, Qt::MouseButtons button) |
|
77 | void handleClicked(QBarSet *barset, QString category, Qt::MouseButtons button) | |
76 | { |
|
78 | { | |
77 | Q_UNUSED(barset) |
|
79 | Q_UNUSED(barset) | |
|
80 | Q_UNUSED(button) | |||
78 | DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender()); |
|
81 | DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender()); | |
79 | changeSeries(series->drilldownSeries(category)); |
|
82 | changeSeries(series->drilldownSeries(category)); | |
80 | } |
|
83 | } | |
81 |
|
84 | |||
82 | private: |
|
85 | private: | |
83 | QSeries* m_currentSeries; |
|
86 | QSeries* m_currentSeries; | |
84 | }; |
|
87 | }; | |
85 | //! [2] |
|
88 | //! [2] | |
86 |
|
89 | |||
87 | int main(int argc, char *argv[]) |
|
90 | int main(int argc, char *argv[]) | |
88 | { |
|
91 | { | |
89 | QApplication a(argc, argv); |
|
92 | QApplication a(argc, argv); | |
90 | QMainWindow window; |
|
93 | QMainWindow window; | |
91 |
|
94 | |||
92 | DrilldownChart* drilldownChart = new DrilldownChart(); |
|
95 | DrilldownChart* drilldownChart = new DrilldownChart(); | |
93 | drilldownChart->setTheme(QChart::ChartThemeBlueIcy); |
|
96 | drilldownChart->setTheme(QChart::ChartThemeBlueIcy); | |
94 | drilldownChart->setAnimationOptions(QChart::SeriesAnimations); |
|
97 | drilldownChart->setAnimationOptions(QChart::SeriesAnimations); | |
95 |
|
98 | |||
96 | //! [3] |
|
99 | //! [3] | |
97 | // Define categories |
|
100 | // Define categories | |
98 | QStringList months; |
|
101 | QStringList months; | |
99 | months << "May" << "Jun" << "Jul" << "Aug" << "Sep"; |
|
102 | months << "May" << "Jun" << "Jul" << "Aug" << "Sep"; | |
100 | QStringList weeks; |
|
103 | QStringList weeks; | |
101 | weeks << "week 1" << "week 2" << "week 3" << "week 4"; |
|
104 | weeks << "week 1" << "week 2" << "week 3" << "week 4"; | |
102 | QStringList plants; |
|
105 | QStringList plants; | |
103 | plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo"; |
|
106 | plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo"; | |
104 | //! [3] |
|
107 | //! [3] | |
105 |
|
108 | |||
106 | //! [4] |
|
109 | //! [4] | |
107 | // Create drilldown structure |
|
110 | // Create drilldown structure | |
108 | DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart); |
|
111 | DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart); | |
109 | seasonSeries->setName("Crop by month - Season"); |
|
112 | seasonSeries->setName("Crop by month - Season"); | |
110 |
|
113 | |||
111 | // Each month in season series has drilldown series for weekly data |
|
114 | // Each month in season series has drilldown series for weekly data | |
112 | foreach (QString month, months) { |
|
115 | foreach (QString month, months) { | |
113 |
|
116 | |||
114 | // Create drilldown series for every week |
|
117 | // Create drilldown series for every week | |
115 | DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart); |
|
118 | DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart); | |
116 | seasonSeries->mapDrilldownSeries(month, weeklySeries); |
|
119 | seasonSeries->mapDrilldownSeries(month, weeklySeries); | |
117 |
|
120 | |||
118 | // Drilling down from weekly data brings us back to season data. |
|
121 | // Drilling down from weekly data brings us back to season data. | |
119 | foreach (QString week, weeks) { |
|
122 | foreach (QString week, weeks) { | |
120 | weeklySeries->mapDrilldownSeries(week, seasonSeries); |
|
123 | weeklySeries->mapDrilldownSeries(week, seasonSeries); | |
121 | weeklySeries->setName(QString("Crop by week - " + month)); |
|
124 | weeklySeries->setName(QString("Crop by week - " + month)); | |
122 | } |
|
125 | } | |
123 |
|
126 | |||
124 | // Use right click signal to implement drilldown |
|
127 | // Use right click signal to implement drilldown | |
125 | QObject::connect(weeklySeries, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)), drilldownChart, SLOT(handleClicked(QBarSet*,QString,Qt::MouseButtons))); |
|
128 | QObject::connect(weeklySeries, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)), drilldownChart, SLOT(handleClicked(QBarSet*,QString,Qt::MouseButtons))); | |
126 | } |
|
129 | } | |
127 |
|
130 | |||
128 | // Enable drilldown from season series using right click. |
|
131 | // Enable drilldown from season series using right click. | |
129 | QObject::connect(seasonSeries, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)), drilldownChart, SLOT(handleClicked(QBarSet*,QString,Qt::MouseButtons))); |
|
132 | QObject::connect(seasonSeries, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)), drilldownChart, SLOT(handleClicked(QBarSet*,QString,Qt::MouseButtons))); | |
130 | //! [4] |
|
133 | //! [4] | |
131 |
|
134 | |||
132 | //! [5] |
|
135 | //! [5] | |
133 | // Fill monthly and weekly series with data |
|
136 | // Fill monthly and weekly series with data | |
134 | foreach (QString plant, plants) { |
|
137 | foreach (QString plant, plants) { | |
135 | QBarSet* monthlyCrop = new QBarSet(plant); |
|
138 | QBarSet* monthlyCrop = new QBarSet(plant); | |
136 | foreach (QString month, months) { |
|
139 | foreach (QString month, months) { | |
137 | QBarSet* weeklyCrop = new QBarSet(plant); |
|
140 | QBarSet* weeklyCrop = new QBarSet(plant); | |
138 | foreach (QString week, weeks ) |
|
141 | foreach (QString week, weeks ) | |
139 | *weeklyCrop << (qrand() % 20); |
|
142 | *weeklyCrop << (qrand() % 20); | |
140 | // Get the drilldown series from season series and add crop to it. |
|
143 | // Get the drilldown series from season series and add crop to it. | |
141 | seasonSeries->drilldownSeries(month)->appendBarSet(weeklyCrop); |
|
144 | seasonSeries->drilldownSeries(month)->appendBarSet(weeklyCrop); | |
142 | seasonSeries->drilldownSeries(month)->setToolTipEnabled(true); |
|
145 | seasonSeries->drilldownSeries(month)->setToolTipEnabled(true); | |
143 | *monthlyCrop << weeklyCrop->total(); |
|
146 | *monthlyCrop << weeklyCrop->total(); | |
144 | } |
|
147 | } | |
145 | seasonSeries->appendBarSet(monthlyCrop); |
|
148 | seasonSeries->appendBarSet(monthlyCrop); | |
146 | } |
|
149 | } | |
147 | //! [5] |
|
150 | //! [5] | |
148 |
|
151 | |||
149 | seasonSeries->setToolTipEnabled(true); |
|
152 | seasonSeries->setToolTipEnabled(true); | |
150 |
|
153 | |||
151 | //! [6] |
|
154 | //! [6] | |
152 | // Show season series in initial view |
|
155 | // Show season series in initial view | |
153 | drilldownChart->changeSeries(seasonSeries); |
|
156 | drilldownChart->changeSeries(seasonSeries); | |
154 | drilldownChart->setTitle(seasonSeries->name()); |
|
157 | drilldownChart->setTitle(seasonSeries->name()); | |
155 | //! [6] |
|
158 | //! [6] | |
156 |
|
159 | |||
157 | drilldownChart->axisX()->setGridLineVisible(false); |
|
160 | drilldownChart->axisX()->setGridLineVisible(false); | |
158 |
|
161 | |||
159 | QChartView *chartView = new QChartView(drilldownChart); |
|
162 | QChartView *chartView = new QChartView(drilldownChart); | |
160 | window.setCentralWidget(chartView); |
|
163 | window.setCentralWidget(chartView); | |
161 | window.resize(400, 300); |
|
164 | window.resize(400, 300); | |
162 | window.show(); |
|
165 | window.show(); | |
163 |
|
166 | |||
164 | return a.exec(); |
|
167 | return a.exec(); | |
165 | } |
|
168 | } | |
166 |
|
169 | |||
167 | #include "main.moc" |
|
170 | #include "main.moc" |
General Comments 0
You need to be logged in to leave comments.
Login now