##// END OF EJS Templates
QValueAxis: added posibility to specify label format
Marek Rosa -
r1854:4846aebc38ef
parent child
Show More
@@ -1,371 +1,376
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 "chartaxis_p.h"
22 22 #include "qabstractaxis.h"
23 23 #include "qabstractaxis_p.h"
24 24 #include "chartpresenter_p.h"
25 25 #include "domain_p.h"
26 26 #include <qmath.h>
27 27 #include <QDateTime>
28 #include <QValueAxis>
28 29
29 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 31
31 32 ChartAxis::ChartAxis(QAbstractAxis *axis,ChartPresenter *presenter) : ChartElement(presenter),
32 33 m_chartAxis(axis),
33 34 m_labelsAngle(0),
34 35 m_grid(new QGraphicsItemGroup(presenter->rootItem())),
35 36 m_shades(new QGraphicsItemGroup(presenter->rootItem())),
36 37 m_labels(new QGraphicsItemGroup(presenter->rootItem())),
37 38 m_arrow(new QGraphicsItemGroup(presenter->rootItem())),
38 39 m_min(0),
39 40 m_max(0),
40 41 m_animation(0),
41 42 m_minWidth(0),
42 43 m_minHeight(0)
43 44 {
44 45 //initial initialization
45 46 m_arrow->setZValue(ChartPresenter::AxisZValue);
46 47 m_arrow->setHandlesChildEvents(false);
47 48 m_labels->setZValue(ChartPresenter::AxisZValue);
48 49 m_shades->setZValue(ChartPresenter::ShadesZValue);
49 50 m_grid->setZValue(ChartPresenter::GridZValue);
50 51
51 52 QObject::connect(m_chartAxis->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
52 53
53 54 QGraphicsSimpleTextItem item;
54 55 m_font = item.font();
55 56
56 57 }
57 58
58 59 ChartAxis::~ChartAxis()
59 60 {
60 61 }
61 62
62 63 void ChartAxis::setAnimation(AxisAnimation* animation)
63 64 {
64 65 m_animation=animation;
65 66 }
66 67
67 68 void ChartAxis::setLayout(QVector<qreal> &layout)
68 69 {
69 70 m_layoutVector=layout;
70 71 }
71 72
72 73 void ChartAxis::createItems(int count)
73 74 {
74 75 if (m_arrow->children().size() == 0)
75 76 m_arrow->addToGroup(new AxisItem(this,presenter()->rootItem()));
76 77 for (int i = 0; i < count; ++i) {
77 78 m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
78 79 m_labels->addToGroup(new QGraphicsSimpleTextItem(presenter()->rootItem()));
79 80 m_arrow->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
80 81 if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2) m_shades->addToGroup(new QGraphicsRectItem(presenter()->rootItem()));
81 82 }
82 83 }
83 84
84 85 void ChartAxis::deleteItems(int count)
85 86 {
86 87 QList<QGraphicsItem *> lines = m_grid->childItems();
87 88 QList<QGraphicsItem *> labels = m_labels->childItems();
88 89 QList<QGraphicsItem *> shades = m_shades->childItems();
89 90 QList<QGraphicsItem *> axis = m_arrow->childItems();
90 91
91 92 for (int i = 0; i < count; ++i) {
92 93 if (lines.size()%2 && lines.size() > 1) delete(shades.takeLast());
93 94 delete(lines.takeLast());
94 95 delete(labels.takeLast());
95 96 delete(axis.takeLast());
96 97 }
97 98 }
98 99
99 100 void ChartAxis::updateLayout(QVector<qreal> &layout)
100 101 {
101 102 int diff = m_layoutVector.size() - layout.size();
102 103
103 104 if (diff>0) {
104 105 deleteItems(diff);
105 106 }
106 107 else if (diff<0) {
107 108 createItems(-diff);
108 109 }
109 110
110 111 if(diff<0) handleAxisUpdated();
111 112
112 113 if (m_animation) {
113 114 switch(presenter()->state()){
114 115 case ChartPresenter::ZoomInState:
115 116 m_animation->setAnimationType(AxisAnimation::ZoomInAnimation);
116 117 m_animation->setAnimationPoint(presenter()->statePoint());
117 118 break;
118 119 case ChartPresenter::ZoomOutState:
119 120 m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation);
120 121 m_animation->setAnimationPoint(presenter()->statePoint());
121 122 break;
122 123 case ChartPresenter::ScrollUpState:
123 124 case ChartPresenter::ScrollLeftState:
124 125 m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation);
125 126 break;
126 127 case ChartPresenter::ScrollDownState:
127 128 case ChartPresenter::ScrollRightState:
128 129 m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation);
129 130 break;
130 131 case ChartPresenter::ShowState:
131 132 m_animation->setAnimationType(AxisAnimation::DefaultAnimation);
132 133 break;
133 134 }
134 135 m_animation->setValues(m_layoutVector,layout);
135 136 presenter()->startAnimation(m_animation);
136 137 }
137 138 else {
138 139 setLayout(layout);
139 140 updateGeometry();
140 141 }
141 142 }
142 143
143 144 void ChartAxis::setArrowOpacity(qreal opacity)
144 145 {
145 146 m_arrow->setOpacity(opacity);
146 147 }
147 148
148 149 qreal ChartAxis::arrowOpacity() const
149 150 {
150 151 return m_arrow->opacity();
151 152 }
152 153
153 154 void ChartAxis::setArrowVisibility(bool visible)
154 155 {
155 156 m_arrow->setOpacity(visible);
156 157 }
157 158
158 159 void ChartAxis::setGridOpacity(qreal opacity)
159 160 {
160 161 m_grid->setOpacity(opacity);
161 162 }
162 163
163 164 qreal ChartAxis::gridOpacity() const
164 165 {
165 166 return m_grid->opacity();
166 167 }
167 168
168 169 void ChartAxis::setGridVisibility(bool visible)
169 170 {
170 171 m_grid->setOpacity(visible);
171 172 }
172 173
173 174 void ChartAxis::setLabelsOpacity(qreal opacity)
174 175 {
175 176 m_labels->setOpacity(opacity);
176 177 }
177 178
178 179 qreal ChartAxis::labelsOpacity() const
179 180 {
180 181 return m_labels->opacity();
181 182 }
182 183
183 184 void ChartAxis::setLabelsVisibility(bool visible)
184 185 {
185 186 m_labels->setOpacity(visible);
186 187 }
187 188
188 189 void ChartAxis::setShadesOpacity(qreal opacity)
189 190 {
190 191 m_shades->setOpacity(opacity);
191 192 }
192 193
193 194 qreal ChartAxis::shadesOpacity() const
194 195 {
195 196 return m_shades->opacity();
196 197 }
197 198
198 199 void ChartAxis::setShadesVisibility(bool visible)
199 200 {
200 201 m_shades->setVisible(visible);
201 202 }
202 203
203 204 void ChartAxis::setLabelsAngle(int angle)
204 205 {
205 206 foreach(QGraphicsItem* item , m_labels->childItems()) {
206 207 item->setRotation(angle);
207 208 }
208 209
209 210 m_labelsAngle=angle;
210 211 }
211 212
212 213 void ChartAxis::setLabelsPen(const QPen &pen)
213 214 {
214 215 foreach(QGraphicsItem* item , m_labels->childItems()) {
215 216 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
216 217 }
217 218 }
218 219
219 220 void ChartAxis::setLabelsBrush(const QBrush &brush)
220 221 {
221 222 foreach(QGraphicsItem* item , m_labels->childItems()) {
222 223 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
223 224 }
224 225 }
225 226
226 227 void ChartAxis::setLabelsFont(const QFont &font)
227 228 {
228 229 foreach(QGraphicsItem* item , m_labels->childItems()) {
229 230 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
230 231 }
231 232 m_font = font;
232 233 }
233 234
234 235 void ChartAxis::setShadesBrush(const QBrush &brush)
235 236 {
236 237 foreach(QGraphicsItem* item , m_shades->childItems()) {
237 238 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
238 239 }
239 240 }
240 241
241 242 void ChartAxis::setShadesPen(const QPen &pen)
242 243 {
243 244 foreach(QGraphicsItem* item , m_shades->childItems()) {
244 245 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
245 246 }
246 247 }
247 248
248 249 void ChartAxis::setArrowPen(const QPen &pen)
249 250 {
250 251 foreach(QGraphicsItem* item , m_arrow->childItems()) {
251 252 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
252 253 }
253 254 }
254 255
255 256 void ChartAxis::setGridPen(const QPen &pen)
256 257 {
257 258 foreach(QGraphicsItem* item , m_grid->childItems()) {
258 259 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
259 260 }
260 261 }
261 262
262 263 bool ChartAxis::isEmpty()
263 264 {
264 265 return m_rect.isEmpty() || qFuzzyIsNull(m_min - m_max);
265 266 }
266 267
267 268 void ChartAxis::handleDomainUpdated()
268 269 {
269 270 Domain* domain = qobject_cast<Domain*>(sender());
270 271 qreal min(0);
271 272 qreal max(0);
272 273
273 274 if(m_chartAxis->orientation()==Qt::Horizontal) {
274 275 min = domain->minX();
275 276 max = domain->maxX();
276 277 }
277 278 else if (m_chartAxis->orientation()==Qt::Vertical)
278 279 {
279 280 min = domain->minY();
280 281 max = domain->maxY();
281 282 }
282 283
283 284 if (!qFuzzyIsNull(m_min - min) || !qFuzzyIsNull(m_max - max))
284 285 {
285 286 m_min = min;
286 287 m_max = max;
287 288
288 289 if (!isEmpty()) {
289 290 QVector<qreal> layout = calculateLayout();
290 291 updateLayout(layout);
291 292 }
292 293 }
293 294 }
294 295
295 296 void ChartAxis::handleAxisUpdated()
296 297 {
297 298 if(isEmpty()) return;
298 299
299 300
300 301 bool visible = m_chartAxis->isVisible();
301 302
302 303 setArrowVisibility(visible && m_chartAxis->isLineVisible());
303 304 setGridVisibility(visible && m_chartAxis->isGridLineVisible());
304 305 setLabelsVisibility(visible && m_chartAxis->labelsVisible());
305 306 setShadesVisibility(visible && m_chartAxis->shadesVisible());
306 307 setLabelsAngle(m_chartAxis->labelsAngle());
307 308 setArrowPen(m_chartAxis->linePen());
308 309 setLabelsPen(m_chartAxis->labelsPen());
309 310 setLabelsBrush(m_chartAxis->labelsBrush());
310 311 setLabelsFont(m_chartAxis->labelsFont());
311 312 setGridPen(m_chartAxis->gridLinePen());
312 313 setShadesPen(m_chartAxis->shadesPen());
313 314 setShadesBrush(m_chartAxis->shadesBrush());
314 315
315 316 }
316 317
317 318 void ChartAxis::hide()
318 319 {
319 320 setArrowVisibility(false);
320 321 setGridVisibility(false);
321 322 setLabelsVisibility(false);
322 323 setShadesVisibility(false);
323 324 }
324 325
325 326 void ChartAxis::handleGeometryChanged(const QRectF &rect)
326 327 {
327 328 if(m_rect != rect)
328 329 {
329 330 m_rect = rect;
330 331 if (isEmpty()) return;
331 332 QVector<qreal> layout = calculateLayout();
332 333 updateLayout(layout);
333 334 }
334 335 }
335 336
336 337
337 338 qreal ChartAxis::minimumWidth()
338 339 {
339 340 if(m_minWidth == 0) updateGeometry();
340 341 return m_minWidth;
341 342 }
342 343
343 344 qreal ChartAxis::minimumHeight()
344 345 {
345 346 if(m_minHeight == 0) updateGeometry();
346 347 return m_minHeight;
347 348 }
348 349
349 350
350 351 void ChartAxis::axisSelected()
351 352 {
352 353 qDebug()<<"TODO: axis clicked";
353 354 }
354 355
355 356
356 357 void ChartAxis::createNumberLabels(QStringList &labels,qreal min, qreal max, int ticks) const
357 358 {
358 359 Q_ASSERT(max>min);
359 360 Q_ASSERT(ticks>1);
360 361
361 362 int n = qMax(int(-qFloor(log10((max-min)/(ticks-1)))),0);
362 363 n++;
364
365 QValueAxis *axis = qobject_cast<QValueAxis *>(m_chartAxis);
366 QByteArray array = axis->labelFormat().toAscii();
363 367 for (int i=0; i< ticks; i++) {
364 368 qreal value = min + (i * (max - min)/ (ticks-1));
365 labels << QString::number(value,'f',n);
369 QString label;
370 labels << label.sprintf(array, value);;
366 371 }
367 372 }
368 373
369 374 #include "moc_chartaxis_p.cpp"
370 375
371 376 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,384 +1,397
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 42 \brief The ValueAxis element is used for manipulating chart's axes
43 43
44 44 ValueAxis can be setup to show axis line with tick marks, grid lines and shades.
45 45 Values of axis are drawn to position of ticks
46 46
47 47 To access Axes you can use ChartView API. For example:
48 48 \code
49 49 ChartView {
50 50 ValueAxis {
51 51 id: xAxis
52 52 min: 0
53 53 max: 10
54 54 }
55 55 // Add a few series...
56 56 }
57 57 \endcode
58 58 */
59 59
60 60 /*!
61 61 \property QValueAxis::min
62 62 Defines the minimum value on the axis.
63 63 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
64 64 */
65 65 /*!
66 66 \qmlproperty real ValueAxis::min
67 67 Defines the minimum value on the axis.
68 68 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
69 69 */
70 70
71 71 /*!
72 72 \property QValueAxis::max
73 73 Defines the maximum value on the axis.
74 74 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
75 75 */
76 76 /*!
77 77 \qmlproperty real ValueAxis::max
78 78 Defines the maximum value on the axis.
79 79 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
80 80 */
81 81
82 82 /*!
83 83 \fn void QValueAxis::minChanged(qreal min)
84 84 Axis emits signal when \a min of axis has changed.
85 85 */
86 86 /*!
87 87 \qmlsignal ValueAxis::onMinChanged(real min)
88 88 Axis emits signal when \a min of axis has changed.
89 89 */
90 90
91 91 /*!
92 92 \fn void QValueAxis::maxChanged(qreal max)
93 93 Axis emits signal when \a max of axis has changed.
94 94 */
95 95 /*!
96 96 \qmlsignal ValueAxis::onMaxChanged(real max)
97 97 Axis emits signal when \a max of axis has changed.
98 98 */
99 99
100 100 /*!
101 101 \fn void QValueAxis::rangeChanged(qreal min, qreal max)
102 102 Axis emits signal when \a min or \a max of axis has changed.
103 103 */
104 104
105 105 /*!
106 106 \property QValueAxis::tickCount
107 107 The number of tick marks for the axis.
108 108 */
109 109
110 110 /*!
111 111 \qmlproperty int ValueAxis::tickCount
112 112 The number of tick marks for the axis.
113 113 */
114 114
115 115 /*!
116 116 \property QValueAxis::niceNumbersEnabled
117 117 Whether the nice numbers algorithm is enabled or not for the axis.
118 118 */
119 119
120 120 /*!
121 121 \qmlproperty bool ValueAxis::niceNumbersEnabled
122 122 Whether the nice numbers algorithm is enabled or not for the axis.
123 123 */
124 124
125 125 /*!
126 126 Constructs an axis object which is a child of \a parent.
127 127 */
128 128 QValueAxis::QValueAxis(QObject *parent) :
129 129 QAbstractAxis(*new QValueAxisPrivate(this),parent)
130 130 {
131 131
132 132 }
133 133
134 134 /*!
135 135 \internal
136 136 */
137 137 QValueAxis::QValueAxis(QValueAxisPrivate &d,QObject *parent) : QAbstractAxis(d,parent)
138 138 {
139 139
140 140 }
141 141
142 142 /*!
143 143 Destroys the object
144 144 */
145 145 QValueAxis::~QValueAxis()
146 146 {
147 147 Q_D(QValueAxis);
148 148 if(d->m_dataset) {
149 149 d->m_dataset->removeAxis(this);
150 150 }
151 151 }
152 152
153 153 void QValueAxis::setMin(qreal min)
154 154 {
155 155 Q_D(QValueAxis);
156 156 setRange(min, qMax(d->m_max, min));
157 157 }
158 158
159 159 qreal QValueAxis::min() const
160 160 {
161 161 Q_D(const QValueAxis);
162 162 return d->m_min;
163 163 }
164 164
165 165 void QValueAxis::setMax(qreal max)
166 166 {
167 167 Q_D(QValueAxis);
168 168 setRange(qMin(d->m_min, max), max);
169 169 }
170 170
171 171 qreal QValueAxis::max() const
172 172 {
173 173 Q_D(const QValueAxis);
174 174 return d->m_max;
175 175 }
176 176
177 177 /*!
178 178 Sets range from \a min to \a max on the axis.
179 179 If min is greater than max then this function returns without making any changes.
180 180 */
181 181 void QValueAxis::setRange(qreal min, qreal max)
182 182 {
183 183 Q_D(QValueAxis);
184 184 bool changed = false;
185 185
186 186 if (min > max) return;
187 187
188 188 if(d->m_niceNumbers) {
189 189 int ticks = d->m_tickCount;
190 190 d->looseNiceNumbers(min, max, ticks);
191 191 if(ticks!=d->m_tickCount) setTickCount(ticks);
192 192 }
193 193
194 194 if (!qFuzzyIsNull(d->m_min - min)) {
195 195 d->m_min = min;
196 196 changed = true;
197 197 emit minChanged(min);
198 198 }
199 199
200 200 if (!qFuzzyIsNull(d->m_max - max)) {
201 201 d->m_max = max;
202 202 changed = true;
203 203 emit maxChanged(max);
204 204 }
205 205
206 206 if (changed) {
207 207 emit rangeChanged(min,max);
208 208 d->emitUpdated();
209 209 }
210 210 }
211 211
212 212 /*!
213 213 Sets \a count for ticks on the axis.
214 214 */
215 215 void QValueAxis::setTickCount(int count)
216 216 {
217 217 Q_D(QValueAxis);
218 218 if (d->m_tickCount != count && count >=2) {
219 219 d->m_tickCount = count;
220 220 d->emitUpdated();
221 221 }
222 222 }
223 223
224 224 /*!
225 225 \fn int QValueAxis::tickCount() const
226 226 Return number of ticks on the axis
227 227 */
228 228 int QValueAxis::tickCount() const
229 229 {
230 230 Q_D(const QValueAxis);
231 231 return d->m_tickCount;
232 232 }
233 233
234 234 void QValueAxis::setNiceNumbersEnabled(bool enable)
235 235 {
236 236 Q_D(QValueAxis);
237 237 if (d->m_niceNumbers != enable){
238 238 d->m_niceNumbers = enable;
239 239 if(enable && !qFuzzyIsNull(d->m_max - d->m_min)) {
240 240 setRange(d->m_min,d->m_max);
241 241 }
242 242 }
243 243 }
244 244
245 245 bool QValueAxis::niceNumbersEnabled() const
246 246 {
247 247 Q_D(const QValueAxis);
248 248 return d->m_niceNumbers;
249 249 }
250 250
251 void QValueAxis::setLabelFormat(const QString &format)
252 {
253 Q_D(QValueAxis);
254 d->m_format = format;
255 }
256
257 QString QValueAxis::labelFormat() const
258 {
259 Q_D(const QValueAxis);
260 return d->m_format;
261 }
262
251 263 /*!
252 264 Returns the type of the axis
253 265 */
254 266 QAbstractAxis::AxisType QValueAxis::type() const
255 267 {
256 268 return AxisTypeValue;
257 269 }
258 270
259 271 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
260 272
261 273 QValueAxisPrivate::QValueAxisPrivate(QValueAxis* q):
262 274 QAbstractAxisPrivate(q),
263 275 m_min(0),
264 276 m_max(0),
265 277 m_tickCount(5),
266 m_niceNumbers(false)
278 m_niceNumbers(false),
279 m_format("%g")
267 280 {
268 281
269 282 }
270 283
271 284 QValueAxisPrivate::~QValueAxisPrivate()
272 285 {
273 286
274 287 }
275 288
276 289 void QValueAxisPrivate::handleDomainUpdated()
277 290 {
278 291 Q_Q(QValueAxis);
279 292 Domain* domain = qobject_cast<Domain*>(sender());
280 293 Q_ASSERT(domain);
281 294
282 295 if(orientation()==Qt::Horizontal){
283 296 q->setRange(domain->minX(),domain->maxX());
284 297 }else if(orientation()==Qt::Vertical){
285 298 q->setRange(domain->minY(),domain->maxY());
286 299 }
287 300 }
288 301
289 302
290 303 void QValueAxisPrivate::setMin(const QVariant &min)
291 304 {
292 305 Q_Q(QValueAxis);
293 306 bool ok;
294 307 qreal value = min.toReal(&ok);
295 308 if(ok) q->setMin(value);
296 309 }
297 310
298 311 void QValueAxisPrivate::setMax(const QVariant &max)
299 312 {
300 313
301 314 Q_Q(QValueAxis);
302 315 bool ok;
303 316 qreal value = max.toReal(&ok);
304 317 if(ok) q->setMax(value);
305 318 }
306 319
307 320 void QValueAxisPrivate::setRange(const QVariant &min, const QVariant &max)
308 321 {
309 322 Q_Q(QValueAxis);
310 323 bool ok1;
311 324 bool ok2;
312 325 qreal value1 = min.toReal(&ok1);
313 326 qreal value2 = max.toReal(&ok2);
314 327 if(ok1&&ok2) q->setRange(value1,value2);
315 328 }
316 329
317 330 ChartAxis* QValueAxisPrivate::createGraphics(ChartPresenter* presenter)
318 331 {
319 332 Q_Q(QValueAxis);
320 333 if(m_orientation == Qt::Vertical){
321 334 return new ChartValueAxisY(q,presenter);
322 335 }else{
323 336 return new ChartValueAxisX(q,presenter);
324 337 }
325 338
326 339 }
327 340
328 341 void QValueAxisPrivate::intializeDomain(Domain* domain)
329 342 {
330 343 Q_Q(QValueAxis);
331 344 if(qFuzzyCompare(m_max,m_min)) {
332 345 if(m_orientation==Qt::Vertical){
333 346 q->setRange(domain->minY(),domain->maxY());
334 347 }else{
335 348 q->setRange(domain->minX(), domain->maxX());
336 349 }
337 350 } else {
338 351 if(m_orientation==Qt::Vertical){
339 352 domain->setRangeY(m_min, m_max);
340 353 }else{
341 354 domain->setRangeX(m_min, m_max);
342 355 }
343 356 }
344 357 }
345 358
346 359 //algorithm defined by Paul S.Heckbert GraphicalGems I
347 360
348 361 void QValueAxisPrivate::looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) const
349 362 {
350 363 qreal range = niceNumber(max-min,true); //range with ceiling
351 364 qreal step = niceNumber(range/(ticksCount-1),false);
352 365 min = qFloor(min/step);
353 366 max = qCeil(max/step);
354 367 ticksCount = int(max-min) +1;
355 368 min*=step;
356 369 max*=step;
357 370 }
358 371
359 372 //nice numbers can be expressed as form of 1*10^n, 2* 10^n or 5*10^n
360 373
361 374 qreal QValueAxisPrivate::niceNumber(qreal x,bool ceiling) const
362 375 {
363 376 qreal z = qPow(10,qFloor(log10(x))); //find corresponding number of the form of 10^n than is smaller than x
364 377 qreal q = x/z;//q<10 && q>=1;
365 378
366 379 if(ceiling) {
367 380 if(q <= 1.0) q=1;
368 381 else if(q <= 2.0) q=2;
369 382 else if(q <= 5.0) q=5;
370 383 else q=10;
371 384 }
372 385 else {
373 386 if(q < 1.5) q=1;
374 387 else if(q < 3.0) q=2;
375 388 else if(q < 7.0) q=5;
376 389 else q=10;
377 390 }
378 391 return q*z;
379 392 }
380 393
381 394 #include "moc_qvalueaxis.cpp"
382 395 #include "moc_qvalueaxis_p.cpp"
383 396
384 397 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,74 +1,77
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 #ifndef QVALUESAXIS_H
22 22 #define QVALUESAXIS_H
23 23
24 24 #include "qabstractaxis.h"
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 class QValueAxisPrivate;
29 29
30 30 class QTCOMMERCIALCHART_EXPORT QValueAxis : public QAbstractAxis
31 31 {
32 32 Q_OBJECT
33 33 Q_PROPERTY(int tickCount READ tickCount WRITE setTickCount)
34 34 Q_PROPERTY(bool niceNumbersEnabled READ niceNumbersEnabled WRITE setNiceNumbersEnabled)
35 35 Q_PROPERTY(qreal min READ min WRITE setMin NOTIFY minChanged)
36 36 Q_PROPERTY(qreal max READ max WRITE setMax NOTIFY maxChanged)
37 37
38 38 public:
39 39 explicit QValueAxis(QObject *parent = 0);
40 40 ~QValueAxis();
41 41
42 42 protected:
43 43 QValueAxis(QValueAxisPrivate &d,QObject *parent = 0);
44 44
45 45 public:
46 46 AxisType type() const;
47 47
48 48 //range handling
49 49 void setMin(qreal min);
50 50 qreal min() const;
51 51 void setMax(qreal max);
52 52 qreal max() const;
53 53 void setRange(qreal min, qreal max);
54 54
55 55 //ticks handling
56 56 void setTickCount(int count);
57 57 int tickCount() const;
58 58
59 void setLabelFormat(const QString &format);
60 QString labelFormat() const;
61
59 62 void setNiceNumbersEnabled(bool enable = true);
60 63 bool niceNumbersEnabled() const;
61 64
62 65 Q_SIGNALS:
63 66 void minChanged(qreal min);
64 67 void maxChanged(qreal max);
65 68 void rangeChanged(qreal min, qreal max);
66 69
67 70 private:
68 71 Q_DECLARE_PRIVATE(QValueAxis)
69 72 Q_DISABLE_COPY(QValueAxis)
70 73 };
71 74
72 75 QTCOMMERCIALCHART_END_NAMESPACE
73 76
74 77 #endif // QVALUESAXIS_H
@@ -1,72 +1,73
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 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30 #ifndef QVALUESAXIS_P_H
31 31 #define QVALUESAXIS_P_H
32 32
33 33 #include "qvalueaxis.h"
34 34 #include "qabstractaxis_p.h"
35 35
36 36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 37
38 38 class QValueAxisPrivate : public QAbstractAxisPrivate
39 39 {
40 40 Q_OBJECT
41 41 public:
42 42 QValueAxisPrivate(QValueAxis *q);
43 43 ~QValueAxisPrivate();
44 44
45 45 public:
46 46 ChartAxis* createGraphics(ChartPresenter* presenter);
47 47 void intializeDomain(Domain* domain);
48 48 void handleDomainUpdated();
49 49 qreal min(){ return m_min; };
50 50 qreal max(){ return m_max; };
51 51 int count() const { return m_tickCount;}
52 52
53 53 protected:
54 54 void setMin(const QVariant &min);
55 55 void setMax(const QVariant &max);
56 56 void setRange(const QVariant &min, const QVariant &max);
57 57
58 58 private:
59 59 void looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) const;
60 60 qreal niceNumber(qreal x,bool ceiling) const;
61 61
62 62 private:
63 63 qreal m_min;
64 64 qreal m_max;
65 65 int m_tickCount;
66 66 bool m_niceNumbers;
67 QString m_format;
67 68 Q_DECLARE_PUBLIC(QValueAxis)
68 69 };
69 70
70 71 QTCOMMERCIALCHART_END_NAMESPACE
71 72
72 73 #endif // QVALUESAXIS_P_H
General Comments 0
You need to be logged in to leave comments. Login now