##// END OF EJS Templates
Added one more type of label placing on Pie chart
Marek Rosa -
r1712:0c76e21b1f34
parent child
Show More
@@ -161,6 +161,8 MainWidget::MainWidget(QWidget* parent)
161 161 m_labelPosition = new QComboBox(this);
162 162 m_labelPosition->addItem("Outside", QPieSlice::LabelOutside);
163 163 m_labelPosition->addItem("Inside", QPieSlice::LabelInside);
164 m_labelPosition->addItem("Inside tangential", QPieSlice::LabelInsideTangential);
165 m_labelPosition->addItem("Inside normal", QPieSlice::LabelInsideNormal);
164 166
165 167 QFormLayout* sliceSettingsLayout = new QFormLayout();
166 168 sliceSettingsLayout->addRow("Label", m_sliceName);
@@ -32,7 +32,7 int main(int argc, char *argv[])
32 32
33 33 //![1]
34 34 QLineSeries* series = new QLineSeries();
35 *series << QPointF(0, 6) << QPointF(9, 4) << QPointF(15, 20) << QPointF(25, 12) << QPointF(39, 36);
35 *series << QPointF(0, 6) << QPointF(9, 4) << QPointF(15, 20) << QPointF(25, 12) << QPointF(29, 26);
36 36 QChart* chart = new QChart();
37 37 chart->legend()->hide();
38 38 chart->addSeries(series);
@@ -23,7 +23,7 Widget::Widget(QWidget *parent)
23 23 for (int i = 0; i < donutsCount; i++) {
24 24 QPieSeries *donut = new QPieSeries;
25 25 donut->setDonut();
26 donut->setLabelsVisible();
26 donut->setLabelsVisible();
27 27 int sliceCount = 3 + qrand() % 3;
28 28 for (int j = 0; j < sliceCount; j++) {
29 29 qreal value = 100 + qrand() % 100;
@@ -37,6 +37,7 Widget::Widget(QWidget *parent)
37 37 }
38 38 m_donuts.append(donut);
39 39 qreal phase = qrand() % 180;
40 donut->setLabelsPosition(QPieSlice::LabelInsideTangential);
40 41 donut->setPieStartAngle(phase);
41 42 donut->setPieEndAngle(360 + phase);
42 43 chartView->chart()->addSeries(donut);
@@ -51,7 +52,7 Widget::Widget(QWidget *parent)
51 52
52 53 updateTimer = new QTimer(this);
53 54 connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateRotation()));
54 updateTimer->start(750);
55 updateTimer->start(1500);
55 56 }
56 57
57 58 Widget::~Widget()
@@ -20,20 +20,18 Widget::Widget(QWidget *parent)
20 20 chartView->chart()->setAnimationOptions(QChart::AllAnimations);
21 21
22 22 mainData = new QPieSeries;
23 mainData->setPieSize(0.6);
23 24 for (int j = 0; j < 4; j++) {
24
25 25 // create new slice for the mainData
26 26 QPieSlice *slice = new QPieSlice;
27 slice->setLabelPosition(QPieSlice::LabelInside);
28 27 slice->setLabelColor(Qt::white);
29 28 mainData->append(slice);
30 29
31 30 // create a new detailed data for the slice
32 31 QPieSeries *donut = new QPieSeries;
33 donut->setDonut();
34 donut->setLabelsVisible();
32 donut->setDonut();
35 33 donut->setDonutInnerSize(mainData->pieSize());
36 donut->setPieSize(mainData->pieSize() + 0.2);
34 donut->setPieSize(mainData->pieSize() + 0.15);
37 35
38 36 // when mainData slice is redrawn make sure the detailed data slices are aligned with it
39 37 connect(slice, SIGNAL(startAngleChanged()), this, SLOT(updatedStartAngle()));
@@ -44,9 +42,9 Widget::Widget(QWidget *parent)
44 42 qreal value = 10 + qrand() % 100;
45 43 QPieSlice *slice = new QPieSlice(QString("%1").arg(value), value);
46 44 donut->append(slice);
47 donut->slices().last()->setLabelVisible(true);
48 donut->slices().last()->setLabelColor(Qt::white);
49 45 }
46 donut->setLabelsPosition(QPieSlice::LabelOutside);
47 donut->setLabelsVisible();
50 48 detailedData.append(donut);
51 49
52 50 // update the value and label of mainData
@@ -55,6 +53,7 Widget::Widget(QWidget *parent)
55 53 }
56 54
57 55 mainData->setLabelsVisible();
56 mainData->setLabelsPosition(QPieSlice::LabelInside);
58 57 chartView->chart()->addSeries(mainData);
59 58 for (int i = 0; i < detailedData.count(); i++)
60 59 chartView->chart()->addSeries(detailedData.at(i));
@@ -84,17 +84,33 void PieSliceItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*op
84 84 painter->setPen(m_data.m_labelBrush.color());
85 85 painter->setBrush(m_data.m_labelBrush);
86 86 painter->setFont(m_data.m_labelFont);
87 if (m_data.m_donut) {
88 painter->translate(m_labelTextRect.center());
89 painter->rotate(m_data.m_startAngle + m_data.m_angleSpan / 2);
90 painter->drawText(-m_labelTextRect.width() / 2, -m_labelTextRect.height() / 2, m_labelTextRect.width(), m_labelTextRect.height(), Qt::AlignCenter, m_data.m_labelText);
91 } else if (m_data.m_labelPosition == QPieSlice::LabelOutside) {
87
88 switch (m_data.m_labelPosition)
89 {
90 case QPieSlice::LabelOutside:
92 91 painter->setClipRect(parentItem()->boundingRect());
93 92 painter->strokePath(m_labelArmPath, m_data.m_labelBrush.color());
94 93 painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText);
95 } else { // QPieSlice::LabelInside
94 break;
95 case QPieSlice::LabelInside:
96 96 painter->setClipPath(m_slicePath);
97 97 painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText);
98 break;
99 case QPieSlice::LabelInsideTangential:
100 painter->setClipPath(m_slicePath);
101 painter->translate(m_labelTextRect.center());
102 painter->rotate(m_data.m_startAngle + m_data.m_angleSpan / 2);
103 painter->drawText(-m_labelTextRect.width() / 2, -m_labelTextRect.height() / 2, m_labelTextRect.width(), m_labelTextRect.height(), Qt::AlignCenter, m_data.m_labelText);
104 break;
105 case QPieSlice::LabelInsideNormal:
106 painter->setClipPath(m_slicePath);
107 painter->translate(m_labelTextRect.center());
108 if (m_data.m_startAngle + m_data.m_angleSpan / 2 < 180)
109 painter->rotate(m_data.m_startAngle + m_data.m_angleSpan / 2 - 90);
110 else
111 painter->rotate(m_data.m_startAngle + m_data.m_angleSpan / 2 + 90);
112 painter->drawText(-m_labelTextRect.width() / 2, -m_labelTextRect.height() / 2, m_labelTextRect.width(), m_labelTextRect.height(), Qt::AlignCenter, m_data.m_labelText);
113 break;
98 114 }
99 115
100 116 painter->restore();
@@ -146,14 +162,22 void PieSliceItem::updateGeometry()
146 162 m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart);
147 163
148 164 // text position
149 if (m_data.m_donut) {
150 QPointF donutCenter = m_data.m_center + offset(centerAngle, m_data.m_innerRadius + (m_data.m_radius - m_data.m_innerRadius) / 2);
151 m_labelTextRect.moveCenter(donutCenter);
152 } else if (m_data.m_labelPosition == QPieSlice::LabelOutside) {
165 switch (m_data.m_labelPosition)
166 {
167 case QPieSlice::LabelOutside:
153 168 m_labelTextRect.moveBottomLeft(labelTextStart);
154 } else {// QPieSlice::LabelInside
155 QPointF sliceCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2);
156 m_labelTextRect.moveCenter(sliceCenter);
169 break;
170 case QPieSlice::LabelInside:
171 case QPieSlice::LabelInsideTangential:
172 case QPieSlice::LabelInsideNormal:{
173 QPointF textCenter;
174 if (m_data.m_donut)
175 textCenter = m_data.m_center + offset(centerAngle, m_data.m_innerRadius + (m_data.m_radius - m_data.m_innerRadius) / 2);
176 else
177 textCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2);
178 m_labelTextRect.moveCenter(textCenter);
179 break;
180 }
157 181 }
158 182
159 183 // bounding rect
@@ -683,6 +683,13 void QPieSeries::setLabelsVisible(bool visible)
683 683 s->setLabelVisible(visible);
684 684 }
685 685
686 void QPieSeries::setLabelsPosition(QPieSlice::LabelPosition position)
687 {
688 Q_D(QPieSeries);
689 foreach (QPieSlice* s, d->m_slices)
690 s->setLabelPosition(position);
691 }
692
686 693 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
687 694
688 695
@@ -22,10 +22,11
22 22 #define PIESERIES_H
23 23
24 24 #include <qabstractseries.h>
25 #include <QPieSlice>
25 26
26 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 28 class QPieSeriesPrivate;
28 class QPieSlice;
29 //class QPieSlice;
29 30
30 31 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QAbstractSeries
31 32 {
@@ -83,6 +84,7 public:
83 84 qreal pieEndAngle() const;
84 85
85 86 void setLabelsVisible(bool visible = true);
87 void setLabelsPosition(QPieSlice::LabelPosition position);
86 88
87 89 Q_SIGNALS:
88 90 void added(QList<QPieSlice*> slices);
@@ -57,7 +57,9 class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject
57 57 public:
58 58 enum LabelPosition {
59 59 LabelOutside,
60 LabelInside
60 LabelInside,
61 LabelInsideTangential,
62 LabelInsideNormal
61 63 };
62 64
63 65 public:
General Comments 0
You need to be logged in to leave comments. Login now