From d63ddbb6caa393b1bdd74348f8a76f00d4429d86 2012-07-18 10:15:01 From: Marek Rosa Date: 2012-07-18 10:15:01 Subject: [PATCH] Documented the donutdrilldown chart example --- diff --git a/doc/src/examples-donutdrilldown.qdoc b/doc/src/examples-donutdrilldown.qdoc new file mode 100644 index 0000000..55e8c55 --- /dev/null +++ b/doc/src/examples-donutdrilldown.qdoc @@ -0,0 +1,52 @@ +/*! + \example examples/donutdrilldown + \title Donut chart drilldown example + \subtitle + + This example shows how to use create a donut drilldown chart using QPieSeries API. + + Let's start by creating a QChartView instance and enabling the Antialiasing on it. Last line enables the animations of the chart. + + \snippet ../examples/donutdrilldown/widget.cpp 1 + + PieSeries is used to present the general data. + + \snippet ../examples/donutdrilldown/widget.cpp 2 + + Following block of code creates the slices for the mainData QPieSeries. + Then for every created slice a new series is created that stores the detailed data. + The details series is set to be a donut. Its size is adjusted so that it wraps around the mainData pie. + Next two signals from the mainData pie's slices are connected. This is used to keep the mainData slices agligned with their respective details series. + + \snippet ../examples/donutdrilldown/widget.cpp 3 + + Set the labels of the mainData to enabled and their postion to Outside. + Then add the mainData and detailedData series to the chart. + + \snippet ../examples/donutdrilldown/widget.cpp 4 + + Finally the widget is placed in a layout used by the application. + + \snippet ../examples/donutdrilldown/widget.cpp 5 + + To show that the detailed data stays aligned with the main data every 2.5 sec. one of the slices is modified. + It should be noted that int this example the mainData slices should not be modified directly as the change + cannot be applied to the detailed slices without the extra knowledge on how the split looks like. + + \snippet ../examples/donutdrilldown/widget.cpp 6 + + When the mainData slice layout is changed the detailed data layout has to be modified accordingly. + This is achived by setting the start and end angle of the detailed series. + + \snippet ../examples/donutdrilldown/widget.cpp 7 + + Highlight slot selects a random slice for the modification. + The slice is set to exploded to notify the user that its going to be changed. + The actual data modification is delayed by 1 sec to give a user a chance to focus on the slice. + + Then the slice is modified. Respective mainData slice is modified as well to contain as the value the sum of the detailed series slices values. + Finally the slice exploded state is set to false. + + \snippet ../examples/donutdrilldown/widget.cpp 8 + +*/ diff --git a/examples/donutdrilldown/widget.cpp b/examples/donutdrilldown/widget.cpp index e0370d9..3675b56 100644 --- a/examples/donutdrilldown/widget.cpp +++ b/examples/donutdrilldown/widget.cpp @@ -15,12 +15,18 @@ Widget::Widget(QWidget *parent) setMinimumSize(800, 600); qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); + //! [1] QChartView *chartView = new QChartView; chartView->setRenderHint(QPainter::Antialiasing); chartView->chart()->setAnimationOptions(QChart::AllAnimations); + //! [1] + //! [2] mainData = new QPieSeries; mainData->setPieSize(0.6); + //! [2] + + //! [3] for (int j = 0; j < 4; j++) { // create new slice for the mainData QPieSlice *slice = new QPieSlice; @@ -51,22 +57,29 @@ Widget::Widget(QWidget *parent) slice->setValue(donut->sum()); slice->setLabel(QString("%1").arg(donut->sum())); } + //! [3] + //! [4] mainData->setLabelsVisible(); mainData->setLabelsPosition(QPieSlice::LabelInside); chartView->chart()->addSeries(mainData); for (int i = 0; i < detailedData.count(); i++) chartView->chart()->addSeries(detailedData.at(i)); + //! [4] + //! [5] // create main layout QGridLayout* mainLayout = new QGridLayout; mainLayout->addWidget(chartView, 1, 1); setLayout(mainLayout); + //! [5] + //! [6] // modify the value of one detailed slice every 2.5 sec QTimer *updateTimer = new QTimer(this); connect(updateTimer, SIGNAL(timeout()), this, SLOT(highlight())); updateTimer->start(2500); + //! [6] } Widget::~Widget() @@ -74,6 +87,7 @@ Widget::~Widget() } + //! [7] void Widget::updatedStartAngle() { // when the mainData slice has been updated the detailed data QPieSeries object as well @@ -89,6 +103,21 @@ void Widget::updatedAngleSpan() QPieSeries *detailsDonut = detailedData.at(slice->series()->slices().indexOf(slice)); detailsDonut->setPieEndAngle(slice->startAngle() + slice->angleSpan()); } + //! [7] + + //! [8] +void Widget::highlight() +{ + // choose one random detailed data slice to be updated. + detailIndex = qrand() % mainData->count(); + sliceIndex = qrand() % detailedData.at(detailIndex)->count(); + + // set the slice to exploded to make the change easier to observe + detailedData.at(detailIndex)->slices().at(sliceIndex)->setExploded(); + + // give the user time to focus on the slice that will be changed + QTimer::singleShot(1000, this, SLOT(updateRotation())); +} void Widget::updateRotation() { @@ -104,16 +133,4 @@ void Widget::updateRotation() // change the explode state of the selected slice back to normal detailedData.at(detailIndex)->slices().at(sliceIndex)->setExploded(false); } - -void Widget::highlight() -{ - // choose one random detailed data slice to be updated. - detailIndex = qrand() % mainData->count(); - sliceIndex = qrand() % detailedData.at(detailIndex)->count(); - - // set the slice to exploded to make the change easier to observe - detailedData.at(detailIndex)->slices().at(sliceIndex)->setExploded(); - - // give the user time to focus on the slice that will be changed - QTimer::singleShot(1000, this, SLOT(updateRotation())); -} + //! [8]