From d24f813f3f2a05c112e803e4256b53535895fc98 Mon Sep 17 00:00:00 2001 From: Jedidiah Barber Date: Wed, 14 Jul 2021 11:49:10 +1200 Subject: Initial mirror commit --- src/charts/PieRound.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/charts/PieRound.cpp (limited to 'src/charts/PieRound.cpp') diff --git a/src/charts/PieRound.cpp b/src/charts/PieRound.cpp new file mode 100644 index 0000000..f90d740 --- /dev/null +++ b/src/charts/PieRound.cpp @@ -0,0 +1,77 @@ +#include "PieRound.h" +#include "PieChartScene.h" + +PieRound::PieRound(const QPointF& center, const PieChartScene* scene): + scene(scene) +{ + setPos(center); + calculateSum(); + addSectors(); +} + +QRectF PieRound::boundingRect() const +{ + return QRectF(-Radius, -Radius, 2 * Radius, 2 * Radius); +} + +void PieRound::calculateSum() +{ + sum = 0; + foreach(DataPoint point, scene->getDataSet()) + sum += point.value; +} + +void PieRound::addSectors() +{ + qreal angle = -90; + for(int i = 0; i < scene->getDataSet().size(); i++) + { + qreal sweep = 360. * scene->getDataSet()[i].value / sum; + addSector(i, angle, sweep); + angle += sweep; + } +} + +void PieRound::addSector(int index, qreal startAngle, qreal sweep) +{ + addSectorItem(createSectorPath(startAngle, sweep), + QColor(scene->getColors()[index])); + addSectorLabel(index, startAngle, sweep); +} + +QPainterPath PieRound::createSectorPath(qreal startAngle, qreal sweep) +{ + QPainterPath sectorPath; + sectorPath.arcTo(boundingRect(), -startAngle, -sweep); + sectorPath.lineTo(QPointF()); + return sectorPath; +} + +void PieRound::addSectorItem(const QPainterPath& path, QColor color) +{ + QGraphicsPathItem* sectorItem = new QGraphicsPathItem(path, this); + sectorItem->setPen(QColor("white")); + sectorItem->setBrush(color); +} + +void PieRound::addSectorLabel(int index, qreal startAngle, qreal sweep) +{ + int value = scene->getDataSet()[index].value; + if(value == 0) + return; + QGraphicsSimpleTextItem* labelItem = + new QGraphicsSimpleTextItem(QString::number(value), this); + labelItem->setPos(getLabelPos(startAngle, sweep, labelItem)); +} + +QPointF PieRound::getLabelPos(qreal startAngle, qreal sweep, + QGraphicsSimpleTextItem* labelItem) +{ + int radius = sweep > 30 ? LabelRadius : 2 * LabelRadius; + QRectF labelArcRect(-radius, -radius, 2 * radius, 2 * radius); + QPainterPath path; + path.arcMoveTo(labelArcRect, -startAngle - sweep / 2); + QSizeF labelSize = labelItem->boundingRect().size(); + return path.currentPosition() - + QPointF(labelSize.width() / 2, labelSize.height() / 2); +} -- cgit