@@ -1,101 +1,101 | |||||
1 | /**************************************************************************** |
|
1 | /**************************************************************************** | |
2 | ** |
|
2 | ** | |
3 | ** Copyright (C) 2013 Digia Plc |
|
3 | ** Copyright (C) 2013 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 Enterprise Charts Add-on. |
|
7 | ** This file is part of the Qt Enterprise Charts Add-on. | |
8 | ** |
|
8 | ** | |
9 | ** $QT_BEGIN_LICENSE$ |
|
9 | ** $QT_BEGIN_LICENSE$ | |
10 | ** Licensees holding valid Qt Enterprise licenses may use this file in |
|
10 | ** Licensees holding valid Qt Enterprise licenses may use this file in | |
11 | ** accordance with the Qt Enterprise License Agreement provided with the |
|
11 | ** accordance with the Qt Enterprise 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 "chart.h" |
|
21 | #include "chart.h" | |
22 | #include <QValueAxis> |
|
22 | #include <QValueAxis> | |
23 | #include <QAbstractAxis> |
|
23 | #include <QAbstractAxis> | |
24 |
#include < |
|
24 | #include <qmath.h> | |
25 |
|
25 | |||
26 | Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags, QLineSeries *series) |
|
26 | Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags, QLineSeries *series) | |
27 | : QChart(QChart::ChartTypeCartesian, parent, wFlags), m_series(series) |
|
27 | : QChart(QChart::ChartTypeCartesian, parent, wFlags), m_series(series) | |
28 | { |
|
28 | { | |
29 | m_clicked = false; |
|
29 | m_clicked = false; | |
30 | } |
|
30 | } | |
31 |
|
31 | |||
32 | Chart::~Chart() |
|
32 | Chart::~Chart() | |
33 | { |
|
33 | { | |
34 | } |
|
34 | } | |
35 |
|
35 | |||
36 | void Chart::clickPoint(const QPointF &point) |
|
36 | void Chart::clickPoint(const QPointF &point) | |
37 | { |
|
37 | { | |
38 | // Find the closes data point |
|
38 | // Find the closes data point | |
39 | m_movingPoint = QPoint(); |
|
39 | m_movingPoint = QPoint(); | |
40 | m_clicked = false; |
|
40 | m_clicked = false; | |
41 | foreach (QPointF p, m_series->points()) { |
|
41 | foreach (QPointF p, m_series->points()) { | |
42 | if (distance(p, point) < distance(m_movingPoint, point)) { |
|
42 | if (distance(p, point) < distance(m_movingPoint, point)) { | |
43 | m_movingPoint = p; |
|
43 | m_movingPoint = p; | |
44 | m_clicked = true; |
|
44 | m_clicked = true; | |
45 | } |
|
45 | } | |
46 | } |
|
46 | } | |
47 | } |
|
47 | } | |
48 |
|
48 | |||
49 | qreal Chart::distance(const QPointF &p1, const QPointF &p2) |
|
49 | qreal Chart::distance(const QPointF &p1, const QPointF &p2) | |
50 | { |
|
50 | { | |
51 |
return |
|
51 | return qSqrt((p1.x() - p2.x()) * (p1.x() - p2.x()) | |
52 | + (p1.y() - p2.y()) * (p1.y() - p2.y())); |
|
52 | + (p1.y() - p2.y()) * (p1.y() - p2.y())); | |
53 | } |
|
53 | } | |
54 |
|
54 | |||
55 | void Chart::setPointClicked(bool clicked) |
|
55 | void Chart::setPointClicked(bool clicked) | |
56 | { |
|
56 | { | |
57 | m_clicked = clicked; |
|
57 | m_clicked = clicked; | |
58 | } |
|
58 | } | |
59 |
|
59 | |||
60 | void Chart::handlePointMove(const QPoint &point) |
|
60 | void Chart::handlePointMove(const QPoint &point) | |
61 | { |
|
61 | { | |
62 | if (m_clicked) { |
|
62 | if (m_clicked) { | |
63 | //Map the point clicked from the ChartView |
|
63 | //Map the point clicked from the ChartView | |
64 | //to the area occupied by the chart. |
|
64 | //to the area occupied by the chart. | |
65 | QPoint mappedPoint = point; |
|
65 | QPoint mappedPoint = point; | |
66 | mappedPoint.setX(point.x() - this->plotArea().x()); |
|
66 | mappedPoint.setX(point.x() - this->plotArea().x()); | |
67 | mappedPoint.setY(point.y() - this->plotArea().y()); |
|
67 | mappedPoint.setY(point.y() - this->plotArea().y()); | |
68 |
|
68 | |||
69 | //Get the x- and y axis to be able to convert the mapped |
|
69 | //Get the x- and y axis to be able to convert the mapped | |
70 | //coordinate point to the charts scale. |
|
70 | //coordinate point to the charts scale. | |
71 | QAbstractAxis *axisx = this->axisX(); |
|
71 | QAbstractAxis *axisx = this->axisX(); | |
72 | QValueAxis *haxis = 0; |
|
72 | QValueAxis *haxis = 0; | |
73 | if (axisx->type() == QAbstractAxis::AxisTypeValue) |
|
73 | if (axisx->type() == QAbstractAxis::AxisTypeValue) | |
74 | haxis = qobject_cast<QValueAxis *>(axisx); |
|
74 | haxis = qobject_cast<QValueAxis *>(axisx); | |
75 |
|
75 | |||
76 | QAbstractAxis *axisy = this->axisY(); |
|
76 | QAbstractAxis *axisy = this->axisY(); | |
77 | QValueAxis *vaxis = 0; |
|
77 | QValueAxis *vaxis = 0; | |
78 | if (axisy->type() == QAbstractAxis::AxisTypeValue) |
|
78 | if (axisy->type() == QAbstractAxis::AxisTypeValue) | |
79 | vaxis = qobject_cast<QValueAxis *>(axisy); |
|
79 | vaxis = qobject_cast<QValueAxis *>(axisy); | |
80 |
|
80 | |||
81 | if (haxis && vaxis) { |
|
81 | if (haxis && vaxis) { | |
82 | //Calculate the "unit" between points on the x |
|
82 | //Calculate the "unit" between points on the x | |
83 | //y axis. |
|
83 | //y axis. | |
84 | double xUnit = this->plotArea().width() / haxis->max(); |
|
84 | double xUnit = this->plotArea().width() / haxis->max(); | |
85 | double yUnit = this->plotArea().height() / vaxis->max(); |
|
85 | double yUnit = this->plotArea().height() / vaxis->max(); | |
86 |
|
86 | |||
87 | //Convert the mappedPoint to the actual chart scale. |
|
87 | //Convert the mappedPoint to the actual chart scale. | |
88 | double x = mappedPoint.x() / xUnit; |
|
88 | double x = mappedPoint.x() / xUnit; | |
89 | double y = vaxis->max() - mappedPoint.y() / yUnit; |
|
89 | double y = vaxis->max() - mappedPoint.y() / yUnit; | |
90 |
|
90 | |||
91 | //Replace the old point with the new one. |
|
91 | //Replace the old point with the new one. | |
92 | m_series->replace(m_movingPoint, QPointF(x, y)); |
|
92 | m_series->replace(m_movingPoint, QPointF(x, y)); | |
93 |
|
93 | |||
94 | //Update the m_movingPoint so we are able to |
|
94 | //Update the m_movingPoint so we are able to | |
95 | //do the replace also during mousemoveEvent. |
|
95 | //do the replace also during mousemoveEvent. | |
96 | m_movingPoint.setX(x); |
|
96 | m_movingPoint.setX(x); | |
97 | m_movingPoint.setY(y); |
|
97 | m_movingPoint.setY(y); | |
98 | } |
|
98 | } | |
99 | } |
|
99 | } | |
100 | } |
|
100 | } | |
101 |
|
101 |
@@ -1,93 +1,93 | |||||
1 | /**************************************************************************** |
|
1 | /**************************************************************************** | |
2 | ** |
|
2 | ** | |
3 | ** Copyright (C) 2013 Digia Plc |
|
3 | ** Copyright (C) 2013 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 Enterprise Charts Add-on. |
|
7 | ** This file is part of the Qt Enterprise Charts Add-on. | |
8 | ** |
|
8 | ** | |
9 | ** $QT_BEGIN_LICENSE$ |
|
9 | ** $QT_BEGIN_LICENSE$ | |
10 | ** Licensees holding valid Qt Enterprise licenses may use this file in |
|
10 | ** Licensees holding valid Qt Enterprise licenses may use this file in | |
11 | ** accordance with the Qt Enterprise License Agreement provided with the |
|
11 | ** accordance with the Qt Enterprise 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 "datasource.h" |
|
21 | #include "datasource.h" | |
22 | #include <QXYSeries> |
|
22 | #include <QXYSeries> | |
23 | #include <QAreaSeries> |
|
23 | #include <QAreaSeries> | |
24 | #include <QDeclarativeView> |
|
24 | #include <QDeclarativeView> | |
25 | #include <QDebug> |
|
25 | #include <QDebug> | |
26 |
#include < |
|
26 | #include <qmath.h> | |
27 |
|
27 | |||
28 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
28 | QTCOMMERCIALCHART_USE_NAMESPACE | |
29 |
|
29 | |||
30 | Q_DECLARE_METATYPE(QAbstractSeries *) |
|
30 | Q_DECLARE_METATYPE(QAbstractSeries *) | |
31 |
|
31 | |||
32 | DataSource::DataSource(QDeclarativeView *appViewer, QObject *parent) : |
|
32 | DataSource::DataSource(QDeclarativeView *appViewer, QObject *parent) : | |
33 | QObject(parent), |
|
33 | QObject(parent), | |
34 | m_appViewer(appViewer), |
|
34 | m_appViewer(appViewer), | |
35 | m_index(-1) |
|
35 | m_index(-1) | |
36 | { |
|
36 | { | |
37 | qRegisterMetaType<QAbstractSeries*>(); |
|
37 | qRegisterMetaType<QAbstractSeries*>(); | |
38 |
|
38 | |||
39 | generateData(0, 5, 1024); |
|
39 | generateData(0, 5, 1024); | |
40 | } |
|
40 | } | |
41 |
|
41 | |||
42 | void DataSource::update(QAbstractSeries *series) |
|
42 | void DataSource::update(QAbstractSeries *series) | |
43 | { |
|
43 | { | |
44 | if (series) { |
|
44 | if (series) { | |
45 | QXYSeries *xySeries = static_cast<QXYSeries *>(series); |
|
45 | QXYSeries *xySeries = static_cast<QXYSeries *>(series); | |
46 | m_index++; |
|
46 | m_index++; | |
47 | if (m_index > m_data.count() - 1) |
|
47 | if (m_index > m_data.count() - 1) | |
48 | m_index = 0; |
|
48 | m_index = 0; | |
49 |
|
49 | |||
50 | QList<QPointF> points = m_data.at(m_index); |
|
50 | QList<QPointF> points = m_data.at(m_index); | |
51 | // Use replace instead of clear + append, it's optimized for performance |
|
51 | // Use replace instead of clear + append, it's optimized for performance | |
52 | xySeries->replace(points); |
|
52 | xySeries->replace(points); | |
53 | } |
|
53 | } | |
54 | } |
|
54 | } | |
55 |
|
55 | |||
56 | void DataSource::generateData(int type, int rowCount, int colCount) |
|
56 | void DataSource::generateData(int type, int rowCount, int colCount) | |
57 | { |
|
57 | { | |
58 | // Remove previous data |
|
58 | // Remove previous data | |
59 | foreach (QList<QPointF> row, m_data) |
|
59 | foreach (QList<QPointF> row, m_data) | |
60 | row.clear(); |
|
60 | row.clear(); | |
61 | m_data.clear(); |
|
61 | m_data.clear(); | |
62 |
|
62 | |||
63 | // Append the new data depending on the type |
|
63 | // Append the new data depending on the type | |
64 | for (int i(0); i < rowCount; i++) { |
|
64 | for (int i(0); i < rowCount; i++) { | |
65 | QList<QPointF> points; |
|
65 | QList<QPointF> points; | |
66 | for (int j(0); j < colCount; j++) { |
|
66 | for (int j(0); j < colCount; j++) { | |
67 | qreal x(0); |
|
67 | qreal x(0); | |
68 | qreal y(0); |
|
68 | qreal y(0); | |
69 | switch (type) { |
|
69 | switch (type) { | |
70 | case 0: |
|
70 | case 0: | |
71 | // data with sin + random component |
|
71 | // data with sin + random component | |
72 |
y = |
|
72 | y = qSin(3.14159265358979 / 50 * j) + 0.5 + (qreal) rand() / (qreal) RAND_MAX; | |
73 | x = j; |
|
73 | x = j; | |
74 | break; |
|
74 | break; | |
75 | case 1: |
|
75 | case 1: | |
76 | // linear data |
|
76 | // linear data | |
77 | x = j; |
|
77 | x = j; | |
78 | y = (qreal) i / 10; |
|
78 | y = (qreal) i / 10; | |
79 | break; |
|
79 | break; | |
80 | default: |
|
80 | default: | |
81 | // unknown, do nothing |
|
81 | // unknown, do nothing | |
82 | break; |
|
82 | break; | |
83 | } |
|
83 | } | |
84 | points.append(QPointF(x, y)); |
|
84 | points.append(QPointF(x, y)); | |
85 | } |
|
85 | } | |
86 | m_data.append(points); |
|
86 | m_data.append(points); | |
87 | } |
|
87 | } | |
88 | } |
|
88 | } | |
89 |
|
89 | |||
90 | void DataSource::setAntialiasing(bool enabled) |
|
90 | void DataSource::setAntialiasing(bool enabled) | |
91 | { |
|
91 | { | |
92 | m_appViewer->setRenderHint(QPainter::Antialiasing, enabled); |
|
92 | m_appViewer->setRenderHint(QPainter::Antialiasing, enabled); | |
93 | } |
|
93 | } |
@@ -1,89 +1,89 | |||||
1 | /**************************************************************************** |
|
1 | /**************************************************************************** | |
2 | ** |
|
2 | ** | |
3 | ** Copyright (C) 2013 Digia Plc |
|
3 | ** Copyright (C) 2013 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 Enterprise Charts Add-on. |
|
7 | ** This file is part of the Qt Enterprise Charts Add-on. | |
8 | ** |
|
8 | ** | |
9 | ** $QT_BEGIN_LICENSE$ |
|
9 | ** $QT_BEGIN_LICENSE$ | |
10 | ** Licensees holding valid Qt Enterprise licenses may use this file in |
|
10 | ** Licensees holding valid Qt Enterprise licenses may use this file in | |
11 | ** accordance with the Qt Enterprise License Agreement provided with the |
|
11 | ** accordance with the Qt Enterprise 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 "datasource.h" |
|
21 | #include "datasource.h" | |
22 | #include <QXYSeries> |
|
22 | #include <QXYSeries> | |
23 | #include <QAreaSeries> |
|
23 | #include <QAreaSeries> | |
24 | #include <QtQuick/QQuickView> |
|
24 | #include <QtQuick/QQuickView> | |
25 | #include <QtQuick/QQuickItem> |
|
25 | #include <QtQuick/QQuickItem> | |
26 | #include <QDebug> |
|
26 | #include <QDebug> | |
27 |
#include < |
|
27 | #include <qmath.h> | |
28 |
|
28 | |||
29 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
29 | QTCOMMERCIALCHART_USE_NAMESPACE | |
30 |
|
30 | |||
31 | Q_DECLARE_METATYPE(QAbstractSeries *) |
|
31 | Q_DECLARE_METATYPE(QAbstractSeries *) | |
32 |
|
32 | |||
33 | DataSource::DataSource(QQuickView *appViewer, QObject *parent) : |
|
33 | DataSource::DataSource(QQuickView *appViewer, QObject *parent) : | |
34 | QObject(parent), |
|
34 | QObject(parent), | |
35 | m_appViewer(appViewer), |
|
35 | m_appViewer(appViewer), | |
36 | m_index(-1) |
|
36 | m_index(-1) | |
37 | { |
|
37 | { | |
38 | qRegisterMetaType<QAbstractSeries*>(); |
|
38 | qRegisterMetaType<QAbstractSeries*>(); | |
39 |
|
39 | |||
40 | generateData(0, 5, 1024); |
|
40 | generateData(0, 5, 1024); | |
41 | } |
|
41 | } | |
42 |
|
42 | |||
43 | void DataSource::update(QAbstractSeries *series) |
|
43 | void DataSource::update(QAbstractSeries *series) | |
44 | { |
|
44 | { | |
45 | if (series) { |
|
45 | if (series) { | |
46 | QXYSeries *xySeries = static_cast<QXYSeries *>(series); |
|
46 | QXYSeries *xySeries = static_cast<QXYSeries *>(series); | |
47 | m_index++; |
|
47 | m_index++; | |
48 | if (m_index > m_data.count() - 1) |
|
48 | if (m_index > m_data.count() - 1) | |
49 | m_index = 0; |
|
49 | m_index = 0; | |
50 |
|
50 | |||
51 | QList<QPointF> points = m_data.at(m_index); |
|
51 | QList<QPointF> points = m_data.at(m_index); | |
52 | // Use replace instead of clear + append, it's optimized for performance |
|
52 | // Use replace instead of clear + append, it's optimized for performance | |
53 | xySeries->replace(points); |
|
53 | xySeries->replace(points); | |
54 | } |
|
54 | } | |
55 | } |
|
55 | } | |
56 |
|
56 | |||
57 | void DataSource::generateData(int type, int rowCount, int colCount) |
|
57 | void DataSource::generateData(int type, int rowCount, int colCount) | |
58 | { |
|
58 | { | |
59 | // Remove previous data |
|
59 | // Remove previous data | |
60 | foreach (QList<QPointF> row, m_data) |
|
60 | foreach (QList<QPointF> row, m_data) | |
61 | row.clear(); |
|
61 | row.clear(); | |
62 | m_data.clear(); |
|
62 | m_data.clear(); | |
63 |
|
63 | |||
64 | // Append the new data depending on the type |
|
64 | // Append the new data depending on the type | |
65 | for (int i(0); i < rowCount; i++) { |
|
65 | for (int i(0); i < rowCount; i++) { | |
66 | QList<QPointF> points; |
|
66 | QList<QPointF> points; | |
67 | for (int j(0); j < colCount; j++) { |
|
67 | for (int j(0); j < colCount; j++) { | |
68 | qreal x(0); |
|
68 | qreal x(0); | |
69 | qreal y(0); |
|
69 | qreal y(0); | |
70 | switch (type) { |
|
70 | switch (type) { | |
71 | case 0: |
|
71 | case 0: | |
72 | // data with sin + random component |
|
72 | // data with sin + random component | |
73 |
y = |
|
73 | y = qSin(3.14159265358979 / 50 * j) + 0.5 + (qreal) rand() / (qreal) RAND_MAX; | |
74 | x = j; |
|
74 | x = j; | |
75 | break; |
|
75 | break; | |
76 | case 1: |
|
76 | case 1: | |
77 | // linear data |
|
77 | // linear data | |
78 | x = j; |
|
78 | x = j; | |
79 | y = (qreal) i / 10; |
|
79 | y = (qreal) i / 10; | |
80 | break; |
|
80 | break; | |
81 | default: |
|
81 | default: | |
82 | // unknown, do nothing |
|
82 | // unknown, do nothing | |
83 | break; |
|
83 | break; | |
84 | } |
|
84 | } | |
85 | points.append(QPointF(x, y)); |
|
85 | points.append(QPointF(x, y)); | |
86 | } |
|
86 | } | |
87 | m_data.append(points); |
|
87 | m_data.append(points); | |
88 | } |
|
88 | } | |
89 | } |
|
89 | } |
@@ -1,77 +1,79 | |||||
1 | /**************************************************************************** |
|
1 | /**************************************************************************** | |
2 | ** |
|
2 | ** | |
3 | ** Copyright (C) 2013 Digia Plc |
|
3 | ** Copyright (C) 2013 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 Enterprise Charts Add-on. |
|
7 | ** This file is part of the Qt Enterprise Charts Add-on. | |
8 | ** |
|
8 | ** | |
9 | ** $QT_BEGIN_LICENSE$ |
|
9 | ** $QT_BEGIN_LICENSE$ | |
10 | ** Licensees holding valid Qt Enterprise licenses may use this file in |
|
10 | ** Licensees holding valid Qt Enterprise licenses may use this file in | |
11 | ** accordance with the Qt Enterprise License Agreement provided with the |
|
11 | ** accordance with the Qt Enterprise 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 <qmath.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()->createDefaultAxes(); | |
49 | chart()->axisX()->setRange(0, 4.5); |
|
49 | chart()->axisX()->setRange(0, 4.5); | |
50 | chart()->axisY()->setRange(0, 4.5); |
|
50 | chart()->axisY()->setRange(0, 4.5); | |
51 |
|
51 | |||
52 | connect(m_scatter, SIGNAL(clicked(QPointF)), this, SLOT(handleClickedPoint(QPointF))); |
|
52 | connect(m_scatter, SIGNAL(clicked(QPointF)), this, SLOT(handleClickedPoint(QPointF))); | |
53 | } |
|
53 | } | |
54 |
|
54 | |||
55 | ChartView::~ChartView() |
|
55 | ChartView::~ChartView() | |
56 | { |
|
56 | { | |
57 | } |
|
57 | } | |
58 |
|
58 | |||
59 | void ChartView::handleClickedPoint(const QPointF &point) |
|
59 | void ChartView::handleClickedPoint(const QPointF &point) | |
60 | { |
|
60 | { | |
61 | QPointF clickedPoint = point; |
|
61 | QPointF clickedPoint = point; | |
62 | // Find the closest point from series 1 |
|
62 | // Find the closest point from series 1 | |
63 | QPointF closest(INT_MAX, INT_MAX); |
|
63 | QPointF closest(INT_MAX, INT_MAX); | |
64 | qreal distance(INT_MAX); |
|
64 | qreal distance(INT_MAX); | |
65 | foreach (QPointF currentPoint, m_scatter->points()) { |
|
65 | foreach (QPointF currentPoint, m_scatter->points()) { | |
66 |
qreal currentDistance = |
|
66 | qreal currentDistance = qSqrt((currentPoint.x() - clickedPoint.x()) | |
67 |
|
|
67 | * (currentPoint.x() - clickedPoint.x()) | |
|
68 | + (currentPoint.y() - clickedPoint.y()) | |||
|
69 | * (currentPoint.y() - clickedPoint.y())); | |||
68 | if (currentDistance < distance) { |
|
70 | if (currentDistance < distance) { | |
69 | distance = currentDistance; |
|
71 | distance = currentDistance; | |
70 | closest = currentPoint; |
|
72 | closest = currentPoint; | |
71 | } |
|
73 | } | |
72 | } |
|
74 | } | |
73 |
|
75 | |||
74 | // Remove the closes point from series 1 and append it to series 2 |
|
76 | // Remove the closes point from series 1 and append it to series 2 | |
75 | m_scatter->remove(closest); |
|
77 | m_scatter->remove(closest); | |
76 | m_scatter2->append(closest); |
|
78 | m_scatter2->append(closest); | |
77 | } |
|
79 | } |
General Comments 0
You need to be logged in to leave comments.
Login now