diff --git a/example/example.pro b/example/example.pro index b7e00c7..f56f194 100644 --- a/example/example.pro +++ b/example/example.pro @@ -7,6 +7,7 @@ SUBDIRS += linechart \ percentbarchart \ scatter \ piechart \ + piechartdrilldown \ dynamiclinechart \ axischart \ multichart \ diff --git a/example/piechartdrilldown/main.cpp b/example/piechartdrilldown/main.cpp new file mode 100644 index 0000000..4e82edc --- /dev/null +++ b/example/piechartdrilldown/main.cpp @@ -0,0 +1,126 @@ +#include +#include +#include +#include +#include +#include +#include + +QTCOMMERCIALCHART_USE_NAMESPACE + +class DrilldownSlice : public QPieSlice +{ + Q_OBJECT + +public: + DrilldownSlice(qreal value, QString prefix, QSeries* drilldownSeries) + :m_drilldownSeries(drilldownSeries), + m_prefix(prefix) + { + setValue(value); + setLabelVisible(true); + updateLabel(); + connect(this, SIGNAL(changed()), this, SLOT(updateLabel())); + } + + QSeries* drilldownSeries() const { return m_drilldownSeries; } + +public Q_SLOTS: + void updateLabel() + { + QString label = m_prefix; + label += " - " + QString::number(this->value())+ "e ("; + label += QString::number(this->percentage()*100, 'f', 1) + "%)"; + qDebug() << "updateLabel" << label; + setLabel(label); + } + +private: + QSeries* m_drilldownSeries; + QString m_prefix; +}; + +class DrilldownChart : public QChartView +{ + Q_OBJECT +public: + explicit DrilldownChart(QWidget *parent = 0):QChartView(parent), m_currentSeries(0) {} + + void changeSeries(QSeries* series) + { + if (m_currentSeries) + removeSeries(m_currentSeries); + m_currentSeries = series; + addSeries(series); + setChartTitle(series->title()); + } + +public Q_SLOTS: + void handleSliceClicked(QPieSlice* slice) + { + DrilldownSlice* drilldownSlice = static_cast(slice); + changeSeries(drilldownSlice->drilldownSeries()); + } + +private: + QSeries* m_currentSeries; +}; + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + + qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); + + QMainWindow window; + + DrilldownChart* drilldownChart = new DrilldownChart(&window); + drilldownChart->setRenderHint(QPainter::Antialiasing); + drilldownChart->setChartTheme(QChart::ChartThemeVanilla); + + QPieSeries* yearSeries = new QPieSeries(drilldownChart); + yearSeries->setTitle("Sales by year - All"); + yearSeries->setHoverHighlighting(); + + QList months; + months << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; + + int max = 1000; + + QPieSeries* monthSeriesJane = new QPieSeries(drilldownChart); + monthSeriesJane->setTitle("Sales by month - Jane"); + monthSeriesJane->setHoverHighlighting(); + foreach (QString month, months) + *monthSeriesJane << new DrilldownSlice(qrand() % max, month, yearSeries); + + QPieSeries* monthSeriesJohn = new QPieSeries(drilldownChart); + monthSeriesJohn->setTitle("Sales by month - John"); + monthSeriesJohn->setHoverHighlighting(); + foreach (QString month, months) + *monthSeriesJohn << new DrilldownSlice(qrand() % max, month, yearSeries); + + QPieSeries* monthSeriesAxel = new QPieSeries(drilldownChart); + monthSeriesAxel->setTitle("Sales by month - Axel"); + monthSeriesAxel->setHoverHighlighting(); + foreach (QString month, months) + *monthSeriesAxel << new DrilldownSlice(qrand() % max, month, yearSeries); + + *yearSeries << new DrilldownSlice(monthSeriesJane->total(), "Jane", monthSeriesJane); + *yearSeries << new DrilldownSlice(monthSeriesJohn->total(), "John", monthSeriesJohn); + *yearSeries << new DrilldownSlice(monthSeriesAxel->total(), "Axel", monthSeriesAxel); + + QObject::connect(monthSeriesJane, SIGNAL(clicked(QPieSlice*)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); + QObject::connect(monthSeriesJohn, SIGNAL(clicked(QPieSlice*)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); + QObject::connect(monthSeriesAxel, SIGNAL(clicked(QPieSlice*)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); + QObject::connect(yearSeries, SIGNAL(clicked(QPieSlice*)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); + + drilldownChart->changeSeries(yearSeries); + + window.setCentralWidget(drilldownChart); + window.resize(600, 600); + window.show(); + + return a.exec(); +} + +#include "main.moc" diff --git a/example/piechartdrilldown/piechartdrilldown.pro b/example/piechartdrilldown/piechartdrilldown.pro new file mode 100644 index 0000000..df16e45 --- /dev/null +++ b/example/piechartdrilldown/piechartdrilldown.pro @@ -0,0 +1,9 @@ +!include( ../example.pri ) { + error( "Couldn't find the example.pri file!" ) +} +TARGET = piechartdrilldown +SOURCES += main.cpp +HEADERS += + + +MOC_DIR = $$PWD/moc diff --git a/src/piechart/qpieseries.cpp b/src/piechart/qpieseries.cpp index 7749348..4e51234 100644 --- a/src/piechart/qpieseries.cpp +++ b/src/piechart/qpieseries.cpp @@ -188,6 +188,12 @@ void QPieSeries::add(QPieSlice* slice) add(QList() << slice); } +QPieSeries& QPieSeries::operator << (QPieSlice* slice) +{ + add(slice); + return *this; +} + /*! Adds a single slice to the series with give \a value and \a name. @@ -348,10 +354,7 @@ void QPieSeries::setClickExplodes(bool enable) Convenience method for highlighting a slice when user hovers over the slice. It changes the slice color to be lighter and shows the label of the slice. Set \a enable to true to highlight a slice when user hovers on top of it. - - \sa QPieSlice::isExploded(), QPieSlice::setExploded() */ - void QPieSeries::setHoverHighlighting(bool enable) { if (enable) { @@ -364,6 +367,16 @@ void QPieSeries::setHoverHighlighting(bool enable) } /*! + Returns the sum of all slice values in this series. + + \sa QPieSlice::value(), QPieSlice::setValue() +*/ +qreal QPieSeries::total() const +{ + return m_total; +} + +/*! \fn void QPieSeries::changed(const QPieSeries::ChangeSet& changeSet) This signal emitted when something has changed in the series. @@ -456,7 +469,6 @@ void QPieSeries::highlightOn(QPieSlice* slice) Q_ASSERT(slice); QColor c = slice->brush().color().lighter(); slice->setBrush(c); - slice->setLabelVisible(true); } void QPieSeries::highlightOff(QPieSlice* slice) @@ -464,7 +476,6 @@ void QPieSeries::highlightOff(QPieSlice* slice) Q_ASSERT(slice); QColor c = slice->brush().color().darker(150); slice->setBrush(c); - slice->setLabelVisible(false); } void QPieSeries::updateDerivativeData() @@ -480,7 +491,10 @@ void QPieSeries::updateDerivativeData() m_total += s->value(); // we must have some values - Q_ASSERT(m_total > 0); // TODO: is this the correct way to handle this? + if (m_total == 0) { + qDebug() << "QPieSeries::updateDerivativeData() total == 0"; + Q_ASSERT(m_total > 0); // TODO: is this the correct way to handle this? + } // update slice attributes qreal sliceAngle = m_pieStartAngle; diff --git a/src/piechart/qpieseries.h b/src/piechart/qpieseries.h index b1fbbc3..96bd797 100644 --- a/src/piechart/qpieseries.h +++ b/src/piechart/qpieseries.h @@ -62,6 +62,7 @@ public: void add(QList slices); void add(QPieSlice* slice); QPieSlice* add(qreal value, QString name); + QPieSeries& operator << (QPieSlice* slice); void remove(QPieSlice* slice); void clear(); @@ -78,6 +79,8 @@ public: void setClickExplodes(bool enable = true); void setHoverHighlighting(bool enable = true); + qreal total() const; + // TODO: find slices? // QList findByValue(qreal value); // ...