##// END OF EJS Templates
pie: change the order of parameters when creating slices to be more intuitive
Jani Honkonen -
r1206:080b090a9a03
parent child
Show More
@@ -259,7 +259,7 QChart* ThemeWidget::createPieChart() const
259 for (int i = 0; i < m_dataTable.count(); i++) {
259 for (int i = 0; i < m_dataTable.count(); i++) {
260 QPieSeries *series = new QPieSeries(chart);
260 QPieSeries *series = new QPieSeries(chart);
261 foreach (Data data, m_dataTable[i]) {
261 foreach (Data data, m_dataTable[i]) {
262 QPieSlice *slice = series->append(data.first.y(), data.second);
262 QPieSlice *slice = series->append(data.second, data.first.y());
263 if (data == m_dataTable[i].first()) {
263 if (data == m_dataTable[i].first()) {
264 slice->setLabelVisible();
264 slice->setLabelVisible();
265 slice->setExploded();
265 slice->setExploded();
@@ -22,8 +22,8
22
22
23 QTCOMMERCIALCHART_USE_NAMESPACE
23 QTCOMMERCIALCHART_USE_NAMESPACE
24
24
25 CustomSlice::CustomSlice(qreal value, QString label)
25 CustomSlice::CustomSlice(QString label, qreal value)
26 :QPieSlice(value, label)
26 :QPieSlice(label, value)
27 {
27 {
28 connect(this, SIGNAL(hovered(bool)), this, SLOT(showHighlight(bool)));
28 connect(this, SIGNAL(hovered(bool)), this, SLOT(showHighlight(bool)));
29 }
29 }
@@ -29,7 +29,7 class CustomSlice : public QPieSlice
29 Q_OBJECT
29 Q_OBJECT
30
30
31 public:
31 public:
32 CustomSlice(qreal value, QString label);
32 CustomSlice(QString label, qreal value);
33
33
34 public:
34 public:
35 QBrush originalBrush();
35 QBrush originalBrush();
@@ -45,11 +45,11 MainWidget::MainWidget(QWidget* parent)
45
45
46 // create series
46 // create series
47 m_series = new QPieSeries();
47 m_series = new QPieSeries();
48 *m_series << new CustomSlice(10.0, "Slice 1");
48 *m_series << new CustomSlice("Slice 1", 10.0);
49 *m_series << new CustomSlice(20.0, "Slice 2");
49 *m_series << new CustomSlice("Slice 2", 20.0);
50 *m_series << new CustomSlice(30.0, "Slice 3");
50 *m_series << new CustomSlice("Slice 3", 30.0);
51 *m_series << new CustomSlice(40.0, "Slice 4");
51 *m_series << new CustomSlice("Slice 4", 40.0);
52 *m_series << new CustomSlice(50.0, "Slice 5");
52 *m_series << new CustomSlice("Slice 5", 50.0);
53 m_series->setLabelsVisible();
53 m_series->setLabelsVisible();
54 chart->addSeries(m_series);
54 chart->addSeries(m_series);
55
55
@@ -304,7 +304,7 void MainWidget::showFontDialog()
304
304
305 void MainWidget::appendSlice()
305 void MainWidget::appendSlice()
306 {
306 {
307 *m_series << new CustomSlice(10.0, "Slice " + QString::number(m_series->count()+1));
307 *m_series << new CustomSlice("Slice " + QString::number(m_series->count()+1), 10.0);
308 }
308 }
309
309
310 void MainWidget::insertSlice()
310 void MainWidget::insertSlice()
@@ -314,7 +314,7 void MainWidget::insertSlice()
314
314
315 int i = m_series->slices().indexOf(m_slice);
315 int i = m_series->slices().indexOf(m_slice);
316
316
317 m_series->insert(i, new CustomSlice(10.0, "Slice " + QString::number(m_series->count()+1)));
317 m_series->insert(i, new CustomSlice("Slice " + QString::number(m_series->count()+1), 10.0));
318 }
318 }
319
319
320 void MainWidget::removeSlice()
320 void MainWidget::removeSlice()
@@ -32,11 +32,11 int main(int argc, char *argv[])
32
32
33 //![1]
33 //![1]
34 QPieSeries *series = new QPieSeries();
34 QPieSeries *series = new QPieSeries();
35 series->append(1, "Jane");
35 series->append("Jane", 1);
36 series->append(2, "Joe");
36 series->append("Joe", 2);
37 series->append(3, "Andy");
37 series->append("Andy", 3);
38 series->append(4, "Barbara");
38 series->append("Barbara", 4);
39 series->append(5, "Axel");
39 series->append("Axel", 5);
40 //![1]
40 //![1]
41
41
42 //![2]
42 //![2]
@@ -81,7 +81,7 QPieSlice* DeclarativePieSeries::find(QString label)
81 QPieSlice* DeclarativePieSeries::append(QString name, qreal value)
81 QPieSlice* DeclarativePieSeries::append(QString name, qreal value)
82 {
82 {
83 // TODO: parameter order is wrong, switch it:
83 // TODO: parameter order is wrong, switch it:
84 return QPieSeries::append(value, name);
84 return QPieSeries::append(name, value);
85 }
85 }
86
86
87 void DeclarativePieSeries::setPieModel(DeclarativeTableModel *model)
87 void DeclarativePieSeries::setPieModel(DeclarativeTableModel *model)
@@ -191,12 +191,12 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
191
191
192
192
193 /*!
193 /*!
194 Appends a single slice to the series with give \a value and \a name.
194 Appends a single slice to the series with give \a value and \a label.
195 Slice ownership is passed to the series.
195 Slice ownership is passed to the series.
196 */
196 */
197 QPieSlice* QPieSeries::append(qreal value, QString name)
197 QPieSlice* QPieSeries::append(QString label, qreal value)
198 {
198 {
199 QPieSlice* slice = new QPieSlice(value, name);
199 QPieSlice* slice = new QPieSlice(label, value);
200 append(slice);
200 append(slice);
201 return slice;
201 return slice;
202 }
202 }
@@ -737,7 +737,7 void QPieSeriesPrivate::initializePieFromModel()
737 else
737 else
738 sliceCount = qMin(m_mapper->count(), m_model->rowCount() - m_mapper->first());
738 sliceCount = qMin(m_mapper->count(), m_model->rowCount() - m_mapper->first());
739 for (int i = m_mapper->first(); i < m_mapper->first() + sliceCount; i++)
739 for (int i = m_mapper->first(); i < m_mapper->first() + sliceCount; i++)
740 q->append(m_model->data(m_model->index(i, m_mapper->mapValues()), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(i, m_mapper->mapLabels()), Qt::DisplayRole).toString());
740 q->append(m_model->data(m_model->index(i, m_mapper->mapLabels()), Qt::DisplayRole).toString(), m_model->data(m_model->index(i, m_mapper->mapValues()), Qt::DisplayRole).toDouble());
741 } else {
741 } else {
742 if (m_mapper->mapValues() >= m_model->rowCount() || m_mapper->mapLabels() >= m_model->rowCount())
742 if (m_mapper->mapValues() >= m_model->rowCount() || m_mapper->mapLabels() >= m_model->rowCount())
743 return; // mapped columns are not existing
743 return; // mapped columns are not existing
@@ -748,7 +748,7 void QPieSeriesPrivate::initializePieFromModel()
748 else
748 else
749 sliceCount = qMin(m_mapper->count(), m_model->columnCount() - m_mapper->first());
749 sliceCount = qMin(m_mapper->count(), m_model->columnCount() - m_mapper->first());
750 for (int i = m_mapper->first(); i < m_mapper->first() + sliceCount; i++)
750 for (int i = m_mapper->first(); i < m_mapper->first() + sliceCount; i++)
751 q->append(m_model->data(m_model->index(m_mapper->mapValues(), i), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(m_mapper->mapLabels(), i), Qt::DisplayRole).toString());
751 q->append(m_model->data(m_model->index(m_mapper->mapLabels(), i), Qt::DisplayRole).toString(), m_model->data(m_model->index(m_mapper->mapValues(), i), Qt::DisplayRole).toDouble());
752 }
752 }
753 q->setLabelsVisible(true);
753 q->setLabelsVisible(true);
754 }
754 }
@@ -48,7 +48,7 public:
48 bool append(QPieSlice* slice);
48 bool append(QPieSlice* slice);
49 bool append(QList<QPieSlice*> slices);
49 bool append(QList<QPieSlice*> slices);
50 QPieSeries& operator << (QPieSlice* slice);
50 QPieSeries& operator << (QPieSlice* slice);
51 QPieSlice* append(qreal value, QString name);
51 QPieSlice* append(QString label, qreal value);
52 bool insert(int index, QPieSlice* slice);
52 bool insert(int index, QPieSlice* slice);
53 bool remove(QPieSlice* slice);
53 bool remove(QPieSlice* slice);
54 void clear();
54 void clear();
@@ -68,7 +68,7 QPieSlice::QPieSlice(QObject *parent)
68 Constructs an empty slice with given \a value, \a label and a \a parent.
68 Constructs an empty slice with given \a value, \a label and a \a parent.
69 \sa QPieSeries::append(), QPieSeries::insert()
69 \sa QPieSeries::append(), QPieSeries::insert()
70 */
70 */
71 QPieSlice::QPieSlice(qreal value, QString label, QObject *parent)
71 QPieSlice::QPieSlice(QString label, qreal value, QObject *parent)
72 :QObject(parent),
72 :QObject(parent),
73 d(new PieSliceData())
73 d(new PieSliceData())
74 {
74 {
@@ -39,7 +39,7 class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject
39
39
40 public:
40 public:
41 explicit QPieSlice(QObject *parent = 0);
41 explicit QPieSlice(QObject *parent = 0);
42 QPieSlice(qreal value, QString label, QObject *parent = 0);
42 QPieSlice(QString label, qreal value, QObject *parent = 0);
43 virtual ~QPieSlice();
43 virtual ~QPieSlice();
44
44
45 void setValue(qreal value);
45 void setValue(qreal value);
@@ -101,7 +101,7 void tst_qpieseries::append()
101 // append pointer
101 // append pointer
102 QPieSlice *slice1 = 0;
102 QPieSlice *slice1 = 0;
103 QVERIFY(!s.append(slice1));
103 QVERIFY(!s.append(slice1));
104 slice1 = new QPieSlice(1, "slice 1");
104 slice1 = new QPieSlice("slice 1", 1);
105 QVERIFY(s.append(slice1));
105 QVERIFY(s.append(slice1));
106 QVERIFY(!s.append(slice1));
106 QVERIFY(!s.append(slice1));
107 QCOMPARE(s.count(), 1);
107 QCOMPARE(s.count(), 1);
@@ -112,19 +112,19 void tst_qpieseries::append()
112 list << (QPieSlice *) 0;
112 list << (QPieSlice *) 0;
113 QVERIFY(!s.append(list));
113 QVERIFY(!s.append(list));
114 list.clear();
114 list.clear();
115 list << new QPieSlice(2, "slice 2");
115 list << new QPieSlice("slice 2", 2);
116 list << new QPieSlice(3, "slice 3");
116 list << new QPieSlice("slice 3", 3);
117 QVERIFY(s.append(list));
117 QVERIFY(s.append(list));
118 QVERIFY(!s.append(list));
118 QVERIFY(!s.append(list));
119 QCOMPARE(s.count(), 3);
119 QCOMPARE(s.count(), 3);
120
120
121 // append operator
121 // append operator
122 s << new QPieSlice(4, "slice 4");
122 s << new QPieSlice("slice 4", 4);
123 s << slice1; // fails because already added
123 s << slice1; // fails because already added
124 QCOMPARE(s.count(), 4);
124 QCOMPARE(s.count(), 4);
125
125
126 // append with params
126 // append with params
127 QPieSlice *slice5 = s.append(5, "slice 5");
127 QPieSlice *slice5 = s.append("slice 5", 5);
128 QVERIFY(slice5 != 0);
128 QVERIFY(slice5 != 0);
129 QCOMPARE(slice5->value(), 5.0);
129 QCOMPARE(slice5->value(), 5.0);
130 QCOMPARE(slice5->label(), QString("slice 5"));
130 QCOMPARE(slice5->label(), QString("slice 5"));
@@ -145,7 +145,7 void tst_qpieseries::insert()
145 // insert one slice
145 // insert one slice
146 QPieSlice *slice1 = 0;
146 QPieSlice *slice1 = 0;
147 QVERIFY(!s.insert(0, slice1));
147 QVERIFY(!s.insert(0, slice1));
148 slice1 = new QPieSlice(1, "slice 1");
148 slice1 = new QPieSlice("slice 1", 1);
149 QVERIFY(!s.insert(-1, slice1));
149 QVERIFY(!s.insert(-1, slice1));
150 QVERIFY(!s.insert(5, slice1));
150 QVERIFY(!s.insert(5, slice1));
151 QVERIFY(s.insert(0, slice1));
151 QVERIFY(s.insert(0, slice1));
@@ -153,12 +153,12 void tst_qpieseries::insert()
153 QCOMPARE(s.count(), 1);
153 QCOMPARE(s.count(), 1);
154
154
155 // add some more slices
155 // add some more slices
156 s.append(2, "slice 2");
156 s.append("slice 2", 2);
157 s.append(4, "slice 4");
157 s.append("slice 4", 4);
158 QCOMPARE(s.count(), 3);
158 QCOMPARE(s.count(), 3);
159
159
160 // insert between slices
160 // insert between slices
161 s.insert(2, new QPieSlice(3, "slice 3"));
161 s.insert(2, new QPieSlice("slice 3", 3));
162 QCOMPARE(s.count(), 4);
162 QCOMPARE(s.count(), 4);
163
163
164 // check slices
164 // check slices
@@ -173,9 +173,9 void tst_qpieseries::remove()
173 QPieSeries s;
173 QPieSeries s;
174
174
175 // add some slices
175 // add some slices
176 QPieSlice *slice1 = s.append(1, "slice 1");
176 QPieSlice *slice1 = s.append("slice 1", 1);
177 QPieSlice *slice2 = s.append(2, "slice 2");
177 QPieSlice *slice2 = s.append("slice 2", 2);
178 QPieSlice *slice3 = s.append(3, "slice 3");
178 QPieSlice *slice3 = s.append("slice 3", 3);
179 QSignalSpy spy1(slice1, SIGNAL(destroyed()));
179 QSignalSpy spy1(slice1, SIGNAL(destroyed()));
180 QSignalSpy spy2(slice2, SIGNAL(destroyed()));
180 QSignalSpy spy2(slice2, SIGNAL(destroyed()));
181 QSignalSpy spy3(slice3, SIGNAL(destroyed()));
181 QSignalSpy spy3(slice3, SIGNAL(destroyed()));
@@ -208,15 +208,15 void tst_qpieseries::calculatedValues()
208 QPieSeries s;
208 QPieSeries s;
209
209
210 // add a slice
210 // add a slice
211 QPieSlice *slice1 = s.append(1, "slice 1");
211 QPieSlice *slice1 = s.append("slice 1", 1);
212 verifyCalculatedData(s, &ok);
212 verifyCalculatedData(s, &ok);
213 if (!ok)
213 if (!ok)
214 return;
214 return;
215
215
216 // add some more slices
216 // add some more slices
217 QList<QPieSlice *> list;
217 QList<QPieSlice *> list;
218 list << new QPieSlice(2, "slice 2");
218 list << new QPieSlice("slice 2", 2);
219 list << new QPieSlice(3, "slice 3");
219 list << new QPieSlice("slice 3", 3);
220 s.append(list);
220 s.append(list);
221 verifyCalculatedData(s, &ok);
221 verifyCalculatedData(s, &ok);
222 if (!ok)
222 if (!ok)
@@ -229,7 +229,7 void tst_qpieseries::calculatedValues()
229 return;
229 return;
230
230
231 // insert a slice
231 // insert a slice
232 s.insert(0, new QPieSlice(1, "Slice 4"));
232 s.insert(0, new QPieSlice("Slice 4", 4));
233 verifyCalculatedData(s, &ok);
233 verifyCalculatedData(s, &ok);
234 if (!ok)
234 if (!ok)
235 return;
235 return;
@@ -271,9 +271,9 void tst_qpieseries::clickedSignal()
271 // create a pie series
271 // create a pie series
272 QPieSeries *series = new QPieSeries();
272 QPieSeries *series = new QPieSeries();
273 series->setPieSize(1.0);
273 series->setPieSize(1.0);
274 QPieSlice *s1 = series->append(1, "slice 1");
274 QPieSlice *s1 = series->append("slice 1", 1);
275 series->append(2, "slice 2");
275 series->append("slice 2", 2);
276 series->append(3, "slice 3");
276 series->append("slice 3", 3);
277 QSignalSpy clickSpy1(series, SIGNAL(clicked(QPieSlice*)));
277 QSignalSpy clickSpy1(series, SIGNAL(clicked(QPieSlice*)));
278
278
279 // add series to the chart
279 // add series to the chart
@@ -296,9 +296,9 void tst_qpieseries::hoverSignal()
296 // create a pie series
296 // create a pie series
297 QPieSeries *series = new QPieSeries();
297 QPieSeries *series = new QPieSeries();
298 series->setPieSize(1.0);
298 series->setPieSize(1.0);
299 QPieSlice *s1 = series->append(1, "slice 1");
299 QPieSlice *s1 = series->append("slice 1", 1);
300 series->append(2, "slice 2");
300 series->append("slice 2", 2);
301 series->append(3, "slice 3");
301 series->append("slice 3", 3);
302
302
303 // add series to the chart
303 // add series to the chart
304 QChartView view(new QChart());
304 QChartView view(new QChart());
@@ -88,7 +88,7 void tst_qpieslice::construction()
88 QCOMPARE(slice1.endAngle(), 0.0);
88 QCOMPARE(slice1.endAngle(), 0.0);
89
89
90 // value and label params
90 // value and label params
91 QPieSlice slice2(1.0, "foobar");
91 QPieSlice slice2("foobar", 1.0);
92 QCOMPARE(slice2.value(), 1.0);
92 QCOMPARE(slice2.value(), 1.0);
93 QCOMPARE(slice2.label(), QString("foobar"));
93 QCOMPARE(slice2.label(), QString("foobar"));
94 QVERIFY(!slice2.isLabelVisible());
94 QVERIFY(!slice2.isLabelVisible());
@@ -136,9 +136,9 void tst_qpieslice::customize()
136 {
136 {
137 // create a pie series
137 // create a pie series
138 QPieSeries *series = new QPieSeries();
138 QPieSeries *series = new QPieSeries();
139 QPieSlice *s1 = series->append(1, "slice 1");
139 QPieSlice *s1 = series->append("slice 1", 1);
140 QPieSlice *s2 = series->append(2, "slice 2");
140 QPieSlice *s2 = series->append("slice 2", 2);
141 series->append(3, "slice 3");
141 series->append("slice 3", 3);
142
142
143 // customize a slice
143 // customize a slice
144 QPen p1(Qt::red);
144 QPen p1(Qt::red);
@@ -171,14 +171,14 void tst_qpieslice::customize()
171 QCOMPARE(s1->labelFont(), f1);
171 QCOMPARE(s1->labelFont(), f1);
172
172
173 // add a slice
173 // add a slice
174 series->append(4, "slice 4");
174 series->append("slice 4", 4);
175 QCOMPARE(s1->pen(), p1);
175 QCOMPARE(s1->pen(), p1);
176 QCOMPARE(s1->brush(), b1);
176 QCOMPARE(s1->brush(), b1);
177 QCOMPARE(s1->labelPen(), p1);
177 QCOMPARE(s1->labelPen(), p1);
178 QCOMPARE(s1->labelFont(), f1);
178 QCOMPARE(s1->labelFont(), f1);
179
179
180 // insert a slice
180 // insert a slice
181 series->insert(0, new QPieSlice(5, "slice 5"));
181 series->insert(0, new QPieSlice("slice 5", 5));
182 QCOMPARE(s1->pen(), p1);
182 QCOMPARE(s1->pen(), p1);
183 QCOMPARE(s1->brush(), b1);
183 QCOMPARE(s1->brush(), b1);
184 QCOMPARE(s1->labelPen(), p1);
184 QCOMPARE(s1->labelPen(), p1);
@@ -198,9 +198,9 void tst_qpieslice::mouseClick()
198 // create a pie series
198 // create a pie series
199 QPieSeries *series = new QPieSeries();
199 QPieSeries *series = new QPieSeries();
200 series->setPieSize(1.0);
200 series->setPieSize(1.0);
201 QPieSlice *s1 = series->append(1, "slice 1");
201 QPieSlice *s1 = series->append("slice 1", 1);
202 QPieSlice *s2 = series->append(2, "slice 2");
202 QPieSlice *s2 = series->append("slice 2", 2);
203 QPieSlice *s3 = series->append(3, "slice 3");
203 QPieSlice *s3 = series->append("slice 3", 3);
204 QSignalSpy clickSpy1(s1, SIGNAL(clicked()));
204 QSignalSpy clickSpy1(s1, SIGNAL(clicked()));
205 QSignalSpy clickSpy2(s2, SIGNAL(clicked()));
205 QSignalSpy clickSpy2(s2, SIGNAL(clicked()));
206 QSignalSpy clickSpy3(s3, SIGNAL(clicked()));
206 QSignalSpy clickSpy3(s3, SIGNAL(clicked()));
@@ -231,9 +231,9 void tst_qpieslice::mouseHover()
231 // create a pie series
231 // create a pie series
232 QPieSeries *series = new QPieSeries();
232 QPieSeries *series = new QPieSeries();
233 series->setPieSize(1.0);
233 series->setPieSize(1.0);
234 QPieSlice *s1 = series->append(1, "slice 1");
234 QPieSlice *s1 = series->append("slice 1", 1);
235 series->append(2, "slice 2");
235 series->append("slice 2", 2);
236 series->append(3, "slice 3");
236 series->append("slice 3", 3);
237
237
238 // add series to the chart
238 // add series to the chart
239 QChartView view(new QChart());
239 QChartView view(new QChart());
@@ -261,7 +261,7 void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QS
261 QPieSeries *series = new QPieSeries();
261 QPieSeries *series = new QPieSeries();
262 QList<qreal> column = data.at(j);
262 QList<qreal> column = data.at(j);
263 for (int i(0); i < column.count(); i++)
263 for (int i(0); i < column.count(); i++)
264 series->append(column.at(i), labels.at(i));
264 series->append(labels.at(i), column.at(i));
265 m_chart->addSeries(series);
265 m_chart->addSeries(series);
266 }
266 }
267 } else if (seriesName == "Bar"
267 } else if (seriesName == "Bar"
General Comments 0
You need to be logged in to leave comments. Login now