##// END OF EJS Templates
QDateTimeAxis: added snippet on how to use it
Marek Rosa -
r1882:325b4aac9444
parent child
Show More
@@ -1,320 +1,341
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 "qdatetimeaxis.h"
22 22 #include "qdatetimeaxis_p.h"
23 23 #include "chartdatetimeaxisx_p.h"
24 24 #include "chartdatetimeaxisy_p.h"
25 25 #include "domain_p.h"
26 26 #include <cmath>
27 27 #include <QDebug>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30 /*!
31 31 \class QDateTimeAxis
32 32 \brief The QDateTimeAxis class is used for manipulating chart's axis.
33 33 \mainclass
34 34
35 35 The labels can be configured by setting an appropriate DateTime format.
36 36 Note that any date before 4714 BCE or after about 1.4 million CE may not be accurately stored.
37 37 QDateTimeAxis can be setup to show axis line with tick marks, grid lines and shades.
38
39 Example code on how to use QDateTimeAxis.
40 \code
41 QChartView *chartView = new QChartView;
42 QLineSeries *series = new QLineSeries;
43
44 QDateTime xValue;
45 xValue.setDate(QDate(2012, 1 , 18));
46 xValue.setTime(QTime(9, 34));
47 series->append(xValue.toMSecsSinceEpoch(), 12);
48
49 xValue.setDate(QDate(2013, 5 , 11));
50 xValue.setTime(QTime(11, 14));
51 series->append(xValue.toMSecsSinceEpoch(), 22);
52 chartView->chart()->addSeries(series);
53
54 // ...
55 QDateTimeAxis *axisX = new QDateTimeAxis;
56 axisX->setFormat("dd-MM-yyyy h:mm");
57 chartView->chart()->setAxisX(series, axisX);
58 \endcode
38 59 */
39 60
40 61 /*!
41 62 \qmlclass DateTimeAxis QDateTimeAxis
42 63 \brief The DateTimeAxis element is used for manipulating chart's axes
43 64
44 65 The labels can be configured by setting an appropriate DateTime format.
45 66 Note that any date before 4714 BCE or after about 1.4 million CE may not be accurately stored.
46 67 DateTimeAxis can be setup to show axis line with tick marks, grid lines and shades.
47 68 */
48 69
49 70 /*!
50 71 \property QDateTimeAxis::min
51 72 Defines the minimum value on the axis.
52 73 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
53 74 */
54 75 /*!
55 76 \qmlproperty real ValuesAxis::min
56 77 Defines the minimum value on the axis.
57 78 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
58 79 */
59 80
60 81 /*!
61 82 \property QDateTimeAxis::max
62 83 Defines the maximum value on the axis.
63 84 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
64 85 */
65 86 /*!
66 87 \qmlproperty real ValuesAxis::max
67 88 Defines the maximum value on the axis.
68 89 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
69 90 */
70 91
71 92 /*!
72 93 \fn void QDateTimeAxis::minChanged(QDateTime min)
73 94 Axis emits signal when \a min of axis has changed.
74 95 */
75 96 /*!
76 97 \qmlsignal ValuesAxis::onMinChanged(QDateTime min)
77 98 Axis emits signal when \a min of axis has changed.
78 99 */
79 100
80 101 /*!
81 102 \fn void QDateTimeAxis::maxChanged(QDateTime max)
82 103 Axis emits signal when \a max of axis has changed.
83 104 */
84 105 /*!
85 106 \qmlsignal ValuesAxis::onMaxChanged(QDateTime max)
86 107 Axis emits signal when \a max of axis has changed.
87 108 */
88 109
89 110 /*!
90 111 \fn void QDateTimeAxis::rangeChanged(QDateTime min, QDateTime max)
91 112 Axis emits signal when \a min or \a max of axis has changed.
92 113 */
93 114
94 115 /*!
95 116 \property QDateTimeAxis::tickCount
96 117 The number of tick marks for the axis.
97 118 */
98 119
99 120 /*!
100 121 \qmlproperty int ValuesAxis::tickCount
101 122 The number of tick marks for the axis.
102 123 */
103 124
104 125 /*!
105 126 Constructs an axis object which is a child of \a parent.
106 127 */
107 128 QDateTimeAxis::QDateTimeAxis(QObject *parent) :
108 129 QAbstractAxis(*new QDateTimeAxisPrivate(this),parent)
109 130 {
110 131
111 132 }
112 133
113 134 /*!
114 135 \internal
115 136 */
116 137 QDateTimeAxis::QDateTimeAxis(QDateTimeAxisPrivate &d,QObject *parent) : QAbstractAxis(d,parent)
117 138 {
118 139
119 140 }
120 141
121 142 /*!
122 143 Destroys the object
123 144 */
124 145 QDateTimeAxis::~QDateTimeAxis()
125 146 {
126 147
127 148 }
128 149
129 150 void QDateTimeAxis::setMin(QDateTime min)
130 151 {
131 152 Q_D(QDateTimeAxis);
132 153 if (min.isValid())
133 154 setRange(min, qMax(d->m_max, min));
134 155 }
135 156
136 157 QDateTime QDateTimeAxis::min() const
137 158 {
138 159 Q_D(const QDateTimeAxis);
139 160 return d->m_min;
140 161 }
141 162
142 163 void QDateTimeAxis::setMax(QDateTime max)
143 164 {
144 165 Q_D(QDateTimeAxis);
145 166 if (max.isValid())
146 167 setRange(qMin(d->m_min, max), max);
147 168 }
148 169
149 170 QDateTime QDateTimeAxis::max() const
150 171 {
151 172 Q_D(const QDateTimeAxis);
152 173 return d->m_max;
153 174 }
154 175
155 176 /*!
156 177 Sets range from \a min to \a max on the axis.
157 178 If min is greater than max then this function returns without making any changes.
158 179 */
159 180 void QDateTimeAxis::setRange(QDateTime min, QDateTime max)
160 181 {
161 182 Q_D(QDateTimeAxis);
162 183 if (!min.isValid() || !max.isValid() || min > max)
163 184 return;
164 185
165 186 bool changed = false;
166 187 if (d->m_min != min) {
167 188 d->m_min = min;
168 189 changed = true;
169 190 emit minChanged(min);
170 191 }
171 192
172 193 if (d->m_max != max) {
173 194 d->m_max = max;
174 195 changed = true;
175 196 emit maxChanged(max);
176 197 }
177 198
178 199 if (changed) {
179 200 emit rangeChanged(d->m_min,d->m_max);
180 201 d->emitUpdated();
181 202 }
182 203 }
183 204
184 205 /*!
185 206 Sets \a format string that is used when creating label for the axis out of the QDateTime object.
186 207 Check QDateTime documentation for information on how the string should be defined.
187 208 \sa format()
188 209 */
189 210 void QDateTimeAxis::setFormat(QString format)
190 211 {
191 212 Q_D(QDateTimeAxis);
192 213 d->m_format = format;
193 214 }
194 215
195 216 /*!
196 217 Returns the format string that is used when creating label for the axis out of the QDateTime object.
197 218 Check QDateTime documentation for information on how the string should be defined.
198 219 \sa setFormat()
199 220 */
200 221 QString QDateTimeAxis::format() const
201 222 {
202 223 Q_D(const QDateTimeAxis);
203 224 return d->m_format;
204 225 }
205 226
206 227 /*!
207 228 Sets \a count for ticks on the axis.
208 229 */
209 230 void QDateTimeAxis::setTickCount(int count)
210 231 {
211 232 Q_D(QDateTimeAxis);
212 233 if (d->m_tickCount != count && count >=2) {
213 234 d->m_tickCount = count;
214 235 d->emitUpdated();
215 236 }
216 237 }
217 238
218 239 /*!
219 240 \fn int QDateTimeAxis::tickCount() const
220 241 Return number of ticks on the axis
221 242 */
222 243 int QDateTimeAxis::tickCount() const
223 244 {
224 245 Q_D(const QDateTimeAxis);
225 246 return d->m_tickCount;
226 247 }
227 248
228 249 /*!
229 250 Returns the type of the axis
230 251 */
231 252 QAbstractAxis::AxisType QDateTimeAxis::type() const
232 253 {
233 254 return AxisTypeDateTime;
234 255 }
235 256
236 257 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
237 258
238 259 QDateTimeAxisPrivate::QDateTimeAxisPrivate(QDateTimeAxis* q):
239 260 QAbstractAxisPrivate(q),
240 261 m_tickCount(5)
241 262 {
242 263 m_min = QDateTime::fromMSecsSinceEpoch(0);
243 264 m_max = QDateTime::fromMSecsSinceEpoch(0);
244 265 m_format = "dd-MMM-yyyy\nh:mm";
245 266 }
246 267
247 268 QDateTimeAxisPrivate::~QDateTimeAxisPrivate()
248 269 {
249 270
250 271 }
251 272
252 273 void QDateTimeAxisPrivate::handleDomainUpdated()
253 274 {
254 275 Q_Q(QDateTimeAxis);
255 276 Domain* domain = qobject_cast<Domain*>(sender());
256 277 Q_ASSERT(domain);
257 278
258 279 if(orientation()==Qt::Horizontal){
259 280 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minX()), QDateTime::fromMSecsSinceEpoch(domain->maxX()));
260 281 }else if(orientation()==Qt::Vertical){
261 282 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minY()), QDateTime::fromMSecsSinceEpoch(domain->maxY()));
262 283 }
263 284 }
264 285
265 286
266 287 void QDateTimeAxisPrivate::setMin(const QVariant &min)
267 288 {
268 289 Q_Q(QDateTimeAxis);
269 290 if (min.canConvert(QVariant::DateTime))
270 291 q->setMin(min.toDateTime());
271 292 }
272 293
273 294 void QDateTimeAxisPrivate::setMax(const QVariant &max)
274 295 {
275 296
276 297 Q_Q(QDateTimeAxis);
277 298 if (max.canConvert(QVariant::DateTime))
278 299 q->setMax(max.toDateTime());
279 300 }
280 301
281 302 void QDateTimeAxisPrivate::setRange(const QVariant &min, const QVariant &max)
282 303 {
283 304 Q_Q(QDateTimeAxis);
284 305 if (min.canConvert(QVariant::DateTime) && max.canConvert(QVariant::DateTime))
285 306 q->setRange(min.toDateTime(), max.toDateTime());
286 307 }
287 308
288 309 ChartAxis* QDateTimeAxisPrivate::createGraphics(ChartPresenter* presenter)
289 310 {
290 311 Q_Q(QDateTimeAxis);
291 312 if(m_orientation == Qt::Vertical){
292 313 return new ChartDateTimeAxisY(q,presenter);
293 314 }else{
294 315 return new ChartDateTimeAxisX(q,presenter);
295 316 }
296 317
297 318 }
298 319
299 320 void QDateTimeAxisPrivate::intializeDomain(Domain* domain)
300 321 {
301 322 Q_Q(QDateTimeAxis);
302 323 if(m_max == m_min) {
303 324 if(m_orientation==Qt::Vertical){
304 325 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minY()), QDateTime::fromMSecsSinceEpoch(domain->maxY()));
305 326 }else{
306 327 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minX()), QDateTime::fromMSecsSinceEpoch(domain->maxX()));
307 328 }
308 329 } else {
309 330 if(m_orientation==Qt::Vertical){
310 331 domain->setRangeY(m_min.toMSecsSinceEpoch(), m_max.toMSecsSinceEpoch());
311 332 }else{
312 333 domain->setRangeX(m_min.toMSecsSinceEpoch(), m_max.toMSecsSinceEpoch());
313 334 }
314 335 }
315 336 }
316 337
317 338 #include "moc_qdatetimeaxis.cpp"
318 339 #include "moc_qdatetimeaxis_p.cpp"
319 340
320 341 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,421 +1,423
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 Example code on how to use QValueAxis.
40 40 \code
41 41 QChartView *chartView = new QChartView;
42 42 QLineSeries *series = new QLineSeries;
43 43 // ...
44 chartView->chart()->addSeries(series);
45
44 46 QValueAxis *axisX = new QValueAxis;
45 47 axisX->setRange(10, 20.5);
46 48 axisX->setTickCount(10);
47 49 axisX->setLabelFormat("%.2f");
48 50 chartView->chart()->setAxisX(series, axisX);
49 51 \endcode
50 52 */
51 53
52 54 /*!
53 55 \qmlclass ValueAxis QValueAxis
54 56 \inherits AbstractAxis
55 57 \brief The ValueAxis element is used for manipulating chart's axes
56 58
57 59 ValueAxis can be setup to show axis line with tick marks, grid lines and shades.
58 60 Values of axis are drawn to position of ticks
59 61
60 62 To access Axes you can use ChartView API. For example:
61 63 \code
62 64 ChartView {
63 65 ValueAxis {
64 66 id: xAxis
65 67 min: 0
66 68 max: 10
67 69 }
68 70 // Add a few series...
69 71 }
70 72 \endcode
71 73 */
72 74
73 75 /*!
74 76 \property QValueAxis::min
75 77 Defines the minimum value on the axis.
76 78 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
77 79 */
78 80 /*!
79 81 \qmlproperty real ValueAxis::min
80 82 Defines the minimum value on the axis.
81 83 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
82 84 */
83 85
84 86 /*!
85 87 \property QValueAxis::max
86 88 Defines the maximum value on the axis.
87 89 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
88 90 */
89 91 /*!
90 92 \qmlproperty real ValueAxis::max
91 93 Defines the maximum value on the axis.
92 94 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
93 95 */
94 96
95 97 /*!
96 98 \property QValueAxis::labelFormat
97 99 Defines the label format for the axis.
98 100 See QString::sprintf() for the details.
99 101 */
100 102 /*!
101 103 \qmlproperty real ValueAxis::labelFormat
102 104 Defines the label format for the axis.
103 105 See QString::sprintf() for the details.
104 106 */
105 107
106 108 /*!
107 109 \fn void QValueAxis::minChanged(qreal min)
108 110 Axis emits signal when \a min of axis has changed.
109 111 */
110 112 /*!
111 113 \qmlsignal ValueAxis::onMinChanged(real min)
112 114 Axis emits signal when \a min of axis has changed.
113 115 */
114 116
115 117 /*!
116 118 \fn void QValueAxis::maxChanged(qreal max)
117 119 Axis emits signal when \a max of axis has changed.
118 120 */
119 121 /*!
120 122 \qmlsignal ValueAxis::onMaxChanged(real max)
121 123 Axis emits signal when \a max of axis has changed.
122 124 */
123 125
124 126 /*!
125 127 \fn void QValueAxis::rangeChanged(qreal min, qreal max)
126 128 Axis emits signal when \a min or \a max of axis has changed.
127 129 */
128 130
129 131 /*!
130 132 \property QValueAxis::tickCount
131 133 The number of tick marks for the axis.
132 134 */
133 135
134 136 /*!
135 137 \qmlproperty int ValueAxis::tickCount
136 138 The number of tick marks for the axis.
137 139 */
138 140
139 141 /*!
140 142 \property QValueAxis::niceNumbersEnabled
141 143 Whether the nice numbers algorithm is enabled or not for the axis.
142 144 */
143 145
144 146 /*!
145 147 \qmlproperty bool ValueAxis::niceNumbersEnabled
146 148 Whether the nice numbers algorithm is enabled or not for the axis.
147 149 */
148 150
149 151 /*!
150 152 Constructs an axis object which is a child of \a parent.
151 153 */
152 154 QValueAxis::QValueAxis(QObject *parent) :
153 155 QAbstractAxis(*new QValueAxisPrivate(this),parent)
154 156 {
155 157
156 158 }
157 159
158 160 /*!
159 161 \internal
160 162 */
161 163 QValueAxis::QValueAxis(QValueAxisPrivate &d,QObject *parent) : QAbstractAxis(d,parent)
162 164 {
163 165
164 166 }
165 167
166 168 /*!
167 169 Destroys the object
168 170 */
169 171 QValueAxis::~QValueAxis()
170 172 {
171 173 Q_D(QValueAxis);
172 174 if(d->m_dataset) {
173 175 d->m_dataset->removeAxis(this);
174 176 }
175 177 }
176 178
177 179 void QValueAxis::setMin(qreal min)
178 180 {
179 181 Q_D(QValueAxis);
180 182 setRange(min, qMax(d->m_max, min));
181 183 }
182 184
183 185 qreal QValueAxis::min() const
184 186 {
185 187 Q_D(const QValueAxis);
186 188 return d->m_min;
187 189 }
188 190
189 191 void QValueAxis::setMax(qreal max)
190 192 {
191 193 Q_D(QValueAxis);
192 194 setRange(qMin(d->m_min, max), max);
193 195 }
194 196
195 197 qreal QValueAxis::max() const
196 198 {
197 199 Q_D(const QValueAxis);
198 200 return d->m_max;
199 201 }
200 202
201 203 /*!
202 204 Sets range from \a min to \a max on the axis.
203 205 If min is greater than max then this function returns without making any changes.
204 206 */
205 207 void QValueAxis::setRange(qreal min, qreal max)
206 208 {
207 209 Q_D(QValueAxis);
208 210 bool changed = false;
209 211
210 212 if (min > max) return;
211 213
212 214 if(d->m_niceNumbers) {
213 215 int ticks = d->m_tickCount;
214 216 d->looseNiceNumbers(min, max, ticks);
215 217 if(ticks!=d->m_tickCount) setTickCount(ticks);
216 218 }
217 219
218 220 if (!qFuzzyIsNull(d->m_min - min)) {
219 221 d->m_min = min;
220 222 changed = true;
221 223 emit minChanged(min);
222 224 }
223 225
224 226 if (!qFuzzyIsNull(d->m_max - max)) {
225 227 d->m_max = max;
226 228 changed = true;
227 229 emit maxChanged(max);
228 230 }
229 231
230 232 if (changed) {
231 233 emit rangeChanged(min,max);
232 234 d->emitUpdated();
233 235 }
234 236 }
235 237
236 238 /*!
237 239 Sets \a count for ticks on the axis.
238 240 */
239 241 void QValueAxis::setTickCount(int count)
240 242 {
241 243 Q_D(QValueAxis);
242 244 if (d->m_tickCount != count && count >=2) {
243 245 d->m_tickCount = count;
244 246 d->emitUpdated();
245 247 }
246 248 }
247 249
248 250 /*!
249 251 \fn int QValueAxis::tickCount() const
250 252 Return number of ticks on the axis
251 253 */
252 254 int QValueAxis::tickCount() const
253 255 {
254 256 Q_D(const QValueAxis);
255 257 return d->m_tickCount;
256 258 }
257 259
258 260 void QValueAxis::setNiceNumbersEnabled(bool enable)
259 261 {
260 262 Q_D(QValueAxis);
261 263 if (d->m_niceNumbers != enable){
262 264 d->m_niceNumbers = enable;
263 265 if(enable && !qFuzzyIsNull(d->m_max - d->m_min)) {
264 266 setRange(d->m_min,d->m_max);
265 267 }
266 268 }
267 269 }
268 270
269 271 bool QValueAxis::niceNumbersEnabled() const
270 272 {
271 273 Q_D(const QValueAxis);
272 274 return d->m_niceNumbers;
273 275 }
274 276
275 277 void QValueAxis::setLabelFormat(const QString &format)
276 278 {
277 279 Q_D(QValueAxis);
278 280 d->m_format = format;
279 281 }
280 282
281 283 QString QValueAxis::labelFormat() const
282 284 {
283 285 Q_D(const QValueAxis);
284 286 return d->m_format;
285 287 }
286 288
287 289 /*!
288 290 Returns the type of the axis
289 291 */
290 292 QAbstractAxis::AxisType QValueAxis::type() const
291 293 {
292 294 return AxisTypeValue;
293 295 }
294 296
295 297 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
296 298
297 299 QValueAxisPrivate::QValueAxisPrivate(QValueAxis* q):
298 300 QAbstractAxisPrivate(q),
299 301 m_min(0),
300 302 m_max(0),
301 303 m_tickCount(5),
302 304 m_niceNumbers(false),
303 305 m_format("%g")
304 306 {
305 307
306 308 }
307 309
308 310 QValueAxisPrivate::~QValueAxisPrivate()
309 311 {
310 312
311 313 }
312 314
313 315 void QValueAxisPrivate::handleDomainUpdated()
314 316 {
315 317 Q_Q(QValueAxis);
316 318 Domain* domain = qobject_cast<Domain*>(sender());
317 319 Q_ASSERT(domain);
318 320
319 321 if(orientation()==Qt::Horizontal){
320 322 q->setRange(domain->minX(),domain->maxX());
321 323 }else if(orientation()==Qt::Vertical){
322 324 q->setRange(domain->minY(),domain->maxY());
323 325 }
324 326 }
325 327
326 328
327 329 void QValueAxisPrivate::setMin(const QVariant &min)
328 330 {
329 331 Q_Q(QValueAxis);
330 332 bool ok;
331 333 qreal value = min.toReal(&ok);
332 334 if(ok) q->setMin(value);
333 335 }
334 336
335 337 void QValueAxisPrivate::setMax(const QVariant &max)
336 338 {
337 339
338 340 Q_Q(QValueAxis);
339 341 bool ok;
340 342 qreal value = max.toReal(&ok);
341 343 if(ok) q->setMax(value);
342 344 }
343 345
344 346 void QValueAxisPrivate::setRange(const QVariant &min, const QVariant &max)
345 347 {
346 348 Q_Q(QValueAxis);
347 349 bool ok1;
348 350 bool ok2;
349 351 qreal value1 = min.toReal(&ok1);
350 352 qreal value2 = max.toReal(&ok2);
351 353 if(ok1&&ok2) q->setRange(value1,value2);
352 354 }
353 355
354 356 ChartAxis* QValueAxisPrivate::createGraphics(ChartPresenter* presenter)
355 357 {
356 358 Q_Q(QValueAxis);
357 359 if(m_orientation == Qt::Vertical){
358 360 return new ChartValueAxisY(q,presenter);
359 361 }else{
360 362 return new ChartValueAxisX(q,presenter);
361 363 }
362 364
363 365 }
364 366
365 367 void QValueAxisPrivate::intializeDomain(Domain* domain)
366 368 {
367 369 Q_Q(QValueAxis);
368 370 if(qFuzzyCompare(m_max,m_min)) {
369 371 if(m_orientation==Qt::Vertical){
370 372 q->setRange(domain->minY(),domain->maxY());
371 373 }else{
372 374 q->setRange(domain->minX(), domain->maxX());
373 375 }
374 376 } else {
375 377 if(m_orientation==Qt::Vertical){
376 378 domain->setRangeY(m_min, m_max);
377 379 }else{
378 380 domain->setRangeX(m_min, m_max);
379 381 }
380 382 }
381 383 }
382 384
383 385 //algorithm defined by Paul S.Heckbert GraphicalGems I
384 386
385 387 void QValueAxisPrivate::looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) const
386 388 {
387 389 qreal range = niceNumber(max-min,true); //range with ceiling
388 390 qreal step = niceNumber(range/(ticksCount-1),false);
389 391 min = qFloor(min/step);
390 392 max = qCeil(max/step);
391 393 ticksCount = int(max-min) +1;
392 394 min*=step;
393 395 max*=step;
394 396 }
395 397
396 398 //nice numbers can be expressed as form of 1*10^n, 2* 10^n or 5*10^n
397 399
398 400 qreal QValueAxisPrivate::niceNumber(qreal x,bool ceiling) const
399 401 {
400 402 qreal z = qPow(10,qFloor(log10(x))); //find corresponding number of the form of 10^n than is smaller than x
401 403 qreal q = x/z;//q<10 && q>=1;
402 404
403 405 if(ceiling) {
404 406 if(q <= 1.0) q=1;
405 407 else if(q <= 2.0) q=2;
406 408 else if(q <= 5.0) q=5;
407 409 else q=10;
408 410 }
409 411 else {
410 412 if(q < 1.5) q=1;
411 413 else if(q < 3.0) q=2;
412 414 else if(q < 7.0) q=5;
413 415 else q=10;
414 416 }
415 417 return q*z;
416 418 }
417 419
418 420 #include "moc_qvalueaxis.cpp"
419 421 #include "moc_qvalueaxis_p.cpp"
420 422
421 423 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now