@@ -1,76 +1,76 | |||||
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()->axisX()->setRange(0, 4.5); |
|
48 | chart()->axisX()->setRange(0, 4.5); | |
49 | chart()->axisY()->setRange(0, 4.5); |
|
49 | chart()->axisY()->setRange(0, 4.5); | |
50 |
|
50 | |||
51 | connect(m_scatter, SIGNAL(clicked(QPointF)), this, SLOT(handleClickedPoint(QPointF))); |
|
51 | connect(m_scatter, SIGNAL(clicked(QPointF)), this, SLOT(handleClickedPoint(QPointF))); | |
52 | } |
|
52 | } | |
53 |
|
53 | |||
54 | ChartView::~ChartView() |
|
54 | ChartView::~ChartView() | |
55 | { |
|
55 | { | |
56 | } |
|
56 | } | |
57 |
|
57 | |||
58 | void ChartView::handleClickedPoint(const QPointF& point) |
|
58 | void ChartView::handleClickedPoint(const QPointF& point) | |
59 | { |
|
59 | { | |
60 | QPointF clickedPoint = point; |
|
60 | QPointF clickedPoint = point; | |
61 | // Find the closest point from series 1 |
|
61 | // Find the closest point from series 1 | |
62 |
QPointF closest(INT |
|
62 | QPointF closest(INT_MAX, INT_MAX); | |
63 |
qreal distance(INT |
|
63 | qreal distance(INT_MAX); | |
64 | foreach(QPointF currentPoint, m_scatter->points()) { |
|
64 | foreach(QPointF currentPoint, m_scatter->points()) { | |
65 | qreal currentDistance = sqrt((currentPoint.x() - clickedPoint.x()) * (currentPoint.x() - clickedPoint.x()) |
|
65 | qreal currentDistance = sqrt((currentPoint.x() - clickedPoint.x()) * (currentPoint.x() - clickedPoint.x()) | |
66 | + (currentPoint.y() - clickedPoint.y()) * (currentPoint.y() - clickedPoint.y())); |
|
66 | + (currentPoint.y() - clickedPoint.y()) * (currentPoint.y() - clickedPoint.y())); | |
67 | if (currentDistance < distance) { |
|
67 | if (currentDistance < distance) { | |
68 | distance = currentDistance; |
|
68 | distance = currentDistance; | |
69 | closest = currentPoint; |
|
69 | closest = currentPoint; | |
70 | } |
|
70 | } | |
71 | } |
|
71 | } | |
72 |
|
72 | |||
73 | // Remove the closes point from series 1 and append it to series 2 |
|
73 | // Remove the closes point from series 1 and append it to series 2 | |
74 | m_scatter->remove(closest); |
|
74 | m_scatter->remove(closest); | |
75 | m_scatter2->append(closest); |
|
75 | m_scatter2->append(closest); | |
76 | } |
|
76 | } |
General Comments 0
You need to be logged in to leave comments.
Login now