##// END OF EJS Templates
Adjust piechartdrilldown to look nicer with legend
Jani Honkonen -
r906:705c29daa707
parent child
Show More
@@ -1,69 +1,70
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 "drilldownslice.h"
21 #include "drilldownslice.h"
22
22
23 QTCOMMERCIALCHART_USE_NAMESPACE
23 QTCOMMERCIALCHART_USE_NAMESPACE
24
24
25 DrilldownSlice::DrilldownSlice(qreal value, QString prefix, QSeries* drilldownSeries)
25 DrilldownSlice::DrilldownSlice(qreal value, QString prefix, QSeries* drilldownSeries)
26 :m_drilldownSeries(drilldownSeries),
26 :m_drilldownSeries(drilldownSeries),
27 m_prefix(prefix)
27 m_prefix(prefix)
28 {
28 {
29 setValue(value);
29 setValue(value);
30 updateLabel();
30 updateLabel();
31 setLabelFont(QFont("Arial", 8));
31 connect(this, SIGNAL(changed()), this, SLOT(updateLabel()));
32 connect(this, SIGNAL(changed()), this, SLOT(updateLabel()));
32 connect(this, SIGNAL(hoverEnter()), this, SLOT(showHighlight()));
33 connect(this, SIGNAL(hoverEnter()), this, SLOT(showHighlight()));
33 connect(this, SIGNAL(hoverLeave()), this, SLOT(hideHighlight()));
34 connect(this, SIGNAL(hoverLeave()), this, SLOT(hideHighlight()));
34 }
35 }
35
36
36 DrilldownSlice::~DrilldownSlice()
37 DrilldownSlice::~DrilldownSlice()
37 {
38 {
38
39
39 }
40 }
40
41
41 QSeries* DrilldownSlice::drilldownSeries() const
42 QSeries* DrilldownSlice::drilldownSeries() const
42 {
43 {
43 return m_drilldownSeries;
44 return m_drilldownSeries;
44 }
45 }
45
46
46 void DrilldownSlice::updateLabel()
47 void DrilldownSlice::updateLabel()
47 {
48 {
48 QString label = m_prefix;
49 QString label = m_prefix;
49 label += " $";
50 label += " $";
50 label += QString::number(this->value());
51 label += QString::number(this->value());
51 label += ", ";
52 label += ", ";
52 label += QString::number(this->percentage()*100, 'f', 1);
53 label += QString::number(this->percentage()*100, 'f', 1);
53 label += "%";
54 label += "%";
54 setLabel(label);
55 setLabel(label);
55 }
56 }
56
57
57 void DrilldownSlice::showHighlight()
58 void DrilldownSlice::showHighlight()
58 {
59 {
59 setExploded();
60 setExploded();
60 setLabelVisible();
61 setLabelVisible();
61 }
62 }
62
63
63 void DrilldownSlice::hideHighlight()
64 void DrilldownSlice::hideHighlight()
64 {
65 {
65 setExploded(false);
66 setExploded(false);
66 setLabelVisible(false);
67 setLabelVisible(false);
67 }
68 }
68
69
69 #include "moc_drilldownslice.cpp"
70 #include "moc_drilldownslice.cpp"
@@ -1,77 +1,77
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 "drilldownchart.h"
21 #include "drilldownchart.h"
22 #include "drilldownslice.h"
22 #include "drilldownslice.h"
23 #include <QtGui/QApplication>
23 #include <QtGui/QApplication>
24 #include <QMainWindow>
24 #include <QMainWindow>
25 #include <QTime>
25 #include <QTime>
26 #include <QChartView>
26 #include <QChartView>
27 #include <QLegend>
27 #include <QLegend>
28 #include <QPieSeries>
28 #include <QPieSeries>
29
29
30 QTCOMMERCIALCHART_USE_NAMESPACE
30 QTCOMMERCIALCHART_USE_NAMESPACE
31
31
32 int main(int argc, char *argv[])
32 int main(int argc, char *argv[])
33 {
33 {
34 QApplication a(argc, argv);
34 QApplication a(argc, argv);
35
35
36 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
36 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
37
37
38 QMainWindow window;
38 QMainWindow window;
39
39
40 DrilldownChart* chart = new DrilldownChart();
40 DrilldownChart* chart = new DrilldownChart();
41 chart->setTheme(QChart::ChartThemeLight);
41 chart->setTheme(QChart::ChartThemeLight);
42 chart->setAnimationOptions(QChart::AllAnimations);
42 chart->setAnimationOptions(QChart::AllAnimations);
43 chart->legend()->setVisible(true);
43 chart->legend()->setVisible(true);
44 chart->legend()->setAlignmnent(QLegend::AlignmentRight);
44 chart->legend()->setAlignmnent(QLegend::AlignmentRight);
45
45
46 QPieSeries* yearSeries = new QPieSeries(&window);
46 QPieSeries* yearSeries = new QPieSeries(&window);
47 yearSeries->setName("Sales by year - All");
47 yearSeries->setName("Sales by year - All");
48
48
49 QList<QString> months;
49 QList<QString> months;
50 months << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
50 months << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
51 QList<QString> names;
51 QList<QString> names;
52 names << "Jane" << "John" << "Axel" << "Mary" << "Samantha" << "Bob";
52 names << "Jane" << "John" << "Axel" << "Mary" << "Susan" << "Bob";
53
53
54 foreach (QString name, names) {
54 foreach (QString name, names) {
55 QPieSeries* series = new QPieSeries(&window);
55 QPieSeries* series = new QPieSeries(&window);
56 series->setName("Sales by month - " + name);
56 series->setName("Sales by month - " + name);
57
57
58 foreach (QString month, months)
58 foreach (QString month, months)
59 *series << new DrilldownSlice(qrand() % 1000, month, yearSeries);
59 *series << new DrilldownSlice(qrand() % 1000, month, yearSeries);
60
60
61 QObject::connect(series, SIGNAL(clicked(QPieSlice*, Qt::MouseButtons)), chart, SLOT(handleSliceClicked(QPieSlice*)));
61 QObject::connect(series, SIGNAL(clicked(QPieSlice*, Qt::MouseButtons)), chart, SLOT(handleSliceClicked(QPieSlice*)));
62
62
63 *yearSeries << new DrilldownSlice(series->total(), name, series);
63 *yearSeries << new DrilldownSlice(series->total(), name, series);
64 }
64 }
65
65
66 QObject::connect(yearSeries, SIGNAL(clicked(QPieSlice*, Qt::MouseButtons)), chart, SLOT(handleSliceClicked(QPieSlice*)));
66 QObject::connect(yearSeries, SIGNAL(clicked(QPieSlice*, Qt::MouseButtons)), chart, SLOT(handleSliceClicked(QPieSlice*)));
67
67
68 chart->changeSeries(yearSeries);
68 chart->changeSeries(yearSeries);
69
69
70 QChartView* chartView = new QChartView(chart);
70 QChartView* chartView = new QChartView(chart);
71 chartView->setRenderHint(QPainter::Antialiasing);
71 chartView->setRenderHint(QPainter::Antialiasing);
72 window.setCentralWidget(chartView);
72 window.setCentralWidget(chartView);
73 window.resize(800, 600);
73 window.resize(800, 500);
74 window.show();
74 window.show();
75
75
76 return a.exec();
76 return a.exec();
77 }
77 }
General Comments 0
You need to be logged in to leave comments. Login now