From 5a9ca9e911f7f2ffbd94f25870e50ab90d0f764e 2012-07-09 10:12:14 From: Marek Rosa Date: 2012-07-09 10:12:14 Subject: [PATCH] Added initial donut chart support to Pie series --- diff --git a/src/piechart/piechartitem.cpp b/src/piechart/piechartitem.cpp index a1851bb..90bb5a1 100644 --- a/src/piechart/piechartitem.cpp +++ b/src/piechart/piechartitem.cpp @@ -214,6 +214,7 @@ PieSliceData PieChartItem::updateSliceGeometry(QPieSlice *slice) PieSliceData &sliceData = QPieSlicePrivate::fromSlice(slice)->m_data; sliceData.m_center = PieSliceItem::sliceCenter(m_pieCenter, m_pieRadius, slice); sliceData.m_radius = m_pieRadius; + sliceData.m_donut = m_series->donut(); return sliceData; } diff --git a/src/piechart/pieslicedata_p.h b/src/piechart/pieslicedata_p.h index ea99772..276b5dd 100644 --- a/src/piechart/pieslicedata_p.h +++ b/src/piechart/pieslicedata_p.h @@ -82,6 +82,7 @@ public: m_radius = 0; m_startAngle = 0; m_angleSpan = 0; + m_donut = false; } bool operator!=(const PieSliceData &other) const @@ -135,6 +136,7 @@ public: qreal m_radius; qreal m_startAngle; qreal m_angleSpan; + bool m_donut; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/piechart/piesliceitem.cpp b/src/piechart/piesliceitem.cpp index 49781cb..b77d392 100644 --- a/src/piechart/piesliceitem.cpp +++ b/src/piechart/piesliceitem.cpp @@ -40,7 +40,7 @@ QPointF offset(qreal angle, qreal length) PieSliceItem::PieSliceItem(QGraphicsItem* parent) :QGraphicsObject(parent), - m_hovered(false) + m_hovered(false) { setAcceptHoverEvents(true); setAcceptedMouseButtons(Qt::MouseButtonMask); @@ -174,11 +174,19 @@ QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAn QRectF rect(center.x()-radius, center.y()-radius, radius*2, radius*2); // slice path - // TODO: draw the shape so that it might have a hole in the center QPainterPath path; - path.moveTo(rect.center()); - path.arcTo(rect, -startAngle + 90, -angleSpan); - path.closeSubpath(); + if (m_data.m_donut) { + qreal donutFraction = 5.0; + QRectF insideRect = rect.adjusted(rect.width() / donutFraction, rect.height() / donutFraction, -rect.width() / donutFraction, -rect.height() / donutFraction); + path.arcMoveTo(rect, -startAngle + 90); + path.arcTo(rect, -startAngle + 90, -angleSpan); + path.arcTo(insideRect, -startAngle + 90 - angleSpan, angleSpan); + path.closeSubpath(); + } else { + path.moveTo(rect.center()); + path.arcTo(rect, -startAngle + 90, -angleSpan); + path.closeSubpath(); + } // calculate label arm start point *armStart = center; @@ -213,12 +221,12 @@ QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length // line to underline the label QPointF parm2 = parm1; if (angle < 180) { // arm swings the other way on the left side - parm2 += QPointF(textWidth, 0); - *textStart = parm1; + parm2 += QPointF(textWidth, 0); + *textStart = parm1; } else { - parm2 += QPointF(-textWidth,0); - *textStart = parm2; + parm2 += QPointF(-textWidth,0); + *textStart = parm2; } QPainterPath path; diff --git a/src/piechart/qpieseries.cpp b/src/piechart/qpieseries.cpp index 0e85f53..6a7e6a5 100644 --- a/src/piechart/qpieseries.cpp +++ b/src/piechart/qpieseries.cpp @@ -523,6 +523,19 @@ qreal QPieSeries::sum() const return d->m_sum; } +void QPieSeries::setDonut(bool donut) +{ + Q_D(QPieSeries); + d->m_donutChart = donut; + d->updateDerivativeData(); +} + +bool QPieSeries::donut() const +{ + Q_D(const QPieSeries); + return d->m_donutChart; +} + void QPieSeries::setHorizontalPosition(qreal relativePosition) { Q_D(QPieSeries); @@ -660,7 +673,8 @@ QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) : m_pieRelativeSize(0.7), m_pieStartAngle(0), m_pieEndAngle(360), - m_sum(0) + m_sum(0), + m_donutChart(false) { } @@ -693,6 +707,7 @@ void QPieSeriesPrivate::updateDerivativeData() d->setPercentage(s->value() / m_sum); d->setStartAngle(sliceAngle); d->setAngleSpan(pieSpan * s->percentage()); + d->m_data.m_donut = m_donutChart; sliceAngle += s->angleSpan(); } diff --git a/src/piechart/qpieseries.h b/src/piechart/qpieseries.h index abedbc7..70366a3 100644 --- a/src/piechart/qpieseries.h +++ b/src/piechart/qpieseries.h @@ -61,6 +61,9 @@ public: qreal sum() const; + void setDonut(bool donut = true); + bool donut() const; + void setHorizontalPosition(qreal relativePosition); qreal horizontalPosition() const; diff --git a/src/piechart/qpieseries_p.h b/src/piechart/qpieseries_p.h index b3f457b..dbb87e8 100644 --- a/src/piechart/qpieseries_p.h +++ b/src/piechart/qpieseries_p.h @@ -77,6 +77,7 @@ private: qreal m_pieStartAngle; qreal m_pieEndAngle; qreal m_sum; + bool m_donutChart; private: friend class QLegendPrivate;