##// END OF EJS Templates
Fixed minor issues in QML axes documentation
Tero Ahola -
r1869:8a10d7ac696d
parent child
Show More
@@ -1,80 +1,80
1 1 /*!
2 2 \page qml.html
3 3 \title QtCommercial Charts QML API
4 4 \keyword Charts QML API
5 5
6 6 QtCommercial Charts QML API is an intuitive and simple way to show charts in your QML
7 7 applications. The following QML shows you to create a simple pie chart:
8 8 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 1
9 9 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 2
10 10 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 3
11 11
12 12 \raw HTML
13 13 <table cellpadding="2" cellspacing="1" border="0" width="95%" class="indextable">
14 14 <tr>
15 15 <th class="titleheader" width="25%"> ChartView and it's properties </th>
16 16 </tr>
17 17 <tr>
18 18 <td valign="top">
19 19 <ul>
20 20 <li><a href="qml-chartview.html">ChartView</a></li>
21 21 <li><a href="qml-abstractaxis.html">AbstractAxis</a></li>
22 <li><a href="qml-valuesaxis.html">ValuesAxis</a></li>
23 <li><a href="qml-barcategoriesaxis.html">BarCategoriesAxis</a></li>
22 <li><a href="qml-valueaxis.html">ValueAxis</a></li>
23 <li><a href="qml-barcategoryaxis.html">BarCategoryAxis</a></li>
24 24 <li><a href="qml-legend.html">Legend</a></li>
25 25 <li><a href="qml-abstractseries.html">AbstractSeries</a></li>
26 26 </ul>
27 27 </td>
28 28 </tr>
29 29 </table>
30 30 <table cellpadding="2" cellspacing="1" border="0" width="95%" class="indextable">
31 31 <tr>
32 32 <th class="titleheader" width="25%"> XY chart </th>
33 33 </tr>
34 34 <tr>
35 35 <td valign="top">
36 36 <ul>
37 37 <li><a href="qml-xyseries.html">XYSeries</a></li>
38 38 <li><a href="qml-lineseries.html">LineSeries</a></li>
39 39 <li><a href="qml-areaseries.html">AreaSeries</a></li>
40 40 <li><a href="qml-scatterseries.html">ScatterSeries</a></li>
41 41 <li><a href="qml-splineseries.html">SplineSeries</a></li>
42 42 <li><a href="qml-xypoint.html">XYPoint</a></li>
43 43 <li><a href="qml-hxymodelmapper.html">HXYModelMapper</a></li>
44 44 <li><a href="qml-vxymodelmapper.html">VXYModelMapper</a></li>
45 45 </ul>
46 46 </td>
47 47 </tr>
48 48 </table>
49 49 <table cellpadding="2" cellspacing="1" border="0" width="95%" class="indextable">
50 50 <tr>
51 51 <th class="titleheader" width="25%"> Pie chart </th>
52 52 <tr>
53 53 <td valign="top">
54 54 <ul>
55 55 <li><a href="qml-pieseries.html">PieSeries</a></li>
56 56 <li><a href="qml-pieslice.html">PieSlice</a></li>
57 57 <li><a href="qml-hpiemodelmapper.html">HPieModelMapper</a></li>
58 58 <li><a href="qml-vpiemodelmapper.html">VPieModelMapper</a></li>
59 59 </ul>
60 60 </td>
61 61 </tr>
62 62 </table>
63 63 <table cellpadding="2" cellspacing="1" border="0" width="95%" class="indextable">
64 64 <tr>
65 65 <th class="titleheader" width="25%"> Bar chart </th>
66 66 <tr>
67 67 <td valign="top">
68 68 <ul>
69 69 <li><a href="qml-barseries.html">BarSeries</a></li>
70 70 <li><a href="qml-stackedbarseries.html">StackedBarSeries</a></li>
71 71 <li><a href="qml-percentbarseries.html">PercentBarSeries</a></li>
72 72 <li><a href="qml-barset.html">BarSet</a></li>
73 73 <li><a href="qml-hbarmodelmapper.html">HBarModelMapper</a></li>
74 74 <li><a href="qml-vbarmodelmapper.html">VBarModelMapper</a></li>
75 75 </ul>
76 76 </td>
77 77 </tr>
78 78 </table>
79 79 \endraw
80 80 */
@@ -1,683 +1,683
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "declarativechart.h"
22 22 #include <QPainter>
23 23 #include <QDeclarativeEngine>
24 24 #include "declarativelineseries.h"
25 25 #include "declarativeareaseries.h"
26 26 #include "declarativebarseries.h"
27 27 #include "declarativepieseries.h"
28 28 #include "declarativesplineseries.h"
29 29 #include "declarativescatterseries.h"
30 30 #include "qbarcategoryaxis.h"
31 31 #include "qvalueaxis.h"
32 32 #include "qcategoryaxis.h"
33 33 #include "qabstractseries_p.h"
34 34
35 35 #ifndef QT_ON_ARM
36 36 #include "qdatetimeaxis.h"
37 37 #endif
38 38
39 39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40 40
41 41 /*!
42 42 \qmlclass ChartView DeclarativeChart
43 43
44 44 ChartView element is the parent that is responsible for showing different chart series types.
45 45
46 46 The following QML shows how to create a simple chart with one pie series:
47 47 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 1
48 48 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 2
49 49 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 3
50 50
51 51 \beginfloatleft
52 52 \image examples_qmlpiechart.png
53 53 \endfloat
54 54 \clearfloat
55 55 */
56 56
57 57 /*!
58 58 \qmlproperty Theme ChartView::theme
59 59 Theme defines the visual appearance of the chart, including for example colors, fonts, line
60 60 widths and chart background.
61 61 */
62 62
63 63 /*!
64 64 \qmlproperty Animation ChartView::animation
65 65 Animation configuration of the chart. One of ChartView.NoAnimation, ChartView.GridAxisAnimations,
66 66 ChartView.SeriesAnimations or ChartView.AllAnimations.
67 67 */
68 68
69 69 /*!
70 70 \qmlproperty Font ChartView::titleFont
71 71 The title font of the chart
72 72
73 73 See the \l {Font} {QML Font Element} for detailed documentation.
74 74 */
75 75
76 76 /*!
77 77 \qmlproperty string ChartView::title
78 78 The title of the chart, shown on top of the chart.
79 79 \sa ChartView::titleColor
80 80 */
81 81
82 82 /*!
83 83 \qmlproperty string ChartView::titleColor
84 84 The color of the title text.
85 85 */
86 86
87 87 /*!
88 88 \qmlproperty Axis ChartView::axisX
89 89 The x-axis of the chart.
90 90 */
91 91
92 92 /*!
93 93 \qmlproperty Axis ChartView::axisY
94 94 The default y-axis of the chart.
95 95 */
96 96
97 97 /*!
98 98 \qmlproperty Legend ChartView::legend
99 99 The legend of the chart. Legend lists all the series, pie slices and bar sets added on the chart.
100 100 */
101 101
102 102 /*!
103 103 \qmlproperty int ChartView::count
104 104 The count of series added to the chart.
105 105 */
106 106
107 107 /*!
108 108 \qmlproperty color ChartView::backgroundColor
109 109 The color of the chart's background. By default background color is defined by chart theme.
110 110 \sa ChartView::theme
111 111 */
112 112
113 113 /*!
114 114 \qmlproperty bool ChartView::dropShadowEnabled
115 115 The chart's border drop shadow. Set to true to enable drop shadow.
116 116 */
117 117
118 118 /*!
119 119 \qmlproperty real ChartView::topMargin
120 120 The space between the top of chart view and the top of the plot area. The title (if non-empty) is drawn on top margin
121 121 area of the chart view. Top margin area is also used by legend, if aligned to top.
122 122 */
123 123
124 124 /*!
125 125 \qmlproperty real ChartView::bottomMargin
126 126 The space between the bottom of chart view and the bottom of the plot area. The bottom margin area may be used by
127 127 legend (if aligned to bottom), x-axis, x-axis labels and x-axis tick marks.
128 128 */
129 129
130 130 /*!
131 131 \qmlproperty real ChartView::leftMargin
132 132 The space between the left side of chart view and the left side of the plot area. The left margin area may be used by
133 133 legend (if aligned to left), y-axis, y-axis labels and y-axis tick marks.
134 134 */
135 135
136 136 /*!
137 137 \qmlproperty real ChartView::rightMargin
138 138 The space between the right side of chart view and the right side of the plot area. The right margin area may be used
139 139 by legend (if aligned to right).
140 140 */
141 141
142 142 /*!
143 143 \qmlmethod AbstractSeries ChartView::series(int index)
144 144 Returns the series with \a index on the chart. This allows you to loop through the series of a chart together with
145 145 the count property of the chart.
146 146 */
147 147
148 148 /*!
149 149 \qmlmethod AbstractSeries ChartView::series(string name)
150 150 Returns the first series on the chart with \a name. If there is no series with that name, returns null.
151 151 */
152 152
153 153 /*!
154 154 \qmlmethod AbstractSeries ChartView::createSeries(SeriesType type, string name)
155 155 Creates a series object of \a type to the chart. For example:
156 156 \code
157 157 var scatter = chartView.createSeries(ChartView.SeriesTypeScatter, "scatter series");
158 158 scatter.markerSize = 22;
159 159 scatter.append(1.1, 2.0);
160 160 \endcode
161 161 */
162 162
163 163 /*!
164 164 \qmlmethod Axis ChartView::axisY(QAbstractSeries *series)
165 165 The y-axis of the series. This is the same as the default y-axis of the chart as multiple y-axes are not yet supported.
166 166 */
167 167
168 168 /*!
169 169 \qmlmethod ChartView::zoomY(real factor)
170 170 Zooms in by \a factor on the center of the chart.
171 171 */
172 172
173 173 /*!
174 174 \qmlmethod ChartView::scrollLeft(real pixels)
175 175 Scrolls to left by \a pixels. This is a convenience function that suits for example for key navigation.
176 \sa ValueAxis::min, ValueAxis::max, BarCategoriesAxis::min, BarCategoriesAxis::max
176 \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max
177 177 */
178 178
179 179 /*!
180 180 \qmlmethod ChartView::scrollRight(real pixels)
181 181 Scrolls to right by \a pixels. This is a convenience function that suits for example for key navigation.
182 \sa ValueAxis::min, ValueAxis::max, BarCategoriesAxis::min, BarCategoriesAxis::max
182 \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max
183 183 */
184 184
185 185 /*!
186 186 \qmlmethod ChartView::scrollUp(real pixels)
187 187 Scrolls up by \a pixels. This is a convenience function that suits for example for key navigation.
188 \sa ValueAxis::min, ValueAxis::max, BarCategoriesAxis::min, BarCategoriesAxis::max
188 \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max
189 189 */
190 190
191 191 /*!
192 192 \qmlmethod ChartView::scrollDown(real pixels)
193 193 Scrolls down by \a pixels. This is a convenience function that suits for example for key navigation.
194 \sa ValueAxis::min, ValueAxis::max, BarCategoriesAxis::min, BarCategoriesAxis::max
194 \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max
195 195 */
196 196
197 197 /*!
198 198 \qmlsignal ChartView::onTopMarginChanged(real margin)
199 199 The top margin of the chart view has changed to \a margin. This may happen for example if you modify font size
200 200 related properties of the legend or chart title.
201 201 */
202 202
203 203 /*!
204 204 \qmlsignal ChartView::onBottomMarginChanged(real margin)
205 205 The bottom margin of the chart view has changed to \a margin. This may happen for example if you modify font size
206 206 related properties of the legend or chart title.
207 207 */
208 208
209 209 /*!
210 210 \qmlsignal ChartView::onLeftMarginChanged(real margin)
211 211 The left margin of the chart view has changed to \a margin. This may happen for example if you modify font size
212 212 related properties of the legend or chart title.
213 213 */
214 214
215 215 /*!
216 216 \qmlsignal ChartView::onRightMarginChanged(real margin)
217 217 The right margin of the chart view has changed to \a margin. This may happen for example if you modify font size
218 218 related properties of the legend or chart title.
219 219 */
220 220
221 221 DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent)
222 222 : QDeclarativeItem(parent),
223 223 m_chart(new QChart(this))
224 224 {
225 225 setFlag(QGraphicsItem::ItemHasNoContents, false);
226 226 // m_chart->axisX()->setNiceNumbersEnabled(false);
227 227 m_chartMargins = m_chart->margins();
228 228 connect(m_chart, SIGNAL(marginsChanged(QRectF)), this, SLOT(handleMarginsChanged(QRectF)));
229 229 }
230 230
231 231 void DeclarativeChart::handleMarginsChanged(QRectF newMargins)
232 232 {
233 233 if (m_chartMargins.top() != newMargins.top())
234 234 topMarginChanged(m_chart->margins().top());
235 235 if (m_chartMargins.bottom() != newMargins.bottom())
236 236 bottomMarginChanged(m_chart->margins().bottom());
237 237 if (m_chartMargins.left() != newMargins.left())
238 238 leftMarginChanged(m_chart->margins().left());
239 239 if (m_chartMargins.right() != newMargins.right())
240 240 rightMarginChanged(m_chart->margins().right());
241 241
242 242 m_chartMargins = m_chart->margins();
243 243 }
244 244
245 245 DeclarativeChart::~DeclarativeChart()
246 246 {
247 247 delete m_chart;
248 248 }
249 249
250 250 void DeclarativeChart::childEvent(QChildEvent *event)
251 251 {
252 252 if (event->type() == QEvent::ChildAdded) {
253 253 if (qobject_cast<QAbstractSeries *>(event->child())) {
254 254 m_chart->addSeries(qobject_cast<QAbstractSeries *>(event->child()));
255 255 }
256 256 }
257 257 }
258 258
259 259 void DeclarativeChart::componentComplete()
260 260 {
261 261 foreach(QObject *child, children()) {
262 262 if (qobject_cast<QAbstractSeries *>(child)) {
263 263 m_chart->addSeries(qobject_cast<QAbstractSeries *>(child));
264 264 if (qobject_cast<DeclarativeLineSeries *>(child)) {
265 265 DeclarativeLineSeries *s = qobject_cast<DeclarativeLineSeries *>(child);
266 266 connect(s, SIGNAL(axisXChanged(QAbstractAxis *)), this, SLOT(handleAxisXSet(QAbstractAxis *)));
267 267 connect(s, SIGNAL(axisYChanged(QAbstractAxis *)), this, SLOT(handleAxisYSet(QAbstractAxis *)));
268 268 setAxisX(s->axisX(), s);
269 269 setAxisY(s->axisY(), s);
270 270 } else if (qobject_cast<DeclarativeSplineSeries *>(child)) {
271 271 DeclarativeSplineSeries *s = qobject_cast<DeclarativeSplineSeries *>(child);
272 272 connect(s, SIGNAL(axisXChanged(QAbstractAxis *)), this, SLOT(handleAxisXSet(QAbstractAxis *)));
273 273 connect(s, SIGNAL(axisYChanged(QAbstractAxis *)), this, SLOT(handleAxisYSet(QAbstractAxis *)));
274 274 setAxisX(s->axisX(), s);
275 275 setAxisY(s->axisY(), s);
276 276 } else if (qobject_cast<DeclarativeScatterSeries *>(child)) {
277 277 DeclarativeScatterSeries *s = qobject_cast<DeclarativeScatterSeries *>(child);
278 278 connect(s, SIGNAL(axisXChanged(QAbstractAxis *)), this, SLOT(handleAxisXSet(QAbstractAxis *)));
279 279 connect(s, SIGNAL(axisYChanged(QAbstractAxis *)), this, SLOT(handleAxisYSet(QAbstractAxis *)));
280 280 setAxisX(s->axisX(), s);
281 281 setAxisY(s->axisY(), s);
282 282 } else if (qobject_cast<DeclarativeAreaSeries *>(child)) {
283 283 DeclarativeAreaSeries *s = qobject_cast<DeclarativeAreaSeries *>(child);
284 284 connect(s, SIGNAL(axisXChanged(QAbstractAxis *)), this, SLOT(handleAxisXSet(QAbstractAxis *)));
285 285 connect(s, SIGNAL(axisYChanged(QAbstractAxis *)), this, SLOT(handleAxisYSet(QAbstractAxis *)));
286 286 setAxisX(s->axisX(), s);
287 287 setAxisY(s->axisY(), s);
288 288 } else if (qobject_cast<DeclarativeBarSeries *>(child)) {
289 289 DeclarativeBarSeries *s = qobject_cast<DeclarativeBarSeries *>(child);
290 290 connect(s, SIGNAL(axisXChanged(QAbstractAxis *)), this, SLOT(handleAxisXSet(QAbstractAxis *)));
291 291 connect(s, SIGNAL(axisYChanged(QAbstractAxis *)), this, SLOT(handleAxisYSet(QAbstractAxis *)));
292 292 setAxisX(s->axisX(), s);
293 293 setAxisY(s->axisY(), s);
294 294 } else if (qobject_cast<DeclarativeStackedBarSeries *>(child)) {
295 295 DeclarativeStackedBarSeries *s = qobject_cast<DeclarativeStackedBarSeries *>(child);
296 296 connect(s, SIGNAL(axisXChanged(QAbstractAxis *)), this, SLOT(handleAxisXSet(QAbstractAxis *)));
297 297 connect(s, SIGNAL(axisYChanged(QAbstractAxis *)), this, SLOT(handleAxisYSet(QAbstractAxis *)));
298 298 setAxisX(s->axisX(), s);
299 299 setAxisY(s->axisY(), s);
300 300 } else if (qobject_cast<DeclarativePercentBarSeries *>(child)) {
301 301 DeclarativePercentBarSeries *s = qobject_cast<DeclarativePercentBarSeries *>(child);
302 302 connect(s, SIGNAL(axisXChanged(QAbstractAxis *)), this, SLOT(handleAxisXSet(QAbstractAxis *)));
303 303 connect(s, SIGNAL(axisYChanged(QAbstractAxis *)), this, SLOT(handleAxisYSet(QAbstractAxis *)));
304 304 setAxisX(s->axisX(), s);
305 305 setAxisY(s->axisY(), s);
306 306 } else if (qobject_cast<DeclarativeHorizontalBarSeries *>(child)) {
307 307 DeclarativeHorizontalBarSeries *s = qobject_cast<DeclarativeHorizontalBarSeries *>(child);
308 308 connect(s, SIGNAL(axisXChanged(QAbstractAxis *)), this, SLOT(handleAxisXSet(QAbstractAxis *)));
309 309 connect(s, SIGNAL(axisYChanged(QAbstractAxis *)), this, SLOT(handleAxisYSet(QAbstractAxis *)));
310 310 setAxisX(s->axisX(), s);
311 311 setAxisY(s->axisY(), s);
312 312 } else if (qobject_cast<DeclarativeHorizontalStackedBarSeries *>(child)) {
313 313 DeclarativeHorizontalStackedBarSeries *s = qobject_cast<DeclarativeHorizontalStackedBarSeries *>(child);
314 314 connect(s, SIGNAL(axisXChanged(QAbstractAxis *)), this, SLOT(handleAxisXSet(QAbstractAxis *)));
315 315 connect(s, SIGNAL(axisYChanged(QAbstractAxis *)), this, SLOT(handleAxisYSet(QAbstractAxis *)));
316 316 setAxisX(s->axisX(), s);
317 317 setAxisY(s->axisY(), s);
318 318 } else if (qobject_cast<DeclarativeHorizontalPercentBarSeries *>(child)) {
319 319 DeclarativeHorizontalPercentBarSeries *s = qobject_cast<DeclarativeHorizontalPercentBarSeries *>(child);
320 320 connect(s, SIGNAL(axisXChanged(QAbstractAxis *)), this, SLOT(handleAxisXSet(QAbstractAxis *)));
321 321 connect(s, SIGNAL(axisYChanged(QAbstractAxis *)), this, SLOT(handleAxisYSet(QAbstractAxis *)));
322 322 setAxisX(s->axisX(), s);
323 323 setAxisY(s->axisY(), s);
324 324 }
325 325 } else if(qobject_cast<QAbstractAxis *>(child)) {
326 326 // Do nothing, axes are set for the chart in the context of series
327 327 }
328 328 }
329 329
330 330 // Create the missing axes for the series that cannot be painted without axes
331 331 foreach(QAbstractSeries *series, m_chart->series())
332 332 createDefaultAxes(series);
333 333
334 334 QDeclarativeItem::componentComplete();
335 335 }
336 336
337 337 void DeclarativeChart::handleAxisXSet(QAbstractAxis* axis)
338 338 {
339 339 // qDebug() << "DeclarativeChart::handleAxisXSet" << sender() << axis;
340 340 if (axis && qobject_cast<DeclarativeLineSeries *>(sender())) {
341 341 m_chart->setAxisX(axis, qobject_cast<DeclarativeLineSeries *>(sender()));
342 342 }
343 343 }
344 344
345 345 void DeclarativeChart::handleAxisYSet(QAbstractAxis* axis)
346 346 {
347 347 // qDebug() << "DeclarativeChart::handleAxisYSet" << sender() << axis;
348 348 if (axis && qobject_cast<DeclarativeLineSeries *>(sender())) {
349 349 m_chart->setAxisY(axis, qobject_cast<DeclarativeLineSeries *>(sender()));
350 350 }
351 351 }
352 352
353 353 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
354 354 {
355 355 // qDebug() << "DeclarativeChart::geometryChanged" << newGeometry.width() << newGeometry.height();
356 356 if (newGeometry.isValid()) {
357 357 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
358 358 m_chart->resize(newGeometry.width(), newGeometry.height());
359 359 }
360 360 }
361 361 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
362 362 }
363 363
364 364 void DeclarativeChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
365 365 {
366 366 Q_UNUSED(option)
367 367 Q_UNUSED(widget)
368 368
369 369 // TODO: optimized?
370 370 painter->setRenderHint(QPainter::Antialiasing, true);
371 371 }
372 372
373 373 void DeclarativeChart::setTheme(DeclarativeChart::Theme theme)
374 374 {
375 375 QChart::ChartTheme chartTheme = (QChart::ChartTheme) theme;
376 376 if (chartTheme != m_chart->theme())
377 377 m_chart->setTheme(chartTheme);
378 378 }
379 379
380 380 DeclarativeChart::Theme DeclarativeChart::theme()
381 381 {
382 382 return (DeclarativeChart::Theme) m_chart->theme();
383 383 }
384 384
385 385 void DeclarativeChart::setAnimationOptions(DeclarativeChart::Animation animations)
386 386 {
387 387 QChart::AnimationOption animationOptions = (QChart::AnimationOption) animations;
388 388 if (animationOptions != m_chart->animationOptions())
389 389 m_chart->setAnimationOptions(animationOptions);
390 390 }
391 391
392 392 DeclarativeChart::Animation DeclarativeChart::animationOptions()
393 393 {
394 394 if (m_chart->animationOptions().testFlag(QChart::AllAnimations))
395 395 return DeclarativeChart::AllAnimations;
396 396 else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations))
397 397 return DeclarativeChart::GridAxisAnimations;
398 398 else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations))
399 399 return DeclarativeChart::SeriesAnimations;
400 400 else
401 401 return DeclarativeChart::NoAnimation;
402 402 }
403 403
404 404 void DeclarativeChart::setTitle(QString title)
405 405 {
406 406 if (title != m_chart->title())
407 407 m_chart->setTitle(title);
408 408 }
409 409 QString DeclarativeChart::title()
410 410 {
411 411 return m_chart->title();
412 412 }
413 413
414 414 QAbstractAxis *DeclarativeChart::axisX(QAbstractSeries *series)
415 415 {
416 416 return m_chart->axisX(series);
417 417 }
418 418
419 419 QAbstractAxis *DeclarativeChart::axisY(QAbstractSeries *series)
420 420 {
421 421 return m_chart->axisY(series);
422 422 }
423 423
424 424 QLegend *DeclarativeChart::legend()
425 425 {
426 426 return m_chart->legend();
427 427 }
428 428
429 429 void DeclarativeChart::setTitleColor(QColor color)
430 430 {
431 431 QBrush b = m_chart->titleBrush();
432 432 if (color != b.color()) {
433 433 b.setColor(color);
434 434 m_chart->setTitleBrush(b);
435 435 emit titleColorChanged(color);
436 436 }
437 437 }
438 438
439 439 QFont DeclarativeChart::titleFont() const
440 440 {
441 441 return m_chart->titleFont();
442 442 }
443 443
444 444 void DeclarativeChart::setTitleFont(const QFont& font)
445 445 {
446 446 m_chart->setTitleFont(font);
447 447 }
448 448
449 449 QColor DeclarativeChart::titleColor()
450 450 {
451 451 return m_chart->titleBrush().color();
452 452 }
453 453
454 454 void DeclarativeChart::setBackgroundColor(QColor color)
455 455 {
456 456 QBrush b = m_chart->backgroundBrush();
457 457 if (b.style() != Qt::SolidPattern || color != b.color()) {
458 458 b.setStyle(Qt::SolidPattern);
459 459 b.setColor(color);
460 460 m_chart->setBackgroundBrush(b);
461 461 emit backgroundColorChanged();
462 462 }
463 463 }
464 464
465 465 QColor DeclarativeChart::backgroundColor()
466 466 {
467 467 return m_chart->backgroundBrush().color();
468 468 }
469 469
470 470 int DeclarativeChart::count()
471 471 {
472 472 return m_chart->series().count();
473 473 }
474 474
475 475 void DeclarativeChart::setDropShadowEnabled(bool enabled)
476 476 {
477 477 if (enabled != m_chart->isDropShadowEnabled()) {
478 478 m_chart->setDropShadowEnabled(enabled);
479 479 dropShadowEnabledChanged(enabled);
480 480 }
481 481 }
482 482
483 483 bool DeclarativeChart::dropShadowEnabled()
484 484 {
485 485 return m_chart->isDropShadowEnabled();
486 486 }
487 487
488 488 qreal DeclarativeChart::topMargin()
489 489 {
490 490 return m_chart->margins().top();
491 491 }
492 492
493 493 qreal DeclarativeChart::bottomMargin()
494 494 {
495 495 return m_chart->margins().bottom();
496 496 }
497 497
498 498 qreal DeclarativeChart::leftMargin()
499 499 {
500 500 return m_chart->margins().left();
501 501 }
502 502
503 503 qreal DeclarativeChart::rightMargin()
504 504 {
505 505 return m_chart->margins().right();
506 506 }
507 507
508 508 void DeclarativeChart::zoom(qreal factor)
509 509 {
510 510 m_chart->zoom(factor);
511 511 }
512 512
513 513 void DeclarativeChart::scrollLeft(qreal pixels)
514 514 {
515 515 m_chart->scroll(pixels, 0);
516 516 }
517 517
518 518 void DeclarativeChart::scrollRight(qreal pixels)
519 519 {
520 520 m_chart->scroll(-pixels, 0);
521 521 }
522 522
523 523 void DeclarativeChart::scrollUp(qreal pixels)
524 524 {
525 525 m_chart->scroll(0, pixels);
526 526 }
527 527
528 528 void DeclarativeChart::scrollDown(qreal pixels)
529 529 {
530 530 m_chart->scroll(0, -pixels);
531 531 }
532 532
533 533 QAbstractSeries *DeclarativeChart::series(int index)
534 534 {
535 535 if (index < m_chart->series().count()) {
536 536 return m_chart->series().at(index);
537 537 }
538 538 return 0;
539 539 }
540 540
541 541 QAbstractSeries *DeclarativeChart::series(QString seriesName)
542 542 {
543 543 foreach(QAbstractSeries *series, m_chart->series()) {
544 544 if (series->name() == seriesName)
545 545 return series;
546 546 }
547 547 return 0;
548 548 }
549 549
550 550 QAbstractSeries *DeclarativeChart::createSeries(DeclarativeChart::SeriesType type, QString name)
551 551 {
552 552 QAbstractSeries *series = 0;
553 553
554 554 switch (type) {
555 555 case DeclarativeChart::SeriesTypeLine:
556 556 series = new DeclarativeLineSeries();
557 557 break;
558 558 case DeclarativeChart::SeriesTypeArea:
559 559 series = new DeclarativeAreaSeries();
560 560 break;
561 561 case DeclarativeChart::SeriesTypeStackedBar:
562 562 series = new DeclarativeStackedBarSeries();
563 563 break;
564 564 case DeclarativeChart::SeriesTypePercentBar:
565 565 series = new DeclarativePercentBarSeries();
566 566 break;
567 567 case DeclarativeChart::SeriesTypeBar:
568 568 series = new DeclarativeBarSeries();
569 569 break;
570 570 case DeclarativeChart::SeriesTypeHorizontalBar:
571 571 series = new DeclarativeHorizontalBarSeries();
572 572 break;
573 573 case DeclarativeChart::SeriesTypeHorizontalPercentBar:
574 574 series = new DeclarativeHorizontalPercentBarSeries();
575 575 break;
576 576 case DeclarativeChart::SeriesTypeHorizontalStackedBar:
577 577 series = new DeclarativeHorizontalStackedBarSeries();
578 578 break;
579 579 case DeclarativeChart::SeriesTypePie:
580 580 series = new DeclarativePieSeries();
581 581 break;
582 582 case DeclarativeChart::SeriesTypeScatter:
583 583 series = new DeclarativeScatterSeries();
584 584 break;
585 585 case DeclarativeChart::SeriesTypeSpline:
586 586 series = new DeclarativeSplineSeries();
587 587 break;
588 588 default:
589 589 qWarning() << "Illegal series type";
590 590 }
591 591
592 592 if (series) {
593 593 series->setName(name);
594 594 m_chart->addSeries(series);
595 595 createDefaultAxes(series);
596 596 }
597 597
598 598 return series;
599 599 }
600 600
601 601 void DeclarativeChart::setAxisX(QAbstractAxis *axis, QAbstractSeries *series)
602 602 {
603 603 if (axis)
604 604 m_chart->setAxisX(axis, series);
605 605 }
606 606
607 607 void DeclarativeChart::setAxisY(QAbstractAxis *axis, QAbstractSeries *series)
608 608 {
609 609 if (axis)
610 610 m_chart->setAxisY(axis, series);
611 611 }
612 612
613 613 void DeclarativeChart::createDefaultAxes()
614 614 {
615 615 qWarning() << "ChartView.createDefaultAxes() is deprecated. Axes are created automatically.";
616 616 }
617 617
618 618 void DeclarativeChart::createDefaultAxes(QAbstractSeries* series)
619 619 {
620 620 foreach (QAbstractSeries *s, m_chart->series()) {
621 621 // If there is already an x axis of the correct type, re-use it
622 622 if (!m_chart->axisX(series) && s != series && m_chart->axisX(s)
623 623 && m_chart->axisX(s)->type() == series->d_ptr->defaultAxisType(Qt::Horizontal))
624 624 m_chart->setAxisX(m_chart->axisX(s), series);
625 625
626 626 // If there is already a y axis of the correct type, re-use it
627 627 if (!m_chart->axisY(series) && s != series && m_chart->axisY(s)
628 628 && m_chart->axisY(s)->type() == series->d_ptr->defaultAxisType(Qt::Vertical))
629 629 m_chart->setAxisY(m_chart->axisY(s), series);
630 630 }
631 631
632 632 // If no x axis of correct type was found, create a new x axis based of default axis type
633 633 if (!m_chart->axisX(series)) {
634 634 switch (series->d_ptr->defaultAxisType(Qt::Horizontal)) {
635 635 case QAbstractAxis::AxisTypeValue:
636 636 m_chart->setAxisX(new QValueAxis(this), series);
637 637 break;
638 638 case QAbstractAxis::AxisTypeBarCategory:
639 639 m_chart->setAxisX(new QBarCategoryAxis(this), series);
640 640 break;
641 641 case QAbstractAxis::AxisTypeCategory:
642 642 m_chart->setAxisX(new QCategoryAxis(this), series);
643 643 break;
644 644 #ifndef QT_ON_ARM
645 645 case QAbstractAxis::AxisTypeDateTime:
646 646 m_chart->setAxisX(new QDateTimeAxis(this), series);
647 647 break;
648 648 #endif
649 649 default:
650 650 // Do nothing, assume AxisTypeNoAxis
651 651 break;
652 652 }
653 653 }
654 654
655 655 // If no y axis of correct type was found, create a new y axis based of default axis type
656 656 if (!m_chart->axisY(series)) {
657 657 switch (series->d_ptr->defaultAxisType(Qt::Vertical)) {
658 658 case QAbstractAxis::AxisTypeValue:
659 659 m_chart->setAxisY(new QValueAxis(this), series);
660 660 break;
661 661 case QAbstractAxis::AxisTypeBarCategory:
662 662 m_chart->setAxisY(new QBarCategoryAxis(this), series);
663 663 break;
664 664 case QAbstractAxis::AxisTypeCategory:
665 665 m_chart->setAxisY(new QCategoryAxis(this), series);
666 666 break;
667 667 #ifndef QT_ON_ARM
668 668 case QAbstractAxis::AxisTypeDateTime:
669 669 m_chart->setAxisY(new QDateTimeAxis(this), series);
670 670 break;
671 671 #endif
672 672 default:
673 673 // Do nothing, assume AxisTypeNoAxis
674 674 break;
675 675 }
676 676 }
677 677
678 678 //qDebug() << "axis for series" << series << "x:" << m_chart->axisX(series) << "y:" << m_chart->axisY(series);
679 679 }
680 680
681 681 #include "moc_declarativechart.cpp"
682 682
683 683 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,471 +1,472
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qbarcategoryaxis.h"
22 22 #include "qbarcategoryaxis_p.h"
23 23 #include "chartbarcategoryaxisx_p.h"
24 24 #include "chartbarcategoryaxisy_p.h"
25 25 #include "domain_p.h"
26 26 #include "chartdataset_p.h"
27 27 #include <qmath.h>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30 /*!
31 31 \class QBarCategoryAxis
32 32 \brief The QBarCategoryAxis class is used for manipulating chart's axis.
33 33 \mainclass
34 34
35 BarCategoriesAxis 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 36 Categories are drawn between ticks. Note that you can use this also with lineseries too.
37 37 See the \l {Line and BarChart Example} {Line and BarChart Example} to learn how to do that.
38 38 */
39 39
40 40 /*!
41 \qmlclass BarCategoriesAxis QBarCategoryAxis
41 \qmlclass BarCategoryAxis QBarCategoryAxis
42 \inherits AbstractAxis
42 43 \brief The Axis element is used for manipulating chart's axes.
43 44
44 45 Axis can be setup to show axis line with tick marks, grid lines and shades.
45 46 Categories are drawn between ticks. Note that you can use this also with lineseries too.
46 47
47 To access BarCategoriesAxis you can use ChartView API. For example:
48 To access BarCategoryAxis you can use ChartView API. For example:
48 49 \code
49 50 ChartView {
50 BarCategoriesAxis {
51 BarCategoryAxis {
51 52 id: categoryAxis
52 53 categories: ["Jan", "Feb", "Mar", "Apr", "May", "Jun" ]
53 54 }
54 55 // Add a few series...
55 56 }
56 57 \endcode
57 58 */
58 59
59 60 /*!
60 61 \property QBarCategoryAxis::categories
61 62 Defines the categories of axis
62 63 */
63 64 /*!
64 \qmlproperty QStringList BarCategoriesAxis::categories
65 \qmlproperty QStringList BarCategoryAxis::categories
65 66 Defines the categories of axis
66 67 */
67 68
68 69 /*!
69 70 \property QBarCategoryAxis::min
70 71 Defines the minimum value on the axis.
71 72 */
72 73 /*!
73 \qmlproperty QString BarCategoriesAxis::min
74 \qmlproperty QString BarCategoryAxis::min
74 75 Defines the minimum value on the axis.
75 76 */
76 77
77 78 /*!
78 79 \property QBarCategoryAxis::max
79 80 Defines the maximum value on the axis.
80 81 */
81 82 /*!
82 \qmlproperty QString BarCategoriesAxis::max
83 \qmlproperty QString BarCategoryAxis::max
83 84 Defines the maximum value on the axis.
84 85 */
85 86
86 87
87 88 /*!
88 89 \fn void QBarCategoryAxis::categoriesChanged()
89 90 Axis emits signal when the categories of the axis has changed.
90 91 */
91 92 /*!
92 93 \fn void QBarCategoryAxis::minChanged(const QString &min)
93 94 Axis emits signal when \a min of axis has changed.
94 95 */
95 96 /*!
96 \qmlsignal BarCategoriesAxis::onMinChanged(const QString &min)
97 \qmlsignal BarCategoryAxis::onMinChanged(const QString &min)
97 98 Axis emits signal when \a min of axis has changed.
98 99 */
99 100
100 101 /*!
101 102 \fn void QBarCategoryAxis::maxChanged(const QString &max)
102 103 Axis emits signal when \a max of axis has changed.
103 104 */
104 105 /*!
105 \qmlsignal BarCategoriesAxis::onMaxChanged(const QString &max)
106 \qmlsignal BarCategoryAxis::onMaxChanged(const QString &max)
106 107 Axis emits signal when \a max of axis has changed.
107 108 */
108 109
109 110 /*!
110 111 \fn void QBarCategoryAxis::rangeChanged(const QString &min, const QString &max)
111 112 Axis emits signal when \a min or \a max of axis has changed.
112 113 */
113 114
114 115 /*!
115 116 Constructs an axis object which is a child of \a parent.
116 117 */
117 118 QBarCategoryAxis::QBarCategoryAxis(QObject *parent):
118 119 QAbstractAxis(*new QBarCategoryAxisPrivate(this),parent)
119 120 {
120 121 }
121 122
122 123 /*!
123 124 Destroys the object
124 125 */
125 126 QBarCategoryAxis::~QBarCategoryAxis()
126 127 {
127 128 Q_D(QBarCategoryAxis);
128 129 if(d->m_dataset){
129 130 d->m_dataset->removeAxis(this);
130 131 }
131 132 }
132 133
133 134 /*!
134 135 \internal
135 136 */
136 137 QBarCategoryAxis::QBarCategoryAxis(QBarCategoryAxisPrivate &d,QObject *parent):QAbstractAxis(d,parent)
137 138 {
138 139
139 140 }
140 141
141 142 /*!
142 143 Appends \a categories to axis
143 144 */
144 145 void QBarCategoryAxis::append(const QStringList &categories)
145 146 {
146 147 if(categories.isEmpty()) return;
147 148
148 149 Q_D(QBarCategoryAxis);
149 150 if (d->m_categories.isEmpty()) {
150 151 d->m_categories.append(categories);
151 152 setRange(categories.first(),categories.last());
152 153 }else{
153 154 d->m_categories.append(categories);
154 155 d->emitUpdated();
155 156 }
156 157 emit categoriesChanged();
157 158 }
158 159
159 160 /*!
160 161 Appends \a category to axis
161 162 */
162 163 void QBarCategoryAxis::append(const QString &category)
163 164 {
164 165 Q_D(QBarCategoryAxis);
165 166 if (d->m_categories.isEmpty()) {
166 167 d->m_categories.append(category);
167 168 setRange(category,category);
168 169 }else{
169 170 d->m_categories.append(category);
170 171 d->emitUpdated();
171 172 }
172 173 emit categoriesChanged();
173 174 }
174 175
175 176 /*!
176 177 Removes \a category from axis
177 178 */
178 179 void QBarCategoryAxis::remove(const QString &category)
179 180 {
180 181 Q_D(QBarCategoryAxis);
181 182 if (d->m_categories.contains(category)) {
182 183 d->m_categories.removeAt(d->m_categories.indexOf(category));
183 184 if(!d->m_categories.isEmpty())
184 185 setRange(d->m_categories.first(),d->m_categories.last());
185 186 else
186 187 setRange(QString::null,QString::null);
187 188 emit categoriesChanged();
188 189 }
189 190 }
190 191
191 192 /*!
192 193 Inserts \a category to axis at \a index
193 194 */
194 195 void QBarCategoryAxis::insert(int index, const QString &category)
195 196 {
196 197 Q_D(QBarCategoryAxis);
197 198 if (d->m_categories.isEmpty()) {
198 199 d->m_categories.insert(index,category);
199 200 setRange(category,category);
200 201 }else{
201 202 d->m_categories.insert(index,category);
202 203 d->emitUpdated();
203 204 }
204 205 emit categoriesChanged();
205 206 }
206 207
207 208 /*!
208 209 Replaces \a oldCategory with \a newCategory.
209 210 If \a oldCategory does not exits on the axis nothing is done.
210 211 */
211 212 void QBarCategoryAxis::replace(const QString &oldCategory, const QString &newCategory)
212 213 {
213 214 Q_D(QBarCategoryAxis);
214 215 int pos = d->m_categories.indexOf(oldCategory);
215 216 if (pos != -1) {
216 217 d->m_categories.replace(pos, newCategory);
217 218 d->emitUpdated();
218 219 emit categoriesChanged();
219 220 }
220 221 }
221 222
222 223 /*!
223 224 Removes all categories.
224 225 */
225 226 void QBarCategoryAxis::clear()
226 227 {
227 228 Q_D(QBarCategoryAxis);
228 229 d->m_categories.clear();
229 230 setRange(QString::null,QString::null);
230 231 emit categoriesChanged();
231 232 }
232 233
233 234 void QBarCategoryAxis::setCategories(const QStringList &categories)
234 235 {
235 236 Q_D(QBarCategoryAxis);
236 237 if(d->m_categories!=categories) {
237 238 d->m_categories = categories;
238 239 setRange(categories.first(),categories.last());
239 240 emit categoriesChanged();
240 241 }
241 242 }
242 243
243 244 QStringList QBarCategoryAxis::categories()
244 245 {
245 246 Q_D(QBarCategoryAxis);
246 247 return d->m_categories;
247 248 }
248 249
249 250 /*!
250 251 Returns number of categories.
251 252 */
252 253 int QBarCategoryAxis::count() const
253 254 {
254 255 Q_D(const QBarCategoryAxis);
255 256 return d->m_categories.count();
256 257 }
257 258
258 259 /*!
259 260 Returns category at \a index. Index must be valid.
260 261 */
261 262 QString QBarCategoryAxis::at(int index) const
262 263 {
263 264 Q_D(const QBarCategoryAxis);
264 265 return d->m_categories.at(index);
265 266 }
266 267
267 268 /*!
268 269 Sets minimum category to \a min.
269 270 */
270 271 void QBarCategoryAxis::setMin(const QString& min)
271 272 {
272 273 Q_D(QBarCategoryAxis);
273 274 setRange(min,d->m_maxCategory);
274 275 }
275 276
276 277 /*!
277 278 Returns minimum category.
278 279 */
279 280 QString QBarCategoryAxis::min() const
280 281 {
281 282 Q_D(const QBarCategoryAxis);
282 283 return d->m_minCategory;
283 284 }
284 285
285 286 /*!
286 287 Sets maximum category to \a max.
287 288 */
288 289 void QBarCategoryAxis::setMax(const QString& max)
289 290 {
290 291 Q_D(QBarCategoryAxis);
291 292 setRange(d->m_minCategory,max);
292 293 }
293 294
294 295 /*!
295 296 Returns maximum category
296 297 */
297 298 QString QBarCategoryAxis::max() const
298 299 {
299 300 Q_D(const QBarCategoryAxis);
300 301 return d->m_maxCategory;
301 302 }
302 303
303 304 /*!
304 305 Sets range from \a minCategory to \a maxCategory
305 306 */
306 307 void QBarCategoryAxis::setRange(const QString& minCategory, const QString& maxCategory)
307 308 {
308 309 Q_D(QBarCategoryAxis);
309 310
310 311 bool changed = false;
311 312
312 313 //special case
313 314 if(minCategory.isNull() && maxCategory.isNull()){
314 315 d->m_minCategory = minCategory;
315 316 d->m_maxCategory = maxCategory;
316 317 d->m_min = 0;
317 318 d->m_max = 0;
318 319 emit minChanged(minCategory);
319 320 emit maxChanged(maxCategory);
320 321 d->m_count=0;
321 322 emit rangeChanged(d->m_minCategory,d->m_maxCategory);
322 323 d->emitUpdated();
323 324 }
324 325
325 326 if(d->m_categories.indexOf(d->m_maxCategory)<d->m_categories.indexOf(d->m_minCategory)) return;
326 327
327 328 if (!minCategory.isEmpty() && d->m_minCategory!=minCategory && d->m_categories.contains(minCategory)) {
328 329 d->m_minCategory = minCategory;
329 330 d->m_min = d->m_categories.indexOf(d->m_minCategory) - 0.5;
330 331 changed = true;
331 332 emit minChanged(minCategory);
332 333 }
333 334
334 335 if (!maxCategory.isEmpty() && d->m_maxCategory!=maxCategory && d->m_categories.contains(maxCategory)) {
335 336 d->m_maxCategory = maxCategory;
336 337 d->m_max = d->m_categories.indexOf(d->m_maxCategory) + 0.5;
337 338 changed = true;
338 339 emit maxChanged(maxCategory);
339 340 }
340 341
341 342 if (changed) {
342 343 d->m_count=d->m_max - d->m_min;
343 344 emit rangeChanged(d->m_minCategory,d->m_maxCategory);
344 345 d->emitUpdated();
345 346 }
346 347 }
347 348
348 349 /*!
349 350 Returns the type of the axis
350 351 */
351 352 QAbstractAxis::AxisType QBarCategoryAxis::type() const
352 353 {
353 354 return AxisTypeBarCategory;
354 355 }
355 356
356 357 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
357 358
358 359 QBarCategoryAxisPrivate::QBarCategoryAxisPrivate(QBarCategoryAxis* q):
359 360 QAbstractAxisPrivate(q),
360 361 m_min(0.0),
361 362 m_max(0.0),
362 363 m_count(0)
363 364 {
364 365
365 366 }
366 367
367 368 QBarCategoryAxisPrivate::~QBarCategoryAxisPrivate()
368 369 {
369 370
370 371 }
371 372
372 373 void QBarCategoryAxisPrivate::setMin(const QVariant &min)
373 374 {
374 375 setRange(min,m_maxCategory);
375 376 }
376 377
377 378 void QBarCategoryAxisPrivate::setMax(const QVariant &max)
378 379 {
379 380 setRange(m_minCategory,max);
380 381 }
381 382
382 383 void QBarCategoryAxisPrivate::setRange(const QVariant &min, const QVariant &max)
383 384 {
384 385 Q_Q(QBarCategoryAxis);
385 386 QString value1 = min.toString();
386 387 QString value2 = max.toString();
387 388 q->setRange(value1,value2);
388 389 }
389 390
390 391 void QBarCategoryAxisPrivate::handleDomainUpdated()
391 392 {
392 393 Q_Q(QBarCategoryAxis);
393 394 Domain* domain = qobject_cast<Domain*>(sender());
394 395
395 396 if(m_orientation==Qt::Horizontal) {
396 397 m_min = domain->minX();
397 398 m_max = domain->maxX();
398 399 }
399 400 else if(m_orientation==Qt::Vertical) {
400 401 m_min = domain->minY();
401 402 m_max = domain->maxY();
402 403 }
403 404
404 405 bool changed = false;
405 406
406 407 int min = m_min + 0.5;
407 408 if(min>=0 && min<m_categories.count()) {
408 409 QString minCategory = m_categories.at(min);
409 410 if(m_minCategory!=minCategory && !minCategory.isEmpty()) {
410 411 m_minCategory=minCategory;
411 412 changed=true;
412 413 emit q->minChanged(minCategory);
413 414 }
414 415 }
415 416 int max = m_max - 0.5;
416 417 if(max>=0 && max<m_categories.count()) {
417 418 QString maxCategory = m_categories.at(max);
418 419 if(m_maxCategory!=maxCategory && !maxCategory.isEmpty()) {
419 420 m_maxCategory=maxCategory;
420 421 emit q->maxChanged(maxCategory);
421 422 }
422 423 }
423 424
424 425 if (changed) {
425 426 emit q->rangeChanged(m_minCategory,m_maxCategory);
426 427 }
427 428 }
428 429
429 430 ChartAxis* QBarCategoryAxisPrivate::createGraphics(ChartPresenter* presenter)
430 431 {
431 432 Q_Q(QBarCategoryAxis);
432 433 if(m_orientation == Qt::Vertical){
433 434 return new ChartCategoriesAxisY(q,presenter);
434 435 }else{
435 436 return new ChartCategoriesAxisX(q,presenter);
436 437 }
437 438 }
438 439
439 440 void QBarCategoryAxisPrivate::intializeDomain(Domain* domain)
440 441 {
441 442
442 443 Q_Q(QBarCategoryAxis);
443 444 if(m_max==m_min) {
444 445 int min;
445 446 int max;
446 447 if(m_orientation==Qt::Vertical) {
447 448 min = domain->minY() + 0.5;
448 449 max = domain->maxY() - 0.5;
449 450 }
450 451 else {
451 452 min = domain->minX() + 0.5;
452 453 max = domain->maxX() - 0.5;
453 454 }
454 455
455 456 if(min>0 && min<m_categories.count() && max>0 && max<m_categories.count())
456 457 q->setRange(m_categories.at(min),m_categories.at(max));
457 458 }
458 459 else {
459 460 if(m_orientation==Qt::Vertical) {
460 461 domain->setRangeY(m_min, m_max);
461 462 }
462 463 else {
463 464 domain->setRangeX(m_min, m_max);
464 465 }
465 466 }
466 467 }
467 468
468 469 #include "moc_qbarcategoryaxis.cpp"
469 470 #include "moc_qbarcategoryaxis_p.cpp"
470 471
471 472 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,261 +1,262
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qcategoryaxis.h"
22 22 #include "qcategoryaxis_p.h"
23 23 #include "chartcategoryaxisx_p.h"
24 24 #include "chartcategoryaxisy_p.h"
25 25 #include <qmath.h>
26 26 #include <QDebug>
27 27
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29 /*!
30 30 \class QCategoryAxis
31 31 \brief The QCategoryAxis class allows putting a named ranges on the axis.
32 32 \mainclass
33 33
34 34 This class can be used when the underlying data needs to be given extra meaning.
35 35 Unlike with the QBarCategoryAxis the QCategoryAxis allows the categories ranges widths to be specified freely.
36 36 */
37 37
38 38 /*!
39 39 \qmlclass CategoryAxis QCategoryAxis
40 \inherits AbstractAxis
40 41 \brief The QCategoryAxis class allows putting a named ranges on the axis.
41 42 */
42 43
43 44 /*!
44 45 \property QCategoryAxis::startValue
45 46 Defines the low end of the first category on the axis.
46 47 */
47 48
48 49 /*!
49 50 \qmlproperty int CategoryAxis::startValue
50 51 Defines the low end of the first category on the axis.
51 52 */
52 53
53 54 /*!
54 55 Constructs an axis object which is a child of \a parent.
55 56 */
56 57 QCategoryAxis::QCategoryAxis(QObject *parent):
57 58 QValueAxis(*new QCategoryAxisPrivate(this),parent)
58 59 {
59 60 }
60 61
61 62 /*!
62 63 Destroys the object
63 64 */
64 65 QCategoryAxis::~QCategoryAxis()
65 66 {
66 67 // Q_D(QValueAxis);
67 68 // if(d->m_dataset) {
68 69 // d->m_dataset->removeAxis(this);
69 70 // }
70 71 }
71 72
72 73 /*!
73 74 \internal
74 75 */
75 76 QCategoryAxis::QCategoryAxis(QCategoryAxisPrivate &d,QObject *parent):QValueAxis(d,parent)
76 77 {
77 78
78 79 }
79 80
80 81 /*!
81 82 Appends new category to the axis with an \a categoryLabel.
82 83 Category label has to be unique.
83 84 Parameter \a categoryEndValue specifies the high end limit of the category.
84 85 It has to be greater than the high end limit of the previous category.
85 86 Otherwise the method returns without adding a new category.
86 87 */
87 88 void QCategoryAxis::append(const QString& categoryLabel, qreal categoryEndValue)
88 89 {
89 90 Q_D(QCategoryAxis);
90 91
91 92 if (!d->m_categories.contains(categoryLabel))
92 93 {
93 94 if(d->m_categories.isEmpty()){
94 95 Range range(d->m_categoryMinimum, categoryEndValue);
95 96 d->m_categoriesMap.insert(categoryLabel, range);
96 97 d->m_categories.append(categoryLabel);
97 98 }else if (categoryEndValue > endValue(d->m_categories.last())){
98 99 Range previousRange = d->m_categoriesMap.value(d->m_categories.last());
99 100 d->m_categoriesMap.insert(categoryLabel, Range(previousRange.second, categoryEndValue));
100 101 d->m_categories.append(categoryLabel);
101 102 }
102 103 }
103 104 }
104 105
105 106 /*!
106 107 Sets \a min to be the low end limit of the first category on the axis.
107 108 If there is already some categories added to the axis then passed value must be lower than the high end value of the already defined first category range.
108 109 Otherwise nothing is done.
109 110 */
110 111 void QCategoryAxis::setStartValue(qreal min)
111 112 {
112 113 Q_D(QCategoryAxis);
113 114 if(d->m_categories.isEmpty()){
114 115 d->m_categoryMinimum = min;
115 116 }else{
116 117 Range range = d->m_categoriesMap.value(d->m_categories.first());
117 118 if (min < range.second)
118 119 d->m_categoriesMap.insert(d->m_categories.first(), Range(min, range.second));
119 120 }
120 121 }
121 122
122 123 /*!
123 124 Returns the low end limit of the category specified by an \a categoryLabel
124 125 */
125 126 qreal QCategoryAxis::startValue(const QString& categoryLabel) const
126 127 {
127 128 Q_D(const QCategoryAxis);
128 129 if (categoryLabel == QString())
129 130 return d->m_categoryMinimum;
130 131 else
131 132 return d->m_categoriesMap.value(categoryLabel).first;
132 133 }
133 134
134 135 /*!
135 136 Returns the high end limit of the interval specified by an \a categoryLabel
136 137 */
137 138 qreal QCategoryAxis::endValue(const QString& categoryLabel) const
138 139 {
139 140 Q_D(const QCategoryAxis);
140 141 return d->m_categoriesMap.value(categoryLabel).second;
141 142 }
142 143
143 144 /*!
144 145 Removes an interval specified by the \a categoryLabel from the axis
145 146 */
146 147 void QCategoryAxis::remove(const QString &categoryLabel)
147 148 {
148 149 Q_D(QCategoryAxis);
149 150 int labelIndex = d->m_categories.indexOf(categoryLabel);
150 151
151 152 // check if such label exists
152 153 if (labelIndex != -1) {
153 154 d->m_categories.removeAt(labelIndex);
154 155 d->m_categoriesMap.remove(categoryLabel);
155 156
156 157 // the range of the interval that follows (if exists) needs to be updated
157 158 if (labelIndex < d->m_categories.count()) {
158 159 QString label = d->m_categories.at(labelIndex);
159 160 Range range = d->m_categoriesMap.value(label);
160 161
161 162 // set the range
162 163 if (labelIndex == 0) {
163 164 range.first = d->m_categoryMinimum;
164 165 d->m_categoriesMap.insert(label, range);
165 166 } else {
166 167 range.first = d->m_categoriesMap.value(d->m_categories.at(labelIndex - 1)).second;
167 168 d->m_categoriesMap.insert(label, range);
168 169 }
169 170 }
170 171 d->emitUpdated();
171 172 }
172 173 }
173 174
174 175 /*!
175 176 Replaces \a oldLabel of an existing category with a \a newLabel
176 177 If the old label does not exist the method returns without making any changes.
177 178 */
178 179 void QCategoryAxis::replaceLabel(const QString& oldLabel, const QString& newLabel)
179 180 {
180 181 Q_D(QCategoryAxis);
181 182 int labelIndex = d->m_categories.indexOf(oldLabel);
182 183
183 184 // check if such label exists
184 185 if (labelIndex != -1) {
185 186 d->m_categories.replace(labelIndex, newLabel);
186 187 Range range = d->m_categoriesMap.value(oldLabel);
187 188 d->m_categoriesMap.remove(oldLabel);
188 189 d->m_categoriesMap.insert(newLabel, range);
189 190 d->emitUpdated();
190 191 }
191 192
192 193 }
193 194
194 195 /*!
195 196 Returns the list of the intervals labels
196 197 */
197 198 QStringList QCategoryAxis::categoriesLabels()
198 199 {
199 200 Q_D(QCategoryAxis);
200 201 return d->m_categories;
201 202 }
202 203
203 204 /*!
204 205 Returns number of intervals.
205 206 */
206 207 int QCategoryAxis::count() const
207 208 {
208 209 Q_D(const QCategoryAxis);
209 210 return d->m_categories.count();
210 211 }
211 212
212 213 /*!
213 214 Returns the type of the axis
214 215 */
215 216 QAbstractAxis::AxisType QCategoryAxis::type() const
216 217 {
217 218 return QAbstractAxis::AxisTypeCategory;
218 219 }
219 220
220 221 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
221 222
222 223 QCategoryAxisPrivate::QCategoryAxisPrivate(QCategoryAxis* q):
223 224 QValueAxisPrivate(q),
224 225 m_categoryMinimum(0)
225 226 {
226 227
227 228 }
228 229
229 230 QCategoryAxisPrivate::~QCategoryAxisPrivate()
230 231 {
231 232
232 233 }
233 234
234 235 int QCategoryAxisPrivate::ticksCount() const
235 236 {
236 237 return m_categories.count() + 1;
237 238 }
238 239
239 240 void QCategoryAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count)
240 241 {
241 242 Q_UNUSED(count);
242 243 Q_UNUSED(min);
243 244 Q_UNUSED(max);
244 245 //m_min = min;
245 246 //m_max = max;
246 247 }
247 248
248 249 ChartAxis* QCategoryAxisPrivate::createGraphics(ChartPresenter* presenter)
249 250 {
250 251 Q_Q(QCategoryAxis);
251 252 if(m_orientation == Qt::Vertical){
252 253 return new ChartCategoryAxisY(q,presenter);
253 254 }else{
254 255 return new ChartCategoryAxisX(q,presenter);
255 256 }
256 257 }
257 258
258 259 #include "moc_qcategoryaxis.cpp"
259 260 #include "moc_qcategoryaxis_p.cpp"
260 261
261 262 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,408 +1,409
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qvalueaxis.h"
22 22 #include "qvalueaxis_p.h"
23 23 #include "chartvalueaxisx_p.h"
24 24 #include "chartvalueaxisy_p.h"
25 25 #include "domain_p.h"
26 26 #include "chartdataset_p.h"
27 27 #include <qmath.h>
28 28
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31 /*!
32 32 \class QValueAxis
33 33 \brief The QValueAxis class is used for manipulating chart's axis.
34 34 \mainclass
35 35
36 36 ValueAxis can be setup to show axis line with tick marks, grid lines and shades.
37 37 Values of axis are drawn to position of ticks
38 38 */
39 39
40 40 /*!
41 41 \qmlclass ValueAxis QValueAxis
42 \inherits AbstractAxis
42 43 \brief The ValueAxis element is used for manipulating chart's axes
43 44
44 45 ValueAxis can be setup to show axis line with tick marks, grid lines and shades.
45 46 Values of axis are drawn to position of ticks
46 47
47 48 To access Axes you can use ChartView API. For example:
48 49 \code
49 50 ChartView {
50 51 ValueAxis {
51 52 id: xAxis
52 53 min: 0
53 54 max: 10
54 55 }
55 56 // Add a few series...
56 57 }
57 58 \endcode
58 59 */
59 60
60 61 /*!
61 62 \property QValueAxis::min
62 63 Defines the minimum value on the axis.
63 64 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
64 65 */
65 66 /*!
66 67 \qmlproperty real ValueAxis::min
67 68 Defines the minimum value on the axis.
68 69 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
69 70 */
70 71
71 72 /*!
72 73 \property QValueAxis::max
73 74 Defines the maximum value on the axis.
74 75 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
75 76 */
76 77 /*!
77 78 \qmlproperty real ValueAxis::max
78 79 Defines the maximum value on the axis.
79 80 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
80 81 */
81 82
82 83 /*!
83 84 \property QValueAxis::labelFormat
84 85 Defines the label format for the axis.
85 86 See QString::sprintf() for the details.
86 87 */
87 88 /*!
88 89 \qmlproperty real ValueAxis::labelFormat
89 90 Defines the label format for the axis.
90 91 See QString::sprintf() for the details.
91 92 */
92 93
93 94 /*!
94 95 \fn void QValueAxis::minChanged(qreal min)
95 96 Axis emits signal when \a min of axis has changed.
96 97 */
97 98 /*!
98 99 \qmlsignal ValueAxis::onMinChanged(real min)
99 100 Axis emits signal when \a min of axis has changed.
100 101 */
101 102
102 103 /*!
103 104 \fn void QValueAxis::maxChanged(qreal max)
104 105 Axis emits signal when \a max of axis has changed.
105 106 */
106 107 /*!
107 108 \qmlsignal ValueAxis::onMaxChanged(real max)
108 109 Axis emits signal when \a max of axis has changed.
109 110 */
110 111
111 112 /*!
112 113 \fn void QValueAxis::rangeChanged(qreal min, qreal max)
113 114 Axis emits signal when \a min or \a max of axis has changed.
114 115 */
115 116
116 117 /*!
117 118 \property QValueAxis::tickCount
118 119 The number of tick marks for the axis.
119 120 */
120 121
121 122 /*!
122 123 \qmlproperty int ValueAxis::tickCount
123 124 The number of tick marks for the axis.
124 125 */
125 126
126 127 /*!
127 128 \property QValueAxis::niceNumbersEnabled
128 129 Whether the nice numbers algorithm is enabled or not for the axis.
129 130 */
130 131
131 132 /*!
132 133 \qmlproperty bool ValueAxis::niceNumbersEnabled
133 134 Whether the nice numbers algorithm is enabled or not for the axis.
134 135 */
135 136
136 137 /*!
137 138 Constructs an axis object which is a child of \a parent.
138 139 */
139 140 QValueAxis::QValueAxis(QObject *parent) :
140 141 QAbstractAxis(*new QValueAxisPrivate(this),parent)
141 142 {
142 143
143 144 }
144 145
145 146 /*!
146 147 \internal
147 148 */
148 149 QValueAxis::QValueAxis(QValueAxisPrivate &d,QObject *parent) : QAbstractAxis(d,parent)
149 150 {
150 151
151 152 }
152 153
153 154 /*!
154 155 Destroys the object
155 156 */
156 157 QValueAxis::~QValueAxis()
157 158 {
158 159 Q_D(QValueAxis);
159 160 if(d->m_dataset) {
160 161 d->m_dataset->removeAxis(this);
161 162 }
162 163 }
163 164
164 165 void QValueAxis::setMin(qreal min)
165 166 {
166 167 Q_D(QValueAxis);
167 168 setRange(min, qMax(d->m_max, min));
168 169 }
169 170
170 171 qreal QValueAxis::min() const
171 172 {
172 173 Q_D(const QValueAxis);
173 174 return d->m_min;
174 175 }
175 176
176 177 void QValueAxis::setMax(qreal max)
177 178 {
178 179 Q_D(QValueAxis);
179 180 setRange(qMin(d->m_min, max), max);
180 181 }
181 182
182 183 qreal QValueAxis::max() const
183 184 {
184 185 Q_D(const QValueAxis);
185 186 return d->m_max;
186 187 }
187 188
188 189 /*!
189 190 Sets range from \a min to \a max on the axis.
190 191 If min is greater than max then this function returns without making any changes.
191 192 */
192 193 void QValueAxis::setRange(qreal min, qreal max)
193 194 {
194 195 Q_D(QValueAxis);
195 196 bool changed = false;
196 197
197 198 if (min > max) return;
198 199
199 200 if(d->m_niceNumbers) {
200 201 int ticks = d->m_tickCount;
201 202 d->looseNiceNumbers(min, max, ticks);
202 203 if(ticks!=d->m_tickCount) setTickCount(ticks);
203 204 }
204 205
205 206 if (!qFuzzyIsNull(d->m_min - min)) {
206 207 d->m_min = min;
207 208 changed = true;
208 209 emit minChanged(min);
209 210 }
210 211
211 212 if (!qFuzzyIsNull(d->m_max - max)) {
212 213 d->m_max = max;
213 214 changed = true;
214 215 emit maxChanged(max);
215 216 }
216 217
217 218 if (changed) {
218 219 emit rangeChanged(min,max);
219 220 d->emitUpdated();
220 221 }
221 222 }
222 223
223 224 /*!
224 225 Sets \a count for ticks on the axis.
225 226 */
226 227 void QValueAxis::setTickCount(int count)
227 228 {
228 229 Q_D(QValueAxis);
229 230 if (d->m_tickCount != count && count >=2) {
230 231 d->m_tickCount = count;
231 232 d->emitUpdated();
232 233 }
233 234 }
234 235
235 236 /*!
236 237 \fn int QValueAxis::tickCount() const
237 238 Return number of ticks on the axis
238 239 */
239 240 int QValueAxis::tickCount() const
240 241 {
241 242 Q_D(const QValueAxis);
242 243 return d->m_tickCount;
243 244 }
244 245
245 246 void QValueAxis::setNiceNumbersEnabled(bool enable)
246 247 {
247 248 Q_D(QValueAxis);
248 249 if (d->m_niceNumbers != enable){
249 250 d->m_niceNumbers = enable;
250 251 if(enable && !qFuzzyIsNull(d->m_max - d->m_min)) {
251 252 setRange(d->m_min,d->m_max);
252 253 }
253 254 }
254 255 }
255 256
256 257 bool QValueAxis::niceNumbersEnabled() const
257 258 {
258 259 Q_D(const QValueAxis);
259 260 return d->m_niceNumbers;
260 261 }
261 262
262 263 void QValueAxis::setLabelFormat(const QString &format)
263 264 {
264 265 Q_D(QValueAxis);
265 266 d->m_format = format;
266 267 }
267 268
268 269 QString QValueAxis::labelFormat() const
269 270 {
270 271 Q_D(const QValueAxis);
271 272 return d->m_format;
272 273 }
273 274
274 275 /*!
275 276 Returns the type of the axis
276 277 */
277 278 QAbstractAxis::AxisType QValueAxis::type() const
278 279 {
279 280 return AxisTypeValue;
280 281 }
281 282
282 283 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
283 284
284 285 QValueAxisPrivate::QValueAxisPrivate(QValueAxis* q):
285 286 QAbstractAxisPrivate(q),
286 287 m_min(0),
287 288 m_max(0),
288 289 m_tickCount(5),
289 290 m_niceNumbers(false),
290 291 m_format("%g")
291 292 {
292 293
293 294 }
294 295
295 296 QValueAxisPrivate::~QValueAxisPrivate()
296 297 {
297 298
298 299 }
299 300
300 301 void QValueAxisPrivate::handleDomainUpdated()
301 302 {
302 303 Q_Q(QValueAxis);
303 304 Domain* domain = qobject_cast<Domain*>(sender());
304 305 Q_ASSERT(domain);
305 306
306 307 if(orientation()==Qt::Horizontal){
307 308 q->setRange(domain->minX(),domain->maxX());
308 309 }else if(orientation()==Qt::Vertical){
309 310 q->setRange(domain->minY(),domain->maxY());
310 311 }
311 312 }
312 313
313 314
314 315 void QValueAxisPrivate::setMin(const QVariant &min)
315 316 {
316 317 Q_Q(QValueAxis);
317 318 bool ok;
318 319 qreal value = min.toReal(&ok);
319 320 if(ok) q->setMin(value);
320 321 }
321 322
322 323 void QValueAxisPrivate::setMax(const QVariant &max)
323 324 {
324 325
325 326 Q_Q(QValueAxis);
326 327 bool ok;
327 328 qreal value = max.toReal(&ok);
328 329 if(ok) q->setMax(value);
329 330 }
330 331
331 332 void QValueAxisPrivate::setRange(const QVariant &min, const QVariant &max)
332 333 {
333 334 Q_Q(QValueAxis);
334 335 bool ok1;
335 336 bool ok2;
336 337 qreal value1 = min.toReal(&ok1);
337 338 qreal value2 = max.toReal(&ok2);
338 339 if(ok1&&ok2) q->setRange(value1,value2);
339 340 }
340 341
341 342 ChartAxis* QValueAxisPrivate::createGraphics(ChartPresenter* presenter)
342 343 {
343 344 Q_Q(QValueAxis);
344 345 if(m_orientation == Qt::Vertical){
345 346 return new ChartValueAxisY(q,presenter);
346 347 }else{
347 348 return new ChartValueAxisX(q,presenter);
348 349 }
349 350
350 351 }
351 352
352 353 void QValueAxisPrivate::intializeDomain(Domain* domain)
353 354 {
354 355 Q_Q(QValueAxis);
355 356 if(qFuzzyCompare(m_max,m_min)) {
356 357 if(m_orientation==Qt::Vertical){
357 358 q->setRange(domain->minY(),domain->maxY());
358 359 }else{
359 360 q->setRange(domain->minX(), domain->maxX());
360 361 }
361 362 } else {
362 363 if(m_orientation==Qt::Vertical){
363 364 domain->setRangeY(m_min, m_max);
364 365 }else{
365 366 domain->setRangeX(m_min, m_max);
366 367 }
367 368 }
368 369 }
369 370
370 371 //algorithm defined by Paul S.Heckbert GraphicalGems I
371 372
372 373 void QValueAxisPrivate::looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) const
373 374 {
374 375 qreal range = niceNumber(max-min,true); //range with ceiling
375 376 qreal step = niceNumber(range/(ticksCount-1),false);
376 377 min = qFloor(min/step);
377 378 max = qCeil(max/step);
378 379 ticksCount = int(max-min) +1;
379 380 min*=step;
380 381 max*=step;
381 382 }
382 383
383 384 //nice numbers can be expressed as form of 1*10^n, 2* 10^n or 5*10^n
384 385
385 386 qreal QValueAxisPrivate::niceNumber(qreal x,bool ceiling) const
386 387 {
387 388 qreal z = qPow(10,qFloor(log10(x))); //find corresponding number of the form of 10^n than is smaller than x
388 389 qreal q = x/z;//q<10 && q>=1;
389 390
390 391 if(ceiling) {
391 392 if(q <= 1.0) q=1;
392 393 else if(q <= 2.0) q=2;
393 394 else if(q <= 5.0) q=5;
394 395 else q=10;
395 396 }
396 397 else {
397 398 if(q < 1.5) q=1;
398 399 else if(q < 3.0) q=2;
399 400 else if(q < 7.0) q=5;
400 401 else q=10;
401 402 }
402 403 return q*z;
403 404 }
404 405
405 406 #include "moc_qvalueaxis.cpp"
406 407 #include "moc_qvalueaxis_p.cpp"
407 408
408 409 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now