##// END OF EJS Templates
Reserve ordering of chart series internally....
Tero Ahola -
r825:d850de289019
parent child
Show More
@@ -48,9 +48,7 void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY)
48 {
48 {
49 if(axisY==0) axisY = m_axisY;
49 if(axisY==0) axisY = m_axisY;
50
50
51 QChartAxis* axis = m_seriesAxisMap.value(series);
51 if (seriesIndex(series) > -1) {
52
53 if(axis) {
54 qWarning() << "Can not add series. Series already on the chart";
52 qWarning() << "Can not add series. Series already on the chart";
55 return;
53 return;
56 }
54 }
@@ -64,8 +62,7 void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY)
64 }
62 }
65
63
66 Domain* domain = m_axisDomainMap.value(axisY);
64 Domain* domain = m_axisDomainMap.value(axisY);
67
65 if (!domain) {
68 if(!domain) {
69 domain = new Domain(axisY);
66 domain = new Domain(axisY);
70 QObject::connect(axisY,SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool)));
67 QObject::connect(axisY,SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool)));
71 QObject::connect(axisX(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool)));
68 QObject::connect(axisX(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool)));
@@ -76,70 +73,75 void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY)
76 emit axisAdded(axisY,domain);
73 emit axisAdded(axisY,domain);
77 }
74 }
78
75
79 if(!m_axisXInitialized){
76 if (!m_axisXInitialized) {
80 emit axisAdded(axisX(),domain);
77 emit axisAdded(axisX(), domain);
81 m_axisXInitialized=true;
78 m_axisXInitialized = true;
82 }
79 }
83
80
84 calculateDomain(series,domain);
81 calculateDomain(series, domain);
85
82
86 m_seriesAxisMap.insert(series,axisY);
83 m_seriesAxisList.append(QPair<QSeries*, QChartAxis*>(series, axisY));
87 emit seriesAdded(series,domain);
88
84
85 emit seriesAdded(series, domain);
89 }
86 }
90
87
91 void ChartDataSet::removeSeries(QSeries* series)
88 void ChartDataSet::removeSeries(QSeries* series)
92 {
89 {
93
90 int index = seriesIndex(series);
94 QChartAxis* axis = m_seriesAxisMap.value(series);
91 if (!index < 0) {
95
92 qWarning() << "Can not remove series. Series not found on the chart.";
96 if(!axis){
97 qWarning()<<"Can not remove series. Series not found on the chart.";
98 return;
93 return;
99 }
94 }
100 emit seriesRemoved(series);
101 m_seriesAxisMap.remove(series);
102
95
103 if(series->parent()==this){
96 // Remove the series and the axis from the container
97 QChartAxis* axis = m_seriesAxisList.at(index).second;
98 m_seriesAxisList.removeAt(index);
99
100 // Delete the series
101 emit seriesRemoved(series);
102 if (series->parent() == this) {
104 delete series;
103 delete series;
105 series=0;
104 series = 0;
106 }
105 }
107
106
108 QList<QChartAxis*> axes = m_seriesAxisMap.values();
107 // Check if the Y axis is still in use
109
108 bool yAxisInUse(false);
110 int i = axes.indexOf(axis);
109 for (int i(0); i < m_seriesAxisList.count(); i++) {
110 QPair<QSeries *, QChartAxis *> pair = m_seriesAxisList.at(i);
111 if (pair.second == axis)
112 yAxisInUse = true;
113 }
111
114
112 if(i==-1){
115 // Remove the Y axis if not in use
116 if (!yAxisInUse) {
113 Domain* domain = m_axisDomainMap.take(axis);
117 Domain* domain = m_axisDomainMap.take(axis);
114 emit axisRemoved(axis);
118 emit axisRemoved(axis);
115 if(axis!=axisY()){
119 if (axis != axisY()) {
116 if(axis->parent()==this){
120 // Delete the Y axis unless it is the default one
121 if (axis->parent() == this) {
117 delete axis;
122 delete axis;
118 axis=0;
123 axis = 0;
119 }
124 }
120 }
125 }
121 delete domain;
126 delete domain;
122 }
127 }
123
128
124 if(m_seriesAxisMap.values().size()==0)
129 // Remove the x axis in case there are no y-axes left
125 {
130 if (m_seriesAxisList.count() == 0) {
126 m_axisXInitialized=false;
131 m_axisXInitialized = false;
127 emit axisRemoved(axisX());
132 emit axisRemoved(axisX());
128 }
133 }
129 }
134 }
130
135
131 void ChartDataSet::removeAllSeries()
136 void ChartDataSet::removeAllSeries()
132 {
137 {
133
138 while (m_seriesAxisList.count()) {
134 QList<QSeries*> series = m_seriesAxisMap.keys();
139 QPair<QSeries *, QChartAxis *> pair = m_seriesAxisList.last();
135
140 removeSeries(pair.first);
136 foreach(QSeries* s , series) {
137 removeSeries(s);
138 }
141 }
139
142
140 Q_ASSERT(m_seriesAxisMap.count()==0);
143 Q_ASSERT(m_seriesAxisList.count() == 0);
141 Q_ASSERT(m_axisDomainMap.count()==0);
144 Q_ASSERT(m_axisDomainMap.count() == 0);
142
143 }
145 }
144
146
145 //to be removed with PIMPL
147 //to be removed with PIMPL
@@ -282,43 +284,30 void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size)
282 }
284 }
283 }
285 }
284
286
285 int ChartDataSet::seriesCount(QSeries::QSeriesType type)
287 int ChartDataSet::seriesIndex(QSeries *series) const
286 {
287 int count=0;
288 QMapIterator<QSeries*, QChartAxis*> i(m_seriesAxisMap);
289 while (i.hasNext()) {
290 i.next();
291 if(i.key()->type()==type) count++;
292 }
293 return count;
294 }
295
296 int ChartDataSet::seriesIndex(QSeries *series)
297 {
288 {
298 int count=-1;
289 for (int i(0); i < m_seriesAxisList.count(); i++) {
299 QMapIterator<QSeries*, QChartAxis*> i(m_seriesAxisMap);
290 QPair<QSeries *, QChartAxis *> pair = m_seriesAxisList.at(i);
300 while (i.hasNext()) {
291 if (pair.first == series)
301 i.next();
292 return i;
302 count++;
303 if (i.key() == series)
304 return count;
305 }
293 }
306 return count;
294 return -1;
307 }
295 }
308
296
309 QChartAxis* ChartDataSet::axisY(QSeries* series) const
297 QChartAxis* ChartDataSet::axisY(QSeries* series) const
310 {
298 {
311 if(series == 0) return m_axisY;
299 if (series == 0)
312 return m_seriesAxisMap.value(series);
300 return m_axisY;
301
302 return m_seriesAxisList.at(seriesIndex(series)).second;
313 }
303 }
314
304
315 Domain* ChartDataSet::domain(QSeries* series) const
305 Domain* ChartDataSet::domain(QSeries* series) const
316 {
306 {
317 QChartAxis* axis = m_seriesAxisMap.value(series);
307 QChartAxis* axis = m_seriesAxisList.at(seriesIndex(series)).second;
318 if(axis){
308 if (axis)
319 return m_axisDomainMap.value(axis);
309 return m_axisDomainMap.value(axis);
320 }else
310 return 0;
321 return 0;
322 }
311 }
323
312
324 Domain* ChartDataSet::domain(QChartAxis* axis) const
313 Domain* ChartDataSet::domain(QChartAxis* axis) const
@@ -54,8 +54,7 public:
54 void zoomOutDomain(const QRectF& rect, const QSizeF& size);
54 void zoomOutDomain(const QRectF& rect, const QSizeF& size);
55 void scrollDomain(int dx,int dy,const QSizeF& size);
55 void scrollDomain(int dx,int dy,const QSizeF& size);
56
56
57 int seriesCount(QSeries::QSeriesType type);
57 int seriesIndex(QSeries *series) const;
58 int seriesIndex(QSeries *series);
59
58
60 Domain* domain(QSeries* series) const;
59 Domain* domain(QSeries* series) const;
61 Domain* domain(QChartAxis* axis) const;
60 Domain* domain(QChartAxis* axis) const;
@@ -75,7 +74,7 private:
75 void setupCategories(QBarSeries* series);
74 void setupCategories(QBarSeries* series);
76
75
77 private:
76 private:
78 QMap<QSeries*, QChartAxis*> m_seriesAxisMap;
77 QList<QPair<QSeries*, QChartAxis*> > m_seriesAxisList;
79 QMap<QChartAxis*, Domain*> m_axisDomainMap;
78 QMap<QChartAxis*, Domain*> m_axisDomainMap;
80 QChartAxis* m_axisX;
79 QChartAxis* m_axisX;
81 QChartAxis* m_axisY;
80 QChartAxis* m_axisY;
@@ -52,8 +52,6 private Q_SLOTS:
52 void removeAllSeries();
52 void removeAllSeries();
53 void axisY_data();
53 void axisY_data();
54 void axisY();
54 void axisY();
55 void seriesCount_data();
56 void seriesCount();
57 void seriesIndex_data();
55 void seriesIndex_data();
58 void seriesIndex();
56 void seriesIndex();
59 void domain_data();
57 void domain_data();
@@ -261,40 +259,6 void tst_ChartDataSet::axisY()
261
259
262 }
260 }
263
261
264 void tst_ChartDataSet::seriesCount_data()
265 {
266 addSeries_data();
267 }
268
269 void tst_ChartDataSet::seriesCount()
270 {
271 QFETCH(QLineSeries*, series0);
272 QFETCH(QChartAxis*, axis0);
273 QFETCH(QLineSeries*, series1);
274 QFETCH(QChartAxis*, axis1);
275 QFETCH(QLineSeries*, series2);
276 QFETCH(QChartAxis*, axis2);
277 QFETCH(int, axisCount);
278 Q_UNUSED(axisCount);
279
280 ChartDataSet dataSet;
281
282 dataSet.addSeries(series0, axis0);
283 dataSet.addSeries(series1, axis1);
284 dataSet.addSeries(series2, axis2);
285
286 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*,Domain*)));
287 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
288 QSignalSpy spy2(&dataSet, SIGNAL(seriesAdded(QSeries*,Domain*)));
289 QSignalSpy spy3(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
290
291 QCOMPARE(dataSet.seriesCount(series0->type()),3);
292 QCOMPARE(spy0.count(), 0);
293 QCOMPARE(spy1.count(), 0);
294 QCOMPARE(spy2.count(), 0);
295 QCOMPARE(spy3.count(), 0);
296 }
297
298 void tst_ChartDataSet::seriesIndex_data()
262 void tst_ChartDataSet::seriesIndex_data()
299 {
263 {
300 addSeries_data();
264 addSeries_data();
@@ -210,6 +210,7 void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QS
210 for (int j(0); j < data.count(); j ++) {
210 for (int j(0); j < data.count(); j ++) {
211 QList<qreal> column = data.at(j);
211 QList<qreal> column = data.at(j);
212 QLineSeries *series = new QLineSeries();
212 QLineSeries *series = new QLineSeries();
213 series->setName("line" + QString::number(j));
213 for (int i(0); i < column.count(); i++)
214 for (int i(0); i < column.count(); i++)
214 series->append(i, column.at(i));
215 series->append(i, column.at(i));
215 m_chart->addSeries(series);
216 m_chart->addSeries(series);
@@ -222,12 +223,14 void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QS
222 for (int i(0); i < column.count(); i++)
223 for (int i(0); i < column.count(); i++)
223 lineSeries->append(i, column.at(i));
224 lineSeries->append(i, column.at(i));
224 QAreaSeries *areaSeries = new QAreaSeries(lineSeries);
225 QAreaSeries *areaSeries = new QAreaSeries(lineSeries);
226 areaSeries->setName("area" + QString::number(j));
225 m_chart->addSeries(areaSeries);
227 m_chart->addSeries(areaSeries);
226 }
228 }
227 } else if (seriesName == "Scatter") {
229 } else if (seriesName == "Scatter") {
228 for (int j(0); j < data.count(); j++) {
230 for (int j(0); j < data.count(); j++) {
229 QList<qreal> column = data.at(j);
231 QList<qreal> column = data.at(j);
230 QScatterSeries *series = new QScatterSeries();
232 QScatterSeries *series = new QScatterSeries();
233 series->setName("scatter" + QString::number(j));
231 for (int i(0); i < column.count(); i++)
234 for (int i(0); i < column.count(); i++)
232 series->append(i, column.at(i));
235 series->append(i, column.at(i));
233 m_chart->addSeries(series);
236 m_chart->addSeries(series);
General Comments 0
You need to be logged in to leave comments. Login now