##// END OF EJS Templates
Scatter interactions fixed
Marek Rosa -
r1620:2314d8026f62
parent child
Show More
@@ -1,184 +1,184
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 import QtQuick 1.0
21 import QtQuick 1.0
22 import QtCommercial.Chart 1.0
22 import QtCommercial.Chart 1.0
23 import QmlCustomModel 1.0
23 import QmlCustomModel 1.0
24
24
25 Rectangle {
25 Rectangle {
26 anchors.fill: parent
26 anchors.fill: parent
27
27
28 //![1]
28 //![1]
29 ChartView {
29 ChartView {
30 id: chartView
30 id: chartView
31 title: "Top-5 car brand shares in Finland"
31 title: "Top-5 car brand shares in Finland"
32 anchors.fill: parent
32 anchors.fill: parent
33 animationOptions: ChartView.SeriesAnimations
33 animationOptions: ChartView.SeriesAnimations
34
34
35 BarCategoriesAxis {
35 BarCategoriesAxis {
36 id: categoryAxis
36 id: categoryAxis
37 categories: ["2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014" ]
37 categories: ["2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014" ]
38 }
38 }
39
39
40 ValuesAxis {
40 ValuesAxis {
41 id: yAxis
41 id: yAxis
42 min: 0
42 min: 0
43 max: 60
43 max: 60
44 }
44 }
45 // ...
45 // ...
46 //![1]
46 //![1]
47
47
48 //![2]
48 //![2]
49 CustomModel {
49 CustomModel {
50 id: customModel
50 id: customModel
51 verticalHeaders: ["Manufacturer", "Volkswagen", "Toyota", "Ford", "Skoda", "Volvo", "Others"]
51 verticalHeaders: ["Manufacturer", "Volkswagen", "Toyota", "Ford", "Skoda", "Volvo", "Others"]
52 CustomModelElement { values: [0, "Manufacturer", 0, 1, 2, 3, 4] }
52 CustomModelElement { values: [0, "Manufacturer", 0, 1, 2, 3, 4] }
53 CustomModelElement { values: [1, "Volkswagen", 10.3, 12.0, 12.8, 13.0, 13.8] }
53 CustomModelElement { values: [1, "Volkswagen", 10.3, 12.0, 12.8, 13.0, 13.8] }
54 CustomModelElement { values: [2, "Toyota", 13.8, 13.5, 16.2, 13.7, 10.7] }
54 CustomModelElement { values: [2, "Toyota", 13.8, 13.5, 16.2, 13.7, 10.7] }
55 CustomModelElement { values: [3, "Ford", 6.4, 7.1, 8.9, 8.2, 8.6] }
55 CustomModelElement { values: [3, "Ford", 6.4, 7.1, 8.9, 8.2, 8.6] }
56 CustomModelElement { values: [4, "Skoda", 4.7, 5.8, 6.9, 8.3, 8.2] }
56 CustomModelElement { values: [4, "Skoda", 4.7, 5.8, 6.9, 8.3, 8.2] }
57 CustomModelElement { values: [5, "Volvo", 7.1, 6.7, 6.5, 6.3, 7.0] }
57 CustomModelElement { values: [5, "Volvo", 7.1, 6.7, 6.5, 6.3, 7.0] }
58 CustomModelElement { values: [6, "Others", 57.7, 54.9, 48.7, 50.5, 51.7] }
58 CustomModelElement { values: [6, "Others", 57.7, 54.9, 48.7, 50.5, 51.7] }
59 }
59 }
60 //![2]
60 //![2]
61
61
62 //![5]
62 //![5]
63 BarSeries {
63 BarSeries {
64 id: myBarSeries
64 id: myBarSeries
65 name: "Others"
65 name: "Others"
66 barWidth: 0.9
66 barWidth: 0.9
67 visible: false
67 visible: false
68 HBarModelMapper {
68 HBarModelMapper {
69 model: customModel
69 model: customModel
70 firstBarSetRow: 6
70 firstBarSetRow: 6
71 lastBarSetRow: 6
71 lastBarSetRow: 6
72 firstColumn: 2
72 firstColumn: 2
73 }
73 }
74 }
74 }
75 //![5]
75 //![5]
76
76
77 //![4]
77 //![4]
78 LineSeries {
78 LineSeries {
79 id: lineSeries1
79 id: lineSeries1
80 name: "Volkswagen"
80 name: "Volkswagen"
81 visible: false
81 visible: false
82 HXYModelMapper {
82 HXYModelMapper {
83 model: customModel
83 model: customModel
84 xRow: 0
84 xRow: 0
85 yRow: 1
85 yRow: 1
86 firstColumn: 2
86 firstColumn: 2
87 }
87 }
88 }
88 }
89 //![4]
89 //![4]
90
90
91 LineSeries {
91 LineSeries {
92 id: lineSeries2
92 id: lineSeries2
93 name: "Toyota"
93 name: "Toyota"
94 visible: false
94 visible: false
95 HXYModelMapper {
95 HXYModelMapper {
96 model: customModel
96 model: customModel
97 xRow: 0
97 xRow: 0
98 yRow: 2
98 yRow: 2
99 firstColumn: 2
99 firstColumn: 2
100 }
100 }
101 }
101 }
102
102
103 LineSeries {
103 LineSeries {
104 id: lineSeries3
104 id: lineSeries3
105 name: "Ford"
105 name: "Ford"
106 visible: false
106 visible: false
107 HXYModelMapper {
107 HXYModelMapper {
108 model: customModel
108 model: customModel
109 xRow: 0
109 xRow: 0
110 yRow: 3
110 yRow: 3
111 firstColumn: 2
111 firstColumn: 2
112 }
112 }
113 }
113 }
114
114
115 LineSeries {
115 LineSeries {
116 id: lineSeries4
116 id: lineSeries4
117 name: "Skoda"
117 name: "Skoda"
118 visible: false
118 visible: false
119 HXYModelMapper {
119 HXYModelMapper {
120 model: customModel
120 model: customModel
121 xRow: 0
121 xRow: 0
122 yRow: 4
122 yRow: 4
123 firstColumn: 2
123 firstColumn: 2
124 }
124 }
125 }
125 }
126
126
127 LineSeries {
127 LineSeries {
128 id: lineSeries5
128 id: lineSeries5
129 name: "Volvo"
129 name: "Volvo"
130 visible: false
130 visible: false
131 HXYModelMapper {
131 HXYModelMapper {
132 model: customModel
132 model: customModel
133 xRow: 0
133 xRow: 0
134 yRow: 5
134 yRow: 5
135 firstColumn: 2
135 firstColumn: 2
136 }
136 }
137 }
137 }
138
138
139 //![3]
139 //![3]
140 PieSeries {
140 PieSeries {
141 id: pieSeries
141 id: pieSeries
142 size: 0.4
142 size: 0.4
143 horizontalPosition: 0.85
143 horizontalPosition: 0.85
144 verticalPosition: 0.4
144 verticalPosition: 0.4
145 onClicked: {
145 onClicked: {
146 // Show the selection by exploding the slice
146 // Show the selection by exploding the slice
147 slice.exploded = !slice.exploded;
147 slice.exploded = !slice.exploded;
148
148
149 // Update the line series to show the yearly data for this slice
149 // Update the line series to show the yearly data for this slice
150 for (var i = 0; i < chartView.count; i++) {
150 for (var i = 0; i < chartView.count; i++) {
151 if (chartView.series(i).name == slice.label) {
151 if (chartView.series(i).name == slice.label) {
152 chartView.series(i).visible = slice.exploded;
152 chartView.series(i).visible = slice.exploded;
153 }
153 }
154 }
154 }
155 }
155 }
156
156
157 VPieModelMapper {
157 VPieModelMapper {
158 model: customModel
158 model: customModel
159 labelsColumn: 1
159 labelsColumn: 1
160 valuesColumn: 2
160 valuesColumn: 2
161 firstRow: 1
161 firstRow: 1
162 }
162 }
163 }
163 }
164 //![3]
164 //![3]
165
165
166 Component.onCompleted: {
166 Component.onCompleted: {
167 createDefaultAxes();
167 // createDefaultAxes();
168 setAxisX(categoryAxis,myBarSeries)
168 setAxisX(categoryAxis,myBarSeries)
169 setAxisX(categoryAxis,lineSeries1)
169 setAxisX(categoryAxis,lineSeries1)
170 setAxisX(categoryAxis,lineSeries2)
170 setAxisX(categoryAxis,lineSeries2)
171 setAxisX(categoryAxis,lineSeries3)
171 setAxisX(categoryAxis,lineSeries3)
172 setAxisX(categoryAxis,lineSeries4)
172 setAxisX(categoryAxis,lineSeries4)
173 setAxisX(categoryAxis,lineSeries5)
173 setAxisX(categoryAxis,lineSeries5)
174 setAxisY(yAxis,myBarSeries)
174 setAxisY(yAxis,myBarSeries)
175 setAxisY(yAxis,lineSeries1)
175 setAxisY(yAxis,lineSeries1)
176 setAxisY(yAxis,lineSeries2)
176 setAxisY(yAxis,lineSeries2)
177 setAxisY(yAxis,lineSeries3)
177 setAxisY(yAxis,lineSeries3)
178 setAxisY(yAxis,lineSeries4)
178 setAxisY(yAxis,lineSeries4)
179 setAxisY(yAxis,lineSeries5)
179 setAxisY(yAxis,lineSeries5)
180 categoryAxis.min = "2007"
180 categoryAxis.min = "2007"
181 categoryAxis.max = "2014"
181 categoryAxis.max = "2014"
182 }
182 }
183 }
183 }
184 }
184 }
@@ -1,76 +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 "chartview.h"
21 #include "chartview.h"
22 #include <math.h>
22 #include <math.h>
23 #include <QDebug>
23 #include <QDebug>
24
24
25 QTCOMMERCIALCHART_USE_NAMESPACE
25 QTCOMMERCIALCHART_USE_NAMESPACE
26
26
27 ChartView::ChartView(QWidget *parent)
27 ChartView::ChartView(QWidget *parent)
28 : QChartView(new QChart(), parent),
28 : QChartView(new QChart(), parent),
29 m_scatter(0),
29 m_scatter(0),
30 m_scatter2(0)
30 m_scatter2(0)
31 {
31 {
32 setRenderHint(QPainter::Antialiasing);
32 setRenderHint(QPainter::Antialiasing);
33
33
34 chart()->setTitle("Click to interact with scatter points");
34 chart()->setTitle("Click to interact with scatter points");
35
35
36 m_scatter = new QScatterSeries();
36 m_scatter = new QScatterSeries();
37 m_scatter->setName("scatter1");
37 m_scatter->setName("scatter1");
38 for(qreal x(0.5); x <= 4.0; x += 0.5) {
38 for(qreal x(0.5); x <= 4.0; x += 0.5) {
39 for(qreal y(0.5); y <= 4.0; y += 0.5) {
39 for(qreal y(0.5); y <= 4.0; y += 0.5) {
40 *m_scatter << QPointF(x, y);
40 *m_scatter << QPointF(x, y);
41 }
41 }
42 }
42 }
43 m_scatter2 = new QScatterSeries();
43 m_scatter2 = new QScatterSeries();
44 m_scatter2->setName("scatter2");
44 m_scatter2->setName("scatter2");
45
45
46 chart()->addSeries(m_scatter2);
46 chart()->addSeries(m_scatter2);
47 chart()->addSeries(m_scatter);
47 chart()->addSeries(m_scatter);
48 chart()->createDefaultAxes();
48 chart()->axisX()->setRange(0, 4.5);
49 chart()->axisX()->setRange(0, 4.5);
49 chart()->axisY()->setRange(0, 4.5);
50 chart()->axisY()->setRange(0, 4.5);
50
51
51 connect(m_scatter, SIGNAL(clicked(QPointF)), this, SLOT(handleClickedPoint(QPointF)));
52 connect(m_scatter, SIGNAL(clicked(QPointF)), this, SLOT(handleClickedPoint(QPointF)));
52 }
53 }
53
54
54 ChartView::~ChartView()
55 ChartView::~ChartView()
55 {
56 {
56 }
57 }
57
58
58 void ChartView::handleClickedPoint(const QPointF& point)
59 void ChartView::handleClickedPoint(const QPointF& point)
59 {
60 {
60 QPointF clickedPoint = point;
61 QPointF clickedPoint = point;
61 // Find the closest point from series 1
62 // Find the closest point from series 1
62 QPointF closest(INT_MAX, INT_MAX);
63 QPointF closest(INT_MAX, INT_MAX);
63 qreal distance(INT_MAX);
64 qreal distance(INT_MAX);
64 foreach(QPointF currentPoint, m_scatter->points()) {
65 foreach(QPointF currentPoint, m_scatter->points()) {
65 qreal currentDistance = sqrt((currentPoint.x() - clickedPoint.x()) * (currentPoint.x() - clickedPoint.x())
66 qreal currentDistance = sqrt((currentPoint.x() - clickedPoint.x()) * (currentPoint.x() - clickedPoint.x())
66 + (currentPoint.y() - clickedPoint.y()) * (currentPoint.y() - clickedPoint.y()));
67 + (currentPoint.y() - clickedPoint.y()) * (currentPoint.y() - clickedPoint.y()));
67 if (currentDistance < distance) {
68 if (currentDistance < distance) {
68 distance = currentDistance;
69 distance = currentDistance;
69 closest = currentPoint;
70 closest = currentPoint;
70 }
71 }
71 }
72 }
72
73
73 // Remove the closes point from series 1 and append it to series 2
74 // Remove the closes point from series 1 and append it to series 2
74 m_scatter->remove(closest);
75 m_scatter->remove(closest);
75 m_scatter2->append(closest);
76 m_scatter2->append(closest);
76 }
77 }
General Comments 0
You need to be logged in to leave comments. Login now