##// END OF EJS Templates
Refactors axis handling...
Michal Klocek -
r223:c82178b4ef05
parent child
Show More
@@ -1,320 +1,299
1 #include "axisitem_p.h"
1 #include "axisitem_p.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 #include <QPainter>
3 #include <QPainter>
4 #include <QDebug>
4 #include <QDebug>
5
5
6 static int label_padding = 5;
6 static int label_padding = 5;
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) :
10 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) :
11 ChartItem(parent),
11 ChartItem(parent),
12 m_ticks(4),
13 m_type(type),
12 m_type(type),
14 m_labelsAngle(0),
13 m_labelsAngle(0),
15 m_shadesEnabled(true),
14 m_shadesEnabled(true),
16 m_grid(parent),
15 m_grid(parent),
17 m_shades(parent),
16 m_shades(parent),
18 m_labels(parent)
17 m_labels(parent)
19 {
18 {
20 //initial initialization
19 //initial initialization
21 m_shades.setZValue(0);
20 m_shades.setZValue(0);
22 m_grid.setZValue(2);
21 m_grid.setZValue(2);
23 createItems();
24 }
22 }
25
23
26 AxisItem::~AxisItem()
24 AxisItem::~AxisItem()
27 {
25 {
28 }
26 }
29
27
30 QRectF AxisItem::boundingRect() const
28 QRectF AxisItem::boundingRect() const
31 {
29 {
32 return m_rect;
30 return m_rect;
33 }
31 }
34
32
35 void AxisItem::createItems()
33 void AxisItem::createItems(int count)
36 {
34 {
37 for (int i = 0; i <= m_ticks; ++i) {
35 for (int i = 0; i < count; ++i) {
38 m_grid.addToGroup(new QGraphicsLineItem(this));
36 m_grid.addToGroup(new QGraphicsLineItem(this));
39 m_labels.addToGroup(new QGraphicsSimpleTextItem(this));
37 m_labels.addToGroup(new QGraphicsSimpleTextItem(this));
40 if(i%2) m_shades.addToGroup(new QGraphicsRectItem(this));
38 if(i%2) m_shades.addToGroup(new QGraphicsRectItem(this));
41 }
39 }
42 }
40 }
43
41
44 void AxisItem::clear()
42 void AxisItem::clear()
45 {
43 {
46 foreach(QGraphicsItem* item , m_shades.childItems()) {
44 foreach(QGraphicsItem* item , m_shades.childItems()) {
47 delete item;
45 delete item;
48 }
46 }
49
47
50 foreach(QGraphicsItem* item , m_grid.childItems()) {
48 foreach(QGraphicsItem* item , m_grid.childItems()) {
51 delete item;
49 delete item;
52 }
50 }
53
51
54 foreach(QGraphicsItem* item , m_labels.childItems()) {
52 foreach(QGraphicsItem* item , m_labels.childItems()) {
55 delete item;
53 delete item;
56 }
54 }
57
55
56 m_thicksList.clear();
57
58 }
58 }
59
59
60 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
60 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
61 {
61 {
62
62
63 }
63 }
64
64
65 void AxisItem::updateDomain()
65 void AxisItem::updateItem(int count)
66 {
66 {
67
67
68 QList<QGraphicsItem *> lines = m_grid.childItems();
68 QList<QGraphicsItem *> lines = m_grid.childItems();
69 QList<QGraphicsItem *> labels = m_labels.childItems();
69 QList<QGraphicsItem *> labels = m_labels.childItems();
70 QList<QGraphicsItem *> shades = m_shades.childItems();
70 QList<QGraphicsItem *> shades = m_shades.childItems();
71
71
72 switch (m_type)
72 switch (m_type)
73 {
73 {
74 case X_AXIS:
74 case X_AXIS:
75 {
75 {
76 const qreal deltaX = m_rect.width() / m_ticks;
76 const qreal deltaX = m_rect.width() / (count-1);
77
77
78 m_axis.setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
78 m_axis.setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
79
79
80 for (int i = 0; i <= m_ticks; ++i) {
80 for (int i = 0; i < count; ++i) {
81
82 int x = i * deltaX + m_rect.left();
81 int x = i * deltaX + m_rect.left();
83
84 qreal label = m_domain.m_minX + (i * m_domain.spanX()/ m_ticks);
85
86 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
82 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
87 lineItem->setLine(x, m_rect.top(), x, m_rect.bottom());
83 lineItem->setLine(x, m_rect.top(), x, m_rect.bottom());
88
89 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
84 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
90 labelItem->setText(QString::number(label));
85 labelItem->setText(m_thicksList.at(i));
91 QPointF center = labelItem->boundingRect().center();
86 QPointF center = labelItem->boundingRect().center();
92 labelItem->setTransformOriginPoint(center.x(), center.y());
87 labelItem->setTransformOriginPoint(center.x(), center.y());
93 labelItem->setPos(x - center.x(), m_rect.bottom() + label_padding);
88 labelItem->setPos(x - center.x(), m_rect.bottom() + label_padding);
94
89
95 if(i%2){
90 if(i%2){
96 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
91 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
97 rectItem->setRect(x,m_rect.top(),deltaX,m_rect.height());
92 rectItem->setRect(x,m_rect.top(),deltaX,m_rect.height());
98 rectItem->setOpacity( 0.5 );
93 rectItem->setOpacity( 0.5 );
99 }
94 }
100 }
95 }
101 }
96 }
102 break;
97 break;
103
98
104 case Y_AXIS:
99 case Y_AXIS:
105 {
100 {
106 const qreal deltaY = m_rect.height()/ m_ticks;
101 const qreal deltaY = m_rect.height()/ (count-1);
107
102
108 m_axis.setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
103 m_axis.setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
109
104
110 for (int i = 0; i <= m_ticks; ++i) {
105 for (int i = 0; i < count; ++i) {
111
112 int y = i * -deltaY + m_rect.bottom();
106 int y = i * -deltaY + m_rect.bottom();
113
114 qreal label = m_domain.m_minY + (i * m_domain.spanY()/ m_ticks);
115
116 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
107 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
117 lineItem->setLine(m_rect.left() , y, m_rect.right(), y);
108 lineItem->setLine(m_rect.left() , y, m_rect.right(), y);
118
119 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
109 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
120 labelItem->setText(QString::number(label));
110 labelItem->setText(m_thicksList.at(i));
121 QPointF center = labelItem->boundingRect().center();
111 QPointF center = labelItem->boundingRect().center();
122 labelItem->setTransformOriginPoint(center.x(), center.y());
112 labelItem->setTransformOriginPoint(center.x(), center.y());
123 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , y-center.y());
113 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , y-center.y());
124
125
126 if(i%2){
114 if(i%2){
127 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
115 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
128 rectItem->setRect(m_rect.left(),y,m_rect.width(),deltaY);
116 rectItem->setRect(m_rect.left(),y,m_rect.width(),deltaY);
129
117
130 }
118 }
131 }
119 }
132 }
120 }
133 break;
121 break;
134 default:
122 default:
135 qDebug()<<"Unknown axis type";
123 qDebug()<<"Unknown axis type";
136 break;
124 break;
137 }
125 }
138 }
126 }
139
127
140 void AxisItem::handleAxisChanged(const QChartAxis& axis)
128 void AxisItem::handleAxisUpdate(QChartAxis* axis)
141 {
129 {
142 if(axis.isAxisVisible()) {
130 if(axis->isAxisVisible()) {
143 setAxisOpacity(100);
131 setAxisOpacity(100);
144 }
132 }
145 else {
133 else {
146 setAxisOpacity(0);
134 setAxisOpacity(0);
147 }
135 }
148
136
149 if(axis.isGridVisible()) {
137 if(axis->isGridVisible()) {
150 setGridOpacity(100);
138 setGridOpacity(100);
151 }
139 }
152 else {
140 else {
153 setGridOpacity(0);
141 setGridOpacity(0);
154 }
142 }
155
143
156 if(axis.isLabelsVisible())
144 if(axis->isLabelsVisible())
157 {
145 {
158 setLabelsOpacity(100);
146 setLabelsOpacity(100);
159 }
147 }
160 else {
148 else {
161 setLabelsOpacity(0);
149 setLabelsOpacity(0);
162 }
150 }
163
151
164 if(axis.isShadesVisible()) {
152 if(axis->isShadesVisible()) {
165 setShadesOpacity(axis.shadesOpacity());
153 setShadesOpacity(axis->shadesOpacity());
166 }
154 }
167 else {
155 else {
168 setShadesOpacity(0);
156 setShadesOpacity(0);
169 }
157 }
170
158
171 switch(axis.labelsOrientation())
159 setLabelsAngle(axis->labelsAngle());
172 {
160 setAxisPen(axis->axisPen());
173 case QChartAxis::LabelsOrientationHorizontal:
161 setLabelsPen(axis->labelsPen());
174 setLabelsAngle(0);
162 setLabelsBrush(axis->labelsBrush());
175 break;
163 setLabelsFont(axis->labelFont());
176 case QChartAxis::LabelsOrientationVertical:
164 setGridPen(axis->gridPen());
177 setLabelsAngle(90);
165 setShadesPen(axis->shadesPen());
178 break;
166 setShadesBrush(axis->shadesBrush());
179 case QChartAxis::LabelsOrientationSlide:
180 setLabelsAngle(-45);
181 break;
182 default:
183 break;
184 }
185
186 setAxisPen(axis.axisPen());
187 setLabelsPen(axis.labelsPen());
188 setLabelsBrush(axis.labelsBrush());
189 setLabelsFont(axis.labelFont());
190 setGridPen(axis.gridPen());
191 setShadesPen(axis.shadesPen());
192 setShadesBrush(axis.shadesBrush());
193
194 }
167 }
195
168
196 void AxisItem::handleDomainChanged(const Domain& domain)
169 void AxisItem::handleLabelsChanged(const QStringList& labels)
197 {
170 {
198 m_domain = domain;
171 m_thicksList=labels;
199 updateDomain();
172 QList<QGraphicsItem*> items = m_labels.childItems();
173 if(items.size()!=m_thicksList.size()){
174 clear();
175 m_thicksList=labels;
176 createItems(m_thicksList.size());
177 items = m_labels.childItems();
178 }
179
180 Q_ASSERT(items.size()==m_thicksList.size());
181
182 int i=0;
183 foreach(QGraphicsItem* item, items){
184 static_cast<QGraphicsSimpleTextItem*>(item)->setText(m_thicksList.at(i));
185 i++;
186 }
200 update();
187 update();
201 }
188 }
202
189
203 void AxisItem::handleGeometryChanged(const QRectF& rect)
190 void AxisItem::handleGeometryChanged(const QRectF& rect)
204 {
191 {
205 m_rect = rect;
192 m_rect = rect;
206 updateDomain();
193 updateItem(m_thicksList.size());
207 update();
194 update();
208 }
195 }
209
196
210 void AxisItem::setAxisOpacity(qreal opacity)
197 void AxisItem::setAxisOpacity(qreal opacity)
211 {
198 {
212 m_axis.setOpacity(opacity);
199 m_axis.setOpacity(opacity);
213 }
200 }
214
201
215 qreal AxisItem::axisOpacity() const
202 qreal AxisItem::axisOpacity() const
216 {
203 {
217 return m_axis.opacity();
204 return m_axis.opacity();
218 }
205 }
219
206
220 void AxisItem::setGridOpacity(qreal opacity)
207 void AxisItem::setGridOpacity(qreal opacity)
221 {
208 {
222 m_grid.setOpacity(opacity);
209 m_grid.setOpacity(opacity);
223 }
210 }
224
211
225
212
226 qreal AxisItem::gridOpacity() const
213 qreal AxisItem::gridOpacity() const
227 {
214 {
228 return m_grid.opacity();
215 return m_grid.opacity();
229 }
216 }
230
217
231 void AxisItem::setLabelsOpacity(qreal opacity)
218 void AxisItem::setLabelsOpacity(qreal opacity)
232 {
219 {
233 m_labels.setOpacity(opacity);
220 m_labels.setOpacity(opacity);
234 }
221 }
235
222
236 qreal AxisItem::labelsOpacity() const
223 qreal AxisItem::labelsOpacity() const
237 {
224 {
238 return m_labels.opacity();
225 return m_labels.opacity();
239 }
226 }
240
227
241 void AxisItem::setShadesOpacity(qreal opacity)
228 void AxisItem::setShadesOpacity(qreal opacity)
242 {
229 {
243 m_shades.setOpacity(opacity);
230 m_shades.setOpacity(opacity);
244 }
231 }
245
232
246 qreal AxisItem::shadesOpacity() const
233 qreal AxisItem::shadesOpacity() const
247 {
234 {
248 return m_shades.opacity();
235 return m_shades.opacity();
249 }
236 }
250
237
251 void AxisItem::setLabelsAngle(int angle)
238 void AxisItem::setLabelsAngle(int angle)
252 {
239 {
253 foreach(QGraphicsItem* item , m_labels.childItems()) {
240 foreach(QGraphicsItem* item , m_labels.childItems()) {
254 QPointF center = item->boundingRect().center();
241 QPointF center = item->boundingRect().center();
255 item->setRotation(angle);
242 item->setRotation(angle);
256 }
243 }
257
244
258 m_labelsAngle=angle;
245 m_labelsAngle=angle;
259 }
246 }
260
247
261 void AxisItem::setLabelsPen(const QPen& pen)
248 void AxisItem::setLabelsPen(const QPen& pen)
262 {
249 {
263 foreach(QGraphicsItem* item , m_labels.childItems()) {
250 foreach(QGraphicsItem* item , m_labels.childItems()) {
264 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
251 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
265 }
252 }
266 }
253 }
267
254
268 void AxisItem::setLabelsBrush(const QBrush& brush)
255 void AxisItem::setLabelsBrush(const QBrush& brush)
269 {
256 {
270 foreach(QGraphicsItem* item , m_labels.childItems()) {
257 foreach(QGraphicsItem* item , m_labels.childItems()) {
271 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
258 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
272 }
259 }
273 }
260 }
274
261
275 void AxisItem::setLabelsFont(const QFont& font)
262 void AxisItem::setLabelsFont(const QFont& font)
276 {
263 {
277 foreach(QGraphicsItem* item , m_labels.childItems()) {
264 foreach(QGraphicsItem* item , m_labels.childItems()) {
278 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
265 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
279 }
266 }
280 }
267 }
281
268
282 void AxisItem::setShadesBrush(const QBrush& brush)
269 void AxisItem::setShadesBrush(const QBrush& brush)
283 {
270 {
284 foreach(QGraphicsItem* item , m_shades.childItems()) {
271 foreach(QGraphicsItem* item , m_shades.childItems()) {
285 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
272 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
286 }
273 }
287 }
274 }
288
275
289 void AxisItem::setShadesPen(const QPen& pen)
276 void AxisItem::setShadesPen(const QPen& pen)
290 {
277 {
291 foreach(QGraphicsItem* item , m_shades.childItems()) {
278 foreach(QGraphicsItem* item , m_shades.childItems()) {
292 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
279 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
293 }
280 }
294 }
281 }
295
282
296 void AxisItem::setAxisPen(const QPen& pen)
283 void AxisItem::setAxisPen(const QPen& pen)
297 {
284 {
298 m_axis.setPen(pen);
285 m_axis.setPen(pen);
299 }
286 }
300
287
301 void AxisItem::setGridPen(const QPen& pen)
288 void AxisItem::setGridPen(const QPen& pen)
302 {
289 {
303 foreach(QGraphicsItem* item , m_grid.childItems()) {
290 foreach(QGraphicsItem* item , m_grid.childItems()) {
304 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
291 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
305 }
292 }
306 }
293 }
307
294
308 void AxisItem::setTicks(int count)
309 {
310 if(count!=m_ticks){
311 clear();
312 m_ticks=count;
313 createItems();
314 }
315 }
316
295
317 //TODO "nice numbers algorithm"
296 //TODO "nice numbers algorithm"
318 #include "moc_axisitem_p.cpp"
297 #include "moc_axisitem_p.cpp"
319
298
320 QTCOMMERCIALCHART_END_NAMESPACE
299 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,80 +1,76
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(AxisType type = X_AXIS,QGraphicsItem* parent = 0);
18 AxisItem(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 setTicks(int count);
43 int ticks() const { return m_ticks;}
44
45 void setShadesBrush(const QBrush& brush);
42 void setShadesBrush(const QBrush& brush);
46 void setShadesPen(const QPen& pen);
43 void setShadesPen(const QPen& pen);
47
44
48 void setAxisPen(const QPen& pen);
45 void setAxisPen(const QPen& pen);
49 void setGridPen(const QPen& pen);
46 void setGridPen(const QPen& pen);
50
47
51 void setLabelsPen(const QPen& pen);
48 void setLabelsPen(const QPen& pen);
52 void setLabelsBrush(const QBrush& brush);
49 void setLabelsBrush(const QBrush& brush);
53 void setLabelsFont(const QFont& font);
50 void setLabelsFont(const QFont& font);
54
51
55 public slots:
52 public slots:
56 void handleAxisChanged(const QChartAxis& axis);
53 void handleAxisUpdate(QChartAxis* axis);
57 void handleDomainChanged(const Domain& domain);
54 void handleLabelsChanged(const QStringList& labels);
58 void handleGeometryChanged(const QRectF& size);
55 void handleGeometryChanged(const QRectF& size);
59 protected:
56 protected:
60 void updateDomain();
57 void updateItem(int count);
61 private:
58 private:
62 void clear();
59 void clear();
63 void createItems();
60 void createItems(int count);
64 private:
61 private:
65 AxisType m_type;
62 AxisType m_type;
66 int m_ticks;
67 Domain m_domain;
68 QRectF m_rect;
63 QRectF m_rect;
69 int m_labelsAngle;
64 int m_labelsAngle;
70 bool m_shadesEnabled;
65 bool m_shadesEnabled;
71 QGraphicsItemGroup m_grid;
66 QGraphicsItemGroup m_grid;
72 QGraphicsItemGroup m_shades;
67 QGraphicsItemGroup m_shades;
73 QGraphicsItemGroup m_labels;
68 QGraphicsItemGroup m_labels;
74 QGraphicsLineItem m_axis;
69 QGraphicsLineItem m_axis;
70 QStringList m_thicksList;
75
71
76 };
72 };
77
73
78 QTCOMMERCIALCHART_END_NAMESPACE
74 QTCOMMERCIALCHART_END_NAMESPACE
79
75
80 #endif /* AXISITEM_H_ */
76 #endif /* AXISITEM_H_ */
@@ -1,163 +1,333
1 #include "chartdataset_p.h"
1 #include "chartdataset_p.h"
2 #include "qchartaxis.h"
2 //series
3 //series
3 #include "qlinechartseries.h"
4 #include "qlinechartseries.h"
4 #include "qbarchartseries.h"
5 #include "qbarchartseries.h"
5 #include "qstackedbarchartseries.h"
6 #include "qstackedbarchartseries.h"
6 #include "qpercentbarchartseries.h"
7 #include "qpercentbarchartseries.h"
7 #include "qpieseries.h"
8 #include "qpieseries.h"
8 #include "qscatterseries.h"
9 #include "qscatterseries.h"
9
10
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
12
12 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent)
13 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
14 m_axisX(new QChartAxis(this)),
15 m_axisY(new QChartAxis(this)),
16 m_domainIndex(0),
17 m_axisXInitialized(false)
13 {
18 {
14 Domain domain;
15 m_domains<<domain;
16 }
19 }
17
20
18 ChartDataSet::~ChartDataSet()
21 ChartDataSet::~ChartDataSet()
19 {
22 {
20 // TODO Auto-generated destructor stub
23 // TODO Auto-generated destructor stub
21 }
24 }
22
25
23 const Domain& ChartDataSet::domain() const
26 const Domain ChartDataSet::domain(QChartAxis *axisY) const
24 {
27 {
25 return m_domains[m_domainIndex];
28 int i = m_domainMap.count(axisY);
29 if(i == 0){
30 return Domain();
31 }
32 i = i - m_domainIndex -1;
33 return m_domainMap.values(axisY).at(i);
26 }
34 }
27
35
28 void ChartDataSet::addSeries(QChartSeries* series)
36 void ChartDataSet::addSeries(QChartSeries* series, QChartAxis *axisY)
29 {
37 {
30 // TODO: we should check the series not already added
38 // TODO: we should check the series not already added
31 m_chartSeries << series;
32 series->setParent(this); // take ownership
39 series->setParent(this); // take ownership
33 m_domainIndex = 0;
34 m_domains.resize(1);
35
40
36 Domain& domain = m_domains[m_domainIndex];
41 clearDomains();
42
43 if(axisY==0) axisY = m_axisY;
44
45 QList<QChartSeries*> seriesList = m_seriesMap.values(axisY);
46
47 QList<Domain> domainList = m_domainMap.values(axisY);
48
49 Q_ASSERT(domainList.size()<=1);
50
51 Domain domain;
52
53 if(domainList.size()>0) domain = domainList.at(0);
37
54
38 switch(series->type())
55 switch(series->type())
39 {
56 {
40 case QChartSeries::SeriesTypeLine: {
57 case QChartSeries::SeriesTypeLine: {
41
58
42 QLineChartSeries* xyseries = static_cast<QLineChartSeries*>(series);
59 QLineChartSeries* xyseries = static_cast<QLineChartSeries*>(series);
43
60
44 for (int i = 0; i < xyseries->count(); i++)
61 for (int i = 0; i < xyseries->count(); i++)
45 {
62 {
46 qreal x = xyseries->x(i);
63 qreal x = xyseries->x(i);
47 qreal y = xyseries->y(i);
64 qreal y = xyseries->y(i);
48 domain.m_minX = qMin(domain.m_minX,x);
65 domain.m_minX = qMin(domain.m_minX,x);
49 domain.m_minY = qMin(domain.m_minY,y);
66 domain.m_minY = qMin(domain.m_minY,y);
50 domain.m_maxX = qMax(domain.m_maxX,x);
67 domain.m_maxX = qMax(domain.m_maxX,x);
51 domain.m_maxY = qMax(domain.m_maxY,y);
68 domain.m_maxY = qMax(domain.m_maxY,y);
52 }
69 }
53 break;
70 break;
54 }
71 }
55 case QChartSeries::SeriesTypeBar: {
72 case QChartSeries::SeriesTypeBar: {
56 qDebug() << "QChartSeries::SeriesTypeBar";
73 qDebug() << "QChartSeries::SeriesTypeBar";
57 QBarChartSeries* barSeries = static_cast<QBarChartSeries*>(series);
74 QBarChartSeries* barSeries = static_cast<QBarChartSeries*>(series);
58 qreal x = barSeries->countCategories();
75 qreal x = barSeries->countCategories();
59 qreal y = barSeries->max();
76 qreal y = barSeries->max();
60 domain.m_minX = qMin(domain.m_minX,x);
77 domain.m_minX = qMin(domain.m_minX,x);
61 domain.m_minY = qMin(domain.m_minY,y);
78 domain.m_minY = qMin(domain.m_minY,y);
62 domain.m_maxX = qMax(domain.m_maxX,x);
79 domain.m_maxX = qMax(domain.m_maxX,x);
63 domain.m_maxY = qMax(domain.m_maxY,y);
80 domain.m_maxY = qMax(domain.m_maxY,y);
64 break;
81 break;
65 }
82 }
66 case QChartSeries::SeriesTypeStackedBar: {
83 case QChartSeries::SeriesTypeStackedBar: {
67 qDebug() << "QChartSeries::SeriesTypeStackedBar";
84 qDebug() << "QChartSeries::SeriesTypeStackedBar";
68
85
69 QStackedBarChartSeries* stackedBarSeries = static_cast<QStackedBarChartSeries*>(series);
86 QStackedBarChartSeries* stackedBarSeries = static_cast<QStackedBarChartSeries*>(series);
70 qreal x = stackedBarSeries->countCategories();
87 qreal x = stackedBarSeries->countCategories();
71 qreal y = stackedBarSeries->maxCategorySum();
88 qreal y = stackedBarSeries->maxCategorySum();
72 domain.m_minX = qMin(domain.m_minX,x);
89 domain.m_minX = qMin(domain.m_minX,x);
73 domain.m_minY = qMin(domain.m_minY,y);
90 domain.m_minY = qMin(domain.m_minY,y);
74 domain.m_maxX = qMax(domain.m_maxX,x);
91 domain.m_maxX = qMax(domain.m_maxX,x);
75 domain.m_maxY = qMax(domain.m_maxY,y);
92 domain.m_maxY = qMax(domain.m_maxY,y);
76 break;
93 break;
77 }
94 }
78 case QChartSeries::SeriesTypePercentBar: {
95 case QChartSeries::SeriesTypePercentBar: {
79 qDebug() << "QChartSeries::SeriesTypePercentBar";
96 qDebug() << "QChartSeries::SeriesTypePercentBar";
80
97
81 QPercentBarChartSeries* percentBarSeries = static_cast<QPercentBarChartSeries*>(series);
98 QPercentBarChartSeries* percentBarSeries = static_cast<QPercentBarChartSeries*>(series);
82 qreal x = percentBarSeries->countCategories();
99 qreal x = percentBarSeries->countCategories();
83 domain.m_minX = qMin(domain.m_minX,x);
100 domain.m_minX = qMin(domain.m_minX,x);
84 domain.m_minY = 0;
101 domain.m_minY = 0;
85 domain.m_maxX = qMax(domain.m_maxX,x);
102 domain.m_maxX = qMax(domain.m_maxX,x);
86 domain.m_maxY = 100;
103 domain.m_maxY = 100;
87 break;
104 break;
88 }
105 }
89
106
90 case QChartSeries::SeriesTypePie: {
107 case QChartSeries::SeriesTypePie: {
91 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
108 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
92 // TODO: domain stuff
109 // TODO: domain stuff
93 break;
110 break;
94 }
111 }
95
112
96 case QChartSeries::SeriesTypeScatter: {
113 case QChartSeries::SeriesTypeScatter: {
97 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
114 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
98 Q_ASSERT(scatterSeries);
115 Q_ASSERT(scatterSeries);
99 foreach (QPointF point, scatterSeries->data()) {
116 foreach (QPointF point, scatterSeries->data()) {
100 domain.m_minX = qMin(domain.m_minX, point.x());
117 domain.m_minX = qMin(domain.m_minX, point.x());
101 domain.m_maxX = qMax(domain.m_maxX, point.x());
118 domain.m_maxX = qMax(domain.m_maxX, point.x());
102 domain.m_minY = qMin(domain.m_minY, point.y());
119 domain.m_minY = qMin(domain.m_minY, point.y());
103 domain.m_maxY = qMax(domain.m_maxY, point.y());
120 domain.m_maxY = qMax(domain.m_maxY, point.y());
104 }
121 }
105 break;
122 break;
106 }
123 }
107
124
108 default: {
125 default: {
109 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
126 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
110 return;
127 return;
111 break;
128 break;
112 }
129 }
113
130
114 }
131 }
115
132
133 if(!m_domainMap.contains(axisY))
134 {
135 emit axisAdded(axisY);
136 QObject::connect(axisY,SIGNAL(minChanged(qreal)),this,SLOT(handleMinChanged(qreal)));
137 QObject::connect(axisY,SIGNAL(maxChanged(qreal)),this,SLOT(handleMaxChanged(qreal)));
138 QObject::connect(axisY,SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*)));
139 }
140 m_domainMap.replace(axisY,domain);
141 m_seriesMap.insert(axisY,series);
142
143 if(!m_axisXInitialized)
144 {
145 emit axisAdded(axisX());
146 QObject::connect(axisX(),SIGNAL(minChanged(qreal)),this,SLOT(handleMinChanged(qreal)));
147 QObject::connect(axisX(),SIGNAL(maxChanged(qreal)),this,SLOT(handleMaxChanged(qreal)));
148 QObject::connect(axisX(),SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*)));
149 m_axisXInitialized=true;
150 }
151
152
116 emit seriesAdded(series);
153 emit seriesAdded(series);
117 emit domainChanged(domain);
154 QStringList ylabels = createLabels(axisY,domain.m_minY,domain.m_maxY);
155 QStringList xlabels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
156 emit axisLabelsChanged(axisY,ylabels);
157 emit axisLabelsChanged(axisX(),xlabels);
158 emit seriesDomainChanged(series,domain);
159
160 }
161
162 void ChartDataSet::removeSeries(QChartSeries* series)
163 {
164 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
165 foreach(QChartAxis* axis , keys) {
166 if(m_seriesMap.contains(axis,series)){
167 emit seriesRemoved(series);
168 m_seriesMap.remove(axis,series);
169 //remove axis if no longer there
170 if(!m_seriesMap.contains(axis) && axis != m_axisY){
171 emit axisRemoved(axis);
172 m_domainMap.remove(axis);
173 delete axis;
174 }
175 break;
176 }
177 }
118 }
178 }
119
179
120 bool ChartDataSet::nextDomain()
180 bool ChartDataSet::nextDomain()
121 {
181 {
122 if (m_domainIndex < m_domains.count() - 1) {
182 int limit = (m_domainMap.values().size()/m_domainMap.uniqueKeys().size())-1;
183
184 if (m_domainIndex < limit) {
123 m_domainIndex++;
185 m_domainIndex++;
124 emit domainChanged(m_domains[m_domainIndex]);
186 setDomain(m_domainIndex);
125 return true;
187 return true;
126 }
188 }
127 else {
189 else {
128 return false;
190 return false;
129 }
191 }
130 }
192 }
131
193
132 bool ChartDataSet::previousDomain()
194 bool ChartDataSet::previousDomain()
133 {
195 {
134 if (m_domainIndex > 0) {
196 if (m_domainIndex > 0) {
135 m_domainIndex--;
197 m_domainIndex--;
136 emit domainChanged(m_domains[m_domainIndex]);
198 setDomain(m_domainIndex);
137 return true;
199 return true;
138 }
200 }
139 else {
201 else {
140 return false;
202 return false;
141 }
203 }
142 }
204 }
143
205
144 void ChartDataSet::clearDomains()
206 void ChartDataSet::setDomain(int index)
145 {
207 {
146 if (m_domainIndex > 0) {
208 QList<QChartAxis*> domainList = m_domainMap.uniqueKeys();
147 m_domainIndex = 0;
209 foreach (QChartAxis* axis , domainList) {
148 emit domainChanged(m_domains[m_domainIndex]);
210 int i = m_domainMap.count(axis) - index -1;
211 Q_ASSERT(i>=0);
212 Domain domain = m_domainMap.values(axis).at(i);
213 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
214 QList<QChartSeries*> seriesList = m_seriesMap.values(axis);
215 foreach(QChartSeries* series, seriesList) {
216 emit seriesDomainChanged(series,domain);
217 }
218 emit axisLabelsChanged(axis,labels);
219 }
220
221 Domain domain = m_domainMap.value(axisY());
222 QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
223 emit axisLabelsChanged(axisX(),labels);
224 }
225
226 void ChartDataSet::clearDomains(int toIndex)
227 {
228 Q_ASSERT(toIndex>=0);
229
230 m_domainIndex = toIndex;
231
232 QList<QChartAxis*> keys = m_domainMap.uniqueKeys();
233
234 foreach (QChartAxis* key , keys)
235 {
236 QList<Domain> domains = m_domainMap.values(key);
237 m_domainMap.remove(key);
238 int i = domains.size() - toIndex - 1;
239 while(i--){
240 domains.removeFirst();
241 }
242 for(int j=domains.size()-1; j>=0 ;j--)
243 m_domainMap.insert(key,domains.at(j));
149 }
244 }
150 }
245 }
151
246
152 void ChartDataSet::addDomain(const Domain& domain)
247 void ChartDataSet::addDomain(const QRectF& rect, const QRectF& viewport)
153 {
248 {
154 m_domains.resize(m_domainIndex + 1);
249 Q_ASSERT(rect.isValid());
155 m_domains << domain;
250 Q_ASSERT(viewport.isValid());
251
252 clearDomains(m_domainIndex);
253
254 QList<QChartAxis*> domainList = m_domainMap.uniqueKeys();
255
256 foreach (QChartAxis* axis , domainList){
257 Domain domain(m_domainMap.value(axis).subDomain(rect,viewport.width(),viewport.height()));
258 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
259 QList<QChartSeries*> seriesList = m_seriesMap.values(axis);
260 foreach(QChartSeries* series, seriesList){
261 emit seriesDomainChanged(series,domain);
262 }
263 emit axisLabelsChanged(axis,labels);
264 m_domainMap.insert(axis,domain);
265 }
266
267 Domain domain = m_domainMap.value(axisY());
268 QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
269 emit axisLabelsChanged(axisX(),labels);
270
156 m_domainIndex++;
271 m_domainIndex++;
272 }
273
274 QChartAxis* ChartDataSet::axisY(QChartSeries* series) const
275 {
276 if(series == 0) return m_axisY;
277
278 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
279
280 foreach(QChartAxis* axis , keys) {
281 if(m_seriesMap.contains(axis,series)){
282 return axis;
283 }
284 }
285 return 0;
286 }
287
288 QStringList ChartDataSet::createLabels(QChartAxis* axis,qreal min, qreal max)
289 {
290 Q_ASSERT(max>=min);
157
291
158 emit domainChanged(domain);
292 QStringList labels;
293
294 int ticks = axis->ticksCount();
295
296 for(int i=0; i<= ticks; i++){
297 qreal value = min + (i * (max - min)/ ticks);
298 QString label = axis->axisTickLabel(value);
299 if(label.isEmpty()){
300 labels << QString::number(value);
301 }else{
302 labels << label;
303 }
304 }
305 return labels;
306 }
307
308
309 void ChartDataSet::handleMinChanged(qreal min)
310 {
311
312 }
313
314 void ChartDataSet::handleMaxChanged(qreal max)
315 {
316
317 }
318
319 void ChartDataSet::handleTickChanged(QChartAxis* axis)
320 {
321 Domain domain = m_domainMap.value(axisY());
322 if(axis==axisX()){
323 QStringList labels = createLabels(axis,domain.m_minX,domain.m_maxX);
324 emit axisLabelsChanged(axis,labels);
325 }else{
326 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
327 emit axisLabelsChanged(axis,labels);
328 }
159 }
329 }
160
330
161 #include "moc_chartdataset_p.cpp"
331 #include "moc_chartdataset_p.cpp"
162
332
163 QTCOMMERCIALCHART_END_NAMESPACE
333 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,36 +1,59
1 #ifndef CHARTENGINE_P_H_
1 #ifndef CHARTDATASET_P_H_
2 #define CHARTENGINE_P_H_
2 #define CHARTDATASET_P_H_
3
3
4 #include <qchartseries.h>
4 #include "qchartseries.h"
5 #include "domain_p.h"
5 #include "domain_p.h"
6 #include <QVector>
6 #include <QVector>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QChartAxis;
11
10 class ChartDataSet : public QObject
12 class ChartDataSet : public QObject
11 {
13 {
12 Q_OBJECT
14 Q_OBJECT
13 public:
15 public:
14 ChartDataSet(QObject* parent =0);
16 ChartDataSet(QObject* parent=0);
15 virtual ~ChartDataSet();
17 virtual ~ChartDataSet();
16
18
17 void addSeries(QChartSeries* series);
19 void addSeries(QChartSeries* series,QChartAxis *axisY = 0);
18 void addDomain(const Domain& domain);
20 void removeSeries(QChartSeries* series);
21 void addDomain(const QRectF& rect, const QRectF& viewport);
19 bool nextDomain();
22 bool nextDomain();
20 bool previousDomain();
23 bool previousDomain();
21 void clearDomains();
24 void clearDomains(int toIndex =0);
22 const Domain& domain() const;
25 const Domain domain(QChartAxis *axisY) const;
26 int domainIndex() const {return m_domainIndex;}
27
28 QChartAxis* axisX() const { return m_axisX;};
29 QChartAxis* axisY(QChartSeries* series = 0) const;
23
30
24 signals:
31 signals:
25 void seriesAdded(QChartSeries* series);
32 void seriesAdded(QChartSeries* series);
26 void domainChanged(const Domain& domain);
33 void seriesRemoved(QChartSeries* series);
34 void axisAdded(QChartAxis* axis);
35 void axisRemoved(QChartAxis* axis);
36 void axisLabelsChanged(QChartAxis* axis, const QStringList& labels);
37 void seriesDomainChanged(QChartSeries* series,const Domain& domain);
38
39 private slots:
40 void handleMinChanged(qreal min);
41 void handleMaxChanged(qreal max);
42 void handleTickChanged(QChartAxis*);
43
44 private:
45 void setDomain(int index);
46 QStringList createLabels(QChartAxis* axis,qreal min, qreal max);
27
47
28 private:
48 private:
29 QList<QChartSeries*> m_chartSeries;
49 QMultiMap<QChartAxis*, Domain> m_domainMap;
30 QVector<Domain> m_domains;
50 QMultiMap<QChartAxis*, QChartSeries*> m_seriesMap;
51 QChartAxis* m_axisX;
52 QChartAxis* m_axisY;
31 int m_domainIndex;
53 int m_domainIndex;
54 bool m_axisXInitialized;
32 };
55 };
33
56
34 QTCOMMERCIALCHART_END_NAMESPACE
57 QTCOMMERCIALCHART_END_NAMESPACE
35
58
36 #endif /* CHARTENGINE_P_H_ */
59 #endif /* CHARTENGINE_P_H_ */
@@ -1,21 +1,24
1 #ifndef CHARTITEM_H_
1 #ifndef CHARTITEM_H_
2 #define CHARTITEM_H_
2 #define CHARTITEM_H_
3
3
4 #include "domain_p.h"
4 #include "domain_p.h"
5 #include <QGraphicsItem>
5 #include <QGraphicsItem>
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 class ChartAnimationManager;
9 class ChartAnimationManager;
10
10
11 class ChartItem : public QGraphicsItem
11 class ChartItem : public QGraphicsItem
12 {
12 {
13 enum ChartItemTypes{ AXIS_ITEM = UserType+1, XYLINE_ITEM};
13 enum ChartItemTypes{ AXIS_ITEM = UserType+1, XYLINE_ITEM};
14 public:
14 public:
15 ChartItem(QGraphicsItem* parent = 0):QGraphicsItem(parent){};
15 ChartItem(QGraphicsItem* parent = 0):QGraphicsItem(parent){};
16 //TODO make pure
17 virtual void handleGeometryChanged(const QRectF&){};
18 virtual void handleDomainChanged(const Domain& domain){};
16 virtual ~ChartItem(){};
19 virtual ~ChartItem(){};
17 };
20 };
18
21
19 QTCOMMERCIALCHART_END_NAMESPACE
22 QTCOMMERCIALCHART_END_NAMESPACE
20
23
21 #endif /* CHARTITEM_H_ */
24 #endif /* CHARTITEM_H_ */
@@ -1,269 +1,223
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 "qbarchartseries.h"
7 #include "qbarchartseries.h"
8 #include "qstackedbarchartseries.h"
8 #include "qstackedbarchartseries.h"
9 #include "qpercentbarchartseries.h"
9 #include "qpercentbarchartseries.h"
10 #include "qlinechartseries.h"
10 #include "qlinechartseries.h"
11 #include "qpieseries.h"
11 #include "qpieseries.h"
12 #include "qscatterseries.h"
12 #include "qscatterseries.h"
13 //items
13 //items
14 #include "axisitem_p.h"
14 #include "axisitem_p.h"
15 #include "barpresenter.h"
15 #include "barpresenter.h"
16 #include "stackedbarpresenter.h"
16 #include "stackedbarpresenter.h"
17 #include "linechartitem_p.h"
17 #include "linechartitem_p.h"
18 #include "percentbarpresenter.h"
18 #include "percentbarpresenter.h"
19 #include "linechartanimationitem_p.h"
19 #include "linechartanimationitem_p.h"
20 #include "piepresenter.h"
20 #include "piepresenter.h"
21 #include "scatterpresenter_p.h"
21 #include "scatterpresenter_p.h"
22
22
23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24
24
25 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
25 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
26 m_chart(chart),
26 m_chart(chart),
27 m_dataset(dataset),
27 m_dataset(dataset),
28 m_chartTheme(0),
28 m_chartTheme(0),
29 m_axisXItem(new AxisItem(AxisItem::X_AXIS,m_chart)),
30 m_axisYItem(new AxisItem(AxisItem::Y_AXIS,m_chart)),
31 m_domainIndex(0),
32 m_marginSize(0),
29 m_marginSize(0),
33 m_rect(QRectF(QPoint(0,0),m_chart->size()))
30 m_rect(QRectF(QPoint(0,0),m_chart->size()))
34 {
31 {
32 createConnections();
35 setChartTheme(QChart::ChartThemeDefault);
33 setChartTheme(QChart::ChartThemeDefault);
36 createConnections();
34
37 }
35 }
38
36
39 ChartPresenter::~ChartPresenter()
37 ChartPresenter::~ChartPresenter()
40 {
38 {
41 }
39 }
42
40
43 void ChartPresenter::createConnections()
41 void ChartPresenter::createConnections()
44 {
42 {
45 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
43 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
46 QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*)));
44 QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*)));
47 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),m_axisXItem,SLOT(handleGeometryChanged(const QRectF&)));
45 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QChartSeries*)),this,SLOT(handleSeriesRemoved(QChartSeries*)));
48 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),m_axisXItem,SLOT(handleDomainChanged(const Domain&)));
46 QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*)),this,SLOT(handleAxisAdded(QChartAxis*)));
49 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),m_axisYItem,SLOT(handleGeometryChanged(const QRectF&)));
47 QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*)));
50 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),m_axisYItem,SLOT(handleDomainChanged(const Domain&)));
48 QObject::connect(m_dataset,SIGNAL(seriesDomainChanged(QChartSeries*,const Domain&)),this,SLOT(handleSeriesDomainChanged(QChartSeries*,const Domain&)));
49 QObject::connect(m_dataset,SIGNAL(axisLabelsChanged(QChartAxis*,const QStringList&)),this,SLOT(handleAxisLabelsChanged(QChartAxis*,const QStringList&)));
50 }
51
52
53 QRectF ChartPresenter::geometry() const
54 {
55 return m_rect;
51 }
56 }
52
57
53 void ChartPresenter::handleGeometryChanged()
58 void ChartPresenter::handleGeometryChanged()
54 {
59 {
55 m_rect = QRectF(QPoint(0,0),m_chart->size());
60 m_rect = QRectF(QPoint(0,0),m_chart->size());
56 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
61 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
57 Q_ASSERT(m_rect.isValid());
62 Q_ASSERT(m_rect.isValid());
58 emit geometryChanged(m_rect);
63 emit geometryChanged(m_rect);
59 }
64 }
60
65
61 int ChartPresenter::margin() const
66 int ChartPresenter::margin() const
62 {
67 {
63 return m_marginSize;
68 return m_marginSize;
64 }
69 }
65
70
66 void ChartPresenter::setMargin(int margin)
71 void ChartPresenter::setMargin(int margin)
67 {
72 {
68 m_marginSize = margin;
73 m_marginSize = margin;
69 }
74 }
70
75
76 void ChartPresenter::handleAxisAdded(QChartAxis* axis)
77 {
78 AxisItem* item ;
79
80 if(axis==m_dataset->axisX()){
81 item = new AxisItem(AxisItem::X_AXIS,m_chart);
82 }else{
83 item = new AxisItem(AxisItem::Y_AXIS,m_chart);
84 }
85 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
86 QObject::connect(axis,SIGNAL(update(QChartAxis*)),item,SLOT(handleAxisUpdate(QChartAxis*)));
87 m_chartTheme->decorate(axis,item);
88 m_axisItems.insert(axis,item);
89 }
90
91 void ChartPresenter::handleAxisRemoved(QChartAxis* axis)
92 {
93 AxisItem* item = m_axisItems.take(axis);
94 Q_ASSERT(item);
95 delete item;
96 }
97
98
71 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
99 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
72 {
100 {
73 qDebug() << " ChartPresenter::handleSeriesAdded";
74 switch(series->type())
101 switch(series->type())
75 {
102 {
76 case QChartSeries::SeriesTypeLine: {
103 case QChartSeries::SeriesTypeLine: {
77 QLineChartSeries* lineSeries = static_cast<QLineChartSeries*>(series);
104 QLineChartSeries* lineSeries = static_cast<QLineChartSeries*>(series);
78 LineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart);
105 LineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart);
79 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
106 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
80 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
107 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
81 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
82 QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
108 QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
83 m_chartItems.insert(series,item);
109 m_chartItems.insert(series,item);
84 break;
110 break;
85 }
111 }
86
112
87 case QChartSeries::SeriesTypeBar: {
113 case QChartSeries::SeriesTypeBar: {
88 QBarChartSeries* barSeries = static_cast<QBarChartSeries*>(series);
114 QBarChartSeries* barSeries = static_cast<QBarChartSeries*>(series);
89 BarPresenter* item = new BarPresenter(barSeries->model(),m_chart);
115 BarPresenter* item = new BarPresenter(barSeries->model(),m_chart);
90 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
116 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
91 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
117 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
92 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
93 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
118 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
94 m_chartItems.insert(series,item);
119 m_chartItems.insert(series,item);
95 // m_axisXItem->setVisible(false);
120 // m_axisXItem->setVisible(false);
96 break;
121 break;
97 }
122 }
98
123
99 case QChartSeries::SeriesTypeStackedBar: {
124 case QChartSeries::SeriesTypeStackedBar: {
100
125
101 QStackedBarChartSeries* stackedBarSeries = static_cast<QStackedBarChartSeries*>(series);
126 QStackedBarChartSeries* stackedBarSeries = static_cast<QStackedBarChartSeries*>(series);
102 StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries->model(),m_chart);
127 StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries->model(),m_chart);
103 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
128 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
104 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
129 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
105 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
106 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
130 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
107 m_chartItems.insert(series,item);
131 m_chartItems.insert(series,item);
108 break;
132 break;
109 }
133 }
110
134
111 case QChartSeries::SeriesTypePercentBar: {
135 case QChartSeries::SeriesTypePercentBar: {
112
136
113 QPercentBarChartSeries* percentBarSeries = static_cast<QPercentBarChartSeries*>(series);
137 QPercentBarChartSeries* percentBarSeries = static_cast<QPercentBarChartSeries*>(series);
114 PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries->model(),m_chart);
138 PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries->model(),m_chart);
115 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
139 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
116 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
140 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
117 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
118 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
141 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
119 m_chartItems.insert(series,item);
142 m_chartItems.insert(series,item);
120 break;
143 break;
121 }
144 }
122 case QChartSeries::SeriesTypeScatter: {
145 case QChartSeries::SeriesTypeScatter: {
123 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
146 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
124 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
147 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
125 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
148 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
126 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
149 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
127 QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)),
128 scatterPresenter, SLOT(handleDomainChanged(const Domain&)));
129 m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count());
150 m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count());
130 m_chartItems.insert(scatterSeries, scatterPresenter);
151 m_chartItems.insert(scatterSeries, scatterPresenter);
131 break;
152 break;
132 }
153 }
133 case QChartSeries::SeriesTypePie: {
154 case QChartSeries::SeriesTypePie: {
134 QPieSeries *s = qobject_cast<QPieSeries *>(series);
155 QPieSeries *s = qobject_cast<QPieSeries *>(series);
135 PiePresenter* pie = new PiePresenter(m_chart, s);
156 PiePresenter* pie = new PiePresenter(m_chart, s);
136 m_chartTheme->decorate(pie, s, m_chartItems.count());
157 m_chartTheme->decorate(pie, s, m_chartItems.count());
137 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
158 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
138 QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)), pie, SLOT(handleDomainChanged(const Domain&)));
139 m_chartItems.insert(series, pie);
159 m_chartItems.insert(series, pie);
140 break;
160 break;
141 }
161 }
142 default: {
162 default: {
143 qDebug()<< "Series type" << series->type() << "not implemented.";
163 qDebug()<< "Series type" << series->type() << "not implemented.";
144 break;
164 break;
145 }
165 }
146 }
166 }
147
167
148 if(m_rect.isValid()) emit geometryChanged(m_rect);
168 if(m_rect.isValid()) emit geometryChanged(m_rect);
149 }
169 }
150
170
151 void ChartPresenter::handleSeriesChanged(QChartSeries* series)
171 void ChartPresenter::handleSeriesRemoved(QChartSeries* series)
152 {
153 //TODO:
154 }
155
156 void ChartPresenter::zoomInToRect(const QRectF& rect)
157 {
172 {
158 if(!rect.isValid()) return;
173 ChartItem* item = m_chartItems.take(series);
159 QRectF r = rect.normalized();
174 delete item;
160 r.translate(-m_marginSize, -m_marginSize);
161 Domain domain (m_dataset->domain().subDomain(r,m_rect.width(),m_rect.height()));
162 m_dataset->addDomain(domain);
163 }
175 }
164
176
165 void ChartPresenter::zoomIn()
177 void ChartPresenter::handleSeriesChanged(QChartSeries* series)
166 {
178 {
167 if (!m_dataset->nextDomain()) {
179 //TODO:
168 QRectF rect = m_rect;
169 rect.setWidth(rect.width()/2);
170 rect.setHeight(rect.height()/2);
171 rect.moveCenter(m_rect.center());
172 Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height()));
173 m_dataset->addDomain(domain);
174 }
175 }
180 }
176
181
177 void ChartPresenter::zoomOut()
182 void ChartPresenter::handleSeriesDomainChanged(QChartSeries* series, const Domain& domain)
178 {
183 {
179 m_dataset->previousDomain();
184 m_chartItems.value(series)->handleDomainChanged(domain);
180 }
185 }
181
186
182 void ChartPresenter::zoomReset()
187 void ChartPresenter::handleAxisLabelsChanged(QChartAxis* axis,const QStringList& labels)
183 {
188 {
184 m_dataset->clearDomains();
189 m_axisItems.value(axis)->handleLabelsChanged(labels);
185 }
190 }
186
191
187 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
192 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
188 {
193 {
189 delete m_chartTheme;
194 delete m_chartTheme;
190
195
191 m_chartTheme = ChartTheme::createTheme(theme);
196 m_chartTheme = ChartTheme::createTheme(theme);
192
197
193 m_chartTheme->decorate(m_chart);
198 m_chartTheme->decorate(m_chart);
194 QMapIterator<QChartSeries*,ChartItem*> i(m_chartItems);
199 QMapIterator<QChartSeries*,ChartItem*> i(m_chartItems);
195
200
196 int index=0;
201 int index=0;
197 while (i.hasNext()) {
202 while (i.hasNext()) {
198 i.next();
203 i.next();
199 index++;
204 index++;
200 m_chartTheme->decorate(i.value(),i.key(),index);
205 m_chartTheme->decorate(i.value(),i.key(),index);
201 }
206 }
202
207
203 m_chartTheme->decorate(m_axisX, m_axisXItem);
208 QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems);
204 m_chartTheme->decorate(m_axisY, m_axisYItem);
209 while (j.hasNext()) {
205
210 j.next();
211 m_chartTheme->decorate(j.key(),j.value());
212 }
206 }
213 }
207
214
208
209 QChart::ChartTheme ChartPresenter::chartTheme()
215 QChart::ChartTheme ChartPresenter::chartTheme()
210 {
216 {
211 return m_chartTheme->id();
217 return m_chartTheme->id();
212 }
218 }
213
219
214 void ChartPresenter::setDefaultAxisX(const QChartAxis& axis)
215 {
216 //if(m_axisX != axis) {
217 m_axisX = axis;
218 m_axisXItem->handleAxisChanged(m_axisX);
219 //}
220 }
221
222 void ChartPresenter::setDefaultAxisY(const QChartAxis& axis)
223 {
224 // if(m_axisY != axis) {
225 m_axisY = axis;
226 m_axisYItem->handleAxisChanged(m_axisY);
227 //}
228 }
229
230 QChartAxis ChartPresenter::defaultAxisX() const
231 {
232 return m_axisX;
233 }
234
235 QChartAxis ChartPresenter::defaultAxisY() const
236 {
237 return m_axisY;
238 }
239
240 QChartAxis ChartPresenter::axisY(int id) const
241 {
242 return m_axis.value(id);
243 }
244
245 int ChartPresenter::addAxisY(const QChartAxis& axis)
246 {
247 int key =0 ;
248
249 while(m_axis.contains(key)){
250 key++;
251 //TODO overflow
252 }
253
254 m_axis.insert(key,axis);
255 m_axisItems.insert(key,new AxisItem(AxisItem::Y_AXIS,m_chart));
256
257 return key;
258 }
259
260
261 void ChartPresenter::removeAxisY(int id)
262 {
263 m_axis.remove(id);
264 delete m_axisItems.take(id);
265 }
266
220
267 #include "moc_chartpresenter_p.cpp"
221 #include "moc_chartpresenter_p.cpp"
268
222
269 QTCOMMERCIALCHART_END_NAMESPACE
223 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,76 +1,61
1 #ifndef CHARTPRESENTER_H_
1 #ifndef CHARTPRESENTER_H_
2 #define CHARTPRESENTER_H_
2 #define CHARTPRESENTER_H_
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qchart.h" //becouse of QChart::ChartThemeId //TODO
5 #include "qchart.h" //becouse of QChart::ChartThemeId //TODO
6 #include "qchartaxis.h"
6 #include "qchartaxis.h"
7 #include <QRectF>
7 #include <QRectF>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 class ChartItem;
11 class ChartItem;
12 class QChartSeries;
12 class QChartSeries;
13 class ChartDataSet;
13 class ChartDataSet;
14 //class QChart;
14 //class QChart;
15 class Domain;
15 class Domain;
16 class AxisItem;
16 class AxisItem;
17 class ChartTheme;
17 class ChartTheme;
18
18
19 class ChartPresenter: public QObject
19 class ChartPresenter: public QObject
20 {
20 {
21 Q_OBJECT
21 Q_OBJECT
22 public:
22 public:
23 ChartPresenter(QChart* chart,ChartDataSet *dataset);
23 ChartPresenter(QChart* chart,ChartDataSet *dataset);
24 virtual ~ChartPresenter();
24 virtual ~ChartPresenter();
25
25
26 void setMargin(int margin);
26 void setMargin(int margin);
27 int margin() const;
27 int margin() const;
28
28
29 void zoomInToRect(const QRectF& rectangle);
29 QRectF geometry() const;
30 void zoomIn();
31 void zoomOut();
32 void zoomReset();
33
30
34 void setChartTheme(QChart::ChartTheme theme);
31 void setChartTheme(QChart::ChartTheme theme);
35 QChart::ChartTheme chartTheme();
32 QChart::ChartTheme chartTheme();
36
33
37 void setDefaultAxisX(const QChartAxis& axis);
38 void setDefaultAxisY(const QChartAxis& axis);
39 QChartAxis defaultAxisX() const;
40 QChartAxis defaultAxisY() const;
41 QChartAxis axisY(int id) const;
42 int addAxisY(const QChartAxis& axis);
43 void removeAxisY(int id);
44
45 private:
34 private:
46 void createConnections();
35 void createConnections();
47
36
48 public slots:
37 public slots:
49 void handleSeriesAdded(QChartSeries* series);
38 void handleSeriesAdded(QChartSeries* series);
50 void handleSeriesRemoved(QChartSeries* series){};
39 void handleSeriesRemoved(QChartSeries* series);
40 void handleAxisAdded(QChartAxis* axis);
41 void handleAxisRemoved(QChartAxis* axis);
42 void handleSeriesDomainChanged(QChartSeries* series, const Domain& domain);
43 void handleAxisLabelsChanged(QChartAxis* axis, const QStringList& labels);
51 void handleSeriesChanged(QChartSeries* series);
44 void handleSeriesChanged(QChartSeries* series);
52 //void handleDomainChanged(Domain oldDomain,Domain newDomain);
53 void handleGeometryChanged();
45 void handleGeometryChanged();
54 signals:
46 signals:
55 void geometryChanged(const QRectF& rect);
47 void geometryChanged(const QRectF& rect);
56 private:
48 private:
57 QMap<QChartSeries*,ChartItem*> m_chartItems;
49 QMap<QChartSeries*,ChartItem*> m_chartItems;
58 QMap<int,AxisItem*> m_axisItems;
50 QMap<QChartAxis*,AxisItem*> m_axisItems;
59 QMap<int,QChartAxis> m_axis;
60 QChart* m_chart;
51 QChart* m_chart;
61 ChartDataSet* m_dataset;
52 ChartDataSet* m_dataset;
62 QVector<Domain> m_domains;
63 ChartTheme *m_chartTheme;
53 ChartTheme *m_chartTheme;
64 QChartAxis m_axisX;
65 AxisItem* m_axisXItem;
66 QChartAxis m_axisY;
67 AxisItem* m_axisYItem;
68 int m_domainIndex;
69 int m_marginSize;
54 int m_marginSize;
70 QRectF m_rect;
55 QRectF m_rect;
71
56
72 };
57 };
73
58
74 QTCOMMERCIALCHART_END_NAMESPACE
59 QTCOMMERCIALCHART_END_NAMESPACE
75
60
76 #endif /* CHARTPRESENTER_H_ */
61 #endif /* CHARTPRESENTER_H_ */
@@ -1,231 +1,229
1 #include "charttheme_p.h"
1 #include "charttheme_p.h"
2 #include "qchart.h"
2 #include "qchart.h"
3 #include "qchartaxis.h"
3 #include "qchartaxis.h"
4
4
5
5
6 //series
6 //series
7 #include "qbarset.h"
7 #include "qbarset.h"
8 #include "qbarchartseries.h"
8 #include "qbarchartseries.h"
9 #include "qstackedbarchartseries.h"
9 #include "qstackedbarchartseries.h"
10 #include "qpercentbarchartseries.h"
10 #include "qpercentbarchartseries.h"
11 #include "qlinechartseries.h"
11 #include "qlinechartseries.h"
12 #include "qscatterseries.h"
12 #include "qscatterseries.h"
13 #include "qpieseries.h"
13 #include "qpieseries.h"
14 #include "qpieslice.h"
14 #include "qpieslice.h"
15
15
16 //items
16 //items
17 #include "axisitem_p.h"
17 #include "axisitem_p.h"
18 #include "barpresenter.h"
18 #include "barpresenter.h"
19 #include "stackedbarpresenter.h"
19 #include "stackedbarpresenter.h"
20 #include "linechartitem_p.h"
20 #include "linechartitem_p.h"
21 #include "percentbarpresenter.h"
21 #include "percentbarpresenter.h"
22 #include "scatterpresenter_p.h"
22 #include "scatterpresenter_p.h"
23 #include "piepresenter.h"
23 #include "piepresenter.h"
24
24
25 //themes
25 //themes
26 #include "chartthemevanilla_p.h"
26 #include "chartthemevanilla_p.h"
27 #include "chartthemeicy_p.h"
27 #include "chartthemeicy_p.h"
28 #include "chartthemegrayscale_p.h"
28 #include "chartthemegrayscale_p.h"
29 #include "chartthemescientific_p.h"
29 #include "chartthemescientific_p.h"
30
30
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 /* TODO
34 /* TODO
35 case QChart::ChartThemeUnnamed1:
35 case QChart::ChartThemeUnnamed1:
36 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2));
36 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2));
37 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2));
37 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2));
38 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2));
38 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2));
39 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2));
39 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2));
40 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2));
40 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2));
41
41
42 m_gradientStartColor = QColor(QRgb(0xfff3dc9e));
42 m_gradientStartColor = QColor(QRgb(0xfff3dc9e));
43 m_gradientEndColor = QColor(QRgb(0xffafafaf));
43 m_gradientEndColor = QColor(QRgb(0xffafafaf));
44 */
44 */
45
45
46 ChartTheme::ChartTheme(QChart::ChartTheme id)
46 ChartTheme::ChartTheme(QChart::ChartTheme id)
47 {
47 {
48 m_id = id;
48 m_id = id;
49 m_seriesColor.append(QRgb(0xff000000));
49 m_seriesColor.append(QRgb(0xff000000));
50 m_seriesColor.append(QRgb(0xff707070));
50 m_seriesColor.append(QRgb(0xff707070));
51 m_gradientStartColor = QColor(QRgb(0xffffffff));
51 m_gradientStartColor = QColor(QRgb(0xffffffff));
52 m_gradientEndColor = QColor(QRgb(0xffafafaf));
52 m_gradientEndColor = QColor(QRgb(0xffafafaf));
53 }
53 }
54
54
55
55
56 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
56 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
57 {
57 {
58 switch(theme) {
58 switch(theme) {
59 case QChart::ChartThemeDefault:
59 case QChart::ChartThemeDefault:
60 return new ChartTheme();
60 return new ChartTheme();
61 case QChart::ChartThemeVanilla:
61 case QChart::ChartThemeVanilla:
62 return new ChartThemeVanilla();
62 return new ChartThemeVanilla();
63 case QChart::ChartThemeIcy:
63 case QChart::ChartThemeIcy:
64 return new ChartThemeIcy();
64 return new ChartThemeIcy();
65 case QChart::ChartThemeGrayscale:
65 case QChart::ChartThemeGrayscale:
66 return new ChartThemeGrayscale();
66 return new ChartThemeGrayscale();
67 case QChart::ChartThemeScientific:
67 case QChart::ChartThemeScientific:
68 return new ChartThemeScientific();
68 return new ChartThemeScientific();
69 }
69 }
70 }
70 }
71
71
72 void ChartTheme::decorate(QChart* chart)
72 void ChartTheme::decorate(QChart* chart)
73 {
73 {
74 QLinearGradient backgroundGradient;
74 QLinearGradient backgroundGradient;
75 backgroundGradient.setColorAt(0.0, m_gradientStartColor);
75 backgroundGradient.setColorAt(0.0, m_gradientStartColor);
76 backgroundGradient.setColorAt(1.0, m_gradientEndColor);
76 backgroundGradient.setColorAt(1.0, m_gradientEndColor);
77 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
77 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
78 chart->setChartBackgroundBrush(backgroundGradient);
78 chart->setChartBackgroundBrush(backgroundGradient);
79 }
79 }
80 //TODO helper to by removed later
80 //TODO helper to by removed later
81 void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count)
81 void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count)
82 {
82 {
83 switch(series->type())
83 switch(series->type())
84 {
84 {
85 case QChartSeries::SeriesTypeLine: {
85 case QChartSeries::SeriesTypeLine: {
86 QLineChartSeries* s = static_cast<QLineChartSeries*>(series);
86 QLineChartSeries* s = static_cast<QLineChartSeries*>(series);
87 LineChartItem* i = static_cast<LineChartItem*>(item);
87 LineChartItem* i = static_cast<LineChartItem*>(item);
88 decorate(i,s,count);
88 decorate(i,s,count);
89 break;
89 break;
90 }
90 }
91 case QChartSeries::SeriesTypeBar: {
91 case QChartSeries::SeriesTypeBar: {
92 QBarChartSeries* b = static_cast<QBarChartSeries*>(series);
92 QBarChartSeries* b = static_cast<QBarChartSeries*>(series);
93 BarPresenter* i = static_cast<BarPresenter*>(item);
93 BarPresenter* i = static_cast<BarPresenter*>(item);
94 decorate(i,b,count);
94 decorate(i,b,count);
95 break;
95 break;
96 }
96 }
97 case QChartSeries::SeriesTypeStackedBar: {
97 case QChartSeries::SeriesTypeStackedBar: {
98 QStackedBarChartSeries* s = static_cast<QStackedBarChartSeries*>(series);
98 QStackedBarChartSeries* s = static_cast<QStackedBarChartSeries*>(series);
99 StackedBarPresenter* i = static_cast<StackedBarPresenter*>(item);
99 StackedBarPresenter* i = static_cast<StackedBarPresenter*>(item);
100 decorate(i,s,count);
100 decorate(i,s,count);
101 break;
101 break;
102 }
102 }
103 case QChartSeries::SeriesTypePercentBar: {
103 case QChartSeries::SeriesTypePercentBar: {
104 QPercentBarChartSeries* s = static_cast<QPercentBarChartSeries*>(series);
104 QPercentBarChartSeries* s = static_cast<QPercentBarChartSeries*>(series);
105 PercentBarPresenter* i = static_cast<PercentBarPresenter*>(item);
105 PercentBarPresenter* i = static_cast<PercentBarPresenter*>(item);
106 decorate(i,s,count);
106 decorate(i,s,count);
107 break;
107 break;
108 }
108 }
109 case QChartSeries::SeriesTypeScatter: {
109 case QChartSeries::SeriesTypeScatter: {
110 QScatterSeries* s = qobject_cast<QScatterSeries*>(series);
110 QScatterSeries* s = qobject_cast<QScatterSeries*>(series);
111 Q_ASSERT(s);
111 Q_ASSERT(s);
112 ScatterPresenter* i = static_cast<ScatterPresenter*>(item);
112 ScatterPresenter* i = static_cast<ScatterPresenter*>(item);
113 Q_ASSERT(i);
113 Q_ASSERT(i);
114 decorate(i, s, count);
114 decorate(i, s, count);
115 break;
115 break;
116 }
116 }
117 case QChartSeries::SeriesTypePie: {
117 case QChartSeries::SeriesTypePie: {
118 QPieSeries* s = static_cast<QPieSeries*>(series);
118 QPieSeries* s = static_cast<QPieSeries*>(series);
119 PiePresenter* i = static_cast<PiePresenter*>(item);
119 PiePresenter* i = static_cast<PiePresenter*>(item);
120 decorate(i,s,count);
120 decorate(i,s,count);
121 break;
121 break;
122 }
122 }
123 default:
123 default:
124 qDebug()<<"Wrong item to be decorated by theme";
124 qDebug()<<"Wrong item to be decorated by theme";
125 break;
125 break;
126 }
126 }
127
127
128 }
128 }
129
129
130 void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int count)
130 void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int count)
131 {
131 {
132 QPen pen;
132 QPen pen;
133 if(pen != series->pen()){
133 if(pen != series->pen()){
134 item->setPen(series->pen());
134 item->setPen(series->pen());
135 return;
135 return;
136 }
136 }
137 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
137 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
138 pen.setWidthF(2);
138 pen.setWidthF(2);
139 item->setPen(pen);
139 item->setPen(pen);
140 }
140 }
141
141
142 void ChartTheme::decorate(BarPresenter* item, QBarChartSeries* series,int count)
142 void ChartTheme::decorate(BarPresenter* item, QBarChartSeries* series,int count)
143 {
143 {
144 for (int i=0; i<series->countSets(); i++) {
144 for (int i=0; i<series->countSets(); i++) {
145 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
145 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
146 }
146 }
147 }
147 }
148
148
149 void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarChartSeries* series,int count)
149 void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarChartSeries* series,int count)
150 {
150 {
151 for (int i=0; i<series->countSets(); i++) {
151 for (int i=0; i<series->countSets(); i++) {
152 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
152 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
153 }
153 }
154 }
154 }
155
155
156 void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarChartSeries* series,int count)
156 void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarChartSeries* series,int count)
157 {
157 {
158 for (int i=0; i<series->countSets(); i++) {
158 for (int i=0; i<series->countSets(); i++) {
159 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
159 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
160 }
160 }
161 }
161 }
162
162
163 void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count)
163 void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count)
164 {
164 {
165 Q_ASSERT(presenter);
165 Q_ASSERT(presenter);
166 Q_ASSERT(series);
166 Q_ASSERT(series);
167
167
168 QColor color = m_seriesColor.at(count % m_seriesColor.size());
168 QColor color = m_seriesColor.at(count % m_seriesColor.size());
169 // TODO: define alpha in the theme? or in the series?
169 // TODO: define alpha in the theme? or in the series?
170 color.setAlpha(120);
170 color.setAlpha(120);
171
171
172 QBrush brush(color, Qt::SolidPattern);
172 QBrush brush(color, Qt::SolidPattern);
173 presenter->m_markerBrush = brush;
173 presenter->m_markerBrush = brush;
174
174
175 QPen pen(brush, 1);
175 QPen pen(brush, 1);
176 pen.setColor(color);
176 pen.setColor(color);
177 presenter->m_markerPen = pen;
177 presenter->m_markerPen = pen;
178 }
178 }
179
179
180 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
180 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
181 {
181 {
182 // create a list of slice colors based on current theme
182 // create a list of slice colors based on current theme
183 int i = 0;
183 int i = 0;
184 QList<QColor> colors;
184 QList<QColor> colors;
185 while (colors.count() < series->count()) {
185 while (colors.count() < series->count()) {
186
186
187 // get base color
187 // get base color
188 QColor c = m_seriesColor[i++];
188 QColor c = m_seriesColor[i++];
189 i = i % m_seriesColor.count();
189 i = i % m_seriesColor.count();
190
190
191 // -1 means achromatic color -> cannot manipulate lightness
191 // -1 means achromatic color -> cannot manipulate lightness
192 // TODO: find a better way to randomize lightness
192 // TODO: find a better way to randomize lightness
193 if (c.toHsv().hue() == -1)
193 if (c.toHsv().hue() == -1)
194 qWarning() << "ChartTheme::decorate() warning: achromatic theme color";
194 qWarning() << "ChartTheme::decorate() warning: achromatic theme color";
195
195
196 // randomize lightness
196 // randomize lightness
197 qreal f = 50 + (qrand() % 100); // 50 is 50% darker, 100 is the same, 150 is 50% lighter
197 qreal f = 50 + (qrand() % 100); // 50 is 50% darker, 100 is the same, 150 is 50% lighter
198 c = c.lighter(f);
198 c = c.lighter(f);
199
199
200 // find duplicates
200 // find duplicates
201 bool isUnique = true;
201 bool isUnique = true;
202 foreach (QColor color, colors) {
202 foreach (QColor color, colors) {
203 if (c == color)
203 if (c == color)
204 isUnique = false;
204 isUnique = false;
205 }
205 }
206
206
207 // add to array if unique
207 // add to array if unique
208 //if (isUnique)
208 //if (isUnique)
209 colors << c;
209 colors << c;
210 }
210 }
211
211
212 // finally update colors
212 // finally update colors
213 foreach (QPieSlice* s, series->slices()) {
213 foreach (QPieSlice* s, series->slices()) {
214 s->setPen(QPen(Qt::black)); // TODO: get from theme
214 s->setPen(QPen(Qt::black)); // TODO: get from theme
215 s->setBrush(colors.takeFirst());
215 s->setBrush(colors.takeFirst());
216 }
216 }
217 }
217 }
218
218
219
219
220 void ChartTheme::decorate(QChartAxis& axis,AxisItem* item)
220 void ChartTheme::decorate(QChartAxis* axis,AxisItem* item)
221 {
221 {
222 //TODO: dummy defults for now
222 //TODO: dummy defults for now
223
223 axis->setLabelsBrush(Qt::black);
224 axis.setLabelsBrush(Qt::black);
224 axis->setLabelsPen(Qt::NoPen);
225 axis.setLabelsPen(Qt::NoPen);
225 axis->setShadesPen(Qt::NoPen);
226 axis.setShadesPen(Qt::NoPen);
226 axis->setShadesOpacity(0.5);
227 axis.setShadesOpacity(0.5);
228 item->handleAxisChanged(axis);
229 }
227 }
230
228
231 QTCOMMERCIALCHART_END_NAMESPACE
229 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,52 +1,52
1 #ifndef CHARTTHEME_H
1 #ifndef CHARTTHEME_H
2 #define CHARTTHEME_H
2 #define CHARTTHEME_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qchart.h"
5 #include "qchart.h"
6 #include <QColor>
6 #include <QColor>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class ChartItem;
10 class ChartItem;
11 class QChartSeries;
11 class QChartSeries;
12 class LineChartItem;
12 class LineChartItem;
13 class QLineChartSeries;
13 class QLineChartSeries;
14 class BarPresenter;
14 class BarPresenter;
15 class QBarChartSeries;
15 class QBarChartSeries;
16 class StackedBarPresenter;
16 class StackedBarPresenter;
17 class QStackedBarChartSeries;
17 class QStackedBarChartSeries;
18 class QPercentBarChartSeries;
18 class QPercentBarChartSeries;
19 class PercentBarPresenter;
19 class PercentBarPresenter;
20 class QScatterSeries;
20 class QScatterSeries;
21 class ScatterPresenter;
21 class ScatterPresenter;
22 class PiePresenter;
22 class PiePresenter;
23 class QPieSeries;
23 class QPieSeries;
24
24
25 class ChartTheme
25 class ChartTheme
26 {
26 {
27 protected:
27 protected:
28 explicit ChartTheme(QChart::ChartTheme id = QChart::ChartThemeDefault);
28 explicit ChartTheme(QChart::ChartTheme id = QChart::ChartThemeDefault);
29 public:
29 public:
30 static ChartTheme* createTheme(QChart::ChartTheme theme);
30 static ChartTheme* createTheme(QChart::ChartTheme theme);
31 QChart::ChartTheme id() const {return m_id;}
31 QChart::ChartTheme id() const {return m_id;}
32 void decorate(QChart* chart);
32 void decorate(QChart* chart);
33 void decorate(ChartItem* item, QChartSeries* series,int count);
33 void decorate(ChartItem* item, QChartSeries* series,int count);
34 void decorate(LineChartItem* item, QLineChartSeries*, int count);
34 void decorate(LineChartItem* item, QLineChartSeries*, int count);
35 void decorate(BarPresenter* item, QBarChartSeries* series,int count);
35 void decorate(BarPresenter* item, QBarChartSeries* series,int count);
36 void decorate(StackedBarPresenter* item, QStackedBarChartSeries* series,int count);
36 void decorate(StackedBarPresenter* item, QStackedBarChartSeries* series,int count);
37 void decorate(PercentBarPresenter* item, QPercentBarChartSeries* series,int count);
37 void decorate(PercentBarPresenter* item, QPercentBarChartSeries* series,int count);
38 void decorate(ScatterPresenter* presenter, QScatterSeries* series, int count);
38 void decorate(ScatterPresenter* presenter, QScatterSeries* series, int count);
39 void decorate(PiePresenter* item, QPieSeries* series, int count);
39 void decorate(PiePresenter* item, QPieSeries* series, int count);
40 void decorate(QChartAxis& axis,AxisItem* item);
40 void decorate(QChartAxis* axis,AxisItem* item);
41
41
42 protected:
42 protected:
43 QChart::ChartTheme m_id;
43 QChart::ChartTheme m_id;
44 QColor m_gradientStartColor;
44 QColor m_gradientStartColor;
45 QColor m_gradientEndColor;
45 QColor m_gradientEndColor;
46 QList<QColor> m_seriesColor;
46 QList<QColor> m_seriesColor;
47
47
48 };
48 };
49
49
50 QTCOMMERCIALCHART_END_NAMESPACE
50 QTCOMMERCIALCHART_END_NAMESPACE
51
51
52 #endif // CHARTTHEME_H
52 #endif // CHARTTHEME_H
@@ -1,209 +1,163
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
5
6 <<<<<<< HEAD
6 //series
7 //series
7 #include "qbarchartseries.h"
8 #include "qbarchartseries.h"
8 #include "qstackedbarchartseries.h"
9 #include "qstackedbarchartseries.h"
9 #include "qpercentbarchartseries.h"
10 #include "qpercentbarchartseries.h"
10 #include "qlinechartseries.h"
11 #include "qlinechartseries.h"
11 #include "qscatterseries.h"
12 #include "qscatterseries.h"
12 //#include "scatterseries_p.h"
13 //#include "scatterseries_p.h"
13 #include "qpieseries.h"
14 #include "qpieseries.h"
14
15 #include <QGraphicsScene>
15 #include <QGraphicsScene>
16 #include <QGraphicsSceneResizeEvent>
16 #include <QGraphicsSceneResizeEvent>
17 #include <QDebug>
17 #include <QDebug>
18
18
19 QTCOMMERCIALCHART_BEGIN_NAMESPACE
19 QTCOMMERCIALCHART_BEGIN_NAMESPACE
20
20
21 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
21 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
22 m_backgroundItem(0),
22 m_backgroundItem(0),
23 m_titleItem(0),
23 m_titleItem(0),
24 m_dataset(new ChartDataSet(this)),
24 m_dataset(new ChartDataSet(this)),
25 m_presenter(new ChartPresenter(this,m_dataset))
25 m_presenter(new ChartPresenter(this,m_dataset))
26 {
26 {
27 }
27 }
28
28
29 QChart::~QChart() {}
29 QChart::~QChart() {}
30
30
31 void QChart::addSeries(QChartSeries* series)
31 void QChart::addSeries(QChartSeries* series,QChartAxis* axisY)
32 {
32 {
33 m_dataset->addSeries(series);
33 m_dataset->addSeries(series,axisY);
34 }
34 }
35
36 //TODO on review, is it really needed ??
37 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
38 {
39 QChartSeries *series(0);
40
41 switch (type) {
42 case QChartSeries::SeriesTypeLine: {
43 series = new QLineChartSeries(this);
44 break;
45 }
46 case QChartSeries::SeriesTypeBar: {
47 //series = new BarChartSeries(this);
48 break;
49 }
50 case QChartSeries::SeriesTypeStackedBar: {
51 //series = new StackedBarChartSeries(this);
52 break;
53 }
54 case QChartSeries::SeriesTypePercentBar: {
55 //series = new PercentBarChartSeries(this);
56 break;
57 }
58 case QChartSeries::SeriesTypeScatter: {
59 series = new QScatterSeries(this);
60 break;
61 }
62 case QChartSeries::SeriesTypePie: {
63 series = new QPieSeries(this);
64 break;
65 }
66 default:
67 Q_ASSERT(false);
68 break;
69 }
70
35
71 addSeries(series);
36 void QChart::removeSeries(QChartSeries* series)
72 return series;
37 {
38 m_dataset->removeSeries(series);
73 }
39 }
74
40
75 void QChart::setChartBackgroundBrush(const QBrush& brush)
41 void QChart::setChartBackgroundBrush(const QBrush& brush)
76 {
42 {
77
43 createChartBackgroundItem();
78 if(!m_backgroundItem) {
79 m_backgroundItem = new QGraphicsRectItem(this);
80 m_backgroundItem->setZValue(-1);
81 }
82
83 m_backgroundItem->setBrush(brush);
44 m_backgroundItem->setBrush(brush);
84 m_backgroundItem->update();
45 m_backgroundItem->update();
85 }
46 }
86
47
87 void QChart::setChartBackgroundPen(const QPen& pen)
48 void QChart::setChartBackgroundPen(const QPen& pen)
88 {
49 {
89
50 createChartBackgroundItem();
90 if(!m_backgroundItem) {
91 m_backgroundItem = new QGraphicsRectItem(this);
92 m_backgroundItem->setZValue(-1);
93 }
94
95 m_backgroundItem->setPen(pen);
51 m_backgroundItem->setPen(pen);
96 m_backgroundItem->update();
52 m_backgroundItem->update();
97 }
53 }
98
54
99 void QChart::setChartTitle(const QString& title)
55 void QChart::setChartTitle(const QString& title)
100 {
56 {
101 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
57 createChartTitleItem();
102 m_titleItem->setPlainText(title);
58 m_titleItem->setPlainText(title);
103 }
59 }
104
60
105 void QChart::setChartTitleFont(const QFont& font)
61 void QChart::setChartTitleFont(const QFont& font)
106 {
62 {
107 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
63 createChartTitleItem();
108 m_titleItem->setFont(font);
64 m_titleItem->setFont(font);
109 }
65 }
110
66
67 void QChart::createChartBackgroundItem()
68 {
69 if(!m_backgroundItem) {
70 m_backgroundItem = new QGraphicsRectItem(this);
71 m_backgroundItem->setZValue(-1);
72 }
73 }
74
75 void QChart::createChartTitleItem()
76 {
77 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
78 }
79
111 int QChart::margin() const
80 int QChart::margin() const
112 {
81 {
113 return m_presenter->margin();
82 return m_presenter->margin();
114 }
83 }
115
84
116 void QChart::setMargin(int margin)
85 void QChart::setMargin(int margin)
117 {
86 {
118 m_presenter->setMargin(margin);
87 m_presenter->setMargin(margin);
119 }
88 }
120
89
121 void QChart::setChartTheme(QChart::ChartTheme theme)
90 void QChart::setChartTheme(QChart::ChartTheme theme)
122 {
91 {
123 m_presenter->setChartTheme(theme);
92 m_presenter->setChartTheme(theme);
124 }
93 }
125
94
126 QChart::ChartTheme QChart::chartTheme() const
95 QChart::ChartTheme QChart::chartTheme() const
127 {
96 {
128 return m_presenter->chartTheme();
97 return m_presenter->chartTheme();
129 }
98 }
130
99
131 void QChart::zoomInToRect(const QRectF& rectangle)
100 void QChart::zoomIn()
132 {
101 {
133 m_presenter->zoomInToRect(rectangle);
102 if (!m_dataset->nextDomain()) {
103 QRectF rect = m_presenter->geometry();
104 rect.setWidth(rect.width()/2);
105 rect.setHeight(rect.height()/2);
106 rect.moveCenter(m_presenter->geometry().center());
107 zoomIn(rect);
108 }
134 }
109 }
135
110
136 void QChart::zoomIn()
111 void QChart::zoomIn(const QRectF& rect)
137 {
112 {
138 m_presenter->zoomIn();
113 if(!rect.isValid()) return;
114 QRectF r = rect.normalized();
115 int margin = m_presenter->margin();
116 r.translate(-margin, -margin);
117 m_dataset->addDomain(r,m_presenter->geometry());
139 }
118 }
140
119
141 void QChart::zoomOut()
120 void QChart::zoomOut()
142 {
121 {
143 m_presenter->zoomOut();
122 m_dataset->previousDomain();
144 }
123 }
145
124
146 void QChart::zoomReset()
125 void QChart::zoomReset()
147 {
126 {
148 m_presenter->zoomReset();
127 m_dataset->clearDomains();
149 }
150
151 void QChart::setDefaultAxisX(const QChartAxis& axis)
152 {
153 m_presenter->setDefaultAxisX(axis);
154 }
155
156 void QChart::setDefaultAxisY(const QChartAxis& axis)
157 {
158 m_presenter->setDefaultAxisY(axis);
159 }
160
161 QChartAxis QChart::defaultAxisX() const
162 {
163 return m_presenter->defaultAxisX();
164 }
165
166 QChartAxis QChart::defaultAxisY() const
167 {
168 return m_presenter->defaultAxisY();
169 }
170
171 int QChart::addAxisY(const QChartAxis& axis)
172 {
173 return m_presenter->addAxisY(axis);
174 }
128 }
175
129
176 QChartAxis QChart::axisY(int id) const
130 QChartAxis* QChart::axisX() const
177 {
131 {
178 return m_presenter->axisY(id);
132 return m_dataset->axisX();
179 }
133 }
180
134
181 void QChart::removeAxisY(int id)
135 QChartAxis* QChart::axisY() const
182 {
136 {
183 m_presenter->removeAxisY(id);
137 return m_dataset->axisY();
184 }
138 }
185
139
186 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
140 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
187 {
141 {
188
142
189 m_rect = QRectF(QPoint(0,0),event->newSize());
143 m_rect = QRectF(QPoint(0,0),event->newSize());
190 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
144 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
191
145
192 // recalculate title position
146 // recalculate title position
193 if (m_titleItem) {
147 if (m_titleItem) {
194 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
148 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
195 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
149 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
196 }
150 }
197
151
198 //recalculate background gradient
152 //recalculate background gradient
199 if (m_backgroundItem) {
153 if (m_backgroundItem) {
200 m_backgroundItem->setRect(rect);
154 m_backgroundItem->setRect(rect);
201 }
155 }
202
156
203 QGraphicsWidget::resizeEvent(event);
157 QGraphicsWidget::resizeEvent(event);
204 update();
158 update();
205 }
159 }
206
160
207 #include "moc_qchart.cpp"
161 #include "moc_qchart.cpp"
208
162
209 QTCOMMERCIALCHART_END_NAMESPACE
163 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,93 +1,84
1 #ifndef CHART_H
1 #ifndef CHART_H
2 #define CHART_H
2 #define CHART_H
3
3
4 #include <qchartglobal.h>
4 #include <qchartglobal.h>
5 #include <qchartseries.h>
5 #include <qchartseries.h>
6 #include <QGraphicsWidget>
6 #include <QGraphicsWidget>
7 #include <QLinearGradient>
7 #include <QLinearGradient>
8 #include <QFont>
8 #include <QFont>
9
9
10 class QGraphicsSceneResizeEvent;
10 class QGraphicsSceneResizeEvent;
11
11
12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13
13
14 class AxisItem;
14 class AxisItem;
15 class QChartSeries;
15 class QChartSeries;
16 class PlotDomain;
16 class PlotDomain;
17 class BarPresenter;
17 class BarPresenter;
18 class QChartAxis;
18 class QChartAxis;
19 class ChartTheme;
19 class ChartTheme;
20 class ChartItem;
20 class ChartItem;
21 class ChartDataSet;
21 class ChartDataSet;
22 class ChartPresenter;
22 class ChartPresenter;
23
23
24 // TODO: We don't need to have QChart tied to QGraphicsItem:
25 //class QTCOMMERCIALCHART_EXPORT QChart
26 //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem {
27 // public: QChartGraphicsItem(QChart &chart);
28
29 /*!
24 /*!
30 * TODO: define the responsibilities
25 * TODO: define the responsibilities
31 */
26 */
32 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
27 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
33 {
28 {
34 Q_OBJECT
29 Q_OBJECT
35 public:
30 public:
36 enum ChartTheme {
31 enum ChartTheme {
37 /*! The default theme follows the GUI style of the Operating System */
32 /*! The default theme follows the GUI style of the Operating System */
38 ChartThemeDefault,
33 ChartThemeDefault,
39 ChartThemeVanilla,
34 ChartThemeVanilla,
40 ChartThemeIcy,
35 ChartThemeIcy,
41 ChartThemeGrayscale,
36 ChartThemeGrayscale,
42 ChartThemeScientific,
37 ChartThemeScientific,
43 //ChartThemeUnnamed1
38 //ChartThemeUnnamed1
44 };
39 };
45
40
46 public:
41 public:
47 QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
42 QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
48 ~QChart();
43 ~QChart();
49
44
50 void addSeries(QChartSeries* series);
45 void addSeries(QChartSeries* series,QChartAxis *axisY=0);
51
46 void removeSeries(QChartSeries* series);
52 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
53 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
54 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
55
47
56 void setMargin(int margin);
48 void setMargin(int margin);
57 int margin() const;
49 int margin() const;
58 void setChartTheme(QChart::ChartTheme theme);
50 void setChartTheme(QChart::ChartTheme theme);
59 QChart::ChartTheme chartTheme() const;
51 QChart::ChartTheme chartTheme() const;
60
52
61 void setChartTitle(const QString& title);
53 void setChartTitle(const QString& title);
62 void setChartTitleFont(const QFont& font);
54 void setChartTitleFont(const QFont& font);
63 void setChartBackgroundBrush(const QBrush& brush);
55 void setChartBackgroundBrush(const QBrush& brush);
64 void setChartBackgroundPen(const QPen& pen);
56 void setChartBackgroundPen(const QPen& pen);
65
57
66 void zoomInToRect(const QRectF& rectangle);
67 void zoomIn();
58 void zoomIn();
59 void zoomIn(const QRectF& rect);
68 void zoomOut();
60 void zoomOut();
69 void zoomReset();
61 void zoomReset();
70
62
71 void setDefaultAxisX(const QChartAxis& axis);
63 QChartAxis* axisX() const;
72 void setDefaultAxisY(const QChartAxis& axis);
64 QChartAxis* axisY() const;
73 QChartAxis defaultAxisX() const;
74 QChartAxis defaultAxisY() const;
75 QChartAxis axisY(int id) const;
76 int addAxisY(const QChartAxis& axis);
77 void removeAxisY(int id);
78
65
79 protected:
66 protected:
80 void resizeEvent(QGraphicsSceneResizeEvent *event);
67 void resizeEvent(QGraphicsSceneResizeEvent *event);
81
68
82 private:
69 private:
70 inline void createChartBackgroundItem();
71 inline void createChartTitleItem();
72
73 private:
83 Q_DISABLE_COPY(QChart)
74 Q_DISABLE_COPY(QChart)
84 QGraphicsRectItem* m_backgroundItem;
75 QGraphicsRectItem* m_backgroundItem;
85 QGraphicsTextItem* m_titleItem;
76 QGraphicsTextItem* m_titleItem;
86 QRectF m_rect;
77 QRectF m_rect;
87 ChartDataSet *m_dataset;
78 ChartDataSet *m_dataset;
88 ChartPresenter *m_presenter;
79 ChartPresenter *m_presenter;
89 };
80 };
90
81
91 QTCOMMERCIALCHART_END_NAMESPACE
82 QTCOMMERCIALCHART_END_NAMESPACE
92
83
93 #endif
84 #endif
@@ -1,86 +1,154
1 #include "qchartaxis.h"
1 #include "qchartaxis.h"
2
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
4
5 QChartAxis::QChartAxis():
5 QChartAxis::QChartAxis(QObject* parent):QObject(parent),
6 m_axisVisible(true),
6 m_axisVisible(true),
7 m_gridVisible(true),
7 m_gridVisible(true),
8 m_labelsVisible(true),
8 m_labelsVisible(true),
9 m_labelsAngle(0),
9 m_shadesVisible(true),
10 m_shadesVisible(true),
10 m_shadesOpacity(1.0)
11 m_shadesOpacity(1.0),
12 m_min(0),
13 m_max(0),
14 m_ticksCount(4)
11 {
15 {
12 // TODO Auto-generated constructor stub
13
16
14 }
17 }
15
18
16 QChartAxis::~QChartAxis()
19 QChartAxis::~QChartAxis()
17 {
20 {
18 // TODO Auto-generated destructor stub
19 }
21 }
20
22
21 void QChartAxis::setAxisPen(const QPen& pen)
23 void QChartAxis::setAxisPen(const QPen& pen)
22 {
24 {
23 m_axisPen=pen;
25 m_axisPen=pen;
26 emit update(this);
24 }
27 }
25
28
26 void QChartAxis::setAxisVisible(bool visible)
29 void QChartAxis::setAxisVisible(bool visible)
27 {
30 {
28 m_axisVisible=visible;
31 m_axisVisible=visible;
32 emit update(this);
29 }
33 }
30
34
31 void QChartAxis::setGridVisible(bool visible)
35 void QChartAxis::setGridVisible(bool visible)
32 {
36 {
33 m_gridVisible=visible;
37 m_gridVisible=visible;
38 emit update(this);
34 }
39 }
35
40
36 void QChartAxis::setGridPen(const QPen& pen)
41 void QChartAxis::setGridPen(const QPen& pen)
37 {
42 {
38 m_gridPen=pen;
43 m_gridPen=pen;
44 emit update(this);
39 }
45 }
40
46
41 void QChartAxis::setLabelsVisible(bool visible)
47 void QChartAxis::setLabelsVisible(bool visible)
42 {
48 {
43 m_labelsVisible=visible;
49 m_labelsVisible=visible;
50 emit update(this);
44 }
51 }
45
52
46 void QChartAxis::setLabelsPen(const QPen& pen)
53 void QChartAxis::setLabelsPen(const QPen& pen)
47 {
54 {
48 m_labelsPen=pen;
55 m_labelsPen=pen;
56 emit update(this);
49 }
57 }
50
58
51 void QChartAxis::setLabelsBrush(const QBrush& brush)
59 void QChartAxis::setLabelsBrush(const QBrush& brush)
52 {
60 {
53 m_labelsBrush=brush;
61 m_labelsBrush=brush;
62 emit update(this);
54 }
63 }
55
64
56 void QChartAxis::setLabelsFont(const QFont& font)
65 void QChartAxis::setLabelsFont(const QFont& font)
57 {
66 {
58 m_labelsFont=font;
67 m_labelsFont=font;
68 emit update(this);
59 }
69 }
60
70
61 void QChartAxis::setLabelsOrientation(LabelsOrientation orientation)
71 void QChartAxis::setLabelsAngle(int angle)
62 {
72 {
63 m_labelsOrientation=orientation;
73 m_labelsAngle=angle;
74 emit update(this);
64 }
75 }
65
76
66 void QChartAxis::setShadesVisible(bool visible)
77 void QChartAxis::setShadesVisible(bool visible)
67 {
78 {
68 m_shadesVisible=visible;
79 m_shadesVisible=visible;
80 emit update(this);
69 }
81 }
70
82
71 void QChartAxis::setShadesPen(const QPen& pen)
83 void QChartAxis::setShadesPen(const QPen& pen)
72 {
84 {
73 m_shadesPen=pen;
85 m_shadesPen=pen;
86 emit update(this);
74 }
87 }
75
88
76 void QChartAxis::setShadesBrush(const QBrush& brush)
89 void QChartAxis::setShadesBrush(const QBrush& brush)
77 {
90 {
78 m_shadesBrush=brush;
91 m_shadesBrush=brush;
92 emit update(this);
79 }
93 }
80
94
81 void QChartAxis::setShadesOpacity(qreal opacity)
95 void QChartAxis::setShadesOpacity(qreal opacity)
82 {
96 {
83 m_shadesOpacity=opacity;
97 m_shadesOpacity=opacity;
98 emit update(this);
84 }
99 }
85
100
101 void QChartAxis::setMin(qreal min)
102 {
103 if(m_min!=min){
104 m_min=min;
105 emit minChanged(m_min);
106 }
107 }
108
109 void QChartAxis::setMax(qreal max)
110 {
111 if(m_max!=max){
112 m_max=max;
113 emit maxChanged(m_max);
114 }
115 }
116
117 void QChartAxis::setRange(qreal min, qreal max)
118 {
119 setMin(min);
120 setMax(max);
121 }
122
123 void QChartAxis::setTicksCount(int count)
124 {
125 m_ticksCount=count;
126 emit ticksChanged(this);
127 }
128
129 void QChartAxis::addAxisTickLabel(qreal value,const QString& label)
130 {
131 m_ticks.insert(value,label);
132 emit ticksChanged(this);
133 }
134
135 void QChartAxis::removeAxisTickLabel(qreal value)
136 {
137 m_ticks.remove(value);
138 emit ticksChanged(this);
139 }
140
141 QString QChartAxis::axisTickLabel(qreal value) const
142 {
143 return m_ticks.value(value);
144 }
145
146 void QChartAxis::clearAxisTickLabels()
147 {
148 m_ticks.clear();
149 emit ticksChanged(this);
150 }
151
152 #include "moc_qchartaxis.cpp"
153
86 QTCOMMERCIALCHART_END_NAMESPACE
154 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,82 +1,103
1 #ifndef QCHARTAXIS_H_
1 #ifndef QCHARTAXIS_H_
2 #define QCHARTAXIS_H_
2 #define QCHARTAXIS_H_
3
3
4 #include <qchartglobal.h>
4 #include <qchartglobal.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
11 class QTCOMMERCIALCHART_EXPORT QChartAxis : public QObject
12 {
12 {
13 Q_OBJECT
13 public:
14 public:
14 enum LabelsOrientation{ LabelsOrientationHorizontal, LabelsOrientationVertical , LabelsOrientationSlide };
15 QChartAxis(QObject* parent =0);
16 ~QChartAxis();
15
17
16 QChartAxis();
18 //axis handling
17 virtual ~QChartAxis();
18
19 //axis
20 bool isAxisVisible() const { return m_axisVisible;};
19 bool isAxisVisible() const { return m_axisVisible;};
21 void setAxisVisible(bool visible);
20 void setAxisVisible(bool visible);
22 void setAxisPen(const QPen& pen);
21 void setAxisPen(const QPen& pen);
23 QPen axisPen() const { return m_axisPen;};
22 QPen axisPen() const { return m_axisPen;};
24 void setAxisBrush(const QBrush& brush);
23 void setAxisBrush(const QBrush& brush);
25 QBrush axisBrush() const { return m_axisBrush;};
24 QBrush axisBrush() const { return m_axisBrush;};
26
25
27 //grid
26 //grid handling
28 bool isGridVisible() const { return m_gridVisible;};
27 bool isGridVisible() const { return m_gridVisible;};
29 void setGridVisible(bool visible);
28 void setGridVisible(bool visible);
30 void setGridPen(const QPen& pen);
29 void setGridPen(const QPen& pen);
31 QPen gridPen() const {return m_gridPen;}
30 QPen gridPen() const {return m_gridPen;}
32
31
33 //labels
32 //labels handling
34 bool isLabelsVisible() const { return m_labelsVisible;};
33 bool isLabelsVisible() const { return m_labelsVisible;};
35 void setLabelsVisible(bool visible);
34 void setLabelsVisible(bool visible);
36 void setLabelsPen(const QPen& pen);
35 void setLabelsPen(const QPen& pen);
37 QPen labelsPen() const { return m_labelsPen;}
36 QPen labelsPen() const { return m_labelsPen;}
38 void setLabelsBrush(const QBrush& brush);
37 void setLabelsBrush(const QBrush& brush);
39 QBrush labelsBrush() const { return m_labelsBrush;}
38 QBrush labelsBrush() const { return m_labelsBrush;}
40 void setLabelsFont(const QFont& font);
39 void setLabelsFont(const QFont& font);
41 QFont labelFont() const { return m_labelsFont;}
40 QFont labelFont() const { return m_labelsFont;}
42 void setLabelsOrientation(LabelsOrientation orientation);
41 void setLabelsAngle(int angle);
43 LabelsOrientation labelsOrientation() const { return m_labelsOrientation;};
42 int labelsAngle() const { return m_labelsAngle;};
44
43
45 //shades
44 //shades handling
46 bool isShadesVisible() const { return m_shadesVisible;};
45 bool isShadesVisible() const { return m_shadesVisible;};
47 void setShadesVisible(bool visible);
46 void setShadesVisible(bool visible);
48 void setShadesPen(const QPen& pen);
47 void setShadesPen(const QPen& pen);
49 QPen shadesPen() const { return m_shadesPen;}
48 QPen shadesPen() const { return m_shadesPen;}
50 void setShadesBrush(const QBrush& brush);
49 void setShadesBrush(const QBrush& brush);
51 QBrush shadesBrush() const { return m_shadesBrush;}
50 QBrush shadesBrush() const { return m_shadesBrush;}
52 void setShadesOpacity(qreal opacity);
51 void setShadesOpacity(qreal opacity);
53 qreal shadesOpacity() const { return m_shadesOpacity;}
52 qreal shadesOpacity() const { return m_shadesOpacity;}
54
53
55
54 //range handling
55 void setMin(qreal min);
56 qreal min() const { return m_min;};
57 void setMax(qreal max);
58 qreal max() const { return m_max;};
59 void setRange(qreal min, qreal max);
60
61 //ticks handling
62 void setTicksCount(int count);
63 int ticksCount() const { return m_ticksCount;}
64 void addAxisTickLabel(qreal value,const QString& label);
65 void removeAxisTickLabel(qreal value);
66 QString axisTickLabel(qreal value) const ;
67 void clearAxisTickLabels();
68
69 signals:
70 void minChanged(qreal min);
71 void maxChanged(qreal max);
72 //private signal
73 void update(QChartAxis*);
74 void ticksChanged(QChartAxis*);
56
75
57 private:
76 private:
58
59 bool m_axisVisible;
77 bool m_axisVisible;
60 QPen m_axisPen;
78 QPen m_axisPen;
61 QBrush m_axisBrush;
79 QBrush m_axisBrush;
62
80
63 bool m_gridVisible;
81 bool m_gridVisible;
64 QPen m_gridPen;
82 QPen m_gridPen;
65
83
66 bool m_labelsVisible;
84 bool m_labelsVisible;
67 QPen m_labelsPen;
85 QPen m_labelsPen;
68 QBrush m_labelsBrush;
86 QBrush m_labelsBrush;
69 QFont m_labelsFont;
87 QFont m_labelsFont;
88 int m_labelsAngle;
70
89
71 bool m_shadesVisible;
90 bool m_shadesVisible;
72 QPen m_shadesPen;
91 QPen m_shadesPen;
73 QBrush m_shadesBrush;
92 QBrush m_shadesBrush;
74
75 qreal m_shadesOpacity;
93 qreal m_shadesOpacity;
76
94
95 qreal m_min;
96 qreal m_max;
77
97
78 LabelsOrientation m_labelsOrientation;
98 int m_ticksCount;
99 QMap<qreal, QString> m_ticks;
79 };
100 };
80
101
81 QTCOMMERCIALCHART_END_NAMESPACE
102 QTCOMMERCIALCHART_END_NAMESPACE
82 #endif /* QCHARTAXIS_H_ */
103 #endif /* QCHARTAXIS_H_ */
@@ -1,241 +1,211
1 #include "qchartview.h"
1 #include "qchartview.h"
2 #include "qchart.h"
2 #include "qchart.h"
3 #include "qchartaxis.h"
3 #include "qchartaxis.h"
4 #include <QGraphicsView>
4 #include <QGraphicsView>
5 #include <QGraphicsScene>
5 #include <QGraphicsScene>
6 #include <QRubberBand>
6 #include <QRubberBand>
7 #include <QResizeEvent>
7 #include <QResizeEvent>
8 #include <QDebug>
8 #include <QDebug>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 QChartView::QChartView(QWidget *parent) :
12 QChartView::QChartView(QWidget *parent) :
13 QGraphicsView(parent),
13 QGraphicsView(parent),
14 m_scene(new QGraphicsScene(this)),
14 m_scene(new QGraphicsScene(this)),
15 m_chart(new QChart()),
15 m_chart(new QChart()),
16 m_rubberBand(0),
16 m_rubberBand(0),
17 m_verticalRubberBand(false),
17 m_verticalRubberBand(false),
18 m_horizonalRubberBand(false)
18 m_horizonalRubberBand(false)
19 {
19 {
20 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
20 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
21 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
21 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
22 setScene(m_scene);
22 setScene(m_scene);
23 m_chart->setMargin(50);
23 m_chart->setMargin(50);
24 m_scene->addItem(m_chart);
24 m_scene->addItem(m_chart);
25 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
25 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
26 }
26 }
27
27
28 QChartView::~QChartView()
28 QChartView::~QChartView()
29 {
29 {
30 }
30 }
31
31
32 void QChartView::resizeEvent(QResizeEvent *event)
32 void QChartView::resizeEvent(QResizeEvent *event)
33 {
33 {
34 m_scene->setSceneRect(0,0,size().width(),size().height());
34 m_scene->setSceneRect(0,0,size().width(),size().height());
35 m_chart->resize(size());
35 m_chart->resize(size());
36 QWidget::resizeEvent(event);
36 QWidget::resizeEvent(event);
37 }
37 }
38
38
39 void QChartView::addSeries(QChartSeries* series)
39 void QChartView::addSeries(QChartSeries* series,QChartAxis *axisY)
40 {
40 {
41 m_chart->addSeries(series);
41 m_chart->addSeries(series,axisY);
42 }
42 }
43
43
44 QChartSeries* QChartView::createSeries(QChartSeries::QChartSeriesType type)
44 void QChartView::zoomIn()
45 {
45 {
46
46 m_chart->zoomIn();
47 return m_chart->createSeries(type);
48 }
47 }
49
48
50 void QChartView::zoomInToRect(const QRect& rectangle)
49 void QChartView::zoomIn(const QRect& rect)
51 {
50 {
52 m_chart->zoomInToRect(rectangle);
51 m_chart->zoomIn(rect);
53 }
54
55 void QChartView::zoomIn()
56 {
57 m_chart->zoomIn();
58 }
52 }
59
53
60 void QChartView::zoomOut()
54 void QChartView::zoomOut()
61 {
55 {
62 m_chart->zoomOut();
56 m_chart->zoomOut();
63 }
57 }
64
58
65 int QChartView::margin() const
59 int QChartView::margin() const
66 {
60 {
67 return m_chart->margin();
61 return m_chart->margin();
68 }
62 }
69
63
70 void QChartView::setChartTitle(const QString& title)
64 void QChartView::setChartTitle(const QString& title)
71 {
65 {
72 m_chart->setChartTitle(title);
66 m_chart->setChartTitle(title);
73 }
67 }
74
68
75 void QChartView::setChartTitleFont(const QFont& font)
69 void QChartView::setChartTitleFont(const QFont& font)
76 {
70 {
77 m_chart->setChartTitleFont(font);
71 m_chart->setChartTitleFont(font);
78 }
72 }
79
73
80 void QChartView::setChartBackgroundBrush(const QBrush& brush)
74 void QChartView::setChartBackgroundBrush(const QBrush& brush)
81 {
75 {
82 m_chart->setChartBackgroundBrush(brush);
76 m_chart->setChartBackgroundBrush(brush);
83 }
77 }
84 void QChartView::setChartBackgroundPen(const QPen& pen)
78 void QChartView::setChartBackgroundPen(const QPen& pen)
85 {
79 {
86 m_chart->setChartBackgroundPen(pen);
80 m_chart->setChartBackgroundPen(pen);
87 }
81 }
88
82
89 void QChartView::setRubberBandPolicy(const RubberBandPolicy policy)
83 void QChartView::setRubberBandPolicy(const RubberBandPolicy policy)
90 {
84 {
91 switch(policy) {
85 switch(policy) {
92 case VerticalRubberBand:
86 case VerticalRubberBand:
93 m_verticalRubberBand = true;
87 m_verticalRubberBand = true;
94 m_horizonalRubberBand = false;
88 m_horizonalRubberBand = false;
95 break;
89 break;
96 case HorizonalRubberBand:
90 case HorizonalRubberBand:
97 m_verticalRubberBand = false;
91 m_verticalRubberBand = false;
98 m_horizonalRubberBand = true;
92 m_horizonalRubberBand = true;
99 break;
93 break;
100 case RectangleRubberBand:
94 case RectangleRubberBand:
101 m_verticalRubberBand = true;
95 m_verticalRubberBand = true;
102 m_horizonalRubberBand = true;
96 m_horizonalRubberBand = true;
103 break;
97 break;
104 case NoRubberBand:
98 case NoRubberBand:
105 default:
99 default:
106 delete m_rubberBand;
100 delete m_rubberBand;
107 m_rubberBand=0;
101 m_rubberBand=0;
108 m_horizonalRubberBand = false;
102 m_horizonalRubberBand = false;
109 m_verticalRubberBand = false;
103 m_verticalRubberBand = false;
110 return;
104 return;
111 }
105 }
112 if(!m_rubberBand) {
106 if(!m_rubberBand) {
113 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
107 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
114 m_rubberBand->setEnabled(true);
108 m_rubberBand->setEnabled(true);
115 }
109 }
116 }
110 }
117
111
118 QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const
112 QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const
119 {
113 {
120 if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand;
114 if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand;
121 if(m_horizonalRubberBand) return HorizonalRubberBand;
115 if(m_horizonalRubberBand) return HorizonalRubberBand;
122 if(m_verticalRubberBand) return VerticalRubberBand;
116 if(m_verticalRubberBand) return VerticalRubberBand;
123 return NoRubberBand;
117 return NoRubberBand;
124 }
118 }
125
119
126 void QChartView::mousePressEvent(QMouseEvent *event)
120 void QChartView::mousePressEvent(QMouseEvent *event)
127 {
121 {
128 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
122 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
129
123
130 int margin = m_chart->margin();
124 int margin = m_chart->margin();
131 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
125 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
132
126
133 if (rect.contains(event->pos())) {
127 if (rect.contains(event->pos())) {
134 m_rubberBandOrigin = event->pos();
128 m_rubberBandOrigin = event->pos();
135 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize()));
129 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize()));
136 m_rubberBand->show();
130 m_rubberBand->show();
137 event->accept();
131 event->accept();
138 }
132 }
139 }
133 }
140 }
134 }
141
135
142 void QChartView::mouseMoveEvent(QMouseEvent *event)
136 void QChartView::mouseMoveEvent(QMouseEvent *event)
143 {
137 {
144 if(m_rubberBand && m_rubberBand->isVisible()) {
138 if(m_rubberBand && m_rubberBand->isVisible()) {
145 int margin = m_chart->margin();
139 int margin = m_chart->margin();
146 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
140 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
147 int width = event->pos().x() - m_rubberBandOrigin.x();
141 int width = event->pos().x() - m_rubberBandOrigin.x();
148 int height = event->pos().y() - m_rubberBandOrigin.y();
142 int height = event->pos().y() - m_rubberBandOrigin.y();
149 if(!m_verticalRubberBand) {
143 if(!m_verticalRubberBand) {
150 m_rubberBandOrigin.setY(rect.top());
144 m_rubberBandOrigin.setY(rect.top());
151 height = rect.height();
145 height = rect.height();
152 }
146 }
153 if(!m_horizonalRubberBand) {
147 if(!m_horizonalRubberBand) {
154 m_rubberBandOrigin.setX(rect.left());
148 m_rubberBandOrigin.setX(rect.left());
155 width= rect.width();
149 width= rect.width();
156 }
150 }
157 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized());
151 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized());
158 }
152 }
159 else {
153 else {
160 QGraphicsView::mouseMoveEvent(event);
154 QGraphicsView::mouseMoveEvent(event);
161 }
155 }
162 }
156 }
163
157
164 void QChartView::mouseReleaseEvent(QMouseEvent *event)
158 void QChartView::mouseReleaseEvent(QMouseEvent *event)
165 {
159 {
166 if(m_rubberBand) {
160 if(m_rubberBand) {
167 if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) {
161 if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) {
168 m_rubberBand->hide();
162 m_rubberBand->hide();
169 QRect rect = m_rubberBand->geometry();
163 QRect rect = m_rubberBand->geometry();
170 m_chart->zoomInToRect(rect);
164 m_chart->zoomIn(rect);
171 event->accept();
165 event->accept();
172 }
166 }
173
167
174 if(event->button()==Qt::RightButton)
168 if(event->button()==Qt::RightButton)
175 m_chart->zoomReset();
169 m_chart->zoomReset();
176 }
170 }
177 else {
171 else {
178 QGraphicsView::mouseReleaseEvent(event);
172 QGraphicsView::mouseReleaseEvent(event);
179 }
173 }
180 }
174 }
181
175
182 void QChartView::keyPressEvent(QKeyEvent *event)
176 void QChartView::keyPressEvent(QKeyEvent *event)
183 {
177 {
184 switch (event->key()) {
178 switch (event->key()) {
185 case Qt::Key_Plus:
179 case Qt::Key_Plus:
186 zoomIn();
180 zoomIn();
187 break;
181 break;
188 case Qt::Key_Minus:
182 case Qt::Key_Minus:
189 zoomOut();
183 zoomOut();
190 break;
184 break;
191 default:
185 default:
192 QGraphicsView::keyPressEvent(event);
186 QGraphicsView::keyPressEvent(event);
193 break;
187 break;
194 }
188 }
195 }
189 }
196
190
197 void QChartView::setChartTheme(QChart::ChartTheme theme)
191 void QChartView::setChartTheme(QChart::ChartTheme theme)
198 {
192 {
199 m_chart->setChartTheme(theme);
193 m_chart->setChartTheme(theme);
200 }
194 }
201
195
202 QChart::ChartTheme QChartView::chartTheme() const
196 QChart::ChartTheme QChartView::chartTheme() const
203 {
197 {
204 return m_chart->chartTheme();
198 return m_chart->chartTheme();
205 }
199 }
206
200
207 void QChartView::setDefaultAxisX(const QChartAxis& axis)
201 QChartAxis* QChartView::axisX() const
208 {
209 m_chart->setDefaultAxisX(axis);
210 }
211
212 void QChartView::setDefaultAxisY(const QChartAxis& axis)
213 {
202 {
214 m_chart->setDefaultAxisY(axis);
203 return m_chart->axisX();
215 }
204 }
216
205
217 QChartAxis QChartView::defaultAxisX() const
206 QChartAxis* QChartView::axisY() const
218 {
207 {
219 return m_chart->defaultAxisX();
208 return m_chart->axisY();
220 }
209 }
221
210
222 QChartAxis QChartView::defaultAxisY() const
223 {
224 return m_chart->defaultAxisY();
225 }
226
227 int QChartView::addAxisY(const QChartAxis& axis)
228 {
229 return m_chart->addAxisY(axis);
230 }
231
232 QChartAxis QChartView::axisY(int id) const
233 {
234 return m_chart->axisY(id);
235 }
236
237 void QChartView::removeAxisY(int id)
238 {
239 m_chart->removeAxisY(id);
240 }
241 QTCOMMERCIALCHART_END_NAMESPACE
211 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,78 +1,71
1 #ifndef QCHARTWIDGET_H
1 #ifndef QCHARTWIDGET_H
2 #define QCHARTWIDGET_H
2 #define QCHARTWIDGET_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qchartseries.h"
5 #include "qchartseries.h"
6 #include "qchart.h"
6 #include "qchart.h"
7 #include <QGraphicsView>
7 #include <QGraphicsView>
8
8
9 class QGraphicsScene;
9 class QGraphicsScene;
10 class QRubberBand;
10 class QRubberBand;
11
11
12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13
13
14 class QChart;
14 class QChart;
15
15
16 class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView
16 class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView
17 {
17 {
18 public:
18 public:
19 enum RubberBandPolicy { NoRubberBand, VerticalRubberBand, HorizonalRubberBand, RectangleRubberBand };
19 enum RubberBandPolicy { NoRubberBand, VerticalRubberBand, HorizonalRubberBand, RectangleRubberBand };
20
20
21 explicit QChartView(QWidget *parent = 0);
21 explicit QChartView(QWidget *parent = 0);
22 ~QChartView();
22 ~QChartView();
23
23
24 //implement from QWidget
24 //implement from QWidget
25 void resizeEvent(QResizeEvent *event);
25 void resizeEvent(QResizeEvent *event);
26
26
27 void addSeries(QChartSeries* series); // takes ownership
27 void addSeries(QChartSeries* series,QChartAxis* axisY=0) ;// takes ownership
28
28 void removeSeries(QChartSeries* series);
29 // Convenience function
30 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
31
29
32 int margin() const;
30 int margin() const;
33
31
34 void setChartTitle(const QString& title);
32 void setChartTitle(const QString& title);
35 void setChartTitleFont(const QFont& font);
33 void setChartTitleFont(const QFont& font);
36 void setChartBackgroundBrush(const QBrush& brush);
34 void setChartBackgroundBrush(const QBrush& brush);
37 void setChartBackgroundPen(const QPen& pen);
35 void setChartBackgroundPen(const QPen& pen);
38
36
39 void zoomInToRect(const QRect& rectangle);
40 void zoomIn();
37 void zoomIn();
38 void zoomIn(const QRect& rect);
41 void zoomOut();
39 void zoomOut();
42
40
43 void setRubberBandPolicy(const RubberBandPolicy );
41 void setRubberBandPolicy(const RubberBandPolicy );
44 RubberBandPolicy rubberBandPolicy() const;
42 RubberBandPolicy rubberBandPolicy() const;
45
43
46 void setChartTheme(QChart::ChartTheme theme);
44 void setChartTheme(QChart::ChartTheme theme);
47 QChart::ChartTheme chartTheme() const;
45 QChart::ChartTheme chartTheme() const;
48
46
49 void setDefaultAxisX(const QChartAxis& axis);
47 QChartAxis* axisX() const;
50 void setDefaultAxisY(const QChartAxis& axis);
48 QChartAxis* axisY() const;
51 QChartAxis defaultAxisX() const;
52 QChartAxis defaultAxisY() const;
53 QChartAxis axisY(int id) const;
54 int addAxisY(const QChartAxis& axis);
55 void removeAxisY(int id);
56
49
57 protected:
50 protected:
58 void mousePressEvent(QMouseEvent *event);
51 void mousePressEvent(QMouseEvent *event);
59 void mouseMoveEvent(QMouseEvent *event);
52 void mouseMoveEvent(QMouseEvent *event);
60 void mouseReleaseEvent(QMouseEvent *event);
53 void mouseReleaseEvent(QMouseEvent *event);
61 void keyPressEvent(QKeyEvent *event);
54 void keyPressEvent(QKeyEvent *event);
62
55
63
56
64 private:
57 private:
65 QGraphicsScene *m_scene;
58 QGraphicsScene *m_scene;
66 QChart* m_chart;
59 QChart* m_chart;
67 QPoint m_rubberBandOrigin;
60 QPoint m_rubberBandOrigin;
68 QRubberBand* m_rubberBand;
61 QRubberBand* m_rubberBand;
69 bool m_verticalRubberBand;
62 bool m_verticalRubberBand;
70 bool m_horizonalRubberBand;
63 bool m_horizonalRubberBand;
71 Q_DISABLE_COPY(QChartView)
64 Q_DISABLE_COPY(QChartView)
72
65
73
66
74 };
67 };
75
68
76 QTCOMMERCIALCHART_END_NAMESPACE
69 QTCOMMERCIALCHART_END_NAMESPACE
77
70
78 #endif // QCHARTWIDGET_H
71 #endif // QCHARTWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now