##// END OF EJS Templates
Adds cliping and proper zoom handling for line chart
Michal Klocek -
r150:19743dc8af25
parent child
Show More
@@ -6,6 +6,8
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 //TODO: optimazie : remove points which are not visible
10
9 LineChartItem::LineChartItem(ChartPresenter* presenter, QLineChartSeries* series,QGraphicsItem *parent):ChartItem(parent),
11 LineChartItem::LineChartItem(ChartPresenter* presenter, QLineChartSeries* series,QGraphicsItem *parent):ChartItem(parent),
10 m_presenter(presenter),
12 m_presenter(presenter),
11 m_series(series),
13 m_series(series),
@@ -32,7 +34,8 void LineChartItem::addPoints(const QVector<QPointF>& points)
32 for(int i=0; i<m_data.size();i++){
34 for(int i=0; i<m_data.size();i++){
33 const QPointF& point =m_data[i];
35 const QPointF& point =m_data[i];
34 QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this);
36 QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this);
35 item->setPos(point.x()-1,point.y()-1);
37 item->setPos(point.x()-1,point.y()-1);;
38 if(!m_clipRect.contains(point)) item->setVisible(false);
36 m_points << item;
39 m_points << item;
37 }
40 }
38 }
41 }
@@ -41,7 +44,9 void LineChartItem::addPoint(const QPointF& point)
41 {
44 {
42 m_data << point;
45 m_data << point;
43 QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this);
46 QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this);
47 m_clipRect.contains(point);
44 item->setPos(point.x()-1,point.y()-1);
48 item->setPos(point.x()-1,point.y()-1);
49 if(!m_clipRect.contains(point)) item->setVisible(false);
45 m_points << item;
50 m_points << item;
46 }
51 }
47
52
@@ -99,6 +104,7 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt
99 Q_UNUSED(widget);
104 Q_UNUSED(widget);
100 Q_UNUSED(option);
105 Q_UNUSED(option);
101 painter->setPen(m_pen);
106 painter->setPen(m_pen);
107 painter->setClipRect(m_clipRect);
102 painter->drawPath(m_path);
108 painter->drawPath(m_path);
103 }
109 }
104
110
@@ -120,9 +126,7 void LineChartItem::calculatePoints(QVector<QPointF>& points, QHash<int,int>& ha
120
126
121 for (int i = 0; i < series->count(); ++i) {
127 for (int i = 0; i < series->count(); ++i) {
122 qreal x = (series->x(i) - domain.m_minX)* deltaX;
128 qreal x = (series->x(i) - domain.m_minX)* deltaX;
123 if(x<0 || x > size.width()) continue;
124 qreal y = (series->y(i) - domain.m_minY)*-deltaY + size.height();
129 qreal y = (series->y(i) - domain.m_minY)*-deltaY + size.height();
125 if(y<0 || y > size.height()) continue;
126 hash[i] = points.size();
130 hash[i] = points.size();
127 points << QPointF(x,y);
131 points << QPointF(x,y);
128 }
132 }
@@ -131,6 +135,7 void LineChartItem::calculatePoints(QVector<QPointF>& points, QHash<int,int>& ha
131 void LineChartItem::updateDomain()
135 void LineChartItem::updateDomain()
132 {
136 {
133 clear();
137 clear();
138 prepareGeometryChange();
134 calculatePoints(m_data,m_hash,m_series,m_size, m_domain);
139 calculatePoints(m_data,m_hash,m_series,m_size, m_domain);
135 addPoints(m_data);
140 addPoints(m_data);
136 }
141 }
@@ -188,8 +193,8 void LineChartItem::handleDomainChanged(const Domain& domain)
188 void LineChartItem::handleGeometryChanged(const QRectF& rect)
193 void LineChartItem::handleGeometryChanged(const QRectF& rect)
189 {
194 {
190 Q_ASSERT(rect.isValid());
195 Q_ASSERT(rect.isValid());
191
192 m_size=rect.size();
196 m_size=rect.size();
197 m_clipRect=rect.translated(-rect.topLeft());
193 updateDomain();
198 updateDomain();
194 updateGeometry();
199 updateGeometry();
195 setPos(rect.topLeft());
200 setPos(rect.topLeft());
@@ -55,6 +55,7 private:
55 QPainterPath m_path;
55 QPainterPath m_path;
56 QSizeF m_size;
56 QSizeF m_size;
57 QRectF m_rect;
57 QRectF m_rect;
58 QRectF m_clipRect;
58 Domain m_domain;
59 Domain m_domain;
59 QList<QGraphicsItem*> m_points;
60 QList<QGraphicsItem*> m_points;
60 QVector<QPointF> m_data;
61 QVector<QPointF> m_data;
@@ -122,7 +122,7 void QChart::setTheme(QChart::ChartThemeId theme)
122
122
123 QChart::ChartThemeId QChart::theme()
123 QChart::ChartThemeId QChart::theme()
124 {
124 {
125 return (QChart::ChartThemeId) m_presenter->theme();
125 return m_presenter->theme();
126 }
126 }
127
127
128 void QChart::zoomInToRect(const QRectF& rectangle)
128 void QChart::zoomInToRect(const QRectF& rectangle)
General Comments 0
You need to be logged in to leave comments. Login now