##// END OF EJS Templates
Refacotr axisitem to handle ticks changes
Michal Klocek -
r452:47a9fc437f2c
parent child
Show More
@@ -1,74 +1,77
1 #include "axisanimationitem_p.h"
1 #include "axisanimationitem_p.h"
2 #include <QTimer>
2 #include <QTimer>
3
3
4 Q_DECLARE_METATYPE(QVector<qreal>)
4 Q_DECLARE_METATYPE(QVector<qreal>)
5
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
7
8 const static int duration = 500;
8 const static int duration = 500;
9
9
10 AxisAnimationItem::AxisAnimationItem(QChartAxis* axis,AxisType type,QGraphicsItem* parent) :
10 AxisAnimationItem::AxisAnimationItem(QChartAxis* axis,AxisType type,QGraphicsItem* parent) :
11 AxisItem(axis,type,parent),
11 AxisItem(axis,type,parent),
12 m_animation(new AxisAnimator(this,this))
12 m_animation(new AxisAnimator(this,this))
13 {
13 {
14 }
14 }
15
15
16 AxisAnimationItem::~AxisAnimationItem()
16 AxisAnimationItem::~AxisAnimationItem()
17 {
17 {
18 }
18 }
19
19
20 void AxisAnimationItem::updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout)
20 void AxisAnimationItem::updateItem()
21 {
21 {
22 QVector<qreal> oldLayout = layout();
23 AxisItem::updateItem();
24 QVector<qreal> newLayout = layout();
25
22 if(newLayout.count()==0) return;
26 if(newLayout.count()==0) return;
23 oldLayout.resize(newLayout.size());
27 oldLayout.resize(newLayout.size());
24
28
25 if(m_animation->state()!=QAbstractAnimation::Stopped){
29 if(m_animation->state()!=QAbstractAnimation::Stopped){
26 m_animation->stop();
30 m_animation->stop();
27 }
31 }
28
32
29 m_animation->setDuration(duration);
33 m_animation->setDuration(duration);
30 m_animation->setEasingCurve(QEasingCurve::InOutBack);
34 m_animation->setEasingCurve(QEasingCurve::InOutBack);
31 m_animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
35 m_animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
32 m_animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
36 m_animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
33 QTimer::singleShot(0,m_animation,SLOT(start()));
37 QTimer::singleShot(0,m_animation,SLOT(start()));
34 oldLayout = newLayout;
35 }
38 }
36
39
37 void AxisAnimationItem::setLabelsAngle(int angle)
40 void AxisAnimationItem::setLabelsAngle(int angle)
38 {
41 {
39 AxisItem::setLabelsAngle(angle);
42 AxisItem::setLabelsAngle(angle);
40 }
43 }
41
44
42 AxisAnimator::AxisAnimator(AxisItem *axis,QObject *parent): QVariantAnimation(parent),
45 AxisAnimator::AxisAnimator(AxisItem *axis,QObject *parent): QVariantAnimation(parent),
43 m_axis(axis)
46 m_axis(axis)
44 {
47 {
45 }
48 }
46
49
47 AxisAnimator::~AxisAnimator()
50 AxisAnimator::~AxisAnimator()
48 {
51 {
49 }
52 }
50
53
51 QVariant AxisAnimator::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const
54 QVariant AxisAnimator::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const
52 {
55 {
53 QVector<qreal> startVector = qVariantValue<QVector<qreal> >(start);
56 QVector<qreal> startVector = qVariantValue<QVector<qreal> >(start);
54 QVector<qreal> endVecotr = qVariantValue<QVector<qreal> >(end);
57 QVector<qreal> endVecotr = qVariantValue<QVector<qreal> >(end);
55 QVector<qreal> result;
58 QVector<qreal> result;
56 Q_ASSERT(startVector.count() == endVecotr.count());
59 Q_ASSERT(startVector.count() == endVecotr.count());
57
60
58 for(int i =0 ;i< startVector.count();i++){
61 for(int i =0 ;i< startVector.count();i++){
59 qreal value = startVector[i] + ((endVecotr[i]- startVector[i]) * progress);//qBound(0.0, progress, 1.0));
62 qreal value = startVector[i] + ((endVecotr[i]- startVector[i]) * progress);//qBound(0.0, progress, 1.0));
60 result << value;
63 result << value;
61 }
64 }
62 return qVariantFromValue(result);
65 return qVariantFromValue(result);
63 }
66 }
64
67
65
68
66 void AxisAnimator::updateCurrentValue (const QVariant & value )
69 void AxisAnimator::updateCurrentValue (const QVariant & value )
67 {
70 {
68 QVector<qreal> vector = qVariantValue<QVector<qreal> >(value);
71 QVector<qreal> vector = qVariantValue<QVector<qreal> >(value);
69 m_axis->applyLayout(vector);
72 m_axis->setLayout(vector);
70 }
73 }
71
74
72 #include "moc_axisanimationitem_p.cpp"
75 #include "moc_axisanimationitem_p.cpp"
73
76
74 QTCOMMERCIALCHART_END_NAMESPACE
77 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,45 +1,45
1 #ifndef AXISANIMATIONITEM_H_
1 #ifndef AXISANIMATIONITEM_H_
2 #define AXISANIMATIONITEM_H_
2 #define AXISANIMATIONITEM_H_
3
3
4 #include "domain_p.h"
4 #include "domain_p.h"
5 #include "axisitem_p.h"
5 #include "axisitem_p.h"
6 #include <QGraphicsItem>
6 #include <QGraphicsItem>
7 #include <QVariantAnimation>
7 #include <QVariantAnimation>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 class AxisAnimator;
11 class AxisAnimator;
12
12
13 class AxisAnimationItem : public AxisItem
13 class AxisAnimationItem : public AxisItem
14 {
14 {
15 Q_OBJECT
15 Q_OBJECT
16
16
17 public:
17 public:
18 AxisAnimationItem(QChartAxis* axis,AxisType type = X_AXIS,QGraphicsItem* parent = 0);
18 AxisAnimationItem(QChartAxis* axis,AxisType type = X_AXIS,QGraphicsItem* parent = 0);
19 ~AxisAnimationItem();
19 ~AxisAnimationItem();
20
20
21 void setLabelsAngle(int angle);
21 void setLabelsAngle(int angle);
22
22
23 protected:
23 protected:
24 virtual void updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout);
24 virtual void updateItem();
25 private:
25 private:
26 AxisAnimator *m_animation;
26 AxisAnimator *m_animation;
27 };
27 };
28
28
29 class AxisAnimator: public QVariantAnimation
29 class AxisAnimator: public QVariantAnimation
30 {
30 {
31 public:
31 public:
32 AxisAnimator(AxisItem *axis,QObject *parent = 0);
32 AxisAnimator(AxisItem *axis,QObject *parent = 0);
33 ~AxisAnimator();
33 ~AxisAnimator();
34 protected:
34 protected:
35 virtual QVariant interpolated (const QVariant & from, const QVariant & to, qreal progress ) const;
35 virtual QVariant interpolated (const QVariant & from, const QVariant & to, qreal progress ) const;
36 virtual void updateCurrentValue (const QVariant & value );
36 virtual void updateCurrentValue (const QVariant & value );
37 private:
37 private:
38 AxisItem* m_axis;
38 AxisItem* m_axis;
39 };
39 };
40
40
41 QTCOMMERCIALCHART_END_NAMESPACE
41 QTCOMMERCIALCHART_END_NAMESPACE
42
42
43
43
44
44
45 #endif /* AXISITEM_H_ */
45 #endif /* AXISITEM_H_ */
@@ -1,377 +1,393
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 <QPainter>
4 #include <QPainter>
5 #include <QDebug>
5 #include <QDebug>
6
6
7 static int label_padding = 5;
7 static int label_padding = 5;
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 AxisItem::AxisItem(QChartAxis* axis,AxisType type,QGraphicsItem* parent) :
11 AxisItem::AxisItem(QChartAxis* axis,AxisType type,QGraphicsItem* parent) :
12 ChartItem(parent),
12 ChartItem(parent),
13 m_chartAxis(axis),
13 m_chartAxis(axis),
14 m_type(type),
14 m_type(type),
15 m_labelsAngle(0),
15 m_labelsAngle(0),
16 m_grid(parent),
16 m_grid(parent),
17 m_shades(parent),
17 m_shades(parent),
18 m_labels(parent),
18 m_labels(parent),
19 m_axis(parent)
19 m_axis(parent)
20 {
20 {
21 //initial initialization
21 //initial initialization
22 m_axis.setZValue(ChartPresenter::AxisZValue);
22 m_axis.setZValue(ChartPresenter::AxisZValue);
23 m_shades.setZValue(ChartPresenter::ShadesZValue);
23 m_shades.setZValue(ChartPresenter::ShadesZValue);
24 m_grid.setZValue(ChartPresenter::GridZValue);
24 m_grid.setZValue(ChartPresenter::GridZValue);
25 setFlags(QGraphicsItem::ItemHasNoContents);
25 setFlags(QGraphicsItem::ItemHasNoContents);
26
26
27 QObject::connect(m_chartAxis,SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
27 QObject::connect(m_chartAxis,SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
28 }
28 }
29
29
30 AxisItem::~AxisItem()
30 AxisItem::~AxisItem()
31 {
31 {
32 }
32 }
33
33
34 QRectF AxisItem::boundingRect() const
34 QRectF AxisItem::boundingRect() const
35 {
35 {
36 return QRectF();
36 return QRectF();
37 }
37 }
38
38
39 void AxisItem::createItems(int count)
39 void AxisItem::createItems(int count)
40 {
40 {
41 if(m_axis.children().size()==0)
41 if(m_axis.children().size()==0)
42 m_axis.addToGroup(new QGraphicsLineItem());
42 m_axis.addToGroup(new QGraphicsLineItem());
43 for (int i = 0; i < count; ++i) {
43 for (int i = 0; i < count; ++i) {
44 m_grid.addToGroup(new QGraphicsLineItem());
44 m_grid.addToGroup(new QGraphicsLineItem());
45 m_labels.addToGroup(new QGraphicsSimpleTextItem());
45 m_labels.addToGroup(new QGraphicsSimpleTextItem());
46 if(m_grid.childItems().size()%2) m_shades.addToGroup(new QGraphicsRectItem());
46 if(m_grid.childItems().size()%2) m_shades.addToGroup(new QGraphicsRectItem());
47 m_axis.addToGroup(new QGraphicsLineItem());
47 m_axis.addToGroup(new QGraphicsLineItem());
48 }
48 }
49 }
49 }
50
50
51 void AxisItem::clear(int count)
51 void AxisItem::clear(int count)
52 {
52 {
53 QList<QGraphicsItem *> lines = m_grid.childItems();
53 QList<QGraphicsItem *> lines = m_grid.childItems();
54 QList<QGraphicsItem *> labels = m_labels.childItems();
54 QList<QGraphicsItem *> labels = m_labels.childItems();
55 QList<QGraphicsItem *> shades = m_shades.childItems();
55 QList<QGraphicsItem *> shades = m_shades.childItems();
56 QList<QGraphicsItem *> axis = m_axis.childItems();
56 QList<QGraphicsItem *> axis = m_axis.childItems();
57
57
58 for (int i = 0; i < count; ++i) {
58 for (int i = 0; i < count; ++i) {
59 delete(lines.takeLast());
59 delete(lines.takeLast());
60 delete(labels.takeLast());
60 delete(labels.takeLast());
61 if(lines.size()%2) delete(shades.takeLast());
61 if(lines.size()%2) delete(shades.takeLast());
62 delete(axis.takeLast());
62 delete(axis.takeLast());
63 }
63 }
64 }
64 }
65
65
66 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
66 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
67 {
67 {
68 Q_UNUSED(painter);
68 Q_UNUSED(painter);
69 Q_UNUSED(option);
69 Q_UNUSED(option);
70 Q_UNUSED(widget);
70 Q_UNUSED(widget);
71 }
71 }
72
72
73 void AxisItem::updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout)
73 void AxisItem::updateItem()
74 {
74 {
75 if(newLayout.count()==0) return;
75 QStringList labels = createLabels(m_ticks,m_min,m_max);
76 applyLayout(newLayout);
76
77 oldLayout=newLayout;
77 int diff = m_thicksList.size() - labels.size();
78
79 if(diff>0) {
80 clear(diff);
81 }
82 else if(diff<0) {
83 createItems(-diff);
84 }
85
86 if(diff!=0) handleAxisUpdated();
87
88 m_thicksList=labels;
89
90 QVector<qreal> layout = calculateLayout();
91 if(layout.count()==0) return;
92 setLayout(layout);
93
78 }
94 }
79
95
80 QStringList AxisItem::createLabels(int ticks, qreal min, qreal max)
96 QStringList AxisItem::createLabels(int ticks, qreal min, qreal max)
81 {
97 {
82
98
83
99
84 Q_ASSERT(max>=min);
100 Q_ASSERT(max>=min);
85
101
86 QStringList labels;
102 QStringList labels;
87
103
88 //int ticks = axis->ticksCount()-1;
104 //int ticks = axis->ticksCount()-1;
89
105
90 for(int i=0; i<= ticks; i++) {
106 for(int i=0; i<= ticks; i++) {
91 qreal value = min + (i * (max - min)/ ticks);
107 qreal value = min + (i * (max - min)/ ticks);
92 QString label ;//= axis->axisTickLabel(value);
108 QString label ;//= axis->axisTickLabel(value);
93 if(label.isEmpty()) {
109 if(label.isEmpty()) {
94 labels << QString::number(value);
110 labels << QString::number(value);
95 }
111 }
96 else {
112 else {
97 labels << label;
113 labels << label;
98 }
114 }
99 }
115 }
100 return labels;
116 return labels;
101 }
117 }
102
118
103 void AxisItem::setAxisOpacity(qreal opacity)
119 void AxisItem::setAxisOpacity(qreal opacity)
104 {
120 {
105 m_axis.setOpacity(opacity);
121 m_axis.setOpacity(opacity);
106 }
122 }
107
123
108 qreal AxisItem::axisOpacity() const
124 qreal AxisItem::axisOpacity() const
109 {
125 {
110 return m_axis.opacity();
126 return m_axis.opacity();
111 }
127 }
112
128
113 void AxisItem::setGridOpacity(qreal opacity)
129 void AxisItem::setGridOpacity(qreal opacity)
114 {
130 {
115 m_grid.setOpacity(opacity);
131 m_grid.setOpacity(opacity);
116 }
132 }
117
133
118 qreal AxisItem::gridOpacity() const
134 qreal AxisItem::gridOpacity() const
119 {
135 {
120 return m_grid.opacity();
136 return m_grid.opacity();
121 }
137 }
122
138
123 void AxisItem::setLabelsOpacity(qreal opacity)
139 void AxisItem::setLabelsOpacity(qreal opacity)
124 {
140 {
125 m_labels.setOpacity(opacity);
141 m_labels.setOpacity(opacity);
126 }
142 }
127
143
128 qreal AxisItem::labelsOpacity() const
144 qreal AxisItem::labelsOpacity() const
129 {
145 {
130 return m_labels.opacity();
146 return m_labels.opacity();
131 }
147 }
132
148
133 void AxisItem::setShadesOpacity(qreal opacity)
149 void AxisItem::setShadesOpacity(qreal opacity)
134 {
150 {
135 m_shades.setOpacity(opacity);
151 m_shades.setOpacity(opacity);
136 }
152 }
137
153
138 qreal AxisItem::shadesOpacity() const
154 qreal AxisItem::shadesOpacity() const
139 {
155 {
140 return m_shades.opacity();
156 return m_shades.opacity();
141 }
157 }
142
158
143 void AxisItem::setLabelsAngle(int angle)
159 void AxisItem::setLabelsAngle(int angle)
144 {
160 {
145 foreach(QGraphicsItem* item , m_labels.childItems()) {
161 foreach(QGraphicsItem* item , m_labels.childItems()) {
146 QPointF center = item->boundingRect().center();
162 QPointF center = item->boundingRect().center();
147 item->setRotation(angle);
163 item->setRotation(angle);
148 }
164 }
149
165
150 m_labelsAngle=angle;
166 m_labelsAngle=angle;
151 }
167 }
152
168
153 void AxisItem::setLabelsPen(const QPen& pen)
169 void AxisItem::setLabelsPen(const QPen& pen)
154 {
170 {
155 foreach(QGraphicsItem* item , m_labels.childItems()) {
171 foreach(QGraphicsItem* item , m_labels.childItems()) {
156 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
172 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
157 }
173 }
158 }
174 }
159
175
160 void AxisItem::setLabelsBrush(const QBrush& brush)
176 void AxisItem::setLabelsBrush(const QBrush& brush)
161 {
177 {
162 foreach(QGraphicsItem* item , m_labels.childItems()) {
178 foreach(QGraphicsItem* item , m_labels.childItems()) {
163 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
179 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
164 }
180 }
165 }
181 }
166
182
167 void AxisItem::setLabelsFont(const QFont& font)
183 void AxisItem::setLabelsFont(const QFont& font)
168 {
184 {
169 foreach(QGraphicsItem* item , m_labels.childItems()) {
185 foreach(QGraphicsItem* item , m_labels.childItems()) {
170 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
186 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
171 }
187 }
172 }
188 }
173
189
174 void AxisItem::setShadesBrush(const QBrush& brush)
190 void AxisItem::setShadesBrush(const QBrush& brush)
175 {
191 {
176 foreach(QGraphicsItem* item , m_shades.childItems()) {
192 foreach(QGraphicsItem* item , m_shades.childItems()) {
177 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
193 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
178 }
194 }
179 }
195 }
180
196
181 void AxisItem::setShadesPen(const QPen& pen)
197 void AxisItem::setShadesPen(const QPen& pen)
182 {
198 {
183 foreach(QGraphicsItem* item , m_shades.childItems()) {
199 foreach(QGraphicsItem* item , m_shades.childItems()) {
184 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
200 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
185 }
201 }
186 }
202 }
187
203
188 void AxisItem::setAxisPen(const QPen& pen)
204 void AxisItem::setAxisPen(const QPen& pen)
189 {
205 {
190 foreach(QGraphicsItem* item , m_axis.childItems()) {
206 foreach(QGraphicsItem* item , m_axis.childItems()) {
191 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
207 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
192 }
208 }
193 }
209 }
194
210
195 void AxisItem::setGridPen(const QPen& pen)
211 void AxisItem::setGridPen(const QPen& pen)
196 {
212 {
197 foreach(QGraphicsItem* item , m_grid.childItems()) {
213 foreach(QGraphicsItem* item , m_grid.childItems()) {
198 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
214 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
199 }
215 }
200 }
216 }
201
217
202 QVector<qreal> AxisItem::calculateLayout() const
218 QVector<qreal> AxisItem::calculateLayout() const
203 {
219 {
204 QVector<qreal> points;
220 QVector<qreal> points;
205 points.resize(m_thicksList.size());
221 points.resize(m_thicksList.size());
206
222
207 switch (m_type)
223 switch (m_type)
208 {
224 {
209 case X_AXIS:
225 case X_AXIS:
210 {
226 {
211 const qreal deltaX = m_rect.width()/(m_thicksList.size()-1);
227 const qreal deltaX = m_rect.width()/(m_thicksList.size()-1);
212 for (int i = 0; i < m_thicksList.size(); ++i) {
228 for (int i = 0; i < m_thicksList.size(); ++i) {
213 int x = i * deltaX + m_rect.left();
229 int x = i * deltaX + m_rect.left();
214 points[i] = x;
230 points[i] = x;
215 }
231 }
216 }
232 }
217 break;
233 break;
218 case Y_AXIS:
234 case Y_AXIS:
219 {
235 {
220 const qreal deltaY = m_rect.height()/(m_thicksList.size()-1);
236 const qreal deltaY = m_rect.height()/(m_thicksList.size()-1);
221 for (int i = 0; i < m_thicksList.size(); ++i) {
237 for (int i = 0; i < m_thicksList.size(); ++i) {
222 int y = i * -deltaY + m_rect.bottom();
238 int y = i * -deltaY + m_rect.bottom();
223 points[i] = y;
239 points[i] = y;
224 }
240 }
225 }
241 }
226 break;
242 break;
227 }
243 }
228 return points;
244 return points;
229 }
245 }
230
246
231 void AxisItem::applyLayout(const QVector<qreal>& points)
247 void AxisItem::setLayout(const QVector<qreal>& layout)
232 {
248 {
233 Q_ASSERT(points.size() == m_thicksList.size());
234
249
235 QList<QGraphicsItem *> lines = m_grid.childItems();
250 QList<QGraphicsItem *> lines = m_grid.childItems();
236 QList<QGraphicsItem *> labels = m_labels.childItems();
251 QList<QGraphicsItem *> labels = m_labels.childItems();
237 QList<QGraphicsItem *> shades = m_shades.childItems();
252 QList<QGraphicsItem *> shades = m_shades.childItems();
238 QList<QGraphicsItem *> axis = m_axis.childItems();
253 QList<QGraphicsItem *> axis = m_axis.childItems();
239
254
240 Q_ASSERT(labels.size() == m_thicksList.size());
255 Q_ASSERT(labels.size() == m_thicksList.size());
256 Q_ASSERT(layout.size() == m_thicksList.size());
241
257
242 switch (m_type)
258 switch (m_type)
243 {
259 {
244 case X_AXIS:
260 case X_AXIS:
245 {
261 {
246 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
262 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
247 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
263 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
248
264
249 for (int i = 0; i < points.size(); ++i) {
265 for (int i = 0; i < layout.size(); ++i) {
250 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
266 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
251 lineItem->setLine(points[i], m_rect.top(), points[i], m_rect.bottom());
267 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
252 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
268 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
253 labelItem->setText(m_thicksList.at(i));
269 labelItem->setText(m_thicksList.at(i));
254 QPointF center = labelItem->boundingRect().center();
270 QPointF center = labelItem->boundingRect().center();
255 labelItem->setTransformOriginPoint(center.x(), center.y());
271 labelItem->setTransformOriginPoint(center.x(), center.y());
256 labelItem->setPos(points[i] - center.x(), m_rect.bottom() + label_padding);
272 labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding);
257 if(i%2){
273 if(i%2){
258 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
274 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
259 rectItem->setRect(points[i],m_rect.top(),points[i+1]-points[i],m_rect.height());
275 rectItem->setRect(layout[i],m_rect.top(),layout[i+1]-layout[i],m_rect.height());
260 }
276 }
261 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
277 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
262 lineItem->setLine(points[i],m_rect.bottom(),points[i],m_rect.bottom()+5);
278 lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5);
263 }
279 }
264 }
280 }
265 break;
281 break;
266
282
267 case Y_AXIS:
283 case Y_AXIS:
268 {
284 {
269 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
285 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
270 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
286 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
271
287
272 for (int i = 0; i < points.size(); ++i) {
288 for (int i = 0; i < layout.size(); ++i) {
273 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
289 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
274 lineItem->setLine(m_rect.left() , points[i], m_rect.right(), points[i]);
290 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
275 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
291 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
276 labelItem->setText(m_thicksList.at(i));
292 labelItem->setText(m_thicksList.at(i));
277 QPointF center = labelItem->boundingRect().center();
293 QPointF center = labelItem->boundingRect().center();
278 labelItem->setTransformOriginPoint(center.x(), center.y());
294 labelItem->setTransformOriginPoint(center.x(), center.y());
279 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , points[i]-center.y());
295 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i]-center.y());
280 if(i%2){
296 if(i%2){
281 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
297 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
282 rectItem->setRect(m_rect.left(),points[i],m_rect.width(),points[i]-points[i+1]);
298 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i]-layout[i+1]);
283 }
299 }
284 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
300 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
285 lineItem->setLine(m_rect.left()-5,points[i],m_rect.left(),points[i]);
301 lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]);
286 }
302 }
287 }
303 }
288 break;
304 break;
289 default:
305 default:
290 qDebug()<<"Unknown axis type";
306 qDebug()<<"Unknown axis type";
291 break;
307 break;
292 }
308 }
309
310 m_layoutVector=layout;
293 }
311 }
294
312
295 //handlers
313 //handlers
296
314
297 void AxisItem::handleAxisUpdated()
315 void AxisItem::handleAxisUpdated()
298 {
316 {
299 if(m_layoutVector.count()==0) return;
317 if(isEmpty()) return;
300
318
301 if(m_chartAxis->isAxisVisible()) {
319 if(m_chartAxis->isAxisVisible()) {
302 setAxisOpacity(100);
320 setAxisOpacity(100);
303 }
321 }
304 else {
322 else {
305 setAxisOpacity(0);
323 setAxisOpacity(0);
306 }
324 }
307
325
308 if(m_chartAxis->isGridVisible()) {
326 if(m_chartAxis->isGridVisible()) {
309 setGridOpacity(100);
327 setGridOpacity(100);
310 }
328 }
311 else {
329 else {
312 setGridOpacity(0);
330 setGridOpacity(0);
313 }
331 }
314
332
315 if(m_chartAxis->labelsVisible())
333 if(m_chartAxis->labelsVisible())
316 {
334 {
317 setLabelsOpacity(100);
335 setLabelsOpacity(100);
318 }
336 }
319 else {
337 else {
320 setLabelsOpacity(0);
338 setLabelsOpacity(0);
321 }
339 }
322
340
323 if(m_chartAxis->shadesVisible()) {
341 if(m_chartAxis->shadesVisible()) {
324 setShadesOpacity(m_chartAxis->shadesOpacity());
342 setShadesOpacity(m_chartAxis->shadesOpacity());
325 }
343 }
326 else {
344 else {
327 setShadesOpacity(0);
345 setShadesOpacity(0);
328 }
346 }
329
347
330 setLabelsAngle(m_chartAxis->labelsAngle());
348 setLabelsAngle(m_chartAxis->labelsAngle());
331 setAxisPen(m_chartAxis->axisPen());
349 setAxisPen(m_chartAxis->axisPen());
332 setLabelsPen(m_chartAxis->labelsPen());
350 setLabelsPen(m_chartAxis->labelsPen());
333 setLabelsBrush(m_chartAxis->labelsBrush());
351 setLabelsBrush(m_chartAxis->labelsBrush());
334 setLabelsFont(m_chartAxis->labelsFont());
352 setLabelsFont(m_chartAxis->labelsFont());
335 setGridPen(m_chartAxis->gridPen());
353 setGridPen(m_chartAxis->gridPen());
336 setShadesPen(m_chartAxis->shadesPen());
354 setShadesPen(m_chartAxis->shadesPen());
337 setShadesBrush(m_chartAxis->shadesBrush());
355 setShadesBrush(m_chartAxis->shadesBrush());
356
338 }
357 }
339
358
340 void AxisItem::handleRangeChanged(qreal min, qreal max)
359 void AxisItem::handleRangeChanged(qreal min, qreal max)
341 {
360 {
342 if(min == max) return;
343
361
344 QStringList labels = createLabels(4,min,max);
362 m_min = min;
363 m_max = max;
345
364
346 int diff = m_thicksList.size() - labels.size();
365 if(isEmpty()) return;
366 updateItem();
347
367
348 if(diff>0){
349 clear(diff);
350 }else if(diff<0){
351 createItems(-diff);
352 }
368 }
353 m_thicksList=labels;
354
369
355 if(m_rect.isEmpty()) return;
370 void AxisItem::handleTicksCountChanged(int ticks)
356
371 {
357 QVector<qreal> vector = calculateLayout();
372 m_ticks=ticks;
358
359 updateItems(m_layoutVector,vector);
360
373
361 if(diff!=0) handleAxisUpdated();
374 if(isEmpty()) return;
375 updateItem();
362 }
376 }
363
377
364 void AxisItem::handleGeometryChanged(const QRectF& rect)
378 void AxisItem::handleGeometryChanged(const QRectF& rect)
365 {
379 {
366 m_rect = rect;
380 m_rect = rect;
381 if(isEmpty()) return;
382 updateItem();
383 }
367
384
368 if(m_thicksList.size()==0) return;
385 bool AxisItem::isEmpty()
369
386 {
370 QVector<qreal> vector = calculateLayout();
387 return m_rect.isEmpty() || m_min==m_max || m_ticks==0;
371 updateItems(m_layoutVector,vector);
372 }
388 }
373
389
374 //TODO "nice numbers algorithm"
390 //TODO "nice numbers algorithm"
375 #include "moc_axisitem_p.cpp"
391 #include "moc_axisitem_p.cpp"
376
392
377 QTCOMMERCIALCHART_END_NAMESPACE
393 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,82 +1,88
1 #ifndef AXISITEM_H_
1 #ifndef AXISITEM_H_
2 #define AXISITEM_H_
2 #define AXISITEM_H_
3
3
4 #include "domain_p.h"
4 #include "domain_p.h"
5 #include "chartitem_p.h"
5 #include "chartitem_p.h"
6 #include <QGraphicsItem>
6 #include <QGraphicsItem>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QChartAxis;
10 class QChartAxis;
11
11
12 class AxisItem : public QObject, public ChartItem
12 class AxisItem : public QObject, public ChartItem
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 public:
15 public:
16 enum AxisType{X_AXIS,Y_AXIS};
16 enum AxisType{X_AXIS,Y_AXIS};
17
17
18 AxisItem(QChartAxis* axis,AxisType type = X_AXIS,QGraphicsItem* parent = 0);
18 AxisItem(QChartAxis* axis,AxisType type = X_AXIS,QGraphicsItem* parent = 0);
19 ~AxisItem();
19 ~AxisItem();
20
20
21 //from QGraphicsItem
21 //from QGraphicsItem
22 QRectF boundingRect() const;
22 QRectF boundingRect() const;
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
24
24
25 AxisType axisType() const {return m_type;};
25 AxisType axisType() const {return m_type;};
26
26
27 void setAxisOpacity(qreal opacity);
27 void setAxisOpacity(qreal opacity);
28 qreal axisOpacity() const;
28 qreal axisOpacity() const;
29
29
30 void setGridOpacity(qreal opacity);
30 void setGridOpacity(qreal opacity);
31 qreal gridOpacity() const;
31 qreal gridOpacity() const;
32
32
33 void setLabelsOpacity(qreal opacity);
33 void setLabelsOpacity(qreal opacity);
34 qreal labelsOpacity() const;
34 qreal labelsOpacity() const;
35
35
36 void setShadesOpacity(qreal opacity);
36 void setShadesOpacity(qreal opacity);
37 qreal shadesOpacity() const;
37 qreal shadesOpacity() const;
38
38
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 void setShadesBrush(const QBrush& brush);
42 void setShadesBrush(const QBrush& brush);
43 void setShadesPen(const QPen& pen);
43 void setShadesPen(const QPen& pen);
44
44
45 void setAxisPen(const QPen& pen);
45 void setAxisPen(const QPen& pen);
46 void setGridPen(const QPen& pen);
46 void setGridPen(const QPen& pen);
47
47
48 void setLabelsPen(const QPen& pen);
48 void setLabelsPen(const QPen& pen);
49 void setLabelsBrush(const QBrush& brush);
49 void setLabelsBrush(const QBrush& brush);
50 void setLabelsFont(const QFont& font);
50 void setLabelsFont(const QFont& font);
51
51
52 public slots:
52 public slots:
53 void handleAxisUpdated();//qchartaxis update calls
53 void handleAxisUpdated();//qchartaxis update calls
54 void handleRangeChanged(qreal min , qreal max); //domain update calls
54 void handleRangeChanged(qreal min , qreal max); //domain update calls
55 void handleTicksCountChanged(int ticks); //ticks changed
55 void handleGeometryChanged(const QRectF& size); //geometry update calls
56 void handleGeometryChanged(const QRectF& size); //geometry update calls
56
57
57 public:
58 public:
58 virtual void updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout);
59 virtual void updateItem();
59 QVector<qreal> calculateLayout() const;
60 QVector<qreal> calculateLayout() const;
60 void applyLayout(const QVector<qreal>& points);
61 void setLayout(const QVector<qreal>& points);
62 QVector<qreal> layout() { return m_layoutVector;};
61
63
62 private:
64 private:
65 inline bool isEmpty();
63 void clear(int count);
66 void clear(int count);
64 void createItems(int count);
67 void createItems(int count);
65 QStringList createLabels(int ticks, qreal min, qreal max);
68 QStringList createLabels(int ticks, qreal min, qreal max);
66 private:
69 private:
67 QChartAxis* m_chartAxis;
70 QChartAxis* m_chartAxis;
68 AxisType m_type;
71 AxisType m_type;
69 QRectF m_rect;
72 QRectF m_rect;
70 int m_labelsAngle;
73 int m_labelsAngle;
71 QGraphicsItemGroup m_grid;
74 QGraphicsItemGroup m_grid;
72 QGraphicsItemGroup m_shades;
75 QGraphicsItemGroup m_shades;
73 QGraphicsItemGroup m_labels;
76 QGraphicsItemGroup m_labels;
74 QGraphicsItemGroup m_axis;
77 QGraphicsItemGroup m_axis;
75 QStringList m_thicksList;
78 QStringList m_thicksList;
76 QVector<qreal> m_layoutVector;
79 QVector<qreal> m_layoutVector;
80 qreal m_min;
81 qreal m_max;
82 int m_ticks;
77
83
78 };
84 };
79
85
80 QTCOMMERCIALCHART_END_NAMESPACE
86 QTCOMMERCIALCHART_END_NAMESPACE
81
87
82 #endif /* AXISITEM_H_ */
88 #endif /* AXISITEM_H_ */
@@ -1,307 +1,307
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
50
51 QObject::connect(axisY,SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeXChanged(qreal,qreal)));
51 QObject::connect(axisY,SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeYChanged(qreal,qreal)));
52 QObject::connect(axisX(),SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeYChanged(qreal,qreal)));
52 QObject::connect(axisX(),SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeXChanged(qreal,qreal)));
53 //initialize
53 //initialize
54 m_axisDomainMap.insert(axisY,domain);
54 m_axisDomainMap.insert(axisY,domain);
55 emit axisAdded(axisY,domain);
55 emit axisAdded(axisY,domain);
56 }
56 }
57
57
58 if(!m_axisXInitialized){
58 if(!m_axisXInitialized){
59 emit axisAdded(axisX(),domain);
59 emit axisAdded(axisX(),domain);
60 m_axisXInitialized=true;
60 m_axisXInitialized=true;
61 }
61 }
62
62
63 calculateDomain(series,domain);
63 calculateDomain(series,domain);
64
64
65 m_seriesAxisMap.insert(series,axisY);
65 m_seriesAxisMap.insert(series,axisY);
66 emit seriesAdded(series,domain);
66 emit seriesAdded(series,domain);
67
67
68 }
68 }
69
69
70 void ChartDataSet::removeSeries(QSeries* series)
70 void ChartDataSet::removeSeries(QSeries* series)
71 {
71 {
72
72
73 QChartAxis* axis = m_seriesAxisMap.value(series);
73 QChartAxis* axis = m_seriesAxisMap.value(series);
74
74
75 if(!axis){
75 if(!axis){
76 qWarning()<<"Can not remove series. Series not found on the chart.";
76 qWarning()<<"Can not remove series. Series not found on the chart.";
77 return;
77 return;
78 }
78 }
79 emit seriesRemoved(series);
79 emit seriesRemoved(series);
80 m_seriesAxisMap.remove(series);
80 m_seriesAxisMap.remove(series);
81
81
82 if(series->parent()==this){
82 if(series->parent()==this){
83 delete series;
83 delete series;
84 series=0;
84 series=0;
85 }
85 }
86
86
87 QList<QChartAxis*> axes = m_seriesAxisMap.values();
87 QList<QChartAxis*> axes = m_seriesAxisMap.values();
88
88
89 int i = axes.indexOf(axis);
89 int i = axes.indexOf(axis);
90
90
91 if(i==-1){
91 if(i==-1){
92 Domain* domain = m_axisDomainMap.take(axis);
92 Domain* domain = m_axisDomainMap.take(axis);
93 emit axisRemoved(axis);
93 emit axisRemoved(axis);
94 if(axis!=axisY()){
94 if(axis!=axisY()){
95 if(axis->parent()==this){
95 if(axis->parent()==this){
96 delete axis;
96 delete axis;
97 axis=0;
97 axis=0;
98 }
98 }
99 }
99 }
100 delete domain;
100 delete domain;
101 }
101 }
102
102
103 if(m_seriesAxisMap.values().size()==0)
103 if(m_seriesAxisMap.values().size()==0)
104 {
104 {
105 m_axisXInitialized=false;
105 m_axisXInitialized=false;
106 emit axisRemoved(axisX());
106 emit axisRemoved(axisX());
107 }
107 }
108 }
108 }
109
109
110 void ChartDataSet::removeAllSeries()
110 void ChartDataSet::removeAllSeries()
111 {
111 {
112
112
113 QList<QSeries*> series = m_seriesAxisMap.keys();
113 QList<QSeries*> series = m_seriesAxisMap.keys();
114
114
115 foreach(QSeries* s , series) {
115 foreach(QSeries* s , series) {
116 removeSeries(s);
116 removeSeries(s);
117 }
117 }
118
118
119 Q_ASSERT(m_seriesAxisMap.count()==0);
119 Q_ASSERT(m_seriesAxisMap.count()==0);
120 Q_ASSERT(m_axisDomainMap.count()==0);
120 Q_ASSERT(m_axisDomainMap.count()==0);
121
121
122 }
122 }
123
123
124 //to be removed with PIMPL
124 //to be removed with PIMPL
125 void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) const
125 void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) const
126 {
126 {
127 switch(series->type())
127 switch(series->type())
128 {
128 {
129 case QSeries::SeriesTypeLine: {
129 case QSeries::SeriesTypeLine: {
130
130
131 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
131 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
132
132
133 for (int i = 0; i < lineSeries->count(); i++)
133 for (int i = 0; i < lineSeries->count(); i++)
134 {
134 {
135 qreal x = lineSeries->x(i);
135 qreal x = lineSeries->x(i);
136 qreal y = lineSeries->y(i);
136 qreal y = lineSeries->y(i);
137 domain->setMinX(qMin(domain->minX(),x));
137 domain->setMinX(qMin(domain->minX(),x));
138 domain->setMinY(qMin(domain->minY(),y));
138 domain->setMinY(qMin(domain->minY(),y));
139 domain->setMaxX(qMax(domain->maxX(),x));
139 domain->setMaxX(qMax(domain->maxX(),x));
140 domain->setMaxY(qMax(domain->maxY(),y));
140 domain->setMaxY(qMax(domain->maxY(),y));
141 }
141 }
142 break;
142 break;
143 }
143 }
144 case QSeries::SeriesTypeArea: {
144 case QSeries::SeriesTypeArea: {
145
145
146 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
146 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
147
147
148 QLineSeries* upperSeries = areaSeries->upperSeries();
148 QLineSeries* upperSeries = areaSeries->upperSeries();
149 QLineSeries* lowerSeries = areaSeries->lowerSeries();
149 QLineSeries* lowerSeries = areaSeries->lowerSeries();
150
150
151 for (int i = 0; i < upperSeries->count(); i++)
151 for (int i = 0; i < upperSeries->count(); i++)
152 {
152 {
153 qreal x = upperSeries->x(i);
153 qreal x = upperSeries->x(i);
154 qreal y = upperSeries->y(i);
154 qreal y = upperSeries->y(i);
155 domain->setMinX(qMin(domain->minX(),x));
155 domain->setMinX(qMin(domain->minX(),x));
156 domain->setMinY(qMin(domain->minY(),y));
156 domain->setMinY(qMin(domain->minY(),y));
157 domain->setMaxX(qMax(domain->maxX(),x));
157 domain->setMaxX(qMax(domain->maxX(),x));
158 domain->setMaxY(qMax(domain->maxY(),y));
158 domain->setMaxY(qMax(domain->maxY(),y));
159 }
159 }
160 if(lowerSeries) {
160 if(lowerSeries) {
161 for (int i = 0; i < lowerSeries->count(); i++)
161 for (int i = 0; i < lowerSeries->count(); i++)
162 {
162 {
163 qreal x = lowerSeries->x(i);
163 qreal x = lowerSeries->x(i);
164 qreal y = lowerSeries->y(i);
164 qreal y = lowerSeries->y(i);
165 domain->setMinX(qMin(domain->minX(),x));
165 domain->setMinX(qMin(domain->minX(),x));
166 domain->setMinY(qMin(domain->minY(),y));
166 domain->setMinY(qMin(domain->minY(),y));
167 domain->setMaxX(qMax(domain->maxX(),x));
167 domain->setMaxX(qMax(domain->maxX(),x));
168 domain->setMaxY(qMax(domain->maxY(),y));
168 domain->setMaxY(qMax(domain->maxY(),y));
169 }}
169 }}
170 break;
170 break;
171 }
171 }
172 case QSeries::SeriesTypeBar: {
172 case QSeries::SeriesTypeBar: {
173 qDebug() << "QChartSeries::SeriesTypeBar";
173 qDebug() << "QChartSeries::SeriesTypeBar";
174 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
174 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
175 qreal x = barSeries->categoryCount();
175 qreal x = barSeries->categoryCount();
176 qreal y = barSeries->max();
176 qreal y = barSeries->max();
177 domain->setMinX(qMin(domain->minX(),x));
177 domain->setMinX(qMin(domain->minX(),x));
178 domain->setMinY(qMin(domain->minY(),y));
178 domain->setMinY(qMin(domain->minY(),y));
179 domain->setMaxX(qMax(domain->maxX(),x));
179 domain->setMaxX(qMax(domain->maxX(),x));
180 domain->setMaxY(qMax(domain->maxY(),y));
180 domain->setMaxY(qMax(domain->maxY(),y));
181 break;
181 break;
182 }
182 }
183 case QSeries::SeriesTypeStackedBar: {
183 case QSeries::SeriesTypeStackedBar: {
184 qDebug() << "QChartSeries::SeriesTypeStackedBar";
184 qDebug() << "QChartSeries::SeriesTypeStackedBar";
185
185
186 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
186 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
187 qreal x = stackedBarSeries->categoryCount();
187 qreal x = stackedBarSeries->categoryCount();
188 qreal y = stackedBarSeries->maxCategorySum();
188 qreal y = stackedBarSeries->maxCategorySum();
189 domain->setMinX(qMin(domain->minX(),x));
189 domain->setMinX(qMin(domain->minX(),x));
190 domain->setMinY(qMin(domain->minY(),y));
190 domain->setMinY(qMin(domain->minY(),y));
191 domain->setMaxX(qMax(domain->maxX(),x));
191 domain->setMaxX(qMax(domain->maxX(),x));
192 domain->setMaxY(qMax(domain->maxY(),y));
192 domain->setMaxY(qMax(domain->maxY(),y));
193 break;
193 break;
194 }
194 }
195 case QSeries::SeriesTypePercentBar: {
195 case QSeries::SeriesTypePercentBar: {
196 qDebug() << "QChartSeries::SeriesTypePercentBar";
196 qDebug() << "QChartSeries::SeriesTypePercentBar";
197
197
198 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
198 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
199 qreal x = percentBarSeries->categoryCount();
199 qreal x = percentBarSeries->categoryCount();
200 domain->setMinX(qMin(domain->minX(),x));
200 domain->setMinX(qMin(domain->minX(),x));
201 domain->setMinY(0);
201 domain->setMinY(0);
202 domain->setMaxX(qMax(domain->maxX(),x));
202 domain->setMaxX(qMax(domain->maxX(),x));
203 domain->setMaxY(100);
203 domain->setMaxY(100);
204 break;
204 break;
205 }
205 }
206
206
207 case QSeries::SeriesTypePie: {
207 case QSeries::SeriesTypePie: {
208 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
208 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
209 // TODO: domain stuff
209 // TODO: domain stuff
210 break;
210 break;
211 }
211 }
212
212
213 case QSeries::SeriesTypeScatter: {
213 case QSeries::SeriesTypeScatter: {
214 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
214 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
215 Q_ASSERT(scatterSeries);
215 Q_ASSERT(scatterSeries);
216 qreal minX(domain->minX());
216 qreal minX(domain->minX());
217 qreal minY(domain->minY());
217 qreal minY(domain->minY());
218 qreal maxX(domain->maxX());
218 qreal maxX(domain->maxX());
219 qreal maxY(domain->maxY());
219 qreal maxY(domain->maxY());
220 foreach (QPointF point, scatterSeries->data()) {
220 foreach (QPointF point, scatterSeries->data()) {
221 minX = qMin(minX, point.x());
221 minX = qMin(minX, point.x());
222 minY = qMin(minY, point.y());
222 minY = qMin(minY, point.y());
223 maxX = qMax(maxX, point.x());
223 maxX = qMax(maxX, point.x());
224 maxY = qMax(maxY, point.y());
224 maxY = qMax(maxY, point.y());
225 }
225 }
226 domain->setMinX(minX);
226 domain->setMinX(minX);
227 domain->setMinY(minY);
227 domain->setMinY(minY);
228 domain->setMaxX(maxX);
228 domain->setMaxX(maxX);
229 domain->setMaxY(maxY);
229 domain->setMaxY(maxY);
230 break;
230 break;
231 }
231 }
232
232
233 case QSeries::SeriesTypeSpline: {
233 case QSeries::SeriesTypeSpline: {
234 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
234 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
235
235
236 for (int i = 0; i < splineSeries->count(); i++)
236 for (int i = 0; i < splineSeries->count(); i++)
237 {
237 {
238 qreal x = splineSeries->x(i);
238 qreal x = splineSeries->x(i);
239 qreal y = splineSeries->y(i);
239 qreal y = splineSeries->y(i);
240 domain->setMinX(qMin(domain->minX(),x));
240 domain->setMinX(qMin(domain->minX(),x));
241 domain->setMinY(qMin(domain->minY(),y));
241 domain->setMinY(qMin(domain->minY(),y));
242 domain->setMaxX(qMax(domain->maxX(),x));
242 domain->setMaxX(qMax(domain->maxX(),x));
243 domain->setMaxY(qMax(domain->maxY(),y));
243 domain->setMaxY(qMax(domain->maxY(),y));
244 }
244 }
245 break;
245 break;
246 }
246 }
247
247
248 default: {
248 default: {
249 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
249 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
250 return;
250 return;
251 break;
251 break;
252 }
252 }
253
253
254 }
254 }
255 }
255 }
256
256
257 void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size)
257 void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size)
258 {
258 {
259 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
259 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
260 while (i.hasNext()) {
260 while (i.hasNext()) {
261 i.next();
261 i.next();
262 i.value()->zoomIn(rect,size);
262 i.value()->zoomIn(rect,size);
263 }
263 }
264 }
264 }
265
265
266 void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size)
266 void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size)
267 {
267 {
268 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
268 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
269 while (i.hasNext()) {
269 while (i.hasNext()) {
270 i.next();
270 i.next();
271 i.value()->zoomOut(rect,size);
271 i.value()->zoomOut(rect,size);
272 }
272 }
273 }
273 }
274
274
275 QChartAxis* ChartDataSet::axisY(QSeries* series) const
275 QChartAxis* ChartDataSet::axisY(QSeries* series) const
276 {
276 {
277 if(series == 0) return m_axisY;
277 if(series == 0) return m_axisY;
278 return m_seriesAxisMap.value(series);
278 return m_seriesAxisMap.value(series);
279 }
279 }
280
280
281 Domain* ChartDataSet::domain(QSeries* series) const
281 Domain* ChartDataSet::domain(QSeries* series) const
282 {
282 {
283 QChartAxis* axis = m_seriesAxisMap.value(series);
283 QChartAxis* axis = m_seriesAxisMap.value(series);
284 if(axis){
284 if(axis){
285 return m_axisDomainMap.value(axis);
285 return m_axisDomainMap.value(axis);
286 }else
286 }else
287 return 0;
287 return 0;
288 }
288 }
289
289
290 Domain* ChartDataSet::domain(QChartAxis* axis) const
290 Domain* ChartDataSet::domain(QChartAxis* axis) const
291 {
291 {
292 if(axis==axisX()) {
292 if(axis==axisX()) {
293 return m_axisDomainMap.value(axisY());
293 return m_axisDomainMap.value(axisY());
294 }
294 }
295 else {
295 else {
296 return m_axisDomainMap.value(axis);
296 return m_axisDomainMap.value(axis);
297 }
297 }
298 }
298 }
299
299
300 QChartAxis* ChartDataSet::axis(QSeries* series) const
300 QChartAxis* ChartDataSet::axis(QSeries* series) const
301 {
301 {
302 return m_seriesAxisMap.value(series);
302 return m_seriesAxisMap.value(series);
303 }
303 }
304
304
305 #include "moc_chartdataset_p.cpp"
305 #include "moc_chartdataset_p.cpp"
306
306
307 QTCOMMERCIALCHART_END_NAMESPACE
307 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,368 +1,370
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include "chartdataset_p.h"
4 #include "chartdataset_p.h"
5 #include "charttheme_p.h"
5 #include "charttheme_p.h"
6 //series
6 //series
7 #include "qbarseries.h"
7 #include "qbarseries.h"
8 #include "qstackedbarseries.h"
8 #include "qstackedbarseries.h"
9 #include "qpercentbarseries.h"
9 #include "qpercentbarseries.h"
10 #include "qlineseries.h"
10 #include "qlineseries.h"
11 #include "qareaseries.h"
11 #include "qareaseries.h"
12 #include "qpieseries.h"
12 #include "qpieseries.h"
13 #include "qscatterseries.h"
13 #include "qscatterseries.h"
14 #include "qsplineseries.h"
14 #include "qsplineseries.h"
15 //items
15 //items
16 #include "axisitem_p.h"
16 #include "axisitem_p.h"
17 #include "axisanimationitem_p.h"
17 #include "axisanimationitem_p.h"
18 #include "areachartitem_p.h"
18 #include "areachartitem_p.h"
19 #include "barpresenter_p.h"
19 #include "barpresenter_p.h"
20 #include "stackedbarpresenter_p.h"
20 #include "stackedbarpresenter_p.h"
21 #include "percentbarpresenter_p.h"
21 #include "percentbarpresenter_p.h"
22 #include "linechartitem_p.h"
22 #include "linechartitem_p.h"
23 #include "linechartanimationitem_p.h"
23 #include "linechartanimationitem_p.h"
24 #include "piepresenter_p.h"
24 #include "piepresenter_p.h"
25 #include "scatterpresenter_p.h"
25 #include "scatterpresenter_p.h"
26 #include "splinepresenter_p.h"
26 #include "splinepresenter_p.h"
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
30 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
31 m_chart(chart),
31 m_chart(chart),
32 m_dataset(dataset),
32 m_dataset(dataset),
33 m_chartTheme(0),
33 m_chartTheme(0),
34 m_zoomIndex(0),
34 m_zoomIndex(0),
35 m_marginSize(0),
35 m_marginSize(0),
36 m_rect(QRectF(QPoint(0,0),m_chart->size())),
36 m_rect(QRectF(QPoint(0,0),m_chart->size())),
37 m_options(QChart::NoAnimation)
37 m_options(QChart::NoAnimation)
38 {
38 {
39 createConnections();
39 createConnections();
40 setChartTheme(QChart::ChartThemeDefault);
40 setChartTheme(QChart::ChartThemeDefault);
41 }
41 }
42
42
43 ChartPresenter::~ChartPresenter()
43 ChartPresenter::~ChartPresenter()
44 {
44 {
45 }
45 }
46
46
47 void ChartPresenter::createConnections()
47 void ChartPresenter::createConnections()
48 {
48 {
49 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
49 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
50 QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),this,SLOT(handleSeriesAdded(QSeries*,Domain*)));
50 QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),this,SLOT(handleSeriesAdded(QSeries*,Domain*)));
51 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),this,SLOT(handleSeriesRemoved(QSeries*)));
51 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),this,SLOT(handleSeriesRemoved(QSeries*)));
52 QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*,Domain*)),this,SLOT(handleAxisAdded(QChartAxis*,Domain*)));
52 QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*,Domain*)),this,SLOT(handleAxisAdded(QChartAxis*,Domain*)));
53 QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*)));
53 QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*)));
54 }
54 }
55
55
56
56
57 QRectF ChartPresenter::geometry() const
57 QRectF ChartPresenter::geometry() const
58 {
58 {
59 return m_rect;
59 return m_rect;
60 }
60 }
61
61
62 void ChartPresenter::handleGeometryChanged()
62 void ChartPresenter::handleGeometryChanged()
63 {
63 {
64 QRectF rect(QPoint(0,0),m_chart->size());
64 QRectF rect(QPoint(0,0),m_chart->size());
65 rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
65 rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
66
66
67 //rewrite zoom stack
67 //rewrite zoom stack
68 for(int i=0;i<m_zoomStack.count();i++){
68 for(int i=0;i<m_zoomStack.count();i++){
69 QRectF r = m_zoomStack[i];
69 QRectF r = m_zoomStack[i];
70 qreal w = rect.width()/m_rect.width();
70 qreal w = rect.width()/m_rect.width();
71 qreal h = rect.height()/m_rect.height();
71 qreal h = rect.height()/m_rect.height();
72 QPointF tl = r.topLeft();
72 QPointF tl = r.topLeft();
73 tl.setX(tl.x()*w);
73 tl.setX(tl.x()*w);
74 tl.setY(tl.y()*h);
74 tl.setY(tl.y()*h);
75 QPointF br = r.bottomRight();
75 QPointF br = r.bottomRight();
76 br.setX(br.x()*w);
76 br.setX(br.x()*w);
77 br.setY(br.y()*h);
77 br.setY(br.y()*h);
78 r.setTopLeft(tl);
78 r.setTopLeft(tl);
79 r.setBottomRight(br);
79 r.setBottomRight(br);
80 m_zoomStack[i]=r;
80 m_zoomStack[i]=r;
81 }
81 }
82
82
83 m_rect = rect;
83 m_rect = rect;
84 Q_ASSERT(m_rect.isValid());
84 Q_ASSERT(m_rect.isValid());
85 emit geometryChanged(m_rect);
85 emit geometryChanged(m_rect);
86 }
86 }
87
87
88 int ChartPresenter::margin() const
88 int ChartPresenter::margin() const
89 {
89 {
90 return m_marginSize;
90 return m_marginSize;
91 }
91 }
92
92
93 void ChartPresenter::setMargin(int margin)
93 void ChartPresenter::setMargin(int margin)
94 {
94 {
95 m_marginSize = margin;
95 m_marginSize = margin;
96 }
96 }
97
97
98 void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain)
98 void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain)
99 {
99 {
100
100
101 AxisItem* item ;
101 AxisItem* item ;
102
102
103 if(!m_options.testFlag(QChart::GridAxisAnimations))
103 if(!m_options.testFlag(QChart::GridAxisAnimations))
104 {
104 {
105 item = new AxisItem(axis,axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart);
105 item = new AxisItem(axis,axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart);
106 }else{
106 }else{
107 item = new AxisAnimationItem(axis,axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart);
107 item = new AxisAnimationItem(axis,axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart);
108 }
108 }
109 if(axis==m_dataset->axisX()){
109 if(axis==m_dataset->axisX()){
110 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal)));
110 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal)));
111 //initialize
111 //initialize
112 item->handleRangeChanged(domain->minX(),domain->maxX());
112 item->handleRangeChanged(domain->minX(),domain->maxX());
113 item->handleTicksCountChanged(4);
113 }
114 }
114 else{
115 else{
115 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal)));
116 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal)));
116 //initialize
117 //initialize
117 item->handleRangeChanged(domain->minY(),domain->maxY());
118 item->handleRangeChanged(domain->minY(),domain->maxY());
119 item->handleTicksCountChanged(4);
118 }
120 }
119
121
120 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
122 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
121 //initialize
123 //initialize
122 item->handleGeometryChanged(m_rect);
124 item->handleGeometryChanged(m_rect);
123 m_chartTheme->decorate(axis,item);
125 m_chartTheme->decorate(axis,item);
124 m_axisItems.insert(axis,item);
126 m_axisItems.insert(axis,item);
125 }
127 }
126
128
127 void ChartPresenter::handleAxisRemoved(QChartAxis* axis)
129 void ChartPresenter::handleAxisRemoved(QChartAxis* axis)
128 {
130 {
129 AxisItem* item = m_axisItems.take(axis);
131 AxisItem* item = m_axisItems.take(axis);
130 Q_ASSERT(item);
132 Q_ASSERT(item);
131 delete item;
133 delete item;
132 }
134 }
133
135
134
136
135 void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain)
137 void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain)
136 {
138 {
137 switch(series->type())
139 switch(series->type())
138 {
140 {
139 case QSeries::SeriesTypeLine: {
141 case QSeries::SeriesTypeLine: {
140
142
141 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
143 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
142 LineChartItem* item;
144 LineChartItem* item;
143 if(m_options.testFlag(QChart::SeriesAnimations)){
145 if(m_options.testFlag(QChart::SeriesAnimations)){
144 item = new LineChartAnimationItem(lineSeries,m_chart);
146 item = new LineChartAnimationItem(lineSeries,m_chart);
145 }else{
147 }else{
146 item = new LineChartItem(lineSeries,m_chart);
148 item = new LineChartItem(lineSeries,m_chart);
147 }
149 }
148 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
150 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
149 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
151 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
150 //initialize
152 //initialize
151 item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY());
153 item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY());
152 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
154 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
153 //decorate
155 //decorate
154 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
156 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
155 m_chartItems.insert(series,item);
157 m_chartItems.insert(series,item);
156 break;
158 break;
157 }
159 }
158
160
159 case QSeries::SeriesTypeArea: {
161 case QSeries::SeriesTypeArea: {
160
162
161 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
163 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
162 AreaChartItem* item;
164 AreaChartItem* item;
163 if(m_options.testFlag(QChart::SeriesAnimations)) {
165 if(m_options.testFlag(QChart::SeriesAnimations)) {
164 item = new AreaChartItem(areaSeries,m_chart);
166 item = new AreaChartItem(areaSeries,m_chart);
165 }
167 }
166 else {
168 else {
167 item = new AreaChartItem(areaSeries,m_chart);
169 item = new AreaChartItem(areaSeries,m_chart);
168 }
170 }
169 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
171 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
170 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
172 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
171 //initialize
173 //initialize
172 item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY());
174 item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY());
173 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
175 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
174 //decorate
176 //decorate
175 m_chartTheme->decorate(item,areaSeries,m_chartItems.count());
177 m_chartTheme->decorate(item,areaSeries,m_chartItems.count());
176 m_chartItems.insert(series,item);
178 m_chartItems.insert(series,item);
177 break;
179 break;
178 }
180 }
179
181
180 case QSeries::SeriesTypeBar: {
182 case QSeries::SeriesTypeBar: {
181 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
183 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
182 BarPresenter* item = new BarPresenter(barSeries,m_chart);
184 BarPresenter* item = new BarPresenter(barSeries,m_chart);
183 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
185 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
184 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
186 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
185 // QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
187 // QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
186 m_chartItems.insert(series,item);
188 m_chartItems.insert(series,item);
187 // m_axisXItem->setVisible(false);
189 // m_axisXItem->setVisible(false);
188 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
190 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
189 break;
191 break;
190 }
192 }
191
193
192 case QSeries::SeriesTypeStackedBar: {
194 case QSeries::SeriesTypeStackedBar: {
193
195
194 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
196 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
195 StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries,m_chart);
197 StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries,m_chart);
196 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
198 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
197 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
199 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
198 // QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
200 // QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
199 m_chartItems.insert(series,item);
201 m_chartItems.insert(series,item);
200 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
202 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
201 break;
203 break;
202 }
204 }
203
205
204 case QSeries::SeriesTypePercentBar: {
206 case QSeries::SeriesTypePercentBar: {
205
207
206 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
208 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
207 PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries,m_chart);
209 PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries,m_chart);
208 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
210 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
209 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
211 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
210 // QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
212 // QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
211 m_chartItems.insert(series,item);
213 m_chartItems.insert(series,item);
212 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
214 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
213 break;
215 break;
214 }
216 }
215 case QSeries::SeriesTypeScatter: {
217 case QSeries::SeriesTypeScatter: {
216 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
218 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
217 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
219 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
218 QObject::connect(scatterPresenter, SIGNAL(clicked(QPointF)),
220 QObject::connect(scatterPresenter, SIGNAL(clicked(QPointF)),
219 scatterSeries, SIGNAL(clicked(QPointF)));
221 scatterSeries, SIGNAL(clicked(QPointF)));
220 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
222 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
221 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
223 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
222 QObject::connect(domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),
224 QObject::connect(domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),
223 scatterPresenter, SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
225 scatterPresenter, SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
224 m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count());
226 m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count());
225 m_chartItems.insert(scatterSeries, scatterPresenter);
227 m_chartItems.insert(scatterSeries, scatterPresenter);
226 if (m_rect.isValid())
228 if (m_rect.isValid())
227 scatterPresenter->handleGeometryChanged(m_rect);
229 scatterPresenter->handleGeometryChanged(m_rect);
228 scatterPresenter->handleDomainChanged(domain->minX(), domain->maxX(), domain->minY(), domain->maxY());
230 scatterPresenter->handleDomainChanged(domain->minX(), domain->maxX(), domain->minY(), domain->maxY());
229 break;
231 break;
230 }
232 }
231 case QSeries::SeriesTypePie: {
233 case QSeries::SeriesTypePie: {
232 QPieSeries *s = qobject_cast<QPieSeries *>(series);
234 QPieSeries *s = qobject_cast<QPieSeries *>(series);
233 PiePresenter* pie = new PiePresenter(m_chart, s);
235 PiePresenter* pie = new PiePresenter(m_chart, s);
234 m_chartTheme->decorate(pie, s, m_chartItems.count());
236 m_chartTheme->decorate(pie, s, m_chartItems.count());
235 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
237 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
236
238
237 // Hide all from background when there is only piechart
239 // Hide all from background when there is only piechart
238 // TODO: refactor this ugly code... should be one setting for this
240 // TODO: refactor this ugly code... should be one setting for this
239 if (m_chartItems.count() == 0) {
241 if (m_chartItems.count() == 0) {
240 m_chart->axisX()->setAxisVisible(false);
242 m_chart->axisX()->setAxisVisible(false);
241 m_chart->axisY()->setAxisVisible(false);
243 m_chart->axisY()->setAxisVisible(false);
242 m_chart->axisX()->setGridVisible(false);
244 m_chart->axisX()->setGridVisible(false);
243 m_chart->axisY()->setGridVisible(false);
245 m_chart->axisY()->setGridVisible(false);
244 m_chart->axisX()->setLabelsVisible(false);
246 m_chart->axisX()->setLabelsVisible(false);
245 m_chart->axisY()->setLabelsVisible(false);
247 m_chart->axisY()->setLabelsVisible(false);
246 m_chart->axisX()->setShadesVisible(false);
248 m_chart->axisX()->setShadesVisible(false);
247 m_chart->axisY()->setShadesVisible(false);
249 m_chart->axisY()->setShadesVisible(false);
248 m_chart->setChartBackgroundBrush(Qt::transparent);
250 m_chart->setChartBackgroundBrush(Qt::transparent);
249 }
251 }
250
252
251 m_chartItems.insert(series, pie);
253 m_chartItems.insert(series, pie);
252 pie->handleGeometryChanged(m_rect);
254 pie->handleGeometryChanged(m_rect);
253 break;
255 break;
254 }
256 }
255
257
256 case QSeries::SeriesTypeSpline: {
258 case QSeries::SeriesTypeSpline: {
257 QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(series);
259 QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(series);
258 SplinePresenter* splinePresenter = new SplinePresenter(splineSeries, m_chart);
260 SplinePresenter* splinePresenter = new SplinePresenter(splineSeries, m_chart);
259 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), splinePresenter, SLOT(handleGeometryChanged(const QRectF&)));
261 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), splinePresenter, SLOT(handleGeometryChanged(const QRectF&)));
260 m_chartTheme->decorate(splinePresenter, splineSeries, m_chartItems.count());
262 m_chartTheme->decorate(splinePresenter, splineSeries, m_chartItems.count());
261 m_chartItems.insert(splineSeries, splinePresenter);
263 m_chartItems.insert(splineSeries, splinePresenter);
262 break;
264 break;
263 }
265 }
264 default: {
266 default: {
265 qDebug()<< "Series type" << series->type() << "not implemented.";
267 qDebug()<< "Series type" << series->type() << "not implemented.";
266 break;
268 break;
267 }
269 }
268 }
270 }
269
271
270 zoomReset();
272 zoomReset();
271 }
273 }
272
274
273 void ChartPresenter::handleSeriesRemoved(QSeries* series)
275 void ChartPresenter::handleSeriesRemoved(QSeries* series)
274 {
276 {
275 ChartItem* item = m_chartItems.take(series);
277 ChartItem* item = m_chartItems.take(series);
276 delete item;
278 delete item;
277 }
279 }
278
280
279 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
281 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
280 {
282 {
281 delete m_chartTheme;
283 delete m_chartTheme;
282
284
283 m_chartTheme = ChartTheme::createTheme(theme);
285 m_chartTheme = ChartTheme::createTheme(theme);
284
286
285 m_chartTheme->decorate(m_chart);
287 m_chartTheme->decorate(m_chart);
286 QMapIterator<QSeries*,ChartItem*> i(m_chartItems);
288 QMapIterator<QSeries*,ChartItem*> i(m_chartItems);
287
289
288 int index=0;
290 int index=0;
289 while (i.hasNext()) {
291 while (i.hasNext()) {
290 i.next();
292 i.next();
291 m_chartTheme->decorate(i.value(),i.key(),index);
293 m_chartTheme->decorate(i.value(),i.key(),index);
292 index++;
294 index++;
293 }
295 }
294
296
295 QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems);
297 QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems);
296 while (j.hasNext()) {
298 while (j.hasNext()) {
297 j.next();
299 j.next();
298 m_chartTheme->decorate(j.key(),j.value());
300 m_chartTheme->decorate(j.key(),j.value());
299 }
301 }
300 }
302 }
301
303
302 QChart::ChartTheme ChartPresenter::chartTheme()
304 QChart::ChartTheme ChartPresenter::chartTheme()
303 {
305 {
304 return m_chartTheme->id();
306 return m_chartTheme->id();
305 }
307 }
306
308
307 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
309 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
308 {
310 {
309 if(m_options!=options) {
311 if(m_options!=options) {
310
312
311 m_options=options;
313 m_options=options;
312
314
313 //recreate elements
315 //recreate elements
314 QList<QChartAxis*> axisList = m_axisItems.uniqueKeys();
316 QList<QChartAxis*> axisList = m_axisItems.uniqueKeys();
315 QList<QSeries*> seriesList = m_chartItems.uniqueKeys();
317 QList<QSeries*> seriesList = m_chartItems.uniqueKeys();
316
318
317 foreach(QChartAxis* axis, axisList) {
319 foreach(QChartAxis* axis, axisList) {
318 handleAxisRemoved(axis);
320 handleAxisRemoved(axis);
319 handleAxisAdded(axis,m_dataset->domain(axis));
321 handleAxisAdded(axis,m_dataset->domain(axis));
320 }
322 }
321 foreach(QSeries* series, seriesList) {
323 foreach(QSeries* series, seriesList) {
322 handleSeriesRemoved(series);
324 handleSeriesRemoved(series);
323 handleSeriesAdded(series,m_dataset->domain(series));
325 handleSeriesAdded(series,m_dataset->domain(series));
324 }
326 }
325 }
327 }
326 }
328 }
327
329
328 void ChartPresenter::zoomIn()
330 void ChartPresenter::zoomIn()
329 {
331 {
330 QRectF rect = geometry();
332 QRectF rect = geometry();
331 rect.setWidth(rect.width()/2);
333 rect.setWidth(rect.width()/2);
332 rect.setHeight(rect.height()/2);
334 rect.setHeight(rect.height()/2);
333 rect.moveCenter(geometry().center());
335 rect.moveCenter(geometry().center());
334 zoomIn(rect);
336 zoomIn(rect);
335 }
337 }
336
338
337 void ChartPresenter::zoomIn(const QRectF& rect)
339 void ChartPresenter::zoomIn(const QRectF& rect)
338 {
340 {
339 QRectF r = rect.normalized();
341 QRectF r = rect.normalized();
340 r.translate(-m_marginSize, -m_marginSize);
342 r.translate(-m_marginSize, -m_marginSize);
341 m_dataset->zoomInDomain(r,geometry().size());
343 m_dataset->zoomInDomain(r,geometry().size());
342 m_zoomStack<<r;
344 m_zoomStack<<r;
343 m_zoomIndex++;
345 m_zoomIndex++;
344 }
346 }
345
347
346 void ChartPresenter::zoomOut()
348 void ChartPresenter::zoomOut()
347 {
349 {
348 if(m_zoomIndex==0) return;
350 if(m_zoomIndex==0) return;
349 m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size());
351 m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size());
350 m_zoomIndex--;
352 m_zoomIndex--;
351 m_zoomStack.resize(m_zoomIndex);
353 m_zoomStack.resize(m_zoomIndex);
352 }
354 }
353
355
354 void ChartPresenter::zoomReset()
356 void ChartPresenter::zoomReset()
355 {
357 {
356 m_zoomIndex=0;
358 m_zoomIndex=0;
357 m_zoomStack.resize(m_zoomIndex);
359 m_zoomStack.resize(m_zoomIndex);
358 }
360 }
359
361
360 QChart::AnimationOptions ChartPresenter::animationOptions() const
362 QChart::AnimationOptions ChartPresenter::animationOptions() const
361 {
363 {
362 return m_options;
364 return m_options;
363 }
365 }
364
366
365
367
366 #include "moc_chartpresenter_p.cpp"
368 #include "moc_chartpresenter_p.cpp"
367
369
368 QTCOMMERCIALCHART_END_NAMESPACE
370 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now