summaryrefslogtreecommitdiff
path: root/src/charts/PieRound.cpp
diff options
context:
space:
mode:
authorJedidiah Barber <contact@jedbarber.id.au>2021-07-14 11:49:10 +1200
committerJedidiah Barber <contact@jedbarber.id.au>2021-07-14 11:49:10 +1200
commitd24f813f3f2a05c112e803e4256b53535895fc98 (patch)
tree601e6ae9a1cd44bcfdcf91739a5ca36aedd827c9 /src/charts/PieRound.cpp
Initial mirror commitHEADmaster
Diffstat (limited to 'src/charts/PieRound.cpp')
-rw-r--r--src/charts/PieRound.cpp77
1 files changed, 77 insertions, 0 deletions
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);
+}