@@ -1,76 +1,76 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include "chartview.h" |
|
22 | 22 | #include <math.h> |
|
23 | 23 | #include <QDebug> |
|
24 | 24 | |
|
25 | 25 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
26 | 26 | |
|
27 | 27 | ChartView::ChartView(QWidget *parent) |
|
28 | 28 | : QChartView(new QChart(), parent), |
|
29 | 29 | m_scatter(0), |
|
30 | 30 | m_scatter2(0) |
|
31 | 31 | { |
|
32 | 32 | setRenderHint(QPainter::Antialiasing); |
|
33 | 33 | |
|
34 | 34 | chart()->setTitle("Click to interact with scatter points"); |
|
35 | 35 | |
|
36 | 36 | m_scatter = new QScatterSeries(); |
|
37 | 37 | m_scatter->setName("scatter1"); |
|
38 | 38 | for(qreal x(0.5); x <= 4.0; x += 0.5) { |
|
39 | 39 | for(qreal y(0.5); y <= 4.0; y += 0.5) { |
|
40 | 40 | *m_scatter << QPointF(x, y); |
|
41 | 41 | } |
|
42 | 42 | } |
|
43 | 43 | m_scatter2 = new QScatterSeries(); |
|
44 | 44 | m_scatter2->setName("scatter2"); |
|
45 | 45 | |
|
46 | 46 | chart()->addSeries(m_scatter2); |
|
47 | 47 | chart()->addSeries(m_scatter); |
|
48 | 48 | chart()->axisX()->setRange(0, 4.5); |
|
49 | 49 | chart()->axisY()->setRange(0, 4.5); |
|
50 | 50 | |
|
51 | 51 | connect(m_scatter, SIGNAL(clicked(QPointF)), this, SLOT(handleClickedPoint(QPointF))); |
|
52 | 52 | } |
|
53 | 53 | |
|
54 | 54 | ChartView::~ChartView() |
|
55 | 55 | { |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | void ChartView::handleClickedPoint(const QPointF& point) |
|
59 | 59 | { |
|
60 | 60 | QPointF clickedPoint = point; |
|
61 | 61 | // Find the closest point from series 1 |
|
62 |
QPointF closest(INT |
|
|
63 |
qreal distance(INT |
|
|
62 | QPointF closest(INT_MAX, INT_MAX); | |
|
63 | qreal distance(INT_MAX); | |
|
64 | 64 | foreach(QPointF currentPoint, m_scatter->points()) { |
|
65 | 65 | qreal currentDistance = sqrt((currentPoint.x() - clickedPoint.x()) * (currentPoint.x() - clickedPoint.x()) |
|
66 | 66 | + (currentPoint.y() - clickedPoint.y()) * (currentPoint.y() - clickedPoint.y())); |
|
67 | 67 | if (currentDistance < distance) { |
|
68 | 68 | distance = currentDistance; |
|
69 | 69 | closest = currentPoint; |
|
70 | 70 | } |
|
71 | 71 | } |
|
72 | 72 | |
|
73 | 73 | // Remove the closes point from series 1 and append it to series 2 |
|
74 | 74 | m_scatter->remove(closest); |
|
75 | 75 | m_scatter2->append(closest); |
|
76 | 76 | } |
General Comments 0
You need to be logged in to leave comments.
Login now