##// END OF EJS Templates
Removed QDonutGroup class. Added Donut Drill down example
Marek Rosa -
r1697:b1c06caff815
parent child
Show More
@@ -0,0 +1,9
1 !include( ../examples.pri ) {
2 error( "Couldn't find the examples.pri file!" )
3 }
4
5 TARGET = donutdrilldown
6 SOURCES += main.cpp\
7 widget.cpp
8
9 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,120
1 #include "widget.h"
2 #include <QGridLayout>
3
4 #include <QPieSlice>
5 #include <QTime>
6 #include <QChartView>
7 #include <QTimer>
8
9 QTCOMMERCIALCHART_USE_NAMESPACE
10
11 Widget::Widget(QWidget *parent)
12 : QWidget(parent),
13 mainData(0)
14 {
15 setMinimumSize(800, 600);
16 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
17
18 QChartView *chartView = new QChartView;
19 chartView->setRenderHint(QPainter::Antialiasing);
20 chartView->chart()->setAnimationOptions(QChart::AllAnimations);
21
22 mainData = new QPieSeries;
23 for (int j = 0; j < 4; j++) {
24
25 // create new slice for the mainData
26 QPieSlice *slice = new QPieSlice;
27 slice->setLabelPosition(QPieSlice::LabelInside);
28 slice->setLabelColor(Qt::white);
29 mainData->append(slice);
30
31 // create a new detailed data for the slice
32 QPieSeries *donut = new QPieSeries;
33 donut->setDonut();
34 donut->setLabelsVisible();
35 donut->setDonutInnerSize(mainData->pieSize());
36 donut->setPieSize(mainData->pieSize() + 0.2);
37
38 // when mainData slice is redrawn make sure the detailed data slices are aligned with it
39 connect(slice, SIGNAL(startAngleChanged()), this, SLOT(updatedStartAngle()));
40 connect(slice, SIGNAL(angleSpanChanged()), this, SLOT(updatedAngleSpan()));
41
42 // create the detailed data
43 for (int j = 0; j < 3; j++) {
44 qreal value = 10 + qrand() % 100;
45 QPieSlice *slice = new QPieSlice(QString("%1").arg(value), value);
46 donut->append(slice);
47 donut->slices().last()->setLabelVisible(true);
48 donut->slices().last()->setLabelColor(Qt::white);
49 }
50 detailedData.append(donut);
51
52 // update the value and label of mainData
53 slice->setValue(donut->sum());
54 slice->setLabel(QString("%1").arg(donut->sum()));
55 }
56
57 mainData->setLabelsVisible();
58 chartView->chart()->addSeries(mainData);
59 for (int i = 0; i < detailedData.count(); i++)
60 chartView->chart()->addSeries(detailedData.at(i));
61
62 // create main layout
63 QGridLayout* mainLayout = new QGridLayout;
64 mainLayout->addWidget(chartView, 1, 1);
65 setLayout(mainLayout);
66
67 // modify the value of one detailed slice every 2.5 sec
68 QTimer *updateTimer = new QTimer(this);
69 connect(updateTimer, SIGNAL(timeout()), this, SLOT(highlight()));
70 updateTimer->start(2500);
71 }
72
73 Widget::~Widget()
74 {
75
76 }
77
78 void Widget::updatedStartAngle()
79 {
80 // when the mainData slice has been updated the detailed data QPieSeries object as well
81 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
82 QPieSeries *detailsDonut = detailedData.at(slice->series()->slices().indexOf(slice));
83 detailsDonut->setPieStartAngle(slice->startAngle());
84 }
85
86 void Widget::updatedAngleSpan()
87 {
88 // when the mainData slice has been updated the detailed data QPieSeries object as well
89 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
90 QPieSeries *detailsDonut = detailedData.at(slice->series()->slices().indexOf(slice));
91 detailsDonut->setPieEndAngle(slice->startAngle() + slice->angleSpan());
92 }
93
94 void Widget::updateRotation()
95 {
96 // update the selected slice
97 qreal newValue = 10 + qrand() % 100;
98 detailedData.at(detailIndex)->slices().at(sliceIndex)->setValue(newValue);
99 detailedData.at(detailIndex)->slices().at(sliceIndex)->setLabel(QString("%1").arg(newValue));
100
101 // update the mainData slice with a new sum of the detailed data values
102 mainData->slices().at(detailIndex)->setValue(detailedData.at(detailIndex)->sum());
103 mainData->slices().at(detailIndex)->setLabel(QString("%1").arg(detailedData.at(detailIndex)->sum()));
104
105 // change the explode state of the selected slice back to normal
106 detailedData.at(detailIndex)->slices().at(sliceIndex)->setExploded(false);
107 }
108
109 void Widget::highlight()
110 {
111 // choose one random detailed data slice to be updated.
112 detailIndex = qrand() % mainData->count();
113 sliceIndex = qrand() % detailedData.at(detailIndex)->count();
114
115 // set the slice to exploded to make the change easier to observe
116 detailedData.at(detailIndex)->slices().at(sliceIndex)->setExploded();
117
118 // give the user time to focus on the slice that will be changed
119 QTimer::singleShot(1000, this, SLOT(updateRotation()));
120 }
@@ -0,0 +1,30
1 #ifndef WIDGET_H
2 #define WIDGET_H
3
4 #include <QWidget>
5 #include <QPieSeries>
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 updatedStartAngle();
19 void updatedAngleSpan();
20 void updateRotation();
21 void highlight();
22
23 private:
24 QPieSeries *mainData;
25 QList<QPieSeries *> detailedData;
26 int detailIndex;
27 int sliceIndex;
28 };
29
30 #endif // WIDGET_H
@@ -17,24 +17,29 Widget::Widget(QWidget *parent)
17 17 QChartView *chartView = new QChartView;
18 18 chartView->setRenderHint(QPainter::Antialiasing);
19 19
20 for (int i = 0; i < 7; i++) {
20 qreal minSize = 0.1;
21 qreal maxSize = 0.9;
22 int donutsCount = 5;
23 for (int i = 0; i < donutsCount; i++) {
21 24 QPieSeries *donut = new QPieSeries;
25 donut->setDonut();
22 26 donut->setLabelsVisible();
23 for (int j = 0; j < 4; j++) {
27 int sliceCount = 3 + qrand() % 3;
28 for (int j = 0; j < sliceCount; j++) {
24 29 qreal value = 100 + qrand() % 100;
25 donut->append(QString("%1").arg(value), value);
30 QPieSlice *slice = new QPieSlice(QString("%1").arg(value), value);
31 connect(slice, SIGNAL(hovered(bool)), this, SLOT(explodeSlice(bool)));
32 donut->append(slice);
26 33 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);
34 donut->slices().last()->setLabelColor(Qt::white);
35 donut->setDonutInnerSize(minSize + i * (maxSize - minSize) / donutsCount);
36 donut->setPieSize(minSize + (i + 1) * (maxSize - minSize) / donutsCount);
32 37 }
38 m_donuts.append(donut);
33 39 qreal phase = qrand() % 180;
34 40 donut->setPieStartAngle(phase);
35 41 donut->setPieEndAngle(360 + phase);
36 42 chartView->chart()->addSeries(donut);
37 m_donutsGroup.append(donut);
38 43 }
39 44
40 45 // create main layout
@@ -44,7 +49,7 Widget::Widget(QWidget *parent)
44 49
45 50 chartView->chart()->setAnimationOptions(QChart::AllAnimations);
46 51
47 QTimer *updateTimer = new QTimer(this);
52 updateTimer = new QTimer(this);
48 53 connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateRotation()));
49 54 updateTimer->start(750);
50 55 }
@@ -56,10 +61,35 Widget::~Widget()
56 61
57 62 void Widget::updateRotation()
58 63 {
59 for (int i = 0; i < m_donutsGroup.count(); i++) {
60 QPieSeries *donut = m_donutsGroup.donuts().at(i);
64 // int tobeupdated = qrand() % m_donutsGroup.count();
65 for (int i = 0; i < m_donuts.count(); i++) {
66 QPieSeries *donut = m_donuts.at(i);
61 67 qreal phaseShift = -50 + qrand() % 100;
62 68 donut->setPieStartAngle(donut->pieStartAngle() + phaseShift);
63 69 donut->setPieEndAngle(donut->pieEndAngle() + phaseShift);
64 70 }
65 71 }
72
73 void Widget::explodeSlice(bool exploded)
74 {
75 QPieSlice *slice = qobject_cast<QPieSlice *>(sender());
76 if (exploded) {
77 updateTimer->stop();
78 qreal sliceStartAngle = slice->startAngle();
79 qreal sliceEndAngle = slice->startAngle() + slice->angleSpan();
80
81 QPieSeries *donut = slice->series();
82 qreal seriesIndex = m_donuts.indexOf(donut);
83 for (int i = seriesIndex + 1; i < m_donuts.count(); i++) {
84 m_donuts.at(i)->setPieStartAngle(sliceEndAngle);
85 m_donuts.at(i)->setPieEndAngle(360 + sliceStartAngle);
86 }
87 } else {
88 for (int i = 0; i < m_donuts.count(); i++) {
89 m_donuts.at(i)->setPieStartAngle(0);
90 m_donuts.at(i)->setPieEndAngle(360);
91 }
92 updateTimer->start();
93 }
94 slice->setExploded(exploded);
95 }
@@ -2,7 +2,9
2 2 #define WIDGET_H
3 3
4 4 #include <QWidget>
5 #include <QDonutGroup>
5 #include <QPieSeries>
6
7 class QTimer;
6 8
7 9 QTCOMMERCIALCHART_USE_NAMESPACE
8 10
@@ -16,9 +18,11 public:
16 18
17 19 public slots:
18 20 void updateRotation();
21 void explodeSlice(bool exploded);
19 22
20 23 private:
21 QDonutGroup m_donutsGroup;
24 QList<QPieSeries *> m_donuts;
25 QTimer *updateTimer;
22 26 };
23 27
24 28 #endif // WIDGET_H
@@ -27,4 +27,5 SUBDIRS += \
27 27 horizontalbarchart \
28 28 horizontalstackedbarchart \
29 29 horizontalpercentbarchart \
30 donut
30 donut \
31 donutdrilldown
@@ -8,8 +8,7 SOURCES += \
8 8 $$PWD/qpieslice.cpp \
9 9 $$PWD/qpiemodelmapper.cpp \
10 10 $$PWD/qvpiemodelmapper.cpp \
11 $$PWD/qhpiemodelmapper.cpp \
12 piechart/qdonutgroup.cpp
11 $$PWD/qhpiemodelmapper.cpp
13 12
14 13 PRIVATE_HEADERS += \
15 14 $$PWD/pieslicedata_p.h \
@@ -17,13 +16,11 PRIVATE_HEADERS += \
17 16 $$PWD/piesliceitem_p.h \
18 17 $$PWD/qpieslice_p.h \
19 18 $$PWD/qpieseries_p.h \
20 $$PWD/qpiemodelmapper_p.h \
21 $$PWD/qdonutgroup_p.h
19 $$PWD/qpiemodelmapper_p.h
22 20
23 21 PUBLIC_HEADERS += \
24 22 $$PWD/qpieseries.h \
25 23 $$PWD/qpieslice.h \
26 24 $$PWD/qpiemodelmapper.h \
27 25 $$PWD/qvpiemodelmapper.h \
28 $$PWD/qhpiemodelmapper.h \
29 $$PWD/qdonutgroup.h
26 $$PWD/qhpiemodelmapper.h
@@ -88,7 +88,7 void PieSliceItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*op
88 88 painter->translate(m_labelTextRect.center());
89 89 painter->rotate(m_data.m_startAngle + m_data.m_angleSpan / 2);
90 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) {
91 } else if (m_data.m_labelPosition == QPieSlice::LabelOutside) {
92 92 painter->setClipRect(parentItem()->boundingRect());
93 93 painter->strokePath(m_labelArmPath, m_data.m_labelBrush.color());
94 94 painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText);
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now