diff --git a/doc/src/examples-donutbreakdown.qdoc b/doc/src/examples-donutbreakdown.qdoc index 2358862..79347bd 100644 --- a/doc/src/examples-donutbreakdown.qdoc +++ b/doc/src/examples-donutbreakdown.qdoc @@ -29,6 +29,15 @@ \snippet ../examples/donutbreakdown/donutbreakdownchart.cpp 3 + Legend markers are customized to show the breakdown percentage and markers for the main level slices are hidden. + + \snippet ../examples/donutbreakdown/donutbreakdownchart.cpp 4 + + Instead the main level slices show the percentage on the label. + + \snippet ../examples/donutbreakdown/mainslice.cpp 1 + \snippet ../examples/donutbreakdown/mainslice.cpp 2 + And now that we have our chart defined we can finally create a QChartView and show the chart. \snippet ../examples/donutbreakdown/main.cpp 3 diff --git a/examples/donutbreakdown/donutbreakdown.pro b/examples/donutbreakdown/donutbreakdown.pro index 0e75db6..fecef7b 100644 --- a/examples/donutbreakdown/donutbreakdown.pro +++ b/examples/donutbreakdown/donutbreakdown.pro @@ -4,7 +4,9 @@ TARGET = donutbreakdown SOURCES += main.cpp\ - donutbreakdownchart.cpp + donutbreakdownchart.cpp \ + mainslice.cpp HEADERS += \ - donutbreakdownchart.h + donutbreakdownchart.h \ + mainslice.h diff --git a/examples/donutbreakdown/donutbreakdownchart.cpp b/examples/donutbreakdown/donutbreakdownchart.cpp index 5bb1beb..f706885 100644 --- a/examples/donutbreakdown/donutbreakdownchart.cpp +++ b/examples/donutbreakdown/donutbreakdownchart.cpp @@ -18,7 +18,9 @@ ** ****************************************************************************/ #include "donutbreakdownchart.h" +#include "mainslice.h" #include +#include QTCOMMERCIALCHART_USE_NAMESPACE @@ -27,23 +29,29 @@ DonutBreakdownChart::DonutBreakdownChart(QGraphicsItem *parent, Qt::WindowFlags : QChart(parent, wFlags) { // create the series for main center pie - mainSeries = new QPieSeries(); - mainSeries->setPieSize(0.7); - QChart::addSeries(mainSeries); + m_mainSeries = new QPieSeries(); + m_mainSeries->setPieSize(0.7); + QChart::addSeries(m_mainSeries); } //![1] //![2] void DonutBreakdownChart::addBreakdownSeries(QPieSeries *breakdownSeries, QColor color) { + QFont font("Arial", 8); + // add breakdown series as a slice to center pie - QPieSlice *slice = mainSeries->append(breakdownSeries->name(), breakdownSeries->sum()); + MainSlice *mainSlice = new MainSlice(breakdownSeries); + mainSlice->setName(breakdownSeries->name()); + mainSlice->setValue(breakdownSeries->sum()); + m_mainSeries->append(mainSlice); // customize the slice - slice->setBrush(color); - slice->setLabelVisible(); - slice->setLabelColor(Qt::white); - slice->setLabelPosition(QPieSlice::LabelInsideHorizontal); + mainSlice->setBrush(color); + mainSlice->setLabelVisible(); + mainSlice->setLabelColor(Qt::white); + mainSlice->setLabelPosition(QPieSlice::LabelInsideHorizontal); + mainSlice->setLabelFont(font); // position and customize the breakdown series breakdownSeries->setPieSize(0.8); @@ -52,7 +60,7 @@ void DonutBreakdownChart::addBreakdownSeries(QPieSeries *breakdownSeries, QColor foreach (QPieSlice *slice, breakdownSeries->slices()) { color = color.lighter(115); slice->setBrush(color); - slice->setLabelFont(QFont("Arial", 8)); + slice->setLabelFont(font); } // add the series to the chart @@ -60,6 +68,9 @@ void DonutBreakdownChart::addBreakdownSeries(QPieSeries *breakdownSeries, QColor // recalculate breakdown donut segments recalculateAngles(); + + // update customize legend markers + updateLegendMarkers(); } //![2] @@ -67,28 +78,33 @@ void DonutBreakdownChart::addBreakdownSeries(QPieSeries *breakdownSeries, QColor void DonutBreakdownChart::recalculateAngles() { qreal angle = 0; - foreach (QPieSlice *slice, mainSeries->slices()) { - QPieSeries *s = find(slice->label()); - if (s) { - s->setPieStartAngle(angle); - angle += slice->percentage() * 360.0; // full pie is 360.0 - s->setPieEndAngle(angle); - } + foreach (QPieSlice *slice, m_mainSeries->slices()) { + QPieSeries *breakdownSeries = qobject_cast(slice)->breakdownSeries(); + breakdownSeries->setPieStartAngle(angle); + angle += slice->percentage() * 360.0; // full pie is 360.0 + breakdownSeries->setPieEndAngle(angle); } } //![3] //![4] -QPieSeries *DonutBreakdownChart::find(QString seriesName) const +void DonutBreakdownChart::updateLegendMarkers() { - // find pieseries by name + // go through all markers foreach (QAbstractSeries *series, QChart::series()) { - QPieSeries *s = qobject_cast(series); - if (!s) - continue; - if (s->name() == seriesName) - return s; + foreach (QLegendMarker *marker, legend()->markers(series)) { + QPieLegendMarker *pieMarker = qobject_cast(marker); + if (series == m_mainSeries) { + // hide markers from main series + pieMarker->setVisible(false); + } else { + // modify markers from breakdown series + pieMarker->setLabel(QString("%1 %2%") + .arg(pieMarker->slice()->label()) + .arg(pieMarker->slice()->percentage() * 100, 0, 'f', 2)); + pieMarker->setFont(QFont("Arial", 8)); + } + } } - return 0; } //![4] diff --git a/examples/donutbreakdown/donutbreakdownchart.h b/examples/donutbreakdown/donutbreakdownchart.h index b2e6054..6d4f41b 100644 --- a/examples/donutbreakdown/donutbreakdownchart.h +++ b/examples/donutbreakdown/donutbreakdownchart.h @@ -33,10 +33,10 @@ public: private: void recalculateAngles(); - QPieSeries *find(QString seriesName) const; + void updateLegendMarkers(); private: - QPieSeries *mainSeries; + QPieSeries *m_mainSeries; }; #endif // DONUTBREAKDOWNCHART_H diff --git a/examples/donutbreakdown/main.cpp b/examples/donutbreakdown/main.cpp index a34794e..b9b724f 100644 --- a/examples/donutbreakdown/main.cpp +++ b/examples/donutbreakdown/main.cpp @@ -58,7 +58,7 @@ int main(int argc, char *argv[]) DonutBreakdownChart *donutBreakdown = new DonutBreakdownChart(); donutBreakdown->setAnimationOptions(QChart::AllAnimations); donutBreakdown->setTitle("Total consumption of energy in Finland 2010"); - donutBreakdown->legend()->setVisible(false); + donutBreakdown->legend()->setAlignment(Qt::AlignRight); donutBreakdown->addBreakdownSeries(series1, Qt::red); donutBreakdown->addBreakdownSeries(series2, Qt::darkGreen); donutBreakdown->addBreakdownSeries(series3, Qt::darkBlue); @@ -69,7 +69,7 @@ int main(int argc, char *argv[]) QChartView *chartView = new QChartView(donutBreakdown); chartView->setRenderHint(QPainter::Antialiasing); window.setCentralWidget(chartView); - window.resize(800, 600); + window.resize(800, 500); window.show(); //![3] diff --git a/examples/donutbreakdown/mainslice.cpp b/examples/donutbreakdown/mainslice.cpp new file mode 100644 index 0000000..874de9f --- /dev/null +++ b/examples/donutbreakdown/mainslice.cpp @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the Qt Commercial Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "mainslice.h" + +QTCOMMERCIALCHART_USE_NAMESPACE + +//![1] +MainSlice::MainSlice(QPieSeries *breakdownSeries, QObject *parent) + : QPieSlice(parent), + m_breakdownSeries(breakdownSeries) +{ + connect(this, SIGNAL(percentageChanged()), this, SLOT(updateLabel())); +} +//![1] + +QPieSeries *MainSlice::breakdownSeries() const +{ + return m_breakdownSeries; +} + +void MainSlice::setName(QString name) +{ + m_name = name; +} + +QString MainSlice::name() const +{ + return m_name; +} + +//![2] +void MainSlice::updateLabel() +{ + this->setLabel(QString("%1 %2%").arg(m_name).arg(percentage() * 100, 0, 'f', 2)); +} +//![2] + +#include "moc_mainslice.cpp" + diff --git a/examples/donutbreakdown/mainslice.h b/examples/donutbreakdown/mainslice.h new file mode 100644 index 0000000..e1e324d --- /dev/null +++ b/examples/donutbreakdown/mainslice.h @@ -0,0 +1,48 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the Qt Commercial Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINSLICE_H +#define MAINSLICE_H + +#include +#include + +QTCOMMERCIALCHART_USE_NAMESPACE + +class MainSlice : public QPieSlice +{ + Q_OBJECT +public: + MainSlice(QPieSeries *breakdownSeries, QObject *parent = 0); + + QPieSeries *breakdownSeries() const; + + void setName(QString name); + QString name() const; + +public Q_SLOTS: + void updateLabel(); + +private: + QPieSeries *m_breakdownSeries; + QString m_name; +}; + +#endif // MAINSLICE_H