##// END OF EJS Templates
Add ownerships takeover to chartaxis
Michal Klocek -
r248:ded845353666
parent child
Show More
@@ -1,334 +1,334
1 1 #include "chartdataset_p.h"
2 2 #include "qchartaxis.h"
3 3 //series
4 4 #include "qlinechartseries.h"
5 5 #include "qbarchartseries.h"
6 6 #include "qstackedbarchartseries.h"
7 7 #include "qpercentbarchartseries.h"
8 8 #include "qpieseries.h"
9 9 #include "qscatterseries.h"
10 10
11 11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 12
13 13 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
14 14 m_axisX(new QChartAxis(this)),
15 15 m_axisY(new QChartAxis(this)),
16 16 m_domainIndex(0),
17 17 m_axisXInitialized(false)
18 18 {
19 19 }
20 20
21 21 ChartDataSet::~ChartDataSet()
22 22 {
23 23 // TODO Auto-generated destructor stub
24 24 }
25 25
26 26 const Domain ChartDataSet::domain(QChartAxis *axisY) const
27 27 {
28 28 int i = m_domainMap.count(axisY);
29 29 if(i == 0){
30 30 return Domain();
31 31 }
32 32 i = i - m_domainIndex -1;
33 33 return m_domainMap.values(axisY).at(i);
34 34 }
35 35
36 36 void ChartDataSet::addSeries(QChartSeries* series, QChartAxis *axisY)
37 37 {
38 38 // TODO: we should check the series not already added
39 series->setParent(this); // take ownership
40 39
41 40 series->setParent(this); // take ownership
42 41 clearDomains();
43 42
44 43 if(axisY==0) axisY = m_axisY;
44 axisY->setParent(this); // take ownership
45 45
46 46 QList<QChartSeries*> seriesList = m_seriesMap.values(axisY);
47 47
48 48 QList<Domain> domainList = m_domainMap.values(axisY);
49 49
50 50 Q_ASSERT(domainList.size()<=1);
51 51
52 52 Domain domain;
53 53
54 54 if(domainList.size()>0) domain = domainList.at(0);
55 55
56 56 switch(series->type())
57 57 {
58 58 case QChartSeries::SeriesTypeLine: {
59 59
60 60 QLineChartSeries* xyseries = static_cast<QLineChartSeries*>(series);
61 61
62 62 for (int i = 0; i < xyseries->count(); i++)
63 63 {
64 64 qreal x = xyseries->x(i);
65 65 qreal y = xyseries->y(i);
66 66 domain.m_minX = qMin(domain.m_minX,x);
67 67 domain.m_minY = qMin(domain.m_minY,y);
68 68 domain.m_maxX = qMax(domain.m_maxX,x);
69 69 domain.m_maxY = qMax(domain.m_maxY,y);
70 70 }
71 71 break;
72 72 }
73 73 case QChartSeries::SeriesTypeBar: {
74 74 qDebug() << "QChartSeries::SeriesTypeBar";
75 75 QBarChartSeries* barSeries = static_cast<QBarChartSeries*>(series);
76 76 qreal x = barSeries->countCategories();
77 77 qreal y = barSeries->max();
78 78 domain.m_minX = qMin(domain.m_minX,x);
79 79 domain.m_minY = qMin(domain.m_minY,y);
80 80 domain.m_maxX = qMax(domain.m_maxX,x);
81 81 domain.m_maxY = qMax(domain.m_maxY,y);
82 82 break;
83 83 }
84 84 case QChartSeries::SeriesTypeStackedBar: {
85 85 qDebug() << "QChartSeries::SeriesTypeStackedBar";
86 86
87 87 QStackedBarChartSeries* stackedBarSeries = static_cast<QStackedBarChartSeries*>(series);
88 88 qreal x = stackedBarSeries->countCategories();
89 89 qreal y = stackedBarSeries->maxCategorySum();
90 90 domain.m_minX = qMin(domain.m_minX,x);
91 91 domain.m_minY = qMin(domain.m_minY,y);
92 92 domain.m_maxX = qMax(domain.m_maxX,x);
93 93 domain.m_maxY = qMax(domain.m_maxY,y);
94 94 break;
95 95 }
96 96 case QChartSeries::SeriesTypePercentBar: {
97 97 qDebug() << "QChartSeries::SeriesTypePercentBar";
98 98
99 99 QPercentBarChartSeries* percentBarSeries = static_cast<QPercentBarChartSeries*>(series);
100 100 qreal x = percentBarSeries->countCategories();
101 101 domain.m_minX = qMin(domain.m_minX,x);
102 102 domain.m_minY = 0;
103 103 domain.m_maxX = qMax(domain.m_maxX,x);
104 104 domain.m_maxY = 100;
105 105 break;
106 106 }
107 107
108 108 case QChartSeries::SeriesTypePie: {
109 109 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
110 110 // TODO: domain stuff
111 111 break;
112 112 }
113 113
114 114 case QChartSeries::SeriesTypeScatter: {
115 115 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
116 116 Q_ASSERT(scatterSeries);
117 117 foreach (QPointF point, scatterSeries->data()) {
118 118 domain.m_minX = qMin(domain.m_minX, point.x());
119 119 domain.m_maxX = qMax(domain.m_maxX, point.x());
120 120 domain.m_minY = qMin(domain.m_minY, point.y());
121 121 domain.m_maxY = qMax(domain.m_maxY, point.y());
122 122 }
123 123 break;
124 124 }
125 125
126 126 default: {
127 127 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
128 128 return;
129 129 break;
130 130 }
131 131
132 132 }
133 133
134 134 if(!m_domainMap.contains(axisY))
135 135 {
136 136 emit axisAdded(axisY);
137 137 QObject::connect(axisY,SIGNAL(minChanged(qreal)),this,SLOT(handleMinChanged(qreal)));
138 138 QObject::connect(axisY,SIGNAL(maxChanged(qreal)),this,SLOT(handleMaxChanged(qreal)));
139 139 QObject::connect(axisY,SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*)));
140 140 }
141 141 m_domainMap.replace(axisY,domain);
142 142 m_seriesMap.insert(axisY,series);
143 143
144 144 if(!m_axisXInitialized)
145 145 {
146 146 emit axisAdded(axisX());
147 147 QObject::connect(axisX(),SIGNAL(minChanged(qreal)),this,SLOT(handleMinChanged(qreal)));
148 148 QObject::connect(axisX(),SIGNAL(maxChanged(qreal)),this,SLOT(handleMaxChanged(qreal)));
149 149 QObject::connect(axisX(),SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*)));
150 150 m_axisXInitialized=true;
151 151 }
152 152
153 153
154 154 emit seriesAdded(series);
155 155 QStringList ylabels = createLabels(axisY,domain.m_minY,domain.m_maxY);
156 156 QStringList xlabels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
157 157 emit axisLabelsChanged(axisY,ylabels);
158 158 emit axisLabelsChanged(axisX(),xlabels);
159 159 emit seriesDomainChanged(series,domain);
160 160
161 161 }
162 162
163 163 void ChartDataSet::removeSeries(QChartSeries* series)
164 164 {
165 165 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
166 166 foreach(QChartAxis* axis , keys) {
167 167 if(m_seriesMap.contains(axis,series)){
168 168 emit seriesRemoved(series);
169 169 m_seriesMap.remove(axis,series);
170 170 //remove axis if no longer there
171 171 if(!m_seriesMap.contains(axis) && axis != m_axisY){
172 172 emit axisRemoved(axis);
173 173 m_domainMap.remove(axis);
174 174 delete axis;
175 175 }
176 176 break;
177 177 }
178 178 }
179 179 }
180 180
181 181 bool ChartDataSet::nextDomain()
182 182 {
183 183 int limit = (m_domainMap.values().size()/m_domainMap.uniqueKeys().size())-1;
184 184
185 185 if (m_domainIndex < limit) {
186 186 m_domainIndex++;
187 187 setDomain(m_domainIndex);
188 188 return true;
189 189 }
190 190 else {
191 191 return false;
192 192 }
193 193 }
194 194
195 195 bool ChartDataSet::previousDomain()
196 196 {
197 197 if (m_domainIndex > 0) {
198 198 m_domainIndex--;
199 199 setDomain(m_domainIndex);
200 200 return true;
201 201 }
202 202 else {
203 203 return false;
204 204 }
205 205 }
206 206
207 207 void ChartDataSet::setDomain(int index)
208 208 {
209 209 QList<QChartAxis*> domainList = m_domainMap.uniqueKeys();
210 210 foreach (QChartAxis* axis , domainList) {
211 211 int i = m_domainMap.count(axis) - index -1;
212 212 Q_ASSERT(i>=0);
213 213 Domain domain = m_domainMap.values(axis).at(i);
214 214 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
215 215 QList<QChartSeries*> seriesList = m_seriesMap.values(axis);
216 216 foreach(QChartSeries* series, seriesList) {
217 217 emit seriesDomainChanged(series,domain);
218 218 }
219 219 emit axisLabelsChanged(axis,labels);
220 220 }
221 221
222 222 Domain domain = m_domainMap.value(axisY());
223 223 QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
224 224 emit axisLabelsChanged(axisX(),labels);
225 225 }
226 226
227 227 void ChartDataSet::clearDomains(int toIndex)
228 228 {
229 229 Q_ASSERT(toIndex>=0);
230 230
231 231 m_domainIndex = toIndex;
232 232
233 233 QList<QChartAxis*> keys = m_domainMap.uniqueKeys();
234 234
235 235 foreach (QChartAxis* key , keys)
236 236 {
237 237 QList<Domain> domains = m_domainMap.values(key);
238 238 m_domainMap.remove(key);
239 239 int i = domains.size() - toIndex - 1;
240 240 while(i--){
241 241 domains.removeFirst();
242 242 }
243 243 for(int j=domains.size()-1; j>=0 ;j--)
244 244 m_domainMap.insert(key,domains.at(j));
245 245 }
246 246 }
247 247
248 248 void ChartDataSet::addDomain(const QRectF& rect, const QRectF& viewport)
249 249 {
250 250 Q_ASSERT(rect.isValid());
251 251 Q_ASSERT(viewport.isValid());
252 252
253 253 clearDomains(m_domainIndex);
254 254
255 255 QList<QChartAxis*> domainList = m_domainMap.uniqueKeys();
256 256
257 257 foreach (QChartAxis* axis , domainList){
258 258 Domain domain(m_domainMap.value(axis).subDomain(rect,viewport.width(),viewport.height()));
259 259 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
260 260 QList<QChartSeries*> seriesList = m_seriesMap.values(axis);
261 261 foreach(QChartSeries* series, seriesList){
262 262 emit seriesDomainChanged(series,domain);
263 263 }
264 264 emit axisLabelsChanged(axis,labels);
265 265 m_domainMap.insert(axis,domain);
266 266 }
267 267
268 268 Domain domain = m_domainMap.value(axisY());
269 269 QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
270 270 emit axisLabelsChanged(axisX(),labels);
271 271
272 272 m_domainIndex++;
273 273 }
274 274
275 275 QChartAxis* ChartDataSet::axisY(QChartSeries* series) const
276 276 {
277 277 if(series == 0) return m_axisY;
278 278
279 279 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
280 280
281 281 foreach(QChartAxis* axis , keys) {
282 282 if(m_seriesMap.contains(axis,series)){
283 283 return axis;
284 284 }
285 285 }
286 286 return 0;
287 287 }
288 288
289 289 QStringList ChartDataSet::createLabels(QChartAxis* axis,qreal min, qreal max)
290 290 {
291 291 Q_ASSERT(max>=min);
292 292
293 293 QStringList labels;
294 294
295 295 int ticks = axis->ticksCount()-1;
296 296
297 297 for(int i=0; i<= ticks; i++){
298 298 qreal value = min + (i * (max - min)/ ticks);
299 299 QString label = axis->axisTickLabel(value);
300 300 if(label.isEmpty()){
301 301 labels << QString::number(value);
302 302 }else{
303 303 labels << label;
304 304 }
305 305 }
306 306 return labels;
307 307 }
308 308
309 309
310 310 void ChartDataSet::handleMinChanged(qreal min)
311 311 {
312 312
313 313 }
314 314
315 315 void ChartDataSet::handleMaxChanged(qreal max)
316 316 {
317 317
318 318 }
319 319
320 320 void ChartDataSet::handleTickChanged(QChartAxis* axis)
321 321 {
322 322 Domain domain = m_domainMap.value(axisY());
323 323 if(axis==axisX()){
324 324 QStringList labels = createLabels(axis,domain.m_minX,domain.m_maxX);
325 325 emit axisLabelsChanged(axis,labels);
326 326 }else{
327 327 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
328 328 emit axisLabelsChanged(axis,labels);
329 329 }
330 330 }
331 331
332 332 #include "moc_chartdataset_p.cpp"
333 333
334 334 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now