@@ -0,0 +1,8 | |||||
|
1 | !include( ../examples.pri ) { | |||
|
2 | error( "Couldn't find the examples.pri file!" ) | |||
|
3 | } | |||
|
4 | ||||
|
5 | TARGET = donut | |||
|
6 | SOURCES += main.cpp\ | |||
|
7 | widget.cpp | |||
|
8 | HEADERS += widget.h |
@@ -0,0 +1,11 | |||||
|
1 | #include <QApplication> | |||
|
2 | #include "widget.h" | |||
|
3 | ||||
|
4 | int main(int argc, char *argv[]) | |||
|
5 | { | |||
|
6 | QApplication a(argc, argv); | |||
|
7 | Widget w; | |||
|
8 | w.show(); | |||
|
9 | ||||
|
10 | return a.exec(); | |||
|
11 | } |
@@ -0,0 +1,65 | |||||
|
1 | #include "widget.h" | |||
|
2 | #include <QChartView> | |||
|
3 | #include <QPieSeries> | |||
|
4 | #include <QPieSlice> | |||
|
5 | #include <QTime> | |||
|
6 | #include <QGridLayout> | |||
|
7 | #include <QTimer> | |||
|
8 | ||||
|
9 | QTCOMMERCIALCHART_USE_NAMESPACE | |||
|
10 | ||||
|
11 | Widget::Widget(QWidget *parent) | |||
|
12 | : QWidget(parent) | |||
|
13 | { | |||
|
14 | setMinimumSize(800, 600); | |||
|
15 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); | |||
|
16 | ||||
|
17 | QChartView *chartView = new QChartView; | |||
|
18 | chartView->setRenderHint(QPainter::Antialiasing); | |||
|
19 | ||||
|
20 | for (int i = 0; i < 7; i++) { | |||
|
21 | QPieSeries *donut = new QPieSeries; | |||
|
22 | donut->setLabelsVisible(); | |||
|
23 | for (int j = 0; j < 4; j++) { | |||
|
24 | qreal value = 100 + qrand() % 100; | |||
|
25 | donut->append(QString("%1").arg(value), value); | |||
|
26 | donut->slices().last()->setLabelVisible(true); | |||
|
27 | // QFont labelFont = donut->slices().last()->labelFont(); | |||
|
28 | // // labelFont.setUnderline(true); | |||
|
29 | // labelFont.setBold(true); | |||
|
30 | // donut->slices().last()->setLabelFont(labelFont); | |||
|
31 | donut->slices().last()->setLabelColor(Qt::white); | |||
|
32 | } | |||
|
33 | qreal phase = qrand() % 180; | |||
|
34 | donut->setPieStartAngle(phase); | |||
|
35 | donut->setPieEndAngle(360 + phase); | |||
|
36 | chartView->chart()->addSeries(donut); | |||
|
37 | m_donutsGroup.append(donut); | |||
|
38 | } | |||
|
39 | ||||
|
40 | // create main layout | |||
|
41 | QGridLayout* mainLayout = new QGridLayout; | |||
|
42 | mainLayout->addWidget(chartView, 1, 1); | |||
|
43 | setLayout(mainLayout); | |||
|
44 | ||||
|
45 | chartView->chart()->setAnimationOptions(QChart::AllAnimations); | |||
|
46 | ||||
|
47 | QTimer *updateTimer = new QTimer(this); | |||
|
48 | connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateRotation())); | |||
|
49 | updateTimer->start(750); | |||
|
50 | } | |||
|
51 | ||||
|
52 | Widget::~Widget() | |||
|
53 | { | |||
|
54 | ||||
|
55 | } | |||
|
56 | ||||
|
57 | void Widget::updateRotation() | |||
|
58 | { | |||
|
59 | for (int i = 0; i < m_donutsGroup.count(); i++) { | |||
|
60 | QPieSeries *donut = m_donutsGroup.donuts().at(i); | |||
|
61 | qreal phaseShift = -50 + qrand() % 100; | |||
|
62 | donut->setPieStartAngle(donut->pieStartAngle() + phaseShift); | |||
|
63 | donut->setPieEndAngle(donut->pieEndAngle() + phaseShift); | |||
|
64 | } | |||
|
65 | } |
@@ -0,0 +1,24 | |||||
|
1 | #ifndef WIDGET_H | |||
|
2 | #define WIDGET_H | |||
|
3 | ||||
|
4 | #include <QWidget> | |||
|
5 | #include <QDonutGroup> | |||
|
6 | ||||
|
7 | QTCOMMERCIALCHART_USE_NAMESPACE | |||
|
8 | ||||
|
9 | class Widget : public QWidget | |||
|
10 | { | |||
|
11 | Q_OBJECT | |||
|
12 | ||||
|
13 | public: | |||
|
14 | Widget(QWidget *parent = 0); | |||
|
15 | ~Widget(); | |||
|
16 | ||||
|
17 | public slots: | |||
|
18 | void updateRotation(); | |||
|
19 | ||||
|
20 | private: | |||
|
21 | QDonutGroup m_donutsGroup; | |||
|
22 | }; | |||
|
23 | ||||
|
24 | #endif // WIDGET_H |
@@ -1,29 +1,30 | |||||
1 | CURRENTLY_BUILDING_COMPONENTS = "examples" |
|
1 | CURRENTLY_BUILDING_COMPONENTS = "examples" | |
2 | !include( ../config.pri ) { |
|
2 | !include( ../config.pri ) { | |
3 | error( "Couldn't find the config.pri file!" ) |
|
3 | error( "Couldn't find the config.pri file!" ) | |
4 | } |
|
4 | } | |
5 |
|
5 | |||
6 | TEMPLATE = subdirs |
|
6 | TEMPLATE = subdirs | |
7 | SUBDIRS += \ |
|
7 | SUBDIRS += \ | |
8 | areachart \ |
|
8 | areachart \ | |
9 | customchart \ |
|
9 | customchart \ | |
10 | linechart \ |
|
10 | linechart \ | |
11 | percentbarchart \ |
|
11 | percentbarchart \ | |
12 | piechart \ |
|
12 | piechart \ | |
13 | piechartdrilldown \ |
|
13 | piechartdrilldown \ | |
14 | presenterchart \ |
|
14 | presenterchart \ | |
15 | scatterchart \ |
|
15 | scatterchart \ | |
16 | scatterinteractions \ |
|
16 | scatterinteractions \ | |
17 | splinechart \ |
|
17 | splinechart \ | |
18 | stackedbarchart \ |
|
18 | stackedbarchart \ | |
19 | stackedbarchartdrilldown \ |
|
19 | stackedbarchartdrilldown \ | |
20 | zoomlinechart \ |
|
20 | zoomlinechart \ | |
21 | modeldata \ |
|
21 | modeldata \ | |
22 | barchart \ |
|
22 | barchart \ | |
23 | legend \ |
|
23 | legend \ | |
24 | barmodelmapper \ |
|
24 | barmodelmapper \ | |
25 | qmlpiechart \ |
|
25 | qmlpiechart \ | |
26 | lineandbar \ |
|
26 | lineandbar \ | |
27 | horizontalbarchart \ |
|
27 | horizontalbarchart \ | |
28 | horizontalstackedbarchart \ |
|
28 | horizontalstackedbarchart \ | |
29 | horizontalpercentbarchart |
|
29 | horizontalpercentbarchart \ | |
|
30 | donut |
@@ -1,241 +1,249 | |||||
1 | /**************************************************************************** |
|
1 | /**************************************************************************** | |
2 | ** |
|
2 | ** | |
3 | ** Copyright (C) 2012 Digia Plc |
|
3 | ** Copyright (C) 2012 Digia Plc | |
4 | ** All rights reserved. |
|
4 | ** All rights reserved. | |
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
6 | ** |
|
6 | ** | |
7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
8 | ** |
|
8 | ** | |
9 | ** $QT_BEGIN_LICENSE$ |
|
9 | ** $QT_BEGIN_LICENSE$ | |
10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
12 | ** Software or, alternatively, in accordance with the terms contained in |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
13 | ** a written agreement between you and Digia. |
|
13 | ** a written agreement between you and Digia. | |
14 | ** |
|
14 | ** | |
15 | ** If you have questions regarding the use of this file, please use |
|
15 | ** If you have questions regarding the use of this file, please use | |
16 | ** contact form at http://qt.digia.com |
|
16 | ** contact form at http://qt.digia.com | |
17 | ** $QT_END_LICENSE$ |
|
17 | ** $QT_END_LICENSE$ | |
18 | ** |
|
18 | ** | |
19 | ****************************************************************************/ |
|
19 | ****************************************************************************/ | |
20 |
|
20 | |||
21 | #include "piesliceitem_p.h" |
|
21 | #include "piesliceitem_p.h" | |
22 | #include "piechartitem_p.h" |
|
22 | #include "piechartitem_p.h" | |
23 | #include "qpieseries.h" |
|
23 | #include "qpieseries.h" | |
24 | #include "qpieslice.h" |
|
24 | #include "qpieslice.h" | |
25 | #include "chartpresenter_p.h" |
|
25 | #include "chartpresenter_p.h" | |
26 | #include <QPainter> |
|
26 | #include <QPainter> | |
27 | #include <qmath.h> |
|
27 | #include <qmath.h> | |
28 | #include <QGraphicsSceneEvent> |
|
28 | #include <QGraphicsSceneEvent> | |
29 | #include <QTime> |
|
29 | #include <QTime> | |
30 | #include <QDebug> |
|
30 | #include <QDebug> | |
31 |
|
31 | |||
32 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
32 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
33 |
|
33 | |||
34 | QPointF offset(qreal angle, qreal length) |
|
34 | QPointF offset(qreal angle, qreal length) | |
35 | { |
|
35 | { | |
36 | qreal dx = qSin(angle*(M_PI/180)) * length; |
|
36 | qreal dx = qSin(angle*(M_PI/180)) * length; | |
37 | qreal dy = qCos(angle*(M_PI/180)) * length; |
|
37 | qreal dy = qCos(angle*(M_PI/180)) * length; | |
38 | return QPointF(dx, -dy); |
|
38 | return QPointF(dx, -dy); | |
39 | } |
|
39 | } | |
40 |
|
40 | |||
41 | PieSliceItem::PieSliceItem(QGraphicsItem* parent) |
|
41 | PieSliceItem::PieSliceItem(QGraphicsItem* parent) | |
42 | :QGraphicsObject(parent), |
|
42 | :QGraphicsObject(parent), | |
43 | m_hovered(false) |
|
43 | m_hovered(false) | |
44 | { |
|
44 | { | |
45 | setAcceptHoverEvents(true); |
|
45 | setAcceptHoverEvents(true); | |
46 | setAcceptedMouseButtons(Qt::MouseButtonMask); |
|
46 | setAcceptedMouseButtons(Qt::MouseButtonMask); | |
47 | setZValue(ChartPresenter::PieSeriesZValue); |
|
47 | setZValue(ChartPresenter::PieSeriesZValue); | |
48 | } |
|
48 | } | |
49 |
|
49 | |||
50 | PieSliceItem::~PieSliceItem() |
|
50 | PieSliceItem::~PieSliceItem() | |
51 | { |
|
51 | { | |
52 | // If user is hovering over the slice and it gets destroyed we do |
|
52 | // If user is hovering over the slice and it gets destroyed we do | |
53 | // not get a hover leave event. So we must emit the signal here. |
|
53 | // not get a hover leave event. So we must emit the signal here. | |
54 | if (m_hovered) |
|
54 | if (m_hovered) | |
55 | emit hovered(false); |
|
55 | emit hovered(false); | |
56 | } |
|
56 | } | |
57 |
|
57 | |||
58 | QRectF PieSliceItem::boundingRect() const |
|
58 | QRectF PieSliceItem::boundingRect() const | |
59 | { |
|
59 | { | |
60 | return m_boundingRect; |
|
60 | return m_boundingRect; | |
61 | } |
|
61 | } | |
62 |
|
62 | |||
63 | QPainterPath PieSliceItem::shape() const |
|
63 | QPainterPath PieSliceItem::shape() const | |
64 | { |
|
64 | { | |
65 | // Don't include the label and label arm. |
|
65 | // Don't include the label and label arm. | |
66 | // This is used to detect a mouse clicks. We do not want clicks from label. |
|
66 | // This is used to detect a mouse clicks. We do not want clicks from label. | |
67 | return m_slicePath; |
|
67 | return m_slicePath; | |
68 | } |
|
68 | } | |
69 |
|
69 | |||
70 | void PieSliceItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/) |
|
70 | void PieSliceItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/) | |
71 | { |
|
71 | { | |
72 | painter->save(); |
|
72 | painter->save(); | |
73 | painter->setClipRect(parentItem()->boundingRect()); |
|
73 | painter->setClipRect(parentItem()->boundingRect()); | |
74 | painter->setPen(m_data.m_slicePen); |
|
74 | painter->setPen(m_data.m_slicePen); | |
75 | painter->setBrush(m_data.m_sliceBrush); |
|
75 | painter->setBrush(m_data.m_sliceBrush); | |
76 | painter->drawPath(m_slicePath); |
|
76 | painter->drawPath(m_slicePath); | |
77 | painter->restore(); |
|
77 | painter->restore(); | |
78 |
|
78 | |||
79 | if (m_data.m_isLabelVisible) { |
|
79 | if (m_data.m_isLabelVisible) { | |
80 | painter->save(); |
|
80 | painter->save(); | |
81 |
|
81 | |||
82 | // Pen for label arm not defined in the QPieSeries api, let's use brush's color instead |
|
82 | // Pen for label arm not defined in the QPieSeries api, let's use brush's color instead | |
83 | // Also, the drawText actually uses the pen color for the text color (unlike QGraphicsSimpleTextItem) |
|
83 | // Also, the drawText actually uses the pen color for the text color (unlike QGraphicsSimpleTextItem) | |
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_ |
|
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) { | |||
88 | painter->setClipRect(parentItem()->boundingRect()); |
|
92 | painter->setClipRect(parentItem()->boundingRect()); | |
89 | painter->strokePath(m_labelArmPath, m_data.m_labelBrush.color()); |
|
93 | painter->strokePath(m_labelArmPath, m_data.m_labelBrush.color()); | |
90 | } else { // QPieSlice::LabelInside |
|
94 | } else { // QPieSlice::LabelInside | |
91 | painter->setClipPath(m_slicePath); |
|
95 | painter->setClipPath(m_slicePath); | |
|
96 | painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText); | |||
92 | } |
|
97 | } | |
93 | painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText); |
|
|||
94 |
|
98 | |||
95 | painter->restore(); |
|
99 | painter->restore(); | |
96 | } |
|
100 | } | |
97 | } |
|
101 | } | |
98 |
|
102 | |||
99 | void PieSliceItem::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/) |
|
103 | void PieSliceItem::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/) | |
100 | { |
|
104 | { | |
101 | m_hovered = true; |
|
105 | m_hovered = true; | |
102 | emit hovered(true); |
|
106 | emit hovered(true); | |
103 | } |
|
107 | } | |
104 |
|
108 | |||
105 | void PieSliceItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/) |
|
109 | void PieSliceItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/) | |
106 | { |
|
110 | { | |
107 | m_hovered = false; |
|
111 | m_hovered = false; | |
108 | emit hovered(false); |
|
112 | emit hovered(false); | |
109 | } |
|
113 | } | |
110 |
|
114 | |||
111 | void PieSliceItem::mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
115 | void PieSliceItem::mousePressEvent(QGraphicsSceneMouseEvent *event) | |
112 | { |
|
116 | { | |
113 | emit clicked(event->buttons()); |
|
117 | emit clicked(event->buttons()); | |
114 | } |
|
118 | } | |
115 |
|
119 | |||
116 | void PieSliceItem::setLayout(const PieSliceData &sliceData) |
|
120 | void PieSliceItem::setLayout(const PieSliceData &sliceData) | |
117 | { |
|
121 | { | |
118 | m_data = sliceData; |
|
122 | m_data = sliceData; | |
119 | updateGeometry(); |
|
123 | updateGeometry(); | |
120 | update(); |
|
124 | update(); | |
121 | } |
|
125 | } | |
122 |
|
126 | |||
123 | void PieSliceItem::updateGeometry() |
|
127 | void PieSliceItem::updateGeometry() | |
124 | { |
|
128 | { | |
125 | if (m_data.m_radius <= 0) |
|
129 | if (m_data.m_radius <= 0) | |
126 | return; |
|
130 | return; | |
127 |
|
131 | |||
128 | prepareGeometryChange(); |
|
132 | prepareGeometryChange(); | |
129 |
|
133 | |||
130 | // slice path |
|
134 | // slice path | |
131 | qreal centerAngle; |
|
135 | qreal centerAngle; | |
132 | QPointF armStart; |
|
136 | QPointF armStart; | |
133 | m_slicePath = slicePath(m_data.m_center, m_data.m_radius, m_data.m_startAngle, m_data.m_angleSpan, ¢erAngle, &armStart); |
|
137 | m_slicePath = slicePath(m_data.m_center, m_data.m_radius, m_data.m_startAngle, m_data.m_angleSpan, ¢erAngle, &armStart); | |
134 |
|
138 | |||
135 | // text rect |
|
139 | // text rect | |
136 | QFontMetricsF fm(m_data.m_labelFont); |
|
140 | QFontMetricsF fm(m_data.m_labelFont); | |
137 | m_labelTextRect = QRectF(0, 0, fm.width(m_data.m_labelText), fm.height()); |
|
141 | m_labelTextRect = QRectF(0, 0, fm.width(m_data.m_labelText), fm.height()); | |
138 |
|
142 | |||
139 | // label arm path |
|
143 | // label arm path | |
140 | QPointF labelTextStart; |
|
144 | QPointF labelTextStart; | |
141 | m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart); |
|
145 | m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart); | |
142 |
|
146 | |||
143 | // text position |
|
147 | // text position | |
144 | if (m_data.m_labelPosition == QPieSlice::LabelOutside) |
|
148 | if (m_data.m_donut) { | |
|
149 | QPointF donutCenter = m_data.m_center + offset(centerAngle, m_data.m_innerRadius + (m_data.m_radius - m_data.m_innerRadius) / 2); | |||
|
150 | m_labelTextRect.moveCenter(donutCenter); | |||
|
151 | } else if (m_data.m_labelPosition == QPieSlice::LabelOutside) { | |||
145 | m_labelTextRect.moveBottomLeft(labelTextStart); |
|
152 | m_labelTextRect.moveBottomLeft(labelTextStart); | |
146 | else {// QPieSlice::LabelInside |
|
153 | } else {// QPieSlice::LabelInside | |
147 | QPointF sliceCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2); |
|
154 | QPointF sliceCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2); | |
148 | m_labelTextRect.moveCenter(sliceCenter); |
|
155 | m_labelTextRect.moveCenter(sliceCenter); | |
149 | } |
|
156 | } | |
150 |
|
157 | |||
151 | // bounding rect |
|
158 | // bounding rect | |
152 | if (m_data.m_isLabelVisible) |
|
159 | if (m_data.m_isLabelVisible) | |
153 | m_boundingRect = m_slicePath.boundingRect().united(m_labelArmPath.boundingRect()).united(m_labelTextRect); |
|
160 | m_boundingRect = m_slicePath.boundingRect().united(m_labelArmPath.boundingRect()).united(m_labelTextRect); | |
154 | else |
|
161 | else | |
155 | m_boundingRect = m_slicePath.boundingRect(); |
|
162 | m_boundingRect = m_slicePath.boundingRect(); | |
156 | } |
|
163 | } | |
157 |
|
164 | |||
158 | QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice) |
|
165 | QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice) | |
159 | { |
|
166 | { | |
160 | if (slice->isExploded()) { |
|
167 | if (slice->isExploded()) { | |
161 | qreal centerAngle = slice->startAngle() + (slice->angleSpan()/2); |
|
168 | qreal centerAngle = slice->startAngle() + (slice->angleSpan()/2); | |
162 | qreal len = radius * slice->explodeDistanceFactor(); |
|
169 | qreal len = radius * slice->explodeDistanceFactor(); | |
163 | point += offset(centerAngle, len); |
|
170 | point += offset(centerAngle, len); | |
164 | } |
|
171 | } | |
165 | return point; |
|
172 | return point; | |
166 | } |
|
173 | } | |
167 |
|
174 | |||
168 | QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF* armStart) |
|
175 | QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF* armStart) | |
169 | { |
|
176 | { | |
170 | // calculate center angle |
|
177 | // calculate center angle | |
171 | *centerAngle = startAngle + (angleSpan/2); |
|
178 | *centerAngle = startAngle + (angleSpan/2); | |
172 |
|
179 | |||
173 | // calculate slice rectangle |
|
180 | // calculate slice rectangle | |
174 | QRectF rect(center.x()-radius, center.y()-radius, radius*2, radius*2); |
|
181 | QRectF rect(center.x()-radius, center.y()-radius, radius*2, radius*2); | |
175 |
|
182 | |||
176 | // slice path |
|
183 | // slice path | |
177 | QPainterPath path; |
|
184 | QPainterPath path; | |
178 | if (m_data.m_donut) { |
|
185 | if (m_data.m_donut) { | |
179 | QRectF insideRect(center.x() - m_data.m_innerRadius, center.y()-m_data.m_innerRadius, m_data.m_innerRadius*2, m_data.m_innerRadius*2); |
|
186 | QRectF insideRect(center.x() - m_data.m_innerRadius, center.y()-m_data.m_innerRadius, m_data.m_innerRadius*2, m_data.m_innerRadius*2); | |
180 | path.arcMoveTo(rect, -startAngle + 90); |
|
187 | path.arcMoveTo(rect, -startAngle + 90); | |
181 | path.arcTo(rect, -startAngle + 90, -angleSpan); |
|
188 | path.arcTo(rect, -startAngle + 90, -angleSpan); | |
182 | path.arcTo(insideRect, -startAngle + 90 - angleSpan, angleSpan); |
|
189 | path.arcTo(insideRect, -startAngle + 90 - angleSpan, angleSpan); | |
183 | path.closeSubpath(); |
|
190 | path.closeSubpath(); | |
184 | } else { |
|
191 | } else { | |
185 | path.moveTo(rect.center()); |
|
192 | path.moveTo(rect.center()); | |
186 | path.arcTo(rect, -startAngle + 90, -angleSpan); |
|
193 | path.arcTo(rect, -startAngle + 90, -angleSpan); | |
187 | path.closeSubpath(); |
|
194 | path.closeSubpath(); | |
188 | } |
|
195 | } | |
189 |
|
196 | |||
190 | // calculate label arm start point |
|
197 | // calculate label arm start point | |
191 | *armStart = center; |
|
198 | *armStart = center; | |
192 | *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP); |
|
199 | *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP); | |
193 |
|
200 | |||
194 | return path; |
|
201 | return path; | |
195 | } |
|
202 | } | |
196 |
|
203 | |||
197 | QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart) |
|
204 | QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart) | |
198 | { |
|
205 | { | |
199 | // Normalize the angle to 0-360 range |
|
206 | // Normalize the angle to 0-360 range | |
200 | // NOTE: We are using int here on purpose. Depenging on platform and hardware |
|
207 | // NOTE: We are using int here on purpose. Depenging on platform and hardware | |
201 | // qreal can be a double, float or something the user gives to the Qt configure |
|
208 | // qreal can be a double, float or something the user gives to the Qt configure | |
202 | // (QT_COORD_TYPE). Compilers do not seem to support modulo for double or float |
|
209 | // (QT_COORD_TYPE). Compilers do not seem to support modulo for double or float | |
203 | // but there are fmod() and fmodf() functions for that. So instead of some #ifdef |
|
210 | // but there are fmod() and fmodf() functions for that. So instead of some #ifdef | |
204 | // that might break we just use int. Precision for this is just fine for our needs. |
|
211 | // that might break we just use int. Precision for this is just fine for our needs. | |
205 | int normalized = angle * 10.0; |
|
212 | int normalized = angle * 10.0; | |
206 | normalized = normalized % 3600; |
|
213 | normalized = normalized % 3600; | |
207 | if (normalized < 0) |
|
214 | if (normalized < 0) | |
208 | normalized += 3600; |
|
215 | normalized += 3600; | |
209 | angle = (qreal) normalized / 10.0; |
|
216 | angle = (qreal) normalized / 10.0; | |
210 |
|
217 | |||
211 | // prevent label arm pointing straight down because it will look bad |
|
218 | // prevent label arm pointing straight down because it will look bad | |
212 | if (angle < 180 && angle > 170) |
|
219 | if (angle < 180 && angle > 170) | |
213 | angle = 170; |
|
220 | angle = 170; | |
214 | if (angle > 180 && angle < 190) |
|
221 | if (angle > 180 && angle < 190) | |
215 | angle = 190; |
|
222 | angle = 190; | |
216 |
|
223 | |||
217 | // line from slice to label |
|
224 | // line from slice to label | |
218 | QPointF parm1 = start + offset(angle, length); |
|
225 | QPointF parm1 = start + offset(angle, length); | |
219 |
|
226 | |||
220 | // line to underline the label |
|
227 | // line to underline the label | |
221 | QPointF parm2 = parm1; |
|
228 | QPointF parm2 = parm1; | |
222 | if (angle < 180) { // arm swings the other way on the left side |
|
229 | if (angle < 180) { // arm swings the other way on the left side | |
223 | parm2 += QPointF(textWidth, 0); |
|
230 | parm2 += QPointF(textWidth, 0); | |
224 | *textStart = parm1; |
|
231 | *textStart = parm1; | |
225 | } |
|
232 | } | |
226 | else { |
|
233 | else { | |
227 | parm2 += QPointF(-textWidth,0); |
|
234 | parm2 += QPointF(-textWidth,0); | |
228 | *textStart = parm2; |
|
235 | *textStart = parm2; | |
229 | } |
|
236 | } | |
230 |
|
237 | |||
231 | QPainterPath path; |
|
238 | QPainterPath path; | |
232 | path.moveTo(start); |
|
239 | path.moveTo(start); | |
233 | path.lineTo(parm1); |
|
240 | path.lineTo(parm1); | |
234 | path.lineTo(parm2); |
|
241 | path.lineTo(parm2); | |
235 |
|
242 | |||
236 | return path; |
|
243 | return path; | |
237 | } |
|
244 | } | |
238 |
|
245 | |||
239 | #include "moc_piesliceitem_p.cpp" |
|
246 | #include "moc_piesliceitem_p.cpp" | |
240 |
|
247 | |||
241 | QTCOMMERCIALCHART_END_NAMESPACE |
|
248 | QTCOMMERCIALCHART_END_NAMESPACE | |
|
249 |
@@ -1,39 +1,75 | |||||
1 | #include "qdonutgroup.h" |
|
1 | #include "qdonutgroup.h" | |
2 | #include "qdonutgroup_p.h" |
|
2 | #include "qdonutgroup_p.h" | |
3 |
|
3 | |||
4 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
4 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
5 |
|
5 | |||
6 | QDonutGroup::QDonutGroup(QObject *parent) : |
|
6 | QDonutGroup::QDonutGroup(QObject *parent) : | |
7 | QObject(parent), |
|
7 | QObject(parent), | |
8 | d_ptr(new QDonutGroupPrivate(this)) |
|
8 | d_ptr(new QDonutGroupPrivate(this)) | |
9 | { |
|
9 | { | |
10 | } |
|
10 | } | |
11 |
|
11 | |||
12 | void QDonutGroup::append(QPieSeries *donut) |
|
12 | void QDonutGroup::append(QPieSeries *donut) | |
13 | { |
|
13 | { | |
|
14 | insert(count(), donut); | |||
|
15 | } | |||
|
16 | ||||
|
17 | bool QDonutGroup::insert(int index, QPieSeries* donut) | |||
|
18 | { | |||
14 | if (donut == 0) |
|
19 | if (donut == 0) | |
15 | return; |
|
20 | return false; | |
16 |
|
21 | |||
17 | donut->setDonut(); |
|
22 | donut->setDonut(); | |
18 | Q_D(QDonutGroup); |
|
23 | Q_D(QDonutGroup); | |
19 |
d->m_donuts. |
|
24 | d->m_donuts.insert(index, donut); | |
20 | qreal donutFraction = 1.0 / (d->m_donuts.count() + 1); |
|
25 | qreal donutFraction = 1.0 / (d->m_donuts.count() + 1); | |
21 | for(int i = 0; i < d->m_donuts.count(); i++) { |
|
26 | for(int i = 0; i < d->m_donuts.count(); i++) { | |
22 | d->m_donuts[i]->setPieSize( (i + 2) * donutFraction); |
|
27 | d->m_donuts[i]->setPieSize( (i + 2) * donutFraction); | |
23 | d->m_donuts[i]->setDonutInnerSize( (i + 1) * donutFraction); |
|
28 | d->m_donuts[i]->setDonutInnerSize( (i + 1) * donutFraction); | |
24 | } |
|
29 | } | |
|
30 | return true; | |||
|
31 | } | |||
|
32 | ||||
|
33 | bool QDonutGroup::remove(QPieSeries* donut) | |||
|
34 | { | |||
|
35 | Q_D(QDonutGroup); | |||
|
36 | int index = d->m_donuts.indexOf(donut); | |||
|
37 | if (index == -1) | |||
|
38 | return false; | |||
|
39 | else | |||
|
40 | d->m_donuts.removeOne(donut); | |||
|
41 | ||||
|
42 | return true; | |||
|
43 | } | |||
|
44 | ||||
|
45 | void QDonutGroup::clear() | |||
|
46 | { | |||
|
47 | Q_D(QDonutGroup); | |||
|
48 | d->m_donuts.clear(); | |||
|
49 | } | |||
|
50 | ||||
|
51 | QList<QPieSeries*> QDonutGroup::donuts() const | |||
|
52 | { | |||
|
53 | Q_D(const QDonutGroup); | |||
|
54 | return d->m_donuts; | |||
|
55 | } | |||
|
56 | ||||
|
57 | int QDonutGroup::count() const | |||
|
58 | { | |||
|
59 | Q_D(const QDonutGroup); | |||
|
60 | return d->m_donuts.count(); | |||
25 | } |
|
61 | } | |
26 |
|
62 | |||
27 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
63 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
28 |
|
64 | |||
29 | QDonutGroupPrivate::QDonutGroupPrivate(QDonutGroup *q): |
|
65 | QDonutGroupPrivate::QDonutGroupPrivate(QDonutGroup *q): | |
30 | QObject(q), |
|
66 | QObject(q), | |
31 | q_ptr(q) |
|
67 | q_ptr(q) | |
32 | { |
|
68 | { | |
33 | // |
|
69 | // | |
34 | } |
|
70 | } | |
35 |
|
71 | |||
36 | #include "moc_qdonutgroup.cpp" |
|
72 | #include "moc_qdonutgroup.cpp" | |
37 | #include "moc_qdonutgroup_p.cpp" |
|
73 | #include "moc_qdonutgroup_p.cpp" | |
38 |
|
74 | |||
39 | QTCOMMERCIALCHART_END_NAMESPACE |
|
75 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,27 +1,35 | |||||
1 | #ifndef QDONUTGROUP_H |
|
1 | #ifndef QDONUTGROUP_H | |
2 | #define QDONUTGROUP_H |
|
2 | #define QDONUTGROUP_H | |
3 |
|
3 | |||
4 | #include <QObject> |
|
4 | #include <QObject> | |
5 | #include <QPieSeries> |
|
5 | #include <QPieSeries> | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 | //class QPieSeries; |
|
9 | //class QPieSeries; | |
10 | class QDonutGroupPrivate; |
|
10 | class QDonutGroupPrivate; | |
11 |
|
11 | |||
12 | class QTCOMMERCIALCHART_EXPORT QDonutGroup : public QObject |
|
12 | class QTCOMMERCIALCHART_EXPORT QDonutGroup : public QObject | |
13 | { |
|
13 | { | |
14 | Q_OBJECT |
|
14 | Q_OBJECT | |
15 | public: |
|
15 | public: | |
16 | explicit QDonutGroup(QObject *parent = 0); |
|
16 | explicit QDonutGroup(QObject *parent = 0); | |
17 |
|
17 | |||
18 | void append(QPieSeries *donut); |
|
18 | void append(QPieSeries *donut); | |
19 |
|
19 | |||
|
20 | bool insert(int index, QPieSeries* donut); | |||
|
21 | ||||
|
22 | bool remove(QPieSeries* donut); | |||
|
23 | void clear(); | |||
|
24 | ||||
|
25 | QList<QPieSeries*> donuts() const; | |||
|
26 | int count() const; | |||
|
27 | ||||
20 | protected: |
|
28 | protected: | |
21 | QDonutGroupPrivate * const d_ptr; |
|
29 | QDonutGroupPrivate * const d_ptr; | |
22 | Q_DECLARE_PRIVATE(QDonutGroup) |
|
30 | Q_DECLARE_PRIVATE(QDonutGroup) | |
23 | }; |
|
31 | }; | |
24 |
|
32 | |||
25 | QTCOMMERCIALCHART_END_NAMESPACE |
|
33 | QTCOMMERCIALCHART_END_NAMESPACE | |
26 |
|
34 | |||
27 | #endif // QDONUTGROUP_H |
|
35 | #endif // QDONUTGROUP_H |
General Comments 0
You need to be logged in to leave comments.
Login now