##// END OF EJS Templates
Adds missing ticks hadnling
Michal Klocek -
r554:7777ba21b3a6
parent child
Show More
@@ -1,413 +1,407
1 #include "axisitem_p.h"
1 #include "axisitem_p.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include "chartanimator_p.h"
4 #include "chartanimator_p.h"
5 #include <QPainter>
5 #include <QPainter>
6 #include <QDebug>
6 #include <QDebug>
7
7
8 static int label_padding = 5;
8 static int label_padding = 5;
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 AxisItem::AxisItem(QChartAxis* axis,ChartPresenter* presenter,AxisType type,QGraphicsItem* parent) :
12 AxisItem::AxisItem(QChartAxis* axis,ChartPresenter* presenter,AxisType type,QGraphicsItem* parent) :
13 ChartItem(parent),
13 ChartItem(parent),
14 m_presenter(presenter),
14 m_presenter(presenter),
15 m_chartAxis(axis),
15 m_chartAxis(axis),
16 m_type(type),
16 m_type(type),
17 m_labelsAngle(0),
17 m_labelsAngle(0),
18 m_grid(parent),
18 m_grid(parent),
19 m_shades(parent),
19 m_shades(parent),
20 m_labels(parent),
20 m_labels(parent),
21 m_axis(parent),
21 m_axis(parent),
22 m_min(0),
22 m_min(0),
23 m_max(0),
23 m_max(0),
24 m_ticksCount(0)
24 m_ticksCount(0)
25 {
25 {
26 //initial initialization
26 //initial initialization
27 m_axis.setZValue(ChartPresenter::AxisZValue);
27 m_axis.setZValue(ChartPresenter::AxisZValue);
28 m_shades.setZValue(ChartPresenter::ShadesZValue);
28 m_shades.setZValue(ChartPresenter::ShadesZValue);
29 m_grid.setZValue(ChartPresenter::GridZValue);
29 m_grid.setZValue(ChartPresenter::GridZValue);
30 setFlags(QGraphicsItem::ItemHasNoContents);
30 setFlags(QGraphicsItem::ItemHasNoContents);
31
31
32 QObject::connect(m_chartAxis,SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
32 QObject::connect(m_chartAxis,SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
33 QObject::connect(m_chartAxis->categories(),SIGNAL(updated()),this,SLOT(handleAxisCategoriesUpdated()));
33 QObject::connect(m_chartAxis->categories(),SIGNAL(updated()),this,SLOT(handleAxisCategoriesUpdated()));
34
34
35 handleAxisUpdated();
35 handleAxisUpdated();
36 }
36 }
37
37
38 AxisItem::~AxisItem()
38 AxisItem::~AxisItem()
39 {
39 {
40 }
40 }
41
41
42 QRectF AxisItem::boundingRect() const
42 QRectF AxisItem::boundingRect() const
43 {
43 {
44 return QRectF();
44 return QRectF();
45 }
45 }
46
46
47 void AxisItem::createItems(int count)
47 void AxisItem::createItems(int count)
48 {
48 {
49
49
50 if(m_axis.children().size()==0)
50 if(m_axis.children().size()==0)
51 m_axis.addToGroup(new QGraphicsLineItem());
51 m_axis.addToGroup(new QGraphicsLineItem());
52 for (int i = 0; i < count; ++i) {
52 for (int i = 0; i < count; ++i) {
53 m_grid.addToGroup(new QGraphicsLineItem());
53 m_grid.addToGroup(new QGraphicsLineItem());
54 m_labels.addToGroup(new QGraphicsSimpleTextItem());
54 m_labels.addToGroup(new QGraphicsSimpleTextItem());
55 m_axis.addToGroup(new QGraphicsLineItem());
55 m_axis.addToGroup(new QGraphicsLineItem());
56 if((m_grid.childItems().size())%2 && m_grid.childItems().size()>2) m_shades.addToGroup(new QGraphicsRectItem());
56 if((m_grid.childItems().size())%2 && m_grid.childItems().size()>2) m_shades.addToGroup(new QGraphicsRectItem());
57 }
57 }
58 }
58 }
59
59
60 void AxisItem::deleteItems(int count)
60 void AxisItem::deleteItems(int count)
61 {
61 {
62 QList<QGraphicsItem *> lines = m_grid.childItems();
62 QList<QGraphicsItem *> lines = m_grid.childItems();
63 QList<QGraphicsItem *> labels = m_labels.childItems();
63 QList<QGraphicsItem *> labels = m_labels.childItems();
64 QList<QGraphicsItem *> shades = m_shades.childItems();
64 QList<QGraphicsItem *> shades = m_shades.childItems();
65 QList<QGraphicsItem *> axis = m_axis.childItems();
65 QList<QGraphicsItem *> axis = m_axis.childItems();
66
66
67 for (int i = 0; i < count; ++i) {
67 for (int i = 0; i < count; ++i) {
68 if(lines.size()%2 && lines.size()>1) delete(shades.takeLast());
68 if(lines.size()%2 && lines.size()>1) delete(shades.takeLast());
69 delete(lines.takeLast());
69 delete(lines.takeLast());
70 delete(labels.takeLast());
70 delete(labels.takeLast());
71 delete(axis.takeLast());
71 delete(axis.takeLast());
72 }
72 }
73 }
73 }
74
74
75 void AxisItem::updateLayout(QVector<qreal>& layout)
75 void AxisItem::updateLayout(QVector<qreal>& layout)
76 {
76 {
77 if(m_animator){
77 if(m_animator){
78 m_animator->applyLayout(this,layout);
78 m_animator->applyLayout(this,layout);
79 }
79 }
80 else setLayout(layout);
80 else setLayout(layout);
81 }
81 }
82
82
83 QStringList AxisItem::createLabels(int ticks, qreal min, qreal max) const
83 QStringList AxisItem::createLabels(int ticks, qreal min, qreal max) const
84 {
84 {
85 Q_ASSERT(max>=min);
85 Q_ASSERT(max>=min);
86 Q_ASSERT(ticks>0);
86 Q_ASSERT(ticks>0);
87
87
88 QStringList labels;
88 QStringList labels;
89
89
90 QChartAxisCategories* categories = m_chartAxis->categories();
90 QChartAxisCategories* categories = m_chartAxis->categories();
91
91
92 for(int i=0; i< ticks; i++) {
92 for(int i=0; i< ticks; i++) {
93 qreal value = min + (i * (max - min)/ (ticks-1));
93 qreal value = min + (i * (max - min)/ (ticks-1));
94 if(categories->count()==0) {
94 if(categories->count()==0) {
95 labels << QString::number(value);
95 labels << QString::number(value);
96 }
96 }
97 else {
97 else {
98
98 QString label = categories->label(value);
99 QString label = categories->label(value);
99 labels << label;
100 labels << label;
100 }
101 }
101 }
102 }
102 return labels;
103 return labels;
103 }
104 }
104
105
105 void AxisItem::setAxisOpacity(qreal opacity)
106 void AxisItem::setAxisOpacity(qreal opacity)
106 {
107 {
107 m_axis.setOpacity(opacity);
108 m_axis.setOpacity(opacity);
108 }
109 }
109
110
110 qreal AxisItem::axisOpacity() const
111 qreal AxisItem::axisOpacity() const
111 {
112 {
112 return m_axis.opacity();
113 return m_axis.opacity();
113 }
114 }
114
115
115 void AxisItem::setGridOpacity(qreal opacity)
116 void AxisItem::setGridOpacity(qreal opacity)
116 {
117 {
117 m_grid.setOpacity(opacity);
118 m_grid.setOpacity(opacity);
118 }
119 }
119
120
120 qreal AxisItem::gridOpacity() const
121 qreal AxisItem::gridOpacity() const
121 {
122 {
122 return m_grid.opacity();
123 return m_grid.opacity();
123 }
124 }
124
125
125 void AxisItem::setLabelsOpacity(qreal opacity)
126 void AxisItem::setLabelsOpacity(qreal opacity)
126 {
127 {
127 m_labels.setOpacity(opacity);
128 m_labels.setOpacity(opacity);
128 }
129 }
129
130
130 qreal AxisItem::labelsOpacity() const
131 qreal AxisItem::labelsOpacity() const
131 {
132 {
132 return m_labels.opacity();
133 return m_labels.opacity();
133 }
134 }
134
135
135 void AxisItem::setShadesOpacity(qreal opacity)
136 void AxisItem::setShadesOpacity(qreal opacity)
136 {
137 {
137 m_shades.setOpacity(opacity);
138 m_shades.setOpacity(opacity);
138 }
139 }
139
140
140 qreal AxisItem::shadesOpacity() const
141 qreal AxisItem::shadesOpacity() const
141 {
142 {
142 return m_shades.opacity();
143 return m_shades.opacity();
143 }
144 }
144
145
145 void AxisItem::setLabelsAngle(int angle)
146 void AxisItem::setLabelsAngle(int angle)
146 {
147 {
147 foreach(QGraphicsItem* item , m_labels.childItems()) {
148 foreach(QGraphicsItem* item , m_labels.childItems()) {
148 QPointF center = item->boundingRect().center();
149 QPointF center = item->boundingRect().center();
149 item->setRotation(angle);
150 item->setRotation(angle);
150 }
151 }
151
152
152 m_labelsAngle=angle;
153 m_labelsAngle=angle;
153 }
154 }
154
155
155 void AxisItem::setLabelsPen(const QPen& pen)
156 void AxisItem::setLabelsPen(const QPen& pen)
156 {
157 {
157 foreach(QGraphicsItem* item , m_labels.childItems()) {
158 foreach(QGraphicsItem* item , m_labels.childItems()) {
158 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
159 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
159 }
160 }
160 }
161 }
161
162
162 void AxisItem::setLabelsBrush(const QBrush& brush)
163 void AxisItem::setLabelsBrush(const QBrush& brush)
163 {
164 {
164 foreach(QGraphicsItem* item , m_labels.childItems()) {
165 foreach(QGraphicsItem* item , m_labels.childItems()) {
165 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
166 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
166 }
167 }
167 }
168 }
168
169
169 void AxisItem::setLabelsFont(const QFont& font)
170 void AxisItem::setLabelsFont(const QFont& font)
170 {
171 {
171 foreach(QGraphicsItem* item , m_labels.childItems()) {
172 foreach(QGraphicsItem* item , m_labels.childItems()) {
172 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
173 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
173 }
174 }
174 }
175 }
175
176
176 void AxisItem::setShadesBrush(const QBrush& brush)
177 void AxisItem::setShadesBrush(const QBrush& brush)
177 {
178 {
178 foreach(QGraphicsItem* item , m_shades.childItems()) {
179 foreach(QGraphicsItem* item , m_shades.childItems()) {
179 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
180 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
180 }
181 }
181 }
182 }
182
183
183 void AxisItem::setShadesPen(const QPen& pen)
184 void AxisItem::setShadesPen(const QPen& pen)
184 {
185 {
185 foreach(QGraphicsItem* item , m_shades.childItems()) {
186 foreach(QGraphicsItem* item , m_shades.childItems()) {
186 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
187 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
187 }
188 }
188 }
189 }
189
190
190 void AxisItem::setAxisPen(const QPen& pen)
191 void AxisItem::setAxisPen(const QPen& pen)
191 {
192 {
192 foreach(QGraphicsItem* item , m_axis.childItems()) {
193 foreach(QGraphicsItem* item , m_axis.childItems()) {
193 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
194 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
194 }
195 }
195 }
196 }
196
197
197 void AxisItem::setGridPen(const QPen& pen)
198 void AxisItem::setGridPen(const QPen& pen)
198 {
199 {
199 foreach(QGraphicsItem* item , m_grid.childItems()) {
200 foreach(QGraphicsItem* item , m_grid.childItems()) {
200 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
201 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
201 }
202 }
202 }
203 }
203
204
204 QVector<qreal> AxisItem::calculateLayout() const
205 QVector<qreal> AxisItem::calculateLayout() const
205 {
206 {
206 Q_ASSERT(m_ticksCount>=2);
207 Q_ASSERT(m_ticksCount>=2);
207
208
208 QVector<qreal> points;
209 QVector<qreal> points;
209 points.resize(m_ticksCount);
210 points.resize(m_ticksCount);
210
211
211 switch (m_type)
212 switch (m_type)
212 {
213 {
213 case X_AXIS:
214 case X_AXIS:
214 {
215 {
215 const qreal deltaX = m_rect.width()/(m_ticksCount-1);
216 const qreal deltaX = m_rect.width()/(m_ticksCount-1);
216 for (int i = 0; i < m_ticksCount; ++i) {
217 for (int i = 0; i < m_ticksCount; ++i) {
217 int x = i * deltaX + m_rect.left();
218 int x = i * deltaX + m_rect.left();
218 points[i] = x;
219 points[i] = x;
219 }
220 }
220 }
221 }
221 break;
222 break;
222 case Y_AXIS:
223 case Y_AXIS:
223 {
224 {
224 const qreal deltaY = m_rect.height()/(m_ticksCount-1);
225 const qreal deltaY = m_rect.height()/(m_ticksCount-1);
225 for (int i = 0; i < m_ticksCount; ++i) {
226 for (int i = 0; i < m_ticksCount; ++i) {
226 int y = i * -deltaY + m_rect.bottom();
227 int y = i * -deltaY + m_rect.bottom();
227 points[i] = y;
228 points[i] = y;
228 }
229 }
229 }
230 }
230 break;
231 break;
231 }
232 }
232 return points;
233 return points;
233 }
234 }
234
235
235 void AxisItem::setLayout(QVector<qreal>& layout)
236 void AxisItem::setLayout(QVector<qreal>& layout)
236 {
237 {
237 int diff = m_layoutVector.size() - layout.size();
238 int diff = m_layoutVector.size() - layout.size();
238
239
239 if(diff>0) {
240 if(diff>0) {
240 deleteItems(diff);
241 deleteItems(diff);
241 }
242 }
242 else if(diff<0) {
243 else if(diff<0) {
243 createItems(-diff);
244 createItems(-diff);
244 }
245 }
245
246
246 if(diff!=0) handleAxisUpdated();
247 if(diff!=0) handleAxisUpdated();
247
248
248 QStringList ticksList = createLabels(layout.size(),m_min,m_max);
249 QStringList ticksList = createLabels(layout.size(),m_min,m_max);
249
250
250 QList<QGraphicsItem *> lines = m_grid.childItems();
251 QList<QGraphicsItem *> lines = m_grid.childItems();
251 QList<QGraphicsItem *> labels = m_labels.childItems();
252 QList<QGraphicsItem *> labels = m_labels.childItems();
252 QList<QGraphicsItem *> shades = m_shades.childItems();
253 QList<QGraphicsItem *> shades = m_shades.childItems();
253 QList<QGraphicsItem *> axis = m_axis.childItems();
254 QList<QGraphicsItem *> axis = m_axis.childItems();
254
255
255 Q_ASSERT(labels.size() == ticksList.size());
256 Q_ASSERT(labels.size() == ticksList.size());
256 Q_ASSERT(layout.size() == ticksList.size());
257 Q_ASSERT(layout.size() == ticksList.size());
257
258
258 switch (m_type)
259 switch (m_type)
259 {
260 {
260 case X_AXIS:
261 case X_AXIS:
261 {
262 {
262 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
263 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
263 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
264 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
264
265
265 for (int i = 0; i < layout.size(); ++i) {
266 for (int i = 0; i < layout.size(); ++i) {
266 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
267 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
267 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
268 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
268 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
269 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
269 labelItem->setText(ticksList.at(i));
270 labelItem->setText(ticksList.at(i));
270 QPointF center = labelItem->boundingRect().center();
271 QPointF center = labelItem->boundingRect().center();
271 labelItem->setTransformOriginPoint(center.x(), center.y());
272 labelItem->setTransformOriginPoint(center.x(), center.y());
272 labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding);
273 labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding);
273 if((i+1)%2 && i>1) {
274 if((i+1)%2 && i>1) {
274 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
275 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
275 rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height());
276 rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height());
276 }
277 }
277 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
278 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
278 lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5);
279 lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5);
279 }
280 }
280 }
281 }
281 break;
282 break;
282
283
283 case Y_AXIS:
284 case Y_AXIS:
284 {
285 {
285 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
286 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
286 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
287 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
287
288
288 for (int i = 0; i < layout.size(); ++i) {
289 for (int i = 0; i < layout.size(); ++i) {
289 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
290 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
290 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
291 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
291 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
292 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
292 labelItem->setText(ticksList.at(i));
293 labelItem->setText(ticksList.at(i));
293 QPointF center = labelItem->boundingRect().center();
294 QPointF center = labelItem->boundingRect().center();
294 labelItem->setTransformOriginPoint(center.x(), center.y());
295 labelItem->setTransformOriginPoint(center.x(), center.y());
295 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i]-center.y());
296 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i]-center.y());
296 if((i+1)%2 && i>1) {
297 if((i+1)%2 && i>1) {
297 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
298 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
298 rectItem->setRect(m_rect.left(),layout[i-1],m_rect.width(),layout[i-1]-layout[i]);
299 rectItem->setRect(m_rect.left(),layout[i-1],m_rect.width(),layout[i-1]-layout[i]);
299 }
300 }
300 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
301 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
301 lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]);
302 lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]);
302 }
303 }
303 }
304 }
304 break;
305 break;
305 default:
306 default:
306 qDebug()<<"Unknown axis type";
307 qDebug()<<"Unknown axis type";
307 break;
308 break;
308 }
309 }
309
310
310 m_layoutVector=layout;
311 m_layoutVector=layout;
311 }
312 }
312
313
313 bool AxisItem::isEmpty()
314 bool AxisItem::isEmpty()
314 {
315 {
315 return m_rect.isEmpty() || m_min==m_max || m_ticksCount==0;
316 return m_rect.isEmpty() || m_min==m_max || m_ticksCount==0;
316 }
317 }
317
318
318 //handlers
319 //handlers
319
320
320 void AxisItem::handleAxisCategoriesUpdated()
321 void AxisItem::handleAxisCategoriesUpdated()
321 {
322 {
322 if(isEmpty()) return;
323 if(isEmpty()) return;
323 updateLayout(m_layoutVector);
324 updateLayout(m_layoutVector);
324 }
325 }
325
326
326 void AxisItem::handleAxisUpdated()
327 void AxisItem::handleAxisUpdated()
327 {
328 {
328
329
329 if(isEmpty()) return;
330 if(isEmpty()) return;
330
331
331 if(m_chartAxis->isAxisVisible()) {
332 if(m_chartAxis->isAxisVisible()) {
332 setAxisOpacity(100);
333 setAxisOpacity(100);
333 }
334 }
334 else {
335 else {
335 setAxisOpacity(0);
336 setAxisOpacity(0);
336 }
337 }
337
338
338 if(m_chartAxis->isGridLineVisible()) {
339 if(m_chartAxis->isGridLineVisible()) {
339 setGridOpacity(100);
340 setGridOpacity(100);
340 }
341 }
341 else {
342 else {
342 setGridOpacity(0);
343 setGridOpacity(0);
343 }
344 }
344
345
345 if(m_chartAxis->labelsVisible())
346 if(m_chartAxis->labelsVisible())
346 {
347 {
347 setLabelsOpacity(100);
348 setLabelsOpacity(100);
348 }
349 }
349 else {
350 else {
350 setLabelsOpacity(0);
351 setLabelsOpacity(0);
351 }
352 }
352
353
353 if(m_chartAxis->shadesVisible()) {
354 if(m_chartAxis->shadesVisible()) {
354 setShadesOpacity(m_chartAxis->shadesOpacity());
355 setShadesOpacity(m_chartAxis->shadesOpacity());
355 }
356 }
356 else {
357 else {
357 setShadesOpacity(0);
358 setShadesOpacity(0);
358 }
359 }
359
360
360 setLabelsAngle(m_chartAxis->labelsAngle());
361 setLabelsAngle(m_chartAxis->labelsAngle());
361 setAxisPen(m_chartAxis->axisPen());
362 setAxisPen(m_chartAxis->axisPen());
362 setLabelsPen(m_chartAxis->labelsPen());
363 setLabelsPen(m_chartAxis->labelsPen());
363 setLabelsBrush(m_chartAxis->labelsBrush());
364 setLabelsBrush(m_chartAxis->labelsBrush());
364 setLabelsFont(m_chartAxis->labelsFont());
365 setLabelsFont(m_chartAxis->labelsFont());
365 setGridPen(m_chartAxis->gridLinePen());
366 setGridPen(m_chartAxis->gridLinePen());
366 setShadesPen(m_chartAxis->shadesPen());
367 setShadesPen(m_chartAxis->shadesPen());
367 setShadesBrush(m_chartAxis->shadesBrush());
368 setShadesBrush(m_chartAxis->shadesBrush());
368
369
369 }
370 }
370
371
371 void AxisItem::handleRangeChanged(qreal min, qreal max,int tickCount)
372 void AxisItem::handleRangeChanged(qreal min, qreal max,int tickCount)
372 {
373 {
374 qDebug()<<min<<max<<tickCount;
375 if(min==max || tickCount<2) return;
376
373 m_min = min;
377 m_min = min;
374 m_max = max;
378 m_max = max;
375 m_ticksCount = tickCount;
379 m_ticksCount= tickCount;
376
380
377 /*= qrand()%10;
378
379 while(m_ticksCount<2){
380 m_ticksCount = qrand()%10;
381 }
382
383 qDebug()<<"Warning : This is testing . Simulating new random ticks "<< m_ticksCount;
384 //m_chartAxis->setTicksCount(m_ticksCount);
385 */
386
387 if(isEmpty()) return;
381 if(isEmpty()) return;
388 QVector<qreal> layout = calculateLayout();
382 QVector<qreal> layout = calculateLayout();
389 updateLayout(layout);
383 updateLayout(layout);
390
384
391 }
385 }
392
386
393 void AxisItem::handleGeometryChanged(const QRectF& rect)
387 void AxisItem::handleGeometryChanged(const QRectF& rect)
394 {
388 {
395 m_rect = rect;
389 m_rect = rect;
396 if(isEmpty()) return;
390 if(isEmpty()) return;
397 QVector<qreal> layout = calculateLayout();
391 QVector<qreal> layout = calculateLayout();
398 updateLayout(layout);
392 updateLayout(layout);
399 }
393 }
400
394
401 //painter
395 //painter
402
396
403 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
397 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
404 {
398 {
405 Q_UNUSED(painter);
399 Q_UNUSED(painter);
406 Q_UNUSED(option);
400 Q_UNUSED(option);
407 Q_UNUSED(widget);
401 Q_UNUSED(widget);
408 }
402 }
409
403
410 //TODO "nice numbers algorithm"
404 //TODO "nice numbers algorithm"
411 #include "moc_axisitem_p.cpp"
405 #include "moc_axisitem_p.cpp"
412
406
413 QTCOMMERCIALCHART_END_NAMESPACE
407 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,391 +1,397
1 #include "qchartaxis.h"
1 #include "qchartaxis.h"
2
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
4
5 /*!
5 /*!
6 \class QChartAxis
6 \class QChartAxis
7 \brief The QChartAxis class is used for manipulating chart's axis
7 \brief The QChartAxis class is used for manipulating chart's axis
8 and for adding optional axes to the chart.
8 and for adding optional axes to the chart.
9 \mainclass
9 \mainclass
10
10
11 There is only one x Axis, however there can be multiple y axes.
11 There is only one x Axis, however there can be multiple y axes.
12 Each chart series can be bound to exactly one Y axis and the share common X axis.
12 Each chart series can be bound to exactly one Y axis and the share common X axis.
13 Axis can be setup to show axis line with ticks, gird lines and shades.
13 Axis can be setup to show axis line with ticks, gird lines and shades.
14
14
15 */
15 */
16
16
17 /*!
17 /*!
18 \fn bool QChartAxis::isAxisVisible() const
18 \fn bool QChartAxis::isAxisVisible() const
19 \brief Returns if axis is visible
19 \brief Returns if axis is visible
20 \sa setAxisVisible()
20 \sa setAxisVisible()
21 */
21 */
22
22
23 /*!
23 /*!
24 \fn QPen QChartAxis::axisPen() const
24 \fn QPen QChartAxis::axisPen() const
25 \brief Returns pen used to draw axis and ticks.
25 \brief Returns pen used to draw axis and ticks.
26 \sa setAxisPen()
26 \sa setAxisPen()
27 */
27 */
28
28
29
29
30 /*!
30 /*!
31 \fn bool QChartAxis::isGridLineVisible() const
31 \fn bool QChartAxis::isGridLineVisible() const
32 \brief Returns if grid is visible
32 \brief Returns if grid is visible
33 \sa setGridLineVisible()
33 \sa setGridLineVisible()
34 */
34 */
35
35
36 /*!
36 /*!
37 \fn QPen QChartAxis::gridLinePen() const
37 \fn QPen QChartAxis::gridLinePen() const
38 \brief Returns pen used to draw grid.
38 \brief Returns pen used to draw grid.
39 \sa setGridLinePen()
39 \sa setGridLinePen()
40 */
40 */
41
41
42 /*!
42 /*!
43 \fn bool QChartAxis::labelsVisible() const
43 \fn bool QChartAxis::labelsVisible() const
44 \brief Returns if grid is visible
44 \brief Returns if grid is visible
45 \sa setLabelsVisible()
45 \sa setLabelsVisible()
46 */
46 */
47
47
48 /*!
48 /*!
49 \fn QPen QChartAxis::labelsPen() const
49 \fn QPen QChartAxis::labelsPen() const
50 \brief Returns the pen used to labels.
50 \brief Returns the pen used to labels.
51 \sa setLabelsPen()
51 \sa setLabelsPen()
52 */
52 */
53
53
54 /*!
54 /*!
55 \fn QBrush QChartAxis::labelsBrush() const
55 \fn QBrush QChartAxis::labelsBrush() const
56 \brief Returns brush used to draw labels.
56 \brief Returns brush used to draw labels.
57 \sa setLabelsBrush()
57 \sa setLabelsBrush()
58 */
58 */
59
59
60 /*!
60 /*!
61 \fn QFont QChartAxis::labelsFont() const
61 \fn QFont QChartAxis::labelsFont() const
62 \brief Returns font used to draw labels.
62 \brief Returns font used to draw labels.
63 \sa setLabelsFont()
63 \sa setLabelsFont()
64 */
64 */
65
65
66 /*!
66 /*!
67 \fn QFont QChartAxis::labelsAngle() const
67 \fn QFont QChartAxis::labelsAngle() const
68 \brief Returns angle used to draw labels.
68 \brief Returns angle used to draw labels.
69 \sa setLabelsAngle()
69 \sa setLabelsAngle()
70 */
70 */
71
71
72 /*!
72 /*!
73 \fn bool QChartAxis::shadesVisible() const
73 \fn bool QChartAxis::shadesVisible() const
74 \brief Returns if shades are visible.
74 \brief Returns if shades are visible.
75 \sa setShadesVisible()
75 \sa setShadesVisible()
76 */
76 */
77
77
78 /*!
78 /*!
79 \fn qreal QChartAxis::shadesOpacity() const
79 \fn qreal QChartAxis::shadesOpacity() const
80 \brief Returns opacity of shades.
80 \brief Returns opacity of shades.
81 */
81 */
82
82
83 /*!
83 /*!
84 \fn QPen QChartAxis::shadesPen() const
84 \fn QPen QChartAxis::shadesPen() const
85 \brief Returns pen used to draw shades.
85 \brief Returns pen used to draw shades.
86 \sa setShadesPen()
86 \sa setShadesPen()
87 */
87 */
88
88
89 /*!
89 /*!
90 \fn QBrush QChartAxis::shadesBrush() const
90 \fn QBrush QChartAxis::shadesBrush() const
91 \brief Returns brush used to draw shades.
91 \brief Returns brush used to draw shades.
92 \sa setShadesBrush()
92 \sa setShadesBrush()
93 */
93 */
94
94
95 /*!
95 /*!
96 \fn qreal QChartAxis::min() const
96 \fn qreal QChartAxis::min() const
97 \brief Returns minimum value on the axis.
97 \brief Returns minimum value on the axis.
98 \sa setMin()
98 \sa setMin()
99 */
99 */
100
100
101 /*!
101 /*!
102 \fn qreal QChartAxis::max() const
102 \fn qreal QChartAxis::max() const
103 \brief Returns maximim value on the axis.
103 \brief Returns maximim value on the axis.
104 \sa setMax()
104 \sa setMax()
105 */
105 */
106
106
107 /*!
107 /*!
108 \fn void QChartAxis::minChanged(qreal min)
108 \fn void QChartAxis::minChanged(qreal min)
109 \brief Axis emits signal when \a min of axis has changed.
109 \brief Axis emits signal when \a min of axis has changed.
110 */
110 */
111
111
112 /*!
112 /*!
113 \fn void QChartAxis::maxChanged(qreal max)
113 \fn void QChartAxis::maxChanged(qreal max)
114 \brief Axis emits signal when \a max of axis has changed.
114 \brief Axis emits signal when \a max of axis has changed.
115 */
115 */
116
116
117 /*!
117 /*!
118 \fn void QChartAxis::rangeChanged(qreal min, qreal max)
118 \fn void QChartAxis::rangeChanged(qreal min, qreal max)
119 \brief Axis emits signal when \a min or \a max of axis has changed.
119 \brief Axis emits signal when \a min or \a max of axis has changed.
120 */
120 */
121
121
122 /*!
122 /*!
123 \fn int QChartAxis::ticksCount() const
123 \fn int QChartAxis::ticksCount() const
124 \brief Return number of ticks on the axis
124 \brief Return number of ticks on the axis
125 \sa setTicksCount()
125 \sa setTicksCount()
126 */
126 */
127
127
128 /*!
128 /*!
129 \fn void QChartAxis::updated()
129 \fn void QChartAxis::updated()
130 \brief \internal
130 \brief \internal
131 */
131 */
132
132
133 /*!
133 /*!
134 \fn void QChartAxis::handleAxisRangeChanged(qreal min, qreal max)
134 \fn void QChartAxis::handleAxisRangeChanged(qreal min, qreal max)
135 \brief \internal \a min \a max
135 \brief \internal \a min \a max
136 */
136 */
137
137
138 /*!
138 /*!
139 Constructs new axis object which is a child of \a parent. Ownership is taken by
139 Constructs new axis object which is a child of \a parent. Ownership is taken by
140 QChatView or QChart when axis added.
140 QChatView or QChart when axis added.
141 */
141 */
142
142
143 QChartAxis::QChartAxis(QObject* parent):QObject(parent),
143 QChartAxis::QChartAxis(QObject* parent):QObject(parent),
144 m_axisVisible(true),
144 m_axisVisible(true),
145 m_gridLineVisible(true),
145 m_gridLineVisible(true),
146 m_labelsVisible(true),
146 m_labelsVisible(true),
147 m_labelsAngle(0),
147 m_labelsAngle(0),
148 m_shadesVisible(false),
148 m_shadesVisible(false),
149 m_shadesOpacity(1.0),
149 m_shadesOpacity(1.0),
150 m_min(0),
150 m_min(0),
151 m_max(0),
151 m_max(0),
152 m_ticksCount(5)
152 m_ticksCount(5)
153 {
153 {
154
154
155 }
155 }
156
156
157 /*!
157 /*!
158 Destructor of the axis object. When axis is added to chart, chart object takes ownership.
158 Destructor of the axis object. When axis is added to chart, chart object takes ownership.
159 */
159 */
160
160
161 QChartAxis::~QChartAxis()
161 QChartAxis::~QChartAxis()
162 {
162 {
163 }
163 }
164
164
165 /*!
165 /*!
166 Sets \a pen used to draw axis line and ticks.
166 Sets \a pen used to draw axis line and ticks.
167 */
167 */
168 void QChartAxis::setAxisPen(const QPen& pen)
168 void QChartAxis::setAxisPen(const QPen& pen)
169 {
169 {
170 if (pen != m_axisPen) {
170 if (pen != m_axisPen) {
171 m_axisPen=pen;
171 m_axisPen=pen;
172 emit updated();
172 emit updated();
173 }
173 }
174 }
174 }
175
175
176 /*!
176 /*!
177 Sets if axis and ticks are \a visible.
177 Sets if axis and ticks are \a visible.
178 */
178 */
179 void QChartAxis::setAxisVisible(bool visible)
179 void QChartAxis::setAxisVisible(bool visible)
180 {
180 {
181 if (m_axisVisible!=visible) {
181 if (m_axisVisible!=visible) {
182 m_axisVisible=visible;
182 m_axisVisible=visible;
183 emit updated();
183 emit updated();
184 }
184 }
185 }
185 }
186
186
187 /*!
187 /*!
188 Sets if grid line is \a visible.
188 Sets if grid line is \a visible.
189 */
189 */
190 void QChartAxis::setGridLineVisible(bool visible)
190 void QChartAxis::setGridLineVisible(bool visible)
191 {
191 {
192 if (m_gridLineVisible!=visible) {
192 if (m_gridLineVisible!=visible) {
193 m_gridLineVisible=visible;
193 m_gridLineVisible=visible;
194 emit updated();
194 emit updated();
195 }
195 }
196 }
196 }
197
197
198 /*!
198 /*!
199 Sets \a pen used to draw grid line.
199 Sets \a pen used to draw grid line.
200 */
200 */
201 void QChartAxis::setGridLinePen(const QPen& pen)
201 void QChartAxis::setGridLinePen(const QPen& pen)
202 {
202 {
203 if (m_gridLinePen!=pen) {
203 if (m_gridLinePen!=pen) {
204 m_gridLinePen=pen;
204 m_gridLinePen=pen;
205 emit updated();
205 emit updated();
206 }
206 }
207 }
207 }
208
208
209 /*!
209 /*!
210 Sets if axis' labels are \a visible.
210 Sets if axis' labels are \a visible.
211 */
211 */
212 void QChartAxis::setLabelsVisible(bool visible)
212 void QChartAxis::setLabelsVisible(bool visible)
213 {
213 {
214 if(m_labelsVisible!=visible) {
214 if(m_labelsVisible!=visible) {
215 m_labelsVisible=visible;
215 m_labelsVisible=visible;
216 emit updated();
216 emit updated();
217 }
217 }
218 }
218 }
219
219
220 /*!
220 /*!
221 Sets \a pen used to draw labels.
221 Sets \a pen used to draw labels.
222 */
222 */
223 void QChartAxis::setLabelsPen(const QPen& pen)
223 void QChartAxis::setLabelsPen(const QPen& pen)
224 {
224 {
225 if(m_labelsPen!=pen) {
225 if(m_labelsPen!=pen) {
226 m_labelsPen=pen;
226 m_labelsPen=pen;
227 emit updated();
227 emit updated();
228 }
228 }
229 }
229 }
230
230
231 /*!
231 /*!
232 Sets \a brush used to draw labels.
232 Sets \a brush used to draw labels.
233 */
233 */
234 void QChartAxis::setLabelsBrush(const QBrush& brush)
234 void QChartAxis::setLabelsBrush(const QBrush& brush)
235 {
235 {
236 if(m_labelsBrush!=brush) {
236 if(m_labelsBrush!=brush) {
237 m_labelsBrush=brush;
237 m_labelsBrush=brush;
238 emit updated();
238 emit updated();
239 }
239 }
240 }
240 }
241
241
242 /*!
242 /*!
243 Sets \a font used to draw labels.
243 Sets \a font used to draw labels.
244 */
244 */
245 void QChartAxis::setLabelsFont(const QFont& font)
245 void QChartAxis::setLabelsFont(const QFont& font)
246 {
246 {
247 if(m_labelsFont!=font) {
247 if(m_labelsFont!=font) {
248 m_labelsFont=font;
248 m_labelsFont=font;
249 emit updated();
249 emit updated();
250 }
250 }
251 }
251 }
252
252
253 /*!
253 /*!
254 Sets \a angle for all the labels on given axis.
254 Sets \a angle for all the labels on given axis.
255 */
255 */
256 void QChartAxis::setLabelsAngle(int angle)
256 void QChartAxis::setLabelsAngle(int angle)
257 {
257 {
258 if(m_labelsAngle!=angle) {
258 if(m_labelsAngle!=angle) {
259 m_labelsAngle=angle;
259 m_labelsAngle=angle;
260 emit updated();
260 emit updated();
261 }
261 }
262 }
262 }
263
263
264 /*!
264 /*!
265 Sets if shades are \a visible.
265 Sets if shades are \a visible.
266 */
266 */
267 void QChartAxis::setShadesVisible(bool visible)
267 void QChartAxis::setShadesVisible(bool visible)
268 {
268 {
269 if(m_shadesVisible!=visible) {
269 if(m_shadesVisible!=visible) {
270 m_shadesVisible=visible;
270 m_shadesVisible=visible;
271 emit updated();
271 emit updated();
272 }
272 }
273 }
273 }
274
274
275 /*!
275 /*!
276 Sets \a pen used to draw shades.
276 Sets \a pen used to draw shades.
277 */
277 */
278 void QChartAxis::setShadesPen(const QPen& pen)
278 void QChartAxis::setShadesPen(const QPen& pen)
279 {
279 {
280 if(m_shadesPen!=pen) {
280 if(m_shadesPen!=pen) {
281 m_shadesPen=pen;
281 m_shadesPen=pen;
282 emit updated();
282 emit updated();
283 }
283 }
284 }
284 }
285
285
286 /*!
286 /*!
287 Sets \a brush used to draw shades.
287 Sets \a brush used to draw shades.
288 */
288 */
289 void QChartAxis::setShadesBrush(const QBrush& brush)
289 void QChartAxis::setShadesBrush(const QBrush& brush)
290 {
290 {
291 if(m_shadesBrush!=brush) {
291 if(m_shadesBrush!=brush) {
292 m_shadesBrush=brush;
292 m_shadesBrush=brush;
293 emit updated();
293 emit updated();
294 }
294 }
295 }
295 }
296
296
297 /*!
297 /*!
298 Sets \a opacity of the shades.
298 Sets \a opacity of the shades.
299 */
299 */
300 void QChartAxis::setShadesOpacity(qreal opacity)
300 void QChartAxis::setShadesOpacity(qreal opacity)
301 {
301 {
302 if(m_shadesOpacity!=opacity) {
302 if(m_shadesOpacity!=opacity) {
303 m_shadesOpacity=opacity;
303 m_shadesOpacity=opacity;
304 emit updated();
304 emit updated();
305 }
305 }
306 }
306 }
307
307
308 /*!
308 /*!
309 Sets \a min value on the axis.
309 Sets \a min value on the axis.
310 */
310 */
311 void QChartAxis::setMin(qreal min)
311 void QChartAxis::setMin(qreal min)
312 {
312 {
313 setRange(min,m_max);
313 setRange(min,m_max);
314 }
314 }
315
315
316 /*!
316 /*!
317 Sets \a max value on the axis.
317 Sets \a max value on the axis.
318 */
318 */
319 void QChartAxis::setMax(qreal max)
319 void QChartAxis::setMax(qreal max)
320 {
320 {
321 setRange(m_min,max);
321 setRange(m_min,max);
322 }
322 }
323
323
324 /*!
324 /*!
325 Sets range from \a min to \a max on the axis.
325 Sets range from \a min to \a max on the axis.
326 */
326 */
327 void QChartAxis::setRange(qreal min, qreal max)
327 void QChartAxis::setRange(qreal min, qreal max)
328 {
328 {
329
329
330
330
331 bool changed = false;
331 bool changed = false;
332 if(m_min!=min) {
332 if(m_min!=min) {
333 m_min=min;
333 m_min=min;
334 changed=true;
334 changed=true;
335 emit minChanged(min);
335 emit minChanged(min);
336 }
336 }
337
337
338 if(m_max!=max) {
338 if(m_max!=max) {
339 m_max=max;
339 m_max=max;
340 changed=true;
340 changed=true;
341 emit maxChanged(max);
341 emit maxChanged(max);
342 }
342 }
343
343
344 if(changed) {
344 if(changed) {
345 emit rangeChanged(m_min,m_max,m_ticksCount);
345 emit rangeChanged(m_min,m_max);
346 }
346 }
347 }
347 }
348
348
349 void QChartAxis::handleAxisRangeChanged(qreal min, qreal max)
350 {
351 setRange(min,max);
352 }
353
354 /*!
349 /*!
355 Sets \a count for ticks on the axis.
350 Sets \a count for ticks on the axis.
356 */
351 */
357 void QChartAxis::setTicksCount(int count)
352 void QChartAxis::setTicksCount(int count)
358 {
353 {
359 if(m_ticksCount!=count) {
354 if(m_ticksCount!=count) {
360 m_ticksCount=count;
355 m_ticksCount=count;
361 emit rangeChanged(m_min,m_max,m_ticksCount);
356 emit ticksCountChanged(count);
362 }
357 }
363 }
358 }
364
359
365 /*!
360 /*!
366 Sets axis, shades, labels and grid lines to be visible.
361 Sets axis, shades, labels and grid lines to be visible.
367 */
362 */
368 void QChartAxis::show()
363 void QChartAxis::show()
369 {
364 {
370 m_axisVisible=true;
365 m_axisVisible=true;
371 m_gridLineVisible=true;
366 m_gridLineVisible=true;
372 m_labelsVisible=true;
367 m_labelsVisible=true;
373 m_shadesVisible=true;
368 m_shadesVisible=true;
374 emit updated();
369 emit updated();
375 }
370 }
376
371
377 /*!
372 /*!
378 Sets axis, shades, labels and grid lines to not be visible.
373 Sets axis, shades, labels and grid lines to not be visible.
379 */
374 */
380 void QChartAxis::hide()
375 void QChartAxis::hide()
381 {
376 {
382 m_axisVisible=false;
377 m_axisVisible=false;
383 m_gridLineVisible=false;
378 m_gridLineVisible=false;
384 m_labelsVisible=false;
379 m_labelsVisible=false;
385 m_shadesVisible=false;
380 m_shadesVisible=false;
386 emit updated();
381 emit updated();
387 }
382 }
388
383
384 void QChartAxis::handleAxisRangeChanged(qreal min, qreal max)
385 {
386 qDebug()<<__FUNCTION__<<min<<max;
387 setRange(min,max);
388 }
389
390 void QChartAxis::handleAxisTicksChanged(int count)
391 {
392 setTicksCount(count);
393 }
394
389 #include "moc_qchartaxis.cpp"
395 #include "moc_qchartaxis.cpp"
390
396
391 QTCOMMERCIALCHART_END_NAMESPACE
397 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,106 +1,108
1 #ifndef QCHARTAXIS_H_
1 #ifndef QCHARTAXIS_H_
2 #define QCHARTAXIS_H_
2 #define QCHARTAXIS_H_
3
3
4 #include <qchartaxiscategories.h>
4 #include <qchartaxiscategories.h>
5 #include <QPen>
5 #include <QPen>
6 #include <QFont>
6 #include <QFont>
7
7
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 class QTCOMMERCIALCHART_EXPORT QChartAxis : public QObject
11 class QTCOMMERCIALCHART_EXPORT QChartAxis : public QObject
12 {
12 {
13 Q_OBJECT
13 Q_OBJECT
14 public:
14 public:
15 QChartAxis(QObject* parent =0);
15 QChartAxis(QObject* parent =0);
16 ~QChartAxis();
16 ~QChartAxis();
17
17
18 //axis handling
18 //axis handling
19 bool isAxisVisible() const { return m_axisVisible;};
19 bool isAxisVisible() const { return m_axisVisible;};
20 void setAxisVisible(bool visible);
20 void setAxisVisible(bool visible);
21 void setAxisPen(const QPen& pen);
21 void setAxisPen(const QPen& pen);
22 QPen axisPen() const { return m_axisPen;};
22 QPen axisPen() const { return m_axisPen;};
23
23
24 //grid handling
24 //grid handling
25 bool isGridLineVisible() const { return m_gridLineVisible;};
25 bool isGridLineVisible() const { return m_gridLineVisible;};
26 void setGridLineVisible(bool visible);
26 void setGridLineVisible(bool visible);
27 void setGridLinePen(const QPen& pen);
27 void setGridLinePen(const QPen& pen);
28 QPen gridLinePen() const {return m_gridLinePen;}
28 QPen gridLinePen() const {return m_gridLinePen;}
29
29
30 //labels handling
30 //labels handling
31 bool labelsVisible() const { return m_labelsVisible;};
31 bool labelsVisible() const { return m_labelsVisible;};
32 void setLabelsVisible(bool visible);
32 void setLabelsVisible(bool visible);
33 void setLabelsPen(const QPen& pen);
33 void setLabelsPen(const QPen& pen);
34 QPen labelsPen() const { return m_labelsPen;}
34 QPen labelsPen() const { return m_labelsPen;}
35 void setLabelsBrush(const QBrush& brush);
35 void setLabelsBrush(const QBrush& brush);
36 QBrush labelsBrush() const { return m_labelsBrush;}
36 QBrush labelsBrush() const { return m_labelsBrush;}
37 void setLabelsFont(const QFont& font);
37 void setLabelsFont(const QFont& font);
38 QFont labelsFont() const { return m_labelsFont;}
38 QFont labelsFont() const { return m_labelsFont;}
39 void setLabelsAngle(int angle);
39 void setLabelsAngle(int angle);
40 int labelsAngle() const { return m_labelsAngle;};
40 int labelsAngle() const { return m_labelsAngle;};
41
41
42 //shades handling
42 //shades handling
43 bool shadesVisible() const { return m_shadesVisible;};
43 bool shadesVisible() const { return m_shadesVisible;};
44 void setShadesVisible(bool visible);
44 void setShadesVisible(bool visible);
45 void setShadesPen(const QPen& pen);
45 void setShadesPen(const QPen& pen);
46 QPen shadesPen() const { return m_shadesPen;}
46 QPen shadesPen() const { return m_shadesPen;}
47 void setShadesBrush(const QBrush& brush);
47 void setShadesBrush(const QBrush& brush);
48 QBrush shadesBrush() const { return m_shadesBrush;}
48 QBrush shadesBrush() const { return m_shadesBrush;}
49 void setShadesOpacity(qreal opacity);
49 void setShadesOpacity(qreal opacity);
50 qreal shadesOpacity() const { return m_shadesOpacity;}
50 qreal shadesOpacity() const { return m_shadesOpacity;}
51
51
52 //range handling
52 //range handling
53 void setMin(qreal min);
53 void setMin(qreal min);
54 qreal min() const { return m_min;};
54 qreal min() const { return m_min;};
55 void setMax(qreal max);
55 void setMax(qreal max);
56 qreal max() const { return m_max;};
56 qreal max() const { return m_max;};
57 void setRange(qreal min, qreal max);
57 void setRange(qreal min, qreal max);
58
58
59 //ticks handling
59 //ticks handling
60 void setTicksCount(int count);
60 void setTicksCount(int count);
61 int ticksCount() const { return m_ticksCount;}
61 int ticksCount() const { return m_ticksCount;}
62
62
63 QChartAxisCategories* categories() { return &m_category; }
63 QChartAxisCategories* categories() { return &m_category; }
64
64
65 void show();
65 void show();
66 void hide();
66 void hide();
67
67
68 signals:
68 signals:
69 void minChanged(qreal min);
69 void minChanged(qreal min);
70 void maxChanged(qreal max);
70 void maxChanged(qreal max);
71 void rangeChanged(qreal min, qreal max,int ticksCount);
71 void rangeChanged(qreal min, qreal max);
72 void ticksCountChanged(int count);
72
73
73 //interal signal
74 //interal signal
74 void updated();
75 void updated();
75 //internal slot
76 //internal slot
76 public slots:
77 public slots:
77 void handleAxisRangeChanged(qreal min, qreal max);
78 void handleAxisRangeChanged(qreal min, qreal max);
79 void handleAxisTicksChanged(int count);
78
80
79 private:
81 private:
80 bool m_axisVisible;
82 bool m_axisVisible;
81 QPen m_axisPen;
83 QPen m_axisPen;
82 QBrush m_axisBrush;
84 QBrush m_axisBrush;
83
85
84 bool m_gridLineVisible;
86 bool m_gridLineVisible;
85 QPen m_gridLinePen;
87 QPen m_gridLinePen;
86
88
87 bool m_labelsVisible;
89 bool m_labelsVisible;
88 QPen m_labelsPen;
90 QPen m_labelsPen;
89 QBrush m_labelsBrush;
91 QBrush m_labelsBrush;
90 QFont m_labelsFont;
92 QFont m_labelsFont;
91 int m_labelsAngle;
93 int m_labelsAngle;
92
94
93 bool m_shadesVisible;
95 bool m_shadesVisible;
94 QPen m_shadesPen;
96 QPen m_shadesPen;
95 QBrush m_shadesBrush;
97 QBrush m_shadesBrush;
96 qreal m_shadesOpacity;
98 qreal m_shadesOpacity;
97
99
98 qreal m_min;
100 qreal m_min;
99 qreal m_max;
101 qreal m_max;
100
102
101 int m_ticksCount;
103 int m_ticksCount;
102 QChartAxisCategories m_category;
104 QChartAxisCategories m_category;
103 };
105 };
104
106
105 QTCOMMERCIALCHART_END_NAMESPACE
107 QTCOMMERCIALCHART_END_NAMESPACE
106 #endif /* QCHARTAXIS_H_ */
108 #endif /* QCHARTAXIS_H_ */
@@ -1,315 +1,319
1 #include "chartdataset_p.h"
1 #include "chartdataset_p.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 //series
3 //series
4 #include "qlineseries.h"
4 #include "qlineseries.h"
5 #include "qareaseries.h"
5 #include "qareaseries.h"
6 #include "qbarseries.h"
6 #include "qbarseries.h"
7 #include "qstackedbarseries.h"
7 #include "qstackedbarseries.h"
8 #include "qpercentbarseries.h"
8 #include "qpercentbarseries.h"
9 #include "qpieseries.h"
9 #include "qpieseries.h"
10 #include "qscatterseries.h"
10 #include "qscatterseries.h"
11 #include "qsplineseries.h"
11 #include "qsplineseries.h"
12
12
13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
14
14
15 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
15 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
16 m_axisX(new QChartAxis(this)),
16 m_axisX(new QChartAxis(this)),
17 m_axisY(new QChartAxis(this)),
17 m_axisY(new QChartAxis(this)),
18 m_domainIndex(0),
18 m_domainIndex(0),
19 m_axisXInitialized(false)
19 m_axisXInitialized(false)
20 {
20 {
21 }
21 }
22
22
23 ChartDataSet::~ChartDataSet()
23 ChartDataSet::~ChartDataSet()
24 {
24 {
25 }
25 }
26
26
27 void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY)
27 void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY)
28 {
28 {
29 if(axisY==0) axisY = m_axisY;
29 if(axisY==0) axisY = m_axisY;
30
30
31 QChartAxis* axis = m_seriesAxisMap.value(series);
31 QChartAxis* axis = m_seriesAxisMap.value(series);
32
32
33 if(axis) {
33 if(axis) {
34 qWarning() << "Can not add series. Series already on the chart";
34 qWarning() << "Can not add series. Series already on the chart";
35 return;
35 return;
36 }
36 }
37
37
38 if(!series->parent()){
38 if(!series->parent()){
39 series->setParent(this); // take ownership
39 series->setParent(this); // take ownership
40 };
40 };
41
41
42 if(!axisY->parent()){
42 if(!axisY->parent()){
43 axisY->setParent(this); // take ownership
43 axisY->setParent(this); // take ownership
44 }
44 }
45
45
46 Domain* domain = m_axisDomainMap.value(axisY);
46 Domain* domain = m_axisDomainMap.value(axisY);
47
47
48 if(!domain) {
48 if(!domain) {
49 domain = new Domain();
49 domain = new Domain();
50 QObject::connect(axisY,SIGNAL(rangeChanged(qreal,qreal,int)),domain,SLOT(handleAxisRangeYChanged(qreal,qreal,int)));
50 QObject::connect(axisY,SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeYChanged(qreal,qreal)));
51 QObject::connect(axisX(),SIGNAL(rangeChanged(qreal,qreal,int)),domain,SLOT(handleAxisRangeXChanged(qreal,qreal,int)));
51 QObject::connect(axisX(),SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeXChanged(qreal,qreal)));
52 QObject::connect(axisY,SIGNAL(ticksCountChanged(int)),domain,SLOT(handleAxisYTicksCountChanged(int)));
53 QObject::connect(axisX(),SIGNAL(ticksCountChanged(int)),domain,SLOT(handleAxisXTicksCountChanged(int)));
54 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axisY,SLOT(handleAxisRangeChanged(qreal,qreal)));
55 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axisX(),SLOT(handleAxisRangeChanged(qreal,qreal)));
52 //initialize
56 //initialize
53 m_axisDomainMap.insert(axisY,domain);
57 m_axisDomainMap.insert(axisY,domain);
54 emit axisAdded(axisY,domain);
58 emit axisAdded(axisY,domain);
55 }
59 }
56
60
57 if(!m_axisXInitialized){
61 if(!m_axisXInitialized){
58 emit axisAdded(axisX(),domain);
62 emit axisAdded(axisX(),domain);
59 m_axisXInitialized=true;
63 m_axisXInitialized=true;
60 }
64 }
61
65
62 calculateDomain(series,domain);
66 calculateDomain(series,domain);
63
67
64 m_seriesAxisMap.insert(series,axisY);
68 m_seriesAxisMap.insert(series,axisY);
65 emit seriesAdded(series,domain);
69 emit seriesAdded(series,domain);
66
70
67 }
71 }
68
72
69 void ChartDataSet::removeSeries(QSeries* series)
73 void ChartDataSet::removeSeries(QSeries* series)
70 {
74 {
71
75
72 QChartAxis* axis = m_seriesAxisMap.value(series);
76 QChartAxis* axis = m_seriesAxisMap.value(series);
73
77
74 if(!axis){
78 if(!axis){
75 qWarning()<<"Can not remove series. Series not found on the chart.";
79 qWarning()<<"Can not remove series. Series not found on the chart.";
76 return;
80 return;
77 }
81 }
78 emit seriesRemoved(series);
82 emit seriesRemoved(series);
79 m_seriesAxisMap.remove(series);
83 m_seriesAxisMap.remove(series);
80
84
81 if(series->parent()==this){
85 if(series->parent()==this){
82 delete series;
86 delete series;
83 series=0;
87 series=0;
84 }
88 }
85
89
86 QList<QChartAxis*> axes = m_seriesAxisMap.values();
90 QList<QChartAxis*> axes = m_seriesAxisMap.values();
87
91
88 int i = axes.indexOf(axis);
92 int i = axes.indexOf(axis);
89
93
90 if(i==-1){
94 if(i==-1){
91 Domain* domain = m_axisDomainMap.take(axis);
95 Domain* domain = m_axisDomainMap.take(axis);
92 emit axisRemoved(axis);
96 emit axisRemoved(axis);
93 if(axis!=axisY()){
97 if(axis!=axisY()){
94 if(axis->parent()==this){
98 if(axis->parent()==this){
95 delete axis;
99 delete axis;
96 axis=0;
100 axis=0;
97 }
101 }
98 }
102 }
99 delete domain;
103 delete domain;
100 }
104 }
101
105
102 if(m_seriesAxisMap.values().size()==0)
106 if(m_seriesAxisMap.values().size()==0)
103 {
107 {
104 m_axisXInitialized=false;
108 m_axisXInitialized=false;
105 emit axisRemoved(axisX());
109 emit axisRemoved(axisX());
106 }
110 }
107 }
111 }
108
112
109 void ChartDataSet::removeAllSeries()
113 void ChartDataSet::removeAllSeries()
110 {
114 {
111
115
112 QList<QSeries*> series = m_seriesAxisMap.keys();
116 QList<QSeries*> series = m_seriesAxisMap.keys();
113
117
114 foreach(QSeries* s , series) {
118 foreach(QSeries* s , series) {
115 removeSeries(s);
119 removeSeries(s);
116 }
120 }
117
121
118 Q_ASSERT(m_seriesAxisMap.count()==0);
122 Q_ASSERT(m_seriesAxisMap.count()==0);
119 Q_ASSERT(m_axisDomainMap.count()==0);
123 Q_ASSERT(m_axisDomainMap.count()==0);
120
124
121 }
125 }
122
126
123 //to be removed with PIMPL
127 //to be removed with PIMPL
124 void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) const
128 void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) const
125 {
129 {
126 switch(series->type())
130 switch(series->type())
127 {
131 {
128 case QSeries::SeriesTypeLine:
132 case QSeries::SeriesTypeLine:
129 case QSeries::SeriesTypeSpline:
133 case QSeries::SeriesTypeSpline:
130 case QSeries::SeriesTypeScatter:
134 case QSeries::SeriesTypeScatter:
131 {
135 {
132
136
133 QXYSeries* xySeries = static_cast<QXYSeries*>(series);
137 QXYSeries* xySeries = static_cast<QXYSeries*>(series);
134
138
135 qreal minX(domain->minX());
139 qreal minX(domain->minX());
136 qreal minY(domain->minY());
140 qreal minY(domain->minY());
137 qreal maxX(domain->maxX());
141 qreal maxX(domain->maxX());
138 qreal maxY(domain->maxY());
142 qreal maxY(domain->maxY());
139
143
140 for (int i = 0; i < xySeries->count(); i++)
144 for (int i = 0; i < xySeries->count(); i++)
141 {
145 {
142 qreal x = xySeries->x(i);
146 qreal x = xySeries->x(i);
143 qreal y = xySeries->y(i);
147 qreal y = xySeries->y(i);
144 minX = qMin(minX, x);
148 minX = qMin(minX, x);
145 minY = qMin(minY, y);
149 minY = qMin(minY, y);
146 maxX = qMax(maxX, x);
150 maxX = qMax(maxX, x);
147 maxY = qMax(maxY, y);
151 maxY = qMax(maxY, y);
148 }
152 }
149
153
150 domain->setRange(minX, maxX, minY, maxY);
154 domain->setRange(minX, maxX, minY, maxY);
151 break;
155 break;
152 }
156 }
153 case QSeries::SeriesTypeArea: {
157 case QSeries::SeriesTypeArea: {
154
158
155 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
159 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
156
160
157 QLineSeries* upperSeries = areaSeries->upperSeries();
161 QLineSeries* upperSeries = areaSeries->upperSeries();
158 QLineSeries* lowerSeries = areaSeries->lowerSeries();
162 QLineSeries* lowerSeries = areaSeries->lowerSeries();
159
163
160 for (int i = 0; i < upperSeries->count(); i++)
164 for (int i = 0; i < upperSeries->count(); i++)
161 {
165 {
162 qreal x = upperSeries->x(i);
166 qreal x = upperSeries->x(i);
163 qreal y = upperSeries->y(i);
167 qreal y = upperSeries->y(i);
164 domain->setMinX(qMin(domain->minX(),x));
168 domain->setMinX(qMin(domain->minX(),x));
165 domain->setMinY(qMin(domain->minY(),y));
169 domain->setMinY(qMin(domain->minY(),y));
166 domain->setMaxX(qMax(domain->maxX(),x));
170 domain->setMaxX(qMax(domain->maxX(),x));
167 domain->setMaxY(qMax(domain->maxY(),y));
171 domain->setMaxY(qMax(domain->maxY(),y));
168 }
172 }
169 if(lowerSeries) {
173 if(lowerSeries) {
170 for (int i = 0; i < lowerSeries->count(); i++)
174 for (int i = 0; i < lowerSeries->count(); i++)
171 {
175 {
172 qreal x = lowerSeries->x(i);
176 qreal x = lowerSeries->x(i);
173 qreal y = lowerSeries->y(i);
177 qreal y = lowerSeries->y(i);
174 domain->setMinX(qMin(domain->minX(),x));
178 domain->setMinX(qMin(domain->minX(),x));
175 domain->setMinY(qMin(domain->minY(),y));
179 domain->setMinY(qMin(domain->minY(),y));
176 domain->setMaxX(qMax(domain->maxX(),x));
180 domain->setMaxX(qMax(domain->maxX(),x));
177 domain->setMaxY(qMax(domain->maxY(),y));
181 domain->setMaxY(qMax(domain->maxY(),y));
178 }}
182 }}
179 break;
183 break;
180 }
184 }
181 case QSeries::SeriesTypeBar: {
185 case QSeries::SeriesTypeBar: {
182 qDebug() << "QChartSeries::SeriesTypeBar";
186 qDebug() << "QChartSeries::SeriesTypeBar";
183 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
187 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
184 qreal x = barSeries->categoryCount();
188 qreal x = barSeries->categoryCount();
185 qreal y = barSeries->max();
189 qreal y = barSeries->max();
186 domain->setMinX(qMin(domain->minX(),x));
190 domain->setMinX(qMin(domain->minX(),x));
187 domain->setMinY(qMin(domain->minY(),y));
191 domain->setMinY(qMin(domain->minY(),y));
188 domain->setMaxX(qMax(domain->maxX(),x));
192 domain->setMaxX(qMax(domain->maxX(),x));
189 domain->setMaxY(qMax(domain->maxY(),y));
193 domain->setMaxY(qMax(domain->maxY(),y));
190 break;
194 break;
191 }
195 }
192 case QSeries::SeriesTypeStackedBar: {
196 case QSeries::SeriesTypeStackedBar: {
193 qDebug() << "QChartSeries::SeriesTypeStackedBar";
197 qDebug() << "QChartSeries::SeriesTypeStackedBar";
194
198
195 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
199 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
196 qreal x = stackedBarSeries->categoryCount();
200 qreal x = stackedBarSeries->categoryCount();
197 qreal y = stackedBarSeries->maxCategorySum();
201 qreal y = stackedBarSeries->maxCategorySum();
198 domain->setMinX(qMin(domain->minX(),x));
202 domain->setMinX(qMin(domain->minX(),x));
199 domain->setMinY(qMin(domain->minY(),y));
203 domain->setMinY(qMin(domain->minY(),y));
200 domain->setMaxX(qMax(domain->maxX(),x));
204 domain->setMaxX(qMax(domain->maxX(),x));
201 domain->setMaxY(qMax(domain->maxY(),y));
205 domain->setMaxY(qMax(domain->maxY(),y));
202 break;
206 break;
203 }
207 }
204 case QSeries::SeriesTypePercentBar: {
208 case QSeries::SeriesTypePercentBar: {
205 qDebug() << "QChartSeries::SeriesTypePercentBar";
209 qDebug() << "QChartSeries::SeriesTypePercentBar";
206
210
207 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
211 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
208 qreal x = percentBarSeries->categoryCount();
212 qreal x = percentBarSeries->categoryCount();
209 domain->setMinX(qMin(domain->minX(),x));
213 domain->setMinX(qMin(domain->minX(),x));
210 domain->setMinY(0);
214 domain->setMinY(0);
211 domain->setMaxX(qMax(domain->maxX(),x));
215 domain->setMaxX(qMax(domain->maxX(),x));
212 domain->setMaxY(100);
216 domain->setMaxY(100);
213 break;
217 break;
214 }
218 }
215
219
216 case QSeries::SeriesTypePie: {
220 case QSeries::SeriesTypePie: {
217 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
221 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
218 // TODO: domain stuff
222 // TODO: domain stuff
219 break;
223 break;
220 }
224 }
221
225
222
226
223 default: {
227 default: {
224 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
228 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
225 return;
229 return;
226 break;
230 break;
227 }
231 }
228
232
229 }
233 }
230 }
234 }
231
235
232 void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size)
236 void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size)
233 {
237 {
234 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
238 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
235 while (i.hasNext()) {
239 while (i.hasNext()) {
236 i.next();
240 i.next();
237 i.value()->zoomIn(rect,size);
241 i.value()->zoomIn(rect,size);
238 }
242 }
239 }
243 }
240
244
241 void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size)
245 void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size)
242 {
246 {
243 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
247 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
244 while (i.hasNext()) {
248 while (i.hasNext()) {
245 i.next();
249 i.next();
246 i.value()->zoomOut(rect,size);
250 i.value()->zoomOut(rect,size);
247 }
251 }
248 }
252 }
249
253
250 int ChartDataSet::seriesCount(QSeries::QSeriesType type)
254 int ChartDataSet::seriesCount(QSeries::QSeriesType type)
251 {
255 {
252 int count=0;
256 int count=0;
253 QMapIterator<QSeries*, QChartAxis*> i( m_seriesAxisMap);
257 QMapIterator<QSeries*, QChartAxis*> i( m_seriesAxisMap);
254 while (i.hasNext()) {
258 while (i.hasNext()) {
255 i.next();
259 i.next();
256 if(i.key()->type()==type) count++;
260 if(i.key()->type()==type) count++;
257 }
261 }
258 return count;
262 return count;
259 }
263 }
260
264
261 int ChartDataSet::seriesIndex(QSeries *series)
265 int ChartDataSet::seriesIndex(QSeries *series)
262 {
266 {
263 int count(-1);
267 int count(-1);
264 QMapIterator<QSeries*, QChartAxis*> i(m_seriesAxisMap);
268 QMapIterator<QSeries*, QChartAxis*> i(m_seriesAxisMap);
265 while (i.hasNext()) {
269 while (i.hasNext()) {
266 i.next();
270 i.next();
267 count++;
271 count++;
268 if (i.key() == series)
272 if (i.key() == series)
269 return count;
273 return count;
270 }
274 }
271 return count;
275 return count;
272 }
276 }
273
277
274 QChartAxis* ChartDataSet::axisY(QSeries* series) const
278 QChartAxis* ChartDataSet::axisY(QSeries* series) const
275 {
279 {
276 if(series == 0) return m_axisY;
280 if(series == 0) return m_axisY;
277 return m_seriesAxisMap.value(series);
281 return m_seriesAxisMap.value(series);
278 }
282 }
279
283
280 Domain* ChartDataSet::domain(QSeries* series) const
284 Domain* ChartDataSet::domain(QSeries* series) const
281 {
285 {
282 QChartAxis* axis = m_seriesAxisMap.value(series);
286 QChartAxis* axis = m_seriesAxisMap.value(series);
283 if(axis){
287 if(axis){
284 return m_axisDomainMap.value(axis);
288 return m_axisDomainMap.value(axis);
285 }else
289 }else
286 return 0;
290 return 0;
287 }
291 }
288
292
289 Domain* ChartDataSet::domain(QChartAxis* axis) const
293 Domain* ChartDataSet::domain(QChartAxis* axis) const
290 {
294 {
291 if(axis==axisX()) {
295 if(axis==axisX()) {
292 return m_axisDomainMap.value(axisY());
296 return m_axisDomainMap.value(axisY());
293 }
297 }
294 else {
298 else {
295 return m_axisDomainMap.value(axis);
299 return m_axisDomainMap.value(axis);
296 }
300 }
297 }
301 }
298
302
299 QChartAxis* ChartDataSet::axis(QSeries* series) const
303 QChartAxis* ChartDataSet::axis(QSeries* series) const
300 {
304 {
301 return m_seriesAxisMap.value(series);
305 return m_seriesAxisMap.value(series);
302 }
306 }
303
307
304 void ChartDataSet::scrollDomain(int dx,int dy,const QSizeF& size)
308 void ChartDataSet::scrollDomain(int dx,int dy,const QSizeF& size)
305 {
309 {
306 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
310 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
307 while (i.hasNext()) {
311 while (i.hasNext()) {
308 i.next();
312 i.next();
309 i.value()->move(dx,dy,size);
313 i.value()->move(dx,dy,size);
310 }
314 }
311 }
315 }
312
316
313 #include "moc_chartdataset_p.cpp"
317 #include "moc_chartdataset_p.cpp"
314
318
315 QTCOMMERCIALCHART_END_NAMESPACE
319 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,176 +1,185
1 #include "domain_p.h"
1 #include "domain_p.h"
2
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
4
5 Domain::Domain(QObject* parent):QObject(parent),
5 Domain::Domain(QObject* parent):QObject(parent),
6 m_minX(0),
6 m_minX(0),
7 m_maxX(0),
7 m_maxX(0),
8 m_minY(0),
8 m_minY(0),
9 m_maxY(0),
9 m_maxY(0),
10 m_tickXCount(5),
10 m_tickXCount(5),
11 m_tickYCount(5)
11 m_tickYCount(5)
12 {
12 {
13 }
13 }
14
14
15 Domain::~Domain()
15 Domain::~Domain()
16 {
16 {
17 }
17 }
18
18
19 void Domain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY)
19 void Domain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY)
20 {
20 {
21 setRange(minX, maxX, minY, maxY, m_tickXCount, m_tickYCount);
22 }
23
24 void Domain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY, int tickXCount,int tickYCount)
25 {
26 bool changed = false;
21 bool changed = false;
27
22
28 if(m_minX!=minX || m_maxX!=maxX || m_tickXCount!=tickXCount)
23 if(m_minX!=minX || m_maxX!=maxX)
29 {
24 {
30 m_minX=minX;
25 m_minX=minX;
31 m_maxX=maxX;
26 m_maxX=maxX;
32 m_tickXCount=tickXCount;
33 changed=true;
27 changed=true;
34 emit rangeXChanged(minX,maxX, m_tickXCount);
28 emit rangeXChanged(minX,maxX, m_tickXCount);
35 }
29 }
36
30
37 if(m_minY!=minY || m_maxY!=maxY || m_tickYCount!=tickYCount){
31 if(m_minY!=minY || m_maxY!=maxY){
38 m_minY=minY;
32 m_minY=minY;
39 m_maxY=maxY;
33 m_maxY=maxY;
40 m_tickYCount=tickYCount;
41 changed=true;
34 changed=true;
42 emit rangeYChanged(minY,maxY, m_tickYCount);
35 emit rangeYChanged(minY,maxY, m_tickYCount);
43 }
36 }
44
37
45 if(changed){
38 if(changed){
46 emit domainChanged(m_minX, m_maxX, m_minY, m_maxY);
39 emit domainChanged(m_minX, m_maxX, m_minY, m_maxY);
47 }
40 }
48 }
41 }
49 void Domain::setRangeX(qreal min, qreal max)
42 void Domain::setRangeX(qreal min, qreal max)
50 {
43 {
51 setRange(min,max,m_minY, m_maxY);
44 setRange(min,max,m_minY, m_maxY);
52 }
45 }
53 void Domain::setRangeY(qreal min, qreal max)
46 void Domain::setRangeY(qreal min, qreal max)
54 {
47 {
55 setRange(m_minX, m_maxX, min, max);
48 setRange(m_minX, m_maxX, min, max);
56 }
49 }
57
50
58 void Domain::setMinX(qreal min)
51 void Domain::setMinX(qreal min)
59 {
52 {
60 setRange(min, m_maxX, m_minY, m_maxY);
53 setRange(min, m_maxX, m_minY, m_maxY);
61 }
54 }
62
55
63 void Domain::setMaxX(qreal max)
56 void Domain::setMaxX(qreal max)
64 {
57 {
65 setRange(m_minX, max, m_minY, m_maxY);
58 setRange(m_minX, max, m_minY, m_maxY);
66 }
59 }
67
60
68 void Domain::setMinY(qreal min)
61 void Domain::setMinY(qreal min)
69 {
62 {
70 setRange(m_minX, m_maxX, min, m_maxY);
63 setRange(m_minX, m_maxX, min, m_maxY);
71 }
64 }
72
65
73 void Domain::setMaxY(qreal max)
66 void Domain::setMaxY(qreal max)
74 {
67 {
75 setRange(m_minX, m_maxX, m_minY, max);
68 setRange(m_minX, m_maxX, m_minY, max);
76
69
77 }
70 }
78
71
79 qreal Domain::spanX() const
72 qreal Domain::spanX() const
80 {
73 {
81 Q_ASSERT(m_maxX >= m_minX);
74 Q_ASSERT(m_maxX >= m_minX);
82 return m_maxX - m_minX;
75 return m_maxX - m_minX;
83 }
76 }
84
77
85 qreal Domain::spanY() const
78 qreal Domain::spanY() const
86 {
79 {
87 Q_ASSERT(m_maxY >= m_minY);
80 Q_ASSERT(m_maxY >= m_minY);
88 return m_maxY - m_minY;
81 return m_maxY - m_minY;
89 }
82 }
90
83
91 bool Domain::isEmpty() const
84 bool Domain::isEmpty() const
92 {
85 {
93 return spanX()==0 || spanY()==0;
86 return spanX()==0 || spanY()==0;
94 }
87 }
95
88
96 void Domain::zoomIn(const QRectF& rect, const QSizeF& size)
89 void Domain::zoomIn(const QRectF& rect, const QSizeF& size)
97 {
90 {
98 qreal dx = spanX() / size.width();
91 qreal dx = spanX() / size.width();
99 qreal dy = spanY() / size.height();
92 qreal dy = spanY() / size.height();
100
93
101 m_maxX = m_minX + dx * rect.right();
94 m_maxX = m_minX + dx * rect.right();
102 m_minX = m_minX + dx * rect.left();
95 m_minX = m_minX + dx * rect.left();
103 m_minY = m_maxY - dy * rect.bottom();
96 m_minY = m_maxY - dy * rect.bottom();
104 m_maxY = m_maxY - dy * rect.top();
97 m_maxY = m_maxY - dy * rect.top();
105
98
106 emit domainChanged(m_minX, m_maxX, m_minY, m_maxY);
99 emit domainChanged(m_minX, m_maxX, m_minY, m_maxY);
107 emit rangeXChanged(m_minX, m_maxX, m_tickXCount);
100 emit rangeXChanged(m_minX, m_maxX, m_tickXCount);
108 emit rangeYChanged(m_minY, m_maxY, m_tickYCount);
101 emit rangeYChanged(m_minY, m_maxY, m_tickYCount);
109 }
102 }
110
103
111 void Domain::zoomOut(const QRectF& rect, const QSizeF& size)
104 void Domain::zoomOut(const QRectF& rect, const QSizeF& size)
112 {
105 {
113 qreal dx = spanX() / rect.width();
106 qreal dx = spanX() / rect.width();
114 qreal dy = spanY() / rect.height();
107 qreal dy = spanY() / rect.height();
115
108
116 m_minX = m_maxX - dx * rect.right();
109 m_minX = m_maxX - dx * rect.right();
117 m_maxX = m_minX + dx * size.width();
110 m_maxX = m_minX + dx * size.width();
118 m_maxY = m_minY + dy * rect.bottom();
111 m_maxY = m_minY + dy * rect.bottom();
119 m_minY = m_maxY - dy * size.height();
112 m_minY = m_maxY - dy * size.height();
120
113
121 emit domainChanged(m_minX, m_maxX, m_minY, m_maxY);
114 emit domainChanged(m_minX, m_maxX, m_minY, m_maxY);
122 emit rangeXChanged(m_minX, m_maxX, m_tickXCount);
115 emit rangeXChanged(m_minX, m_maxX, m_tickXCount);
123 emit rangeYChanged(m_minY, m_maxY, m_tickYCount);
116 emit rangeYChanged(m_minY, m_maxY, m_tickYCount);
124 }
117 }
125
118
126 void Domain::move(int dx,int dy,const QSizeF& size)
119 void Domain::move(int dx,int dy,const QSizeF& size)
127 {
120 {
128 qreal x = spanX() / size.width();
121 qreal x = spanX() / size.width();
129 qreal y = spanY() / size.height();
122 qreal y = spanY() / size.height();
130
123
131 if(dx!=0){
124 if(dx!=0){
132 m_minX = m_minX + x * dx;
125 m_minX = m_minX + x * dx;
133 m_maxX = m_maxX + x * dx;
126 m_maxX = m_maxX + x * dx;
134 emit rangeXChanged(m_minX, m_maxX, m_tickXCount);
127 emit rangeXChanged(m_minX, m_maxX, m_tickXCount);
135 }
128 }
136 if(dy!=0){
129 if(dy!=0){
137 m_minY = m_minY + y * dy;
130 m_minY = m_minY + y * dy;
138 m_maxY = m_maxY + y * dy;
131 m_maxY = m_maxY + y * dy;
139 emit rangeYChanged(m_minY, m_maxY, m_tickYCount);
132 emit rangeYChanged(m_minY, m_maxY, m_tickYCount);
140 }
133 }
141
134
142 emit domainChanged(m_minX, m_maxX, m_minY, m_maxY);
135 emit domainChanged(m_minX, m_maxX, m_minY, m_maxY);
143 }
136 }
144
137
145 void Domain::handleAxisRangeXChanged(qreal min,qreal max,int tickCount)
138 void Domain::handleAxisRangeXChanged(qreal min,qreal max)
139 {
140 setRange(min,max,m_minY, m_maxY);
141 }
142
143 void Domain::handleAxisRangeYChanged(qreal min,qreal max)
146 {
144 {
147 setRange(min,max,m_minY, m_maxY,tickCount,m_tickYCount);
145 setRange(m_minX, m_maxX, min, max);
148 }
146 }
149
147
150 void Domain::handleAxisRangeYChanged(qreal min,qreal max,int tickCount)
148 void Domain::handleAxisXTicksCountChanged(int tickCount)
151 {
149 {
152 setRange(m_minX, m_maxX, min, max,m_tickXCount, tickCount);
150 if(m_tickXCount!=tickCount){
151 m_tickXCount=tickCount;
152 emit rangeXChanged(m_minX,m_maxX, m_tickXCount);
153 }
154 }
155
156 void Domain::handleAxisYTicksCountChanged(int tickCount)
157 {
158 if(m_tickYCount!=tickCount){
159 m_tickYCount=tickCount;
160 emit rangeYChanged(m_minY,m_maxY, m_tickYCount);
161 }
153 }
162 }
154
163
155 bool operator== (const Domain &domain1, const Domain &domain2)
164 bool operator== (const Domain &domain1, const Domain &domain2)
156 {
165 {
157 return (domain1.m_maxX == domain2.m_maxX &&
166 return (domain1.m_maxX == domain2.m_maxX &&
158 domain1.m_maxY == domain2.m_maxY &&
167 domain1.m_maxY == domain2.m_maxY &&
159 domain1.m_minX == domain2.m_minX &&
168 domain1.m_minX == domain2.m_minX &&
160 domain1.m_minY == domain2.m_minY);
169 domain1.m_minY == domain2.m_minY);
161 }
170 }
162
171
163 bool operator!= (const Domain &domain1, const Domain &domain2)
172 bool operator!= (const Domain &domain1, const Domain &domain2)
164 {
173 {
165 return !(domain1 == domain2);
174 return !(domain1 == domain2);
166 }
175 }
167
176
168 QDebug operator<<(QDebug dbg, const Domain &domain)
177 QDebug operator<<(QDebug dbg, const Domain &domain)
169 {
178 {
170 dbg.nospace() << "Domain("<<domain.m_minX<<','<<domain.m_maxX<<','<<domain.m_minY<<','<<domain.m_maxY<<')';
179 dbg.nospace() << "Domain("<<domain.m_minX<<','<<domain.m_maxX<<','<<domain.m_minY<<','<<domain.m_maxY<<')';
171 return dbg.maybeSpace();
180 return dbg.maybeSpace();
172 }
181 }
173
182
174 #include "moc_domain_p.cpp"
183 #include "moc_domain_p.cpp"
175
184
176 QTCOMMERCIALCHART_END_NAMESPACE
185 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,65 +1,66
1 #ifndef DOMAIN_H_
1 #ifndef DOMAIN_H_
2 #define DOMAIN_H_
2 #define DOMAIN_H_
3 #include "qchartglobal.h"
3 #include "qchartglobal.h"
4 #include <QRectF>
4 #include <QRectF>
5 #include <QSizeF>
5 #include <QSizeF>
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 class Domain: public QObject {
9 class Domain: public QObject {
10 Q_OBJECT
10 Q_OBJECT
11 public:
11 public:
12 explicit Domain(QObject* object=0);
12 explicit Domain(QObject* object=0);
13 virtual ~Domain();
13 virtual ~Domain();
14
14
15 void setRange(qreal minX, qreal maxX, qreal minY, qreal maxY);
15 void setRange(qreal minX, qreal maxX, qreal minY, qreal maxY);
16 void setRange(qreal minX, qreal maxX, qreal minY, qreal maxY,int tickXCount,int tickYCount);
17 void setRangeX(qreal min, qreal max);
16 void setRangeX(qreal min, qreal max);
18 void setRangeY(qreal min, qreal max);
17 void setRangeY(qreal min, qreal max);
19 void setMinX(qreal min);
18 void setMinX(qreal min);
20 void setMaxX(qreal max);
19 void setMaxX(qreal max);
21 void setMinY(qreal min);
20 void setMinY(qreal min);
22 void setMaxY(qreal max);
21 void setMaxY(qreal max);
23
22
24 qreal minX() const {return m_minX;};
23 qreal minX() const {return m_minX;};
25 qreal maxX() const {return m_maxX;};
24 qreal maxX() const {return m_maxX;};
26 qreal minY() const {return m_minY;};
25 qreal minY() const {return m_minY;};
27 qreal maxY() const {return m_maxY;};
26 qreal maxY() const {return m_maxY;};
28
27
29 qreal spanX() const;
28 qreal spanX() const;
30 qreal spanY() const;
29 qreal spanY() const;
31 bool isEmpty() const;
30 bool isEmpty() const;
32
31
33 int tickXCount() const {return m_tickXCount;}
32 int tickXCount() const {return m_tickXCount;}
34 int tickYCount() const {return m_tickYCount;}
33 int tickYCount() const {return m_tickYCount;}
35
34
36 friend bool operator== (const Domain &domain1, const Domain &domain2);
35 friend bool operator== (const Domain &domain1, const Domain &domain2);
37 friend bool operator!= (const Domain &domain1, const Domain &domain2);
36 friend bool operator!= (const Domain &domain1, const Domain &domain2);
38 friend QDebug operator<<(QDebug dbg, const Domain &domain);
37 friend QDebug operator<<(QDebug dbg, const Domain &domain);
39
38
40 void zoomIn(const QRectF& rect, const QSizeF& size);
39 void zoomIn(const QRectF& rect, const QSizeF& size);
41 void zoomOut(const QRectF& rect, const QSizeF& size);
40 void zoomOut(const QRectF& rect, const QSizeF& size);
42 void move(int dx,int dy,const QSizeF& size);
41 void move(int dx,int dy,const QSizeF& size);
43
42
44 signals:
43 signals:
45 void domainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
44 void domainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
46 void rangeXChanged(qreal min, qreal max, int tickXCount);
45 void rangeXChanged(qreal min, qreal max, int tickXCount);
47 void rangeYChanged(qreal min, qreal max, int tickYCount);
46 void rangeYChanged(qreal min, qreal max, int tickYCount);
48
47
49
48
50 public slots:
49 public slots:
51 void handleAxisRangeXChanged(qreal min,qreal max,int tickCount);
50 void handleAxisRangeXChanged(qreal min,qreal max);
52 void handleAxisRangeYChanged(qreal min,qreal max,int tickCount);
51 void handleAxisRangeYChanged(qreal min,qreal max);
52 void handleAxisXTicksCountChanged(int tickCount);
53 void handleAxisYTicksCountChanged(int tickCount);
53
54
54 private:
55 private:
55 qreal m_minX;
56 qreal m_minX;
56 qreal m_maxX;
57 qreal m_maxX;
57 qreal m_minY;
58 qreal m_minY;
58 qreal m_maxY;
59 qreal m_maxY;
59 int m_tickXCount;
60 int m_tickXCount;
60 int m_tickYCount;
61 int m_tickYCount;
61 };
62 };
62
63
63 QTCOMMERCIALCHART_END_NAMESPACE
64 QTCOMMERCIALCHART_END_NAMESPACE
64
65
65 #endif
66 #endif
General Comments 0
You need to be logged in to leave comments. Login now