##// END OF EJS Templates
Added a new QXYSeries::replace override for performance reasons....
Tero Ahola -
r1783:86f3ac473a35
parent child
Show More
@@ -0,0 +1,55
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "datasource.h"
22 #include <QXYSeries>
23 #include <QDebug>
24
25 QTCOMMERCIALCHART_USE_NAMESPACE
26
27 DataSource::DataSource(QObject *parent) :
28 QObject(parent),
29 m_index(0)
30 {
31 const int rowCount = 5;
32 const int colCount = 1024;
33
34 for (int i(0); i < rowCount; i++) {
35 QList<QPointF> points;
36 for (int j(0); j < colCount; j++) {
37 qreal x = j;
38 qreal y = sin(3.14159265358979 / 50 * x) + 0.5 + (qreal) rand() / (qreal) RAND_MAX;
39 points.append(QPointF(x, y));
40 }
41 m_data.append(points);
42 }
43 }
44
45 void DataSource::update(QAbstractSeries *series)
46 {
47 QXYSeries *xySeries = qobject_cast<QXYSeries *>(series);
48 Q_ASSERT(xySeries);
49
50 QList<QPointF> points = m_data.at(m_index);
51 //xySeries->clear();
52 //xySeries->append(points);
53 xySeries->replace(points);
54 m_index = (m_index + 1) % m_data.count();
55 }
@@ -0,0 +1,45
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #ifndef DATASOURCE_H
22 #define DATASOURCE_H
23
24 #include <QObject>
25 #include <QAbstractSeries>
26
27 QTCOMMERCIALCHART_USE_NAMESPACE
28
29 class DataSource : public QObject
30 {
31 Q_OBJECT
32 public:
33 explicit DataSource(QObject *parent = 0);
34
35 signals:
36
37 public slots:
38 void update(QAbstractSeries *series);
39
40 private:
41 QList<QList<QPointF> > m_data;
42 int m_index;
43 };
44
45 #endif // DATASOURCE_H
@@ -0,0 +1,42
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include <QtGui/QApplication>
22 #include <QDeclarativeEngine>
23 #include <QDeclarativeContext>
24 #include <QGLWidget>
25 #include "qmlapplicationviewer.h"
26 #include "datasource.h"
27
28 Q_DECL_EXPORT int main(int argc, char *argv[])
29 {
30 QScopedPointer<QApplication> app(createApplication(argc, argv));
31 QScopedPointer<QmlApplicationViewer> viewer(QmlApplicationViewer::create());
32
33 DataSource dataSource;
34 viewer->rootContext()->setContextProperty("dataSource", &dataSource);
35
36 viewer->setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
37 viewer->setSource(QUrl("qrc:/qml/qmloscilloscope/main.qml"));
38 viewer->setViewport(new QGLWidget());
39 viewer->showExpanded();
40
41 return app->exec();
42 }
@@ -0,0 +1,72
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 import QtQuick 1.0
22 import QtCommercial.Chart 1.0
23
24 Rectangle {
25 width: 400
26 height: 300
27
28 ChartView {
29 id: chartView
30 anchors.fill: parent
31 title: "Oscilloscope"
32 animationOptions: ChartView.NoAnimation
33
34 ValuesAxis {
35 id: axisY
36 min: -1
37 max: 3
38 }
39
40 ValuesAxis {
41 id: axisX
42 min: 0
43 max: 1000
44 }
45
46 LineSeries {
47 id: lineSeries1
48 name: "signal 1"
49 }
50 LineSeries {
51 id: lineSeries2
52 name: "signal 2"
53 }
54 }
55
56 Timer {
57 interval: 16 // 60 Hz
58 running: true
59 repeat: true
60 onTriggered: {
61 dataSource.update(lineSeries1);
62 dataSource.update(lineSeries2);
63 }
64 }
65
66 Component.onCompleted: {
67 chartView.setAxisX(axisX, lineSeries1);
68 chartView.setAxisY(axisY, lineSeries1);
69 chartView.setAxisX(axisX, lineSeries2);
70 chartView.setAxisY(axisY, lineSeries2);
71 }
72 }
@@ -0,0 +1,200
1 // checksum 0x78c version 0x60010
2 /*
3 This file was generated by the Qt Quick Application wizard of Qt Creator.
4 QmlApplicationViewer is a convenience class containing mobile device specific
5 code such as screen orientation handling. Also QML paths and debugging are
6 handled here.
7 It is recommended not to modify this file, since newer versions of Qt Creator
8 may offer an updated version of it.
9 */
10
11 #include "qmlapplicationviewer.h"
12
13 #include <QtCore/QDir>
14 #include <QtCore/QFileInfo>
15 #include <QtDeclarative/QDeclarativeComponent>
16 #include <QtDeclarative/QDeclarativeEngine>
17 #include <QtDeclarative/QDeclarativeContext>
18 #include <QtGui/QApplication>
19
20 #include <qplatformdefs.h> // MEEGO_EDITION_HARMATTAN
21
22 #ifdef HARMATTAN_BOOSTER
23 #include <MDeclarativeCache>
24 #endif
25
26 #if defined(QMLJSDEBUGGER) && QT_VERSION < 0x040800
27
28 #include <qt_private/qdeclarativedebughelper_p.h>
29
30 #if !defined(NO_JSDEBUGGER)
31 #include <jsdebuggeragent.h>
32 #endif
33 #if !defined(NO_QMLOBSERVER)
34 #include <qdeclarativeviewobserver.h>
35 #endif
36
37 // Enable debugging before any QDeclarativeEngine is created
38 struct QmlJsDebuggingEnabler
39 {
40 QmlJsDebuggingEnabler()
41 {
42 QDeclarativeDebugHelper::enableDebugging();
43 }
44 };
45
46 // Execute code in constructor before first QDeclarativeEngine is instantiated
47 static QmlJsDebuggingEnabler enableDebuggingHelper;
48
49 #endif // QMLJSDEBUGGER
50
51 class QmlApplicationViewerPrivate
52 {
53 QmlApplicationViewerPrivate(QDeclarativeView *view_) : view(view_) {}
54
55 QString mainQmlFile;
56 QDeclarativeView *view;
57 friend class QmlApplicationViewer;
58 QString adjustPath(const QString &path);
59 };
60
61 QString QmlApplicationViewerPrivate::adjustPath(const QString &path)
62 {
63 #ifdef Q_OS_UNIX
64 #ifdef Q_OS_MAC
65 if (!QDir::isAbsolutePath(path))
66 return QCoreApplication::applicationDirPath()
67 + QLatin1String("/../Resources/") + path;
68 #else
69 QString pathInInstallDir;
70 const QString applicationDirPath = QCoreApplication::applicationDirPath();
71 pathInInstallDir = QString::fromAscii("%1/../%2").arg(applicationDirPath, path);
72
73 if (QFileInfo(pathInInstallDir).exists())
74 return pathInInstallDir;
75 #endif
76 #endif
77 return path;
78 }
79
80 QmlApplicationViewer::QmlApplicationViewer(QWidget *parent)
81 : QDeclarativeView(parent)
82 , d(new QmlApplicationViewerPrivate(this))
83 {
84 connect(engine(), SIGNAL(quit()), SLOT(close()));
85 setResizeMode(QDeclarativeView::SizeRootObjectToView);
86 // Qt versions prior to 4.8.0 don't have QML/JS debugging services built in
87 #if defined(QMLJSDEBUGGER) && QT_VERSION < 0x040800
88 #if !defined(NO_JSDEBUGGER)
89 new QmlJSDebugger::JSDebuggerAgent(d->view->engine());
90 #endif
91 #if !defined(NO_QMLOBSERVER)
92 new QmlJSDebugger::QDeclarativeViewObserver(d->view, d->view);
93 #endif
94 #endif
95 }
96
97 QmlApplicationViewer::QmlApplicationViewer(QDeclarativeView *view, QWidget *parent)
98 : QDeclarativeView(parent)
99 , d(new QmlApplicationViewerPrivate(view))
100 {
101 connect(view->engine(), SIGNAL(quit()), view, SLOT(close()));
102 view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
103 // Qt versions prior to 4.8.0 don't have QML/JS debugging services built in
104 #if defined(QMLJSDEBUGGER) && QT_VERSION < 0x040800
105 #if !defined(NO_JSDEBUGGER)
106 new QmlJSDebugger::JSDebuggerAgent(d->view->engine());
107 #endif
108 #if !defined(NO_QMLOBSERVER)
109 new QmlJSDebugger::QDeclarativeViewObserver(d->view, d->view);
110 #endif
111 #endif
112 }
113
114 QmlApplicationViewer::~QmlApplicationViewer()
115 {
116 delete d;
117 }
118
119 QmlApplicationViewer *QmlApplicationViewer::create()
120 {
121 #ifdef HARMATTAN_BOOSTER
122 return new QmlApplicationViewer(MDeclarativeCache::qDeclarativeView(), 0);
123 #else
124 return new QmlApplicationViewer();
125 #endif
126 }
127
128 void QmlApplicationViewer::setMainQmlFile(const QString &file)
129 {
130 d->mainQmlFile = d->adjustPath(file);
131 d->view->setSource(QUrl::fromLocalFile(d->mainQmlFile));
132 }
133
134 void QmlApplicationViewer::addImportPath(const QString &path)
135 {
136 d->view->engine()->addImportPath(d->adjustPath(path));
137 }
138
139 void QmlApplicationViewer::setOrientation(ScreenOrientation orientation)
140 {
141 #if defined(Q_OS_SYMBIAN)
142 // If the version of Qt on the device is < 4.7.2, that attribute won't work
143 if (orientation != ScreenOrientationAuto) {
144 const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
145 if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
146 qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
147 return;
148 }
149 }
150 #endif // Q_OS_SYMBIAN
151
152 Qt::WidgetAttribute attribute;
153 switch (orientation) {
154 #if QT_VERSION < 0x040702
155 // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
156 case ScreenOrientationLockPortrait:
157 attribute = static_cast<Qt::WidgetAttribute>(128);
158 break;
159 case ScreenOrientationLockLandscape:
160 attribute = static_cast<Qt::WidgetAttribute>(129);
161 break;
162 default:
163 case ScreenOrientationAuto:
164 attribute = static_cast<Qt::WidgetAttribute>(130);
165 break;
166 #else // QT_VERSION < 0x040702
167 case ScreenOrientationLockPortrait:
168 attribute = Qt::WA_LockPortraitOrientation;
169 break;
170 case ScreenOrientationLockLandscape:
171 attribute = Qt::WA_LockLandscapeOrientation;
172 break;
173 default:
174 case ScreenOrientationAuto:
175 attribute = Qt::WA_AutoOrientation;
176 break;
177 #endif // QT_VERSION < 0x040702
178 };
179 setAttribute(attribute, true);
180 }
181
182 void QmlApplicationViewer::showExpanded()
183 {
184 #if defined(Q_OS_SYMBIAN) || defined(MEEGO_EDITION_HARMATTAN) || defined(Q_WS_SIMULATOR)
185 d->view->showFullScreen();
186 #elif defined(Q_WS_MAEMO_5)
187 d->view->showMaximized();
188 #else
189 d->view->show();
190 #endif
191 }
192
193 QApplication *createApplication(int &argc, char **argv)
194 {
195 #ifdef HARMATTAN_BOOSTER
196 return MDeclarativeCache::qApplication(argc, argv);
197 #else
198 return new QApplication(argc, argv);
199 #endif
200 }
@@ -0,0 +1,47
1 // checksum 0x82ed version 0x60010
2 /*
3 This file was generated by the Qt Quick Application wizard of Qt Creator.
4 QmlApplicationViewer is a convenience class containing mobile device specific
5 code such as screen orientation handling. Also QML paths and debugging are
6 handled here.
7 It is recommended not to modify this file, since newer versions of Qt Creator
8 may offer an updated version of it.
9 */
10
11 #ifndef QMLAPPLICATIONVIEWER_H
12 #define QMLAPPLICATIONVIEWER_H
13
14 #include <QtDeclarative/QDeclarativeView>
15
16 class QmlApplicationViewer : public QDeclarativeView
17 {
18 Q_OBJECT
19
20 public:
21 enum ScreenOrientation {
22 ScreenOrientationLockPortrait,
23 ScreenOrientationLockLandscape,
24 ScreenOrientationAuto
25 };
26
27 explicit QmlApplicationViewer(QWidget *parent = 0);
28 virtual ~QmlApplicationViewer();
29
30 static QmlApplicationViewer *create();
31
32 void setMainQmlFile(const QString &file);
33 void addImportPath(const QString &path);
34
35 // Note that this will only have an effect on Symbian and Fremantle.
36 void setOrientation(ScreenOrientation orientation);
37
38 void showExpanded();
39
40 private:
41 explicit QmlApplicationViewer(QDeclarativeView *view, QWidget *parent);
42 class QmlApplicationViewerPrivate *d;
43 };
44
45 QApplication *createApplication(int &argc, char **argv);
46
47 #endif // QMLAPPLICATIONVIEWER_H
@@ -0,0 +1,13
1 QT += declarative
2
3 SOURCES += $$PWD/qmlapplicationviewer.cpp
4 HEADERS += $$PWD/qmlapplicationviewer.h
5 INCLUDEPATH += $$PWD
6
7 # Include JS debugger library if QMLJSDEBUGGER_PATH is set
8 !isEmpty(QMLJSDEBUGGER_PATH) {
9 include($$QMLJSDEBUGGER_PATH/qmljsdebugger-lib.pri)
10 } else {
11 DEFINES -= QMLJSDEBUGGER
12 }
13
@@ -0,0 +1,13
1 !include( ../demos.pri ) {
2 error( "Couldn't find the demos.pri file!" )
3 }
4
5 QT += opengl
6 RESOURCES += resources.qrc
7 SOURCES += main.cpp \
8 datasource.cpp
9
10 include(qmlapplicationviewer/qmlapplicationviewer.pri)
11
12 HEADERS += \
13 datasource.h
@@ -0,0 +1,5
1 <RCC>
2 <qresource prefix="/">
3 <file>qml/qmloscilloscope/main.qml</file>
4 </qresource>
5 </RCC>
@@ -1,15 +1,16
1 CURRENTLY_BUILDING_COMPONENTS = "demos"
1 CURRENTLY_BUILDING_COMPONENTS = "demos"
2 !include( ../config.pri ) {
2 !include( ../config.pri ) {
3 error( "Couldn't find the config.pri file!" )
3 error( "Couldn't find the config.pri file!" )
4 }
4 }
5
5
6 TEMPLATE = subdirs
6 TEMPLATE = subdirs
7 SUBDIRS += chartthemes \
7 SUBDIRS += chartthemes \
8 piechartcustomization \
8 piechartcustomization \
9 dynamicspline \
9 dynamicspline \
10 qmlchart \
10 qmlchart \
11 qmlweather \
11 qmlweather \
12 qmlf1legends \
12 qmlf1legends \
13 qmlcustomizations \
13 qmlcustomizations \
14 qmlcustommodel \
14 qmlcustommodel \
15 chartviewer No newline at end of file
15 qmloscilloscope \
16 chartviewer
@@ -1,233 +1,234
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 "qsplineseries.h"
21 #include "qsplineseries.h"
22 #include "qsplineseries_p.h"
22 #include "qsplineseries_p.h"
23 #include "splinechartitem_p.h"
23 #include "splinechartitem_p.h"
24 #include "chartdataset_p.h"
24 #include "chartdataset_p.h"
25 #include "charttheme_p.h"
25 #include "charttheme_p.h"
26 #include "splineanimation_p.h"
26 #include "splineanimation_p.h"
27
27
28 /*!
28 /*!
29 \class QSplineSeries
29 \class QSplineSeries
30 \brief Series type used to store data needed to draw a spline.
30 \brief Series type used to store data needed to draw a spline.
31
31
32 QSplineSeries stores the data points along with the segment control points needed by QPainterPath to draw spline
32 QSplineSeries stores the data points along with the segment control points needed by QPainterPath to draw spline
33 Control points are automatically calculated when data changes. The algorithm computes the points so that the normal spline can be drawn.
33 Control points are automatically calculated when data changes. The algorithm computes the points so that the normal spline can be drawn.
34
34
35 \image examples_splinechart.png
35 \image examples_splinechart.png
36
36
37 Creating basic spline chart is simple:
37 Creating basic spline chart is simple:
38 \code
38 \code
39 QSplineSeries* series = new QSplineSeries();
39 QSplineSeries* series = new QSplineSeries();
40 series->append(0, 6);
40 series->append(0, 6);
41 series->append(2, 4);
41 series->append(2, 4);
42 ...
42 ...
43 chart->addSeries(series);
43 chart->addSeries(series);
44 \endcode
44 \endcode
45 */
45 */
46
46
47 /*!
47 /*!
48 \qmlclass SplineSeries QSplineSeries
48 \qmlclass SplineSeries QSplineSeries
49 \inherits XYSeries
49 \inherits XYSeries
50
50
51 The following QML shows how to create a simple spline chart:
51 The following QML shows how to create a simple spline chart:
52 \snippet ../demos/qmlchart/qml/qmlchart/View3.qml 1
52 \snippet ../demos/qmlchart/qml/qmlchart/View3.qml 1
53 \beginfloatleft
53 \beginfloatleft
54 \image demos_qmlchart3.png
54 \image demos_qmlchart3.png
55 \endfloat
55 \endfloat
56 \clearfloat
56 \clearfloat
57 */
57 */
58
58
59 /*!
59 /*!
60 \fn QSeriesType QSplineSeries::type() const
60 \fn QSeriesType QSplineSeries::type() const
61 Returns the type of the series
61 Returns the type of the series
62 */
62 */
63
63
64 QTCOMMERCIALCHART_BEGIN_NAMESPACE
64 QTCOMMERCIALCHART_BEGIN_NAMESPACE
65
65
66 /*!
66 /*!
67 Constructs empty series object which is a child of \a parent.
67 Constructs empty series object which is a child of \a parent.
68 When series object is added to QChartView or QChart instance then the ownerships is transferred.
68 When series object is added to QChartView or QChart instance then the ownerships is transferred.
69 */
69 */
70
70
71 QSplineSeries::QSplineSeries(QObject *parent) :
71 QSplineSeries::QSplineSeries(QObject *parent) :
72 QLineSeries(*new QSplineSeriesPrivate(this),parent)
72 QLineSeries(*new QSplineSeriesPrivate(this),parent)
73 {
73 {
74 Q_D(QSplineSeries);
74 Q_D(QSplineSeries);
75 QObject::connect(this,SIGNAL(pointAdded(int)), d, SLOT(updateControlPoints()));
75 QObject::connect(this,SIGNAL(pointAdded(int)), d, SLOT(updateControlPoints()));
76 QObject::connect(this,SIGNAL(pointRemoved(int)), d, SLOT(updateControlPoints()));
76 QObject::connect(this,SIGNAL(pointRemoved(int)), d, SLOT(updateControlPoints()));
77 QObject::connect(this,SIGNAL(pointReplaced(int)), d, SLOT(updateControlPoints()));
77 QObject::connect(this,SIGNAL(pointReplaced(int)), d, SLOT(updateControlPoints()));
78 QObject::connect(this,SIGNAL(pointsReplaced()), d, SLOT(updateControlPoints()));
78 }
79 }
79
80
80 /*!
81 /*!
81 Destroys the object.
82 Destroys the object.
82 */
83 */
83 QSplineSeries::~QSplineSeries()
84 QSplineSeries::~QSplineSeries()
84 {
85 {
85 Q_D(QSplineSeries);
86 Q_D(QSplineSeries);
86 if(d->m_dataset){
87 if(d->m_dataset){
87 d->m_dataset->removeSeries(this);
88 d->m_dataset->removeSeries(this);
88 }
89 }
89 }
90 }
90
91
91 QAbstractSeries::SeriesType QSplineSeries::type() const
92 QAbstractSeries::SeriesType QSplineSeries::type() const
92 {
93 {
93 return QAbstractSeries::SeriesTypeSpline;
94 return QAbstractSeries::SeriesTypeSpline;
94 }
95 }
95
96
96 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
97 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
97
98
98 QSplineSeriesPrivate::QSplineSeriesPrivate(QSplineSeries* q):QLineSeriesPrivate(q)
99 QSplineSeriesPrivate::QSplineSeriesPrivate(QSplineSeries* q):QLineSeriesPrivate(q)
99 {
100 {
100 }
101 }
101
102
102 /*!
103 /*!
103 Calculates control points which are needed by QPainterPath.cubicTo function to draw the cubic Bezier cureve between two points.
104 Calculates control points which are needed by QPainterPath.cubicTo function to draw the cubic Bezier cureve between two points.
104 */
105 */
105 void QSplineSeriesPrivate::calculateControlPoints()
106 void QSplineSeriesPrivate::calculateControlPoints()
106 {
107 {
107 Q_Q(QSplineSeries);
108 Q_Q(QSplineSeries);
108
109
109 const QList<QPointF>& points = q->points();
110 const QList<QPointF>& points = q->points();
110
111
111 int n = points.count() - 1;
112 int n = points.count() - 1;
112
113
113 if (n == 1)
114 if (n == 1)
114 {
115 {
115 //for n==1
116 //for n==1
116 m_controlPoints[0].setX((2 * points[0].x() + points[1].x()) / 3);
117 m_controlPoints[0].setX((2 * points[0].x() + points[1].x()) / 3);
117 m_controlPoints[0].setY((2 * points[0].y() + points[1].y()) / 3);
118 m_controlPoints[0].setY((2 * points[0].y() + points[1].y()) / 3);
118 m_controlPoints[1].setX(2 * m_controlPoints[0].x() - points[0].x());
119 m_controlPoints[1].setX(2 * m_controlPoints[0].x() - points[0].x());
119 m_controlPoints[1].setY(2 * m_controlPoints[0].y() - points[0].y());
120 m_controlPoints[1].setY(2 * m_controlPoints[0].y() - points[0].y());
120 return;
121 return;
121 }
122 }
122
123
123 // Calculate first Bezier control points
124 // Calculate first Bezier control points
124 // Set of equations for P0 to Pn points.
125 // Set of equations for P0 to Pn points.
125 //
126 //
126 // | 2 1 0 0 ... 0 0 0 ... 0 0 0 | | P1_1 | | P0 + 2 * P1 |
127 // | 2 1 0 0 ... 0 0 0 ... 0 0 0 | | P1_1 | | P0 + 2 * P1 |
127 // | 1 4 1 0 ... 0 0 0 ... 0 0 0 | | P1_2 | | 4 * P1 + 2 * P2 |
128 // | 1 4 1 0 ... 0 0 0 ... 0 0 0 | | P1_2 | | 4 * P1 + 2 * P2 |
128 // | 0 1 4 1 ... 0 0 0 ... 0 0 0 | | P1_3 | | 4 * P2 + 2 * P3 |
129 // | 0 1 4 1 ... 0 0 0 ... 0 0 0 | | P1_3 | | 4 * P2 + 2 * P3 |
129 // | . . . . . . . . . . . . | | ... | | ... |
130 // | . . . . . . . . . . . . | | ... | | ... |
130 // | 0 0 0 0 ... 1 4 1 ... 0 0 0 | * | P1_i | = | 4 * P(i-1) + 2 * Pi |
131 // | 0 0 0 0 ... 1 4 1 ... 0 0 0 | * | P1_i | = | 4 * P(i-1) + 2 * Pi |
131 // | . . . . . . . . . . . . | | ... | | ... |
132 // | . . . . . . . . . . . . | | ... | | ... |
132 // | 0 0 0 0 0 0 0 0 ... 1 4 1 | | P1_(n-1)| | 4 * P(n-2) + 2 * P(n-1) |
133 // | 0 0 0 0 0 0 0 0 ... 1 4 1 | | P1_(n-1)| | 4 * P(n-2) + 2 * P(n-1) |
133 // | 0 0 0 0 0 0 0 0 ... 0 2 7 | | P1_n | | 8 * P(n-1) + Pn |
134 // | 0 0 0 0 0 0 0 0 ... 0 2 7 | | P1_n | | 8 * P(n-1) + Pn |
134 //
135 //
135 QVector<qreal> vector;
136 QVector<qreal> vector;
136 vector.resize(n);
137 vector.resize(n);
137
138
138 vector[0] = points[0].x() + 2 * points[1].x();
139 vector[0] = points[0].x() + 2 * points[1].x();
139
140
140
141
141 for (int i = 1; i < n - 1; ++i){
142 for (int i = 1; i < n - 1; ++i){
142 vector[i] = 4 * points[i].x() + 2 * points[i + 1].x();
143 vector[i] = 4 * points[i].x() + 2 * points[i + 1].x();
143 }
144 }
144
145
145 vector[n - 1] = (8 * points[n-1].x() + points[n].x()) / 2.0;
146 vector[n - 1] = (8 * points[n-1].x() + points[n].x()) / 2.0;
146
147
147 QVector<qreal> xControl = firstControlPoints(vector);
148 QVector<qreal> xControl = firstControlPoints(vector);
148
149
149 vector[0] = points[0].y() + 2 * points[1].y();
150 vector[0] = points[0].y() + 2 * points[1].y();
150
151
151 for (int i = 1; i < n - 1; ++i) {
152 for (int i = 1; i < n - 1; ++i) {
152 vector[i] = 4 * points[i].y() + 2 * points[i + 1].y();
153 vector[i] = 4 * points[i].y() + 2 * points[i + 1].y();
153 }
154 }
154
155
155 vector[n - 1] = (8 * points[n-1].y() + points[n].y()) / 2.0;
156 vector[n - 1] = (8 * points[n-1].y() + points[n].y()) / 2.0;
156
157
157 QVector<qreal> yControl = firstControlPoints(vector);
158 QVector<qreal> yControl = firstControlPoints(vector);
158
159
159 for (int i = 0,j =0; i < n; ++i, ++j) {
160 for (int i = 0,j =0; i < n; ++i, ++j) {
160
161
161 m_controlPoints[j].setX(xControl[i]);
162 m_controlPoints[j].setX(xControl[i]);
162 m_controlPoints[j].setY(yControl[i]);
163 m_controlPoints[j].setY(yControl[i]);
163
164
164 j++;
165 j++;
165
166
166 if (i < n - 1){
167 if (i < n - 1){
167 m_controlPoints[j].setX(2 * points[i+1].x() - xControl[i + 1]);
168 m_controlPoints[j].setX(2 * points[i+1].x() - xControl[i + 1]);
168 m_controlPoints[j].setY(2 * points[i+1].y() - yControl[i + 1]);
169 m_controlPoints[j].setY(2 * points[i+1].y() - yControl[i + 1]);
169 }else{
170 }else{
170 m_controlPoints[j].setX((points[n].x() + xControl[n - 1]) / 2);
171 m_controlPoints[j].setX((points[n].x() + xControl[n - 1]) / 2);
171 m_controlPoints[j].setY((points[n].y() + yControl[n - 1]) / 2);
172 m_controlPoints[j].setY((points[n].y() + yControl[n - 1]) / 2);
172 }
173 }
173 }
174 }
174 }
175 }
175
176
176 QVector<qreal> QSplineSeriesPrivate::firstControlPoints(const QVector<qreal>& vector)
177 QVector<qreal> QSplineSeriesPrivate::firstControlPoints(const QVector<qreal>& vector)
177 {
178 {
178 QVector<qreal> result;
179 QVector<qreal> result;
179
180
180 int count = vector.count();
181 int count = vector.count();
181 result.resize(count);
182 result.resize(count);
182 result[0] = vector[0] / 2.0;
183 result[0] = vector[0] / 2.0;
183
184
184 QVector<qreal> temp;
185 QVector<qreal> temp;
185 temp.resize(count);
186 temp.resize(count);
186 temp[0] = 0;
187 temp[0] = 0;
187
188
188 qreal b = 2.0;
189 qreal b = 2.0;
189
190
190 for (int i = 1; i < count; i++) {
191 for (int i = 1; i < count; i++) {
191 temp[i] = 1 / b;
192 temp[i] = 1 / b;
192 b = (i < count - 1 ? 4.0 : 3.5) - temp[i];
193 b = (i < count - 1 ? 4.0 : 3.5) - temp[i];
193 result[i]=(vector[i] - result[i - 1]) / b;
194 result[i]=(vector[i] - result[i - 1]) / b;
194 }
195 }
195 for (int i = 1; i < count; i++)
196 for (int i = 1; i < count; i++)
196 result[count - i - 1] -= temp[count - i] * result[count - i];
197 result[count - i - 1] -= temp[count - i] * result[count - i];
197
198
198 return result;
199 return result;
199 }
200 }
200
201
201 QPointF QSplineSeriesPrivate::controlPoint(int index) const
202 QPointF QSplineSeriesPrivate::controlPoint(int index) const
202 {
203 {
203 return m_controlPoints[index];
204 return m_controlPoints[index];
204 }
205 }
205
206
206 /*!
207 /*!
207 Updates the control points, besed on currently avaiable knots.
208 Updates the control points, besed on currently avaiable knots.
208 */
209 */
209 void QSplineSeriesPrivate::updateControlPoints()
210 void QSplineSeriesPrivate::updateControlPoints()
210 {
211 {
211 Q_Q(QSplineSeries);
212 Q_Q(QSplineSeries);
212 if (q->count() > 1) {
213 if (q->count() > 1) {
213 m_controlPoints.resize(2*q->count()-2);
214 m_controlPoints.resize(2*q->count()-2);
214 calculateControlPoints();
215 calculateControlPoints();
215 }
216 }
216 }
217 }
217
218
218 ChartElement* QSplineSeriesPrivate::createGraphics(ChartPresenter* presenter)
219 ChartElement* QSplineSeriesPrivate::createGraphics(ChartPresenter* presenter)
219 {
220 {
220 Q_Q(QSplineSeries);
221 Q_Q(QSplineSeries);
221 SplineChartItem* spline = new SplineChartItem(q,presenter);
222 SplineChartItem* spline = new SplineChartItem(q,presenter);
222 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
223 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
223 spline->setAnimation(new SplineAnimation(spline));
224 spline->setAnimation(new SplineAnimation(spline));
224 }
225 }
225
226
226 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
227 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
227 return spline;
228 return spline;
228 }
229 }
229
230
230 #include "moc_qsplineseries.cpp"
231 #include "moc_qsplineseries.cpp"
231 #include "moc_qsplineseries_p.cpp"
232 #include "moc_qsplineseries_p.cpp"
232
233
233 QTCOMMERCIALCHART_END_NAMESPACE
234 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,448 +1,469
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 "qxyseries.h"
21 #include "qxyseries.h"
22 #include "qxyseries_p.h"
22 #include "qxyseries_p.h"
23 #include "domain_p.h"
23 #include "domain_p.h"
24 #include "legendmarker_p.h"
24 #include "legendmarker_p.h"
25 #include "qvaluesaxis.h"
25 #include "qvaluesaxis.h"
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 /*!
29 /*!
30 \class QXYSeries
30 \class QXYSeries
31 \brief The QXYSeries class is a base class for line, spline and scatter series.
31 \brief The QXYSeries class is a base class for line, spline and scatter series.
32 */
32 */
33 /*!
33 /*!
34 \qmlclass XYSeries
34 \qmlclass XYSeries
35 \inherits AbstractSeries
35 \inherits AbstractSeries
36 The XYSeries class is a base class for line, spline and scatter series.
36 The XYSeries class is a base class for line, spline and scatter series.
37
37
38 The class cannot be instantiated directly.
38 The class cannot be instantiated directly.
39 */
39 */
40
40
41 /*!
41 /*!
42 \property QXYSeries::pointsVisible
42 \property QXYSeries::pointsVisible
43 Controls if the data points are visible and should be drawn.
43 Controls if the data points are visible and should be drawn.
44 */
44 */
45 /*!
45 /*!
46 \qmlproperty bool XYSeries::pointsVisible
46 \qmlproperty bool XYSeries::pointsVisible
47 Controls if the data points are visible and should be drawn.
47 Controls if the data points are visible and should be drawn.
48 */
48 */
49
49
50 /*!
50 /*!
51 \fn QPen QXYSeries::pen() const
51 \fn QPen QXYSeries::pen() const
52 \brief Returns pen used to draw points for series.
52 \brief Returns pen used to draw points for series.
53 \sa setPen()
53 \sa setPen()
54 */
54 */
55
55
56 /*!
56 /*!
57 \fn QBrush QXYSeries::brush() const
57 \fn QBrush QXYSeries::brush() const
58 \brief Returns brush used to draw points for series.
58 \brief Returns brush used to draw points for series.
59 \sa setBrush()
59 \sa setBrush()
60 */
60 */
61
61
62 /*!
62 /*!
63 \property QXYSeries::color
63 \property QXYSeries::color
64 The color of the series. This is line (pen) color in case of QLineSeries or QSplineSeries and
64 The color of the series. This is line (pen) color in case of QLineSeries or QSplineSeries and
65 fill (brush) color in case of QScatterSeries or QAreaSeries.
65 fill (brush) color in case of QScatterSeries or QAreaSeries.
66 \sa QXYSeries::pen(), QXYSeries::brush()
66 \sa QXYSeries::pen(), QXYSeries::brush()
67 */
67 */
68 /*!
68 /*!
69 \qmlproperty color XYSeries::color
69 \qmlproperty color XYSeries::color
70 The color of the series. This is line (pen) color in case of LineSeries or SplineSeries and
70 The color of the series. This is line (pen) color in case of LineSeries or SplineSeries and
71 fill (brush) color in case of ScatterSeries or AreaSeries.
71 fill (brush) color in case of ScatterSeries or AreaSeries.
72 */
72 */
73
73
74 /*!
74 /*!
75 \fn void QXYSeries::clicked(const QPointF& point)
75 \fn void QXYSeries::clicked(const QPointF& point)
76 \brief Signal is emitted when user clicks the \a point on chart.
76 \brief Signal is emitted when user clicks the \a point on chart.
77 */
77 */
78 /*!
78 /*!
79 \qmlsignal XYSeries::onClicked(QPointF point)
79 \qmlsignal XYSeries::onClicked(QPointF point)
80 Signal is emitted when user clicks the \a point on chart. For example:
80 Signal is emitted when user clicks the \a point on chart. For example:
81 \code
81 \code
82 LineSeries {
82 LineSeries {
83 XYPoint { x: 0; y: 0 }
83 XYPoint { x: 0; y: 0 }
84 XYPoint { x: 1.1; y: 2.1 }
84 XYPoint { x: 1.1; y: 2.1 }
85 onClicked: console.log("onClicked: " + point.x + ", " + point.y);
85 onClicked: console.log("onClicked: " + point.x + ", " + point.y);
86 }
86 }
87 \endcode
87 \endcode
88 */
88 */
89
89
90 /*!
90 /*!
91 \fn void QXYSeries::pointReplaced(int index)
91 \fn void QXYSeries::pointReplaced(int index)
92 Signal is emitted when a point has been replaced at \a index.
92 Signal is emitted when a point has been replaced at \a index.
93 \sa replace()
93 \sa replace()
94 */
94 */
95 /*!
95 /*!
96 \qmlsignal XYSeries::onPointReplaced(int index)
96 \qmlsignal XYSeries::onPointReplaced(int index)
97 Signal is emitted when a point has been replaced at \a index.
97 Signal is emitted when a point has been replaced at \a index.
98 */
98 */
99
99
100 /*!
100 /*!
101 \fn void QXYSeries::pointsReplaced()
102 Signal is emitted when all points have been replaced with another points.
103 \sa replace()
104 */
105 /*!
106 \qmlsignal XYSeries::onPointsReplaced()
107 */
108
109 /*!
101 \fn void QXYSeries::pointAdded(int index)
110 \fn void QXYSeries::pointAdded(int index)
102 Signal is emitted when a point has been added at \a index.
111 Signal is emitted when a point has been added at \a index.
103 \sa append(), insert()
112 \sa append(), insert()
104 */
113 */
105 /*!
114 /*!
106 \qmlsignal XYSeries::onPointAdded(int index)
115 \qmlsignal XYSeries::onPointAdded(int index)
107 Signal is emitted when a point has been added at \a index.
116 Signal is emitted when a point has been added at \a index.
108 */
117 */
109
118
110 /*!
119 /*!
111 \fn void QXYSeries::pointRemoved(int index)
120 \fn void QXYSeries::pointRemoved(int index)
112 Signal is emitted when a point has been removed from \a index.
121 Signal is emitted when a point has been removed from \a index.
113 \sa remove()
122 \sa remove()
114 */
123 */
115 /*!
124 /*!
116 \qmlsignal XYSeries::onPointRemoved(int index)
125 \qmlsignal XYSeries::onPointRemoved(int index)
117 Signal is emitted when a point has been removed from \a index.
126 Signal is emitted when a point has been removed from \a index.
118 */
127 */
119
128
120 /*!
129 /*!
121 \fn void QXYSeries::colorChanged(QColor color)
130 \fn void QXYSeries::colorChanged(QColor color)
122 \brief Signal is emitted when the line (pen) color has changed to \a color.
131 \brief Signal is emitted when the line (pen) color has changed to \a color.
123 */
132 */
124 /*!
133 /*!
125 \qmlsignal XYSeries::onColorChanged(color color)
134 \qmlsignal XYSeries::onColorChanged(color color)
126 Signal is emitted when the line (pen) color has changed to \a color.
135 Signal is emitted when the line (pen) color has changed to \a color.
127 */
136 */
128
137
129 /*!
138 /*!
130 \fn void QXYSeriesPrivate::updated()
139 \fn void QXYSeriesPrivate::updated()
131 \brief \internal
140 \brief \internal
132 */
141 */
133
142
134 /*!
143 /*!
135 \qmlmethod XYSeries::append(real x, real y)
144 \qmlmethod XYSeries::append(real x, real y)
136 Append point (\a x, \a y) to the series
145 Append point (\a x, \a y) to the series
137 */
146 */
138
147
139 /*!
148 /*!
140 \qmlmethod XYSeries::replace(real oldX, real oldY, real newX, real newY)
149 \qmlmethod XYSeries::replace(real oldX, real oldY, real newX, real newY)
141 Replaces point (\a oldX, \a oldY) with point (\a newX, \a newY). Does nothing, if point (oldX, oldY) does not
150 Replaces point (\a oldX, \a oldY) with point (\a newX, \a newY). Does nothing, if point (oldX, oldY) does not
142 exist.
151 exist.
143 */
152 */
144
153
145 /*!
154 /*!
146 \qmlmethod XYSeries::remove(real x, real y)
155 \qmlmethod XYSeries::remove(real x, real y)
147 Removes point (\a x, \a y) from the series. Does nothing, if point (x, y) does not exist.
156 Removes point (\a x, \a y) from the series. Does nothing, if point (x, y) does not exist.
148 */
157 */
149
158
150 /*!
159 /*!
151 \qmlmethod XYSeries::insert(int index, real x, real y)
160 \qmlmethod XYSeries::insert(int index, real x, real y)
152 Inserts point (\a x, \a y) to the \a index. If index is 0 or smaller than 0 the point is prepended to the list of
161 Inserts point (\a x, \a y) to the \a index. If index is 0 or smaller than 0 the point is prepended to the list of
153 points. If index is the same as or bigger than count, the point is appended to the list of points.
162 points. If index is the same as or bigger than count, the point is appended to the list of points.
154 */
163 */
155
164
156 /*!
165 /*!
157 \qmlmethod QPointF XYSeries::at(int index)
166 \qmlmethod QPointF XYSeries::at(int index)
158 Returns point at \a index. Returns (0, 0) if the index is not valid.
167 Returns point at \a index. Returns (0, 0) if the index is not valid.
159 */
168 */
160
169
161 /*!
170 /*!
162 \internal
171 \internal
163
172
164 Constructs empty series object which is a child of \a parent.
173 Constructs empty series object which is a child of \a parent.
165 When series object is added to QChartView or QChart instance ownerships is transferred.
174 When series object is added to QChartView or QChart instance ownerships is transferred.
166 */
175 */
167 QXYSeries::QXYSeries(QXYSeriesPrivate &d,QObject *parent) : QAbstractSeries(d, parent)
176 QXYSeries::QXYSeries(QXYSeriesPrivate &d,QObject *parent) : QAbstractSeries(d, parent)
168 {
177 {
169 }
178 }
170
179
171 /*!
180 /*!
172 Destroys the object. Series added to QChartView or QChart instances are owned by those,
181 Destroys the object. Series added to QChartView or QChart instances are owned by those,
173 and are deleted when mentioned object are destroyed.
182 and are deleted when mentioned object are destroyed.
174 */
183 */
175 QXYSeries::~QXYSeries()
184 QXYSeries::~QXYSeries()
176 {
185 {
177 }
186 }
178
187
179 /*!
188 /*!
180 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
189 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
181 */
190 */
182 void QXYSeries::append(qreal x,qreal y)
191 void QXYSeries::append(qreal x,qreal y)
183 {
192 {
184 append(QPointF(x,y));
193 append(QPointF(x,y));
185 }
194 }
186
195
187 /*!
196 /*!
188 This is an overloaded function.
197 This is an overloaded function.
189 Adds data \a point to the series. Points are connected with lines on the chart.
198 Adds data \a point to the series. Points are connected with lines on the chart.
190 */
199 */
191 void QXYSeries::append(const QPointF &point)
200 void QXYSeries::append(const QPointF &point)
192 {
201 {
193 Q_D(QXYSeries);
202 Q_D(QXYSeries);
194 d->m_points<<point;
203 d->m_points<<point;
195 // emit d->pointAdded(d->m_points.count()-1);
196 emit pointAdded(d->m_points.count()-1);
204 emit pointAdded(d->m_points.count()-1);
197 }
205 }
198
206
199 /*!
207 /*!
200 This is an overloaded function.
208 This is an overloaded function.
201 Adds list of data \a points to the series. Points are connected with lines on the chart.
209 Adds list of data \a points to the series. Points are connected with lines on the chart.
202 */
210 */
203 void QXYSeries::append(const QList<QPointF> &points)
211 void QXYSeries::append(const QList<QPointF> &points)
204 {
212 {
205 foreach(const QPointF& point , points) {
213 foreach(const QPointF& point , points) {
206 append(point);
214 append(point);
207 }
215 }
208 }
216 }
209
217
210 /*!
218 /*!
211 Replaces data point \a oldX \a oldY with data point \a newX \a newY.
219 Replaces data point \a oldX \a oldY with data point \a newX \a newY.
220 \sa QXYSeries::pointReplaced()
212 */
221 */
213 void QXYSeries::replace(qreal oldX,qreal oldY,qreal newX,qreal newY)
222 void QXYSeries::replace(qreal oldX,qreal oldY,qreal newX,qreal newY)
214 {
223 {
215 replace(QPointF(oldX,oldY),QPointF(newX,newY));
224 replace(QPointF(oldX,oldY),QPointF(newX,newY));
216 }
225 }
217
226
218 /*!
227 /*!
219 Replaces \a oldPoint with \a newPoint.
228 Replaces \a oldPoint with \a newPoint.
229 \sa QXYSeries::pointReplaced()
220 */
230 */
221 void QXYSeries::replace(const QPointF &oldPoint,const QPointF &newPoint)
231 void QXYSeries::replace(const QPointF &oldPoint,const QPointF &newPoint)
222 {
232 {
223 Q_D(QXYSeries);
233 Q_D(QXYSeries);
224 int index = d->m_points.indexOf(oldPoint);
234 int index = d->m_points.indexOf(oldPoint);
225 if(index==-1) return;
235 if(index==-1) return;
226 d->m_points[index] = newPoint;
236 d->m_points[index] = newPoint;
227 emit pointReplaced(index);
237 emit pointReplaced(index);
228 }
238 }
229
239
230 /*!
240 /*!
241 Replaces the current points with \a points. This is faster than replacing data points one by one,
242 or first clearing all data, and then appending the new data. Emits QXYSeries::pointsReplaced()
243 when the points have been replaced.
244 \sa QXYSeries::pointsReplaced()
245 */
246 void QXYSeries::replace(QList<QPointF> points)
247 {
248 Q_D(QXYSeries);
249 d->m_points = points.toVector();
250 emit pointsReplaced();
251 }
252
253 /*!
231 Removes current \a x and \a y value.
254 Removes current \a x and \a y value.
232 */
255 */
233 void QXYSeries::remove(qreal x,qreal y)
256 void QXYSeries::remove(qreal x,qreal y)
234 {
257 {
235 remove(QPointF(x,y));
258 remove(QPointF(x,y));
236 }
259 }
237
260
238 /*!
261 /*!
239 Removes current \a point x value.
262 Removes current \a point x value.
240
263
241 Note: point y value is ignored.
264 Note: point y value is ignored.
242 */
265 */
243 void QXYSeries::remove(const QPointF &point)
266 void QXYSeries::remove(const QPointF &point)
244 {
267 {
245 Q_D(QXYSeries);
268 Q_D(QXYSeries);
246 int index = d->m_points.indexOf(point);
269 int index = d->m_points.indexOf(point);
247 if(index==-1) return;
270 if(index==-1) return;
248 d->m_points.remove(index);
271 d->m_points.remove(index);
249 // emit d->pointRemoved(index);
250 emit pointRemoved(index);
272 emit pointRemoved(index);
251 }
273 }
252
274
253 /*!
275 /*!
254 Inserts a \a point in the series at \a index position.
276 Inserts a \a point in the series at \a index position.
255 */
277 */
256 void QXYSeries::insert(int index, const QPointF &point)
278 void QXYSeries::insert(int index, const QPointF &point)
257 {
279 {
258 Q_D(QXYSeries);
280 Q_D(QXYSeries);
259 d->m_points.insert(index, point);
281 d->m_points.insert(index, point);
260 // emit d->pointAdded(index);
261 emit pointAdded(index);
282 emit pointAdded(index);
262 }
283 }
263
284
264 /*!
285 /*!
265 Removes all points from the series.
286 Removes all points from the series.
266 */
287 */
267 void QXYSeries::clear()
288 void QXYSeries::clear()
268 {
289 {
269 Q_D(QXYSeries);
290 Q_D(QXYSeries);
270 for (int i = d->m_points.size() - 1; i >= 0; i--)
291 for (int i = d->m_points.size() - 1; i >= 0; i--)
271 remove(d->m_points.at(i));
292 remove(d->m_points.at(i));
272 }
293 }
273
294
274 /*!
295 /*!
275 Returns list of points in the series.
296 Returns list of points in the series.
276 */
297 */
277 QList<QPointF> QXYSeries::points() const
298 QList<QPointF> QXYSeries::points() const
278 {
299 {
279 Q_D(const QXYSeries);
300 Q_D(const QXYSeries);
280 return d->m_points.toList();
301 return d->m_points.toList();
281 }
302 }
282
303
283 /*!
304 /*!
284 Returns number of data points within series.
305 Returns number of data points within series.
285 */
306 */
286 int QXYSeries::count() const
307 int QXYSeries::count() const
287 {
308 {
288 Q_D(const QXYSeries);
309 Q_D(const QXYSeries);
289 return d->m_points.count();
310 return d->m_points.count();
290 }
311 }
291
312
292
313
293 /*!
314 /*!
294 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
315 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
295 pen from chart theme is used.
316 pen from chart theme is used.
296 \sa QChart::setTheme()
317 \sa QChart::setTheme()
297 */
318 */
298 void QXYSeries::setPen(const QPen &pen)
319 void QXYSeries::setPen(const QPen &pen)
299 {
320 {
300 Q_D(QXYSeries);
321 Q_D(QXYSeries);
301 if (d->m_pen != pen) {
322 if (d->m_pen != pen) {
302 bool emitColorChanged = d->m_pen.color() != pen.color();
323 bool emitColorChanged = d->m_pen.color() != pen.color();
303 d->m_pen = pen;
324 d->m_pen = pen;
304 emit d->updated();
325 emit d->updated();
305 if (emitColorChanged)
326 if (emitColorChanged)
306 emit colorChanged(pen.color());
327 emit colorChanged(pen.color());
307 }
328 }
308 }
329 }
309
330
310 QPen QXYSeries::pen() const
331 QPen QXYSeries::pen() const
311 {
332 {
312 Q_D(const QXYSeries);
333 Q_D(const QXYSeries);
313 return d->m_pen;
334 return d->m_pen;
314 }
335 }
315
336
316 /*!
337 /*!
317 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
338 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
318 from chart theme setting is used.
339 from chart theme setting is used.
319 \sa QChart::setTheme()
340 \sa QChart::setTheme()
320 */
341 */
321 void QXYSeries::setBrush(const QBrush &brush)
342 void QXYSeries::setBrush(const QBrush &brush)
322 {
343 {
323 Q_D(QXYSeries);
344 Q_D(QXYSeries);
324 if (d->m_brush!=brush) {
345 if (d->m_brush!=brush) {
325 d->m_brush = brush;
346 d->m_brush = brush;
326 emit d->updated();
347 emit d->updated();
327 }
348 }
328 }
349 }
329
350
330 QBrush QXYSeries::brush() const
351 QBrush QXYSeries::brush() const
331 {
352 {
332 Q_D(const QXYSeries);
353 Q_D(const QXYSeries);
333 return d->m_brush;
354 return d->m_brush;
334 }
355 }
335
356
336 void QXYSeries::setColor(const QColor &color)
357 void QXYSeries::setColor(const QColor &color)
337 {
358 {
338 QPen p = pen();
359 QPen p = pen();
339 if (p.color() != color) {
360 if (p.color() != color) {
340 p.setColor(color);
361 p.setColor(color);
341 setPen(p);
362 setPen(p);
342 }
363 }
343 }
364 }
344
365
345 QColor QXYSeries::color() const
366 QColor QXYSeries::color() const
346 {
367 {
347 return pen().color();
368 return pen().color();
348 }
369 }
349
370
350 void QXYSeries::setPointsVisible(bool visible)
371 void QXYSeries::setPointsVisible(bool visible)
351 {
372 {
352 Q_D(QXYSeries);
373 Q_D(QXYSeries);
353 if (d->m_pointsVisible != visible){
374 if (d->m_pointsVisible != visible){
354 d->m_pointsVisible = visible;
375 d->m_pointsVisible = visible;
355 emit d->updated();
376 emit d->updated();
356 }
377 }
357 }
378 }
358
379
359 bool QXYSeries::pointsVisible() const
380 bool QXYSeries::pointsVisible() const
360 {
381 {
361 Q_D(const QXYSeries);
382 Q_D(const QXYSeries);
362 return d->m_pointsVisible;
383 return d->m_pointsVisible;
363 }
384 }
364
385
365
386
366 /*!
387 /*!
367 Stream operator for adding a data \a point to the series.
388 Stream operator for adding a data \a point to the series.
368 \sa append()
389 \sa append()
369 */
390 */
370 QXYSeries& QXYSeries::operator<< (const QPointF &point)
391 QXYSeries& QXYSeries::operator<< (const QPointF &point)
371 {
392 {
372 append(point);
393 append(point);
373 return *this;
394 return *this;
374 }
395 }
375
396
376
397
377 /*!
398 /*!
378 Stream operator for adding a list of \a points to the series.
399 Stream operator for adding a list of \a points to the series.
379 \sa append()
400 \sa append()
380 */
401 */
381
402
382 QXYSeries& QXYSeries::operator<< (const QList<QPointF>& points)
403 QXYSeries& QXYSeries::operator<< (const QList<QPointF>& points)
383 {
404 {
384 append(points);
405 append(points);
385 return *this;
406 return *this;
386 }
407 }
387
408
388 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
409 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
389
410
390
411
391 QXYSeriesPrivate::QXYSeriesPrivate(QXYSeries *q) :
412 QXYSeriesPrivate::QXYSeriesPrivate(QXYSeries *q) :
392 QAbstractSeriesPrivate(q),
413 QAbstractSeriesPrivate(q),
393 m_pointsVisible(false)
414 m_pointsVisible(false)
394 {
415 {
395 }
416 }
396
417
397 void QXYSeriesPrivate::scaleDomain(Domain& domain)
418 void QXYSeriesPrivate::scaleDomain(Domain& domain)
398 {
419 {
399 qreal minX(0);
420 qreal minX(0);
400 qreal minY(0);
421 qreal minY(0);
401 qreal maxX(1);
422 qreal maxX(1);
402 qreal maxY(1);
423 qreal maxY(1);
403
424
404 Q_Q(QXYSeries);
425 Q_Q(QXYSeries);
405
426
406 const QList<QPointF>& points = q->points();
427 const QList<QPointF>& points = q->points();
407
428
408 if (!points.isEmpty()){
429 if (!points.isEmpty()){
409 minX = points[0].x();
430 minX = points[0].x();
410 minY = points[0].y();
431 minY = points[0].y();
411 maxX = minX;
432 maxX = minX;
412 maxY = minY;
433 maxY = minY;
413
434
414 for (int i = 0; i < points.count(); i++) {
435 for (int i = 0; i < points.count(); i++) {
415 qreal x = points[i].x();
436 qreal x = points[i].x();
416 qreal y = points[i].y();
437 qreal y = points[i].y();
417 minX = qMin(minX, x);
438 minX = qMin(minX, x);
418 minY = qMin(minY, y);
439 minY = qMin(minY, y);
419 maxX = qMax(maxX, x);
440 maxX = qMax(maxX, x);
420 maxY = qMax(maxY, y);
441 maxY = qMax(maxY, y);
421 }
442 }
422 }
443 }
423
444
424 domain.setRange(minX,maxX,minY,maxY);
445 domain.setRange(minX,maxX,minY,maxY);
425 }
446 }
426
447
427 QList<LegendMarker*> QXYSeriesPrivate::createLegendMarker(QLegend* legend)
448 QList<LegendMarker*> QXYSeriesPrivate::createLegendMarker(QLegend* legend)
428 {
449 {
429 Q_Q(QXYSeries);
450 Q_Q(QXYSeries);
430 QList<LegendMarker*> list;
451 QList<LegendMarker*> list;
431 return list << new XYLegendMarker(q,legend);
452 return list << new XYLegendMarker(q,legend);
432 }
453 }
433
454
434 void QXYSeriesPrivate::initializeAxis(QAbstractAxis* axis)
455 void QXYSeriesPrivate::initializeAxis(QAbstractAxis* axis)
435 {
456 {
436 Q_UNUSED(axis);
457 Q_UNUSED(axis);
437 }
458 }
438
459
439 QAbstractAxis::AxisType QXYSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
460 QAbstractAxis::AxisType QXYSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
440 {
461 {
441 Q_UNUSED(orientation);
462 Q_UNUSED(orientation);
442 return QAbstractAxis::AxisTypeValues;
463 return QAbstractAxis::AxisTypeValues;
443 }
464 }
444
465
445 #include "moc_qxyseries.cpp"
466 #include "moc_qxyseries.cpp"
446 #include "moc_qxyseries_p.cpp"
467 #include "moc_qxyseries_p.cpp"
447
468
448 QTCOMMERCIALCHART_END_NAMESPACE
469 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,91 +1,94
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 #ifndef QXYSERIES_H
21 #ifndef QXYSERIES_H
22 #define QXYSERIES_H
22 #define QXYSERIES_H
23
23
24 #include <qchartglobal.h>
24 #include <qchartglobal.h>
25 #include <qabstractseries.h>
25 #include <qabstractseries.h>
26 #include <QPen>
26 #include <QPen>
27 #include <QBrush>
27 #include <QBrush>
28
28
29 class QModelIndex;
29 class QModelIndex;
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 class QXYSeriesPrivate;
33 class QXYSeriesPrivate;
34 class QXYModelMapper;
34 class QXYModelMapper;
35
35
36 class QTCOMMERCIALCHART_EXPORT QXYSeries : public QAbstractSeries
36 class QTCOMMERCIALCHART_EXPORT QXYSeries : public QAbstractSeries
37 {
37 {
38 Q_OBJECT
38 Q_OBJECT
39 Q_PROPERTY(bool pointsVisible READ pointsVisible WRITE setPointsVisible)
39 Q_PROPERTY(bool pointsVisible READ pointsVisible WRITE setPointsVisible)
40 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
40 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
41
41
42 protected:
42 protected:
43 explicit QXYSeries(QXYSeriesPrivate &d,QObject *parent = 0);
43 explicit QXYSeries(QXYSeriesPrivate &d,QObject *parent = 0);
44
44
45 public:
45 public:
46 ~QXYSeries();
46 ~QXYSeries();
47 void append(qreal x, qreal y);
47 void append(qreal x, qreal y);
48 void append(const QPointF &point);
48 void append(const QPointF &point);
49 void append(const QList<QPointF> &points);
49 void append(const QList<QPointF> &points);
50 void replace(qreal oldX,qreal oldY,qreal newX,qreal newY);
50 void replace(qreal oldX,qreal oldY,qreal newX,qreal newY);
51 void replace(const QPointF &oldPoint,const QPointF &newPoint);
51 void replace(const QPointF &oldPoint,const QPointF &newPoint);
52 void remove(qreal x, qreal y);
52 void remove(qreal x, qreal y);
53 void remove(const QPointF &point);
53 void remove(const QPointF &point);
54 void insert(int index, const QPointF &point);
54 void insert(int index, const QPointF &point);
55 void clear();
55 void clear();
56
56
57 int count() const;
57 int count() const;
58 QList<QPointF> points() const;
58 QList<QPointF> points() const;
59
59
60 QXYSeries& operator << (const QPointF &point);
60 QXYSeries& operator << (const QPointF &point);
61 QXYSeries& operator << (const QList<QPointF> &points);
61 QXYSeries& operator << (const QList<QPointF> &points);
62
62
63 virtual void setPen(const QPen &pen);
63 virtual void setPen(const QPen &pen);
64 QPen pen() const;
64 QPen pen() const;
65
65
66 virtual void setBrush(const QBrush &brush);
66 virtual void setBrush(const QBrush &brush);
67 QBrush brush() const;
67 QBrush brush() const;
68
68
69 virtual void setColor(const QColor &color);
69 virtual void setColor(const QColor &color);
70 virtual QColor color() const;
70 virtual QColor color() const;
71
71
72 void setPointsVisible(bool visible = true);
72 void setPointsVisible(bool visible = true);
73 bool pointsVisible() const;
73 bool pointsVisible() const;
74
74
75 void replace(QList<QPointF> points);
76
75 Q_SIGNALS:
77 Q_SIGNALS:
76 void clicked(const QPointF &point);
78 void clicked(const QPointF &point);
77 void pointReplaced(int index);
79 void pointReplaced(int index);
78 void pointRemoved(int index);
80 void pointRemoved(int index);
79 void pointAdded(int index);
81 void pointAdded(int index);
80 void colorChanged(QColor color);
82 void colorChanged(QColor color);
83 void pointsReplaced();
81
84
82 private:
85 private:
83 Q_DECLARE_PRIVATE(QXYSeries)
86 Q_DECLARE_PRIVATE(QXYSeries)
84 Q_DISABLE_COPY(QXYSeries)
87 Q_DISABLE_COPY(QXYSeries)
85 friend class XYLegendMarker;
88 friend class XYLegendMarker;
86 friend class XYChart;
89 friend class XYChart;
87 };
90 };
88
91
89 QTCOMMERCIALCHART_END_NAMESPACE
92 QTCOMMERCIALCHART_END_NAMESPACE
90
93
91 #endif
94 #endif
@@ -1,219 +1,227
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 "xychart_p.h"
21 #include "xychart_p.h"
22 #include "qxyseries.h"
22 #include "qxyseries.h"
23 #include "qxyseries_p.h"
23 #include "qxyseries_p.h"
24 #include "chartpresenter_p.h"
24 #include "chartpresenter_p.h"
25 #include "domain_p.h"
25 #include "domain_p.h"
26 #include "qxymodelmapper.h"
26 #include "qxymodelmapper.h"
27 #include <QPainter>
27 #include <QPainter>
28 #include <QAbstractItemModel>
28 #include <QAbstractItemModel>
29
29
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 //TODO: optimize : remove points which are not visible
33 //TODO: optimize : remove points which are not visible
34
34
35 XYChart::XYChart(QXYSeries *series, ChartPresenter *presenter):ChartElement(presenter),
35 XYChart::XYChart(QXYSeries *series, ChartPresenter *presenter):ChartElement(presenter),
36 m_minX(0),
36 m_minX(0),
37 m_maxX(0),
37 m_maxX(0),
38 m_minY(0),
38 m_minY(0),
39 m_maxY(0),
39 m_maxY(0),
40 m_series(series),
40 m_series(series),
41 m_animation(0),
41 m_animation(0),
42 m_dirty(true)
42 m_dirty(true)
43 {
43 {
44 QObject::connect(series, SIGNAL(pointReplaced(int)), this, SLOT(handlePointReplaced(int)));
44 QObject::connect(series, SIGNAL(pointReplaced(int)), this, SLOT(handlePointReplaced(int)));
45 QObject::connect(series, SIGNAL(pointsReplaced()), this, SLOT(handlePointsReplaced()));
45 QObject::connect(series, SIGNAL(pointAdded(int)), this, SLOT(handlePointAdded(int)));
46 QObject::connect(series, SIGNAL(pointAdded(int)), this, SLOT(handlePointAdded(int)));
46 QObject::connect(series, SIGNAL(pointRemoved(int)), this, SLOT(handlePointRemoved(int)));
47 QObject::connect(series, SIGNAL(pointRemoved(int)), this, SLOT(handlePointRemoved(int)));
47 QObject::connect(this, SIGNAL(clicked(QPointF)), series, SIGNAL(clicked(QPointF)));
48 QObject::connect(this, SIGNAL(clicked(QPointF)), series, SIGNAL(clicked(QPointF)));
48 }
49 }
49
50
50 void XYChart::setGeometryPoints(const QVector<QPointF>& points)
51 void XYChart::setGeometryPoints(const QVector<QPointF>& points)
51 {
52 {
52 m_points = points;
53 m_points = points;
53 }
54 }
54
55
55 void XYChart::setClipRect(const QRectF &rect)
56 void XYChart::setClipRect(const QRectF &rect)
56 {
57 {
57 m_clipRect = rect;
58 m_clipRect = rect;
58 }
59 }
59
60
60 void XYChart::setAnimation(XYAnimation* animation)
61 void XYChart::setAnimation(XYAnimation* animation)
61 {
62 {
62 m_animation=animation;
63 m_animation=animation;
63 }
64 }
64
65
65 void XYChart::setDirty(bool dirty)
66 void XYChart::setDirty(bool dirty)
66 {
67 {
67 m_dirty=dirty;
68 m_dirty=dirty;
68 }
69 }
69
70
70 QPointF XYChart::calculateGeometryPoint(const QPointF &point) const
71 QPointF XYChart::calculateGeometryPoint(const QPointF &point) const
71 {
72 {
72 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
73 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
73 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
74 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
74 qreal x = (point.x() - m_minX)* deltaX;
75 qreal x = (point.x() - m_minX)* deltaX;
75 qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
76 qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
76 return QPointF(x,y);
77 return QPointF(x,y);
77 }
78 }
78
79
79 QPointF XYChart::calculateGeometryPoint(int index) const
80 QPointF XYChart::calculateGeometryPoint(int index) const
80 {
81 {
81 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
82 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
82 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
83 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
83 const QList<QPointF>& vector = m_series->points();
84 const QList<QPointF>& vector = m_series->points();
84 qreal x = (vector[index].x() - m_minX)* deltaX;
85 qreal x = (vector[index].x() - m_minX)* deltaX;
85 qreal y = (vector[index].y() - m_minY)*-deltaY + m_size.height();
86 qreal y = (vector[index].y() - m_minY)*-deltaY + m_size.height();
86 return QPointF(x,y);
87 return QPointF(x,y);
87 }
88 }
88
89
89 QVector<QPointF> XYChart::calculateGeometryPoints() const
90 QVector<QPointF> XYChart::calculateGeometryPoints() const
90 {
91 {
91 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
92 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
92 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
93 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
93
94
94 QVector<QPointF> result;
95 QVector<QPointF> result;
95 result.resize(m_series->count());
96 result.resize(m_series->count());
96 const QList<QPointF>& vector = m_series->points();
97 const QList<QPointF>& vector = m_series->points();
97 for (int i = 0; i < m_series->count(); ++i) {
98 for (int i = 0; i < m_series->count(); ++i) {
98 qreal x = (vector[i].x() - m_minX)* deltaX;
99 qreal x = (vector[i].x() - m_minX)* deltaX;
99 qreal y = (vector[i].y() - m_minY)*-deltaY + m_size.height();
100 qreal y = (vector[i].y() - m_minY)*-deltaY + m_size.height();
100 result[i].setX(x);
101 result[i].setX(x);
101 result[i].setY(y);
102 result[i].setY(y);
102 }
103 }
103 return result;
104 return result;
104 }
105 }
105
106
106 QPointF XYChart::calculateDomainPoint(const QPointF &point) const
107 QPointF XYChart::calculateDomainPoint(const QPointF &point) const
107 {
108 {
108 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
109 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
109 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
110 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
110 qreal x = point.x()/deltaX +m_minX;
111 qreal x = point.x()/deltaX +m_minX;
111 qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
112 qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
112 return QPointF(x,y);
113 return QPointF(x,y);
113 }
114 }
114
115
115 void XYChart::updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index)
116 void XYChart::updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index)
116 {
117 {
117
118
118 if (m_animation) {
119 if (m_animation) {
119 m_animation->setup(oldPoints, newPoints, index);
120 m_animation->setup(oldPoints, newPoints, index);
120 m_points = newPoints;
121 m_points = newPoints;
121 setDirty(false);
122 setDirty(false);
122 presenter()->startAnimation(m_animation);
123 presenter()->startAnimation(m_animation);
123 }
124 }
124 else {
125 else {
125 m_points = newPoints;
126 m_points = newPoints;
126 updateGeometry();
127 updateGeometry();
127 }
128 }
128 }
129 }
129
130
130 //handlers
131 //handlers
131
132
132 void XYChart::handlePointAdded(int index)
133 void XYChart::handlePointAdded(int index)
133 {
134 {
134 Q_ASSERT(index<m_series->count());
135 Q_ASSERT(index<m_series->count());
135 Q_ASSERT(index>=0);
136 Q_ASSERT(index>=0);
136
137
137 QVector<QPointF> points;
138 QVector<QPointF> points;
138
139
139 if(m_dirty) {
140 if(m_dirty) {
140 points = calculateGeometryPoints();
141 points = calculateGeometryPoints();
141 } else {
142 } else {
142 points = m_points;
143 points = m_points;
143 QPointF point = calculateGeometryPoint(index);
144 QPointF point = calculateGeometryPoint(index);
144 points.insert(index, point);
145 points.insert(index, point);
145 }
146 }
146
147
147 updateChart(m_points,points,index);
148 updateChart(m_points,points,index);
148 }
149 }
149
150
150 void XYChart::handlePointRemoved(int index)
151 void XYChart::handlePointRemoved(int index)
151 {
152 {
152 Q_ASSERT(index<=m_series->count());
153 Q_ASSERT(index<=m_series->count());
153 Q_ASSERT(index>=0);
154 Q_ASSERT(index>=0);
154
155
155 QVector<QPointF> points;
156 QVector<QPointF> points;
156
157
157 if(m_dirty) {
158 if(m_dirty) {
158 points = calculateGeometryPoints();
159 points = calculateGeometryPoints();
159 } else {
160 } else {
160 points = m_points;
161 points = m_points;
161 points.remove(index);
162 points.remove(index);
162 }
163 }
163
164
164 updateChart(m_points,points,index);
165 updateChart(m_points,points,index);
165 }
166 }
166
167
167 void XYChart::handlePointReplaced(int index)
168 void XYChart::handlePointReplaced(int index)
168 {
169 {
169 Q_ASSERT(index<m_series->count());
170 Q_ASSERT(index<m_series->count());
170 Q_ASSERT(index>=0);
171 Q_ASSERT(index>=0);
171
172
172 QVector<QPointF> points;
173 QVector<QPointF> points;
173
174
174 if(m_dirty) {
175 if(m_dirty) {
175 points = calculateGeometryPoints();
176 points = calculateGeometryPoints();
176 } else {
177 } else {
177 QPointF point = calculateGeometryPoint(index);
178 QPointF point = calculateGeometryPoint(index);
178 points = m_points;
179 points = m_points;
179 points.replace(index,point);
180 points.replace(index,point);
180 }
181 }
181
182
182 updateChart(m_points,points,index);
183 updateChart(m_points,points,index);
183 }
184 }
184
185
186 void XYChart::handlePointsReplaced()
187 {
188 // All the points were replaced -> recalculate
189 QVector<QPointF> points = calculateGeometryPoints();
190 updateChart(m_points, points, -1);
191 }
192
185 void XYChart::handleDomainUpdated()
193 void XYChart::handleDomainUpdated()
186 {
194 {
187 m_minX=domain()->minX();
195 m_minX=domain()->minX();
188 m_maxX=domain()->maxX();
196 m_maxX=domain()->maxX();
189 m_minY=domain()->minY();
197 m_minY=domain()->minY();
190 m_maxY=domain()->maxY();
198 m_maxY=domain()->maxY();
191 if (isEmpty()) return;
199 if (isEmpty()) return;
192
200
193 QVector<QPointF> points = calculateGeometryPoints();
201 QVector<QPointF> points = calculateGeometryPoints();
194
202
195 updateChart(m_points,points);
203 updateChart(m_points,points);
196 }
204 }
197
205
198 void XYChart::handleGeometryChanged(const QRectF &rect)
206 void XYChart::handleGeometryChanged(const QRectF &rect)
199 {
207 {
200 Q_ASSERT(rect.isValid());
208 Q_ASSERT(rect.isValid());
201 m_size=rect.size();
209 m_size=rect.size();
202 m_clipRect=rect.translated(-rect.topLeft());
210 m_clipRect=rect.translated(-rect.topLeft());
203 m_origin=rect.topLeft();
211 m_origin=rect.topLeft();
204
212
205 if (isEmpty()) return;
213 if (isEmpty()) return;
206
214
207 QVector<QPointF> points = calculateGeometryPoints();
215 QVector<QPointF> points = calculateGeometryPoints();
208
216
209 updateChart(m_points,points);
217 updateChart(m_points,points);
210 }
218 }
211
219
212 bool XYChart::isEmpty()
220 bool XYChart::isEmpty()
213 {
221 {
214 return !m_clipRect.isValid() || qFuzzyIsNull(m_maxX - m_minX) || qFuzzyIsNull(m_maxY - m_minY) || m_series->points().isEmpty();
222 return !m_clipRect.isValid() || qFuzzyIsNull(m_maxX - m_minX) || qFuzzyIsNull(m_maxY - m_minY) || m_series->points().isEmpty();
215 }
223 }
216
224
217 #include "moc_xychart_p.cpp"
225 #include "moc_xychart_p.cpp"
218
226
219 QTCOMMERCIALCHART_END_NAMESPACE
227 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,106 +1,107
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 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef XYCHARTITEM_H
30 #ifndef XYCHARTITEM_H
31 #define XYCHARTITEM_H
31 #define XYCHARTITEM_H
32
32
33 #include "qchartglobal.h"
33 #include "qchartglobal.h"
34 #include "chartitem_p.h"
34 #include "chartitem_p.h"
35 #include "xyanimation_p.h"
35 #include "xyanimation_p.h"
36 #include "qvaluesaxis.h"
36 #include "qvaluesaxis.h"
37 #include <QPen>
37 #include <QPen>
38
38
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40
40
41 class ChartPresenter;
41 class ChartPresenter;
42 class QXYSeries;
42 class QXYSeries;
43
43
44 class XYChart : public ChartElement
44 class XYChart : public ChartElement
45 {
45 {
46 Q_OBJECT
46 Q_OBJECT
47 public:
47 public:
48 explicit XYChart(QXYSeries *series, ChartPresenter *presenter);
48 explicit XYChart(QXYSeries *series, ChartPresenter *presenter);
49 ~XYChart() {}
49 ~XYChart() {}
50
50
51 void setGeometryPoints(const QVector<QPointF>& points);
51 void setGeometryPoints(const QVector<QPointF>& points);
52 QVector<QPointF> geometryPoints() const { return m_points; }
52 QVector<QPointF> geometryPoints() const { return m_points; }
53
53
54 void setClipRect(const QRectF &rect);
54 void setClipRect(const QRectF &rect);
55 QRectF clipRect() const { return m_clipRect; }
55 QRectF clipRect() const { return m_clipRect; }
56
56
57 QSizeF size() const { return m_size; }
57 QSizeF size() const { return m_size; }
58 QPointF origin() const { return m_origin; }
58 QPointF origin() const { return m_origin; }
59
59
60 void setAnimation(XYAnimation* animation);
60 void setAnimation(XYAnimation* animation);
61 ChartAnimation* animation() const { return m_animation; }
61 ChartAnimation* animation() const { return m_animation; }
62 virtual void updateGeometry() = 0;
62 virtual void updateGeometry() = 0;
63
63
64 bool isDirty() const { return m_dirty; }
64 bool isDirty() const { return m_dirty; }
65 void setDirty(bool dirty);
65 void setDirty(bool dirty);
66
66
67 public Q_SLOTS:
67 public Q_SLOTS:
68 void handlePointAdded(int index);
68 void handlePointAdded(int index);
69 void handlePointRemoved(int index);
69 void handlePointRemoved(int index);
70 void handlePointReplaced(int index);
70 void handlePointReplaced(int index);
71 void handlePointsReplaced();
71 void handleDomainUpdated();
72 void handleDomainUpdated();
72 void handleGeometryChanged(const QRectF &size);
73 void handleGeometryChanged(const QRectF &size);
73
74
74 Q_SIGNALS:
75 Q_SIGNALS:
75 void clicked(const QPointF& point);
76 void clicked(const QPointF& point);
76
77
77 protected:
78 protected:
78 virtual void updateChart(QVector<QPointF> &oldPoints,QVector<QPointF> &newPoints,int index = -1);
79 virtual void updateChart(QVector<QPointF> &oldPoints,QVector<QPointF> &newPoints,int index = -1);
79 QPointF calculateGeometryPoint(const QPointF &point) const;
80 QPointF calculateGeometryPoint(const QPointF &point) const;
80 QPointF calculateGeometryPoint(int index) const;
81 QPointF calculateGeometryPoint(int index) const;
81 QPointF calculateDomainPoint(const QPointF &point) const;
82 QPointF calculateDomainPoint(const QPointF &point) const;
82 QVector<QPointF> calculateGeometryPoints() const;
83 QVector<QPointF> calculateGeometryPoints() const;
83
84
84 private:
85 private:
85 inline bool isEmpty();
86 inline bool isEmpty();
86
87
87 protected:
88 protected:
88 qreal m_minX;
89 qreal m_minX;
89 qreal m_maxX;
90 qreal m_maxX;
90 qreal m_minY;
91 qreal m_minY;
91 qreal m_maxY;
92 qreal m_maxY;
92 QXYSeries* m_series;
93 QXYSeries* m_series;
93 QSizeF m_size;
94 QSizeF m_size;
94 QPointF m_origin;
95 QPointF m_origin;
95 QRectF m_clipRect;
96 QRectF m_clipRect;
96 QVector<QPointF> m_points;
97 QVector<QPointF> m_points;
97 XYAnimation* m_animation;
98 XYAnimation* m_animation;
98 bool m_dirty;
99 bool m_dirty;
99
100
100 friend class AreaChartItem;
101 friend class AreaChartItem;
101
102
102 };
103 };
103
104
104 QTCOMMERCIALCHART_END_NAMESPACE
105 QTCOMMERCIALCHART_END_NAMESPACE
105
106
106 #endif
107 #endif
@@ -1,429 +1,439
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 "tst_qxyseries.h"
21 #include "tst_qxyseries.h"
22
22
23 Q_DECLARE_METATYPE(QList<QPointF>)
23 Q_DECLARE_METATYPE(QList<QPointF>)
24
24
25 void tst_QXYSeries::initTestCase()
25 void tst_QXYSeries::initTestCase()
26 {
26 {
27 }
27 }
28
28
29 void tst_QXYSeries::cleanupTestCase()
29 void tst_QXYSeries::cleanupTestCase()
30 {
30 {
31 }
31 }
32
32
33 void tst_QXYSeries::init()
33 void tst_QXYSeries::init()
34 {
34 {
35 m_view = new QChartView(new QChart());
35 m_view = new QChartView(new QChart());
36 m_chart = m_view->chart();
36 m_chart = m_view->chart();
37 }
37 }
38
38
39 void tst_QXYSeries::cleanup()
39 void tst_QXYSeries::cleanup()
40 {
40 {
41 delete m_view;
41 delete m_view;
42 m_view = 0;
42 m_view = 0;
43 m_chart = 0;
43 m_chart = 0;
44 m_series = 0;
44 m_series = 0;
45 }
45 }
46
46
47 void tst_QXYSeries::seriesName()
47 void tst_QXYSeries::seriesName()
48 {
48 {
49 QSignalSpy nameSpy(m_series, SIGNAL(nameChanged()));
49 QSignalSpy nameSpy(m_series, SIGNAL(nameChanged()));
50 QCOMPARE(m_series->name(), QString());
50 QCOMPARE(m_series->name(), QString());
51 m_series->setName("seriesname");
51 m_series->setName("seriesname");
52 QCOMPARE(m_series->name(), QString("seriesname"));
52 QCOMPARE(m_series->name(), QString("seriesname"));
53 TRY_COMPARE(nameSpy.count(), 1);
53 TRY_COMPARE(nameSpy.count(), 1);
54 }
54 }
55
55
56 void tst_QXYSeries::seriesVisible()
56 void tst_QXYSeries::seriesVisible()
57 {
57 {
58 QSignalSpy visibleSpy(m_series, SIGNAL(visibleChanged()));
58 QSignalSpy visibleSpy(m_series, SIGNAL(visibleChanged()));
59 QCOMPARE(m_series->isVisible(), true);
59 QCOMPARE(m_series->isVisible(), true);
60 m_series->setVisible(false);
60 m_series->setVisible(false);
61 QCOMPARE(m_series->isVisible(), false);
61 QCOMPARE(m_series->isVisible(), false);
62 m_series->setVisible(true);
62 m_series->setVisible(true);
63 TRY_COMPARE(visibleSpy.count(), 2);
63 TRY_COMPARE(visibleSpy.count(), 2);
64 }
64 }
65
65
66 void tst_QXYSeries::append_data()
66 void tst_QXYSeries::append_data()
67 {
67 {
68 QTest::addColumn< QList<QPointF> >("points");
68 QTest::addColumn< QList<QPointF> >("points");
69 QTest::newRow("0,0 1,1 2,2 3,3") << (QList<QPointF>() << QPointF(0,0) << QPointF(1,1) << QPointF(2,2) << QPointF(3,3));
69 QTest::newRow("0,0 1,1 2,2 3,3") << (QList<QPointF>() << QPointF(0,0) << QPointF(1,1) << QPointF(2,2) << QPointF(3,3));
70 QTest::newRow("0,0 -1,-1 -2,-2 -3,-3") << (QList<QPointF>() << QPointF(0,0) << QPointF(-1,-1) << QPointF(-2,-2) << QPointF(-3,-3));
70 QTest::newRow("0,0 -1,-1 -2,-2 -3,-3") << (QList<QPointF>() << QPointF(0,0) << QPointF(-1,-1) << QPointF(-2,-2) << QPointF(-3,-3));
71 }
71 }
72
72
73
73
74 void tst_QXYSeries::append_raw_data()
74 void tst_QXYSeries::append_raw_data()
75 {
75 {
76 append_data();
76 append_data();
77 }
77 }
78
78
79 void tst_QXYSeries::append_raw()
79 void tst_QXYSeries::append_raw()
80 {
80 {
81 QFETCH(QList<QPointF>, points);
81 QFETCH(QList<QPointF>, points);
82 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
82 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
83 QSignalSpy addedSpy(m_series, SIGNAL(pointAdded(int)));
83 QSignalSpy addedSpy(m_series, SIGNAL(pointAdded(int)));
84 m_series->append(points);
84 m_series->append(points);
85 TRY_COMPARE(spy0.count(), 0);
85 TRY_COMPARE(spy0.count(), 0);
86 TRY_COMPARE(addedSpy.count(), points.count());
86 TRY_COMPARE(addedSpy.count(), points.count());
87 QCOMPARE(m_series->points(), points);
87 QCOMPARE(m_series->points(), points);
88 }
88 }
89
89
90 void tst_QXYSeries::chart_append_data()
90 void tst_QXYSeries::chart_append_data()
91 {
91 {
92 append_data();
92 append_data();
93 }
93 }
94
94
95 void tst_QXYSeries::chart_append()
95 void tst_QXYSeries::chart_append()
96 {
96 {
97 append_raw();
97 append_raw();
98 m_chart->addSeries(m_series);
98 m_chart->addSeries(m_series);
99 m_view->show();
99 m_view->show();
100 QTest::qWaitForWindowShown(m_view);
100 QTest::qWaitForWindowShown(m_view);
101 }
101 }
102
102
103 void tst_QXYSeries::append_chart_data()
103 void tst_QXYSeries::append_chart_data()
104 {
104 {
105 append_data();
105 append_data();
106 }
106 }
107
107
108 void tst_QXYSeries::append_chart()
108 void tst_QXYSeries::append_chart()
109 {
109 {
110 m_view->show();
110 m_view->show();
111 m_chart->addSeries(m_series);
111 m_chart->addSeries(m_series);
112 QTest::qWaitForWindowShown(m_view);
112 QTest::qWaitForWindowShown(m_view);
113 append_raw();
113 append_raw();
114
114
115 }
115 }
116
116
117 void tst_QXYSeries::append_chart_animation_data()
117 void tst_QXYSeries::append_chart_animation_data()
118 {
118 {
119 append_data();
119 append_data();
120 }
120 }
121
121
122 void tst_QXYSeries::append_chart_animation()
122 void tst_QXYSeries::append_chart_animation()
123 {
123 {
124 m_chart->setAnimationOptions(QChart::AllAnimations);
124 m_chart->setAnimationOptions(QChart::AllAnimations);
125 append_chart();
125 append_chart();
126 }
126 }
127
127
128 void tst_QXYSeries::count_data()
128 void tst_QXYSeries::count_data()
129 {
129 {
130 QTest::addColumn<int>("count");
130 QTest::addColumn<int>("count");
131 QTest::newRow("0") << 0;
131 QTest::newRow("0") << 0;
132 QTest::newRow("5") << 5;
132 QTest::newRow("5") << 5;
133 QTest::newRow("10") << 5;
133 QTest::newRow("10") << 5;
134 }
134 }
135
135
136 void tst_QXYSeries::count_raw_data()
136 void tst_QXYSeries::count_raw_data()
137 {
137 {
138 count_data();
138 count_data();
139 }
139 }
140
140
141 void tst_QXYSeries::count_raw()
141 void tst_QXYSeries::count_raw()
142 {
142 {
143 QFETCH(int, count);
143 QFETCH(int, count);
144
144
145 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
145 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
146
146
147 for(int i=0 ; i< count; ++i)
147 for(int i=0 ; i< count; ++i)
148 m_series->append(i,i);
148 m_series->append(i,i);
149
149
150 TRY_COMPARE(spy0.count(), 0);
150 TRY_COMPARE(spy0.count(), 0);
151 QCOMPARE(m_series->count(), count);
151 QCOMPARE(m_series->count(), count);
152 }
152 }
153
153
154 void tst_QXYSeries::remove_raw_data()
154 void tst_QXYSeries::remove_raw_data()
155 {
155 {
156 append_data();
156 append_data();
157 }
157 }
158
158
159 void tst_QXYSeries::remove_raw()
159 void tst_QXYSeries::remove_raw()
160 {
160 {
161 QFETCH(QList<QPointF>, points);
161 QFETCH(QList<QPointF>, points);
162 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
162 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
163 m_series->append(points);
163 m_series->append(points);
164 TRY_COMPARE(spy0.count(), 0);
164 TRY_COMPARE(spy0.count(), 0);
165 QCOMPARE(m_series->points(), points);
165 QCOMPARE(m_series->points(), points);
166
166
167 foreach(const QPointF& point,points)
167 foreach(const QPointF& point,points)
168 m_series->remove(point);
168 m_series->remove(point);
169
169
170 QCOMPARE(m_series->points().count(), 0);
170 QCOMPARE(m_series->points().count(), 0);
171 TRY_COMPARE(spy0.count(), 0);
171 TRY_COMPARE(spy0.count(), 0);
172
172
173 m_series->append(points);
173 m_series->append(points);
174 QCOMPARE(m_series->points(), points);
174 QCOMPARE(m_series->points(), points);
175
175
176 //reverse order
176 //reverse order
177 for(int i = points.count()-1 ; i>=0; i--){
177 for(int i = points.count()-1 ; i>=0; i--){
178 m_series->remove(points[i]);
178 m_series->remove(points[i]);
179 }
179 }
180 QCOMPARE(m_series->points().count(), 0);
180 QCOMPARE(m_series->points().count(), 0);
181 }
181 }
182
182
183 void tst_QXYSeries::remove_chart_data()
183 void tst_QXYSeries::remove_chart_data()
184 {
184 {
185 append_data();
185 append_data();
186 }
186 }
187
187
188 void tst_QXYSeries::remove_chart()
188 void tst_QXYSeries::remove_chart()
189 {
189 {
190 m_view->show();
190 m_view->show();
191 m_chart->addSeries(m_series);
191 m_chart->addSeries(m_series);
192 QTest::qWaitForWindowShown(m_view);
192 QTest::qWaitForWindowShown(m_view);
193 remove_raw();
193 remove_raw();
194 }
194 }
195
195
196 void tst_QXYSeries::remove_chart_animation_data()
196 void tst_QXYSeries::remove_chart_animation_data()
197 {
197 {
198 append_data();
198 append_data();
199 }
199 }
200
200
201 void tst_QXYSeries::remove_chart_animation()
201 void tst_QXYSeries::remove_chart_animation()
202 {
202 {
203 m_chart->setAnimationOptions(QChart::AllAnimations);
203 m_chart->setAnimationOptions(QChart::AllAnimations);
204 remove_chart();
204 remove_chart();
205 }
205 }
206
206
207
207
208 void tst_QXYSeries::clear_raw_data()
208 void tst_QXYSeries::clear_raw_data()
209 {
209 {
210 append_data();
210 append_data();
211 }
211 }
212
212
213 void tst_QXYSeries::clear_raw()
213 void tst_QXYSeries::clear_raw()
214 {
214 {
215 QFETCH(QList<QPointF>, points);
215 QFETCH(QList<QPointF>, points);
216 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
216 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
217 m_series->append(points);
217 m_series->append(points);
218 TRY_COMPARE(spy0.count(), 0);
218 TRY_COMPARE(spy0.count(), 0);
219 QCOMPARE(m_series->points(), points);
219 QCOMPARE(m_series->points(), points);
220 m_series->clear();
220 m_series->clear();
221 TRY_COMPARE(spy0.count(), 0);
221 TRY_COMPARE(spy0.count(), 0);
222 QCOMPARE(m_series->points().count(), 0);
222 QCOMPARE(m_series->points().count(), 0);
223 }
223 }
224
224
225 void tst_QXYSeries::clear_chart_data()
225 void tst_QXYSeries::clear_chart_data()
226 {
226 {
227 append_data();
227 append_data();
228 }
228 }
229
229
230 void tst_QXYSeries::clear_chart()
230 void tst_QXYSeries::clear_chart()
231 {
231 {
232 m_view->show();
232 m_view->show();
233 m_chart->addSeries(m_series);
233 m_chart->addSeries(m_series);
234 QTest::qWaitForWindowShown(m_view);
234 QTest::qWaitForWindowShown(m_view);
235 clear_raw();
235 clear_raw();
236 }
236 }
237
237
238 void tst_QXYSeries::clear_chart_animation_data()
238 void tst_QXYSeries::clear_chart_animation_data()
239 {
239 {
240 append_data();
240 append_data();
241 }
241 }
242
242
243 void tst_QXYSeries::clear_chart_animation()
243 void tst_QXYSeries::clear_chart_animation()
244 {
244 {
245 m_chart->setAnimationOptions(QChart::AllAnimations);
245 m_chart->setAnimationOptions(QChart::AllAnimations);
246 clear_chart();
246 clear_chart();
247 }
247 }
248
248
249 void tst_QXYSeries::replace_raw_data()
249 void tst_QXYSeries::replace_raw_data()
250 {
250 {
251 append_data();
251 append_data();
252 }
252 }
253
253
254 void tst_QXYSeries::replace_raw()
254 void tst_QXYSeries::replace_raw()
255 {
255 {
256 QFETCH(QList<QPointF>, points);
256 QFETCH(QList<QPointF>, points);
257 QSignalSpy replacedSpy(m_series, SIGNAL(pointReplaced(int)));
257 QSignalSpy pointReplacedSpy(m_series, SIGNAL(pointReplaced(int)));
258 QSignalSpy pointsReplacedSpy(m_series, SIGNAL(pointsReplaced()));
258 m_series->append(points);
259 m_series->append(points);
259 TRY_COMPARE(replacedSpy.count(), 0);
260 TRY_COMPARE(pointReplacedSpy.count(), 0);
261 TRY_COMPARE(pointsReplacedSpy.count(), 0);
260 QCOMPARE(m_series->points(), points);
262 QCOMPARE(m_series->points(), points);
261
263
262 foreach(const QPointF& point, points)
264 foreach(const QPointF& point, points)
263 m_series->replace(point.x(),point.y(),point.x(),0);
265 m_series->replace(point.x(),point.y(),point.x(),0);
264 TRY_COMPARE(replacedSpy.count(), points.count());
266 TRY_COMPARE(pointReplacedSpy.count(), points.count());
267 TRY_COMPARE(pointsReplacedSpy.count(), 0);
265
268
266 // Replace a point that does not exist
269 // Replace a point that does not exist
267 m_series->replace(-123, 999, 0, 0);
270 m_series->replace(-123, 999, 0, 0);
268 TRY_COMPARE(replacedSpy.count(), points.count());
271 TRY_COMPARE(pointReplacedSpy.count(), points.count());
272 TRY_COMPARE(pointsReplacedSpy.count(), 0);
269
273
270 QList<QPointF> newPoints = m_series->points();
274 QList<QPointF> newPoints = m_series->points();
271
272 QCOMPARE(newPoints.count(), points.count());
275 QCOMPARE(newPoints.count(), points.count());
273
274 for(int i =0 ; i<points.count() ; ++i) {
276 for(int i =0 ; i<points.count() ; ++i) {
275 QCOMPARE(points[i].x(), newPoints[i].x());
277 QCOMPARE(points[i].x(), newPoints[i].x());
276 QCOMPARE(newPoints[i].y(), 0.0);
278 QCOMPARE(newPoints[i].y(), 0.0);
277 }
279 }
280
281 // Replace all points
282 QList<QPointF> allPoints;
283 for (int i = 0; i < 10; i++)
284 allPoints.append(QPointF(i, (qreal) rand() / (qreal) RAND_MAX));
285 m_series->replace(allPoints);
286 TRY_COMPARE(pointReplacedSpy.count(), points.count());
287 TRY_COMPARE(pointsReplacedSpy.count(), 1);
278 }
288 }
279
289
280
290
281 void tst_QXYSeries::replace_chart_data()
291 void tst_QXYSeries::replace_chart_data()
282 {
292 {
283 append_data();
293 append_data();
284 }
294 }
285
295
286 void tst_QXYSeries::replace_chart()
296 void tst_QXYSeries::replace_chart()
287 {
297 {
288 m_view->show();
298 m_view->show();
289 m_chart->addSeries(m_series);
299 m_chart->addSeries(m_series);
290 QTest::qWaitForWindowShown(m_view);
300 QTest::qWaitForWindowShown(m_view);
291 replace_raw();
301 replace_raw();
292 }
302 }
293
303
294 void tst_QXYSeries::replace_chart_animation_data()
304 void tst_QXYSeries::replace_chart_animation_data()
295 {
305 {
296 append_data();
306 append_data();
297 }
307 }
298
308
299 void tst_QXYSeries::replace_chart_animation()
309 void tst_QXYSeries::replace_chart_animation()
300 {
310 {
301 m_chart->setAnimationOptions(QChart::AllAnimations);
311 m_chart->setAnimationOptions(QChart::AllAnimations);
302 replace_chart();
312 replace_chart();
303 }
313 }
304
314
305 void tst_QXYSeries::insert_data()
315 void tst_QXYSeries::insert_data()
306 {
316 {
307 append_data();
317 append_data();
308 }
318 }
309
319
310 void tst_QXYSeries::insert()
320 void tst_QXYSeries::insert()
311 {
321 {
312 QFETCH(QList<QPointF>, points);
322 QFETCH(QList<QPointF>, points);
313 m_series->append(points);
323 m_series->append(points);
314
324
315 QSignalSpy addedSpy(m_series, SIGNAL(pointAdded(int)));
325 QSignalSpy addedSpy(m_series, SIGNAL(pointAdded(int)));
316
326
317 m_series->insert(0, QPointF(5, 5));
327 m_series->insert(0, QPointF(5, 5));
318 TRY_COMPARE(addedSpy.count(), 1);
328 TRY_COMPARE(addedSpy.count(), 1);
319 QCOMPARE(m_series->points().count(), points.count() + 1);
329 QCOMPARE(m_series->points().count(), points.count() + 1);
320
330
321 m_series->insert(m_series->count(), QPointF(6, 6));
331 m_series->insert(m_series->count(), QPointF(6, 6));
322 TRY_COMPARE(addedSpy.count(), 2);
332 TRY_COMPARE(addedSpy.count(), 2);
323 QCOMPARE(m_series->points().count(), points.count() + 2);
333 QCOMPARE(m_series->points().count(), points.count() + 2);
324 }
334 }
325
335
326 void tst_QXYSeries::oper_data()
336 void tst_QXYSeries::oper_data()
327 {
337 {
328 append_data();
338 append_data();
329 }
339 }
330
340
331 void tst_QXYSeries::oper()
341 void tst_QXYSeries::oper()
332 {
342 {
333 QFETCH(QList<QPointF>, points);
343 QFETCH(QList<QPointF>, points);
334
344
335 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
345 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
336
346
337 foreach(const QPointF& point,points)
347 foreach(const QPointF& point,points)
338 {
348 {
339 *m_series<<point;
349 *m_series<<point;
340 }
350 }
341
351
342 QCOMPARE(m_series->points(), points);
352 QCOMPARE(m_series->points(), points);
343 TRY_COMPARE(spy0.count(), 0);
353 TRY_COMPARE(spy0.count(), 0);
344 }
354 }
345
355
346
356
347 void tst_QXYSeries::pen_data()
357 void tst_QXYSeries::pen_data()
348 {
358 {
349 QTest::addColumn<QPen>("pen");
359 QTest::addColumn<QPen>("pen");
350 QTest::newRow("null") << QPen();
360 QTest::newRow("null") << QPen();
351 QTest::newRow("blue") << QPen(Qt::blue);
361 QTest::newRow("blue") << QPen(Qt::blue);
352 QTest::newRow("black") << QPen(Qt::black);
362 QTest::newRow("black") << QPen(Qt::black);
353 QTest::newRow("red") << QPen(Qt::red);
363 QTest::newRow("red") << QPen(Qt::red);
354 }
364 }
355
365
356 void tst_QXYSeries::pen()
366 void tst_QXYSeries::pen()
357 {
367 {
358 QFETCH(QPen, pen);
368 QFETCH(QPen, pen);
359
369
360 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
370 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
361 m_series->setPen(pen);
371 m_series->setPen(pen);
362
372
363 TRY_COMPARE(spy0.count(), 0);
373 TRY_COMPARE(spy0.count(), 0);
364 QCOMPARE(m_series->pen(), pen);
374 QCOMPARE(m_series->pen(), pen);
365
375
366 m_chart->addSeries(m_series);
376 m_chart->addSeries(m_series);
367
377
368 if (pen != QPen())
378 if (pen != QPen())
369 QCOMPARE(m_series->pen(), pen);
379 QCOMPARE(m_series->pen(), pen);
370
380
371 m_chart->setTheme(QChart::ChartThemeDark);
381 m_chart->setTheme(QChart::ChartThemeDark);
372
382
373 // setting a theme will overwrite all customizations
383 // setting a theme will overwrite all customizations
374 if (pen != QPen())
384 if (pen != QPen())
375 QVERIFY(m_series->pen() != pen);
385 QVERIFY(m_series->pen() != pen);
376 }
386 }
377
387
378 void tst_QXYSeries::pointsVisible_data()
388 void tst_QXYSeries::pointsVisible_data()
379 {
389 {
380 QTest::addColumn<bool>("pointsVisible");
390 QTest::addColumn<bool>("pointsVisible");
381 QTest::newRow("true") << true;
391 QTest::newRow("true") << true;
382 QTest::newRow("false") << false;
392 QTest::newRow("false") << false;
383 }
393 }
384
394
385 void tst_QXYSeries::pointsVisible_raw_data()
395 void tst_QXYSeries::pointsVisible_raw_data()
386 {
396 {
387 pointsVisible_data();
397 pointsVisible_data();
388 }
398 }
389
399
390 void tst_QXYSeries::pointsVisible_raw()
400 void tst_QXYSeries::pointsVisible_raw()
391 {
401 {
392 QFETCH(bool, pointsVisible);
402 QFETCH(bool, pointsVisible);
393 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
403 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
394 m_series->setPointsVisible(pointsVisible);
404 m_series->setPointsVisible(pointsVisible);
395 TRY_COMPARE(spy0.count(), 0);
405 TRY_COMPARE(spy0.count(), 0);
396 QCOMPARE(m_series->pointsVisible(), pointsVisible);
406 QCOMPARE(m_series->pointsVisible(), pointsVisible);
397 }
407 }
398
408
399 void tst_QXYSeries::changedSignals()
409 void tst_QXYSeries::changedSignals()
400 {
410 {
401 QSignalSpy visibleSpy(m_series, SIGNAL(visibleChanged()));
411 QSignalSpy visibleSpy(m_series, SIGNAL(visibleChanged()));
402 QSignalSpy nameSpy(m_series, SIGNAL(nameChanged()));
412 QSignalSpy nameSpy(m_series, SIGNAL(nameChanged()));
403 QSignalSpy colorSpy(m_series, SIGNAL(colorChanged(QColor)));
413 QSignalSpy colorSpy(m_series, SIGNAL(colorChanged(QColor)));
404
414
405 // Visibility
415 // Visibility
406 m_series->setVisible(false);
416 m_series->setVisible(false);
407 m_series->setVisible(false);
417 m_series->setVisible(false);
408 TRY_COMPARE(visibleSpy.count(), 1);
418 TRY_COMPARE(visibleSpy.count(), 1);
409 m_series->setVisible(true);
419 m_series->setVisible(true);
410 TRY_COMPARE(visibleSpy.count(), 2);
420 TRY_COMPARE(visibleSpy.count(), 2);
411
421
412 // Color
422 // Color
413 m_series->setColor(QColor("aliceblue"));
423 m_series->setColor(QColor("aliceblue"));
414 TRY_COMPARE(colorSpy.count(), 1);
424 TRY_COMPARE(colorSpy.count(), 1);
415
425
416 // Pen and Brush
426 // Pen and Brush
417 QPen p = m_series->pen();
427 QPen p = m_series->pen();
418 p.setColor("aquamarine");
428 p.setColor("aquamarine");
419 m_series->setPen(p);
429 m_series->setPen(p);
420 QBrush b = m_series->brush();
430 QBrush b = m_series->brush();
421 b.setColor("beige");
431 b.setColor("beige");
422 m_series->setBrush(b);
432 m_series->setBrush(b);
423 TRY_COMPARE(colorSpy.count(), 2);
433 TRY_COMPARE(colorSpy.count(), 2);
424
434
425 // Verify all the signals again, to make sure no extra signals were emitted
435 // Verify all the signals again, to make sure no extra signals were emitted
426 TRY_COMPARE(visibleSpy.count(), 2);
436 TRY_COMPARE(visibleSpy.count(), 2);
427 TRY_COMPARE(nameSpy.count(), 0);
437 TRY_COMPARE(nameSpy.count(), 0);
428 TRY_COMPARE(colorSpy.count(), 2);
438 TRY_COMPARE(colorSpy.count(), 2);
429 }
439 }
General Comments 0
You need to be logged in to leave comments. Login now