##// END OF EJS Templates
QSeries name and QPieSeries properties to QML api
Tero Ahola -
r884:bc9561bc4b37
parent child
Show More
@@ -265,7 +265,8 QChart* ThemeWidget::createPieChart() const
265 }
265 }
266 qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count());
266 qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count());
267 series->setPieSize(pieSize);
267 series->setPieSize(pieSize);
268 series->setPiePosition(hPos, 0.5);
268 series->setHorizontalPosition(hPos);
269 series->setVerticalPosition(0.5);
269 chart->addSeries(series);
270 chart->addSeries(series);
270 }
271 }
271
272
@@ -85,13 +85,13 MainWidget::MainWidget(QWidget* parent)
85 m_hPosition->setMinimum(0.0);
85 m_hPosition->setMinimum(0.0);
86 m_hPosition->setMaximum(1.0);
86 m_hPosition->setMaximum(1.0);
87 m_hPosition->setSingleStep(0.1);
87 m_hPosition->setSingleStep(0.1);
88 m_hPosition->setValue(m_series->pieHorizontalPosition());
88 m_hPosition->setValue(m_series->horizontalPosition());
89
89
90 m_vPosition = new QDoubleSpinBox();
90 m_vPosition = new QDoubleSpinBox();
91 m_vPosition->setMinimum(0.0);
91 m_vPosition->setMinimum(0.0);
92 m_vPosition->setMaximum(1.0);
92 m_vPosition->setMaximum(1.0);
93 m_vPosition->setSingleStep(0.1);
93 m_vPosition->setSingleStep(0.1);
94 m_vPosition->setValue(m_series->pieVerticalPosition());
94 m_vPosition->setValue(m_series->verticalPosition());
95
95
96 m_sizeFactor = new QDoubleSpinBox();
96 m_sizeFactor = new QDoubleSpinBox();
97 m_sizeFactor->setMinimum(0.0);
97 m_sizeFactor->setMinimum(0.0);
@@ -215,7 +215,8 void MainWidget::updateChartSettings()
215
215
216 void MainWidget::updateSerieSettings()
216 void MainWidget::updateSerieSettings()
217 {
217 {
218 m_series->setPiePosition(m_hPosition->value(), m_vPosition->value());
218 m_series->setHorizontalPosition(m_hPosition->value());
219 m_series->setVerticalPosition(m_vPosition->value());
219 m_series->setPieSize(m_sizeFactor->value());
220 m_series->setPieSize(m_sizeFactor->value());
220 m_series->setPieStartAngle(m_startAngle->value());
221 m_series->setPieStartAngle(m_startAngle->value());
221 m_series->setPieEndAngle(m_endAngle->value());
222 m_series->setPieEndAngle(m_endAngle->value());
@@ -40,7 +40,6 void DeclarativePieSeries::componentComplete()
40 if (declarativeChart) {
40 if (declarativeChart) {
41 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
41 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
42 Q_ASSERT(chart);
42 Q_ASSERT(chart);
43 qDebug() << "parent for pie:" << chart;
44 chart->addSeries(this);
43 chart->addSeries(this);
45 }
44 }
46 }
45 }
@@ -133,8 +133,8 void PieChartItem::handleGeometryChanged(const QRectF& rect)
133 void PieChartItem::calculatePieLayout()
133 void PieChartItem::calculatePieLayout()
134 {
134 {
135 // find pie center coordinates
135 // find pie center coordinates
136 m_pieCenter.setX(m_rect.left() + (m_rect.width() * m_series->pieHorizontalPosition()));
136 m_pieCenter.setX(m_rect.left() + (m_rect.width() * m_series->horizontalPosition()));
137 m_pieCenter.setY(m_rect.top() + (m_rect.height() * m_series->pieVerticalPosition()));
137 m_pieCenter.setY(m_rect.top() + (m_rect.height() * m_series->verticalPosition()));
138
138
139 // find maximum radius for pie
139 // find maximum radius for pie
140 m_pieRadius = m_rect.height() / 2;
140 m_pieRadius = m_rect.height() / 2;
@@ -184,6 +184,32 void QPieSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
184 q->remove(m_slices.at(start));
184 q->remove(m_slices.at(start));
185 }
185 }
186
186
187 bool QPieSeriesPrivate::setRealValue(qreal &value, qreal newValue, qreal max, qreal min)
188 {
189 // Remove rounding errors
190 qreal roundedValue = newValue;
191 if (qFuzzyIsNull(min) && qFuzzyIsNull(newValue))
192 roundedValue = 0.0;
193 else if (qFuzzyCompare(newValue, max))
194 roundedValue = max;
195 else if (qFuzzyCompare(newValue, min))
196 roundedValue = min;
197
198 // Check if the position is valid after removing the rounding errors
199 if (roundedValue < min || roundedValue > max) {
200 qWarning("QPieSeries: Illegal value");
201 return false;
202 }
203
204 if (!qFuzzyIsNull(value - roundedValue)) {
205 value = roundedValue;
206 return true;
207 }
208
209 // The change was so small it is considered a rounding error
210 return false;
211 }
212
187
213
188
214
189 /*!
215 /*!
@@ -380,32 +406,31 QList<QPieSlice*> QPieSeries::slices() const
380 }
406 }
381
407
382 /*!
408 /*!
383 Sets the center position of the pie by \a relativeHorizontalPosition and \a relativeVerticalPosition.
409 Sets the horizontal center position of the pie to \relativePosition. If \relativePosition is
384
410 set to 0.0 the pie is drawn on the left side of the chart and if it's set to 1.0 the pie is
385 The factors are relative to the chart rectangle where:
411 drawn on right side of the chart. The default value 0.5 puts the pie in the middle.
386
412
387 \a relativeHorizontalPosition 0.0 means the absolute left.
413 \sa setHorizontalPosition(), setPieSize()
388 \a relativeHorizontalPosition 1.0 means the absolute right.
414 */
389 \a relativeVerticalPosition 0.0 means the absolute top.
415 void QPieSeries::setHorizontalPosition(qreal relativePosition)
390 \a relativeVerticalPosition 1.0 means the absolute bottom.
416 {
417 Q_D(QPieSeries);
418 if (d->setRealValue(d->m_pieRelativeHorPos, relativePosition, 1.0))
419 emit piePositionChanged();
420 }
391
421
392 By default both values are 0.5 which puts the pie in the middle of the chart rectangle.
422 /*!
423 Sets the vertical center position of the pie to \relativePosition. If \relativePosition is
424 set to 0.0 the pie is drawn on the top of the chart and if it's set to 1.0 the pie is drawn
425 on bottom of the chart. The default value 0.5 puts the pie in the middle.
393
426
394 \sa pieHorizontalPosition(), pieVerticalPosition(), setPieSize()
427 \sa setVerticalPosition(), setPieSize()
395 */
428 */
396 void QPieSeries::setPiePosition(qreal relativeHorizontalPosition, qreal relativeVerticalPosition)
429 void QPieSeries::setVerticalPosition(qreal relativePosition)
397 {
430 {
398 Q_D(QPieSeries);
431 Q_D(QPieSeries);
399 if (relativeHorizontalPosition < 0.0 || relativeHorizontalPosition > 1.0 ||
432 if (d->setRealValue(d->m_pieRelativeVerPos, relativePosition, 1.0))
400 relativeVerticalPosition < 0.0 || relativeVerticalPosition > 1.0)
401 return;
402
403 if (!qFuzzyIsNull(d->m_pieRelativeHorPos - relativeHorizontalPosition) ||
404 !qFuzzyIsNull(d->m_pieRelativeVerPos - relativeVerticalPosition)) {
405 d->m_pieRelativeHorPos = relativeHorizontalPosition;
406 d->m_pieRelativeVerPos = relativeVerticalPosition;
407 emit piePositionChanged();
433 emit piePositionChanged();
408 }
409 }
434 }
410
435
411 /*!
436 /*!
@@ -418,9 +443,9 void QPieSeries::setPiePosition(qreal relativeHorizontalPosition, qreal relative
418
443
419 By default it is 0.5 which puts the pie in the horizontal middle of the chart rectangle.
444 By default it is 0.5 which puts the pie in the horizontal middle of the chart rectangle.
420
445
421 \sa setPiePosition(), pieVerticalPosition(), setPieSize()
446 \sa verticalPosition(), setPieSize()
422 */
447 */
423 qreal QPieSeries::pieHorizontalPosition() const
448 qreal QPieSeries::horizontalPosition() const
424 {
449 {
425 Q_D(const QPieSeries);
450 Q_D(const QPieSeries);
426 return d->m_pieRelativeHorPos;
451 return d->m_pieRelativeHorPos;
@@ -436,9 +461,9 qreal QPieSeries::pieHorizontalPosition() const
436
461
437 By default it is 0.5 which puts the pie in the vertical middle of the chart rectangle.
462 By default it is 0.5 which puts the pie in the vertical middle of the chart rectangle.
438
463
439 \sa setPiePosition(), pieHorizontalPosition(), setPieSize()
464 \sa horizontalPosition(), setPieSize()
440 */
465 */
441 qreal QPieSeries::pieVerticalPosition() const
466 qreal QPieSeries::verticalPosition() const
442 {
467 {
443 Q_D(const QPieSeries);
468 Q_D(const QPieSeries);
444 return d->m_pieRelativeVerPos;
469 return d->m_pieRelativeVerPos;
@@ -456,13 +481,8 qreal QPieSeries::pieVerticalPosition() const
456 void QPieSeries::setPieSize(qreal relativeSize)
481 void QPieSeries::setPieSize(qreal relativeSize)
457 {
482 {
458 Q_D(QPieSeries);
483 Q_D(QPieSeries);
459 if (relativeSize < 0.0 || relativeSize > 1.0)
484 if (d->setRealValue(d->m_pieRelativeSize, relativeSize, 1.0))
460 return;
461
462 if (!qFuzzyIsNull(d->m_pieRelativeSize- relativeSize)) {
463 d->m_pieRelativeSize = relativeSize;
464 emit pieSizeChanged();
485 emit pieSizeChanged();
465 }
466 }
486 }
467
487
468 /*!
488 /*!
@@ -493,14 +513,8 qreal QPieSeries::pieSize() const
493 void QPieSeries::setPieStartAngle(qreal angle)
513 void QPieSeries::setPieStartAngle(qreal angle)
494 {
514 {
495 Q_D(QPieSeries);
515 Q_D(QPieSeries);
496
516 if (d->setRealValue(d->m_pieStartAngle, angle, d->m_pieEndAngle))
497 if (angle < 0 || angle > 360 || angle > d->m_pieEndAngle)
498 return;
499
500 if (!qFuzzyIsNull(angle - d->m_pieStartAngle)) {
501 d->m_pieStartAngle = angle;
502 d->updateDerivativeData();
517 d->updateDerivativeData();
503 }
504 }
518 }
505
519
506 /*!
520 /*!
@@ -529,13 +543,8 void QPieSeries::setPieEndAngle(qreal angle)
529 {
543 {
530 Q_D(QPieSeries);
544 Q_D(QPieSeries);
531
545
532 if (angle < 0 || angle > 360 || angle < d->m_pieStartAngle)
546 if (d->setRealValue(d->m_pieEndAngle, angle, 360.0, d->m_pieStartAngle))
533 return;
534
535 if (!qFuzzyIsNull(angle - d->m_pieEndAngle)) {
536 d->m_pieEndAngle = angle;
537 d->updateDerivativeData();
547 d->updateDerivativeData();
538 }
539 }
548 }
540
549
541 /*!
550 /*!
@@ -30,6 +30,11 class QPieSlice;
30 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QSeries
30 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QSeries
31 {
31 {
32 Q_OBJECT
32 Q_OBJECT
33 Q_PROPERTY(qreal horizontalPosition READ horizontalPosition WRITE setHorizontalPosition)
34 Q_PROPERTY(qreal verticalPosition READ verticalPosition WRITE setVerticalPosition)
35 Q_PROPERTY(qreal size READ pieSize WRITE setPieSize)
36 Q_PROPERTY(qreal startAngle READ pieStartAngle WRITE setPieStartAngle)
37 Q_PROPERTY(qreal endAngle READ pieEndAngle WRITE setPieEndAngle)
33
38
34 public:
39 public:
35 QPieSeries(QObject *parent = 0);
40 QPieSeries(QObject *parent = 0);
@@ -57,9 +62,10 public:
57 qreal total() const;
62 qreal total() const;
58
63
59 // pie customization
64 // pie customization
60 void setPiePosition(qreal relativeHorizontalPosition, qreal relativeVerticalPosition);
65 void setHorizontalPosition(qreal relativePosition);
61 qreal pieHorizontalPosition() const;
66 qreal horizontalPosition() const;
62 qreal pieVerticalPosition() const;
67 void setVerticalPosition(qreal relativePosition);
68 qreal verticalPosition() const;
63 void setPieSize(qreal relativeSize);
69 void setPieSize(qreal relativeSize);
64 qreal pieSize() const;
70 qreal pieSize() const;
65 void setPieStartAngle(qreal startAngle);
71 void setPieStartAngle(qreal startAngle);
@@ -46,6 +46,7 public Q_SLOTS:
46 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
46 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
47 void modelDataAdded(QModelIndex parent, int start, int end);
47 void modelDataAdded(QModelIndex parent, int start, int end);
48 void modelDataRemoved(QModelIndex parent, int start, int end);
48 void modelDataRemoved(QModelIndex parent, int start, int end);
49 bool setRealValue(qreal &value, qreal newValue, qreal max, qreal min = 0.0);
49
50
50 public:
51 public:
51 QPieSeries * const q_ptr;
52 QPieSeries * const q_ptr;
@@ -82,6 +82,5
82 \brief Returns the name of the series.
82 \brief Returns the name of the series.
83 */
83 */
84
84
85 QTCOMMERCIALCHART_BEGIN_NAMESPACE
85 QTCOMMERCIALCHART_USE_NAMESPACE
86 #include "moc_qseries.cpp"
86 #include "moc_qseries.cpp"
87 QTCOMMERCIALCHART_END_NAMESPACE
@@ -33,6 +33,8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 class QTCOMMERCIALCHART_EXPORT QSeries : public QObject
33 class QTCOMMERCIALCHART_EXPORT QSeries : public QObject
34 {
34 {
35 Q_OBJECT
35 Q_OBJECT
36 Q_PROPERTY(QString name READ name WRITE setName)
37
36 public:
38 public:
37 enum QSeriesType {
39 enum QSeriesType {
38 SeriesTypeLine,
40 SeriesTypeLine,
@@ -58,16 +58,20 Rectangle {
58
58
59 BarSeries {
59 BarSeries {
60 barCategories: [ "2008", "2009", "2010", "2011", "2012" ]
60 barCategories: [ "2008", "2009", "2010", "2011", "2012" ]
61 // TBD: data
61 }
62 }
62
63
63 PieSeries {
64 PieSeries {
65 horizontalPosition: 0.2
66 verticalPosition: 0.3
67 size: 0.4
68 endAngle: 0.52 * 360 // The share of "others" is 52%
64 slices: [
69 slices: [
65 PieSlice { label: "Volkswagen"; value: 13.5 },
70 PieSlice { label: "Volkswagen"; value: 13.5 },
66 PieSlice { label: "Toyota"; value: 10.9 },
71 PieSlice { label: "Toyota"; value: 10.9 },
67 PieSlice { label: "Ford"; value: 8.6 },
72 PieSlice { label: "Ford"; value: 8.6 },
68 PieSlice { label: "Skoda"; value: 8.2 },
73 PieSlice { label: "Skoda"; value: 8.2 },
69 PieSlice { label: "Volvo"; value: 6.8 },
74 PieSlice { label: "Volvo"; value: 6.8 }
70 PieSlice { label: "Others"; value: 52.0 }
71 ]
75 ]
72 }
76 }
73 }
77 }
@@ -82,6 +86,7 Rectangle {
82 theme: Chart.ChartThemeBrownSand
86 theme: Chart.ChartThemeBrownSand
83
87
84 LineSeries {
88 LineSeries {
89 name: "Line"
85 points: [
90 points: [
86 XyPoint { x: 0.0; y: 0.0 },
91 XyPoint { x: 0.0; y: 0.0 },
87 XyPoint { x: 1.1; y: 2.1 },
92 XyPoint { x: 1.1; y: 2.1 },
@@ -91,6 +96,7 Rectangle {
91 }
96 }
92
97
93 SplineSeries {
98 SplineSeries {
99 name: "Spline"
94 points: [
100 points: [
95 XyPoint { x: 0.0; y: 0.3 },
101 XyPoint { x: 0.0; y: 0.3 },
96 XyPoint { x: 1.1; y: 3.2 },
102 XyPoint { x: 1.1; y: 3.2 },
@@ -99,6 +105,7 Rectangle {
99 }
105 }
100
106
101 AreaSeries {
107 AreaSeries {
108 name: "Area"
102 points: [
109 points: [
103 XyPoint { x: 0.0; y: 1.1 },
110 XyPoint { x: 0.0; y: 1.1 },
104 XyPoint { x: 2.5; y: 3.6 },
111 XyPoint { x: 2.5; y: 3.6 },
@@ -112,6 +119,7 Rectangle {
112 }
119 }
113
120
114 ScatterSeries {
121 ScatterSeries {
122 name: "Scatter1"
115 points: [
123 points: [
116 XyPoint { x: 1.5; y: 1.5 },
124 XyPoint { x: 1.5; y: 1.5 },
117 XyPoint { x: 1.5; y: 1.6 },
125 XyPoint { x: 1.5; y: 1.6 },
@@ -119,6 +127,7 Rectangle {
119 ]
127 ]
120 }
128 }
121 ScatterSeries {
129 ScatterSeries {
130 name: "Scatter2"
122 points: [
131 points: [
123 XyPoint { x: 2.0; y: 2.0 },
132 XyPoint { x: 2.0; y: 2.0 },
124 XyPoint { x: 2.0; y: 2.1 },
133 XyPoint { x: 2.0; y: 2.1 },
@@ -126,6 +135,7 Rectangle {
126 ]
135 ]
127 }
136 }
128 ScatterSeries {
137 ScatterSeries {
138 name: "Scatter3"
129 points: [
139 points: [
130 XyPoint { x: 2.6; y: 2.6 },
140 XyPoint { x: 2.6; y: 2.6 },
131 XyPoint { x: 2.6; y: 2.7 },
141 XyPoint { x: 2.6; y: 2.7 },
General Comments 0
You need to be logged in to leave comments. Login now