From 0c76e21b1f34ce2599cd1ac5c2e6122222151438 2012-07-17 12:58:55 From: Marek Rosa Date: 2012-07-17 12:58:55 Subject: [PATCH] Added one more type of label placing on Pie chart --- diff --git a/demos/piechartcustomization/mainwidget.cpp b/demos/piechartcustomization/mainwidget.cpp index 0527e93..ec0bfdc 100644 --- a/demos/piechartcustomization/mainwidget.cpp +++ b/demos/piechartcustomization/mainwidget.cpp @@ -161,6 +161,8 @@ MainWidget::MainWidget(QWidget* parent) m_labelPosition = new QComboBox(this); m_labelPosition->addItem("Outside", QPieSlice::LabelOutside); m_labelPosition->addItem("Inside", QPieSlice::LabelInside); + m_labelPosition->addItem("Inside tangential", QPieSlice::LabelInsideTangential); + m_labelPosition->addItem("Inside normal", QPieSlice::LabelInsideNormal); QFormLayout* sliceSettingsLayout = new QFormLayout(); sliceSettingsLayout->addRow("Label", m_sliceName); diff --git a/examples/customchart/main.cpp b/examples/customchart/main.cpp index 733d5f6..878eab6 100644 --- a/examples/customchart/main.cpp +++ b/examples/customchart/main.cpp @@ -32,7 +32,7 @@ int main(int argc, char *argv[]) //![1] QLineSeries* series = new QLineSeries(); - *series << QPointF(0, 6) << QPointF(9, 4) << QPointF(15, 20) << QPointF(25, 12) << QPointF(39, 36); + *series << QPointF(0, 6) << QPointF(9, 4) << QPointF(15, 20) << QPointF(25, 12) << QPointF(29, 26); QChart* chart = new QChart(); chart->legend()->hide(); chart->addSeries(series); diff --git a/examples/donut/widget.cpp b/examples/donut/widget.cpp index ba7cc5c..61a0922 100644 --- a/examples/donut/widget.cpp +++ b/examples/donut/widget.cpp @@ -23,7 +23,7 @@ Widget::Widget(QWidget *parent) for (int i = 0; i < donutsCount; i++) { QPieSeries *donut = new QPieSeries; donut->setDonut(); - donut->setLabelsVisible(); + donut->setLabelsVisible(); int sliceCount = 3 + qrand() % 3; for (int j = 0; j < sliceCount; j++) { qreal value = 100 + qrand() % 100; @@ -37,6 +37,7 @@ Widget::Widget(QWidget *parent) } m_donuts.append(donut); qreal phase = qrand() % 180; + donut->setLabelsPosition(QPieSlice::LabelInsideTangential); donut->setPieStartAngle(phase); donut->setPieEndAngle(360 + phase); chartView->chart()->addSeries(donut); @@ -51,7 +52,7 @@ Widget::Widget(QWidget *parent) updateTimer = new QTimer(this); connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateRotation())); - updateTimer->start(750); + updateTimer->start(1500); } Widget::~Widget() diff --git a/examples/donutdrilldown/widget.cpp b/examples/donutdrilldown/widget.cpp index fd749ba..e0370d9 100644 --- a/examples/donutdrilldown/widget.cpp +++ b/examples/donutdrilldown/widget.cpp @@ -20,20 +20,18 @@ Widget::Widget(QWidget *parent) chartView->chart()->setAnimationOptions(QChart::AllAnimations); mainData = new QPieSeries; + mainData->setPieSize(0.6); for (int j = 0; j < 4; j++) { - // create new slice for the mainData QPieSlice *slice = new QPieSlice; - slice->setLabelPosition(QPieSlice::LabelInside); slice->setLabelColor(Qt::white); mainData->append(slice); // create a new detailed data for the slice QPieSeries *donut = new QPieSeries; - donut->setDonut(); - donut->setLabelsVisible(); + donut->setDonut(); donut->setDonutInnerSize(mainData->pieSize()); - donut->setPieSize(mainData->pieSize() + 0.2); + donut->setPieSize(mainData->pieSize() + 0.15); // when mainData slice is redrawn make sure the detailed data slices are aligned with it connect(slice, SIGNAL(startAngleChanged()), this, SLOT(updatedStartAngle())); @@ -44,9 +42,9 @@ Widget::Widget(QWidget *parent) qreal value = 10 + qrand() % 100; QPieSlice *slice = new QPieSlice(QString("%1").arg(value), value); donut->append(slice); - donut->slices().last()->setLabelVisible(true); - donut->slices().last()->setLabelColor(Qt::white); } + donut->setLabelsPosition(QPieSlice::LabelOutside); + donut->setLabelsVisible(); detailedData.append(donut); // update the value and label of mainData @@ -55,6 +53,7 @@ Widget::Widget(QWidget *parent) } mainData->setLabelsVisible(); + mainData->setLabelsPosition(QPieSlice::LabelInside); chartView->chart()->addSeries(mainData); for (int i = 0; i < detailedData.count(); i++) chartView->chart()->addSeries(detailedData.at(i)); diff --git a/src/piechart/piesliceitem.cpp b/src/piechart/piesliceitem.cpp index 83916a7..082af7c 100644 --- a/src/piechart/piesliceitem.cpp +++ b/src/piechart/piesliceitem.cpp @@ -84,17 +84,33 @@ void PieSliceItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*op painter->setPen(m_data.m_labelBrush.color()); painter->setBrush(m_data.m_labelBrush); painter->setFont(m_data.m_labelFont); - if (m_data.m_donut) { - painter->translate(m_labelTextRect.center()); - painter->rotate(m_data.m_startAngle + m_data.m_angleSpan / 2); - painter->drawText(-m_labelTextRect.width() / 2, -m_labelTextRect.height() / 2, m_labelTextRect.width(), m_labelTextRect.height(), Qt::AlignCenter, m_data.m_labelText); - } else if (m_data.m_labelPosition == QPieSlice::LabelOutside) { + + switch (m_data.m_labelPosition) + { + case QPieSlice::LabelOutside: painter->setClipRect(parentItem()->boundingRect()); painter->strokePath(m_labelArmPath, m_data.m_labelBrush.color()); painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText); - } else { // QPieSlice::LabelInside + break; + case QPieSlice::LabelInside: painter->setClipPath(m_slicePath); painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText); + break; + case QPieSlice::LabelInsideTangential: + painter->setClipPath(m_slicePath); + painter->translate(m_labelTextRect.center()); + painter->rotate(m_data.m_startAngle + m_data.m_angleSpan / 2); + painter->drawText(-m_labelTextRect.width() / 2, -m_labelTextRect.height() / 2, m_labelTextRect.width(), m_labelTextRect.height(), Qt::AlignCenter, m_data.m_labelText); + break; + case QPieSlice::LabelInsideNormal: + painter->setClipPath(m_slicePath); + painter->translate(m_labelTextRect.center()); + if (m_data.m_startAngle + m_data.m_angleSpan / 2 < 180) + painter->rotate(m_data.m_startAngle + m_data.m_angleSpan / 2 - 90); + else + 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); + break; } painter->restore(); @@ -146,14 +162,22 @@ void PieSliceItem::updateGeometry() m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart); // text position - if (m_data.m_donut) { - QPointF donutCenter = m_data.m_center + offset(centerAngle, m_data.m_innerRadius + (m_data.m_radius - m_data.m_innerRadius) / 2); - m_labelTextRect.moveCenter(donutCenter); - } else if (m_data.m_labelPosition == QPieSlice::LabelOutside) { + switch (m_data.m_labelPosition) + { + case QPieSlice::LabelOutside: m_labelTextRect.moveBottomLeft(labelTextStart); - } else {// QPieSlice::LabelInside - QPointF sliceCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2); - m_labelTextRect.moveCenter(sliceCenter); + break; + case QPieSlice::LabelInside: + case QPieSlice::LabelInsideTangential: + case QPieSlice::LabelInsideNormal:{ + QPointF textCenter; + if (m_data.m_donut) + textCenter = m_data.m_center + offset(centerAngle, m_data.m_innerRadius + (m_data.m_radius - m_data.m_innerRadius) / 2); + else + textCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2); + m_labelTextRect.moveCenter(textCenter); + break; + } } // bounding rect diff --git a/src/piechart/qpieseries.cpp b/src/piechart/qpieseries.cpp index 6ca762d..5683238 100644 --- a/src/piechart/qpieseries.cpp +++ b/src/piechart/qpieseries.cpp @@ -683,6 +683,13 @@ void QPieSeries::setLabelsVisible(bool visible) s->setLabelVisible(visible); } +void QPieSeries::setLabelsPosition(QPieSlice::LabelPosition position) +{ + Q_D(QPieSeries); + foreach (QPieSlice* s, d->m_slices) + s->setLabelPosition(position); +} + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/piechart/qpieseries.h b/src/piechart/qpieseries.h index 25db8c7..d78d869 100644 --- a/src/piechart/qpieseries.h +++ b/src/piechart/qpieseries.h @@ -22,10 +22,11 @@ #define PIESERIES_H #include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE class QPieSeriesPrivate; -class QPieSlice; +//class QPieSlice; class QTCOMMERCIALCHART_EXPORT QPieSeries : public QAbstractSeries { @@ -83,6 +84,7 @@ public: qreal pieEndAngle() const; void setLabelsVisible(bool visible = true); + void setLabelsPosition(QPieSlice::LabelPosition position); Q_SIGNALS: void added(QList slices); diff --git a/src/piechart/qpieslice.h b/src/piechart/qpieslice.h index 8c8f2f0..e579006 100644 --- a/src/piechart/qpieslice.h +++ b/src/piechart/qpieslice.h @@ -57,7 +57,9 @@ class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject public: enum LabelPosition { LabelOutside, - LabelInside + LabelInside, + LabelInsideTangential, + LabelInsideNormal }; public: