@@ -11,32 +11,35 | |||||
11 | Mouse button clicked on a month, shows the harvest from that month: |
|
11 | Mouse button clicked on a month, shows the harvest from that month: | |
12 | \image examples_stackedbarchartdrilldown2.png |
|
12 | \image examples_stackedbarchartdrilldown2.png | |
13 |
|
13 | |||
14 |
|
|
14 | First we define a drilldown series class, which adds categories to stacked bar series and mapping for categories to other drilldown series. | |
15 |
Purpose of drilldown series is to contain knowledge of the drilldown structure. The mapDrilldownSeries function maps the category to given series. We can ask the mapping for category with drilldownSeries( |
|
15 | Purpose of drilldown series is to contain knowledge of the drilldown structure. The mapDrilldownSeries function maps the category to given series. We can ask the mapping for category with drilldownSeries(int category) function. | |
16 |
|
16 | |||
17 |
\snippet ../examples/stackedbarchartdrilldown/ |
|
17 | \snippet ../examples/stackedbarchartdrilldown/drilldownseries.h 1 | |
18 |
|
18 | |||
19 | To enable drilldown we create own view, which implements handler for right click. All QBarSeries derived classes send out rightClicked(QBarSet*, QString) signal when series is clicked with right mouse button. The parameters QBarSet and QString contain the pointer to clicked bar set and name of category, where click occurred. |
|
19 | Next we define our own drilldown chart, which implements handler for mouse click. All QBarSeries derived classes send out clicked(QBarSet*, int) signal when series is clicked with mouse. The parameter QBarSet contains pointer to clicked bar set and parameter int contains the index of clicked category. | |
20 |
|
20 | |||
21 | In our DrilldownChart we implement handler, which selects the drilldown chart with the category. |
|
21 | \snippet ../examples/stackedbarchartdrilldown/drilldownchart.h 1 | |
22 |
|
22 | |||
23 | \snippet ../examples/stackedbarchartdrilldown/main.cpp 2 |
|
23 | Now we have our drilldown classes and we can start using them. | |
|
24 | First create the chart. | |||
|
25 | ||||
|
26 | \snippet ../examples/stackedbarchartdrilldown/main.cpp 1 | |||
24 |
|
27 | |||
25 | Here we define data, which we use to construct the chart. |
|
28 | We define categories, from wich the drilldown series will be constructed. | |
26 |
|
29 | |||
27 |
\snippet ../examples/stackedbarchartdrilldown/main.cpp |
|
30 | \snippet ../examples/stackedbarchartdrilldown/main.cpp 2 | |
28 |
|
31 | |||
29 | To create the drilldown structure, we first create our top level series, which we call seasonSeries. For each month in seasonSeries we create a drilldown series, called weeklySeries which contains more detailed data for that month. |
|
32 | To create the drilldown structure, we first create our top level series, which we call seasonSeries. For each month in seasonSeries we create a drilldown series, called weeklySeries which contains more detailed data for that month. | |
30 | In weeklySeries, we use the drilldown handler to bring us back to seasonSeries. To do this we add mapping to the series. The seasonSeries is mapped to weeklySeries for each month. Every weeklySeries is mapped back to the seasonSeries. |
|
33 | In weeklySeries, we use the drilldown handler to bring us back to seasonSeries. To do this we add mapping to the series. The seasonSeries is mapped to weeklySeries for each month. Every weeklySeries is mapped back to the seasonSeries. | |
31 |
To make mapping work, we connect the |
|
34 | To make mapping work, we connect the clicked signals from our series to the drilldownChart. | |
32 |
|
35 | |||
33 |
\snippet ../examples/stackedbarchartdrilldown/main.cpp |
|
36 | \snippet ../examples/stackedbarchartdrilldown/main.cpp 3 | |
34 |
|
37 | |||
35 |
When we have our drilldown structure ready, we can add the data to it. Here we generate random crop for each plant in each week. The monthly crop is calculated from weekly crops |
|
38 | When we have our drilldown structure ready, we can add the data to it. Here we generate random crop for each plant in each week. The monthly crop is calculated from weekly crops and is set as value to monthly series. | |
36 |
|
39 | |||
37 |
\snippet ../examples/stackedbarchartdrilldown/main.cpp |
|
40 | \snippet ../examples/stackedbarchartdrilldown/main.cpp 4 | |
38 |
|
41 | |||
39 | Here we set the chart to show top level series initially. |
|
42 | Here we set the chart to show top level series initially. | |
40 |
|
43 | |||
41 |
\snippet ../examples/stackedbarchartdrilldown/main.cpp |
|
44 | \snippet ../examples/stackedbarchartdrilldown/main.cpp 5 | |
42 | */ |
|
45 | */ |
@@ -26,7 +26,7 | |||||
26 |
|
26 | |||
27 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
27 | QTCOMMERCIALCHART_USE_NAMESPACE | |
28 |
|
28 | |||
29 |
//! [ |
|
29 | //! [1] | |
30 | class DrilldownChart : public QChart |
|
30 | class DrilldownChart : public QChart | |
31 | { |
|
31 | { | |
32 | Q_OBJECT |
|
32 | Q_OBJECT | |
@@ -41,6 +41,6 public Q_SLOTS: | |||||
41 | private: |
|
41 | private: | |
42 | DrilldownBarSeries* m_currentSeries; |
|
42 | DrilldownBarSeries* m_currentSeries; | |
43 | }; |
|
43 | }; | |
44 |
//! [ |
|
44 | //! [1] | |
45 |
|
45 | |||
46 | #endif // DRILLDOWNCHART_H |
|
46 | #endif // DRILLDOWNCHART_H |
@@ -30,18 +30,16 QTCOMMERCIALCHART_USE_NAMESPACE | |||||
30 |
|
30 | |||
31 | int main(int argc, char *argv[]) |
|
31 | int main(int argc, char *argv[]) | |
32 | { |
|
32 | { | |
33 | //! [1] |
|
|||
34 | QApplication a(argc, argv); |
|
33 | QApplication a(argc, argv); | |
35 | QMainWindow window; |
|
34 | QMainWindow window; | |
36 | //! [1] |
|
|||
37 |
|
35 | |||
38 |
//! [ |
|
36 | //! [1] | |
39 | DrilldownChart* drilldownChart = new DrilldownChart(); |
|
37 | DrilldownChart* drilldownChart = new DrilldownChart(); | |
40 | drilldownChart->setTheme(QChart::ChartThemeBlueIcy); |
|
38 | drilldownChart->setTheme(QChart::ChartThemeBlueIcy); | |
41 | drilldownChart->setAnimationOptions(QChart::SeriesAnimations); |
|
39 | drilldownChart->setAnimationOptions(QChart::SeriesAnimations); | |
42 |
//! [ |
|
40 | //! [1] | |
43 |
|
41 | |||
44 |
//! [ |
|
42 | //! [2] | |
45 | // Define categories |
|
43 | // Define categories | |
46 | QStringList months; |
|
44 | QStringList months; | |
47 | months << "May" << "Jun" << "Jul" << "Aug" << "Sep"; |
|
45 | months << "May" << "Jun" << "Jul" << "Aug" << "Sep"; | |
@@ -49,9 +47,9 int main(int argc, char *argv[]) | |||||
49 | weeks << "week 1" << "week 2" << "week 3" << "week 4"; |
|
47 | weeks << "week 1" << "week 2" << "week 3" << "week 4"; | |
50 | QStringList plants; |
|
48 | QStringList plants; | |
51 | plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo"; |
|
49 | plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo"; | |
52 |
//! [ |
|
50 | //! [2] | |
53 |
|
51 | |||
54 |
//! [ |
|
52 | //! [3] | |
55 | // Create drilldown structure |
|
53 | // Create drilldown structure | |
56 | DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart); |
|
54 | DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart); | |
57 | seasonSeries->setName("Crop by month - Season"); |
|
55 | seasonSeries->setName("Crop by month - Season"); | |
@@ -75,9 +73,9 int main(int argc, char *argv[]) | |||||
75 |
|
73 | |||
76 | // Enable drilldown from season series using right click. |
|
74 | // Enable drilldown from season series using right click. | |
77 | QObject::connect(seasonSeries, SIGNAL(clicked(QBarSet*,int)), drilldownChart, SLOT(handleClicked(QBarSet*,int))); |
|
75 | QObject::connect(seasonSeries, SIGNAL(clicked(QBarSet*,int)), drilldownChart, SLOT(handleClicked(QBarSet*,int))); | |
78 |
//! [ |
|
76 | //! [3] | |
79 |
|
77 | |||
80 |
//! [ |
|
78 | //! [4] | |
81 | // Fill monthly and weekly series with data |
|
79 | // Fill monthly and weekly series with data | |
82 | foreach (QString plant, plants) { |
|
80 | foreach (QString plant, plants) { | |
83 | QBarSet* monthlyCrop = new QBarSet(plant); |
|
81 | QBarSet* monthlyCrop = new QBarSet(plant); | |
@@ -92,20 +90,20 int main(int argc, char *argv[]) | |||||
92 | } |
|
90 | } | |
93 | seasonSeries->append(monthlyCrop); |
|
91 | seasonSeries->append(monthlyCrop); | |
94 | } |
|
92 | } | |
95 |
//! [ |
|
93 | //! [4] | |
96 |
|
94 | |||
97 |
//! [ |
|
95 | //! [5] | |
98 | // Show season series in initial view |
|
96 | // Show season series in initial view | |
99 | drilldownChart->changeSeries(seasonSeries); |
|
97 | drilldownChart->changeSeries(seasonSeries); | |
100 | drilldownChart->setTitle(seasonSeries->name()); |
|
98 | drilldownChart->setTitle(seasonSeries->name()); | |
101 |
//! [ |
|
99 | //! [5] | |
102 |
|
100 | |||
103 |
//! [ |
|
101 | //! [6] | |
104 | drilldownChart->axisX()->setGridLineVisible(false); |
|
102 | drilldownChart->axisX()->setGridLineVisible(false); | |
105 | drilldownChart->axisY()->setNiceNumbersEnabled(true); |
|
103 | drilldownChart->axisY()->setNiceNumbersEnabled(true); | |
106 | drilldownChart->legend()->setVisible(true); |
|
104 | drilldownChart->legend()->setVisible(true); | |
107 | drilldownChart->legend()->setAlignment(Qt::AlignBottom); |
|
105 | drilldownChart->legend()->setAlignment(Qt::AlignBottom); | |
108 |
//! [ |
|
106 | //! [6] | |
109 |
|
107 | |||
110 | QChartView *chartView = new QChartView(drilldownChart); |
|
108 | QChartView *chartView = new QChartView(drilldownChart); | |
111 | window.setCentralWidget(chartView); |
|
109 | window.setCentralWidget(chartView); |
General Comments 0
You need to be logged in to leave comments.
Login now