##// END OF EJS Templates
Fixed documentation issues in QML API
Tero Ahola -
r2331:d7e6106b698e
parent child
Show More
@@ -1,769 +1,765
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 "declarativechart.h"
21 #include "declarativechart.h"
22 #include <QPainter>
22 #include <QPainter>
23 #include <QDeclarativeEngine>
23 #include <QDeclarativeEngine>
24 #include "declarativelineseries.h"
24 #include "declarativelineseries.h"
25 #include "declarativeareaseries.h"
25 #include "declarativeareaseries.h"
26 #include "declarativebarseries.h"
26 #include "declarativebarseries.h"
27 #include "declarativepieseries.h"
27 #include "declarativepieseries.h"
28 #include "declarativesplineseries.h"
28 #include "declarativesplineseries.h"
29 #include "declarativescatterseries.h"
29 #include "declarativescatterseries.h"
30 #include "qbarcategoryaxis.h"
30 #include "qbarcategoryaxis.h"
31 #include "qvalueaxis.h"
31 #include "qvalueaxis.h"
32 #include "qcategoryaxis.h"
32 #include "qcategoryaxis.h"
33 #include "qabstractseries_p.h"
33 #include "qabstractseries_p.h"
34 #include "declarativemargins.h"
34 #include "declarativemargins.h"
35 #include "chartdataset_p.h"
35 #include "chartdataset_p.h"
36 #include "declarativeaxes.h"
36 #include "declarativeaxes.h"
37 #include "qchart_p.h"
37 #include "qchart_p.h"
38
38
39 #ifndef QT_ON_ARM
39 #ifndef QT_ON_ARM
40 #include "qdatetimeaxis.h"
40 #include "qdatetimeaxis.h"
41 #endif
41 #endif
42
42
43 QTCOMMERCIALCHART_BEGIN_NAMESPACE
43 QTCOMMERCIALCHART_BEGIN_NAMESPACE
44
44
45 /*!
45 /*!
46 \qmlclass ChartView DeclarativeChart
46 \qmlclass ChartView DeclarativeChart
47
47
48 ChartView element is the parent that is responsible for showing different chart series types.
48 ChartView element is the parent that is responsible for showing different chart series types.
49
49
50 The following QML shows how to create a simple chart with one pie series:
50 The following QML shows how to create a simple chart with one pie series:
51 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 1
51 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 1
52 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 2
52 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 2
53 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 3
53 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 3
54
54
55 \beginfloatleft
55 \beginfloatleft
56 \image examples_qmlpiechart.png
56 \image examples_qmlpiechart.png
57 \endfloat
57 \endfloat
58 \clearfloat
58 \clearfloat
59 */
59 */
60
60
61 /*!
61 /*!
62 \qmlproperty Theme ChartView::theme
62 \qmlproperty Theme ChartView::theme
63 Theme defines the visual appearance of the chart, including for example colors, fonts, line
63 Theme defines the visual appearance of the chart, including for example colors, fonts, line
64 widths and chart background.
64 widths and chart background.
65 */
65 */
66
66
67 /*!
67 /*!
68 \qmlproperty Animation ChartView::animationOptions
68 \qmlproperty Animation ChartView::animationOptions
69 Animation configuration of the chart. One of ChartView.NoAnimation, ChartView.GridAxisAnimations,
69 Animation configuration of the chart. One of ChartView.NoAnimation, ChartView.GridAxisAnimations,
70 ChartView.SeriesAnimations or ChartView.AllAnimations.
70 ChartView.SeriesAnimations or ChartView.AllAnimations.
71 */
71 */
72
72
73 /*!
73 /*!
74 \qmlproperty Font ChartView::titleFont
74 \qmlproperty Font ChartView::titleFont
75 The title font of the chart
75 The title font of the chart
76
76
77 See the \l {Font} {QML Font Element} for detailed documentation.
77 See the \l {Font} {QML Font Element} for detailed documentation.
78 */
78 */
79
79
80 /*!
80 /*!
81 \qmlproperty string ChartView::title
81 \qmlproperty string ChartView::title
82 The title of the chart, shown on top of the chart.
82 The title of the chart, shown on top of the chart.
83 \sa ChartView::titleColor
83 \sa ChartView::titleColor
84 */
84 */
85
85
86 /*!
86 /*!
87 \qmlproperty color ChartView::titleColor
87 \qmlproperty color ChartView::titleColor
88 The color of the title text.
88 The color of the title text.
89 */
89 */
90
90
91 /*!
91 /*!
92 \qmlproperty Legend ChartView::legend
92 \qmlproperty Legend ChartView::legend
93 The legend of the chart. Legend lists all the series, pie slices and bar sets added on the chart.
93 The legend of the chart. Legend lists all the series, pie slices and bar sets added on the chart.
94 */
94 */
95
95
96 /*!
96 /*!
97 \qmlproperty int ChartView::count
97 \qmlproperty int ChartView::count
98 The count of series added to the chart.
98 The count of series added to the chart.
99 */
99 */
100
100
101 /*!
101 /*!
102 \qmlproperty color ChartView::backgroundColor
102 \qmlproperty color ChartView::backgroundColor
103 The color of the chart's background. By default background color is defined by chart theme.
103 The color of the chart's background. By default background color is defined by chart theme.
104 \sa ChartView::theme
104 \sa ChartView::theme
105 */
105 */
106
106
107 /*!
107 /*!
108 \qmlproperty bool ChartView::dropShadowEnabled
108 \qmlproperty bool ChartView::dropShadowEnabled
109 The chart's border drop shadow. Set to true to enable drop shadow.
109 The chart's border drop shadow. Set to true to enable drop shadow.
110 */
110 */
111
111
112 /*!
112 /*!
113 \qmlproperty real ChartView::topMargin
113 \qmlproperty real ChartView::topMargin
114 Deprecated. Use minimumMargins and plotArea instead.
114 Deprecated. Use minimumMargins and plotArea instead.
115 */
115 */
116
116
117 /*!
117 /*!
118 \qmlproperty real ChartView::bottomMargin
118 \qmlproperty real ChartView::bottomMargin
119 Deprecated. Use minimumMargins and plotArea instead.
119 Deprecated. Use minimumMargins and plotArea instead.
120 */
120 */
121
121
122 /*!
122 /*!
123 \qmlproperty real ChartView::leftMargin
123 \qmlproperty real ChartView::leftMargin
124 Deprecated. Use minimumMargins and plotArea instead.
124 Deprecated. Use minimumMargins and plotArea instead.
125 */
125 */
126
126
127 /*!
127 /*!
128 \qmlproperty real ChartView::rightMargin
128 \qmlproperty real ChartView::rightMargin
129 Deprecated. Use minimumMargins and plotArea instead.
129 Deprecated. Use minimumMargins and plotArea instead.
130 */
130 */
131
131
132 /*!
132 /*!
133 \qmlproperty Margins ChartView::minimumMargins
133 \qmlproperty Margins ChartView::minimumMargins
134 The minimum margins allowed between the outer bounds and the plotArea of the ChartView. Margins
134 The minimum margins allowed between the outer bounds and the plotArea of the ChartView. Margins
135 area of ChartView is used for drawing title, axes and legend. Please note that setting the
135 area of ChartView is used for drawing title, axes and legend. Please note that setting the
136 properties of minimumMargins may be bigger than the defined value, depending on other ChartView
136 properties of minimumMargins may be bigger than the defined value, depending on other ChartView
137 properties that affect it's layout. If you need to know the actual plotting area used at any
137 properties that affect it's layout. If you need to know the actual plotting area used at any
138 given time, you can check ChartView::plotArea instead.
138 given time, you can check ChartView::plotArea instead.
139 */
139 */
140
140
141 /*!
141 /*!
142 \qmlproperty rect ChartView::plotArea
142 \qmlproperty rect ChartView::plotArea
143 The area on the ChartView that is used for drawing series. This is the ChartView rect without the
143 The area on the ChartView that is used for drawing series. This is the ChartView rect without the
144 margins.
144 margins.
145 \sa ChartView::minimumMargins
145 \sa ChartView::minimumMargins
146 */
146 */
147
147
148 /*!
148 /*!
149 \qmlmethod AbstractSeries ChartView::series(int index)
149 \qmlmethod AbstractSeries ChartView::series(int index)
150 Returns the series with \a index on the chart. This allows you to loop through the series of a chart together with
150 Returns the series with \a index on the chart. This allows you to loop through the series of a chart together with
151 the count property of the chart.
151 the count property of the chart.
152 */
152 */
153
153
154 /*!
154 /*!
155 \qmlmethod AbstractSeries ChartView::series(string name)
155 \qmlmethod AbstractSeries ChartView::series(string name)
156 Returns the first series on the chart with \a name. If there is no series with that name, returns null.
156 Returns the first series on the chart with \a name. If there is no series with that name, returns null.
157 */
157 */
158
158
159 /*!
159 /*!
160 \qmlmethod AbstractSeries ChartView::createSeries(SeriesType type, string name, AbstractAxis axisX, AbstractAxis axisY)
160 \qmlmethod AbstractSeries ChartView::createSeries(SeriesType type, string name, AbstractAxis axisX, AbstractAxis axisY)
161 Creates a series object of \a type to the chart with name \a name, optional axis \a axisX and
161 Creates a series object of \a type to the chart with name \a name, optional axis \a axisX and
162 optional axis \a axisY. For example:
162 optional axis \a axisY. For example:
163 \code
163 \code
164 // lineSeries is a LineSeries object that has already been added to the ChartView; re-use it's axes
164 // lineSeries is a LineSeries object that has already been added to the ChartView; re-use it's axes
165 var myAxisX = chartView.axisX(lineSeries);
165 var myAxisX = chartView.axisX(lineSeries);
166 var myAxisY = chartView.axisY(lineSeries);
166 var myAxisY = chartView.axisY(lineSeries);
167 var scatter = chartView.createSeries(ChartView.SeriesTypeScatter, "scatter series", myAxisX, myAxisY);
167 var scatter = chartView.createSeries(ChartView.SeriesTypeScatter, "scatter series", myAxisX, myAxisY);
168 \endcode
168 \endcode
169 */
169 */
170
170
171 /*!
171 /*!
172 \qmlmethod ChartView::removeSeries(AbstractSeries series)
172 \qmlmethod ChartView::removeSeries(AbstractSeries series)
173 Removes the \a series from the chart. The series object is also destroyed.
173 Removes the \a series from the chart. The series object is also destroyed.
174 */
174 */
175
175
176 /*!
176 /*!
177 \qmlmethod ChartView::removeAllSeries()
177 \qmlmethod ChartView::removeAllSeries()
178 Removes all series from the chart. All the series objects are also destroyed.
178 Removes all series from the chart. All the series objects are also destroyed.
179 */
179 */
180
180
181 /*!
181 /*!
182 \qmlmethod Axis ChartView::axisX(AbstractSeries series)
182 \qmlmethod Axis ChartView::axisX(AbstractSeries series)
183 The x-axis of the series.
183 The x-axis of the series.
184 */
184 */
185
185
186 /*!
186 /*!
187 \qmlmethod Axis ChartView::axisY(AbstractSeries series)
187 \qmlmethod Axis ChartView::axisY(AbstractSeries series)
188 The y-axis of the series.
188 The y-axis of the series.
189 */
189 */
190
190
191 /*!
191 /*!
192 \qmlmethod ChartView::zoomY(real factor)
192 \qmlmethod ChartView::zoomY(real factor)
193 Zooms in by \a factor on the center of the chart.
193 Zooms in by \a factor on the center of the chart.
194 */
194 */
195
195
196 /*!
196 /*!
197 \qmlmethod ChartView::scrollLeft(real pixels)
197 \qmlmethod ChartView::scrollLeft(real pixels)
198 Scrolls to left by \a pixels. This is a convenience function that suits for example for key navigation.
198 Scrolls to left by \a pixels. This is a convenience function that suits for example for key navigation.
199 \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max
200 */
199 */
201
200
202 /*!
201 /*!
203 \qmlmethod ChartView::scrollRight(real pixels)
202 \qmlmethod ChartView::scrollRight(real pixels)
204 Scrolls to right by \a pixels. This is a convenience function that suits for example for key navigation.
203 Scrolls to right by \a pixels. This is a convenience function that suits for example for key navigation.
205 \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max
206 */
204 */
207
205
208 /*!
206 /*!
209 \qmlmethod ChartView::scrollUp(real pixels)
207 \qmlmethod ChartView::scrollUp(real pixels)
210 Scrolls up by \a pixels. This is a convenience function that suits for example for key navigation.
208 Scrolls up by \a pixels. This is a convenience function that suits for example for key navigation.
211 \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max
212 */
209 */
213
210
214 /*!
211 /*!
215 \qmlmethod ChartView::scrollDown(real pixels)
212 \qmlmethod ChartView::scrollDown(real pixels)
216 Scrolls down by \a pixels. This is a convenience function that suits for example for key navigation.
213 Scrolls down by \a pixels. This is a convenience function that suits for example for key navigation.
217 \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max
218 */
214 */
219
215
220 /*!
216 /*!
221 \qmlsignal ChartView::onPlotAreaChanged(rect plotArea)
217 \qmlsignal ChartView::onPlotAreaChanged(rect plotArea)
222 The plot area of the chart has changed. This may happen for example, if you modify minimumMargins
218 The plot area of the chart has changed. This may happen for example, if you modify minimumMargins
223 or if you resize the chart, or if you modify font size related properties of the legend or chart
219 or if you resize the chart, or if you modify font size related properties of the legend or chart
224 title.
220 title.
225 */
221 */
226
222
227 /*!
223 /*!
228 \qmlsignal ChartView::seriesAdded(AbstractSeries series)
224 \qmlsignal ChartView::seriesAdded(AbstractSeries series)
229 The \a series has been added to the chart.
225 The \a series has been added to the chart.
230 */
226 */
231
227
232 /*!
228 /*!
233 \qmlsignal ChartView::seriesRemoved(AbstractSeries series)
229 \qmlsignal ChartView::seriesRemoved(AbstractSeries series)
234 The \a series has been removed from the chart. Please note that \a series is no longer a valid
230 The \a series has been removed from the chart. Please note that \a series is no longer a valid
235 object after the signal handler has completed.
231 object after the signal handler has completed.
236 */
232 */
237
233
238 DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent)
234 DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent)
239 : QDeclarativeItem(parent),
235 : QDeclarativeItem(parent),
240 m_chart(new QChart(this))
236 m_chart(new QChart(this))
241 {
237 {
242 setFlag(QGraphicsItem::ItemHasNoContents, false);
238 setFlag(QGraphicsItem::ItemHasNoContents, false);
243 m_margins = new DeclarativeMargins(this);
239 m_margins = new DeclarativeMargins(this);
244 m_margins->setTop(m_chart->margins().top());
240 m_margins->setTop(m_chart->margins().top());
245 m_margins->setLeft(m_chart->margins().left());
241 m_margins->setLeft(m_chart->margins().left());
246 m_margins->setRight(m_chart->margins().right());
242 m_margins->setRight(m_chart->margins().right());
247 m_margins->setBottom(m_chart->margins().bottom());
243 m_margins->setBottom(m_chart->margins().bottom());
248 connect(m_margins, SIGNAL(topChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int)));
244 connect(m_margins, SIGNAL(topChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int)));
249 connect(m_margins, SIGNAL(bottomChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int)));
245 connect(m_margins, SIGNAL(bottomChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int)));
250 connect(m_margins, SIGNAL(leftChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int)));
246 connect(m_margins, SIGNAL(leftChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int)));
251 connect(m_margins, SIGNAL(rightChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int)));
247 connect(m_margins, SIGNAL(rightChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int)));
252 connect(m_chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*)), this, SLOT(handleSeriesAdded(QAbstractSeries*)));
248 connect(m_chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*)), this, SLOT(handleSeriesAdded(QAbstractSeries*)));
253 connect(m_chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), this, SIGNAL(seriesRemoved(QAbstractSeries*)));
249 connect(m_chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), this, SIGNAL(seriesRemoved(QAbstractSeries*)));
254 }
250 }
255
251
256 void DeclarativeChart::handleSeriesAdded(QAbstractSeries *series)
252 void DeclarativeChart::handleSeriesAdded(QAbstractSeries *series)
257 {
253 {
258 emit seriesAdded(series);
254 emit seriesAdded(series);
259 }
255 }
260
256
261 void DeclarativeChart::changeMinimumMargins(int top, int bottom, int left, int right)
257 void DeclarativeChart::changeMinimumMargins(int top, int bottom, int left, int right)
262 {
258 {
263 m_chart->setMargins(QMargins(left, top, right, bottom));
259 m_chart->setMargins(QMargins(left, top, right, bottom));
264 emit minimumMarginsChanged();
260 emit minimumMarginsChanged();
265 emit plotAreaChanged(m_chart->plotArea());
261 emit plotAreaChanged(m_chart->plotArea());
266 }
262 }
267
263
268 DeclarativeChart::~DeclarativeChart()
264 DeclarativeChart::~DeclarativeChart()
269 {
265 {
270 delete m_chart;
266 delete m_chart;
271 }
267 }
272
268
273 void DeclarativeChart::childEvent(QChildEvent *event)
269 void DeclarativeChart::childEvent(QChildEvent *event)
274 {
270 {
275 if (event->type() == QEvent::ChildAdded) {
271 if (event->type() == QEvent::ChildAdded) {
276 if (qobject_cast<QAbstractSeries *>(event->child())) {
272 if (qobject_cast<QAbstractSeries *>(event->child())) {
277 m_chart->addSeries(qobject_cast<QAbstractSeries *>(event->child()));
273 m_chart->addSeries(qobject_cast<QAbstractSeries *>(event->child()));
278 }
274 }
279 }
275 }
280 }
276 }
281
277
282 void DeclarativeChart::componentComplete()
278 void DeclarativeChart::componentComplete()
283 {
279 {
284 foreach (QObject *child, children()) {
280 foreach (QObject *child, children()) {
285 if (qobject_cast<QAbstractSeries *>(child)) {
281 if (qobject_cast<QAbstractSeries *>(child)) {
286 // Add series to the chart
282 // Add series to the chart
287 QAbstractSeries *series = qobject_cast<QAbstractSeries *>(child);
283 QAbstractSeries *series = qobject_cast<QAbstractSeries *>(child);
288 m_chart->addSeries(series);
284 m_chart->addSeries(series);
289
285
290 // Connect to axis changed signals (unless this is a pie series)
286 // Connect to axis changed signals (unless this is a pie series)
291 if (!qobject_cast<DeclarativePieSeries *>(series)) {
287 if (!qobject_cast<DeclarativePieSeries *>(series)) {
292 connect(series, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
288 connect(series, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
293 connect(series, SIGNAL(axisXTopChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
289 connect(series, SIGNAL(axisXTopChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
294 connect(series, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*)));
290 connect(series, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*)));
295 connect(series, SIGNAL(axisYRightChanged(QAbstractAxis*)), this, SLOT(handleAxisYRightSet(QAbstractAxis*)));
291 connect(series, SIGNAL(axisYRightChanged(QAbstractAxis*)), this, SLOT(handleAxisYRightSet(QAbstractAxis*)));
296 }
292 }
297
293
298 initializeAxes(series);
294 initializeAxes(series);
299 }
295 }
300 }
296 }
301
297
302 QDeclarativeItem::componentComplete();
298 QDeclarativeItem::componentComplete();
303 }
299 }
304
300
305 void DeclarativeChart::handleAxisXSet(QAbstractAxis *axis)
301 void DeclarativeChart::handleAxisXSet(QAbstractAxis *axis)
306 {
302 {
307 QAbstractSeries *s = qobject_cast<QAbstractSeries *>(sender());
303 QAbstractSeries *s = qobject_cast<QAbstractSeries *>(sender());
308 if (axis && s) {
304 if (axis && s) {
309 if (!m_chart->axes(Qt::Horizontal).contains(axis))
305 if (!m_chart->axes(Qt::Horizontal).contains(axis))
310 m_chart->addAxis(axis, Qt::AlignBottom);
306 m_chart->addAxis(axis, Qt::AlignBottom);
311 if (!s->attachedAxes().contains(axis))
307 if (!s->attachedAxes().contains(axis))
312 s->attachAxis(axis);
308 s->attachAxis(axis);
313 } else {
309 } else {
314 qWarning() << "Trying to set axisX to null.";
310 qWarning() << "Trying to set axisX to null.";
315 }
311 }
316 }
312 }
317
313
318 void DeclarativeChart::handleAxisXTopSet(QAbstractAxis *axis)
314 void DeclarativeChart::handleAxisXTopSet(QAbstractAxis *axis)
319 {
315 {
320 QAbstractSeries *s = qobject_cast<QAbstractSeries *>(sender());
316 QAbstractSeries *s = qobject_cast<QAbstractSeries *>(sender());
321 if (axis && s) {
317 if (axis && s) {
322 if (!m_chart->axes(Qt::Horizontal).contains(axis))
318 if (!m_chart->axes(Qt::Horizontal).contains(axis))
323 m_chart->addAxis(axis, Qt::AlignTop);
319 m_chart->addAxis(axis, Qt::AlignTop);
324 if (!s->attachedAxes().contains(axis))
320 if (!s->attachedAxes().contains(axis))
325 s->attachAxis(axis);
321 s->attachAxis(axis);
326 } else {
322 } else {
327 qWarning() << "Trying to set axisXTop to null.";
323 qWarning() << "Trying to set axisXTop to null.";
328 }
324 }
329 }
325 }
330
326
331 void DeclarativeChart::handleAxisYSet(QAbstractAxis *axis)
327 void DeclarativeChart::handleAxisYSet(QAbstractAxis *axis)
332 {
328 {
333 QAbstractSeries *s = qobject_cast<QAbstractSeries *>(sender());
329 QAbstractSeries *s = qobject_cast<QAbstractSeries *>(sender());
334 if (axis && s) {
330 if (axis && s) {
335 if (!m_chart->axes(Qt::Vertical).contains(axis))
331 if (!m_chart->axes(Qt::Vertical).contains(axis))
336 m_chart->addAxis(axis, Qt::AlignLeft);
332 m_chart->addAxis(axis, Qt::AlignLeft);
337 if (!s->attachedAxes().contains(axis))
333 if (!s->attachedAxes().contains(axis))
338 s->attachAxis(axis);
334 s->attachAxis(axis);
339 } else {
335 } else {
340 qWarning() << "Trying to set axisY to null.";
336 qWarning() << "Trying to set axisY to null.";
341 }
337 }
342 }
338 }
343
339
344 void DeclarativeChart::handleAxisYRightSet(QAbstractAxis *axis)
340 void DeclarativeChart::handleAxisYRightSet(QAbstractAxis *axis)
345 {
341 {
346 QAbstractSeries *s = qobject_cast<QAbstractSeries *>(sender());
342 QAbstractSeries *s = qobject_cast<QAbstractSeries *>(sender());
347 if (axis && s) {
343 if (axis && s) {
348 if (!m_chart->axes(Qt::Vertical).contains(axis))
344 if (!m_chart->axes(Qt::Vertical).contains(axis))
349 m_chart->addAxis(axis, Qt::AlignRight);
345 m_chart->addAxis(axis, Qt::AlignRight);
350 if (!s->attachedAxes().contains(axis))
346 if (!s->attachedAxes().contains(axis))
351 s->attachAxis(axis);
347 s->attachAxis(axis);
352 } else {
348 } else {
353 qWarning() << "Trying to set axisYRight to null.";
349 qWarning() << "Trying to set axisYRight to null.";
354 }
350 }
355 }
351 }
356
352
357 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
353 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
358 {
354 {
359 // qDebug() << "DeclarativeChart::geometryChanged" << newGeometry.width() << newGeometry.height();
355 // qDebug() << "DeclarativeChart::geometryChanged" << newGeometry.width() << newGeometry.height();
360 if (newGeometry.isValid()) {
356 if (newGeometry.isValid()) {
361 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
357 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
362 m_chart->resize(newGeometry.width(), newGeometry.height());
358 m_chart->resize(newGeometry.width(), newGeometry.height());
363 }
359 }
364 }
360 }
365 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
361 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
366
362
367 // It would be better to trigger the plotAreaChanged signal from QChart::plotAreaChanged or
363 // It would be better to trigger the plotAreaChanged signal from QChart::plotAreaChanged or
368 // similar. Since that kind of a signal is not clearly needed in the C++ API the work-around is
364 // similar. Since that kind of a signal is not clearly needed in the C++ API the work-around is
369 // to implement it here for the QML API purposes.
365 // to implement it here for the QML API purposes.
370 emit plotAreaChanged(m_chart->plotArea());
366 emit plotAreaChanged(m_chart->plotArea());
371 }
367 }
372
368
373 void DeclarativeChart::setTheme(DeclarativeChart::Theme theme)
369 void DeclarativeChart::setTheme(DeclarativeChart::Theme theme)
374 {
370 {
375 QChart::ChartTheme chartTheme = (QChart::ChartTheme) theme;
371 QChart::ChartTheme chartTheme = (QChart::ChartTheme) theme;
376 if (chartTheme != m_chart->theme())
372 if (chartTheme != m_chart->theme())
377 m_chart->setTheme(chartTheme);
373 m_chart->setTheme(chartTheme);
378 }
374 }
379
375
380 DeclarativeChart::Theme DeclarativeChart::theme()
376 DeclarativeChart::Theme DeclarativeChart::theme()
381 {
377 {
382 return (DeclarativeChart::Theme) m_chart->theme();
378 return (DeclarativeChart::Theme) m_chart->theme();
383 }
379 }
384
380
385 void DeclarativeChart::setAnimationOptions(DeclarativeChart::Animation animations)
381 void DeclarativeChart::setAnimationOptions(DeclarativeChart::Animation animations)
386 {
382 {
387 QChart::AnimationOption animationOptions = (QChart::AnimationOption) animations;
383 QChart::AnimationOption animationOptions = (QChart::AnimationOption) animations;
388 if (animationOptions != m_chart->animationOptions())
384 if (animationOptions != m_chart->animationOptions())
389 m_chart->setAnimationOptions(animationOptions);
385 m_chart->setAnimationOptions(animationOptions);
390 }
386 }
391
387
392 DeclarativeChart::Animation DeclarativeChart::animationOptions()
388 DeclarativeChart::Animation DeclarativeChart::animationOptions()
393 {
389 {
394 if (m_chart->animationOptions().testFlag(QChart::AllAnimations))
390 if (m_chart->animationOptions().testFlag(QChart::AllAnimations))
395 return DeclarativeChart::AllAnimations;
391 return DeclarativeChart::AllAnimations;
396 else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations))
392 else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations))
397 return DeclarativeChart::GridAxisAnimations;
393 return DeclarativeChart::GridAxisAnimations;
398 else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations))
394 else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations))
399 return DeclarativeChart::SeriesAnimations;
395 return DeclarativeChart::SeriesAnimations;
400 else
396 else
401 return DeclarativeChart::NoAnimation;
397 return DeclarativeChart::NoAnimation;
402 }
398 }
403
399
404 void DeclarativeChart::setTitle(QString title)
400 void DeclarativeChart::setTitle(QString title)
405 {
401 {
406 if (title != m_chart->title())
402 if (title != m_chart->title())
407 m_chart->setTitle(title);
403 m_chart->setTitle(title);
408 }
404 }
409 QString DeclarativeChart::title()
405 QString DeclarativeChart::title()
410 {
406 {
411 return m_chart->title();
407 return m_chart->title();
412 }
408 }
413
409
414 QAbstractAxis *DeclarativeChart::axisX(QAbstractSeries *series)
410 QAbstractAxis *DeclarativeChart::axisX(QAbstractSeries *series)
415 {
411 {
416 QList<QAbstractAxis *> axes = m_chart->axes(Qt::Horizontal, series);
412 QList<QAbstractAxis *> axes = m_chart->axes(Qt::Horizontal, series);
417 if (axes.count())
413 if (axes.count())
418 return axes[0];
414 return axes[0];
419 return 0;
415 return 0;
420 }
416 }
421
417
422 QAbstractAxis *DeclarativeChart::axisY(QAbstractSeries *series)
418 QAbstractAxis *DeclarativeChart::axisY(QAbstractSeries *series)
423 {
419 {
424 QList<QAbstractAxis *> axes = m_chart->axes(Qt::Vertical, series);
420 QList<QAbstractAxis *> axes = m_chart->axes(Qt::Vertical, series);
425 if (axes.count())
421 if (axes.count())
426 return axes[0];
422 return axes[0];
427 return 0;
423 return 0;
428 }
424 }
429
425
430 QLegend *DeclarativeChart::legend()
426 QLegend *DeclarativeChart::legend()
431 {
427 {
432 return m_chart->legend();
428 return m_chart->legend();
433 }
429 }
434
430
435 void DeclarativeChart::setTitleColor(QColor color)
431 void DeclarativeChart::setTitleColor(QColor color)
436 {
432 {
437 QBrush b = m_chart->titleBrush();
433 QBrush b = m_chart->titleBrush();
438 if (color != b.color()) {
434 if (color != b.color()) {
439 b.setColor(color);
435 b.setColor(color);
440 m_chart->setTitleBrush(b);
436 m_chart->setTitleBrush(b);
441 emit titleColorChanged(color);
437 emit titleColorChanged(color);
442 }
438 }
443 }
439 }
444
440
445 QFont DeclarativeChart::titleFont() const
441 QFont DeclarativeChart::titleFont() const
446 {
442 {
447 return m_chart->titleFont();
443 return m_chart->titleFont();
448 }
444 }
449
445
450 void DeclarativeChart::setTitleFont(const QFont &font)
446 void DeclarativeChart::setTitleFont(const QFont &font)
451 {
447 {
452 m_chart->setTitleFont(font);
448 m_chart->setTitleFont(font);
453 }
449 }
454
450
455 QColor DeclarativeChart::titleColor()
451 QColor DeclarativeChart::titleColor()
456 {
452 {
457 return m_chart->titleBrush().color();
453 return m_chart->titleBrush().color();
458 }
454 }
459
455
460 void DeclarativeChart::setBackgroundColor(QColor color)
456 void DeclarativeChart::setBackgroundColor(QColor color)
461 {
457 {
462 QBrush b = m_chart->backgroundBrush();
458 QBrush b = m_chart->backgroundBrush();
463 if (b.style() != Qt::SolidPattern || color != b.color()) {
459 if (b.style() != Qt::SolidPattern || color != b.color()) {
464 b.setStyle(Qt::SolidPattern);
460 b.setStyle(Qt::SolidPattern);
465 b.setColor(color);
461 b.setColor(color);
466 m_chart->setBackgroundBrush(b);
462 m_chart->setBackgroundBrush(b);
467 emit backgroundColorChanged();
463 emit backgroundColorChanged();
468 }
464 }
469 }
465 }
470
466
471 QColor DeclarativeChart::backgroundColor()
467 QColor DeclarativeChart::backgroundColor()
472 {
468 {
473 return m_chart->backgroundBrush().color();
469 return m_chart->backgroundBrush().color();
474 }
470 }
475
471
476 int DeclarativeChart::count()
472 int DeclarativeChart::count()
477 {
473 {
478 return m_chart->series().count();
474 return m_chart->series().count();
479 }
475 }
480
476
481 void DeclarativeChart::setDropShadowEnabled(bool enabled)
477 void DeclarativeChart::setDropShadowEnabled(bool enabled)
482 {
478 {
483 if (enabled != m_chart->isDropShadowEnabled()) {
479 if (enabled != m_chart->isDropShadowEnabled()) {
484 m_chart->setDropShadowEnabled(enabled);
480 m_chart->setDropShadowEnabled(enabled);
485 dropShadowEnabledChanged(enabled);
481 dropShadowEnabledChanged(enabled);
486 }
482 }
487 }
483 }
488
484
489 bool DeclarativeChart::dropShadowEnabled()
485 bool DeclarativeChart::dropShadowEnabled()
490 {
486 {
491 return m_chart->isDropShadowEnabled();
487 return m_chart->isDropShadowEnabled();
492 }
488 }
493
489
494 qreal DeclarativeChart::topMargin()
490 qreal DeclarativeChart::topMargin()
495 {
491 {
496 qWarning() << "ChartView.topMargin is deprecated. Use minimumMargins and plotArea instead.";
492 qWarning() << "ChartView.topMargin is deprecated. Use minimumMargins and plotArea instead.";
497 return m_chart->plotArea().top();
493 return m_chart->plotArea().top();
498 }
494 }
499
495
500 qreal DeclarativeChart::bottomMargin()
496 qreal DeclarativeChart::bottomMargin()
501 {
497 {
502
498
503 qWarning() << "ChartView.bottomMargin is deprecated. Use minimumMargins and plotArea instead.";
499 qWarning() << "ChartView.bottomMargin is deprecated. Use minimumMargins and plotArea instead.";
504 return m_chart->plotArea().bottom();
500 return m_chart->plotArea().bottom();
505 }
501 }
506
502
507 qreal DeclarativeChart::leftMargin()
503 qreal DeclarativeChart::leftMargin()
508 {
504 {
509 qWarning() << "ChartView.leftMargin is deprecated. Use minimumMargins and plotArea instead.";
505 qWarning() << "ChartView.leftMargin is deprecated. Use minimumMargins and plotArea instead.";
510 return m_chart->plotArea().left();
506 return m_chart->plotArea().left();
511 }
507 }
512
508
513 qreal DeclarativeChart::rightMargin()
509 qreal DeclarativeChart::rightMargin()
514 {
510 {
515 qWarning() << "ChartView.rightMargin is deprecated. Use minimumMargins and plotArea instead.";
511 qWarning() << "ChartView.rightMargin is deprecated. Use minimumMargins and plotArea instead.";
516 return m_chart->plotArea().right();
512 return m_chart->plotArea().right();
517 }
513 }
518
514
519 void DeclarativeChart::zoom(qreal factor)
515 void DeclarativeChart::zoom(qreal factor)
520 {
516 {
521 m_chart->zoom(factor);
517 m_chart->zoom(factor);
522 }
518 }
523
519
524 void DeclarativeChart::scrollLeft(qreal pixels)
520 void DeclarativeChart::scrollLeft(qreal pixels)
525 {
521 {
526 m_chart->scroll(-pixels, 0);
522 m_chart->scroll(-pixels, 0);
527 }
523 }
528
524
529 void DeclarativeChart::scrollRight(qreal pixels)
525 void DeclarativeChart::scrollRight(qreal pixels)
530 {
526 {
531 m_chart->scroll(pixels, 0);
527 m_chart->scroll(pixels, 0);
532 }
528 }
533
529
534 void DeclarativeChart::scrollUp(qreal pixels)
530 void DeclarativeChart::scrollUp(qreal pixels)
535 {
531 {
536 m_chart->scroll(0, pixels);
532 m_chart->scroll(0, pixels);
537 }
533 }
538
534
539 void DeclarativeChart::scrollDown(qreal pixels)
535 void DeclarativeChart::scrollDown(qreal pixels)
540 {
536 {
541 m_chart->scroll(0, -pixels);
537 m_chart->scroll(0, -pixels);
542 }
538 }
543
539
544 QDeclarativeListProperty<QAbstractAxis> DeclarativeChart::axes()
540 QDeclarativeListProperty<QAbstractAxis> DeclarativeChart::axes()
545 {
541 {
546 return QDeclarativeListProperty<QAbstractAxis>(this, 0,
542 return QDeclarativeListProperty<QAbstractAxis>(this, 0,
547 &DeclarativeChart::axesAppendFunc,
543 &DeclarativeChart::axesAppendFunc,
548 &DeclarativeChart::axesCountFunc,
544 &DeclarativeChart::axesCountFunc,
549 &DeclarativeChart::axesAtFunc);
545 &DeclarativeChart::axesAtFunc);
550 }
546 }
551
547
552 void DeclarativeChart::axesAppendFunc(QDeclarativeListProperty<QAbstractAxis> *list, QAbstractAxis *element)
548 void DeclarativeChart::axesAppendFunc(QDeclarativeListProperty<QAbstractAxis> *list, QAbstractAxis *element)
553 {
549 {
554 // Empty implementation
550 // Empty implementation
555 Q_UNUSED(list);
551 Q_UNUSED(list);
556 Q_UNUSED(element);
552 Q_UNUSED(element);
557 }
553 }
558
554
559 int DeclarativeChart::axesCountFunc(QDeclarativeListProperty<QAbstractAxis> *list)
555 int DeclarativeChart::axesCountFunc(QDeclarativeListProperty<QAbstractAxis> *list)
560 {
556 {
561 if (qobject_cast<DeclarativeChart *>(list->object)) {
557 if (qobject_cast<DeclarativeChart *>(list->object)) {
562 DeclarativeChart *chart = qobject_cast<DeclarativeChart *>(list->object);
558 DeclarativeChart *chart = qobject_cast<DeclarativeChart *>(list->object);
563 return chart->m_chart->axes(Qt::Horizontal | Qt::Vertical).count();
559 return chart->m_chart->axes(Qt::Horizontal | Qt::Vertical).count();
564 }
560 }
565 return 0;
561 return 0;
566 }
562 }
567
563
568 QAbstractAxis *DeclarativeChart::axesAtFunc(QDeclarativeListProperty<QAbstractAxis> *list, int index)
564 QAbstractAxis *DeclarativeChart::axesAtFunc(QDeclarativeListProperty<QAbstractAxis> *list, int index)
569 {
565 {
570 if (qobject_cast<DeclarativeChart *>(list->object)) {
566 if (qobject_cast<DeclarativeChart *>(list->object)) {
571 DeclarativeChart *chart = qobject_cast<DeclarativeChart *>(list->object);
567 DeclarativeChart *chart = qobject_cast<DeclarativeChart *>(list->object);
572 QList<QAbstractAxis *> axes = chart->m_chart->axes(Qt::Horizontal | Qt::Vertical, chart->m_chart->series()[0]);
568 QList<QAbstractAxis *> axes = chart->m_chart->axes(Qt::Horizontal | Qt::Vertical, chart->m_chart->series()[0]);
573 return axes.at(index);
569 return axes.at(index);
574 }
570 }
575 return 0;
571 return 0;
576 }
572 }
577
573
578 QAbstractSeries *DeclarativeChart::series(int index)
574 QAbstractSeries *DeclarativeChart::series(int index)
579 {
575 {
580 if (index < m_chart->series().count()) {
576 if (index < m_chart->series().count()) {
581 return m_chart->series().at(index);
577 return m_chart->series().at(index);
582 }
578 }
583 return 0;
579 return 0;
584 }
580 }
585
581
586 QAbstractSeries *DeclarativeChart::series(QString seriesName)
582 QAbstractSeries *DeclarativeChart::series(QString seriesName)
587 {
583 {
588 foreach (QAbstractSeries *series, m_chart->series()) {
584 foreach (QAbstractSeries *series, m_chart->series()) {
589 if (series->name() == seriesName)
585 if (series->name() == seriesName)
590 return series;
586 return series;
591 }
587 }
592 return 0;
588 return 0;
593 }
589 }
594
590
595 QAbstractSeries *DeclarativeChart::createSeries(DeclarativeChart::SeriesType type, QString name)
591 QAbstractSeries *DeclarativeChart::createSeries(DeclarativeChart::SeriesType type, QString name)
596 {
592 {
597 return createSeries(type, name, 0, 0);
593 return createSeries(type, name, 0, 0);
598 }
594 }
599
595
600 QAbstractSeries *DeclarativeChart::createSeries(DeclarativeChart::SeriesType type, QString name, QAbstractAxis *axisX, QAbstractAxis *axisY)
596 QAbstractSeries *DeclarativeChart::createSeries(DeclarativeChart::SeriesType type, QString name, QAbstractAxis *axisX, QAbstractAxis *axisY)
601 {
597 {
602 QAbstractSeries *series = 0;
598 QAbstractSeries *series = 0;
603
599
604 switch (type) {
600 switch (type) {
605 case DeclarativeChart::SeriesTypeLine:
601 case DeclarativeChart::SeriesTypeLine:
606 series = new DeclarativeLineSeries();
602 series = new DeclarativeLineSeries();
607 break;
603 break;
608 case DeclarativeChart::SeriesTypeArea: {
604 case DeclarativeChart::SeriesTypeArea: {
609 DeclarativeAreaSeries *area = new DeclarativeAreaSeries();
605 DeclarativeAreaSeries *area = new DeclarativeAreaSeries();
610 area->setUpperSeries(new DeclarativeLineSeries());
606 area->setUpperSeries(new DeclarativeLineSeries());
611 series = area;
607 series = area;
612 break;
608 break;
613 }
609 }
614 case DeclarativeChart::SeriesTypeStackedBar:
610 case DeclarativeChart::SeriesTypeStackedBar:
615 series = new DeclarativeStackedBarSeries();
611 series = new DeclarativeStackedBarSeries();
616 break;
612 break;
617 case DeclarativeChart::SeriesTypePercentBar:
613 case DeclarativeChart::SeriesTypePercentBar:
618 series = new DeclarativePercentBarSeries();
614 series = new DeclarativePercentBarSeries();
619 break;
615 break;
620 case DeclarativeChart::SeriesTypeBar:
616 case DeclarativeChart::SeriesTypeBar:
621 series = new DeclarativeBarSeries();
617 series = new DeclarativeBarSeries();
622 break;
618 break;
623 case DeclarativeChart::SeriesTypeHorizontalBar:
619 case DeclarativeChart::SeriesTypeHorizontalBar:
624 series = new DeclarativeHorizontalBarSeries();
620 series = new DeclarativeHorizontalBarSeries();
625 break;
621 break;
626 case DeclarativeChart::SeriesTypeHorizontalPercentBar:
622 case DeclarativeChart::SeriesTypeHorizontalPercentBar:
627 series = new DeclarativeHorizontalPercentBarSeries();
623 series = new DeclarativeHorizontalPercentBarSeries();
628 break;
624 break;
629 case DeclarativeChart::SeriesTypeHorizontalStackedBar:
625 case DeclarativeChart::SeriesTypeHorizontalStackedBar:
630 series = new DeclarativeHorizontalStackedBarSeries();
626 series = new DeclarativeHorizontalStackedBarSeries();
631 break;
627 break;
632 case DeclarativeChart::SeriesTypePie:
628 case DeclarativeChart::SeriesTypePie:
633 series = new DeclarativePieSeries();
629 series = new DeclarativePieSeries();
634 break;
630 break;
635 case DeclarativeChart::SeriesTypeScatter:
631 case DeclarativeChart::SeriesTypeScatter:
636 series = new DeclarativeScatterSeries();
632 series = new DeclarativeScatterSeries();
637 break;
633 break;
638 case DeclarativeChart::SeriesTypeSpline:
634 case DeclarativeChart::SeriesTypeSpline:
639 series = new DeclarativeSplineSeries();
635 series = new DeclarativeSplineSeries();
640 break;
636 break;
641 default:
637 default:
642 qWarning() << "Illegal series type";
638 qWarning() << "Illegal series type";
643 }
639 }
644
640
645 if (series) {
641 if (series) {
646 // Connect to axis changed signals (unless this is a pie series)
642 // Connect to axis changed signals (unless this is a pie series)
647 if (!qobject_cast<DeclarativePieSeries *>(series)) {
643 if (!qobject_cast<DeclarativePieSeries *>(series)) {
648 connect(series, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
644 connect(series, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
649 connect(series, SIGNAL(axisXTopChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
645 connect(series, SIGNAL(axisXTopChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
650 connect(series, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*)));
646 connect(series, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*)));
651 connect(series, SIGNAL(axisYRightChanged(QAbstractAxis*)), this, SLOT(handleAxisYRightSet(QAbstractAxis*)));
647 connect(series, SIGNAL(axisYRightChanged(QAbstractAxis*)), this, SLOT(handleAxisYRightSet(QAbstractAxis*)));
652 }
648 }
653
649
654 series->setName(name);
650 series->setName(name);
655 m_chart->addSeries(series);
651 m_chart->addSeries(series);
656
652
657 if (axisX)
653 if (axisX)
658 setAxisX(axisX, series);
654 setAxisX(axisX, series);
659 if (axisY)
655 if (axisY)
660 setAxisY(axisY, series);
656 setAxisY(axisY, series);
661
657
662 if (series->attachedAxes().count() < 2)
658 if (series->attachedAxes().count() < 2)
663 initializeAxes(series);
659 initializeAxes(series);
664 }
660 }
665
661
666 return series;
662 return series;
667 }
663 }
668
664
669 void DeclarativeChart::removeSeries(QAbstractSeries *series)
665 void DeclarativeChart::removeSeries(QAbstractSeries *series)
670 {
666 {
671 if (series)
667 if (series)
672 m_chart->removeSeries(series);
668 m_chart->removeSeries(series);
673 else
669 else
674 qWarning("removeSeries: cannot remove null");
670 qWarning("removeSeries: cannot remove null");
675 }
671 }
676
672
677 void DeclarativeChart::setAxisX(QAbstractAxis *axis, QAbstractSeries *series)
673 void DeclarativeChart::setAxisX(QAbstractAxis *axis, QAbstractSeries *series)
678 {
674 {
679 if (axis)
675 if (axis)
680 m_chart->setAxisX(axis, series);
676 m_chart->setAxisX(axis, series);
681 }
677 }
682
678
683 void DeclarativeChart::setAxisY(QAbstractAxis *axis, QAbstractSeries *series)
679 void DeclarativeChart::setAxisY(QAbstractAxis *axis, QAbstractSeries *series)
684 {
680 {
685 if (axis)
681 if (axis)
686 m_chart->setAxisY(axis, series);
682 m_chart->setAxisY(axis, series);
687 }
683 }
688
684
689 void DeclarativeChart::createDefaultAxes()
685 void DeclarativeChart::createDefaultAxes()
690 {
686 {
691 qWarning() << "ChartView.createDefaultAxes() is deprecated. Axes are created automatically.";
687 qWarning() << "ChartView.createDefaultAxes() is deprecated. Axes are created automatically.";
692 }
688 }
693
689
694 QAbstractAxis *DeclarativeChart::defaultAxis(Qt::Orientation orientation, QAbstractSeries *series)
690 QAbstractAxis *DeclarativeChart::defaultAxis(Qt::Orientation orientation, QAbstractSeries *series)
695 {
691 {
696 if (!series) {
692 if (!series) {
697 qWarning() << "No axis type defined for null series";
693 qWarning() << "No axis type defined for null series";
698 return 0;
694 return 0;
699 }
695 }
700
696
701 foreach (QAbstractAxis *existingAxis, m_chart->axes(orientation)) {
697 foreach (QAbstractAxis *existingAxis, m_chart->axes(orientation)) {
702 if (existingAxis->type() == series->d_ptr->defaultAxisType(orientation))
698 if (existingAxis->type() == series->d_ptr->defaultAxisType(orientation))
703 return existingAxis;
699 return existingAxis;
704 }
700 }
705
701
706 switch (series->d_ptr->defaultAxisType(orientation)) {
702 switch (series->d_ptr->defaultAxisType(orientation)) {
707 case QAbstractAxis::AxisTypeValue:
703 case QAbstractAxis::AxisTypeValue:
708 return new QValueAxis(this);
704 return new QValueAxis(this);
709 case QAbstractAxis::AxisTypeBarCategory:
705 case QAbstractAxis::AxisTypeBarCategory:
710 return new QBarCategoryAxis(this);
706 return new QBarCategoryAxis(this);
711 case QAbstractAxis::AxisTypeCategory:
707 case QAbstractAxis::AxisTypeCategory:
712 return new QCategoryAxis(this);
708 return new QCategoryAxis(this);
713 #ifndef QT_ON_ARM
709 #ifndef QT_ON_ARM
714 case QAbstractAxis::AxisTypeDateTime:
710 case QAbstractAxis::AxisTypeDateTime:
715 return new QDateTimeAxis(this);
711 return new QDateTimeAxis(this);
716 #endif
712 #endif
717 default:
713 default:
718 // assume AxisTypeNoAxis
714 // assume AxisTypeNoAxis
719 return 0;
715 return 0;
720 }
716 }
721 }
717 }
722
718
723 void DeclarativeChart::initializeAxes(QAbstractSeries *series)
719 void DeclarativeChart::initializeAxes(QAbstractSeries *series)
724 {
720 {
725 if (qobject_cast<DeclarativeLineSeries *>(series))
721 if (qobject_cast<DeclarativeLineSeries *>(series))
726 doInitializeAxes(series, qobject_cast<DeclarativeLineSeries *>(series)->m_axes);
722 doInitializeAxes(series, qobject_cast<DeclarativeLineSeries *>(series)->m_axes);
727 else if (qobject_cast<DeclarativeScatterSeries *>(series))
723 else if (qobject_cast<DeclarativeScatterSeries *>(series))
728 doInitializeAxes(series, qobject_cast<DeclarativeScatterSeries *>(series)->m_axes);
724 doInitializeAxes(series, qobject_cast<DeclarativeScatterSeries *>(series)->m_axes);
729 else if (qobject_cast<DeclarativeSplineSeries *>(series))
725 else if (qobject_cast<DeclarativeSplineSeries *>(series))
730 doInitializeAxes(series, qobject_cast<DeclarativeSplineSeries *>(series)->m_axes);
726 doInitializeAxes(series, qobject_cast<DeclarativeSplineSeries *>(series)->m_axes);
731 else if (qobject_cast<DeclarativeAreaSeries *>(series))
727 else if (qobject_cast<DeclarativeAreaSeries *>(series))
732 doInitializeAxes(series, qobject_cast<DeclarativeAreaSeries *>(series)->m_axes);
728 doInitializeAxes(series, qobject_cast<DeclarativeAreaSeries *>(series)->m_axes);
733 else if (qobject_cast<DeclarativeBarSeries *>(series))
729 else if (qobject_cast<DeclarativeBarSeries *>(series))
734 doInitializeAxes(series, qobject_cast<DeclarativeBarSeries *>(series)->m_axes);
730 doInitializeAxes(series, qobject_cast<DeclarativeBarSeries *>(series)->m_axes);
735 else if (qobject_cast<DeclarativeStackedBarSeries *>(series))
731 else if (qobject_cast<DeclarativeStackedBarSeries *>(series))
736 doInitializeAxes(series, qobject_cast<DeclarativeStackedBarSeries *>(series)->m_axes);
732 doInitializeAxes(series, qobject_cast<DeclarativeStackedBarSeries *>(series)->m_axes);
737 else if (qobject_cast<DeclarativePercentBarSeries *>(series))
733 else if (qobject_cast<DeclarativePercentBarSeries *>(series))
738 doInitializeAxes(series, qobject_cast<DeclarativePercentBarSeries *>(series)->m_axes);
734 doInitializeAxes(series, qobject_cast<DeclarativePercentBarSeries *>(series)->m_axes);
739 else if (qobject_cast<DeclarativeHorizontalBarSeries *>(series))
735 else if (qobject_cast<DeclarativeHorizontalBarSeries *>(series))
740 doInitializeAxes(series, qobject_cast<DeclarativeHorizontalBarSeries *>(series)->m_axes);
736 doInitializeAxes(series, qobject_cast<DeclarativeHorizontalBarSeries *>(series)->m_axes);
741 else if (qobject_cast<DeclarativeHorizontalStackedBarSeries *>(series))
737 else if (qobject_cast<DeclarativeHorizontalStackedBarSeries *>(series))
742 doInitializeAxes(series, qobject_cast<DeclarativeHorizontalStackedBarSeries *>(series)->m_axes);
738 doInitializeAxes(series, qobject_cast<DeclarativeHorizontalStackedBarSeries *>(series)->m_axes);
743 else if (qobject_cast<DeclarativeHorizontalPercentBarSeries *>(series))
739 else if (qobject_cast<DeclarativeHorizontalPercentBarSeries *>(series))
744 doInitializeAxes(series, qobject_cast<DeclarativeHorizontalPercentBarSeries *>(series)->m_axes);
740 doInitializeAxes(series, qobject_cast<DeclarativeHorizontalPercentBarSeries *>(series)->m_axes);
745 // else: do nothing
741 // else: do nothing
746 }
742 }
747
743
748 void DeclarativeChart::doInitializeAxes(QAbstractSeries *series, DeclarativeAxes *axes)
744 void DeclarativeChart::doInitializeAxes(QAbstractSeries *series, DeclarativeAxes *axes)
749 {
745 {
750 // Initialize axis X
746 // Initialize axis X
751 if (axes->axisX())
747 if (axes->axisX())
752 axes->emitAxisXChanged();
748 axes->emitAxisXChanged();
753 else if (axes->axisXTop())
749 else if (axes->axisXTop())
754 axes->emitAxisXTopChanged();
750 axes->emitAxisXTopChanged();
755 else
751 else
756 axes->setAxisX(defaultAxis(Qt::Horizontal, series));
752 axes->setAxisX(defaultAxis(Qt::Horizontal, series));
757
753
758 // Initialize axis Y
754 // Initialize axis Y
759 if (axes->axisY())
755 if (axes->axisY())
760 axes->emitAxisYChanged();
756 axes->emitAxisYChanged();
761 else if (axes->axisYRight())
757 else if (axes->axisYRight())
762 axes->emitAxisYRightChanged();
758 axes->emitAxisYRightChanged();
763 else
759 else
764 axes->setAxisY(defaultAxis(Qt::Vertical, series));
760 axes->setAxisY(defaultAxis(Qt::Vertical, series));
765 }
761 }
766
762
767 #include "moc_declarativechart.cpp"
763 #include "moc_declarativechart.cpp"
768
764
769 QTCOMMERCIALCHART_END_NAMESPACE
765 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,599 +1,598
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 "qbarcategoryaxis.h"
21 #include "qbarcategoryaxis.h"
22 #include "qbarcategoryaxis_p.h"
22 #include "qbarcategoryaxis_p.h"
23 #include "chartbarcategoryaxisx_p.h"
23 #include "chartbarcategoryaxisx_p.h"
24 #include "chartbarcategoryaxisy_p.h"
24 #include "chartbarcategoryaxisy_p.h"
25 #include "abstractdomain_p.h"
25 #include "abstractdomain_p.h"
26 #include "qchart.h"
26 #include "qchart.h"
27 #include <qmath.h>
27 #include <qmath.h>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 /*!
30 /*!
31 \class QBarCategoryAxis
31 \class QBarCategoryAxis
32 \brief The QBarCategoryAxis class is used for manipulating chart's axis.
32 \brief The QBarCategoryAxis class is used for manipulating chart's axis.
33 \mainclass
33 \mainclass
34
34
35 BarCategoryAxis can be setup to show axis line with tick marks, grid lines and shades.
35 BarCategoryAxis can be setup to show axis line with tick marks, grid lines and shades.
36 Categories are drawn between ticks. Note that you can use this also with lineseries too.
36 Categories are drawn between ticks. Note that you can use this also with lineseries too.
37 See the \l {Line and BarChart Example} {Line and BarChart Example} to learn how to do that.
37 See the \l {Line and BarChart Example} {Line and BarChart Example} to learn how to do that.
38
38
39 Example code on how to use QBarCategoryAxis.
39 Example code on how to use QBarCategoryAxis.
40 \code
40 \code
41 QChartView *chartView = new QChartView;
41 QChartView *chartView = new QChartView;
42 QBarSeries *series = new QBarSeries;
42 QBarSeries *series = new QBarSeries;
43 // ...
43 // ...
44 chartView->chart()->addSeries(series);
44 chartView->chart()->addSeries(series);
45 chartView->chart()->createDefaultAxes();
45 chartView->chart()->createDefaultAxes();
46
46
47 QBarCategoryAxis *axisX = new QBarCategoryAxis;
47 QBarCategoryAxis *axisX = new QBarCategoryAxis;
48 QStringList categories;
48 QStringList categories;
49 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
49 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
50 axisX->append(categories);
50 axisX->append(categories);
51 axisX->setRange("Feb", "May");
51 axisX->setRange("Feb", "May");
52 chartView->chart()->setAxisX(axisX, series);
52 chartView->chart()->setAxisX(axisX, series);
53 \endcode
53 \endcode
54 */
54 */
55
55
56 /*!
56 /*!
57 \qmlclass BarCategoryAxis QBarCategoryAxis
57 \qmlclass BarCategoryAxis QBarCategoryAxis
58 \inherits AbstractAxis
58 \inherits AbstractAxis
59 \brief The Axis element is used for manipulating chart's axes.
59 \brief The Axis element is used for manipulating chart's axes.
60
60
61 Axis can be setup to show axis line with tick marks, grid lines and shades.
61 Axis can be setup to show axis line with tick marks, grid lines and shades.
62 Categories are drawn between ticks. Note that you can use this also with lineseries too.
62 Categories are drawn between ticks. Note that you can use this also with lineseries too.
63
63
64 To access BarCategoryAxis you can use ChartView API. For example:
64 To access BarCategoryAxis you can use ChartView API. For example:
65 \code
65 \code
66 ChartView {
66 ChartView {
67 BarCategoryAxis {
67 BarCategoryAxis {
68 id: categoryAxis
68 id: categoryAxis
69 categories: ["Jan", "Feb", "Mar", "Apr", "May", "Jun" ]
69 categories: ["Jan", "Feb", "Mar", "Apr", "May", "Jun" ]
70 }
70 }
71 // Add a few series...
71 // Add a few series...
72 }
72 }
73 \endcode
73 \endcode
74 */
74 */
75
75
76 /*!
76 /*!
77 \property QBarCategoryAxis::categories
77 \property QBarCategoryAxis::categories
78 Defines the categories of axis
78 Defines the categories of axis
79 */
79 */
80 /*!
80 /*!
81 \qmlproperty QStringList BarCategoryAxis::categories
81 \qmlproperty QStringList BarCategoryAxis::categories
82 Defines the categories of axis
82 Defines the categories of axis
83 */
83 */
84
84
85 /*!
85 /*!
86 \property QBarCategoryAxis::min
86 \property QBarCategoryAxis::min
87 Defines the minimum value on the axis.
87 Defines the minimum value on the axis.
88 */
88 */
89 /*!
89 /*!
90 \qmlproperty QString Bar if (d->m_chart)
90 \qmlproperty string BarCategoryAxis::min
91 d->m_chart->removeAxis(this);CategoryAxis::min
92 Defines the minimum value on the axis.
91 Defines the minimum value on the axis.
93 */
92 */
94
93
95 /*!
94 /*!
96 \property QBarCategoryAxis::max
95 \property QBarCategoryAxis::max
97 Defines the maximum value on the axis.
96 Defines the maximum value on the axis.
98 */
97 */
99 /*!
98 /*!
100 \qmlproperty QString BarCategoryAxis::max
99 \qmlproperty string BarCategoryAxis::max
101 Defines the maximum value on the axis.
100 Defines the maximum value on the axis.
102 */
101 */
103
102
104 /*!
103 /*!
105 \property QBarCategoryAxis::count
104 \property QBarCategoryAxis::count
106 The count of categories.
105 The count of categories.
107 */
106 */
108 /*!
107 /*!
109 \qmlproperty int BarCategoryAxis::count
108 \qmlproperty int BarCategoryAxis::count
110 The count of categories.
109 The count of categories.
111 */
110 */
112
111
113 /*!
112 /*!
114 \fn void QBarCategoryAxis::categoriesChanged()
113 \fn void QBarCategoryAxis::categoriesChanged()
115 Axis emits signal when the categories of the axis has changed.
114 Axis emits signal when the categories of the axis has changed.
116 */
115 */
117
116
118 /*!
117 /*!
119 \fn void QBarCategoryAxis::minChanged(const QString &min)
118 \fn void QBarCategoryAxis::minChanged(const QString &min)
120 Axis emits signal when \a min of axis has changed.
119 Axis emits signal when \a min of axis has changed.
121 */
120 */
122 /*!
121 /*!
123 \qmlsignal BarCategoryAxis::onMinChanged(const QString &min)
122 \qmlsignal BarCategoryAxis::onMinChanged(const QString &min)
124 Axis emits signal when \a min of axis has changed.
123 Axis emits signal when \a min of axis has changed.
125 */
124 */
126
125
127 /*!
126 /*!
128 \fn void QBarCategoryAxis::maxChanged(const QString &max)
127 \fn void QBarCategoryAxis::maxChanged(const QString &max)
129 Axis emits signal when \a max of axis has changed.
128 Axis emits signal when \a max of axis has changed.
130 */
129 */
131 /*!
130 /*!
132 \qmlsignal BarCategoryAxis::onMaxChanged(const QString &max)
131 \qmlsignal BarCategoryAxis::onMaxChanged(const QString &max)
133 Axis emits signal when \a max of axis has changed.
132 Axis emits signal when \a max of axis has changed.
134 */
133 */
135
134
136 /*!
135 /*!
137 \fn void QBarCategoryAxis::countChanged()
136 \fn void QBarCategoryAxis::countChanged()
138 Axis emits signal when the count of categories has changed.
137 Axis emits signal when the count of categories has changed.
139 */
138 */
140 /*!
139 /*!
141 \qmlsignal BarCategoryAxis::onCountChanged()
140 \qmlsignal BarCategoryAxis::onCountChanged()
142 Axis emits signal when the count of categories has changed.
141 Axis emits signal when the count of categories has changed.
143 */
142 */
144
143
145 /*!
144 /*!
146 \fn void QBarCategoryAxis::rangeChanged(const QString &min, const QString &max)
145 \fn void QBarCategoryAxis::rangeChanged(const QString &min, const QString &max)
147 Axis emits signal when \a min or \a max of axis has changed.
146 Axis emits signal when \a min or \a max of axis has changed.
148 */
147 */
149
148
150 /*!
149 /*!
151 Constructs an axis object which is a child of \a parent.
150 Constructs an axis object which is a child of \a parent.
152 */
151 */
153 QBarCategoryAxis::QBarCategoryAxis(QObject *parent):
152 QBarCategoryAxis::QBarCategoryAxis(QObject *parent):
154 QAbstractAxis(*new QBarCategoryAxisPrivate(this), parent)
153 QAbstractAxis(*new QBarCategoryAxisPrivate(this), parent)
155 {
154 {
156 }
155 }
157
156
158 /*!
157 /*!
159 Destroys the object
158 Destroys the object
160 */
159 */
161 QBarCategoryAxis::~QBarCategoryAxis()
160 QBarCategoryAxis::~QBarCategoryAxis()
162 {
161 {
163 Q_D(QBarCategoryAxis);
162 Q_D(QBarCategoryAxis);
164 if (d->m_chart)
163 if (d->m_chart)
165 d->m_chart->removeAxis(this);
164 d->m_chart->removeAxis(this);
166 }
165 }
167
166
168 /*!
167 /*!
169 \internal
168 \internal
170 */
169 */
171 QBarCategoryAxis::QBarCategoryAxis(QBarCategoryAxisPrivate &d, QObject *parent)
170 QBarCategoryAxis::QBarCategoryAxis(QBarCategoryAxisPrivate &d, QObject *parent)
172 : QAbstractAxis(d, parent)
171 : QAbstractAxis(d, parent)
173 {
172 {
174
173
175 }
174 }
176
175
177 /*!
176 /*!
178 Appends \a categories to axis. A maximum of the axis will be changed to last category in \a categories.
177 Appends \a categories to axis. A maximum of the axis will be changed to last category in \a categories.
179 If there were no categories previously defined, minimum of axis will be also changed to first category in \a categories.
178 If there were no categories previously defined, minimum of axis will be also changed to first category in \a categories.
180 A category has to be valid QStrings and can not be duplicated. Duplicated categories will not be appended.
179 A category has to be valid QStrings and can not be duplicated. Duplicated categories will not be appended.
181 */
180 */
182 void QBarCategoryAxis::append(const QStringList &categories)
181 void QBarCategoryAxis::append(const QStringList &categories)
183 {
182 {
184 if (categories.isEmpty())
183 if (categories.isEmpty())
185 return;
184 return;
186
185
187 Q_D(QBarCategoryAxis);
186 Q_D(QBarCategoryAxis);
188
187
189 int count = d->m_categories.count();
188 int count = d->m_categories.count();
190
189
191 foreach(QString category, categories) {
190 foreach(QString category, categories) {
192 if (!d->m_categories.contains(category) && !category.isNull()) {
191 if (!d->m_categories.contains(category) && !category.isNull()) {
193 d->m_categories.append(category);
192 d->m_categories.append(category);
194 }
193 }
195 }
194 }
196
195
197 if (d->m_categories.count() == count)
196 if (d->m_categories.count() == count)
198 return;
197 return;
199
198
200 if (count == 0)
199 if (count == 0)
201 setRange(d->m_categories.first(), d->m_categories.last());
200 setRange(d->m_categories.first(), d->m_categories.last());
202 else
201 else
203 setRange(d->m_minCategory, d->m_categories.last());
202 setRange(d->m_minCategory, d->m_categories.last());
204
203
205 emit categoriesChanged();
204 emit categoriesChanged();
206 emit countChanged();
205 emit countChanged();
207 }
206 }
208
207
209 /*!
208 /*!
210 Appends \a category to axis. A maximum of the axis will be changed to last \a category.
209 Appends \a category to axis. A maximum of the axis will be changed to last \a category.
211 If there were no categories previously defined, minimum of axis will be also changed to \a category.
210 If there were no categories previously defined, minimum of axis will be also changed to \a category.
212 A \a category has to be valid QStrings and can not be duplicated. Duplicated categories will not be appended.
211 A \a category has to be valid QStrings and can not be duplicated. Duplicated categories will not be appended.
213 */
212 */
214 void QBarCategoryAxis::append(const QString &category)
213 void QBarCategoryAxis::append(const QString &category)
215 {
214 {
216 Q_D(QBarCategoryAxis);
215 Q_D(QBarCategoryAxis);
217
216
218 int count = d->m_categories.count();
217 int count = d->m_categories.count();
219
218
220 if (!d->m_categories.contains(category) && !category.isNull())
219 if (!d->m_categories.contains(category) && !category.isNull())
221 d->m_categories.append(category);
220 d->m_categories.append(category);
222
221
223 if (d->m_categories.count() == count)
222 if (d->m_categories.count() == count)
224 return;
223 return;
225
224
226 if (count == 0)
225 if (count == 0)
227 setRange(d->m_categories.last(), d->m_categories.last());
226 setRange(d->m_categories.last(), d->m_categories.last());
228 else
227 else
229 setRange(d->m_minCategory, d->m_categories.last());
228 setRange(d->m_minCategory, d->m_categories.last());
230
229
231 emit categoriesChanged();
230 emit categoriesChanged();
232 emit countChanged();
231 emit countChanged();
233 }
232 }
234
233
235 /*!
234 /*!
236 Removes \a category from axis. Removing category which is currently maximum or minimum
235 Removes \a category from axis. Removing category which is currently maximum or minimum
237 will affect the axis range.
236 will affect the axis range.
238 */
237 */
239 void QBarCategoryAxis::remove(const QString &category)
238 void QBarCategoryAxis::remove(const QString &category)
240 {
239 {
241 Q_D(QBarCategoryAxis);
240 Q_D(QBarCategoryAxis);
242
241
243 if (d->m_categories.contains(category)) {
242 if (d->m_categories.contains(category)) {
244 d->m_categories.removeAt(d->m_categories.indexOf(category));
243 d->m_categories.removeAt(d->m_categories.indexOf(category));
245 if (!d->m_categories.isEmpty()) {
244 if (!d->m_categories.isEmpty()) {
246 if (d->m_minCategory == category) {
245 if (d->m_minCategory == category) {
247 setRange(d->m_categories.first(), d->m_maxCategory);
246 setRange(d->m_categories.first(), d->m_maxCategory);
248 } else if (d->m_maxCategory == category) {
247 } else if (d->m_maxCategory == category) {
249 setRange(d->m_minCategory, d->m_categories.last());
248 setRange(d->m_minCategory, d->m_categories.last());
250 } else {
249 } else {
251 d->updateCategoryDomain();
250 d->updateCategoryDomain();
252 //TODO:: d->emitUpdated();
251 //TODO:: d->emitUpdated();
253 }
252 }
254 } else {
253 } else {
255 setRange(QString::null, QString::null);
254 setRange(QString::null, QString::null);
256 }
255 }
257 emit categoriesChanged();
256 emit categoriesChanged();
258 emit countChanged();
257 emit countChanged();
259 }
258 }
260 }
259 }
261
260
262 /*!
261 /*!
263 Inserts \a category to axis at \a index. A \a category has to be valid QStrings and can not be duplicated.
262 Inserts \a category to axis at \a index. A \a category has to be valid QStrings and can not be duplicated.
264 If \a category is prepended or appended to categories, minimum and maximum of axis is updated accordingly.
263 If \a category is prepended or appended to categories, minimum and maximum of axis is updated accordingly.
265 */
264 */
266 void QBarCategoryAxis::insert(int index, const QString &category)
265 void QBarCategoryAxis::insert(int index, const QString &category)
267 {
266 {
268 Q_D(QBarCategoryAxis);
267 Q_D(QBarCategoryAxis);
269
268
270 int count = d->m_categories.count();
269 int count = d->m_categories.count();
271
270
272 if (!d->m_categories.contains(category) && !category.isNull())
271 if (!d->m_categories.contains(category) && !category.isNull())
273 d->m_categories.insert(index, category);
272 d->m_categories.insert(index, category);
274
273
275 if (d->m_categories.count() == count)
274 if (d->m_categories.count() == count)
276 return;
275 return;
277
276
278 if (count == 0) {
277 if (count == 0) {
279 setRange(d->m_categories.first(), d->m_categories.first());
278 setRange(d->m_categories.first(), d->m_categories.first());
280 } else if (index == 0) {
279 } else if (index == 0) {
281 setRange(d->m_categories.first(), d->m_maxCategory);
280 setRange(d->m_categories.first(), d->m_maxCategory);
282 } else if (index == count) {
281 } else if (index == count) {
283 setRange(d->m_minCategory, d->m_categories.last());
282 setRange(d->m_minCategory, d->m_categories.last());
284 } else {
283 } else {
285 d->updateCategoryDomain();
284 d->updateCategoryDomain();
286 //TODO:: d->emitUpdated();
285 //TODO:: d->emitUpdated();
287 }
286 }
288
287
289 emit categoriesChanged();
288 emit categoriesChanged();
290 emit countChanged();
289 emit countChanged();
291 }
290 }
292
291
293 /*!
292 /*!
294 Replaces \a oldCategory with \a newCategory. If \a oldCategory does not exits on the axis nothing is done.
293 Replaces \a oldCategory with \a newCategory. If \a oldCategory does not exits on the axis nothing is done.
295 A \a newCategory has to be valid QStrings and can not be duplicated. In case of replacing minimum or maximum category,
294 A \a newCategory has to be valid QStrings and can not be duplicated. In case of replacing minimum or maximum category,
296 minimum and maximum of axis is updated accordingly.
295 minimum and maximum of axis is updated accordingly.
297 */
296 */
298 void QBarCategoryAxis::replace(const QString &oldCategory, const QString &newCategory)
297 void QBarCategoryAxis::replace(const QString &oldCategory, const QString &newCategory)
299 {
298 {
300 Q_D(QBarCategoryAxis);
299 Q_D(QBarCategoryAxis);
301
300
302 int pos = d->m_categories.indexOf(oldCategory);
301 int pos = d->m_categories.indexOf(oldCategory);
303
302
304 if (pos != -1 && !d->m_categories.contains(newCategory) && !newCategory.isNull()) {
303 if (pos != -1 && !d->m_categories.contains(newCategory) && !newCategory.isNull()) {
305 d->m_categories.replace(pos, newCategory);
304 d->m_categories.replace(pos, newCategory);
306 if (d->m_minCategory == oldCategory) {
305 if (d->m_minCategory == oldCategory) {
307 setRange(newCategory, d->m_maxCategory);
306 setRange(newCategory, d->m_maxCategory);
308 } else if (d->m_maxCategory == oldCategory) {
307 } else if (d->m_maxCategory == oldCategory) {
309 setRange(d->m_minCategory, newCategory);
308 setRange(d->m_minCategory, newCategory);
310 } else {
309 } else {
311 //TODO:: d->emitUpdated();
310 //TODO:: d->emitUpdated();
312 }
311 }
313 emit categoriesChanged();
312 emit categoriesChanged();
314 emit countChanged();
313 emit countChanged();
315 }
314 }
316 }
315 }
317
316
318 /*!
317 /*!
319 Removes all categories. Sets the maximum and minimum of the axis's range to QString::null.
318 Removes all categories. Sets the maximum and minimum of the axis's range to QString::null.
320 */
319 */
321 void QBarCategoryAxis::clear()
320 void QBarCategoryAxis::clear()
322 {
321 {
323 Q_D(QBarCategoryAxis);
322 Q_D(QBarCategoryAxis);
324 d->m_categories.clear();
323 d->m_categories.clear();
325 setRange(QString::null, QString::null);
324 setRange(QString::null, QString::null);
326 emit categoriesChanged();
325 emit categoriesChanged();
327 emit countChanged();
326 emit countChanged();
328 }
327 }
329
328
330 /*!
329 /*!
331 Set \a categories and discards the old ones, range of axis is adjusted to match first and last category in \a categories.
330 Set \a categories and discards the old ones, range of axis is adjusted to match first and last category in \a categories.
332 A category has to be valid QStrings and can not be duplicated.
331 A category has to be valid QStrings and can not be duplicated.
333 */
332 */
334 void QBarCategoryAxis::setCategories(const QStringList &categories)
333 void QBarCategoryAxis::setCategories(const QStringList &categories)
335 {
334 {
336 Q_D(QBarCategoryAxis);
335 Q_D(QBarCategoryAxis);
337 d->m_categories.clear();
336 d->m_categories.clear();
338 d->m_minCategory = QString::null;
337 d->m_minCategory = QString::null;
339 d->m_maxCategory = QString::null;
338 d->m_maxCategory = QString::null;
340 d->m_min = 0;
339 d->m_min = 0;
341 d->m_max = 0;
340 d->m_max = 0;
342 d->m_count = 0;
341 d->m_count = 0;
343 append(categories);
342 append(categories);
344 }
343 }
345
344
346 /*!
345 /*!
347 Returns categories
346 Returns categories
348 */
347 */
349 QStringList QBarCategoryAxis::categories()
348 QStringList QBarCategoryAxis::categories()
350 {
349 {
351 Q_D(QBarCategoryAxis);
350 Q_D(QBarCategoryAxis);
352 return d->m_categories;
351 return d->m_categories;
353 }
352 }
354
353
355 /*!
354 /*!
356 Returns number of categories.
355 Returns number of categories.
357 */
356 */
358 int QBarCategoryAxis::count() const
357 int QBarCategoryAxis::count() const
359 {
358 {
360 Q_D(const QBarCategoryAxis);
359 Q_D(const QBarCategoryAxis);
361 return d->m_categories.count();
360 return d->m_categories.count();
362 }
361 }
363
362
364 /*!
363 /*!
365 Returns category at \a index. Index must be valid.
364 Returns category at \a index. Index must be valid.
366 */
365 */
367 QString QBarCategoryAxis::at(int index) const
366 QString QBarCategoryAxis::at(int index) const
368 {
367 {
369 Q_D(const QBarCategoryAxis);
368 Q_D(const QBarCategoryAxis);
370 return d->m_categories.at(index);
369 return d->m_categories.at(index);
371 }
370 }
372
371
373 /*!
372 /*!
374 Sets minimum category to \a min.
373 Sets minimum category to \a min.
375 */
374 */
376 void QBarCategoryAxis::setMin(const QString &min)
375 void QBarCategoryAxis::setMin(const QString &min)
377 {
376 {
378 Q_D(QBarCategoryAxis);
377 Q_D(QBarCategoryAxis);
379 d->setRange(min, d->m_maxCategory);
378 d->setRange(min, d->m_maxCategory);
380 }
379 }
381
380
382 /*!
381 /*!
383 Returns minimum category.
382 Returns minimum category.
384 */
383 */
385 QString QBarCategoryAxis::min() const
384 QString QBarCategoryAxis::min() const
386 {
385 {
387 Q_D(const QBarCategoryAxis);
386 Q_D(const QBarCategoryAxis);
388 return d->m_minCategory;
387 return d->m_minCategory;
389 }
388 }
390
389
391 /*!
390 /*!
392 Sets maximum category to \a max.
391 Sets maximum category to \a max.
393 */
392 */
394 void QBarCategoryAxis::setMax(const QString &max)
393 void QBarCategoryAxis::setMax(const QString &max)
395 {
394 {
396 Q_D(QBarCategoryAxis);
395 Q_D(QBarCategoryAxis);
397 d->setRange(d->m_minCategory, max);
396 d->setRange(d->m_minCategory, max);
398 }
397 }
399
398
400 /*!
399 /*!
401 Returns maximum category
400 Returns maximum category
402 */
401 */
403 QString QBarCategoryAxis::max() const
402 QString QBarCategoryAxis::max() const
404 {
403 {
405 Q_D(const QBarCategoryAxis);
404 Q_D(const QBarCategoryAxis);
406 return d->m_maxCategory;
405 return d->m_maxCategory;
407 }
406 }
408
407
409 /*!
408 /*!
410 Sets range from \a minCategory to \a maxCategory
409 Sets range from \a minCategory to \a maxCategory
411 */
410 */
412 void QBarCategoryAxis::setRange(const QString &minCategory, const QString &maxCategory)
411 void QBarCategoryAxis::setRange(const QString &minCategory, const QString &maxCategory)
413 {
412 {
414 Q_D(QBarCategoryAxis);
413 Q_D(QBarCategoryAxis);
415 d->setRange(minCategory,maxCategory);
414 d->setRange(minCategory,maxCategory);
416 }
415 }
417
416
418 /*!
417 /*!
419 Returns the type of the axis
418 Returns the type of the axis
420 */
419 */
421 QAbstractAxis::AxisType QBarCategoryAxis::type() const
420 QAbstractAxis::AxisType QBarCategoryAxis::type() const
422 {
421 {
423 return AxisTypeBarCategory;
422 return AxisTypeBarCategory;
424 }
423 }
425
424
426 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
425 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
427
426
428 QBarCategoryAxisPrivate::QBarCategoryAxisPrivate(QBarCategoryAxis *q)
427 QBarCategoryAxisPrivate::QBarCategoryAxisPrivate(QBarCategoryAxis *q)
429 : QAbstractAxisPrivate(q),
428 : QAbstractAxisPrivate(q),
430 m_min(0.0),
429 m_min(0.0),
431 m_max(0.0),
430 m_max(0.0),
432 m_count(0)
431 m_count(0)
433 {
432 {
434
433
435 }
434 }
436
435
437 QBarCategoryAxisPrivate::~QBarCategoryAxisPrivate()
436 QBarCategoryAxisPrivate::~QBarCategoryAxisPrivate()
438 {
437 {
439
438
440 }
439 }
441
440
442 void QBarCategoryAxisPrivate::setMin(const QVariant &min)
441 void QBarCategoryAxisPrivate::setMin(const QVariant &min)
443 {
442 {
444 setRange(min, m_maxCategory);
443 setRange(min, m_maxCategory);
445 }
444 }
446
445
447 void QBarCategoryAxisPrivate::setMax(const QVariant &max)
446 void QBarCategoryAxisPrivate::setMax(const QVariant &max)
448 {
447 {
449 setRange(m_minCategory, max);
448 setRange(m_minCategory, max);
450 }
449 }
451
450
452 void QBarCategoryAxisPrivate::setRange(const QVariant &min, const QVariant &max)
451 void QBarCategoryAxisPrivate::setRange(const QVariant &min, const QVariant &max)
453 {
452 {
454 QString value1 = min.toString();
453 QString value1 = min.toString();
455 QString value2 = max.toString();
454 QString value2 = max.toString();
456 setRange(value1, value2);
455 setRange(value1, value2);
457 }
456 }
458
457
459 void QBarCategoryAxisPrivate::setRange(qreal min, qreal max)
458 void QBarCategoryAxisPrivate::setRange(qreal min, qreal max)
460 {
459 {
461 Q_Q(QBarCategoryAxis);
460 Q_Q(QBarCategoryAxis);
462
461
463 bool categoryChanged = false;
462 bool categoryChanged = false;
464 bool changed = false;
463 bool changed = false;
465
464
466 if (min > max)
465 if (min > max)
467 return;
466 return;
468
467
469 if (!qFuzzyIsNull(m_min - min)) {
468 if (!qFuzzyIsNull(m_min - min)) {
470 m_min = min;
469 m_min = min;
471 changed = true;
470 changed = true;
472
471
473 int imin = m_min + 0.5;
472 int imin = m_min + 0.5;
474 if (imin >= 0 && imin < m_categories.count()) {
473 if (imin >= 0 && imin < m_categories.count()) {
475 QString minCategory = m_categories.at(imin);
474 QString minCategory = m_categories.at(imin);
476 if (m_minCategory != minCategory && !minCategory.isEmpty()) {
475 if (m_minCategory != minCategory && !minCategory.isEmpty()) {
477 m_minCategory = minCategory;
476 m_minCategory = minCategory;
478 categoryChanged = true;
477 categoryChanged = true;
479 emit q->minChanged(minCategory);
478 emit q->minChanged(minCategory);
480 }
479 }
481 }
480 }
482
481
483 }
482 }
484
483
485 if (!qFuzzyIsNull(m_max - max)) {
484 if (!qFuzzyIsNull(m_max - max)) {
486 m_max = max;
485 m_max = max;
487 changed = true;
486 changed = true;
488
487
489 int imax = m_max - 0.5;
488 int imax = m_max - 0.5;
490 if (imax >= 0 && imax < m_categories.count()) {
489 if (imax >= 0 && imax < m_categories.count()) {
491 QString maxCategory = m_categories.at(imax);
490 QString maxCategory = m_categories.at(imax);
492 if (m_maxCategory != maxCategory && !maxCategory.isEmpty()) {
491 if (m_maxCategory != maxCategory && !maxCategory.isEmpty()) {
493 m_maxCategory = maxCategory;
492 m_maxCategory = maxCategory;
494 categoryChanged = true;
493 categoryChanged = true;
495 emit q->maxChanged(maxCategory);
494 emit q->maxChanged(maxCategory);
496 }
495 }
497 }
496 }
498 }
497 }
499
498
500 if (categoryChanged){
499 if (categoryChanged){
501 emit q->rangeChanged(m_minCategory, m_maxCategory);
500 emit q->rangeChanged(m_minCategory, m_maxCategory);
502 }
501 }
503
502
504 if (changed) {
503 if (changed) {
505 emit rangeChanged(m_min,m_max);
504 emit rangeChanged(m_min,m_max);
506 }
505 }
507 }
506 }
508
507
509 void QBarCategoryAxisPrivate::setRange(const QString &minCategory, const QString &maxCategory)
508 void QBarCategoryAxisPrivate::setRange(const QString &minCategory, const QString &maxCategory)
510 {
509 {
511 Q_Q(QBarCategoryAxis);
510 Q_Q(QBarCategoryAxis);
512 bool changed = false;
511 bool changed = false;
513
512
514 //special case in case or clearing all categories
513 //special case in case or clearing all categories
515 if (minCategory.isNull() && maxCategory.isNull()) {
514 if (minCategory.isNull() && maxCategory.isNull()) {
516 m_minCategory = minCategory;
515 m_minCategory = minCategory;
517 m_maxCategory = maxCategory;
516 m_maxCategory = maxCategory;
518 m_min = 0;
517 m_min = 0;
519 m_max = 0;
518 m_max = 0;
520 m_count = 0;
519 m_count = 0;
521 emit q->minChanged(minCategory);
520 emit q->minChanged(minCategory);
522 emit q->maxChanged(maxCategory);
521 emit q->maxChanged(maxCategory);
523 emit q->rangeChanged(m_minCategory, m_maxCategory);
522 emit q->rangeChanged(m_minCategory, m_maxCategory);
524 emit rangeChanged(m_min,m_max);
523 emit rangeChanged(m_min,m_max);
525 }
524 }
526
525
527 if (m_categories.indexOf(maxCategory) < m_categories.indexOf(minCategory))
526 if (m_categories.indexOf(maxCategory) < m_categories.indexOf(minCategory))
528 return;
527 return;
529
528
530 if (!minCategory.isEmpty() && m_minCategory != minCategory && m_categories.contains(minCategory)) {
529 if (!minCategory.isEmpty() && m_minCategory != minCategory && m_categories.contains(minCategory)) {
531 m_minCategory = minCategory;
530 m_minCategory = minCategory;
532 m_min = m_categories.indexOf(m_minCategory) - 0.5;
531 m_min = m_categories.indexOf(m_minCategory) - 0.5;
533 changed = true;
532 changed = true;
534 emit q->minChanged(minCategory);
533 emit q->minChanged(minCategory);
535 }
534 }
536
535
537 if (!maxCategory.isEmpty() && m_maxCategory != maxCategory && m_categories.contains(maxCategory)) {
536 if (!maxCategory.isEmpty() && m_maxCategory != maxCategory && m_categories.contains(maxCategory)) {
538 m_maxCategory = maxCategory;
537 m_maxCategory = maxCategory;
539 m_max = m_categories.indexOf(m_maxCategory) + 0.5;
538 m_max = m_categories.indexOf(m_maxCategory) + 0.5;
540 changed = true;
539 changed = true;
541 emit q->maxChanged(maxCategory);
540 emit q->maxChanged(maxCategory);
542 }
541 }
543
542
544 if (changed) {
543 if (changed) {
545 m_count = m_max - m_min;
544 m_count = m_max - m_min;
546 emit q->rangeChanged(m_minCategory, m_maxCategory);
545 emit q->rangeChanged(m_minCategory, m_maxCategory);
547 emit rangeChanged(m_min,m_max);
546 emit rangeChanged(m_min,m_max);
548 }
547 }
549 }
548 }
550
549
551 void QBarCategoryAxisPrivate::initializeGraphics(QGraphicsItem* parent)
550 void QBarCategoryAxisPrivate::initializeGraphics(QGraphicsItem* parent)
552 {
551 {
553 Q_Q(QBarCategoryAxis);
552 Q_Q(QBarCategoryAxis);
554 ChartAxis* axis(0);
553 ChartAxis* axis(0);
555 if (orientation() == Qt::Vertical)
554 if (orientation() == Qt::Vertical)
556 axis = new ChartBarCategoryAxisY(q,parent);
555 axis = new ChartBarCategoryAxisY(q,parent);
557 if (orientation() == Qt::Horizontal)
556 if (orientation() == Qt::Horizontal)
558 axis = new ChartBarCategoryAxisX(q,parent);
557 axis = new ChartBarCategoryAxisX(q,parent);
559
558
560 m_item.reset(axis);
559 m_item.reset(axis);
561 QAbstractAxisPrivate::initializeGraphics(parent);
560 QAbstractAxisPrivate::initializeGraphics(parent);
562 }
561 }
563
562
564 void QBarCategoryAxisPrivate::updateCategoryDomain()
563 void QBarCategoryAxisPrivate::updateCategoryDomain()
565 {
564 {
566 m_min = m_categories.indexOf(m_minCategory) - 0.5;
565 m_min = m_categories.indexOf(m_minCategory) - 0.5;
567 m_max = m_categories.indexOf(m_maxCategory) + 0.5;
566 m_max = m_categories.indexOf(m_maxCategory) + 0.5;
568 m_count = m_max - m_min;
567 m_count = m_max - m_min;
569 }
568 }
570
569
571
570
572 void QBarCategoryAxisPrivate::initializeDomain(AbstractDomain *domain)
571 void QBarCategoryAxisPrivate::initializeDomain(AbstractDomain *domain)
573 {
572 {
574 Q_Q(QBarCategoryAxis);
573 Q_Q(QBarCategoryAxis);
575 if (m_max == m_min) {
574 if (m_max == m_min) {
576 int min;
575 int min;
577 int max;
576 int max;
578 if (orientation() == Qt::Vertical) {
577 if (orientation() == Qt::Vertical) {
579 min = domain->minY() + 0.5;
578 min = domain->minY() + 0.5;
580 max = domain->maxY() - 0.5;
579 max = domain->maxY() - 0.5;
581 } else {
580 } else {
582 min = domain->minX() + 0.5;
581 min = domain->minX() + 0.5;
583 max = domain->maxX() - 0.5;
582 max = domain->maxX() - 0.5;
584 }
583 }
585
584
586 if (min > 0 && min < m_categories.count() && max > 0 && max < m_categories.count())
585 if (min > 0 && min < m_categories.count() && max > 0 && max < m_categories.count())
587 q->setRange(m_categories.at(min), m_categories.at(max));
586 q->setRange(m_categories.at(min), m_categories.at(max));
588 } else {
587 } else {
589 if (orientation() == Qt::Vertical)
588 if (orientation() == Qt::Vertical)
590 domain->setRangeY(m_min, m_max);
589 domain->setRangeY(m_min, m_max);
591 else
590 else
592 domain->setRangeX(m_min, m_max);
591 domain->setRangeX(m_min, m_max);
593 }
592 }
594 }
593 }
595
594
596 #include "moc_qbarcategoryaxis.cpp"
595 #include "moc_qbarcategoryaxis.cpp"
597 #include "moc_qbarcategoryaxis_p.cpp"
596 #include "moc_qbarcategoryaxis_p.cpp"
598
597
599 QTCOMMERCIALCHART_END_NAMESPACE
598 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,873 +1,901
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 "qabstractbarseries.h"
21 #include "qabstractbarseries.h"
22 #include "qabstractbarseries_p.h"
22 #include "qabstractbarseries_p.h"
23 #include "qbarset.h"
23 #include "qbarset.h"
24 #include "qbarset_p.h"
24 #include "qbarset_p.h"
25 #include "abstractdomain_p.h"
25 #include "abstractdomain_p.h"
26 #include "chartdataset_p.h"
26 #include "chartdataset_p.h"
27 #include "charttheme_p.h"
27 #include "charttheme_p.h"
28 #include "qvalueaxis.h"
28 #include "qvalueaxis.h"
29 #include "qbarcategoryaxis.h"
29 #include "qbarcategoryaxis.h"
30 #include "qbarlegendmarker.h"
30 #include "qbarlegendmarker.h"
31 #include "baranimation_p.h"
31 #include "baranimation_p.h"
32 #include "abstractbarchartitem_p.h"
32 #include "abstractbarchartitem_p.h"
33
33
34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35
35
36 /*!
36 /*!
37 \class QAbstractBarSeries
37 \class QAbstractBarSeries
38 \brief Series for creating a bar chart
38 \brief Series for creating a bar chart
39 \mainclass
39 \mainclass
40
40
41 QAbstractBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
41 QAbstractBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
42 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
42 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
43 and y-value is the height of the bar. The category names are ignored with this series and x-axis
43 and y-value is the height of the bar. The category names are ignored with this series and x-axis
44 shows the x-values.
44 shows the x-values.
45
45
46 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
46 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
47 \image examples_barchart.png
47 \image examples_barchart.png
48
48
49 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
49 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
50 */
50 */
51 /*!
51 /*!
52 \qmlclass AbstractBarSeries QAbstractBarSeries
52 \qmlclass AbstractBarSeries QAbstractBarSeries
53 \inherits QAbstractSeries
53 \inherits QAbstractSeries
54
54
55 The following QML shows how to create a simple bar chart:
55 The following QML shows how to create a simple bar chart:
56 \snippet ../demos/qmlchart/qml/qmlchart/View6.qml 1
56 \snippet ../demos/qmlchart/qml/qmlchart/View6.qml 1
57
57
58 \beginfloatleft
58 \beginfloatleft
59 \image demos_qmlchart6.png
59 \image demos_qmlchart6.png
60 \endfloat
60 \endfloat
61 \clearfloat
61 \clearfloat
62 */
62 */
63
63
64 /*!
64 /*!
65 \qmlproperty AbstractAxis AbstractBarSeries::axisX
66 The x axis used for the series. If you leave both axisX and axisXTop undefined, a BarCategoriesAxis is created for
67 the series.
68 \sa axisXTop
69 */
70
71 /*!
72 \qmlproperty AbstractAxis AbstractBarSeries::axisY
73 The y axis used for the series. If you leave both axisY and axisYRight undefined, a ValueAxis is created for
74 the series.
75 \sa axisYRight
76 */
77
78 /*!
79 \qmlproperty AbstractAxis AbstractBarSeries::axisXTop
80 The x axis used for the series, drawn on top of the chart view. Note that you can only provide either axisX or
81 axisXTop, but not both.
82 \sa axisX
83 */
84
85 /*!
86 \qmlproperty AbstractAxis AbstractBarSeries::axisYRight
87 The y axis used for the series, drawn to the right on the chart view. Note that you can only provide either axisY
88 or axisYRight, but not both.
89 \sa axisY
90 */
91
92 /*!
65 \property QAbstractBarSeries::barWidth
93 \property QAbstractBarSeries::barWidth
66 The width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
94 The width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
67 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
95 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
68 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
96 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
69 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
97 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
70 \sa QBarSeries
98 \sa QBarSeries
71 */
99 */
72 /*!
100 /*!
73 \qmlproperty real AbstractBarSeries::barWidth
101 \qmlproperty real AbstractBarSeries::barWidth
74 The width of the bars of the series. The unit of width is the unit of x-axis. The minimum width for bars
102 The width of the bars of the series. The unit of width is the unit of x-axis. The minimum width for bars
75 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
103 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
76 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
104 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
77 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
105 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
78 */
106 */
79
107
80 /*!
108 /*!
81 \property QAbstractBarSeries::count
109 \property QAbstractBarSeries::count
82 Holds the number of sets in series.
110 Holds the number of sets in series.
83 */
111 */
84 /*!
112 /*!
85 \qmlproperty int AbstractBarSeries::count
113 \qmlproperty int AbstractBarSeries::count
86 Holds the number of sets in series.
114 Holds the number of sets in series.
87 */
115 */
88
116
89 /*!
117 /*!
90 \property QAbstractBarSeries::labelsVisible
118 \property QAbstractBarSeries::labelsVisible
91 Defines the visibility of the labels in series
119 Defines the visibility of the labels in series
92 */
120 */
93 /*!
121 /*!
94 \qmlproperty bool AbstractBarSeries::labelsVisible
122 \qmlproperty bool AbstractBarSeries::labelsVisible
95 Defines the visibility of the labels in series
123 Defines the visibility of the labels in series
96 */
124 */
97
125
98 /*!
126 /*!
99 \fn void QAbstractBarSeries::clicked(int index, QBarSet *barset)
127 \fn void QAbstractBarSeries::clicked(int index, QBarSet *barset)
100 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
128 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
101 Clicked bar inside set is indexed by \a index
129 Clicked bar inside set is indexed by \a index
102 */
130 */
103 /*!
131 /*!
104 \qmlsignal AbstractBarSeries::onClicked(int index, BarSet barset)
132 \qmlsignal AbstractBarSeries::onClicked(int index, BarSet barset)
105 The signal is emitted if the user clicks with a mouse on top of BarSet.
133 The signal is emitted if the user clicks with a mouse on top of BarSet.
106 Clicked bar inside set is indexed by \a index
134 Clicked bar inside set is indexed by \a index
107 */
135 */
108
136
109 /*!
137 /*!
110 \fn void QAbstractBarSeries::hovered(bool status, QBarSet* barset)
138 \fn void QAbstractBarSeries::hovered(bool status, QBarSet* barset)
111
139
112 The signal is emitted if mouse is hovered on top of series.
140 The signal is emitted if mouse is hovered on top of series.
113 Parameter \a barset is the pointer of barset, where hover happened.
141 Parameter \a barset is the pointer of barset, where hover happened.
114 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
142 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
115 */
143 */
116 /*!
144 /*!
117 \qmlsignal AbstractBarSeries::onHovered(bool status, BarSet barset)
145 \qmlsignal AbstractBarSeries::onHovered(bool status, BarSet barset)
118
146
119 The signal is emitted if mouse is hovered on top of series.
147 The signal is emitted if mouse is hovered on top of series.
120 Parameter \a barset is the pointer of barset, where hover happened.
148 Parameter \a barset is the pointer of barset, where hover happened.
121 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
149 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
122 */
150 */
123
151
124 /*!
152 /*!
125 \fn void QAbstractBarSeries::countChanged()
153 \fn void QAbstractBarSeries::countChanged()
126 This signal is emitted when barset count has been changed, for example by append or remove.
154 This signal is emitted when barset count has been changed, for example by append or remove.
127 */
155 */
128 /*!
156 /*!
129 \qmlsignal AbstractBarSeries::onCountChanged()
157 \qmlsignal AbstractBarSeries::onCountChanged()
130 This signal is emitted when barset count has been changed, for example by append or remove.
158 This signal is emitted when barset count has been changed, for example by append or remove.
131 */
159 */
132
160
133 /*!
161 /*!
134 \fn void QAbstractBarSeries::labelsVisibleChanged()
162 \fn void QAbstractBarSeries::labelsVisibleChanged()
135 This signal is emitted when labels visibility have changed.
163 This signal is emitted when labels visibility have changed.
136 \sa isLabelsVisible(), setLabelsVisible()
164 \sa isLabelsVisible(), setLabelsVisible()
137 */
165 */
138
166
139 /*!
167 /*!
140 \fn void QAbstractBarSeries::barsetsAdded(QList<QBarSet*> sets)
168 \fn void QAbstractBarSeries::barsetsAdded(QList<QBarSet*> sets)
141 This signal is emitted when \a sets have been added to the series.
169 This signal is emitted when \a sets have been added to the series.
142 \sa append(), insert()
170 \sa append(), insert()
143 */
171 */
144 /*!
172 /*!
145 \qmlsignal AbstractBarSeries::onBarsetsAdded(BarSet barset)
173 \qmlsignal AbstractBarSeries::onBarsetsAdded(BarSet barset)
146 Emitted when \a barset has been added to the series.
174 Emitted when \a barset has been added to the series.
147 */
175 */
148
176
149 /*!
177 /*!
150 \fn void QAbstractBarSeries::barsetsRemoved(QList<QBarSet*> sets)
178 \fn void QAbstractBarSeries::barsetsRemoved(QList<QBarSet*> sets)
151 This signal is emitted when \a sets have been removed from the series.
179 This signal is emitted when \a sets have been removed from the series.
152 \sa remove()
180 \sa remove()
153 */
181 */
154 /*!
182 /*!
155 \qmlsignal AbstractBarSeries::onBarsetsRemoved(BarSet barset)
183 \qmlsignal AbstractBarSeries::onBarsetsRemoved(BarSet barset)
156 Emitted when \a barset has been removed from the series.
184 Emitted when \a barset has been removed from the series.
157 */
185 */
158
186
159 /*!
187 /*!
160 \qmlmethod BarSet AbstractBarSeries::at(int index)
188 \qmlmethod BarSet AbstractBarSeries::at(int index)
161 Returns bar set at \a index. Returns null if the index is not valid.
189 Returns bar set at \a index. Returns null if the index is not valid.
162 */
190 */
163
191
164 /*!
192 /*!
165 \qmlmethod BarSet AbstractBarSeries::append(string label, VariantList values)
193 \qmlmethod BarSet AbstractBarSeries::append(string label, VariantList values)
166 Adds a new bar set with \a label and \a values to \a index. Values is a list of reals.
194 Adds a new bar set with \a label and \a values to \a index. Values is a list of reals.
167 For example:
195 For example:
168 \code
196 \code
169 myBarSeries.append("set 1", [0, 0.2, 0.2, 0.5, 0.4, 1.5, 0.9]);
197 myBarSeries.append("set 1", [0, 0.2, 0.2, 0.5, 0.4, 1.5, 0.9]);
170 \endcode
198 \endcode
171 */
199 */
172
200
173 /*!
201 /*!
174 \qmlmethod BarSet AbstractBarSeries::insert(int index, string label, VariantList values)
202 \qmlmethod BarSet AbstractBarSeries::insert(int index, string label, VariantList values)
175 Inserts a new bar set with \a label and \a values to \a index. Values can be a list of reals or a list of XYPoints.
203 Inserts a new bar set with \a label and \a values to \a index. Values can be a list of reals or a list of XYPoints.
176 If index is zero or smaller, the new barset is prepended. If the index is count or bigger, the new barset is
204 If index is zero or smaller, the new barset is prepended. If the index is count or bigger, the new barset is
177 appended.
205 appended.
178 \sa AbstractBarSeries::append()
206 \sa AbstractBarSeries::append()
179 */
207 */
180
208
181 /*!
209 /*!
182 \qmlmethod bool AbstractBarSeries::remove(BarSet barset)
210 \qmlmethod bool AbstractBarSeries::remove(BarSet barset)
183 Removes the barset from the series. Returns true if successful, false otherwise.
211 Removes the barset from the series. Returns true if successful, false otherwise.
184 */
212 */
185
213
186 /*!
214 /*!
187 \qmlmethod AbstractBarSeries::clear()
215 \qmlmethod AbstractBarSeries::clear()
188 Removes all barsets from the series.
216 Removes all barsets from the series.
189 */
217 */
190
218
191 /*!
219 /*!
192 Destructs abstractbarseries and owned barsets.
220 Destructs abstractbarseries and owned barsets.
193 */
221 */
194 QAbstractBarSeries::~QAbstractBarSeries()
222 QAbstractBarSeries::~QAbstractBarSeries()
195 {
223 {
196
224
197 }
225 }
198
226
199 /*!
227 /*!
200 \internal
228 \internal
201 */
229 */
202 QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &o, QObject *parent)
230 QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &o, QObject *parent)
203 : QAbstractSeries(o, parent)
231 : QAbstractSeries(o, parent)
204 {
232 {
205 Q_D(QAbstractSeries);
233 Q_D(QAbstractSeries);
206 QObject::connect(this, SIGNAL(countChanged()), d, SIGNAL(countChanged()));
234 QObject::connect(this, SIGNAL(countChanged()), d, SIGNAL(countChanged()));
207 }
235 }
208
236
209 /*!
237 /*!
210 Sets the width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
238 Sets the width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
211 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
239 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
212 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
240 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
213 Note that with \link QBarSeries \endlink this value means the width of one group of bars instead of just one bar.
241 Note that with \link QBarSeries \endlink this value means the width of one group of bars instead of just one bar.
214 */
242 */
215 void QAbstractBarSeries::setBarWidth(qreal width)
243 void QAbstractBarSeries::setBarWidth(qreal width)
216 {
244 {
217 Q_D(QAbstractBarSeries);
245 Q_D(QAbstractBarSeries);
218 d->setBarWidth(width);
246 d->setBarWidth(width);
219 }
247 }
220
248
221 /*!
249 /*!
222 Returns the width of the bars of the series.
250 Returns the width of the bars of the series.
223 \sa setBarWidth()
251 \sa setBarWidth()
224 */
252 */
225 qreal QAbstractBarSeries::barWidth() const
253 qreal QAbstractBarSeries::barWidth() const
226 {
254 {
227 Q_D(const QAbstractBarSeries);
255 Q_D(const QAbstractBarSeries);
228 return d->barWidth();
256 return d->barWidth();
229 }
257 }
230
258
231 /*!
259 /*!
232 Adds a set of bars to series. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
260 Adds a set of bars to series. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
233 Returns true, if appending succeeded.
261 Returns true, if appending succeeded.
234 */
262 */
235 bool QAbstractBarSeries::append(QBarSet *set)
263 bool QAbstractBarSeries::append(QBarSet *set)
236 {
264 {
237 Q_D(QAbstractBarSeries);
265 Q_D(QAbstractBarSeries);
238 bool success = d->append(set);
266 bool success = d->append(set);
239 if (success) {
267 if (success) {
240 QList<QBarSet *> sets;
268 QList<QBarSet *> sets;
241 sets.append(set);
269 sets.append(set);
242 set->setParent(this);
270 set->setParent(this);
243 emit barsetsAdded(sets);
271 emit barsetsAdded(sets);
244 emit countChanged();
272 emit countChanged();
245 }
273 }
246 return success;
274 return success;
247 }
275 }
248
276
249 /*!
277 /*!
250 Removes barset from series. Releases ownership of \a set. Deletes the set, if remove
278 Removes barset from series. Releases ownership of \a set. Deletes the set, if remove
251 was successful.
279 was successful.
252 Returns true, if set was removed.
280 Returns true, if set was removed.
253 */
281 */
254 bool QAbstractBarSeries::remove(QBarSet *set)
282 bool QAbstractBarSeries::remove(QBarSet *set)
255 {
283 {
256 Q_D(QAbstractBarSeries);
284 Q_D(QAbstractBarSeries);
257 bool success = d->remove(set);
285 bool success = d->remove(set);
258 if (success) {
286 if (success) {
259 QList<QBarSet *> sets;
287 QList<QBarSet *> sets;
260 sets.append(set);
288 sets.append(set);
261 set->setParent(0);
289 set->setParent(0);
262 emit barsetsRemoved(sets);
290 emit barsetsRemoved(sets);
263 emit countChanged();
291 emit countChanged();
264 delete set;
292 delete set;
265 set = 0;
293 set = 0;
266 }
294 }
267 return success;
295 return success;
268 }
296 }
269
297
270 /*!
298 /*!
271 Takes a single \a set from the series. Does not delete the barset object.
299 Takes a single \a set from the series. Does not delete the barset object.
272
300
273 NOTE: The series remains as the barset's parent object. You must set the
301 NOTE: The series remains as the barset's parent object. You must set the
274 parent object to take full ownership.
302 parent object to take full ownership.
275
303
276 Returns true if take was successful.
304 Returns true if take was successful.
277 */
305 */
278 bool QAbstractBarSeries::take(QBarSet *set)
306 bool QAbstractBarSeries::take(QBarSet *set)
279 {
307 {
280 Q_D(QAbstractBarSeries);
308 Q_D(QAbstractBarSeries);
281 bool success = d->remove(set);
309 bool success = d->remove(set);
282 if (success) {
310 if (success) {
283 QList<QBarSet *> sets;
311 QList<QBarSet *> sets;
284 sets.append(set);
312 sets.append(set);
285 emit barsetsRemoved(sets);
313 emit barsetsRemoved(sets);
286 emit countChanged();
314 emit countChanged();
287 }
315 }
288 return success;
316 return success;
289 }
317 }
290
318
291 /*!
319 /*!
292 Adds a list of barsets to series. Takes ownership of \a sets.
320 Adds a list of barsets to series. Takes ownership of \a sets.
293 Returns true, if all sets were appended successfully. If any of the sets is null or is already appended to series,
321 Returns true, if all sets were appended successfully. If any of the sets is null or is already appended to series,
294 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
322 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
295 and function returns false.
323 and function returns false.
296 */
324 */
297 bool QAbstractBarSeries::append(QList<QBarSet *> sets)
325 bool QAbstractBarSeries::append(QList<QBarSet *> sets)
298 {
326 {
299 Q_D(QAbstractBarSeries);
327 Q_D(QAbstractBarSeries);
300 bool success = d->append(sets);
328 bool success = d->append(sets);
301 if (success) {
329 if (success) {
302 emit barsetsAdded(sets);
330 emit barsetsAdded(sets);
303 emit countChanged();
331 emit countChanged();
304 }
332 }
305 return success;
333 return success;
306 }
334 }
307
335
308 /*!
336 /*!
309 Insert a set of bars to series at \a index postion. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
337 Insert a set of bars to series at \a index postion. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
310 Returns true, if inserting succeeded.
338 Returns true, if inserting succeeded.
311
339
312 */
340 */
313 bool QAbstractBarSeries::insert(int index, QBarSet *set)
341 bool QAbstractBarSeries::insert(int index, QBarSet *set)
314 {
342 {
315 Q_D(QAbstractBarSeries);
343 Q_D(QAbstractBarSeries);
316 bool success = d->insert(index, set);
344 bool success = d->insert(index, set);
317 if (success) {
345 if (success) {
318 QList<QBarSet *> sets;
346 QList<QBarSet *> sets;
319 sets.append(set);
347 sets.append(set);
320 emit barsetsAdded(sets);
348 emit barsetsAdded(sets);
321 emit countChanged();
349 emit countChanged();
322 }
350 }
323 return success;
351 return success;
324 }
352 }
325
353
326 /*!
354 /*!
327 Removes all barsets from the series. Deletes removed sets.
355 Removes all barsets from the series. Deletes removed sets.
328 */
356 */
329 void QAbstractBarSeries::clear()
357 void QAbstractBarSeries::clear()
330 {
358 {
331 Q_D(QAbstractBarSeries);
359 Q_D(QAbstractBarSeries);
332 QList<QBarSet *> sets = barSets();
360 QList<QBarSet *> sets = barSets();
333 bool success = d->remove(sets);
361 bool success = d->remove(sets);
334 if (success) {
362 if (success) {
335 emit barsetsRemoved(sets);
363 emit barsetsRemoved(sets);
336 emit countChanged();
364 emit countChanged();
337 foreach (QBarSet *set, sets)
365 foreach (QBarSet *set, sets)
338 delete set;
366 delete set;
339 }
367 }
340 }
368 }
341
369
342 /*!
370 /*!
343 Returns number of sets in series.
371 Returns number of sets in series.
344 */
372 */
345 int QAbstractBarSeries::count() const
373 int QAbstractBarSeries::count() const
346 {
374 {
347 Q_D(const QAbstractBarSeries);
375 Q_D(const QAbstractBarSeries);
348 return d->m_barSets.count();
376 return d->m_barSets.count();
349 }
377 }
350
378
351 /*!
379 /*!
352 Returns a list of sets in series. Keeps ownership of sets.
380 Returns a list of sets in series. Keeps ownership of sets.
353 */
381 */
354 QList<QBarSet *> QAbstractBarSeries::barSets() const
382 QList<QBarSet *> QAbstractBarSeries::barSets() const
355 {
383 {
356 Q_D(const QAbstractBarSeries);
384 Q_D(const QAbstractBarSeries);
357 return d->m_barSets;
385 return d->m_barSets;
358 }
386 }
359
387
360 /*!
388 /*!
361 Sets the visibility of labels in series to \a visible
389 Sets the visibility of labels in series to \a visible
362 */
390 */
363 void QAbstractBarSeries::setLabelsVisible(bool visible)
391 void QAbstractBarSeries::setLabelsVisible(bool visible)
364 {
392 {
365 Q_D(QAbstractBarSeries);
393 Q_D(QAbstractBarSeries);
366 if (d->m_labelsVisible != visible) {
394 if (d->m_labelsVisible != visible) {
367 d->setLabelsVisible(visible);
395 d->setLabelsVisible(visible);
368 emit labelsVisibleChanged();
396 emit labelsVisibleChanged();
369 }
397 }
370 }
398 }
371
399
372 /*!
400 /*!
373 Returns the visibility of labels
401 Returns the visibility of labels
374 */
402 */
375 bool QAbstractBarSeries::isLabelsVisible() const
403 bool QAbstractBarSeries::isLabelsVisible() const
376 {
404 {
377 Q_D(const QAbstractBarSeries);
405 Q_D(const QAbstractBarSeries);
378 return d->m_labelsVisible;
406 return d->m_labelsVisible;
379 }
407 }
380
408
381 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
409 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
382
410
383 QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) :
411 QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) :
384 QAbstractSeriesPrivate(q),
412 QAbstractSeriesPrivate(q),
385 m_barWidth(0.5), // Default value is 50% of category width
413 m_barWidth(0.5), // Default value is 50% of category width
386 m_labelsVisible(false),
414 m_labelsVisible(false),
387 m_visible(true)
415 m_visible(true)
388 {
416 {
389 }
417 }
390
418
391 int QAbstractBarSeriesPrivate::categoryCount() const
419 int QAbstractBarSeriesPrivate::categoryCount() const
392 {
420 {
393 // No categories defined. return count of longest set.
421 // No categories defined. return count of longest set.
394 int count = 0;
422 int count = 0;
395 for (int i = 0; i < m_barSets.count(); i++) {
423 for (int i = 0; i < m_barSets.count(); i++) {
396 if (m_barSets.at(i)->count() > count)
424 if (m_barSets.at(i)->count() > count)
397 count = m_barSets.at(i)->count();
425 count = m_barSets.at(i)->count();
398 }
426 }
399
427
400 return count;
428 return count;
401 }
429 }
402
430
403 void QAbstractBarSeriesPrivate::setBarWidth(qreal width)
431 void QAbstractBarSeriesPrivate::setBarWidth(qreal width)
404 {
432 {
405 if (width < 0.0)
433 if (width < 0.0)
406 width = 0.0;
434 width = 0.0;
407 m_barWidth = width;
435 m_barWidth = width;
408 emit updatedLayout();
436 emit updatedLayout();
409 }
437 }
410
438
411 qreal QAbstractBarSeriesPrivate::barWidth() const
439 qreal QAbstractBarSeriesPrivate::barWidth() const
412 {
440 {
413 return m_barWidth;
441 return m_barWidth;
414 }
442 }
415
443
416 QBarSet *QAbstractBarSeriesPrivate::barsetAt(int index)
444 QBarSet *QAbstractBarSeriesPrivate::barsetAt(int index)
417 {
445 {
418 return m_barSets.at(index);
446 return m_barSets.at(index);
419 }
447 }
420
448
421 void QAbstractBarSeriesPrivate::setVisible(bool visible)
449 void QAbstractBarSeriesPrivate::setVisible(bool visible)
422 {
450 {
423 m_visible = visible;
451 m_visible = visible;
424 emit visibleChanged();
452 emit visibleChanged();
425 }
453 }
426
454
427 void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible)
455 void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible)
428 {
456 {
429 m_labelsVisible = visible;
457 m_labelsVisible = visible;
430 emit labelsVisibleChanged(visible);
458 emit labelsVisibleChanged(visible);
431 }
459 }
432
460
433 qreal QAbstractBarSeriesPrivate::min()
461 qreal QAbstractBarSeriesPrivate::min()
434 {
462 {
435 if (m_barSets.count() <= 0)
463 if (m_barSets.count() <= 0)
436 return 0;
464 return 0;
437
465
438 qreal min = INT_MAX;
466 qreal min = INT_MAX;
439
467
440 for (int i = 0; i < m_barSets.count(); i++) {
468 for (int i = 0; i < m_barSets.count(); i++) {
441 int categoryCount = m_barSets.at(i)->count();
469 int categoryCount = m_barSets.at(i)->count();
442 for (int j = 0; j < categoryCount; j++) {
470 for (int j = 0; j < categoryCount; j++) {
443 qreal temp = m_barSets.at(i)->at(j);
471 qreal temp = m_barSets.at(i)->at(j);
444 if (temp < min)
472 if (temp < min)
445 min = temp;
473 min = temp;
446 }
474 }
447 }
475 }
448 return min;
476 return min;
449 }
477 }
450
478
451 qreal QAbstractBarSeriesPrivate::max()
479 qreal QAbstractBarSeriesPrivate::max()
452 {
480 {
453 if (m_barSets.count() <= 0)
481 if (m_barSets.count() <= 0)
454 return 0;
482 return 0;
455
483
456 qreal max = INT_MIN;
484 qreal max = INT_MIN;
457
485
458 for (int i = 0; i < m_barSets.count(); i++) {
486 for (int i = 0; i < m_barSets.count(); i++) {
459 int categoryCount = m_barSets.at(i)->count();
487 int categoryCount = m_barSets.at(i)->count();
460 for (int j = 0; j < categoryCount; j++) {
488 for (int j = 0; j < categoryCount; j++) {
461 qreal temp = m_barSets.at(i)->at(j);
489 qreal temp = m_barSets.at(i)->at(j);
462 if (temp > max)
490 if (temp > max)
463 max = temp;
491 max = temp;
464 }
492 }
465 }
493 }
466
494
467 return max;
495 return max;
468 }
496 }
469
497
470 qreal QAbstractBarSeriesPrivate::valueAt(int set, int category)
498 qreal QAbstractBarSeriesPrivate::valueAt(int set, int category)
471 {
499 {
472 if ((set < 0) || (set >= m_barSets.count()))
500 if ((set < 0) || (set >= m_barSets.count()))
473 return 0; // No set, no value.
501 return 0; // No set, no value.
474 else if ((category < 0) || (category >= m_barSets.at(set)->count()))
502 else if ((category < 0) || (category >= m_barSets.at(set)->count()))
475 return 0; // No category, no value.
503 return 0; // No category, no value.
476
504
477 return m_barSets.at(set)->at(category);
505 return m_barSets.at(set)->at(category);
478 }
506 }
479
507
480 qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category)
508 qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category)
481 {
509 {
482 if ((set < 0) || (set >= m_barSets.count()))
510 if ((set < 0) || (set >= m_barSets.count()))
483 return 0; // No set, no value.
511 return 0; // No set, no value.
484 else if ((category < 0) || (category >= m_barSets.at(set)->count()))
512 else if ((category < 0) || (category >= m_barSets.at(set)->count()))
485 return 0; // No category, no value.
513 return 0; // No category, no value.
486
514
487 qreal value = m_barSets.at(set)->at(category);
515 qreal value = m_barSets.at(set)->at(category);
488 qreal sum = categorySum(category);
516 qreal sum = categorySum(category);
489 if (qFuzzyCompare(sum, 0))
517 if (qFuzzyCompare(sum, 0))
490 return 0;
518 return 0;
491
519
492 return value / sum;
520 return value / sum;
493 }
521 }
494
522
495 qreal QAbstractBarSeriesPrivate::categorySum(int category)
523 qreal QAbstractBarSeriesPrivate::categorySum(int category)
496 {
524 {
497 qreal sum(0);
525 qreal sum(0);
498 int count = m_barSets.count(); // Count sets
526 int count = m_barSets.count(); // Count sets
499 for (int set = 0; set < count; set++) {
527 for (int set = 0; set < count; set++) {
500 if (category < m_barSets.at(set)->count())
528 if (category < m_barSets.at(set)->count())
501 sum += m_barSets.at(set)->at(category);
529 sum += m_barSets.at(set)->at(category);
502 }
530 }
503 return sum;
531 return sum;
504 }
532 }
505
533
506 qreal QAbstractBarSeriesPrivate::absoluteCategorySum(int category)
534 qreal QAbstractBarSeriesPrivate::absoluteCategorySum(int category)
507 {
535 {
508 qreal sum(0);
536 qreal sum(0);
509 int count = m_barSets.count(); // Count sets
537 int count = m_barSets.count(); // Count sets
510 for (int set = 0; set < count; set++) {
538 for (int set = 0; set < count; set++) {
511 if (category < m_barSets.at(set)->count())
539 if (category < m_barSets.at(set)->count())
512 sum += qAbs(m_barSets.at(set)->at(category));
540 sum += qAbs(m_barSets.at(set)->at(category));
513 }
541 }
514 return sum;
542 return sum;
515 }
543 }
516
544
517 qreal QAbstractBarSeriesPrivate::maxCategorySum()
545 qreal QAbstractBarSeriesPrivate::maxCategorySum()
518 {
546 {
519 qreal max = INT_MIN;
547 qreal max = INT_MIN;
520 int count = categoryCount();
548 int count = categoryCount();
521 for (int i = 0; i < count; i++) {
549 for (int i = 0; i < count; i++) {
522 qreal sum = categorySum(i);
550 qreal sum = categorySum(i);
523 if (sum > max)
551 if (sum > max)
524 max = sum;
552 max = sum;
525 }
553 }
526 return max;
554 return max;
527 }
555 }
528
556
529 qreal QAbstractBarSeriesPrivate::minX()
557 qreal QAbstractBarSeriesPrivate::minX()
530 {
558 {
531 if (m_barSets.count() <= 0)
559 if (m_barSets.count() <= 0)
532 return 0;
560 return 0;
533
561
534 qreal min = INT_MAX;
562 qreal min = INT_MAX;
535
563
536 for (int i = 0; i < m_barSets.count(); i++) {
564 for (int i = 0; i < m_barSets.count(); i++) {
537 int categoryCount = m_barSets.at(i)->count();
565 int categoryCount = m_barSets.at(i)->count();
538 for (int j = 0; j < categoryCount; j++) {
566 for (int j = 0; j < categoryCount; j++) {
539 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
567 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
540 if (temp < min)
568 if (temp < min)
541 min = temp;
569 min = temp;
542 }
570 }
543 }
571 }
544 return min;
572 return min;
545 }
573 }
546
574
547 qreal QAbstractBarSeriesPrivate::maxX()
575 qreal QAbstractBarSeriesPrivate::maxX()
548 {
576 {
549 if (m_barSets.count() <= 0)
577 if (m_barSets.count() <= 0)
550 return 0;
578 return 0;
551
579
552 qreal max = INT_MIN;
580 qreal max = INT_MIN;
553
581
554 for (int i = 0; i < m_barSets.count(); i++) {
582 for (int i = 0; i < m_barSets.count(); i++) {
555 int categoryCount = m_barSets.at(i)->count();
583 int categoryCount = m_barSets.at(i)->count();
556 for (int j = 0; j < categoryCount; j++) {
584 for (int j = 0; j < categoryCount; j++) {
557 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
585 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
558 if (temp > max)
586 if (temp > max)
559 max = temp;
587 max = temp;
560 }
588 }
561 }
589 }
562
590
563 return max;
591 return max;
564 }
592 }
565
593
566 qreal QAbstractBarSeriesPrivate::categoryTop(int category)
594 qreal QAbstractBarSeriesPrivate::categoryTop(int category)
567 {
595 {
568 // Returns top (sum of all positive values) of category.
596 // Returns top (sum of all positive values) of category.
569 // Returns 0, if all values are negative
597 // Returns 0, if all values are negative
570 qreal top(0);
598 qreal top(0);
571 int count = m_barSets.count();
599 int count = m_barSets.count();
572 for (int set = 0; set < count; set++) {
600 for (int set = 0; set < count; set++) {
573 if (category < m_barSets.at(set)->count()) {
601 if (category < m_barSets.at(set)->count()) {
574 qreal temp = m_barSets.at(set)->at(category);
602 qreal temp = m_barSets.at(set)->at(category);
575 if (temp > 0) {
603 if (temp > 0) {
576 top += temp;
604 top += temp;
577 }
605 }
578 }
606 }
579 }
607 }
580 return top;
608 return top;
581 }
609 }
582
610
583 qreal QAbstractBarSeriesPrivate::categoryBottom(int category)
611 qreal QAbstractBarSeriesPrivate::categoryBottom(int category)
584 {
612 {
585 // Returns bottom (sum of all negative values) of category
613 // Returns bottom (sum of all negative values) of category
586 // Returns 0, if all values are positive
614 // Returns 0, if all values are positive
587 qreal bottom(0);
615 qreal bottom(0);
588 int count = m_barSets.count();
616 int count = m_barSets.count();
589 for (int set = 0; set < count; set++) {
617 for (int set = 0; set < count; set++) {
590 if (category < m_barSets.at(set)->count()) {
618 if (category < m_barSets.at(set)->count()) {
591 qreal temp = m_barSets.at(set)->at(category);
619 qreal temp = m_barSets.at(set)->at(category);
592 if (temp < 0) {
620 if (temp < 0) {
593 bottom += temp;
621 bottom += temp;
594 }
622 }
595 }
623 }
596 }
624 }
597 return bottom;
625 return bottom;
598 }
626 }
599
627
600 qreal QAbstractBarSeriesPrivate::top()
628 qreal QAbstractBarSeriesPrivate::top()
601 {
629 {
602 // Returns top of all categories
630 // Returns top of all categories
603 qreal top(0);
631 qreal top(0);
604 int count = categoryCount();
632 int count = categoryCount();
605 for (int i = 0; i < count; i++) {
633 for (int i = 0; i < count; i++) {
606 qreal temp = categoryTop(i);
634 qreal temp = categoryTop(i);
607 if (temp > top)
635 if (temp > top)
608 top = temp;
636 top = temp;
609 }
637 }
610 return top;
638 return top;
611 }
639 }
612
640
613 qreal QAbstractBarSeriesPrivate::bottom()
641 qreal QAbstractBarSeriesPrivate::bottom()
614 {
642 {
615 // Returns bottom of all categories
643 // Returns bottom of all categories
616 qreal bottom(0);
644 qreal bottom(0);
617 int count = categoryCount();
645 int count = categoryCount();
618 for (int i = 0; i < count; i++) {
646 for (int i = 0; i < count; i++) {
619 qreal temp = categoryBottom(i);
647 qreal temp = categoryBottom(i);
620 if (temp < bottom)
648 if (temp < bottom)
621 bottom = temp;
649 bottom = temp;
622 }
650 }
623 return bottom;
651 return bottom;
624 }
652 }
625
653
626
654
627 void QAbstractBarSeriesPrivate::initializeDomain()
655 void QAbstractBarSeriesPrivate::initializeDomain()
628 {
656 {
629 qreal minX(domain()->minX());
657 qreal minX(domain()->minX());
630 qreal minY(domain()->minY());
658 qreal minY(domain()->minY());
631 qreal maxX(domain()->maxX());
659 qreal maxX(domain()->maxX());
632 qreal maxY(domain()->maxY());
660 qreal maxY(domain()->maxY());
633
661
634 qreal seriesMinX = this->minX();
662 qreal seriesMinX = this->minX();
635 qreal seriesMaxX = this->maxX();
663 qreal seriesMaxX = this->maxX();
636 qreal y = max();
664 qreal y = max();
637 minX = qMin(minX, seriesMinX - (qreal)0.5);
665 minX = qMin(minX, seriesMinX - (qreal)0.5);
638 minY = qMin(minY, y);
666 minY = qMin(minY, y);
639 maxX = qMax(maxX, seriesMaxX + (qreal)0.5);
667 maxX = qMax(maxX, seriesMaxX + (qreal)0.5);
640 maxY = qMax(maxY, y);
668 maxY = qMax(maxY, y);
641
669
642 domain()->setRange(minX, maxX, minY, maxY);
670 domain()->setRange(minX, maxX, minY, maxY);
643 }
671 }
644
672
645 QList<QLegendMarker*> QAbstractBarSeriesPrivate::createLegendMarkers(QLegend* legend)
673 QList<QLegendMarker*> QAbstractBarSeriesPrivate::createLegendMarkers(QLegend* legend)
646 {
674 {
647 Q_Q(QAbstractBarSeries);
675 Q_Q(QAbstractBarSeries);
648 QList<QLegendMarker*> markers;
676 QList<QLegendMarker*> markers;
649
677
650 foreach(QBarSet* set, q->barSets()) {
678 foreach(QBarSet* set, q->barSets()) {
651 QBarLegendMarker* marker = new QBarLegendMarker(q,set,legend);
679 QBarLegendMarker* marker = new QBarLegendMarker(q,set,legend);
652 markers << marker;
680 markers << marker;
653 }
681 }
654 return markers;
682 return markers;
655 }
683 }
656
684
657
685
658 bool QAbstractBarSeriesPrivate::append(QBarSet *set)
686 bool QAbstractBarSeriesPrivate::append(QBarSet *set)
659 {
687 {
660 if ((m_barSets.contains(set)) || (set == 0))
688 if ((m_barSets.contains(set)) || (set == 0))
661 return false; // Fail if set is already in list or set is null.
689 return false; // Fail if set is already in list or set is null.
662
690
663 m_barSets.append(set);
691 m_barSets.append(set);
664 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
692 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
665 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
693 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
666 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
694 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
667
695
668 emit restructuredBars(); // this notifies barchartitem
696 emit restructuredBars(); // this notifies barchartitem
669 return true;
697 return true;
670 }
698 }
671
699
672 bool QAbstractBarSeriesPrivate::remove(QBarSet *set)
700 bool QAbstractBarSeriesPrivate::remove(QBarSet *set)
673 {
701 {
674 if (!m_barSets.contains(set))
702 if (!m_barSets.contains(set))
675 return false; // Fail if set is not in list
703 return false; // Fail if set is not in list
676
704
677 m_barSets.removeOne(set);
705 m_barSets.removeOne(set);
678 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
706 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
679 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
707 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
680 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
708 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
681
709
682 emit restructuredBars(); // this notifies barchartitem
710 emit restructuredBars(); // this notifies barchartitem
683 return true;
711 return true;
684 }
712 }
685
713
686 bool QAbstractBarSeriesPrivate::append(QList<QBarSet * > sets)
714 bool QAbstractBarSeriesPrivate::append(QList<QBarSet * > sets)
687 {
715 {
688 foreach (QBarSet *set, sets) {
716 foreach (QBarSet *set, sets) {
689 if ((set == 0) || (m_barSets.contains(set)))
717 if ((set == 0) || (m_barSets.contains(set)))
690 return false; // Fail if any of the sets is null or is already appended.
718 return false; // Fail if any of the sets is null or is already appended.
691 if (sets.count(set) != 1)
719 if (sets.count(set) != 1)
692 return false; // Also fail if same set is more than once in given list.
720 return false; // Also fail if same set is more than once in given list.
693 }
721 }
694
722
695 foreach (QBarSet *set, sets) {
723 foreach (QBarSet *set, sets) {
696 m_barSets.append(set);
724 m_barSets.append(set);
697 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
725 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
698 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
726 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
699 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
727 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
700 }
728 }
701
729
702 emit restructuredBars(); // this notifies barchartitem
730 emit restructuredBars(); // this notifies barchartitem
703 return true;
731 return true;
704 }
732 }
705
733
706 bool QAbstractBarSeriesPrivate::remove(QList<QBarSet * > sets)
734 bool QAbstractBarSeriesPrivate::remove(QList<QBarSet * > sets)
707 {
735 {
708 if (sets.count() == 0)
736 if (sets.count() == 0)
709 return false;
737 return false;
710
738
711 foreach (QBarSet *set, sets) {
739 foreach (QBarSet *set, sets) {
712 if ((set == 0) || (!m_barSets.contains(set)))
740 if ((set == 0) || (!m_barSets.contains(set)))
713 return false; // Fail if any of the sets is null or is not in series
741 return false; // Fail if any of the sets is null or is not in series
714 if (sets.count(set) != 1)
742 if (sets.count(set) != 1)
715 return false; // Also fail if same set is more than once in given list.
743 return false; // Also fail if same set is more than once in given list.
716 }
744 }
717
745
718 foreach (QBarSet *set, sets) {
746 foreach (QBarSet *set, sets) {
719 m_barSets.removeOne(set);
747 m_barSets.removeOne(set);
720 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
748 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
721 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
749 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
722 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
750 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
723 }
751 }
724
752
725 emit restructuredBars(); // this notifies barchartitem
753 emit restructuredBars(); // this notifies barchartitem
726
754
727 return true;
755 return true;
728 }
756 }
729
757
730 bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set)
758 bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set)
731 {
759 {
732 if ((m_barSets.contains(set)) || (set == 0))
760 if ((m_barSets.contains(set)) || (set == 0))
733 return false; // Fail if set is already in list or set is null.
761 return false; // Fail if set is already in list or set is null.
734
762
735 m_barSets.insert(index, set);
763 m_barSets.insert(index, set);
736 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
764 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
737 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
765 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
738 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
766 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
739
767
740 emit restructuredBars(); // this notifies barchartitem
768 emit restructuredBars(); // this notifies barchartitem
741 return true;
769 return true;
742 }
770 }
743
771
744 void QAbstractBarSeriesPrivate::initializeAxes()
772 void QAbstractBarSeriesPrivate::initializeAxes()
745 {
773 {
746 Q_Q(QAbstractBarSeries);
774 Q_Q(QAbstractBarSeries);
747
775
748 foreach(QAbstractAxis* axis, m_axes) {
776 foreach(QAbstractAxis* axis, m_axes) {
749
777
750 if (axis->type() == QAbstractAxis::AxisTypeBarCategory) {
778 if (axis->type() == QAbstractAxis::AxisTypeBarCategory) {
751 switch (q->type()) {
779 switch (q->type()) {
752 case QAbstractSeries::SeriesTypeHorizontalBar:
780 case QAbstractSeries::SeriesTypeHorizontalBar:
753 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
781 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
754 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
782 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
755 if (axis->orientation() == Qt::Vertical)
783 if (axis->orientation() == Qt::Vertical)
756 populateCategories(qobject_cast<QBarCategoryAxis *>(axis));
784 populateCategories(qobject_cast<QBarCategoryAxis *>(axis));
757 break;
785 break;
758 case QAbstractSeries::SeriesTypeBar:
786 case QAbstractSeries::SeriesTypeBar:
759 case QAbstractSeries::SeriesTypePercentBar:
787 case QAbstractSeries::SeriesTypePercentBar:
760 case QAbstractSeries::SeriesTypeStackedBar:
788 case QAbstractSeries::SeriesTypeStackedBar:
761 if (axis->orientation() == Qt::Horizontal)
789 if (axis->orientation() == Qt::Horizontal)
762 populateCategories(qobject_cast<QBarCategoryAxis *>(axis));
790 populateCategories(qobject_cast<QBarCategoryAxis *>(axis));
763 break;
791 break;
764 default:
792 default:
765 qWarning() << "Unexpected series type";
793 qWarning() << "Unexpected series type";
766 break;
794 break;
767 }
795 }
768 }
796 }
769 }
797 }
770 }
798 }
771
799
772 QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
800 QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
773 {
801 {
774 Q_Q(const QAbstractBarSeries);
802 Q_Q(const QAbstractBarSeries);
775
803
776 switch (q->type()) {
804 switch (q->type()) {
777 case QAbstractSeries::SeriesTypeHorizontalBar:
805 case QAbstractSeries::SeriesTypeHorizontalBar:
778 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
806 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
779 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
807 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
780 if (orientation == Qt::Vertical)
808 if (orientation == Qt::Vertical)
781 return QAbstractAxis::AxisTypeBarCategory;
809 return QAbstractAxis::AxisTypeBarCategory;
782 break;
810 break;
783 case QAbstractSeries::SeriesTypeBar:
811 case QAbstractSeries::SeriesTypeBar:
784 case QAbstractSeries::SeriesTypePercentBar:
812 case QAbstractSeries::SeriesTypePercentBar:
785 case QAbstractSeries::SeriesTypeStackedBar:
813 case QAbstractSeries::SeriesTypeStackedBar:
786 if (orientation == Qt::Horizontal)
814 if (orientation == Qt::Horizontal)
787 return QAbstractAxis::AxisTypeBarCategory;
815 return QAbstractAxis::AxisTypeBarCategory;
788 break;
816 break;
789 default:
817 default:
790 qWarning() << "Unexpected series type";
818 qWarning() << "Unexpected series type";
791 break;
819 break;
792 }
820 }
793 return QAbstractAxis::AxisTypeValue;
821 return QAbstractAxis::AxisTypeValue;
794
822
795 }
823 }
796
824
797 void QAbstractBarSeriesPrivate::populateCategories(QBarCategoryAxis *axis)
825 void QAbstractBarSeriesPrivate::populateCategories(QBarCategoryAxis *axis)
798 {
826 {
799 QStringList categories;
827 QStringList categories;
800 if (axis->categories().isEmpty()) {
828 if (axis->categories().isEmpty()) {
801 for (int i(1); i < categoryCount() + 1; i++)
829 for (int i(1); i < categoryCount() + 1; i++)
802 categories << QString::number(i);
830 categories << QString::number(i);
803 axis->append(categories);
831 axis->append(categories);
804 }
832 }
805 }
833 }
806
834
807 QAbstractAxis* QAbstractBarSeriesPrivate::createDefaultAxis(Qt::Orientation orientation) const
835 QAbstractAxis* QAbstractBarSeriesPrivate::createDefaultAxis(Qt::Orientation orientation) const
808 {
836 {
809 Q_UNUSED(orientation);
837 Q_UNUSED(orientation);
810 return 0;
838 return 0;
811 }
839 }
812
840
813 void QAbstractBarSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced)
841 void QAbstractBarSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced)
814 {
842 {
815 const QList<QGradient> gradients = theme->seriesGradients();
843 const QList<QGradient> gradients = theme->seriesGradients();
816
844
817 QBrush brush;
845 QBrush brush;
818 QPen pen;
846 QPen pen;
819
847
820 qreal takeAtPos = 0.5;
848 qreal takeAtPos = 0.5;
821 qreal step = 0.2;
849 qreal step = 0.2;
822 if (m_barSets.count() > 1) {
850 if (m_barSets.count() > 1) {
823 step = 1.0 / (qreal) m_barSets.count();
851 step = 1.0 / (qreal) m_barSets.count();
824 if (m_barSets.count() % gradients.count())
852 if (m_barSets.count() % gradients.count())
825 step *= gradients.count();
853 step *= gradients.count();
826 else
854 else
827 step *= (gradients.count() - 1);
855 step *= (gradients.count() - 1);
828 }
856 }
829
857
830 for (int i(0); i < m_barSets.count(); i++) {
858 for (int i(0); i < m_barSets.count(); i++) {
831 int colorIndex = (index + i) % gradients.count();
859 int colorIndex = (index + i) % gradients.count();
832 if (i > 0 && i %gradients.count() == 0) {
860 if (i > 0 && i %gradients.count() == 0) {
833 // There is no dedicated base color for each sets, generate more colors
861 // There is no dedicated base color for each sets, generate more colors
834 takeAtPos += step;
862 takeAtPos += step;
835 if (takeAtPos == 1.0)
863 if (takeAtPos == 1.0)
836 takeAtPos += step;
864 takeAtPos += step;
837 takeAtPos -= (int) takeAtPos;
865 takeAtPos -= (int) takeAtPos;
838 }
866 }
839 if (forced || brush == m_barSets.at(i)->brush())
867 if (forced || brush == m_barSets.at(i)->brush())
840 m_barSets.at(i)->setBrush(ChartThemeManager::colorAt(gradients.at(colorIndex), takeAtPos));
868 m_barSets.at(i)->setBrush(ChartThemeManager::colorAt(gradients.at(colorIndex), takeAtPos));
841
869
842 // Pick label color from the opposite end of the gradient.
870 // Pick label color from the opposite end of the gradient.
843 // 0.3 as a boundary seems to work well.
871 // 0.3 as a boundary seems to work well.
844 if (forced || brush == m_barSets.at(i)->labelBrush()) {
872 if (forced || brush == m_barSets.at(i)->labelBrush()) {
845 if (takeAtPos < 0.3)
873 if (takeAtPos < 0.3)
846 m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 1));
874 m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 1));
847 else
875 else
848 m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0));
876 m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0));
849 }
877 }
850
878
851 if (forced || pen == m_barSets.at(i)->pen()) {
879 if (forced || pen == m_barSets.at(i)->pen()) {
852 QColor c = ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0);
880 QColor c = ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0);
853 m_barSets.at(i)->setPen(c);
881 m_barSets.at(i)->setPen(c);
854 }
882 }
855 }
883 }
856 }
884 }
857
885
858 void QAbstractBarSeriesPrivate::initializeAnimations(QChart::AnimationOptions options)
886 void QAbstractBarSeriesPrivate::initializeAnimations(QChart::AnimationOptions options)
859 {
887 {
860 AbstractBarChartItem *bar = static_cast<AbstractBarChartItem *>(m_item.data());
888 AbstractBarChartItem *bar = static_cast<AbstractBarChartItem *>(m_item.data());
861 Q_ASSERT(bar);
889 Q_ASSERT(bar);
862 if (options.testFlag(QChart::SeriesAnimations)) {
890 if (options.testFlag(QChart::SeriesAnimations)) {
863 bar->setAnimation(new BarAnimation(bar));
891 bar->setAnimation(new BarAnimation(bar));
864 }else{
892 }else{
865 bar->setAnimation(0);
893 bar->setAnimation(0);
866 }
894 }
867 QAbstractSeriesPrivate::initializeAnimations(options);
895 QAbstractSeriesPrivate::initializeAnimations(options);
868 }
896 }
869
897
870 #include "moc_qabstractbarseries.cpp"
898 #include "moc_qabstractbarseries.cpp"
871 #include "moc_qabstractbarseries_p.cpp"
899 #include "moc_qabstractbarseries_p.cpp"
872
900
873 QTCOMMERCIALCHART_END_NAMESPACE
901 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,174 +1,162
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 "qlineseries.h"
21 #include "qlineseries.h"
22 #include "qlineseries_p.h"
22 #include "qlineseries_p.h"
23 #include "linechartitem_p.h"
23 #include "linechartitem_p.h"
24 #include "chartdataset_p.h"
24 #include "chartdataset_p.h"
25 #include "charttheme_p.h"
25 #include "charttheme_p.h"
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 /*!
29 /*!
30 \class QLineSeries
30 \class QLineSeries
31 \brief The QLineSeries class is used for making line charts.
31 \brief The QLineSeries class is used for making line charts.
32
32
33 \mainclass
33 \mainclass
34
34
35 A line chart is used to show information as a series of data points
35 A line chart is used to show information as a series of data points
36 connected by straight lines.
36 connected by straight lines.
37
37
38 \image examples_linechart.png
38 \image examples_linechart.png
39
39
40 Creating basic line chart is simple:
40 Creating basic line chart is simple:
41 \code
41 \code
42 QLineSeries* series = new QLineSeries();
42 QLineSeries* series = new QLineSeries();
43 series->append(0, 6);
43 series->append(0, 6);
44 series->append(2, 4);
44 series->append(2, 4);
45 ...
45 ...
46 chart->addSeries(series);
46 chart->addSeries(series);
47 \endcode
47 \endcode
48 */
48 */
49 /*!
49 /*!
50 \qmlclass LineSeries QLineSeries
50 \qmlclass LineSeries QLineSeries
51 \inherits XYSeries
51 \inherits XYSeries
52
52
53 The following QML shows how to create a simple line chart:
53 The following QML shows how to create a simple line chart:
54 \snippet ../demos/qmlchart/qml/qmlchart/View2.qml 1
54 \snippet ../demos/qmlchart/qml/qmlchart/View2.qml 1
55 \beginfloatleft
55 \beginfloatleft
56 \image demos_qmlchart2.png
56 \image demos_qmlchart2.png
57 \endfloat
57 \endfloat
58 \clearfloat
58 \clearfloat
59 */
59 */
60
60
61 /*!
61 /*!
62 \fn virtual SeriesType QLineSeries::type() const
62 \fn virtual SeriesType QLineSeries::type() const
63 \brief Returns type of series.
63 \brief Returns type of series.
64 \sa QAbstractSeries, SeriesType
64 \sa QAbstractSeries, SeriesType
65 */
65 */
66
66
67 /*!
67 /*!
68 \qmlproperty AbstractAxis LineSeries::axisX
69 The x axis used for the series. If you leave the x axis undefined, a ValueAxis is created for
70 the series.
71 */
72
73 /*!
74 \qmlproperty AbstractAxis LineSeries::axisY
75 The y axis used for the series. If you leave the y axis undefined, a ValueAxis is created for
76 the series.
77 */
78
79 /*!
80 \qmlproperty real LineSeries::width
68 \qmlproperty real LineSeries::width
81 The width of the line. By default the width is 2.0.
69 The width of the line. By default the width is 2.0.
82 */
70 */
83
71
84 /*!
72 /*!
85 \qmlproperty Qt::PenStyle LineSeries::style
73 \qmlproperty Qt::PenStyle LineSeries::style
86 Controls the style of the line. Set to one of Qt.NoPen, Qt.SolidLine, Qt.DashLine, Qt.DotLine,
74 Controls the style of the line. Set to one of Qt.NoPen, Qt.SolidLine, Qt.DashLine, Qt.DotLine,
87 Qt.DashDotLine or Qt.DashDotDotLine. Using Qt.CustomDashLine is not supported in the QML API.
75 Qt.DashDotLine or Qt.DashDotDotLine. Using Qt.CustomDashLine is not supported in the QML API.
88 By default the style is Qt.SolidLine.
76 By default the style is Qt.SolidLine.
89 */
77 */
90
78
91 /*!
79 /*!
92 \qmlproperty Qt::PenCapStyle LineSeries::capStyle
80 \qmlproperty Qt::PenCapStyle LineSeries::capStyle
93 Controls the cap style of the line. Set to one of Qt.FlatCap, Qt.SquareCap or Qt.RoundCap. By
81 Controls the cap style of the line. Set to one of Qt.FlatCap, Qt.SquareCap or Qt.RoundCap. By
94 default the cap style is Qt.SquareCap.
82 default the cap style is Qt.SquareCap.
95 */
83 */
96
84
97 /*!
85 /*!
98 Constructs empty series object which is a child of \a parent.
86 Constructs empty series object which is a child of \a parent.
99 When series object is added to QChartView or QChart instance ownerships is transferred.
87 When series object is added to QChartView or QChart instance ownerships is transferred.
100 */
88 */
101 QLineSeries::QLineSeries(QObject *parent)
89 QLineSeries::QLineSeries(QObject *parent)
102 : QXYSeries(*new QLineSeriesPrivate(this), parent)
90 : QXYSeries(*new QLineSeriesPrivate(this), parent)
103 {
91 {
104
92
105 }
93 }
106
94
107 /*!
95 /*!
108 \internal
96 \internal
109 */
97 */
110 QLineSeries::QLineSeries(QLineSeriesPrivate &d, QObject *parent)
98 QLineSeries::QLineSeries(QLineSeriesPrivate &d, QObject *parent)
111 : QXYSeries(d, parent)
99 : QXYSeries(d, parent)
112 {
100 {
113
101
114 }
102 }
115 /*!
103 /*!
116 Destroys the object. Series added to QChartView or QChart instances are owned by those,
104 Destroys the object. Series added to QChartView or QChart instances are owned by those,
117 and are deleted when mentioned object are destroyed.
105 and are deleted when mentioned object are destroyed.
118 */
106 */
119 QLineSeries::~QLineSeries()
107 QLineSeries::~QLineSeries()
120 {
108 {
121 Q_D(QLineSeries);
109 Q_D(QLineSeries);
122 if (d->m_chart)
110 if (d->m_chart)
123 d->m_chart->removeSeries(this);
111 d->m_chart->removeSeries(this);
124 }
112 }
125
113
126 QAbstractSeries::SeriesType QLineSeries::type() const
114 QAbstractSeries::SeriesType QLineSeries::type() const
127 {
115 {
128 return QAbstractSeries::SeriesTypeLine;
116 return QAbstractSeries::SeriesTypeLine;
129 }
117 }
130
118
131 /*
119 /*
132 QDebug operator<< (QDebug debug, const QLineSeries series)
120 QDebug operator<< (QDebug debug, const QLineSeries series)
133 {
121 {
134 Q_ASSERT(series.d_func()->m_x.size() == series.d_func()->m_y.size());
122 Q_ASSERT(series.d_func()->m_x.size() == series.d_func()->m_y.size());
135 int size = series.d_func()->m_x.size();
123 int size = series.d_func()->m_x.size();
136 for (int i=0; i<size; i++) {
124 for (int i=0; i<size; i++) {
137 debug.nospace() << "(" << series.d_func()->m_x.at(i) << ','<< series.d_func()->m_y.at(i) << ") ";
125 debug.nospace() << "(" << series.d_func()->m_x.at(i) << ','<< series.d_func()->m_y.at(i) << ") ";
138 }
126 }
139 return debug.space();
127 return debug.space();
140 }
128 }
141 */
129 */
142
130
143 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
131 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
144
132
145 QLineSeriesPrivate::QLineSeriesPrivate(QLineSeries *q)
133 QLineSeriesPrivate::QLineSeriesPrivate(QLineSeries *q)
146 : QXYSeriesPrivate(q)
134 : QXYSeriesPrivate(q)
147 {
135 {
148
136
149 };
137 };
150
138
151 void QLineSeriesPrivate::initializeGraphics(QGraphicsItem *parent)
139 void QLineSeriesPrivate::initializeGraphics(QGraphicsItem *parent)
152 {
140 {
153 Q_Q(QLineSeries);
141 Q_Q(QLineSeries);
154 LineChartItem *line = new LineChartItem(q,parent);
142 LineChartItem *line = new LineChartItem(q,parent);
155 m_item.reset(line);
143 m_item.reset(line);
156 QAbstractSeriesPrivate::initializeGraphics(parent);
144 QAbstractSeriesPrivate::initializeGraphics(parent);
157 }
145 }
158
146
159 void QLineSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced)
147 void QLineSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced)
160 {
148 {
161 Q_Q(QLineSeries);
149 Q_Q(QLineSeries);
162 const QList<QColor> colors = theme->seriesColors();
150 const QList<QColor> colors = theme->seriesColors();
163
151
164 QPen pen;
152 QPen pen;
165 if (forced || pen == m_pen) {
153 if (forced || pen == m_pen) {
166 pen.setColor(colors.at(index % colors.size()));
154 pen.setColor(colors.at(index % colors.size()));
167 pen.setWidthF(2);
155 pen.setWidthF(2);
168 q->setPen(pen);
156 q->setPen(pen);
169 }
157 }
170 }
158 }
171
159
172 #include "moc_qlineseries.cpp"
160 #include "moc_qlineseries.cpp"
173
161
174 QTCOMMERCIALCHART_END_NAMESPACE
162 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,284 +1,272
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 /*!
64 /*!
65 \qmlproperty AbstractAxis SplineSeries::axisX
66 The x axis used for the series. If you leave the x axis undefined, a ValueAxis is created for
67 the series.
68 */
69
70 /*!
71 \qmlproperty AbstractAxis SplineSeries::axisY
72 The y axis used for the series. If you leave the y axis undefined, a ValueAxis is created for
73 the series.
74 */
75
76 /*!
77 \qmlproperty real SplineSeries::width
65 \qmlproperty real SplineSeries::width
78 The width of the line. By default the width is 2.0.
66 The width of the line. By default the width is 2.0.
79 */
67 */
80
68
81 /*!
69 /*!
82 \qmlproperty Qt::PenStyle SplineSeries::style
70 \qmlproperty Qt::PenStyle SplineSeries::style
83 Controls the style of the line. Set to one of Qt.NoPen, Qt.SolidLine, Qt.DashLine, Qt.DotLine,
71 Controls the style of the line. Set to one of Qt.NoPen, Qt.SolidLine, Qt.DashLine, Qt.DotLine,
84 Qt.DashDotLine or Qt.DashDotDotLine. Using Qt.CustomDashLine is not supported in the QML API.
72 Qt.DashDotLine or Qt.DashDotDotLine. Using Qt.CustomDashLine is not supported in the QML API.
85 By default the style is Qt.SolidLine.
73 By default the style is Qt.SolidLine.
86 */
74 */
87
75
88 /*!
76 /*!
89 \qmlproperty Qt::PenCapStyle SplineSeries::capStyle
77 \qmlproperty Qt::PenCapStyle SplineSeries::capStyle
90 Controls the cap style of the line. Set to one of Qt.FlatCap, Qt.SquareCap or Qt.RoundCap. By
78 Controls the cap style of the line. Set to one of Qt.FlatCap, Qt.SquareCap or Qt.RoundCap. By
91 default the cap style is Qt.SquareCap.
79 default the cap style is Qt.SquareCap.
92 */
80 */
93
81
94 QTCOMMERCIALCHART_BEGIN_NAMESPACE
82 QTCOMMERCIALCHART_BEGIN_NAMESPACE
95
83
96 /*!
84 /*!
97 Constructs empty series object which is a child of \a parent.
85 Constructs empty series object which is a child of \a parent.
98 When series object is added to QChartView or QChart instance then the ownerships is transferred.
86 When series object is added to QChartView or QChart instance then the ownerships is transferred.
99 */
87 */
100
88
101 QSplineSeries::QSplineSeries(QObject *parent)
89 QSplineSeries::QSplineSeries(QObject *parent)
102 : QLineSeries(*new QSplineSeriesPrivate(this), parent)
90 : QLineSeries(*new QSplineSeriesPrivate(this), parent)
103 {
91 {
104 Q_D(QSplineSeries);
92 Q_D(QSplineSeries);
105 QObject::connect(this, SIGNAL(pointAdded(int)), d, SLOT(updateControlPoints()));
93 QObject::connect(this, SIGNAL(pointAdded(int)), d, SLOT(updateControlPoints()));
106 QObject::connect(this, SIGNAL(pointRemoved(int)), d, SLOT(updateControlPoints()));
94 QObject::connect(this, SIGNAL(pointRemoved(int)), d, SLOT(updateControlPoints()));
107 QObject::connect(this, SIGNAL(pointReplaced(int)), d, SLOT(updateControlPoints()));
95 QObject::connect(this, SIGNAL(pointReplaced(int)), d, SLOT(updateControlPoints()));
108 QObject::connect(this, SIGNAL(pointsReplaced()), d, SLOT(updateControlPoints()));
96 QObject::connect(this, SIGNAL(pointsReplaced()), d, SLOT(updateControlPoints()));
109 }
97 }
110
98
111 /*!
99 /*!
112 Destroys the object.
100 Destroys the object.
113 */
101 */
114 QSplineSeries::~QSplineSeries()
102 QSplineSeries::~QSplineSeries()
115 {
103 {
116 Q_D(QSplineSeries);
104 Q_D(QSplineSeries);
117 if (d->m_chart)
105 if (d->m_chart)
118 d->m_chart->removeSeries(this);
106 d->m_chart->removeSeries(this);
119 }
107 }
120
108
121 QAbstractSeries::SeriesType QSplineSeries::type() const
109 QAbstractSeries::SeriesType QSplineSeries::type() const
122 {
110 {
123 return QAbstractSeries::SeriesTypeSpline;
111 return QAbstractSeries::SeriesTypeSpline;
124 }
112 }
125
113
126 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
114 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
127
115
128 QSplineSeriesPrivate::QSplineSeriesPrivate(QSplineSeries *q)
116 QSplineSeriesPrivate::QSplineSeriesPrivate(QSplineSeries *q)
129 : QLineSeriesPrivate(q)
117 : QLineSeriesPrivate(q)
130 {
118 {
131 }
119 }
132
120
133 /*!
121 /*!
134 Calculates control points which are needed by QPainterPath.cubicTo function to draw the cubic Bezier cureve between two points.
122 Calculates control points which are needed by QPainterPath.cubicTo function to draw the cubic Bezier cureve between two points.
135 */
123 */
136 void QSplineSeriesPrivate::calculateControlPoints()
124 void QSplineSeriesPrivate::calculateControlPoints()
137 {
125 {
138 Q_Q(QSplineSeries);
126 Q_Q(QSplineSeries);
139
127
140 const QList<QPointF>& points = q->points();
128 const QList<QPointF>& points = q->points();
141
129
142 int n = points.count() - 1;
130 int n = points.count() - 1;
143
131
144 if (n == 1) {
132 if (n == 1) {
145 //for n==1
133 //for n==1
146 m_controlPoints[0].setX((2 * points[0].x() + points[1].x()) / 3);
134 m_controlPoints[0].setX((2 * points[0].x() + points[1].x()) / 3);
147 m_controlPoints[0].setY((2 * points[0].y() + points[1].y()) / 3);
135 m_controlPoints[0].setY((2 * points[0].y() + points[1].y()) / 3);
148 m_controlPoints[1].setX(2 * m_controlPoints[0].x() - points[0].x());
136 m_controlPoints[1].setX(2 * m_controlPoints[0].x() - points[0].x());
149 m_controlPoints[1].setY(2 * m_controlPoints[0].y() - points[0].y());
137 m_controlPoints[1].setY(2 * m_controlPoints[0].y() - points[0].y());
150 return;
138 return;
151 }
139 }
152
140
153 // Calculate first Bezier control points
141 // Calculate first Bezier control points
154 // Set of equations for P0 to Pn points.
142 // Set of equations for P0 to Pn points.
155 //
143 //
156 // | 2 1 0 0 ... 0 0 0 ... 0 0 0 | | P1_1 | | P0 + 2 * P1 |
144 // | 2 1 0 0 ... 0 0 0 ... 0 0 0 | | P1_1 | | P0 + 2 * P1 |
157 // | 1 4 1 0 ... 0 0 0 ... 0 0 0 | | P1_2 | | 4 * P1 + 2 * P2 |
145 // | 1 4 1 0 ... 0 0 0 ... 0 0 0 | | P1_2 | | 4 * P1 + 2 * P2 |
158 // | 0 1 4 1 ... 0 0 0 ... 0 0 0 | | P1_3 | | 4 * P2 + 2 * P3 |
146 // | 0 1 4 1 ... 0 0 0 ... 0 0 0 | | P1_3 | | 4 * P2 + 2 * P3 |
159 // | . . . . . . . . . . . . | | ... | | ... |
147 // | . . . . . . . . . . . . | | ... | | ... |
160 // | 0 0 0 0 ... 1 4 1 ... 0 0 0 | * | P1_i | = | 4 * P(i-1) + 2 * Pi |
148 // | 0 0 0 0 ... 1 4 1 ... 0 0 0 | * | P1_i | = | 4 * P(i-1) + 2 * Pi |
161 // | . . . . . . . . . . . . | | ... | | ... |
149 // | . . . . . . . . . . . . | | ... | | ... |
162 // | 0 0 0 0 0 0 0 0 ... 1 4 1 | | P1_(n-1)| | 4 * P(n-2) + 2 * P(n-1) |
150 // | 0 0 0 0 0 0 0 0 ... 1 4 1 | | P1_(n-1)| | 4 * P(n-2) + 2 * P(n-1) |
163 // | 0 0 0 0 0 0 0 0 ... 0 2 7 | | P1_n | | 8 * P(n-1) + Pn |
151 // | 0 0 0 0 0 0 0 0 ... 0 2 7 | | P1_n | | 8 * P(n-1) + Pn |
164 //
152 //
165 QVector<qreal> vector;
153 QVector<qreal> vector;
166 vector.resize(n);
154 vector.resize(n);
167
155
168 vector[0] = points[0].x() + 2 * points[1].x();
156 vector[0] = points[0].x() + 2 * points[1].x();
169
157
170
158
171 for (int i = 1; i < n - 1; ++i)
159 for (int i = 1; i < n - 1; ++i)
172 vector[i] = 4 * points[i].x() + 2 * points[i + 1].x();
160 vector[i] = 4 * points[i].x() + 2 * points[i + 1].x();
173
161
174 vector[n - 1] = (8 * points[n - 1].x() + points[n].x()) / 2.0;
162 vector[n - 1] = (8 * points[n - 1].x() + points[n].x()) / 2.0;
175
163
176 QVector<qreal> xControl = firstControlPoints(vector);
164 QVector<qreal> xControl = firstControlPoints(vector);
177
165
178 vector[0] = points[0].y() + 2 * points[1].y();
166 vector[0] = points[0].y() + 2 * points[1].y();
179
167
180 for (int i = 1; i < n - 1; ++i)
168 for (int i = 1; i < n - 1; ++i)
181 vector[i] = 4 * points[i].y() + 2 * points[i + 1].y();
169 vector[i] = 4 * points[i].y() + 2 * points[i + 1].y();
182
170
183 vector[n - 1] = (8 * points[n - 1].y() + points[n].y()) / 2.0;
171 vector[n - 1] = (8 * points[n - 1].y() + points[n].y()) / 2.0;
184
172
185 QVector<qreal> yControl = firstControlPoints(vector);
173 QVector<qreal> yControl = firstControlPoints(vector);
186
174
187 for (int i = 0, j = 0; i < n; ++i, ++j) {
175 for (int i = 0, j = 0; i < n; ++i, ++j) {
188
176
189 m_controlPoints[j].setX(xControl[i]);
177 m_controlPoints[j].setX(xControl[i]);
190 m_controlPoints[j].setY(yControl[i]);
178 m_controlPoints[j].setY(yControl[i]);
191
179
192 j++;
180 j++;
193
181
194 if (i < n - 1) {
182 if (i < n - 1) {
195 m_controlPoints[j].setX(2 * points[i + 1].x() - xControl[i + 1]);
183 m_controlPoints[j].setX(2 * points[i + 1].x() - xControl[i + 1]);
196 m_controlPoints[j].setY(2 * points[i + 1].y() - yControl[i + 1]);
184 m_controlPoints[j].setY(2 * points[i + 1].y() - yControl[i + 1]);
197 } else {
185 } else {
198 m_controlPoints[j].setX((points[n].x() + xControl[n - 1]) / 2);
186 m_controlPoints[j].setX((points[n].x() + xControl[n - 1]) / 2);
199 m_controlPoints[j].setY((points[n].y() + yControl[n - 1]) / 2);
187 m_controlPoints[j].setY((points[n].y() + yControl[n - 1]) / 2);
200 }
188 }
201 }
189 }
202 }
190 }
203
191
204 QVector<qreal> QSplineSeriesPrivate::firstControlPoints(const QVector<qreal>& vector)
192 QVector<qreal> QSplineSeriesPrivate::firstControlPoints(const QVector<qreal>& vector)
205 {
193 {
206 QVector<qreal> result;
194 QVector<qreal> result;
207
195
208 int count = vector.count();
196 int count = vector.count();
209 result.resize(count);
197 result.resize(count);
210 result[0] = vector[0] / 2.0;
198 result[0] = vector[0] / 2.0;
211
199
212 QVector<qreal> temp;
200 QVector<qreal> temp;
213 temp.resize(count);
201 temp.resize(count);
214 temp[0] = 0;
202 temp[0] = 0;
215
203
216 qreal b = 2.0;
204 qreal b = 2.0;
217
205
218 for (int i = 1; i < count; i++) {
206 for (int i = 1; i < count; i++) {
219 temp[i] = 1 / b;
207 temp[i] = 1 / b;
220 b = (i < count - 1 ? 4.0 : 3.5) - temp[i];
208 b = (i < count - 1 ? 4.0 : 3.5) - temp[i];
221 result[i] = (vector[i] - result[i - 1]) / b;
209 result[i] = (vector[i] - result[i - 1]) / b;
222 }
210 }
223
211
224 for (int i = 1; i < count; i++)
212 for (int i = 1; i < count; i++)
225 result[count - i - 1] -= temp[count - i] * result[count - i];
213 result[count - i - 1] -= temp[count - i] * result[count - i];
226
214
227 return result;
215 return result;
228 }
216 }
229
217
230 QPointF QSplineSeriesPrivate::controlPoint(int index) const
218 QPointF QSplineSeriesPrivate::controlPoint(int index) const
231 {
219 {
232 return m_controlPoints[index];
220 return m_controlPoints[index];
233 }
221 }
234
222
235 /*!
223 /*!
236 Updates the control points, besed on currently avaiable knots.
224 Updates the control points, besed on currently avaiable knots.
237 */
225 */
238 void QSplineSeriesPrivate::updateControlPoints()
226 void QSplineSeriesPrivate::updateControlPoints()
239 {
227 {
240 Q_Q(QSplineSeries);
228 Q_Q(QSplineSeries);
241 if (q->count() > 1) {
229 if (q->count() > 1) {
242 m_controlPoints.resize(2 * q->count() - 2);
230 m_controlPoints.resize(2 * q->count() - 2);
243 calculateControlPoints();
231 calculateControlPoints();
244 }
232 }
245 }
233 }
246
234
247 void QSplineSeriesPrivate::initializeGraphics(QGraphicsItem* parent)
235 void QSplineSeriesPrivate::initializeGraphics(QGraphicsItem* parent)
248 {
236 {
249 Q_Q(QSplineSeries);
237 Q_Q(QSplineSeries);
250 SplineChartItem *spline = new SplineChartItem(q,parent);
238 SplineChartItem *spline = new SplineChartItem(q,parent);
251 m_item.reset(spline);
239 m_item.reset(spline);
252 QAbstractSeriesPrivate::initializeGraphics(parent);
240 QAbstractSeriesPrivate::initializeGraphics(parent);
253 }
241 }
254
242
255 void QSplineSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced)
243 void QSplineSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced)
256 {
244 {
257 Q_Q(QSplineSeries);
245 Q_Q(QSplineSeries);
258 const QList<QColor> colors = theme->seriesColors();
246 const QList<QColor> colors = theme->seriesColors();
259
247
260 QPen pen;
248 QPen pen;
261 if (forced || pen == m_pen) {
249 if (forced || pen == m_pen) {
262 pen.setColor(colors.at(index % colors.size()));
250 pen.setColor(colors.at(index % colors.size()));
263 pen.setWidthF(2);
251 pen.setWidthF(2);
264 q->setPen(pen);
252 q->setPen(pen);
265 }
253 }
266 }
254 }
267
255
268
256
269 void QSplineSeriesPrivate::initializeAnimations(QtCommercialChart::QChart::AnimationOptions options)
257 void QSplineSeriesPrivate::initializeAnimations(QtCommercialChart::QChart::AnimationOptions options)
270 {
258 {
271 SplineChartItem *item = static_cast<SplineChartItem *>(m_item.data());
259 SplineChartItem *item = static_cast<SplineChartItem *>(m_item.data());
272 Q_ASSERT(item);
260 Q_ASSERT(item);
273 if (options.testFlag(QChart::SeriesAnimations)) {
261 if (options.testFlag(QChart::SeriesAnimations)) {
274 item->setAnimation(new SplineAnimation(item));
262 item->setAnimation(new SplineAnimation(item));
275 }else{
263 }else{
276 item->setAnimation(0);
264 item->setAnimation(0);
277 }
265 }
278 QAbstractSeriesPrivate::initializeAnimations(options);
266 QAbstractSeriesPrivate::initializeAnimations(options);
279 }
267 }
280
268
281 #include "moc_qsplineseries.cpp"
269 #include "moc_qsplineseries.cpp"
282 #include "moc_qsplineseries_p.cpp"
270 #include "moc_qsplineseries_p.cpp"
283
271
284 QTCOMMERCIALCHART_END_NAMESPACE
272 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,490 +1,518
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 "abstractdomain_p.h"
23 #include "abstractdomain_p.h"
24 #include "qvalueaxis.h"
24 #include "qvalueaxis.h"
25 #include "xychart_p.h"
25 #include "xychart_p.h"
26 #include "qxylegendmarker.h"
26 #include "qxylegendmarker.h"
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 /*!
30 /*!
31 \class QXYSeries
31 \class QXYSeries
32 \brief The QXYSeries class is a base class for line, spline and scatter series.
32 \brief The QXYSeries class is a base class for line, spline and scatter series.
33 */
33 */
34 /*!
34 /*!
35 \qmlclass XYSeries
35 \qmlclass XYSeries
36 \inherits AbstractSeries
36 \inherits AbstractSeries
37 The XYSeries class is a base class for line, spline and scatter series.
37 The XYSeries class is a base class for line, spline and scatter series.
38
38
39 The class cannot be instantiated directly.
39 The class cannot be instantiated directly.
40 */
40 */
41
41
42 /*!
42 /*!
43 \qmlproperty AbstractAxis XYSeries::axisX
44 The x axis used for the series. If you leave both axisX and axisXTop undefined, a ValueAxis is created for
45 the series.
46 \sa axisXTop
47 */
48
49 /*!
50 \qmlproperty AbstractAxis XYSeries::axisY
51 The y axis used for the series. If you leave both axisY and axisYRight undefined, a ValueAxis is created for
52 the series.
53 \sa axisYRight
54 */
55
56 /*!
57 \qmlproperty AbstractAxis XYSeries::axisXTop
58 The x axis used for the series, drawn on top of the chart view. Note that you can only provide either axisX or
59 axisXTop, but not both.
60 \sa axisX
61 */
62
63 /*!
64 \qmlproperty AbstractAxis XYSeries::axisYRight
65 The y axis used for the series, drawn to the right on the chart view. Note that you can only provide either axisY
66 or axisYRight, but not both.
67 \sa axisY
68 */
69
70 /*!
43 \property QXYSeries::pointsVisible
71 \property QXYSeries::pointsVisible
44 Controls if the data points are visible and should be drawn.
72 Controls if the data points are visible and should be drawn.
45 */
73 */
46 /*!
74 /*!
47 \qmlproperty bool XYSeries::pointsVisible
75 \qmlproperty bool XYSeries::pointsVisible
48 Controls if the data points are visible and should be drawn.
76 Controls if the data points are visible and should be drawn.
49 */
77 */
50
78
51 /*!
79 /*!
52 \fn QPen QXYSeries::pen() const
80 \fn QPen QXYSeries::pen() const
53 \brief Returns pen used to draw points for series.
81 \brief Returns pen used to draw points for series.
54 \sa setPen()
82 \sa setPen()
55 */
83 */
56
84
57 /*!
85 /*!
58 \fn QBrush QXYSeries::brush() const
86 \fn QBrush QXYSeries::brush() const
59 \brief Returns brush used to draw points for series.
87 \brief Returns brush used to draw points for series.
60 \sa setBrush()
88 \sa setBrush()
61 */
89 */
62
90
63 /*!
91 /*!
64 \property QXYSeries::color
92 \property QXYSeries::color
65 The color of the series. This is line (pen) color in case of QLineSeries or QSplineSeries and
93 The color of the series. This is line (pen) color in case of QLineSeries or QSplineSeries and
66 fill (brush) color in case of QScatterSeries or QAreaSeries.
94 fill (brush) color in case of QScatterSeries or QAreaSeries.
67 \sa QXYSeries::pen(), QXYSeries::brush()
95 \sa QXYSeries::pen(), QXYSeries::brush()
68 */
96 */
69 /*!
97 /*!
70 \qmlproperty color XYSeries::color
98 \qmlproperty color XYSeries::color
71 The color of the series. This is line (pen) color in case of LineSeries or SplineSeries and
99 The color of the series. This is line (pen) color in case of LineSeries or SplineSeries and
72 fill (brush) color in case of ScatterSeries or AreaSeries.
100 fill (brush) color in case of ScatterSeries or AreaSeries.
73 */
101 */
74
102
75 /*!
103 /*!
76 \fn void QXYSeries::clicked(const QPointF& point)
104 \fn void QXYSeries::clicked(const QPointF& point)
77 \brief Signal is emitted when user clicks the \a point on chart.
105 \brief Signal is emitted when user clicks the \a point on chart.
78 */
106 */
79 /*!
107 /*!
80 \qmlsignal XYSeries::onClicked(QPointF point)
108 \qmlsignal XYSeries::onClicked(QPointF point)
81 Signal is emitted when user clicks the \a point on chart. For example:
109 Signal is emitted when user clicks the \a point on chart. For example:
82 \code
110 \code
83 LineSeries {
111 LineSeries {
84 XYPoint { x: 0; y: 0 }
112 XYPoint { x: 0; y: 0 }
85 XYPoint { x: 1.1; y: 2.1 }
113 XYPoint { x: 1.1; y: 2.1 }
86 onClicked: console.log("onClicked: " + point.x + ", " + point.y);
114 onClicked: console.log("onClicked: " + point.x + ", " + point.y);
87 }
115 }
88 \endcode
116 \endcode
89 */
117 */
90
118
91 /*!
119 /*!
92 \fn void QXYSeries::pointReplaced(int index)
120 \fn void QXYSeries::pointReplaced(int index)
93 Signal is emitted when a point has been replaced at \a index.
121 Signal is emitted when a point has been replaced at \a index.
94 \sa replace()
122 \sa replace()
95 */
123 */
96 /*!
124 /*!
97 \qmlsignal XYSeries::onPointReplaced(int index)
125 \qmlsignal XYSeries::onPointReplaced(int index)
98 Signal is emitted when a point has been replaced at \a index.
126 Signal is emitted when a point has been replaced at \a index.
99 */
127 */
100
128
101 /*!
129 /*!
102 \fn void QXYSeries::pointsReplaced()
130 \fn void QXYSeries::pointsReplaced()
103 Signal is emitted when all points have been replaced with another points.
131 Signal is emitted when all points have been replaced with another points.
104 \sa replace()
132 \sa replace()
105 */
133 */
106 /*!
134 /*!
107 \qmlsignal XYSeries::onPointsReplaced()
135 \qmlsignal XYSeries::onPointsReplaced()
108 */
136 */
109
137
110 /*!
138 /*!
111 \fn void QXYSeries::pointAdded(int index)
139 \fn void QXYSeries::pointAdded(int index)
112 Signal is emitted when a point has been added at \a index.
140 Signal is emitted when a point has been added at \a index.
113 \sa append(), insert()
141 \sa append(), insert()
114 */
142 */
115 /*!
143 /*!
116 \qmlsignal XYSeries::onPointAdded(int index)
144 \qmlsignal XYSeries::onPointAdded(int index)
117 Signal is emitted when a point has been added at \a index.
145 Signal is emitted when a point has been added at \a index.
118 */
146 */
119
147
120 /*!
148 /*!
121 \fn void QXYSeries::pointRemoved(int index)
149 \fn void QXYSeries::pointRemoved(int index)
122 Signal is emitted when a point has been removed from \a index.
150 Signal is emitted when a point has been removed from \a index.
123 \sa remove()
151 \sa remove()
124 */
152 */
125 /*!
153 /*!
126 \qmlsignal XYSeries::onPointRemoved(int index)
154 \qmlsignal XYSeries::onPointRemoved(int index)
127 Signal is emitted when a point has been removed from \a index.
155 Signal is emitted when a point has been removed from \a index.
128 */
156 */
129
157
130 /*!
158 /*!
131 \fn void QXYSeries::colorChanged(QColor color)
159 \fn void QXYSeries::colorChanged(QColor color)
132 \brief Signal is emitted when the line (pen) color has changed to \a color.
160 \brief Signal is emitted when the line (pen) color has changed to \a color.
133 */
161 */
134 /*!
162 /*!
135 \qmlsignal XYSeries::onColorChanged(color color)
163 \qmlsignal XYSeries::onColorChanged(color color)
136 Signal is emitted when the line (pen) color has changed to \a color.
164 Signal is emitted when the line (pen) color has changed to \a color.
137 */
165 */
138
166
139 /*!
167 /*!
140 \fn void QXYSeriesPrivate::updated()
168 \fn void QXYSeriesPrivate::updated()
141 \brief \internal
169 \brief \internal
142 */
170 */
143
171
144 /*!
172 /*!
145 \qmlmethod XYSeries::append(real x, real y)
173 \qmlmethod XYSeries::append(real x, real y)
146 Append point (\a x, \a y) to the series
174 Append point (\a x, \a y) to the series
147 */
175 */
148
176
149 /*!
177 /*!
150 \qmlmethod XYSeries::replace(real oldX, real oldY, real newX, real newY)
178 \qmlmethod XYSeries::replace(real oldX, real oldY, real newX, real newY)
151 Replaces point (\a oldX, \a oldY) with point (\a newX, \a newY). Does nothing, if point (oldX, oldY) does not
179 Replaces point (\a oldX, \a oldY) with point (\a newX, \a newY). Does nothing, if point (oldX, oldY) does not
152 exist.
180 exist.
153 */
181 */
154
182
155 /*!
183 /*!
156 \qmlmethod XYSeries::remove(real x, real y)
184 \qmlmethod XYSeries::remove(real x, real y)
157 Removes point (\a x, \a y) from the series. Does nothing, if point (x, y) does not exist.
185 Removes point (\a x, \a y) from the series. Does nothing, if point (x, y) does not exist.
158 */
186 */
159
187
160 /*!
188 /*!
161 \qmlmethod XYSeries::insert(int index, real x, real y)
189 \qmlmethod XYSeries::insert(int index, real x, real y)
162 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
190 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
163 points. If index is the same as or bigger than count, the point is appended to the list of points.
191 points. If index is the same as or bigger than count, the point is appended to the list of points.
164 */
192 */
165
193
166 /*!
194 /*!
167 \qmlmethod QPointF XYSeries::at(int index)
195 \qmlmethod QPointF XYSeries::at(int index)
168 Returns point at \a index. Returns (0, 0) if the index is not valid.
196 Returns point at \a index. Returns (0, 0) if the index is not valid.
169 */
197 */
170
198
171 /*!
199 /*!
172 \internal
200 \internal
173
201
174 Constructs empty series object which is a child of \a parent.
202 Constructs empty series object which is a child of \a parent.
175 When series object is added to QChartView or QChart instance ownerships is transferred.
203 When series object is added to QChartView or QChart instance ownerships is transferred.
176 */
204 */
177 QXYSeries::QXYSeries(QXYSeriesPrivate &d, QObject *parent)
205 QXYSeries::QXYSeries(QXYSeriesPrivate &d, QObject *parent)
178 : QAbstractSeries(d, parent)
206 : QAbstractSeries(d, parent)
179 {
207 {
180 }
208 }
181
209
182 /*!
210 /*!
183 Destroys the object. Series added to QChartView or QChart instances are owned by those,
211 Destroys the object. Series added to QChartView or QChart instances are owned by those,
184 and are deleted when mentioned object are destroyed.
212 and are deleted when mentioned object are destroyed.
185 */
213 */
186 QXYSeries::~QXYSeries()
214 QXYSeries::~QXYSeries()
187 {
215 {
188 }
216 }
189
217
190 /*!
218 /*!
191 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
219 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
192 */
220 */
193 void QXYSeries::append(qreal x, qreal y)
221 void QXYSeries::append(qreal x, qreal y)
194 {
222 {
195 append(QPointF(x, y));
223 append(QPointF(x, y));
196 }
224 }
197
225
198 /*!
226 /*!
199 This is an overloaded function.
227 This is an overloaded function.
200 Adds data \a point to the series. Points are connected with lines on the chart.
228 Adds data \a point to the series. Points are connected with lines on the chart.
201 */
229 */
202 void QXYSeries::append(const QPointF &point)
230 void QXYSeries::append(const QPointF &point)
203 {
231 {
204 Q_D(QXYSeries);
232 Q_D(QXYSeries);
205 d->m_points << point;
233 d->m_points << point;
206 emit pointAdded(d->m_points.count() - 1);
234 emit pointAdded(d->m_points.count() - 1);
207 }
235 }
208
236
209 /*!
237 /*!
210 This is an overloaded function.
238 This is an overloaded function.
211 Adds list of data \a points to the series. Points are connected with lines on the chart.
239 Adds list of data \a points to the series. Points are connected with lines on the chart.
212 */
240 */
213 void QXYSeries::append(const QList<QPointF> &points)
241 void QXYSeries::append(const QList<QPointF> &points)
214 {
242 {
215 foreach (const QPointF &point , points)
243 foreach (const QPointF &point , points)
216 append(point);
244 append(point);
217 }
245 }
218
246
219 /*!
247 /*!
220 Replaces data point \a oldX \a oldY with data point \a newX \a newY.
248 Replaces data point \a oldX \a oldY with data point \a newX \a newY.
221 \sa QXYSeries::pointReplaced()
249 \sa QXYSeries::pointReplaced()
222 */
250 */
223 void QXYSeries::replace(qreal oldX, qreal oldY, qreal newX, qreal newY)
251 void QXYSeries::replace(qreal oldX, qreal oldY, qreal newX, qreal newY)
224 {
252 {
225 replace(QPointF(oldX, oldY), QPointF(newX, newY));
253 replace(QPointF(oldX, oldY), QPointF(newX, newY));
226 }
254 }
227
255
228 /*!
256 /*!
229 Replaces \a oldPoint with \a newPoint.
257 Replaces \a oldPoint with \a newPoint.
230 \sa QXYSeries::pointReplaced()
258 \sa QXYSeries::pointReplaced()
231 */
259 */
232 void QXYSeries::replace(const QPointF &oldPoint, const QPointF &newPoint)
260 void QXYSeries::replace(const QPointF &oldPoint, const QPointF &newPoint)
233 {
261 {
234 Q_D(QXYSeries);
262 Q_D(QXYSeries);
235 int index = d->m_points.indexOf(oldPoint);
263 int index = d->m_points.indexOf(oldPoint);
236 if (index == -1)
264 if (index == -1)
237 return;
265 return;
238 d->m_points[index] = newPoint;
266 d->m_points[index] = newPoint;
239 emit pointReplaced(index);
267 emit pointReplaced(index);
240 }
268 }
241
269
242 /*!
270 /*!
243 Replaces the current points with \a points. This is faster than replacing data points one by one,
271 Replaces the current points with \a points. This is faster than replacing data points one by one,
244 or first clearing all data, and then appending the new data. Emits QXYSeries::pointsReplaced()
272 or first clearing all data, and then appending the new data. Emits QXYSeries::pointsReplaced()
245 when the points have been replaced.
273 when the points have been replaced.
246 \sa QXYSeries::pointsReplaced()
274 \sa QXYSeries::pointsReplaced()
247 */
275 */
248 void QXYSeries::replace(QList<QPointF> points)
276 void QXYSeries::replace(QList<QPointF> points)
249 {
277 {
250 Q_D(QXYSeries);
278 Q_D(QXYSeries);
251 d->m_points = points.toVector();
279 d->m_points = points.toVector();
252 emit pointsReplaced();
280 emit pointsReplaced();
253 }
281 }
254
282
255 /*!
283 /*!
256 Removes current \a x and \a y value.
284 Removes current \a x and \a y value.
257 */
285 */
258 void QXYSeries::remove(qreal x, qreal y)
286 void QXYSeries::remove(qreal x, qreal y)
259 {
287 {
260 remove(QPointF(x, y));
288 remove(QPointF(x, y));
261 }
289 }
262
290
263 /*!
291 /*!
264 Removes current \a point x value.
292 Removes current \a point x value.
265
293
266 Note: point y value is ignored.
294 Note: point y value is ignored.
267 */
295 */
268 void QXYSeries::remove(const QPointF &point)
296 void QXYSeries::remove(const QPointF &point)
269 {
297 {
270 Q_D(QXYSeries);
298 Q_D(QXYSeries);
271 int index = d->m_points.indexOf(point);
299 int index = d->m_points.indexOf(point);
272 if (index == -1)
300 if (index == -1)
273 return;
301 return;
274 d->m_points.remove(index);
302 d->m_points.remove(index);
275 emit pointRemoved(index);
303 emit pointRemoved(index);
276 }
304 }
277
305
278 /*!
306 /*!
279 Inserts a \a point in the series at \a index position.
307 Inserts a \a point in the series at \a index position.
280 */
308 */
281 void QXYSeries::insert(int index, const QPointF &point)
309 void QXYSeries::insert(int index, const QPointF &point)
282 {
310 {
283 Q_D(QXYSeries);
311 Q_D(QXYSeries);
284 d->m_points.insert(index, point);
312 d->m_points.insert(index, point);
285 emit pointAdded(index);
313 emit pointAdded(index);
286 }
314 }
287
315
288 /*!
316 /*!
289 Removes all points from the series.
317 Removes all points from the series.
290 */
318 */
291 void QXYSeries::clear()
319 void QXYSeries::clear()
292 {
320 {
293 Q_D(QXYSeries);
321 Q_D(QXYSeries);
294 for (int i = d->m_points.size() - 1; i >= 0; i--)
322 for (int i = d->m_points.size() - 1; i >= 0; i--)
295 remove(d->m_points.at(i));
323 remove(d->m_points.at(i));
296 }
324 }
297
325
298 /*!
326 /*!
299 Returns list of points in the series.
327 Returns list of points in the series.
300 */
328 */
301 QList<QPointF> QXYSeries::points() const
329 QList<QPointF> QXYSeries::points() const
302 {
330 {
303 Q_D(const QXYSeries);
331 Q_D(const QXYSeries);
304 return d->m_points.toList();
332 return d->m_points.toList();
305 }
333 }
306
334
307 /*!
335 /*!
308 Returns number of data points within series.
336 Returns number of data points within series.
309 */
337 */
310 int QXYSeries::count() const
338 int QXYSeries::count() const
311 {
339 {
312 Q_D(const QXYSeries);
340 Q_D(const QXYSeries);
313 return d->m_points.count();
341 return d->m_points.count();
314 }
342 }
315
343
316
344
317 /*!
345 /*!
318 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
346 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
319 pen from chart theme is used.
347 pen from chart theme is used.
320 \sa QChart::setTheme()
348 \sa QChart::setTheme()
321 */
349 */
322 void QXYSeries::setPen(const QPen &pen)
350 void QXYSeries::setPen(const QPen &pen)
323 {
351 {
324 Q_D(QXYSeries);
352 Q_D(QXYSeries);
325 if (d->m_pen != pen) {
353 if (d->m_pen != pen) {
326 bool emitColorChanged = d->m_pen.color() != pen.color();
354 bool emitColorChanged = d->m_pen.color() != pen.color();
327 d->m_pen = pen;
355 d->m_pen = pen;
328 emit d->updated();
356 emit d->updated();
329 if (emitColorChanged)
357 if (emitColorChanged)
330 emit colorChanged(pen.color());
358 emit colorChanged(pen.color());
331 }
359 }
332 }
360 }
333
361
334 QPen QXYSeries::pen() const
362 QPen QXYSeries::pen() const
335 {
363 {
336 Q_D(const QXYSeries);
364 Q_D(const QXYSeries);
337 return d->m_pen;
365 return d->m_pen;
338 }
366 }
339
367
340 /*!
368 /*!
341 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
369 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
342 from chart theme setting is used.
370 from chart theme setting is used.
343 \sa QChart::setTheme()
371 \sa QChart::setTheme()
344 */
372 */
345 void QXYSeries::setBrush(const QBrush &brush)
373 void QXYSeries::setBrush(const QBrush &brush)
346 {
374 {
347 Q_D(QXYSeries);
375 Q_D(QXYSeries);
348 if (d->m_brush != brush) {
376 if (d->m_brush != brush) {
349 d->m_brush = brush;
377 d->m_brush = brush;
350 emit d->updated();
378 emit d->updated();
351 }
379 }
352 }
380 }
353
381
354 QBrush QXYSeries::brush() const
382 QBrush QXYSeries::brush() const
355 {
383 {
356 Q_D(const QXYSeries);
384 Q_D(const QXYSeries);
357 return d->m_brush;
385 return d->m_brush;
358 }
386 }
359
387
360 void QXYSeries::setColor(const QColor &color)
388 void QXYSeries::setColor(const QColor &color)
361 {
389 {
362 QPen p = pen();
390 QPen p = pen();
363 if (p.color() != color) {
391 if (p.color() != color) {
364 p.setColor(color);
392 p.setColor(color);
365 setPen(p);
393 setPen(p);
366 }
394 }
367 }
395 }
368
396
369 QColor QXYSeries::color() const
397 QColor QXYSeries::color() const
370 {
398 {
371 return pen().color();
399 return pen().color();
372 }
400 }
373
401
374 void QXYSeries::setPointsVisible(bool visible)
402 void QXYSeries::setPointsVisible(bool visible)
375 {
403 {
376 Q_D(QXYSeries);
404 Q_D(QXYSeries);
377 if (d->m_pointsVisible != visible) {
405 if (d->m_pointsVisible != visible) {
378 d->m_pointsVisible = visible;
406 d->m_pointsVisible = visible;
379 emit d->updated();
407 emit d->updated();
380 }
408 }
381 }
409 }
382
410
383 bool QXYSeries::pointsVisible() const
411 bool QXYSeries::pointsVisible() const
384 {
412 {
385 Q_D(const QXYSeries);
413 Q_D(const QXYSeries);
386 return d->m_pointsVisible;
414 return d->m_pointsVisible;
387 }
415 }
388
416
389
417
390 /*!
418 /*!
391 Stream operator for adding a data \a point to the series.
419 Stream operator for adding a data \a point to the series.
392 \sa append()
420 \sa append()
393 */
421 */
394 QXYSeries &QXYSeries::operator<< (const QPointF &point)
422 QXYSeries &QXYSeries::operator<< (const QPointF &point)
395 {
423 {
396 append(point);
424 append(point);
397 return *this;
425 return *this;
398 }
426 }
399
427
400
428
401 /*!
429 /*!
402 Stream operator for adding a list of \a points to the series.
430 Stream operator for adding a list of \a points to the series.
403 \sa append()
431 \sa append()
404 */
432 */
405
433
406 QXYSeries &QXYSeries::operator<< (const QList<QPointF>& points)
434 QXYSeries &QXYSeries::operator<< (const QList<QPointF>& points)
407 {
435 {
408 append(points);
436 append(points);
409 return *this;
437 return *this;
410 }
438 }
411
439
412 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
440 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
413
441
414
442
415 QXYSeriesPrivate::QXYSeriesPrivate(QXYSeries *q)
443 QXYSeriesPrivate::QXYSeriesPrivate(QXYSeries *q)
416 : QAbstractSeriesPrivate(q),
444 : QAbstractSeriesPrivate(q),
417 m_pointsVisible(false)
445 m_pointsVisible(false)
418 {
446 {
419 }
447 }
420
448
421 void QXYSeriesPrivate::initializeDomain()
449 void QXYSeriesPrivate::initializeDomain()
422 {
450 {
423 qreal minX(0);
451 qreal minX(0);
424 qreal minY(0);
452 qreal minY(0);
425 qreal maxX(1);
453 qreal maxX(1);
426 qreal maxY(1);
454 qreal maxY(1);
427
455
428 Q_Q(QXYSeries);
456 Q_Q(QXYSeries);
429
457
430 const QList<QPointF>& points = q->points();
458 const QList<QPointF>& points = q->points();
431
459
432 if (!points.isEmpty()) {
460 if (!points.isEmpty()) {
433 minX = points[0].x();
461 minX = points[0].x();
434 minY = points[0].y();
462 minY = points[0].y();
435 maxX = minX;
463 maxX = minX;
436 maxY = minY;
464 maxY = minY;
437
465
438 for (int i = 0; i < points.count(); i++) {
466 for (int i = 0; i < points.count(); i++) {
439 qreal x = points[i].x();
467 qreal x = points[i].x();
440 qreal y = points[i].y();
468 qreal y = points[i].y();
441 minX = qMin(minX, x);
469 minX = qMin(minX, x);
442 minY = qMin(minY, y);
470 minY = qMin(minY, y);
443 maxX = qMax(maxX, x);
471 maxX = qMax(maxX, x);
444 maxY = qMax(maxY, y);
472 maxY = qMax(maxY, y);
445 }
473 }
446 }
474 }
447
475
448 domain()->setRange(minX, maxX, minY, maxY);
476 domain()->setRange(minX, maxX, minY, maxY);
449 }
477 }
450
478
451 QList<QLegendMarker*> QXYSeriesPrivate::createLegendMarkers(QLegend* legend)
479 QList<QLegendMarker*> QXYSeriesPrivate::createLegendMarkers(QLegend* legend)
452 {
480 {
453 Q_Q(QXYSeries);
481 Q_Q(QXYSeries);
454 QList<QLegendMarker*> list;
482 QList<QLegendMarker*> list;
455 return list << new QXYLegendMarker(q,legend);
483 return list << new QXYLegendMarker(q,legend);
456 }
484 }
457
485
458 void QXYSeriesPrivate::initializeAxes()
486 void QXYSeriesPrivate::initializeAxes()
459 {
487 {
460
488
461 }
489 }
462
490
463 QAbstractAxis::AxisType QXYSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
491 QAbstractAxis::AxisType QXYSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
464 {
492 {
465 Q_UNUSED(orientation);
493 Q_UNUSED(orientation);
466 return QAbstractAxis::AxisTypeValue;
494 return QAbstractAxis::AxisTypeValue;
467 }
495 }
468
496
469 QAbstractAxis* QXYSeriesPrivate::createDefaultAxis(Qt::Orientation orientation) const
497 QAbstractAxis* QXYSeriesPrivate::createDefaultAxis(Qt::Orientation orientation) const
470 {
498 {
471 Q_UNUSED(orientation);
499 Q_UNUSED(orientation);
472 return 0;
500 return 0;
473 }
501 }
474
502
475 void QXYSeriesPrivate::initializeAnimations(QtCommercialChart::QChart::AnimationOptions options)
503 void QXYSeriesPrivate::initializeAnimations(QtCommercialChart::QChart::AnimationOptions options)
476 {
504 {
477 XYChart *item = static_cast<XYChart *>(m_item.data());
505 XYChart *item = static_cast<XYChart *>(m_item.data());
478 Q_ASSERT(item);
506 Q_ASSERT(item);
479 if (options.testFlag(QChart::SeriesAnimations)) {
507 if (options.testFlag(QChart::SeriesAnimations)) {
480 item->setAnimation(new XYAnimation(item));
508 item->setAnimation(new XYAnimation(item));
481 }else{
509 }else{
482 item->setAnimation(0);
510 item->setAnimation(0);
483 }
511 }
484 QAbstractSeriesPrivate::initializeAnimations(options);
512 QAbstractSeriesPrivate::initializeAnimations(options);
485 }
513 }
486
514
487 #include "moc_qxyseries.cpp"
515 #include "moc_qxyseries.cpp"
488 #include "moc_qxyseries_p.cpp"
516 #include "moc_qxyseries_p.cpp"
489
517
490 QTCOMMERCIALCHART_END_NAMESPACE
518 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now