##// END OF EJS Templates
Values and Intervals axes ranges are now initialized only if they haven't been preset earlier
Marek Rosa -
r1703:d83729eb88d8
parent child
Show More
@@ -1,125 +1,121
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 <QApplication>
22 22 #include <QMainWindow>
23 23 #include <QChartView>
24 24 #include <QLineSeries>
25 25 #include <QIntervalsAxis>
26 26
27 27 QTCOMMERCIALCHART_USE_NAMESPACE
28 28
29 29 int main(int argc, char *argv[])
30 30 {
31 31 QApplication a(argc, argv);
32 32
33 33 //![1]
34 34 QLineSeries* series = new QLineSeries();
35 *series << QPointF(0, 6) << QPointF(9, 4) << QPointF(15, 20) << QPointF(25, 12) << QPointF(29, 26);
35 *series << QPointF(0, 6) << QPointF(9, 4) << QPointF(15, 20) << QPointF(25, 12) << QPointF(39, 36);
36 36 QChart* chart = new QChart();
37 37 chart->legend()->hide();
38 38 chart->addSeries(series);
39 39 //![1]
40 40
41 41 //![2]
42 42 // Customize series
43 43 QPen pen(QRgb(0xfdb157));
44 44 pen.setWidth(5);
45 45 series->setPen(pen);
46 46
47 47 // Customize chart title
48 48 QFont font;
49 49 font.setPixelSize(18);
50 50 chart->setTitleFont(font);
51 51 chart->setTitleBrush(QBrush(Qt::white));
52 52 chart->setTitle("Customchart example");
53 53
54 54 // Customize chart background
55 55 QLinearGradient backgroundGradient;
56 56 backgroundGradient.setStart(QPointF(0,0));
57 57 backgroundGradient.setFinalStop(QPointF(0,1));
58 58 backgroundGradient.setColorAt(0.0, QRgb(0xd2d0d1));
59 59 backgroundGradient.setColorAt(1.0, QRgb(0x4c4547));
60 60 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
61 61 chart->setBackgroundBrush(backgroundGradient);
62 62 //![2]
63 63
64 64 //![3]
65 65 QIntervalsAxis* axisX = new QIntervalsAxis();
66 66 QIntervalsAxis* axisY = new QIntervalsAxis();
67 67
68 68 // Customize axis label font
69 69 QFont labelsFont;
70 70 labelsFont.setPixelSize(12);
71 71 axisX->setLabelsFont(labelsFont);
72 72 axisY->setLabelsFont(labelsFont);
73 73
74 74 // Customize axis colors
75 75 QPen axisPen(QRgb(0xd18952));
76 76 axisPen.setWidth(2);
77 77 axisX->setAxisPen(axisPen);
78 78 axisY->setAxisPen(axisPen);
79 79
80 80 // Customize axis label colors
81 81 QBrush axisBrush(Qt::white);
82 82 axisX->setLabelsBrush(axisBrush);
83 83 axisY->setLabelsBrush(axisBrush);
84 84
85 85 // Customize grid lines and shades
86 86 axisX->setGridLineVisible(false);
87 87 axisY->setGridLineVisible(false);
88 88 axisY->setShadesPen(Qt::NoPen);
89 89 axisY->setShadesBrush(QBrush(QRgb(0xa5a2a3)));
90 90 axisY->setShadesVisible(true);
91 91 //![3]
92 92
93 93 //![4]
94 94 axisX->append("low", 10);
95 95 axisX->append("optimal", 20);
96 96 axisX->append("high", 30);
97 97 axisX->setRange(0, 30);
98 // axisX->setRange("low","high");
99 98
100 99 axisY->append("slow", 10);
101 100 axisY->append("med", 20);
102 101 axisY->append("fast", 30);
103 102 axisY->setRange(0, 30);
104 // axisY->setRange("slow","fast");
105 103
106 104 chart->setAxisX(axisX, series);
107 105 chart->setAxisY(axisY, series);
108 axisX->setRange(0, 30);
109 axisY->setRange(0, 30);
110 106 //![4]
111 107
112 108 //![5]
113 109 QChartView* chartView = new QChartView(chart);
114 110 chartView->setRenderHint(QPainter::Antialiasing);
115 111 //![5]
116 112
117 113 //![6]
118 114 QMainWindow window;
119 115 window.setCentralWidget(chartView);
120 116 window.resize(400, 300);
121 117 window.show();
122 118 //![6]
123 119
124 120 return a.exec();
125 121 }
@@ -1,190 +1,190
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 "qintervalsaxis.h"
22 22 #include "qintervalsaxis_p.h"
23 23 #include "chartintervalsaxisx_p.h"
24 24 #include "chartintervalsaxisy_p.h"
25 25 #include <qmath.h>
26 26 #include <QDebug>
27 27
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29 /*!
30 30 \internal
31 31 \class QIntervalsAxis
32 32 \brief The QIntervalsAxis class is used for manipulating chart's axis.
33 33 \mainclass
34 34
35 35 Axis can be setup to show axis line with tick marks, grid lines and shades.
36 36 */
37 37
38 38 /*!
39 39 \qmlclass Axis QIntervalsAxis
40 40 \brief The Axis element is used for manipulating chart's axes.
41 41
42 42 Axis can be setup to show axis line with tick marks, grid lines and shades.
43 43
44 44 To access Axes you can use ChartView API. For example:
45 45 \code
46 46 // TODO :)
47 47 \endcode
48 48 */
49 49
50 50 /*!
51 51 Constructs an axis object which is a child of \a parent.
52 52 */
53 53 QIntervalsAxis::QIntervalsAxis(QObject *parent):
54 54 QValuesAxis(*new QIntervalsAxisPrivate(this),parent)
55 55 {
56 56 }
57 57
58 58 /*!
59 59 Destroys the object
60 60 */
61 61 QIntervalsAxis::~QIntervalsAxis()
62 62 {
63 63 }
64 64
65 65 /*!
66 66 \internal
67 67 */
68 68 QIntervalsAxis::QIntervalsAxis(QIntervalsAxisPrivate &d,QObject *parent):QValuesAxis(d,parent)
69 69 {
70 70
71 71 }
72 72
73 73 /*!
74 74 Appends \a category to axis
75 75 */
76 76 void QIntervalsAxis::append(const QString& intervalLabel, qreal interval)
77 77 {
78 78 Q_D(QIntervalsAxis);
79 79 if (!d->m_intervals.contains(intervalLabel))
80 80 {
81 81 if(d->m_intervals.isEmpty()){
82 82 Range range(d->m_categoryMinimum,interval);
83 83 d->m_intervalsMap.insert(intervalLabel, range);
84 84 d->m_intervals.append(intervalLabel);
85 85 }else{
86 86 Range range = d->m_intervalsMap.value(d->m_intervals.last());
87 87 d->m_intervalsMap.insert(intervalLabel, Range(range.second,interval));
88 88 d->m_intervals.append(intervalLabel);
89 89 }
90 setRange(d->m_min,interval);
90 // setRange(d->m_min,interval);
91 91 }
92 92 }
93 93
94 94 void QIntervalsAxis::setFisrtIntervalMinimum(qreal min)
95 95 {
96 96 Q_D(QIntervalsAxis);
97 97 if(d->m_intervals.isEmpty()){
98 98 d->m_categoryMinimum = min;
99 99 }else{
100 100 Range range = d->m_intervalsMap.value(d->m_intervals.first());
101 101 d->m_intervalsMap.insert(d->m_intervals.first(), Range(min, range.second));
102 102 setRange(min, d->m_min);
103 103 }
104 104 }
105 105
106 106 qreal QIntervalsAxis::intervalMin(const QString& intervalLabel) const
107 107 {
108 108 Q_D(const QIntervalsAxis);
109 109 return d->m_intervalsMap.value(intervalLabel).first;
110 110 }
111 111
112 112 qreal QIntervalsAxis::intervalMax(const QString& intervalLabel) const
113 113 {
114 114 Q_D(const QIntervalsAxis);
115 115 return d->m_intervalsMap.value(intervalLabel).second;
116 116 }
117 117
118 118 /*!
119 119 Removes \a category from axis
120 120 */
121 121 void QIntervalsAxis::remove(const QString &intervalLabel)
122 122 {
123 123 Q_UNUSED(intervalLabel);
124 124 //TODO
125 125 }
126 126
127 127 QStringList QIntervalsAxis::intervalsLabels()
128 128 {
129 129 Q_D(QIntervalsAxis);
130 130 return d->m_intervals;
131 131 }
132 132
133 133 /*!
134 134 Returns number of categories.
135 135 */
136 136 int QIntervalsAxis::count() const
137 137 {
138 138 Q_D(const QIntervalsAxis);
139 139 return d->m_intervals.count();
140 140 }
141 141
142 142 /*!
143 143 Returns the type of the axis
144 144 */
145 145 QAbstractAxis::AxisType QIntervalsAxis::type() const
146 146 {
147 147 return AxisTypeCategories;
148 148 }
149 149
150 150 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
151 151
152 152 QIntervalsAxisPrivate::QIntervalsAxisPrivate(QIntervalsAxis* q):
153 153 QValuesAxisPrivate(q),
154 154 m_categoryMinimum(0)
155 155 {
156 156
157 157 }
158 158
159 159 QIntervalsAxisPrivate::~QIntervalsAxisPrivate()
160 160 {
161 161
162 162 }
163 163
164 164 int QIntervalsAxisPrivate::ticksCount() const
165 165 {
166 166 return m_intervals.count() + 1;
167 167 }
168 168
169 169 void QIntervalsAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count)
170 170 {
171 171 Q_UNUSED(count);
172 172 m_min = min;
173 173 m_max = max;
174 174 // m_ticksCount = count;
175 175 }
176 176
177 177 ChartAxis* QIntervalsAxisPrivate::createGraphics(ChartPresenter* presenter)
178 178 {
179 179 Q_Q(QIntervalsAxis);
180 180 if(m_orientation == Qt::Vertical){
181 181 return new ChartIntervalAxisY(q,presenter);
182 182 }else{
183 183 return new ChartIntervalAxisX(q,presenter);
184 184 }
185 185 }
186 186
187 187 #include "moc_qintervalsaxis.cpp"
188 188 #include "moc_qintervalsaxis_p.cpp"
189 189
190 190 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,362 +1,368
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 "qvaluesaxis.h"
22 22 #include "qvaluesaxis_p.h"
23 23 #include "chartvaluesaxisx_p.h"
24 24 #include "chartvaluesaxisy_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 QValuesAxis
32 32 \brief The QValuesAxis class is used for manipulating chart's axis.
33 33 \mainclass
34 34
35 35 ValuesAxis can be setup to show axis line with tick marks, grid lines and shades.
36 36 Values of axis are drawn to position of ticks
37 37 */
38 38
39 39 /*!
40 40 \qmlclass ValuesAxis QValuesAxis
41 41 \brief The ValuesAxis element is used for manipulating chart's axes
42 42
43 43 ValueAxis can be setup to show axis line with tick marks, grid lines and shades.
44 44 Values of axis are drawn to position of ticks
45 45
46 46 To access Axes you can use ChartView API. For example:
47 47 \code
48 48 ChartView {
49 49 ValuesAxis {
50 50 id: xAxis
51 51 min: 0
52 52 max: 10
53 53 }
54 54 // Add a few series...
55 55 }
56 56 \endcode
57 57 */
58 58
59 59 /*!
60 60 \property QValuesAxis::min
61 61 Defines the minimum value on the axis.
62 62 */
63 63 /*!
64 64 \qmlproperty real ValuesAxis::min
65 65 Defines the minimum value on the axis.
66 66 */
67 67
68 68 /*!
69 69 \property QValuesAxis::max
70 70 Defines the maximum value on the axis.
71 71 */
72 72 /*!
73 73 \qmlproperty real ValuesAxis::max
74 74 Defines the maximum value on the axis.
75 75 */
76 76
77 77 /*!
78 78 \fn void QValuesAxis::minChanged(qreal min)
79 79 Axis emits signal when \a min of axis has changed.
80 80 */
81 81 /*!
82 82 \qmlsignal ValuesAxis::onMinChanged(real min)
83 83 Axis emits signal when \a min of axis has changed.
84 84 */
85 85
86 86 /*!
87 87 \fn void QValuesAxis::maxChanged(qreal max)
88 88 Axis emits signal when \a max of axis has changed.
89 89 */
90 90 /*!
91 91 \qmlsignal ValuesAxis::onMaxChanged(real max)
92 92 Axis emits signal when \a max of axis has changed.
93 93 */
94 94
95 95 /*!
96 96 \fn void QValuesAxis::rangeChanged(qreal min, qreal max)
97 97 Axis emits signal when \a min or \a max of axis has changed.
98 98 */
99 99
100 100 /*!
101 101 \property QValuesAxis::ticksCount
102 102 The number of tick marks for the axis.
103 103 */
104 104
105 105 /*!
106 106 \qmlproperty int ValuesAxis::ticksCount
107 107 The number of tick marks for the axis.
108 108 */
109 109
110 110 /*!
111 111 \property QValuesAxis::niceNumbersEnabled
112 112 Whether the nice numbers algorithm is enabled or not for the axis.
113 113 */
114 114
115 115 /*!
116 116 \qmlproperty bool ValuesAxis::niceNumbersEnabled
117 117 Whether the nice numbers algorithm is enabled or not for the axis.
118 118 */
119 119
120 120 /*!
121 121 Constructs an axis object which is a child of \a parent.
122 122 */
123 123 QValuesAxis::QValuesAxis(QObject *parent) :
124 124 QAbstractAxis(*new QValuesAxisPrivate(this),parent)
125 125 {
126 126
127 127 }
128 128
129 129 /*!
130 130 \internal
131 131 */
132 132 QValuesAxis::QValuesAxis(QValuesAxisPrivate &d,QObject *parent) : QAbstractAxis(d,parent)
133 133 {
134 134
135 135 }
136 136
137 137 /*!
138 138 Destroys the object
139 139 */
140 140 QValuesAxis::~QValuesAxis()
141 141 {
142 142
143 143 }
144 144
145 145 void QValuesAxis::setMin(qreal min)
146 146 {
147 147 Q_D(QValuesAxis);
148 148 setRange(min,d->m_max);
149 149 }
150 150
151 151 qreal QValuesAxis::min() const
152 152 {
153 153 Q_D(const QValuesAxis);
154 154 return d->m_min;
155 155 }
156 156
157 157 void QValuesAxis::setMax(qreal max)
158 158 {
159 159 Q_D(QValuesAxis);
160 160 setRange(d->m_min,max);
161 161 }
162 162
163 163 qreal QValuesAxis::max() const
164 164 {
165 165 Q_D(const QValuesAxis);
166 166 return d->m_max;
167 167 }
168 168
169 169 /*!
170 170 Sets range from \a min to \a max on the axis.
171 171 */
172 172 void QValuesAxis::setRange(qreal min, qreal max)
173 173 {
174 174 Q_D(QValuesAxis);
175 175 bool changed = false;
176 176
177 177 if (!qFuzzyIsNull(d->m_min - min)) {
178 178 d->m_min = min;
179 179 changed = true;
180 180 emit minChanged(min);
181 181 }
182 182
183 183 if (!qFuzzyIsNull(d->m_max - max)) {
184 184 d->m_max = max;
185 185 changed = true;
186 186 emit maxChanged(max);
187 187 }
188 188
189 189 if(d->m_niceNumbers) d->looseNiceNumbers(d->m_min, d->m_max, d->m_tickCount);
190 190
191 191 if (changed) {
192 192 emit rangeChanged(d->m_min,d->m_max);
193 193 d->emitUpdated();
194 194 }
195 195 }
196 196
197 197 /*!
198 198 Sets \a count for ticks on the axis.
199 199 */
200 200 void QValuesAxis::setTicksCount(int count)
201 201 {
202 202 Q_D(QValuesAxis);
203 203 if (d->m_tickCount != count && count >=2) {
204 204 d->m_tickCount = count;
205 205 d->emitUpdated();
206 206 }
207 207 }
208 208
209 209 /*!
210 210 \fn int QValuesAxis::ticksCount() const
211 211 Return number of ticks on the axis
212 212 */
213 213 int QValuesAxis::ticksCount() const
214 214 {
215 215 Q_D(const QValuesAxis);
216 216 return d->m_tickCount;
217 217 }
218 218
219 219 void QValuesAxis::setNiceNumbersEnabled(bool enable)
220 220 {
221 221 Q_D(QValuesAxis);
222 222 if (d->m_niceNumbers != enable){
223 223 d->m_niceNumbers = enable;
224 224 if(enable && !qFuzzyIsNull(d->m_max - d->m_min)) setRange(d->min(),d->max());
225 225 }
226 226 }
227 227
228 228 bool QValuesAxis::niceNumbersEnabled() const
229 229 {
230 230 Q_D(const QValuesAxis);
231 231 return d->m_niceNumbers;
232 232 }
233 233
234 234 /*!
235 235 Returns the type of the axis
236 236 */
237 237 QAbstractAxis::AxisType QValuesAxis::type() const
238 238 {
239 239 return AxisTypeValues;
240 240 }
241 241
242 242 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
243 243
244 244 QValuesAxisPrivate::QValuesAxisPrivate(QValuesAxis* q):
245 245 QAbstractAxisPrivate(q),
246 246 m_min(0),
247 247 m_max(0),
248 248 m_tickCount(5),
249 249 m_niceNumbers(false)
250 250 {
251 251
252 252 }
253 253
254 254 QValuesAxisPrivate::~QValuesAxisPrivate()
255 255 {
256 256
257 257 }
258 258
259 259 void QValuesAxisPrivate::handleDomainUpdated()
260 260 {
261 261 Q_Q(QValuesAxis);
262 262 Domain* domain = qobject_cast<Domain*>(sender());
263 263 Q_ASSERT(domain);
264 264
265 265 if(orientation()==Qt::Horizontal){
266 266 q->setRange(domain->minX(),domain->maxX());
267 267 }else if(orientation()==Qt::Vertical){
268 268 q->setRange(domain->minY(),domain->maxY());
269 269 }
270 270 }
271 271
272 272
273 273 void QValuesAxisPrivate::setMin(const QVariant &min)
274 274 {
275 275 Q_Q(QValuesAxis);
276 276 bool ok;
277 277 qreal value = min.toReal(&ok);
278 278 if(ok) q->setMin(value);
279 279 }
280 280
281 281 void QValuesAxisPrivate::setMax(const QVariant &max)
282 282 {
283 283
284 284 Q_Q(QValuesAxis);
285 285 bool ok;
286 286 qreal value = max.toReal(&ok);
287 287 if(ok) q->setMax(value);
288 288 }
289 289
290 290 void QValuesAxisPrivate::setRange(const QVariant &min, const QVariant &max)
291 291 {
292 292 Q_Q(QValuesAxis);
293 293 bool ok1;
294 294 bool ok2;
295 295 qreal value1 = min.toReal(&ok1);
296 296 qreal value2 = max.toReal(&ok2);
297 297 if(ok1&&ok2) q->setRange(value1,value2);
298 298 }
299 299
300 300 ChartAxis* QValuesAxisPrivate::createGraphics(ChartPresenter* presenter)
301 301 {
302 302 Q_Q(QValuesAxis);
303 303 if(m_orientation == Qt::Vertical){
304 304 return new ChartValuesAxisY(q,presenter);
305 305 }else{
306 306 return new ChartValuesAxisX(q,presenter);
307 307 }
308 308
309 309 }
310 310
311 311 void QValuesAxisPrivate::intializeDomain(Domain* domain)
312 312 {
313 313 if(qFuzzyCompare(m_max,m_min)) {
314 314 if(m_orientation==Qt::Vertical){
315 315 m_min = domain->minY();
316 316 m_max = domain->maxY();
317 317 }else{
318 318 m_min = domain->minX();
319 319 m_max = domain->maxX();
320 320 }
321 } else {
322 if(m_orientation==Qt::Vertical){
323 domain->setRangeY(m_min, m_max);
324 }else{
325 domain->setRangeX(m_min, m_max);
326 }
321 327 }
322 328 }
323 329
324 330 //algorithm defined by Paul S.Heckbert GraphicalGems I
325 331
326 332 void QValuesAxisPrivate::looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) const
327 333 {
328 334 qreal range = niceNumber(max-min,true); //range with ceiling
329 335 qreal step = niceNumber(range/(ticksCount-1),false);
330 336 min = floor(min/step);
331 337 max = ceil(max/step);
332 338 ticksCount = int(max-min) +1;
333 339 min*=step;
334 340 max*=step;
335 341 }
336 342
337 343 //nice numbers can be expressed as form of 1*10^n, 2* 10^n or 5*10^n
338 344
339 345 qreal QValuesAxisPrivate::niceNumber(qreal x,bool ceiling) const
340 346 {
341 347 qreal z = pow(10,floor(log10(x))); //find corresponding number of the form of 10^n than is smaller than x
342 348 qreal q = x/z;//q<10 && q>=1;
343 349
344 350 if(ceiling) {
345 351 if(q <= 1.0) q=1;
346 352 else if(q <= 2.0) q=2;
347 353 else if(q <= 5.0) q=5;
348 354 else q=10;
349 355 }
350 356 else {
351 357 if(q < 1.5) q=1;
352 358 else if(q < 3.0) q=2;
353 359 else if(q < 7.0) q=5;
354 360 else q=10;
355 361 }
356 362 return q*z;
357 363 }
358 364
359 365 #include "moc_qvaluesaxis.cpp"
360 366 #include "moc_qvaluesaxis_p.cpp"
361 367
362 368 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now