donutbreakdownchart.cpp
110 lines
| 3.5 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r1916 | /**************************************************************************** | ||
** | ||||
Miikka Heikkinen
|
r2433 | ** Copyright (C) 2013 Digia Plc | ||
Jani Honkonen
|
r1916 | ** 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
|
r1875 | #include "donutbreakdownchart.h" | ||
Jani Honkonen
|
r2233 | #include "mainslice.h" | ||
Jani Honkonen
|
r1875 | #include <QPieSlice> | ||
Jani Honkonen
|
r2233 | #include <QPieLegendMarker> | ||
Jani Honkonen
|
r1875 | |||
QTCOMMERCIALCHART_USE_NAMESPACE | ||||
//![1] | ||||
DonutBreakdownChart::DonutBreakdownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) | ||||
Miikka Heikkinen
|
r2483 | : QChart(QChart::ChartTypeCartesian, parent, wFlags) | ||
Jani Honkonen
|
r1875 | { | ||
// create the series for main center pie | ||||
Jani Honkonen
|
r2233 | m_mainSeries = new QPieSeries(); | ||
m_mainSeries->setPieSize(0.7); | ||||
QChart::addSeries(m_mainSeries); | ||||
Jani Honkonen
|
r1875 | } | ||
//![1] | ||||
//![2] | ||||
void DonutBreakdownChart::addBreakdownSeries(QPieSeries *breakdownSeries, QColor color) | ||||
{ | ||||
Jani Honkonen
|
r2233 | QFont font("Arial", 8); | ||
Jani Honkonen
|
r1875 | // add breakdown series as a slice to center pie | ||
Jani Honkonen
|
r2233 | MainSlice *mainSlice = new MainSlice(breakdownSeries); | ||
mainSlice->setName(breakdownSeries->name()); | ||||
mainSlice->setValue(breakdownSeries->sum()); | ||||
m_mainSeries->append(mainSlice); | ||||
Jani Honkonen
|
r1875 | |||
// customize the slice | ||||
Jani Honkonen
|
r2233 | mainSlice->setBrush(color); | ||
mainSlice->setLabelVisible(); | ||||
mainSlice->setLabelColor(Qt::white); | ||||
mainSlice->setLabelPosition(QPieSlice::LabelInsideHorizontal); | ||||
mainSlice->setLabelFont(font); | ||||
Jani Honkonen
|
r1875 | |||
// position and customize the breakdown series | ||||
breakdownSeries->setPieSize(0.8); | ||||
breakdownSeries->setHoleSize(0.7); | ||||
breakdownSeries->setLabelsVisible(); | ||||
foreach (QPieSlice *slice, breakdownSeries->slices()) { | ||||
Jani Honkonen
|
r1905 | color = color.lighter(115); | ||
Jani Honkonen
|
r1875 | slice->setBrush(color); | ||
Jani Honkonen
|
r2233 | slice->setLabelFont(font); | ||
Jani Honkonen
|
r1875 | } | ||
// add the series to the chart | ||||
Jani Honkonen
|
r1888 | QChart::addSeries(breakdownSeries); | ||
Jani Honkonen
|
r1875 | |||
// recalculate breakdown donut segments | ||||
recalculateAngles(); | ||||
Jani Honkonen
|
r2233 | |||
// update customize legend markers | ||||
updateLegendMarkers(); | ||||
Jani Honkonen
|
r1875 | } | ||
//![2] | ||||
Jani Honkonen
|
r1888 | //![3] | ||
Jani Honkonen
|
r1875 | void DonutBreakdownChart::recalculateAngles() | ||
{ | ||||
qreal angle = 0; | ||||
Jani Honkonen
|
r2233 | foreach (QPieSlice *slice, m_mainSeries->slices()) { | ||
QPieSeries *breakdownSeries = qobject_cast<MainSlice *>(slice)->breakdownSeries(); | ||||
breakdownSeries->setPieStartAngle(angle); | ||||
angle += slice->percentage() * 360.0; // full pie is 360.0 | ||||
breakdownSeries->setPieEndAngle(angle); | ||||
Jani Honkonen
|
r1875 | } | ||
} | ||||
Jani Honkonen
|
r1888 | //![3] | ||
Jani Honkonen
|
r1875 | |||
Jani Honkonen
|
r1888 | //![4] | ||
Jani Honkonen
|
r2233 | void DonutBreakdownChart::updateLegendMarkers() | ||
Jani Honkonen
|
r1875 | { | ||
Jani Honkonen
|
r2233 | // go through all markers | ||
Jani Honkonen
|
r1888 | foreach (QAbstractSeries *series, QChart::series()) { | ||
Jani Honkonen
|
r2233 | foreach (QLegendMarker *marker, legend()->markers(series)) { | ||
QPieLegendMarker *pieMarker = qobject_cast<QPieLegendMarker *>(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)); | ||||
} | ||||
} | ||||
Jani Honkonen
|
r1875 | } | ||
} | ||||
Jani Honkonen
|
r1888 | //![4] | ||