diff --git a/doc/src/examples-donut.qdoc b/doc/src/examples-donut.qdoc new file mode 100644 index 0000000..1cfaf22 --- /dev/null +++ b/doc/src/examples-donut.qdoc @@ -0,0 +1,48 @@ +/*! + \example examples/donut + \title Donut example + \subtitle + + This example shows how to use create a nested donuts 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/donut/widget.cpp 1 + + Three variables are defined that will be used to define the donut chart. Min and max size define the relative size of the whole donut. + minSize is the relative inner size of the smallest donut. maxSize is the relative outer size of the biggest donut. + + \snippet ../examples/donut/widget.cpp 2 + + Following block of code defines the individual donuts and their slices. First new QPieSeries object is created. + Callign setDonut() changes pie into a donut. The number of slices in each donut is randomized. + The internal for loop creates the slices with a random value and label same as the value. + Next the label of the slice is set to be visible and its color is set to white. + To make the example more interesting the hovered signal of the slice is connected to widget's slot which inner workings are explained later. + Finally the slice is added to the donut. The donut's size is adjusted to achive the nesting of the donuts. + Then the donut is added to the widget's list of donuts and to the chart. + + \snippet ../examples/donut/widget.cpp 3 + + Finally the widget is placed in a layout used by the application. + + \snippet ../examples/donut/widget.cpp 4 + + To make the example more interesting the donuts are rotated randomly every 1.25 sec. + + \snippet ../examples/donut/widget.cpp 5 + + The widget's updatedRotation slot is defined below. + It goes through all of the donuts and modifies thier current rotation by a random value. + + \snippet ../examples/donut/widget.cpp 6 + + The earlier mentioned explodeSlice slot code is provided below. + If the slice is set to exploded then stop the timer that controls the donuts rotation. + Then the slice's start and end agles are obtained from the slice. + To highlight the selected slice all the other donuts that lie outward from the one that contains the selected slice + have their start and end angles modified so that they wouldn't "block" the way for the hightlighted slice. + If the slice is no longer selected return to the original state. + + \snippet ../examples/donut/widget.cpp 7 +*/ diff --git a/doc/src/examples.qdoc b/doc/src/examples.qdoc index b92cb69..ca79f16 100644 --- a/doc/src/examples.qdoc +++ b/doc/src/examples.qdoc @@ -27,6 +27,8 @@
  • Percent bar chart
  • Pie chart
  • Pie chart drilldown
  • +
  • Donut chart
  • +
  • Donut chart drilldown
  • Presenter chart
  • Scatter chart
  • Scatter interactions
  • diff --git a/examples/donut/widget.cpp b/examples/donut/widget.cpp index 61a0922..f030219 100644 --- a/examples/donut/widget.cpp +++ b/examples/donut/widget.cpp @@ -14,45 +14,51 @@ 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] qreal minSize = 0.1; qreal maxSize = 0.9; int donutsCount = 5; + //! [2] + + //! [3] for (int i = 0; i < donutsCount; i++) { QPieSeries *donut = new QPieSeries; donut->setDonut(); - donut->setLabelsVisible(); int sliceCount = 3 + qrand() % 3; for (int j = 0; j < sliceCount; j++) { qreal value = 100 + qrand() % 100; QPieSlice *slice = new QPieSlice(QString("%1").arg(value), value); + slice->setLabelVisible(true); + slice->setLabelColor(Qt::white); + slice->setLabelPosition(QPieSlice::LabelInsideTangential); connect(slice, SIGNAL(hovered(bool)), this, SLOT(explodeSlice(bool))); donut->append(slice); - donut->slices().last()->setLabelVisible(true); - donut->slices().last()->setLabelColor(Qt::white); donut->setDonutInnerSize(minSize + i * (maxSize - minSize) / donutsCount); donut->setPieSize(minSize + (i + 1) * (maxSize - minSize) / donutsCount); } m_donuts.append(donut); - qreal phase = qrand() % 180; - donut->setLabelsPosition(QPieSlice::LabelInsideTangential); - donut->setPieStartAngle(phase); - donut->setPieEndAngle(360 + phase); chartView->chart()->addSeries(donut); } + //! [3] // create main layout + //! [4] QGridLayout* mainLayout = new QGridLayout; mainLayout->addWidget(chartView, 1, 1); setLayout(mainLayout); + //! [4] - chartView->chart()->setAnimationOptions(QChart::AllAnimations); - + //! [5] updateTimer = new QTimer(this); connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateRotation())); - updateTimer->start(1500); + updateTimer->start(1250); + //! [5] } Widget::~Widget() @@ -60,9 +66,9 @@ Widget::~Widget() } + //! [6] void Widget::updateRotation() { - // int tobeupdated = qrand() % m_donutsGroup.count(); for (int i = 0; i < m_donuts.count(); i++) { QPieSeries *donut = m_donuts.at(i); qreal phaseShift = -50 + qrand() % 100; @@ -70,7 +76,9 @@ void Widget::updateRotation() donut->setPieEndAngle(donut->pieEndAngle() + phaseShift); } } + //! [6] + //! [7] void Widget::explodeSlice(bool exploded) { QPieSlice *slice = qobject_cast(sender()); @@ -94,3 +102,4 @@ void Widget::explodeSlice(bool exploded) } slice->setExploded(exploded); } + //! [7]