##// END OF EJS Templates
some cleanup on legend and pie. No need for pie to know too much about legend
some cleanup on legend and pie. No need for pie to know too much about legend

File last commit:

r2098:46e62d22b0b4
r2173:8e5e86b8b162
Show More
donutbreakdownchart.cpp
94 lines | 2.7 KiB | text/x-c | CppLexer
Jani Honkonen
add missing license statements
r1916 /****************************************************************************
**
** 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$
**
****************************************************************************/
Jani Honkonen
Refactored donutbreakdown example
r1875 #include "donutbreakdownchart.h"
#include <QPieSlice>
QTCOMMERCIALCHART_USE_NAMESPACE
//![1]
DonutBreakdownChart::DonutBreakdownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
Jani Honkonen
coding style fixes for examples
r2098 : QChart(parent, wFlags)
Jani Honkonen
Refactored donutbreakdown example
r1875 {
// create the series for main center pie
mainSeries = new QPieSeries();
mainSeries->setPieSize(0.7);
Jani Honkonen
Update donutbreakdown example docs
r1888 QChart::addSeries(mainSeries);
Jani Honkonen
Refactored donutbreakdown example
r1875 }
//![1]
//![2]
void DonutBreakdownChart::addBreakdownSeries(QPieSeries *breakdownSeries, QColor color)
{
// add breakdown series as a slice to center pie
QPieSlice *slice = mainSeries->append(breakdownSeries->name(), breakdownSeries->sum());
// customize the slice
slice->setBrush(color);
slice->setLabelVisible();
slice->setLabelColor(Qt::white);
slice->setLabelPosition(QPieSlice::LabelInsideHorizontal);
// position and customize the breakdown series
breakdownSeries->setPieSize(0.8);
breakdownSeries->setHoleSize(0.7);
breakdownSeries->setLabelsVisible();
foreach (QPieSlice *slice, breakdownSeries->slices()) {
Jani Honkonen
fine tuning donutbreakdownchart
r1905 color = color.lighter(115);
Jani Honkonen
Refactored donutbreakdown example
r1875 slice->setBrush(color);
Tero Ahola
Updated donut screen shots
r1900 slice->setLabelFont(QFont("Arial", 8));
Jani Honkonen
Refactored donutbreakdown example
r1875 }
// add the series to the chart
Jani Honkonen
Update donutbreakdown example docs
r1888 QChart::addSeries(breakdownSeries);
Jani Honkonen
Refactored donutbreakdown example
r1875
// recalculate breakdown donut segments
recalculateAngles();
}
//![2]
Jani Honkonen
Update donutbreakdown example docs
r1888 //![3]
Jani Honkonen
Refactored donutbreakdown example
r1875 void DonutBreakdownChart::recalculateAngles()
{
qreal angle = 0;
foreach (QPieSlice *slice, mainSeries->slices()) {
QPieSeries *s = find(slice->label());
if (s) {
s->setPieStartAngle(angle);
Jani Honkonen
Update donutbreakdown example docs
r1888 angle += slice->percentage() * 360.0; // full pie is 360.0
Jani Honkonen
Refactored donutbreakdown example
r1875 s->setPieEndAngle(angle);
}
}
}
Jani Honkonen
Update donutbreakdown example docs
r1888 //![3]
Jani Honkonen
Refactored donutbreakdown example
r1875
Jani Honkonen
Update donutbreakdown example docs
r1888 //![4]
Jani Honkonen
Refactored donutbreakdown example
r1875 QPieSeries *DonutBreakdownChart::find(QString seriesName) const
{
// find pieseries by name
Jani Honkonen
Update donutbreakdown example docs
r1888 foreach (QAbstractSeries *series, QChart::series()) {
Jani Honkonen
Refactored donutbreakdown example
r1875 QPieSeries *s = qobject_cast<QPieSeries*>(series);
if (!s)
continue;
if (s->name() == seriesName)
return s;
}
return 0;
}
Jani Honkonen
Update donutbreakdown example docs
r1888 //![4]