##// 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 m_labelPosition = new QComboBox(this);
161 m_labelPosition = new QComboBox(this);
162 m_labelPosition->addItem("Outside", QPieSlice::LabelOutside);
162 m_labelPosition->addItem("Outside", QPieSlice::LabelOutside);
163 m_labelPosition->addItem("Inside", QPieSlice::LabelInside);
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 QFormLayout* sliceSettingsLayout = new QFormLayout();
167 QFormLayout* sliceSettingsLayout = new QFormLayout();
166 sliceSettingsLayout->addRow("Label", m_sliceName);
168 sliceSettingsLayout->addRow("Label", m_sliceName);
@@ -32,7 +32,7 int main(int argc, char *argv[])
32
32
33 //![1]
33 //![1]
34 QLineSeries* series = new QLineSeries();
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 QChart* chart = new QChart();
36 QChart* chart = new QChart();
37 chart->legend()->hide();
37 chart->legend()->hide();
38 chart->addSeries(series);
38 chart->addSeries(series);
@@ -37,6 +37,7 Widget::Widget(QWidget *parent)
37 }
37 }
38 m_donuts.append(donut);
38 m_donuts.append(donut);
39 qreal phase = qrand() % 180;
39 qreal phase = qrand() % 180;
40 donut->setLabelsPosition(QPieSlice::LabelInsideTangential);
40 donut->setPieStartAngle(phase);
41 donut->setPieStartAngle(phase);
41 donut->setPieEndAngle(360 + phase);
42 donut->setPieEndAngle(360 + phase);
42 chartView->chart()->addSeries(donut);
43 chartView->chart()->addSeries(donut);
@@ -51,7 +52,7 Widget::Widget(QWidget *parent)
51
52
52 updateTimer = new QTimer(this);
53 updateTimer = new QTimer(this);
53 connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateRotation()));
54 connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateRotation()));
54 updateTimer->start(750);
55 updateTimer->start(1500);
55 }
56 }
56
57
57 Widget::~Widget()
58 Widget::~Widget()
@@ -20,20 +20,18 Widget::Widget(QWidget *parent)
20 chartView->chart()->setAnimationOptions(QChart::AllAnimations);
20 chartView->chart()->setAnimationOptions(QChart::AllAnimations);
21
21
22 mainData = new QPieSeries;
22 mainData = new QPieSeries;
23 mainData->setPieSize(0.6);
23 for (int j = 0; j < 4; j++) {
24 for (int j = 0; j < 4; j++) {
24
25 // create new slice for the mainData
25 // create new slice for the mainData
26 QPieSlice *slice = new QPieSlice;
26 QPieSlice *slice = new QPieSlice;
27 slice->setLabelPosition(QPieSlice::LabelInside);
28 slice->setLabelColor(Qt::white);
27 slice->setLabelColor(Qt::white);
29 mainData->append(slice);
28 mainData->append(slice);
30
29
31 // create a new detailed data for the slice
30 // create a new detailed data for the slice
32 QPieSeries *donut = new QPieSeries;
31 QPieSeries *donut = new QPieSeries;
33 donut->setDonut();
32 donut->setDonut();
34 donut->setLabelsVisible();
35 donut->setDonutInnerSize(mainData->pieSize());
33 donut->setDonutInnerSize(mainData->pieSize());
36 donut->setPieSize(mainData->pieSize() + 0.2);
34 donut->setPieSize(mainData->pieSize() + 0.15);
37
35
38 // when mainData slice is redrawn make sure the detailed data slices are aligned with it
36 // when mainData slice is redrawn make sure the detailed data slices are aligned with it
39 connect(slice, SIGNAL(startAngleChanged()), this, SLOT(updatedStartAngle()));
37 connect(slice, SIGNAL(startAngleChanged()), this, SLOT(updatedStartAngle()));
@@ -44,9 +42,9 Widget::Widget(QWidget *parent)
44 qreal value = 10 + qrand() % 100;
42 qreal value = 10 + qrand() % 100;
45 QPieSlice *slice = new QPieSlice(QString("%1").arg(value), value);
43 QPieSlice *slice = new QPieSlice(QString("%1").arg(value), value);
46 donut->append(slice);
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 detailedData.append(donut);
48 detailedData.append(donut);
51
49
52 // update the value and label of mainData
50 // update the value and label of mainData
@@ -55,6 +53,7 Widget::Widget(QWidget *parent)
55 }
53 }
56
54
57 mainData->setLabelsVisible();
55 mainData->setLabelsVisible();
56 mainData->setLabelsPosition(QPieSlice::LabelInside);
58 chartView->chart()->addSeries(mainData);
57 chartView->chart()->addSeries(mainData);
59 for (int i = 0; i < detailedData.count(); i++)
58 for (int i = 0; i < detailedData.count(); i++)
60 chartView->chart()->addSeries(detailedData.at(i));
59 chartView->chart()->addSeries(detailedData.at(i));
@@ -84,17 +84,33 void PieSliceItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*op
84 painter->setPen(m_data.m_labelBrush.color());
84 painter->setPen(m_data.m_labelBrush.color());
85 painter->setBrush(m_data.m_labelBrush);
85 painter->setBrush(m_data.m_labelBrush);
86 painter->setFont(m_data.m_labelFont);
86 painter->setFont(m_data.m_labelFont);
87 if (m_data.m_donut) {
87
88 painter->translate(m_labelTextRect.center());
88 switch (m_data.m_labelPosition)
89 painter->rotate(m_data.m_startAngle + m_data.m_angleSpan / 2);
89 {
90 painter->drawText(-m_labelTextRect.width() / 2, -m_labelTextRect.height() / 2, m_labelTextRect.width(), m_labelTextRect.height(), Qt::AlignCenter, m_data.m_labelText);
90 case QPieSlice::LabelOutside:
91 } else if (m_data.m_labelPosition == QPieSlice::LabelOutside) {
92 painter->setClipRect(parentItem()->boundingRect());
91 painter->setClipRect(parentItem()->boundingRect());
93 painter->strokePath(m_labelArmPath, m_data.m_labelBrush.color());
92 painter->strokePath(m_labelArmPath, m_data.m_labelBrush.color());
94 painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText);
93 painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText);
95 } else { // QPieSlice::LabelInside
94 break;
95 case QPieSlice::LabelInside:
96 painter->setClipPath(m_slicePath);
96 painter->setClipPath(m_slicePath);
97 painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText);
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 painter->restore();
116 painter->restore();
@@ -146,14 +162,22 void PieSliceItem::updateGeometry()
146 m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart);
162 m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart);
147
163
148 // text position
164 // text position
149 if (m_data.m_donut) {
165 switch (m_data.m_labelPosition)
150 QPointF donutCenter = m_data.m_center + offset(centerAngle, m_data.m_innerRadius + (m_data.m_radius - m_data.m_innerRadius) / 2);
166 {
151 m_labelTextRect.moveCenter(donutCenter);
167 case QPieSlice::LabelOutside:
152 } else if (m_data.m_labelPosition == QPieSlice::LabelOutside) {
153 m_labelTextRect.moveBottomLeft(labelTextStart);
168 m_labelTextRect.moveBottomLeft(labelTextStart);
154 } else {// QPieSlice::LabelInside
169 break;
155 QPointF sliceCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2);
170 case QPieSlice::LabelInside:
156 m_labelTextRect.moveCenter(sliceCenter);
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 // bounding rect
183 // bounding rect
@@ -683,6 +683,13 void QPieSeries::setLabelsVisible(bool visible)
683 s->setLabelVisible(visible);
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 #define PIESERIES_H
22 #define PIESERIES_H
23
23
24 #include <qabstractseries.h>
24 #include <qabstractseries.h>
25 #include <QPieSlice>
25
26
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 class QPieSeriesPrivate;
28 class QPieSeriesPrivate;
28 class QPieSlice;
29 //class QPieSlice;
29
30
30 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QAbstractSeries
31 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QAbstractSeries
31 {
32 {
@@ -83,6 +84,7 public:
83 qreal pieEndAngle() const;
84 qreal pieEndAngle() const;
84
85
85 void setLabelsVisible(bool visible = true);
86 void setLabelsVisible(bool visible = true);
87 void setLabelsPosition(QPieSlice::LabelPosition position);
86
88
87 Q_SIGNALS:
89 Q_SIGNALS:
88 void added(QList<QPieSlice*> slices);
90 void added(QList<QPieSlice*> slices);
@@ -57,7 +57,9 class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject
57 public:
57 public:
58 enum LabelPosition {
58 enum LabelPosition {
59 LabelOutside,
59 LabelOutside,
60 LabelInside
60 LabelInside,
61 LabelInsideTangential,
62 LabelInsideNormal
61 };
63 };
62
64
63 public:
65 public:
General Comments 0
You need to be logged in to leave comments. Login now