##// END OF EJS Templates
Refactor line spline to common xyline...
Michal Klocek -
r465:7f683774d508
parent child
Show More
@@ -16,6 +16,6 SUBDIRS += linechart \
16 16 presenterchart \
17 17 chartview \
18 18 scatterinteractions \
19 #splinechart \
19 splinechart \
20 20 areachart \
21 21 stackedbarchartdrilldown
@@ -20,11 +20,10 LineChartAnimationItem::~LineChartAnimationItem()
20 20 {
21 21 }
22 22
23 void LineChartAnimationItem::updateAllPoints()
23 void LineChartAnimationItem::updatePoints(QVector<QPointF>& newPoints)
24 24 {
25 25 QVector<QPointF> oldPoints = points();
26 LineChartItem::updateAllPoints();
27 QVector<QPointF> newPoints = points();
26 LineChartItem::updatePoints(newPoints);
28 27
29 28 if(newPoints.count()==0) return;
30 29 oldPoints.resize(newPoints.size());
@@ -19,7 +19,7 public:
19 19 virtual ~LineChartAnimationItem();
20 20
21 21 protected:
22 virtual void updateAllPoints();
22 virtual void updatePoints(QVector<QPointF>& newPoints);
23 23 virtual void updatePoint(int index,QPointF& newPoint);
24 24
25 25 private slots:
@@ -8,20 +8,12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9 //TODO: optimize : remove points which are not visible
10 10
11 LineChartItem::LineChartItem(QLineSeries* series,QGraphicsItem *parent):ChartItem(parent),
12 m_minX(0),
13 m_maxX(0),
14 m_minY(0),
15 m_maxY(0),
11 LineChartItem::LineChartItem(QLineSeries* series,QGraphicsItem *parent):XYChartItem(series,parent),
16 12 m_series(series),
17 13 m_items(this)
18 14 {
19 15 //m_items.setZValue(ChartPresenter::LineChartZValue);
20 16 setZValue(ChartPresenter::LineChartZValue);
21
22 QObject::connect(series,SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
23 QObject::connect(series,SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
24 QObject::connect(series,SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
25 17 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
26 18
27 19 handleUpdated();
@@ -45,7 +37,7 void LineChartItem::createPoints(int count)
45 37 }
46 38 }
47 39
48 void LineChartItem::clearPoints(int count)
40 void LineChartItem::deletePoints(int count)
49 41 {
50 42 QList<QGraphicsItem *> items = m_items.childItems();
51 43
@@ -54,70 +46,19 void LineChartItem::clearPoints(int count)
54 46 }
55 47 }
56 48
57 QPointF LineChartItem::calculateGeometryPoint(int index) const
58 {
59 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
60 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
61 qreal x = (m_series->x(index) - m_minX)* deltaX;
62 qreal y = (m_series->y(index) - m_minY)*-deltaY + m_size.height();
63 return QPointF(x,y);
64 }
65
66 QVector<QPointF> LineChartItem::calculateGeometryPoints() const
67 {
68 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
69 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
70
71 QVector<QPointF> points;
72 points.reserve(m_series->count());
73 for (int i = 0; i < m_series->count(); ++i) {
74 qreal x = (m_series->x(i) - m_minX)* deltaX;
75 qreal y = (m_series->y(i) - m_minY)*-deltaY + m_size.height();
76 points << QPointF(x,y);
77 }
78 return points;
79 }
80
81 void LineChartItem::updateAllPoints()
49 void LineChartItem::setGeometry(QVector<QPointF>& points)
82 50 {
83 QVector<QPointF> points = calculateGeometryPoints();
84
85 int diff = m_points.size() - points.size();
86
87 if(diff>0) {
88 clearPoints(diff);
89 }
90 else if(diff<0) {
91 createPoints(-diff);
92 }
93
94 setGeometry(points);
95 }
51 if(points.size()==0) return;
96 52
97 void LineChartItem::updatePoints(QVector<QPointF>& points)
98 {
99 int diff = m_points.size() - points.size();
53 int diff = XYChartItem::points().size() - points.size();
100 54
101 55 if(diff>0) {
102 clearPoints(diff);
56 deletePoints(diff);
103 57 }
104 58 else if(diff<0) {
105 59 createPoints(-diff);
106 60 }
107 61
108 setGeometry(points);
109 }
110
111 void LineChartItem::updatePoint(int index,QPointF& newPoint)
112 {
113 m_points.replace(index,newPoint);
114 setGeometry(m_points);
115 }
116
117 void LineChartItem::setGeometry(QVector<QPointF>& points)
118 {
119 if(points.size()==0) return;
120
121 62 QList<QGraphicsItem*> items = m_items.childItems();
122 63
123 64 QPainterPath path;
@@ -125,9 +66,10 void LineChartItem::setGeometry(QVector<QPointF>& points)
125 66 path.moveTo(point);
126 67 QGraphicsItem* item = items.at(0);
127 68 item->setPos(point.x()-1,point.y()-1);
128 if(!m_clipRect.contains(point)) {
69 if(!clipRect().contains(point)) {
129 70 item->setVisible(false);
130 }else{
71 }
72 else {
131 73 item->setVisible(true);
132 74 }
133 75
@@ -135,9 +77,10 void LineChartItem::setGeometry(QVector<QPointF>& points)
135 77 QGraphicsItem* item = items.at(i);
136 78 const QPointF& point = points.at(i);
137 79 item->setPos(point.x()-1,point.y()-1);
138 if(!m_clipRect.contains(point)) {
80 if(!clipRect().contains(point)) {
139 81 item->setVisible(false);
140 }else{
82 }
83 else {
141 84 item->setVisible(true);
142 85 }
143 86 path.lineTo(point);
@@ -146,7 +89,8 void LineChartItem::setGeometry(QVector<QPointF>& points)
146 89 prepareGeometryChange();
147 90 m_path = path;
148 91 m_rect = path.boundingRect();
149 m_points = points;
92
93 XYChartItem::setGeometry(points);
150 94 }
151 95
152 96 void LineChartItem::setPen(const QPen& pen)
@@ -154,66 +98,6 void LineChartItem::setPen(const QPen& pen)
154 98 m_pen = pen;
155 99 }
156 100
157 //handlers
158
159 void LineChartItem::handlePointAdded(int index)
160 {
161 Q_ASSERT(index<m_series->count());
162 Q_ASSERT(index>=0);
163
164 QPointF point = calculateGeometryPoint(index);
165 createPoints(1);
166 QVector<QPointF> points = m_points;
167 points.insert(index,point);
168
169 updatePoints(points);
170 update();
171 }
172 void LineChartItem::handlePointRemoved(int index)
173 {
174 Q_ASSERT(index<m_series->count());
175 Q_ASSERT(index>=0);
176
177 QPointF point = calculateGeometryPoint(index);
178 clearPoints(1);
179 QVector<QPointF> points = m_points;
180 points.remove(index);
181 updatePoints(points);
182 update();
183 }
184
185 void LineChartItem::handlePointReplaced(int index)
186 {
187 Q_ASSERT(index<m_series->count());
188 Q_ASSERT(index>=0);
189 QPointF point = calculateGeometryPoint(index);
190 updatePoint(index,point);
191 update();
192 }
193
194 void LineChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
195 {
196 m_minX=minX;
197 m_maxX=maxX;
198 m_minY=minY;
199 m_maxY=maxY;
200
201 if(isEmpty()) return;
202 updateAllPoints();
203 update();
204 }
205
206 void LineChartItem::handleGeometryChanged(const QRectF& rect)
207 {
208 Q_ASSERT(rect.isValid());
209 m_size=rect.size();
210 m_clipRect=rect.translated(-rect.topLeft());
211 setPos(rect.topLeft());
212
213 if(isEmpty()) return;
214 updateAllPoints();
215 update();
216 }
217 101
218 102 void LineChartItem::handleUpdated()
219 103 {
@@ -230,16 +114,11 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt
230 114 Q_UNUSED(option);
231 115 painter->save();
232 116 painter->setPen(m_pen);
233 painter->setClipRect(m_clipRect);
117 painter->setClipRect(clipRect());
234 118 painter->drawPath(m_path);
235 119 painter->restore();
236 120 }
237 121
238 bool LineChartItem::isEmpty()
239 {
240 return !m_clipRect.isValid() || m_maxX - m_minX == 0 || m_maxY - m_minY ==0 ;
241 }
242
243 122 #include "moc_linechartitem_p.cpp"
244 123
245 124 QTCOMMERCIALCHART_END_NAMESPACE
@@ -2,7 +2,7
2 2 #define LINECHARTITEM_H
3 3
4 4 #include "qchartglobal.h"
5 #include "chartitem_p.h"
5 #include "xychartitem_p.h"
6 6 #include <QPen>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -10,7 +10,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10 class ChartPresenter;
11 11 class QLineSeries;
12 12
13 class LineChartItem : public QObject , public ChartItem
13 class LineChartItem : public XYChartItem
14 14 {
15 15 Q_OBJECT
16 16 public:
@@ -25,39 +25,20 public:
25 25 void setPen(const QPen& pen);
26 26 void setPointsVisible(bool visible);
27 27
28 void createPoints(int count);
29 void deletePoints(int count);
30
28 31 public slots:
29 void handlePointAdded(int index);
30 void handlePointRemoved(int index);
31 void handlePointReplaced(int index);
32 32 void handleUpdated();
33 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
34 void handleGeometryChanged(const QRectF& size);
35 33
36 34 protected:
37 virtual void updateAllPoints();
38 virtual void updatePoints(QVector<QPointF>& points);
39 virtual void updatePoint(int index,QPointF& newPoint);
40 35 virtual void setGeometry(QVector<QPointF>& points);
41 36
42 QVector<QPointF> points() {return m_points;}
43 void createPoints(int count);
44 void clearPoints(int count);
45 QPointF calculateGeometryPoint(int index) const;
46 QVector<QPointF> calculateGeometryPoints() const;
47 inline bool isEmpty();
48
49 protected:
50 qreal m_minX;
51 qreal m_maxX;
52 qreal m_minY;
53 qreal m_maxY;
54 QPainterPath m_path;
55 QRectF m_rect;
37 private:
56 38 QLineSeries* m_series;
57 QSizeF m_size;
58 QRectF m_clipRect;
59 39 QGraphicsItemGroup m_items;
60 QVector<QPointF> m_points;
40 QPainterPath m_path;
41 QRectF m_rect;
61 42 QPen m_pen;
62 43
63 44 friend class LineChartAnimatator;
@@ -66,7 +66,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
66 66 Constructs empty series object which is a child of \a parent.
67 67 When series object is added to QChartView or QChart instance ownerships is transfered.
68 68 */
69 QLineSeries::QLineSeries(QObject* parent):QSeries(parent),
69 QLineSeries::QLineSeries(QObject* parent):QXYSeries(parent),
70 70 m_pointsVisible(false)
71 71 {
72 72 }
@@ -79,101 +79,6 QLineSeries::~QLineSeries()
79 79 }
80 80
81 81 /*!
82 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
83 */
84 void QLineSeries::add(qreal x,qreal y)
85 {
86 Q_ASSERT(m_x.size() == m_y.size());
87 m_x<<x;
88 m_y<<y;
89 emit pointAdded(m_x.size()-1);
90 }
91
92 /*!
93 This is an overloaded function.
94 Adds data \a point to the series. Points are connected with lines on the chart.
95 */
96 void QLineSeries::add(const QPointF& point)
97 {
98 add(point.x(),point.y());
99 }
100
101 /*!
102 Modifies \a y value for given \a x a value.
103 */
104 void QLineSeries::replace(qreal x,qreal y)
105 {
106 int index = m_x.indexOf(x);
107 m_x[index]=x;
108 m_y[index]=y;
109 emit pointReplaced(index);
110 }
111
112 /*!
113 This is an overloaded function.
114 Replaces current y value of for given \a point x value with \a point y value.
115 */
116 void QLineSeries::replace(const QPointF& point)
117 {
118 replace(point.x(),point.y());
119 }
120
121 /*!
122 Removes current \a x and y value.
123 */
124 void QLineSeries::remove(qreal x)
125 {
126 int index = m_x.indexOf(x);
127 emit pointRemoved(index);
128 m_x.remove(index);
129 m_y.remove(index);
130 }
131
132 /*!
133 Removes current \a point x value. Note \a point y value is ignored.
134 */
135 void QLineSeries::remove(const QPointF& point)
136 {
137 remove(point.x());
138 }
139
140 /*!
141 Clears all the data.
142 */
143 void QLineSeries::clear()
144 {
145 m_x.clear();
146 m_y.clear();
147 }
148
149 /*!
150 \internal \a pos
151 */
152 qreal QLineSeries::x(int pos) const
153 {
154 return m_x.at(pos);
155 }
156
157 /*!
158 \internal \a pos
159 */
160 qreal QLineSeries::y(int pos) const
161 {
162 return m_y.at(pos);
163 }
164
165 /*!
166 Returns number of data points within series.
167 */
168 int QLineSeries::count() const
169 {
170 Q_ASSERT(m_x.size() == m_y.size());
171
172 return m_x.size();
173
174 }
175
176 /*!
177 82 Sets \a pen used for drawing given series..
178 83 */
179 84 void QLineSeries::setPen(const QPen& pen)
@@ -207,18 +112,6 QDebug operator<< (QDebug debug, const QLineSeries series)
207 112 return debug.space();
208 113 }
209 114
210 /*!
211 Stream operator for adding a data \a point to the series.
212 \sa add()
213 */
214
215 QLineSeries& QLineSeries::operator<< (const QPointF &point)
216 {
217 add(point);
218 return *this;
219 }
220
221
222 115 #include "moc_qlineseries.cpp"
223 116
224 117 QTCOMMERCIALCHART_END_NAMESPACE
@@ -2,14 +2,14
2 2 #define QLINESERIES_H_
3 3
4 4 #include "qchartglobal.h"
5 #include "qseries.h"
5 #include "qxyseries.h"
6 6 #include <QDebug>
7 7 #include <QPen>
8 8 #include <QBrush>
9 9
10 10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 11
12 class QTCOMMERCIALCHART_EXPORT QLineSeries : public QSeries
12 class QTCOMMERCIALCHART_EXPORT QLineSeries : public QXYSeries
13 13 {
14 14 Q_OBJECT
15 15 public:
@@ -18,38 +18,13 public:
18 18
19 19 public: // from QChartSeries
20 20 virtual QSeriesType type() const {return QSeries::SeriesTypeLine;}
21 void add(qreal x, qreal y);
22 void add(const QPointF& point);
23 void replace(qreal x,qreal y);
24 void replace(const QPointF& point);
25 void remove(qreal x);
26 void remove(const QPointF& point);
27 void clear();
28
29 21 void setPen(const QPen& pen);
30 22 QPen pen() const {return m_pen;}
31 23
32 24 void setPointsVisible(bool visible);
33 25 bool pointsVisible() const {return m_pointsVisible;}
34 26
35 int count() const;
36 qreal x(int pos) const;
37 qreal y(int pos) const;
38
39 QLineSeries& operator << (const QPointF &point);
40 27 friend QDebug operator<< (QDebug d, const QLineSeries series);
41
42 signals:
43 void updated();
44 void pointReplaced(int index);
45 void pointRemoved(int index);
46 void pointAdded(int index);
47
48
49 protected:
50 QVector<qreal> m_x;
51 QVector<qreal> m_y;
52
53 28 private:
54 29 QPen m_pen;
55 30 bool m_pointsVisible;
@@ -4,21 +4,24
4 4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 5
6 6 SplineChartItem::SplineChartItem(QSplineSeries* series, QGraphicsObject *parent) :
7 LineChartItem(series, parent)//,m_boundingRect()
7 XYChartItem(series, parent),
8 m_series(series)
8 9 {
9 //
10 10 }
11 11
12 QRectF SplineChartItem::boundingRect() const
13 {
14 return m_rect;
15 }
12 16
17 QPainterPath SplineChartItem::shape() const
18 {
19 return m_path;
20 }
13 21
14 22 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const
15 23 {
16 QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series);
17 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
18 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
19 qreal x = (splineSeries->controlPoint(index).x() - m_minX)* deltaX;
20 qreal y = (splineSeries->controlPoint(index).y() - m_minY)*-deltaY + m_size.height();
21 return QPointF(x,y);
24 return XYChartItem::calculateGeometryPoint(m_series->controlPoint(index));
22 25 }
23 26
24 27 void SplineChartItem::setGeometry(QVector<QPointF>& points)
@@ -29,7 +32,6 void SplineChartItem::setGeometry(QVector<QPointF>& points)
29 32 const QPointF& point = points.at(0);
30 33 splinePath.moveTo(point);
31 34
32 // QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series);
33 35 for (int i = 0; i < points.size() - 1; i++)
34 36 {
35 37 const QPointF& point = points.at(i + 1);
@@ -39,22 +41,29 void SplineChartItem::setGeometry(QVector<QPointF>& points)
39 41 prepareGeometryChange();
40 42 m_path = splinePath;
41 43 m_rect = splinePath.boundingRect();
44 XYChartItem::setGeometry(points);
45 }
46
47 void SplineChartItem::setPen(const QPen& pen)
48 {
49 m_pen = pen;
42 50 }
43 51
52
44 53 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
45 54 {
46 55 Q_UNUSED(widget);
47 56 Q_UNUSED(option);
48 57 painter->save();
49 painter->setPen(m_pen);
50 painter->setClipRect(m_clipRect);
58 painter->setClipRect(clipRect());
51 59 painter->drawPath(m_path);
52 60
53 QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series);
54 for (int i = 0; i < m_points.size() - 1; i++)
61 const QVector<QPointF> points = XYChartItem::points();
62
63 for (int i = 0; i < points.size() - 1; i++)
55 64 {
56 65 painter->setPen(Qt::red);
57 painter->drawEllipse(m_points[i], 2, 2);
66 painter->drawEllipse(points[i], 2, 2);
58 67
59 68 painter->setPen(Qt::blue);
60 69 // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i));
@@ -62,10 +71,10 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *o
62 71 // painter->drawEllipse(calculateGeometryControlPoint(2 * i), 4, 4);
63 72 // painter->drawEllipse(calculateGeometryControlPoint(2 * i + 1), 4, 4);
64 73 }
65 if (m_points.count() > 0)
74 if (points.count() > 0)
66 75 {
67 76 painter->setPen(Qt::red);
68 painter->drawEllipse(m_points[m_points.count() - 1], 2, 2);
77 painter->drawEllipse(points[points.count() - 1], 2, 2);
69 78 }
70 79 painter->restore();
71 80 }
@@ -4,23 +4,35
4 4 #include "chartitem_p.h"
5 5 #include <QObject>
6 6 #include "qsplineseries.h"
7 #include "linechartitem_p.h"
7 #include "xychartitem_p.h"
8 8
9 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 class SplineChartItem : public LineChartItem
11 class SplineChartItem : public XYChartItem
12 12 {
13 13 Q_OBJECT
14 14 public:
15 15 SplineChartItem(QSplineSeries* series, QGraphicsObject *parent = 0);
16 16
17 void updateGeometry();
17 //from QGraphicsItem
18 QRectF boundingRect() const;
19 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
20 QPainterPath shape() const;
21
22 void setPen(const QPen& pen);
23 void setPointsVisible(bool visible);
18 24
25 protected:
19 26 void setGeometry(QVector<QPointF>& points);
20 27
28 private:
21 29 QPointF calculateGeometryControlPoint(int index) const;
22 30
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
31 private:
32 QSplineSeries* m_series;
33 QPainterPath m_path;
34 QRectF m_rect;
35 QPen m_pen;
24 36 };
25 37
26 38 QTCOMMERCIALCHART_END_NAMESPACE
@@ -30,6 +30,7 PUBLIC_HEADERS += qchart.h \
30 30 qchartglobal.h \
31 31 qseries.h \
32 32 qchartview.h
33 include(xychart/xychart.pri)
33 34 include(linechart/linechart.pri)
34 35 include(areachart/areachart.pri)
35 36 include(barchart/barchart.pri)
General Comments 0
You need to be logged in to leave comments. Login now