##// END OF EJS Templates
Added initial donut chart support to Pie series
Marek Rosa -
r1670:5a9ca9e911f7
parent child
Show More
@@ -214,6 +214,7 PieSliceData PieChartItem::updateSliceGeometry(QPieSlice *slice)
214 214 PieSliceData &sliceData = QPieSlicePrivate::fromSlice(slice)->m_data;
215 215 sliceData.m_center = PieSliceItem::sliceCenter(m_pieCenter, m_pieRadius, slice);
216 216 sliceData.m_radius = m_pieRadius;
217 sliceData.m_donut = m_series->donut();
217 218 return sliceData;
218 219 }
219 220
@@ -82,6 +82,7 public:
82 82 m_radius = 0;
83 83 m_startAngle = 0;
84 84 m_angleSpan = 0;
85 m_donut = false;
85 86 }
86 87
87 88 bool operator!=(const PieSliceData &other) const
@@ -135,6 +136,7 public:
135 136 qreal m_radius;
136 137 qreal m_startAngle;
137 138 qreal m_angleSpan;
139 bool m_donut;
138 140 };
139 141
140 142 QTCOMMERCIALCHART_END_NAMESPACE
@@ -40,7 +40,7 QPointF offset(qreal angle, qreal length)
40 40
41 41 PieSliceItem::PieSliceItem(QGraphicsItem* parent)
42 42 :QGraphicsObject(parent),
43 m_hovered(false)
43 m_hovered(false)
44 44 {
45 45 setAcceptHoverEvents(true);
46 46 setAcceptedMouseButtons(Qt::MouseButtonMask);
@@ -174,11 +174,19 QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAn
174 174 QRectF rect(center.x()-radius, center.y()-radius, radius*2, radius*2);
175 175
176 176 // slice path
177 // TODO: draw the shape so that it might have a hole in the center
178 177 QPainterPath path;
179 path.moveTo(rect.center());
180 path.arcTo(rect, -startAngle + 90, -angleSpan);
181 path.closeSubpath();
178 if (m_data.m_donut) {
179 qreal donutFraction = 5.0;
180 QRectF insideRect = rect.adjusted(rect.width() / donutFraction, rect.height() / donutFraction, -rect.width() / donutFraction, -rect.height() / donutFraction);
181 path.arcMoveTo(rect, -startAngle + 90);
182 path.arcTo(rect, -startAngle + 90, -angleSpan);
183 path.arcTo(insideRect, -startAngle + 90 - angleSpan, angleSpan);
184 path.closeSubpath();
185 } else {
186 path.moveTo(rect.center());
187 path.arcTo(rect, -startAngle + 90, -angleSpan);
188 path.closeSubpath();
189 }
182 190
183 191 // calculate label arm start point
184 192 *armStart = center;
@@ -213,12 +221,12 QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length
213 221 // line to underline the label
214 222 QPointF parm2 = parm1;
215 223 if (angle < 180) { // arm swings the other way on the left side
216 parm2 += QPointF(textWidth, 0);
217 *textStart = parm1;
224 parm2 += QPointF(textWidth, 0);
225 *textStart = parm1;
218 226 }
219 227 else {
220 parm2 += QPointF(-textWidth,0);
221 *textStart = parm2;
228 parm2 += QPointF(-textWidth,0);
229 *textStart = parm2;
222 230 }
223 231
224 232 QPainterPath path;
@@ -523,6 +523,19 qreal QPieSeries::sum() const
523 523 return d->m_sum;
524 524 }
525 525
526 void QPieSeries::setDonut(bool donut)
527 {
528 Q_D(QPieSeries);
529 d->m_donutChart = donut;
530 d->updateDerivativeData();
531 }
532
533 bool QPieSeries::donut() const
534 {
535 Q_D(const QPieSeries);
536 return d->m_donutChart;
537 }
538
526 539 void QPieSeries::setHorizontalPosition(qreal relativePosition)
527 540 {
528 541 Q_D(QPieSeries);
@@ -660,7 +673,8 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) :
660 673 m_pieRelativeSize(0.7),
661 674 m_pieStartAngle(0),
662 675 m_pieEndAngle(360),
663 m_sum(0)
676 m_sum(0),
677 m_donutChart(false)
664 678 {
665 679 }
666 680
@@ -693,6 +707,7 void QPieSeriesPrivate::updateDerivativeData()
693 707 d->setPercentage(s->value() / m_sum);
694 708 d->setStartAngle(sliceAngle);
695 709 d->setAngleSpan(pieSpan * s->percentage());
710 d->m_data.m_donut = m_donutChart;
696 711 sliceAngle += s->angleSpan();
697 712 }
698 713
@@ -61,6 +61,9 public:
61 61
62 62 qreal sum() const;
63 63
64 void setDonut(bool donut = true);
65 bool donut() const;
66
64 67 void setHorizontalPosition(qreal relativePosition);
65 68 qreal horizontalPosition() const;
66 69
@@ -77,6 +77,7 private:
77 77 qreal m_pieStartAngle;
78 78 qreal m_pieEndAngle;
79 79 qreal m_sum;
80 bool m_donutChart;
80 81
81 82 private:
82 83 friend class QLegendPrivate;
General Comments 0
You need to be logged in to leave comments. Login now