@@ -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 |
@@ -26,4 +26,5 SUBDIRS += \ | |||||
26 | lineandbar \ |
|
26 | lineandbar \ | |
27 | horizontalbarchart \ |
|
27 | horizontalbarchart \ | |
28 | horizontalstackedbarchart \ |
|
28 | horizontalstackedbarchart \ | |
29 | horizontalpercentbarchart |
|
29 | horizontalpercentbarchart \ | |
|
30 | donut |
@@ -84,13 +84,17 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_ |
|
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); | |
92 | } |
|
|||
93 | painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText); |
|
96 | painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText); | |
|
97 | } | |||
94 |
|
98 | |||
95 | painter->restore(); |
|
99 | painter->restore(); | |
96 | } |
|
100 | } | |
@@ -141,9 +145,12 void PieSliceItem::updateGeometry() | |||||
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 | } | |
@@ -239,3 +246,4 QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length | |||||
239 | #include "moc_piesliceitem_p.cpp" |
|
246 | #include "moc_piesliceitem_p.cpp" | |
240 |
|
247 | |||
241 | QTCOMMERCIALCHART_END_NAMESPACE |
|
248 | QTCOMMERCIALCHART_END_NAMESPACE | |
|
249 |
@@ -11,17 +11,53 QDonutGroup::QDonutGroup(QObject *parent) : | |||||
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 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
@@ -17,6 +17,14 public: | |||||
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) |
General Comments 0
You need to be logged in to leave comments.
Login now