##// END OF EJS Templates
Fix scatter series color...
Titta Heikkala -
r2670:553ffb845c79
parent child
Show More
@@ -1,308 +1,310
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2013 Digia Plc
3 ** Copyright (C) 2013 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Enterprise Charts Add-on.
7 ** This file is part of the Qt Enterprise Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Enterprise licenses may use this file in
10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 ** accordance with the Qt Enterprise License Agreement provided with the
11 ** accordance with the Qt Enterprise License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qscatterseries.h"
21 #include "qscatterseries.h"
22 #include "qscatterseries_p.h"
22 #include "qscatterseries_p.h"
23 #include "scatterchartitem_p.h"
23 #include "scatterchartitem_p.h"
24 #include "chartdataset_p.h"
24 #include "chartdataset_p.h"
25 #include "charttheme_p.h"
25 #include "charttheme_p.h"
26 #include "scatteranimation_p.h"
26 #include "scatteranimation_p.h"
27 #include "qchart_p.h"
27 #include "qchart_p.h"
28
28
29 /*!
29 /*!
30 \class QScatterSeries
30 \class QScatterSeries
31 \inmodule Qt Charts
31 \inmodule Qt Charts
32 \brief The QScatterSeries class is used for making scatter charts.
32 \brief The QScatterSeries class is used for making scatter charts.
33
33
34 \mainclass
34 \mainclass
35
35
36 The scatter data is displayed as a collection of points on the chart. Each point determines the position on the horizontal axis
36 The scatter data is displayed as a collection of points on the chart. Each point determines the position on the horizontal axis
37 and the vertical axis.
37 and the vertical axis.
38
38
39 \image examples_scatterchart.png
39 \image examples_scatterchart.png
40
40
41 Creating basic scatter chart is simple:
41 Creating basic scatter chart is simple:
42 \code
42 \code
43 QScatterSeries* series = new QScatterSeries();
43 QScatterSeries* series = new QScatterSeries();
44 series->append(0, 6);
44 series->append(0, 6);
45 series->append(2, 4);
45 series->append(2, 4);
46 ...
46 ...
47 chart->addSeries(series);
47 chart->addSeries(series);
48 \endcode
48 \endcode
49 */
49 */
50 #ifdef QDOC_QT5
50 #ifdef QDOC_QT5
51 /*!
51 /*!
52 \qmltype ScatterSeries
52 \qmltype ScatterSeries
53 \instantiates QScatterSeries
53 \instantiates QScatterSeries
54 \inqmlmodule QtCommercial.Chart
54 \inqmlmodule QtCommercial.Chart
55
55
56 \include doc/src/scatterseries.qdocinc
56 \include doc/src/scatterseries.qdocinc
57 */
57 */
58 #else
58 #else
59 /*!
59 /*!
60 \qmlclass ScatterSeries QScatterSeries
60 \qmlclass ScatterSeries QScatterSeries
61
61
62 \include ../doc/src/scatterseries.qdocinc
62 \include ../doc/src/scatterseries.qdocinc
63 */
63 */
64 #endif
64 #endif
65
65
66 /*!
66 /*!
67 \enum QScatterSeries::MarkerShape
67 \enum QScatterSeries::MarkerShape
68
68
69 This enum describes the shape used when rendering marker items.
69 This enum describes the shape used when rendering marker items.
70
70
71 \value MarkerShapeCircle
71 \value MarkerShapeCircle
72 \value MarkerShapeRectangle
72 \value MarkerShapeRectangle
73 */
73 */
74
74
75 /*!
75 /*!
76 \property QScatterSeries::color
76 \property QScatterSeries::color
77 Fill (brush) color of the series. This is a convenience property for modifying the color of brush.
77 Fill (brush) color of the series. This is a convenience property for modifying the color of brush.
78 \sa QScatterSeries::brush()
78 \sa QScatterSeries::brush()
79 */
79 */
80
80
81 /*!
81 /*!
82 \property QScatterSeries::borderColor
82 \property QScatterSeries::borderColor
83 Line (pen) color of the series. This is a convenience property for modifying the color of pen.
83 Line (pen) color of the series. This is a convenience property for modifying the color of pen.
84 \sa QScatterSeries::pen()
84 \sa QScatterSeries::pen()
85 */
85 */
86 /*!
86 /*!
87 \qmlproperty color ScatterSeries::borderColor
87 \qmlproperty color ScatterSeries::borderColor
88 Border (pen) color of the series.
88 Border (pen) color of the series.
89 */
89 */
90
90
91 /*!
91 /*!
92 \qmlproperty real ScatterSeries::borderWidth
92 \qmlproperty real ScatterSeries::borderWidth
93 The width of the border line. By default the width is 2.0.
93 The width of the border line. By default the width is 2.0.
94 */
94 */
95
95
96 /*!
96 /*!
97 \property QScatterSeries::markerShape
97 \property QScatterSeries::markerShape
98 Defines the shape of the marker used to draw the points in the series. The default shape is MarkerShapeCircle.
98 Defines the shape of the marker used to draw the points in the series. The default shape is MarkerShapeCircle.
99 */
99 */
100 /*!
100 /*!
101 \qmlproperty MarkerShape ScatterSeries::markerShape
101 \qmlproperty MarkerShape ScatterSeries::markerShape
102 Defines the shape of the marker used to draw the points in the series. One of ScatterSeries
102 Defines the shape of the marker used to draw the points in the series. One of ScatterSeries
103 ScatterSeries.MarkerShapeCircle or ScatterSeries.MarkerShapeRectangle.
103 ScatterSeries.MarkerShapeCircle or ScatterSeries.MarkerShapeRectangle.
104 The default shape is ScatterSeries.MarkerShapeCircle.
104 The default shape is ScatterSeries.MarkerShapeCircle.
105 */
105 */
106
106
107 /*!
107 /*!
108 \property QScatterSeries::markerSize
108 \property QScatterSeries::markerSize
109 Defines the size of the marker used to draw the points in the series. The default size is 15.0.
109 Defines the size of the marker used to draw the points in the series. The default size is 15.0.
110 */
110 */
111 /*!
111 /*!
112 \qmlproperty real ScatterSeries::markerSize
112 \qmlproperty real ScatterSeries::markerSize
113 Defines the size of the marker used to draw the points in the series. The default size is 15.0.
113 Defines the size of the marker used to draw the points in the series. The default size is 15.0.
114 */
114 */
115
115
116 /*!
116 /*!
117 \fn void QScatterSeries::colorChanged(QColor color)
117 \fn void QScatterSeries::colorChanged(QColor color)
118 Signal is emitted when the fill (brush) color has changed to \a color.
118 Signal is emitted when the fill (brush) color has changed to \a color.
119 */
119 */
120
120
121 /*!
121 /*!
122 \fn void QScatterSeries::borderColorChanged(QColor color)
122 \fn void QScatterSeries::borderColorChanged(QColor color)
123 Signal is emitted when the line (pen) color has changed to \a color.
123 Signal is emitted when the line (pen) color has changed to \a color.
124 */
124 */
125 /*!
125 /*!
126 \qmlsignal ScatterSeries::borderColorChanged(color color)
126 \qmlsignal ScatterSeries::borderColorChanged(color color)
127 Signal is emitted when the line (pen) color has changed to \a color.
127 Signal is emitted when the line (pen) color has changed to \a color.
128 */
128 */
129
129
130 /*!
130 /*!
131 \fn QAbstractSeries::SeriesType QScatterSeries::type() const
131 \fn QAbstractSeries::SeriesType QScatterSeries::type() const
132 Returns QAbstractSeries::SeriesTypeScatter.
132 Returns QAbstractSeries::SeriesTypeScatter.
133 \sa QAbstractSeries, SeriesType
133 \sa QAbstractSeries, SeriesType
134 */
134 */
135
135
136 QTCOMMERCIALCHART_BEGIN_NAMESPACE
136 QTCOMMERCIALCHART_BEGIN_NAMESPACE
137
137
138 /*!
138 /*!
139 Constructs a series object which is a child of \a parent.
139 Constructs a series object which is a child of \a parent.
140 */
140 */
141 QScatterSeries::QScatterSeries(QObject *parent)
141 QScatterSeries::QScatterSeries(QObject *parent)
142 : QXYSeries(*new QScatterSeriesPrivate(this), parent)
142 : QXYSeries(*new QScatterSeriesPrivate(this), parent)
143 {
143 {
144 }
144 }
145
145
146 /*!
146 /*!
147 Destroys the object. Note that adding series to QChart transfers the ownership to the chart.
147 Destroys the object. Note that adding series to QChart transfers the ownership to the chart.
148 */
148 */
149 QScatterSeries::~QScatterSeries()
149 QScatterSeries::~QScatterSeries()
150 {
150 {
151 Q_D(QScatterSeries);
151 Q_D(QScatterSeries);
152 if (d->m_chart)
152 if (d->m_chart)
153 d->m_chart->removeSeries(this);
153 d->m_chart->removeSeries(this);
154 }
154 }
155
155
156 QAbstractSeries::SeriesType QScatterSeries::type() const
156 QAbstractSeries::SeriesType QScatterSeries::type() const
157 {
157 {
158 return QAbstractSeries::SeriesTypeScatter;
158 return QAbstractSeries::SeriesTypeScatter;
159 }
159 }
160
160
161 /*!
161 /*!
162 Sets \a pen used for drawing points' border on the chart. If the pen is not defined, the
162 Sets \a pen used for drawing points' border on the chart. If the pen is not defined, the
163 pen from chart theme is used.
163 pen from chart theme is used.
164 \sa QChart::setTheme()
164 \sa QChart::setTheme()
165 */
165 */
166 void QScatterSeries::setPen(const QPen &pen)
166 void QScatterSeries::setPen(const QPen &pen)
167 {
167 {
168 Q_D(QXYSeries);
168 Q_D(QXYSeries);
169 if (d->m_pen != pen) {
169 if (d->m_pen != pen) {
170 bool emitColorChanged = d->m_pen.color() != pen.color();
170 bool emitColorChanged = d->m_pen.color() != pen.color();
171 d->m_pen = pen;
171 d->m_pen = pen;
172 emit d->updated();
172 emit d->updated();
173 if (emitColorChanged)
173 if (emitColorChanged)
174 emit borderColorChanged(pen.color());
174 emit borderColorChanged(pen.color());
175 }
175 }
176 }
176 }
177
177
178 /*!
178 /*!
179 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
179 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
180 from chart theme setting is used.
180 from chart theme setting is used.
181 \sa QChart::setTheme()
181 \sa QChart::setTheme()
182 */
182 */
183 void QScatterSeries::setBrush(const QBrush &brush)
183 void QScatterSeries::setBrush(const QBrush &brush)
184 {
184 {
185 Q_D(QScatterSeries);
185 Q_D(QScatterSeries);
186 if (d->m_brush != brush) {
186 if (d->m_brush != brush) {
187 bool emitColorChanged = d->m_brush.color() != brush.color();
187 bool emitColorChanged = d->m_brush.color() != brush.color();
188 d->m_brush = brush;
188 d->m_brush = brush;
189 emit d->updated();
189 emit d->updated();
190 if (emitColorChanged)
190 if (emitColorChanged)
191 emit colorChanged(brush.color());
191 emit colorChanged(brush.color());
192 }
192 }
193 }
193 }
194
194
195 void QScatterSeries::setColor(const QColor &color)
195 void QScatterSeries::setColor(const QColor &color)
196 {
196 {
197 QBrush b = brush();
197 QBrush b = brush();
198 if (b == QChartPrivate::defaultBrush())
199 b = QBrush();
198 if (b == QBrush())
200 if (b == QBrush())
199 b.setStyle(Qt::SolidPattern);
201 b.setStyle(Qt::SolidPattern);
200 b.setColor(color);
202 b.setColor(color);
201 setBrush(b);
203 setBrush(b);
202 }
204 }
203
205
204 QColor QScatterSeries::color() const
206 QColor QScatterSeries::color() const
205 {
207 {
206 return brush().color();
208 return brush().color();
207 }
209 }
208
210
209 void QScatterSeries::setBorderColor(const QColor &color)
211 void QScatterSeries::setBorderColor(const QColor &color)
210 {
212 {
211 QPen p = pen();
213 QPen p = pen();
212 if (p.color() != color) {
214 if (p == QChartPrivate::defaultPen())
215 p = QPen();
213 p.setColor(color);
216 p.setColor(color);
214 setPen(p);
217 setPen(p);
215 }
218 }
216 }
217
219
218 QColor QScatterSeries::borderColor() const
220 QColor QScatterSeries::borderColor() const
219 {
221 {
220 return pen().color();
222 return pen().color();
221 }
223 }
222
224
223 QScatterSeries::MarkerShape QScatterSeries::markerShape() const
225 QScatterSeries::MarkerShape QScatterSeries::markerShape() const
224 {
226 {
225 Q_D(const QScatterSeries);
227 Q_D(const QScatterSeries);
226 return d->m_shape;
228 return d->m_shape;
227 }
229 }
228
230
229 void QScatterSeries::setMarkerShape(MarkerShape shape)
231 void QScatterSeries::setMarkerShape(MarkerShape shape)
230 {
232 {
231 Q_D(QScatterSeries);
233 Q_D(QScatterSeries);
232 if (d->m_shape != shape) {
234 if (d->m_shape != shape) {
233 d->m_shape = shape;
235 d->m_shape = shape;
234 emit d->updated();
236 emit d->updated();
235 }
237 }
236 }
238 }
237
239
238 qreal QScatterSeries::markerSize() const
240 qreal QScatterSeries::markerSize() const
239 {
241 {
240 Q_D(const QScatterSeries);
242 Q_D(const QScatterSeries);
241 return d->m_size;
243 return d->m_size;
242 }
244 }
243
245
244 void QScatterSeries::setMarkerSize(qreal size)
246 void QScatterSeries::setMarkerSize(qreal size)
245 {
247 {
246 Q_D(QScatterSeries);
248 Q_D(QScatterSeries);
247
249
248 if (!qFuzzyCompare(d->m_size, size)) {
250 if (!qFuzzyCompare(d->m_size, size)) {
249 d->m_size = size;
251 d->m_size = size;
250 emit d->updated();
252 emit d->updated();
251 }
253 }
252 }
254 }
253
255
254 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
256 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
255
257
256 QScatterSeriesPrivate::QScatterSeriesPrivate(QScatterSeries *q)
258 QScatterSeriesPrivate::QScatterSeriesPrivate(QScatterSeries *q)
257 : QXYSeriesPrivate(q),
259 : QXYSeriesPrivate(q),
258 m_shape(QScatterSeries::MarkerShapeCircle),
260 m_shape(QScatterSeries::MarkerShapeCircle),
259 m_size(15.0)
261 m_size(15.0)
260 {
262 {
261 }
263 }
262
264
263 void QScatterSeriesPrivate::initializeGraphics(QGraphicsItem* parent)
265 void QScatterSeriesPrivate::initializeGraphics(QGraphicsItem* parent)
264 {
266 {
265 Q_Q(QScatterSeries);
267 Q_Q(QScatterSeries);
266 ScatterChartItem *scatter = new ScatterChartItem(q,parent);
268 ScatterChartItem *scatter = new ScatterChartItem(q,parent);
267 m_item.reset(scatter);
269 m_item.reset(scatter);
268 QAbstractSeriesPrivate::initializeGraphics(parent);
270 QAbstractSeriesPrivate::initializeGraphics(parent);
269 }
271 }
270
272
271 void QScatterSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced)
273 void QScatterSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced)
272 {
274 {
273 Q_Q(QScatterSeries);
275 Q_Q(QScatterSeries);
274 const QList<QColor> colors = theme->seriesColors();
276 const QList<QColor> colors = theme->seriesColors();
275 const QList<QGradient> gradients = theme->seriesGradients();
277 const QList<QGradient> gradients = theme->seriesGradients();
276
278
277 if (forced || QChartPrivate::defaultPen() == m_pen) {
279 if (forced || QChartPrivate::defaultPen() == m_pen) {
278 QPen pen;
280 QPen pen;
279 pen.setColor(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0));
281 pen.setColor(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0));
280 pen.setWidthF(2);
282 pen.setWidthF(2);
281 q->setPen(pen);
283 q->setPen(pen);
282 }
284 }
283
285
284 if (forced || QChartPrivate::defaultBrush() == m_brush) {
286 if (forced || QChartPrivate::defaultBrush() == m_brush) {
285 QBrush brush(colors.at(index % colors.size()));
287 QBrush brush(colors.at(index % colors.size()));
286 q->setBrush(brush);
288 q->setBrush(brush);
287 }
289 }
288 }
290 }
289
291
290 void QScatterSeriesPrivate::initializeAnimations(QChart::AnimationOptions options)
292 void QScatterSeriesPrivate::initializeAnimations(QChart::AnimationOptions options)
291 {
293 {
292 ScatterChartItem *item = static_cast<ScatterChartItem *>(m_item.data());
294 ScatterChartItem *item = static_cast<ScatterChartItem *>(m_item.data());
293 Q_ASSERT(item);
295 Q_ASSERT(item);
294
296
295 if (item->animation())
297 if (item->animation())
296 item->animation()->stopAndDestroyLater();
298 item->animation()->stopAndDestroyLater();
297
299
298 if (options.testFlag(QChart::SeriesAnimations))
300 if (options.testFlag(QChart::SeriesAnimations))
299 item->setAnimation(new ScatterAnimation(item));
301 item->setAnimation(new ScatterAnimation(item));
300 else
302 else
301 item->setAnimation(0);
303 item->setAnimation(0);
302
304
303 QAbstractSeriesPrivate::initializeAnimations(options);
305 QAbstractSeriesPrivate::initializeAnimations(options);
304 }
306 }
305
307
306 #include "moc_qscatterseries.cpp"
308 #include "moc_qscatterseries.cpp"
307
309
308 QTCOMMERCIALCHART_END_NAMESPACE
310 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now