##// END OF EJS Templates
Refactor scatter chart to fit the other classes...
Michal Klocek -
r470:5c708e3e7c7c
parent child
Show More
@@ -0,0 +1,178
1 #include "scatterchartitem_p.h"
2 #include "qscatterseries.h"
3 #include "chartpresenter_p.h"
4 #include <QPainter>
5 #include <QGraphicsScene>
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
9 ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsObject *parent) :
10 XYChartItem(series,parent),
11 m_series(series),
12 m_items(this),
13 m_shape(QScatterSeries::MarkerShapeRectangle),
14 m_size(10)
15
16 {
17 Q_ASSERT(parent);
18 Q_ASSERT(series);
19
20 connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated()));
21
22 setZValue(ChartPresenter::ScatterSeriesZValue);
23 setFlags(QGraphicsItem::ItemHasNoContents);
24 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
25
26 handleUpdated();
27
28 // TODO: how to draw a drop shadow?
29 // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect();
30 // dropShadow->setOffset(2.0);
31 // dropShadow->setBlurRadius(2.0);
32 // setGraphicsEffect(dropShadow);
33 }
34
35
36 QRectF ScatterChartItem::boundingRect() const
37 {
38 return m_rect;
39 }
40
41 void ScatterChartItem::createPoints(int count)
42 {
43 for (int i = 0; i < count; ++i) {
44
45 QGraphicsItem *item;
46
47 switch (m_shape) {
48 case QScatterSeries::MarkerShapeDefault:
49 // Fallthrough, defaults to circle
50 case QScatterSeries::MarkerShapeCircle:
51 item = new QGraphicsEllipseItem(0,0,m_size,m_size);
52 break;
53 case QScatterSeries::MarkerShapeRectangle:
54 item = new QGraphicsRectItem(0,0,m_size,m_size);
55 break;
56 case QScatterSeries::MarkerShapeRoundedRectangle:
57 //m_path.addRoundedRect(x, y, size, size, size / 4.0, size / 4.0);
58 break;
59 case QScatterSeries::MarkerShapeTiltedRectangle:
60 // TODO: tilt the rectangle
61 //m_path.addRect(x, y, size, size);
62 //break;
63 case QScatterSeries::MarkerShapeTriangle:
64 //QPolygonF polygon;
65 //polygon << QPointF(0.0, -size) << QPointF(size / 2.0, 0.0) << QPointF(-size / 2, 0.0);
66 // TODO: the position is not exactly right...
67 //m_path.addPolygon(polygon.translated(x + size / 2.0, y + size));
68 break;
69 }
70 m_items.addToGroup(item);
71 }
72 }
73
74 void ScatterChartItem::deletePoints(int count)
75 {
76 QList<QGraphicsItem *> items = m_items.childItems();
77
78 for (int i = 0; i < count; ++i) {
79 delete(items.takeLast());
80 }
81 }
82
83 /*
84
85 void ScatterChartItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
86 {
87
88 QPointF clickedPoint(
89 m_minX + (event->lastPos().x() / m_clippingRect.width()) * (m_maxX-m_minX),
90 m_maxY - (event->lastPos().y() / m_clippingRect.height()) * (m_maxY-m_minY));
91 emit clicked(clickedPoint);
92
93 }
94 */
95
96 void ScatterChartItem::setGeometry(QVector<QPointF>& points)
97 {
98 if(points.size()==0) return;
99
100 int diff = XYChartItem::points().size() - points.size();
101
102 if(diff>0) {
103 deletePoints(diff);
104 }
105 else if(diff<0) {
106 createPoints(-diff);
107 }
108
109 if(diff!=0) handleUpdated();
110
111 QList<QGraphicsItem*> items = m_items.childItems();
112
113 for(int i=0; i< points.size();i++) {
114 QGraphicsItem* item = items.at(i);
115 const QPointF& point = points.at(i);
116 item->setPos(point.x()-1,point.y()-1);
117 if(!clipRect().contains(point)) {
118 item->setVisible(false);
119 }
120 else {
121 item->setVisible(true);
122 }
123 }
124
125 prepareGeometryChange();
126 m_rect = clipRect();
127 XYChartItem::setGeometry(points);
128 }
129
130
131 void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
132 {
133 Q_UNUSED(painter);
134 Q_UNUSED(option);
135 Q_UNUSED(widget);
136 }
137
138 void ScatterChartItem::setPen(const QPen& pen)
139 {
140 foreach(QGraphicsItem* item , m_items.childItems()) {
141 static_cast<QAbstractGraphicsShapeItem*>(item)->setPen(pen);
142 }
143 }
144
145 void ScatterChartItem::setBrush(const QBrush& brush)
146 {
147 foreach(QGraphicsItem* item , m_items.childItems()) {
148 static_cast<QAbstractGraphicsShapeItem*>(item)->setBrush(brush);
149 }
150 }
151
152 void ScatterChartItem::handleUpdated()
153 {
154
155 int count = m_items.childItems().count();
156
157 if(count==0) return;
158
159 bool recreate = m_size != m_series->size() || m_shape != m_series->shape();
160
161 //TODO: only rewrite on size change
162
163 m_size = m_series->size();
164 m_shape = m_series->shape();
165
166 if(recreate){
167 deletePoints(count);
168 createPoints(count);
169 }
170
171 setPen(m_series->pen());
172 setBrush(m_series->brush());
173
174 }
175
176 #include "moc_scatterchartitem_p.cpp"
177
178 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,50
1 #ifndef SCATTERPRESENTER_H
2 #define SCATTERPRESENTER_H
3
4 #include "qchartglobal.h"
5 #include "xychartitem_p.h"
6 #include <QObject>
7 #include <QPen>
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
11 class QScatterSeries;
12
13 class ScatterChartItem : public XYChartItem
14 {
15 Q_OBJECT
16 public:
17 explicit ScatterChartItem(QScatterSeries *series, QGraphicsObject *parent = 0);
18
19 public:
20 //from QGraphicsItem
21 QRectF boundingRect() const;
22 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23
24 void setPen(const QPen& pen);
25 void setBrush(const QBrush& brush);
26
27 signals:
28 void clicked(QPointF coordinates);
29
30 public slots:
31 void handleUpdated();
32
33 private:
34 void createPoints(int count);
35 void deletePoints(int count);
36
37 protected:
38 virtual void setGeometry(QVector<QPointF>& points);
39
40 private:
41 QScatterSeries *m_series;
42 QGraphicsItemGroup m_items;
43 int m_shape;
44 int m_size;
45 QRectF m_rect;
46 };
47
48 QTCOMMERCIALCHART_END_NAMESPACE
49
50 #endif // SCATTERPRESENTER_H
@@ -27,15 +27,16 int main(int argc, char *argv[])
27
27
28 // And more
28 // And more
29 //! [2]
29 //! [2]
30 *scatter << QPointF(2.0, 5.5) << QPointF(2.2, 5.4);
30 // *scatter << QPointF(2.0, 5.5) << QPointF(2.2, 5.4);
31 //! [2]
31 //! [2]
32
32
33 //! [3]
33 //! [3]
34 QBrush brush(QColor(255, 0, 0, 80), Qt::SolidPattern);
34 QBrush brush(Qt::red);
35 scatter->setBrush(brush);
35 scatter->setBrush(brush);
36 QPen pen(QColor(0, 255, 0, 60), 3);
36 QPen pen(Qt::black);
37 pen.setWidth(3);
37 scatter->setPen(pen);
38 scatter->setPen(pen);
38 scatter->setShape(QScatterSeries::MarkerShapeRectangle);
39 scatter->setShape(QScatterSeries::MarkerShapeCircle);
39 scatter->setSize(25.0);
40 scatter->setSize(25.0);
40 //! [3]
41 //! [3]
41
42
@@ -43,7 +44,7 int main(int argc, char *argv[])
43 QMainWindow w;
44 QMainWindow w;
44 w.resize(400, 300);
45 w.resize(400, 300);
45 w.setCentralWidget(chartView);
46 w.setCentralWidget(chartView);
46 w.setWindowFlags(Qt::FramelessWindowHint);
47 // w.setWindowFlags(Qt::FramelessWindowHint);
47 w.show();
48 w.show();
48
49
49 return a.exec();
50 return a.exec();
@@ -40,6 +40,8 MainWindow::~MainWindow()
40 void MainWindow::clickPoint(QPointF coordinate)
40 void MainWindow::clickPoint(QPointF coordinate)
41 {
41 {
42 // Remove the clicked point from the series and add points to the two other series we have
42 // Remove the clicked point from the series and add points to the two other series we have
43 //TODO: fix me
44 /*
43 int index = m_scatter->closestPoint(coordinate);
45 int index = m_scatter->closestPoint(coordinate);
44 QPointF point = m_scatter->data().at(index);
46 QPointF point = m_scatter->data().at(index);
45 Q_ASSERT(m_scatter->removeAt(index));
47 Q_ASSERT(m_scatter->removeAt(index));
@@ -48,4 +50,5 void MainWindow::clickPoint(QPointF coordinate)
48 *m_scatter2 << point;
50 *m_scatter2 << point;
49 point.ry() -= 0.25;
51 point.ry() -= 0.25;
50 *m_scatter3 << point;
52 *m_scatter3 << point;
53 */
51 }
54 }
@@ -126,14 +126,26 void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) const
126 {
126 {
127 switch(series->type())
127 switch(series->type())
128 {
128 {
129 case QSeries::SeriesTypeLine: {
129 case QSeries::SeriesTypeLine:
130 case QSeries::SeriesTypeSpline:
131 case QSeries::SeriesTypeScatter:
132 {
130
133
131 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
134 QXYSeries* xySeries = static_cast<QXYSeries*>(series);
132
135
133 for (int i = 0; i < lineSeries->count(); i++)
136 qreal minX(domain->minX());
137 qreal minY(domain->minY());
138 qreal maxX(domain->maxX());
139 qreal maxY(domain->maxY());
140
141 for (int i = 0; i < xySeries->count(); i++)
134 {
142 {
135 qreal x = lineSeries->x(i);
143 qreal x = xySeries->x(i);
136 qreal y = lineSeries->y(i);
144 qreal y = xySeries->y(i);
145 minX = qMin(minX, x);
146 minY = qMin(minY, y);
147 maxX = qMax(maxX, x);
148 maxY = qMax(maxY, y);
137 domain->setMinX(qMin(domain->minX(),x));
149 domain->setMinX(qMin(domain->minX(),x));
138 domain->setMinY(qMin(domain->minY(),y));
150 domain->setMinY(qMin(domain->minY(),y));
139 domain->setMaxX(qMax(domain->maxX(),x));
151 domain->setMaxX(qMax(domain->maxX(),x));
@@ -210,40 +222,6 void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) const
210 break;
222 break;
211 }
223 }
212
224
213 case QSeries::SeriesTypeScatter: {
214 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
215 Q_ASSERT(scatterSeries);
216 qreal minX(domain->minX());
217 qreal minY(domain->minY());
218 qreal maxX(domain->maxX());
219 qreal maxY(domain->maxY());
220 foreach (QPointF point, scatterSeries->data()) {
221 minX = qMin(minX, point.x());
222 minY = qMin(minY, point.y());
223 maxX = qMax(maxX, point.x());
224 maxY = qMax(maxY, point.y());
225 }
226 domain->setMinX(minX);
227 domain->setMinY(minY);
228 domain->setMaxX(maxX);
229 domain->setMaxY(maxY);
230 break;
231 }
232
233 case QSeries::SeriesTypeSpline: {
234 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
235
236 for (int i = 0; i < splineSeries->count(); i++)
237 {
238 qreal x = splineSeries->x(i);
239 qreal y = splineSeries->y(i);
240 domain->setMinX(qMin(domain->minX(),x));
241 domain->setMinY(qMin(domain->minY(),y));
242 domain->setMaxX(qMax(domain->maxX(),x));
243 domain->setMaxY(qMax(domain->maxY(),y));
244 }
245 break;
246 }
247
225
248 default: {
226 default: {
249 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
227 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
@@ -22,7 +22,7
22 #include "linechartitem_p.h"
22 #include "linechartitem_p.h"
23 #include "linechartanimationitem_p.h"
23 #include "linechartanimationitem_p.h"
24 #include "piepresenter_p.h"
24 #include "piepresenter_p.h"
25 #include "scatterpresenter_p.h"
25 #include "scatterchartitem_p.h"
26 #include "splinechartitem_p.h"
26 #include "splinechartitem_p.h"
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -216,7 +216,7 void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain)
216 }
216 }
217 case QSeries::SeriesTypeScatter: {
217 case QSeries::SeriesTypeScatter: {
218 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
218 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
219 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
219 ScatterChartItem *scatterPresenter = new ScatterChartItem(scatterSeries, m_chart);
220 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
220 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
221 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
221 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
222 QObject::connect(domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),
222 QObject::connect(domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),
@@ -22,7 +22,7
22 #include "percentbarpresenter_p.h"
22 #include "percentbarpresenter_p.h"
23 #include "linechartitem_p.h"
23 #include "linechartitem_p.h"
24 #include "areachartitem_p.h"
24 #include "areachartitem_p.h"
25 #include "scatterpresenter_p.h"
25 #include "scatterchartitem_p.h"
26 #include "piepresenter_p.h"
26 #include "piepresenter_p.h"
27 #include "splinechartitem_p.h"
27 #include "splinechartitem_p.h"
28
28
@@ -121,7 +121,7 void ChartTheme::decorate(ChartItem* item, QSeries* series,int count)
121 case QSeries::SeriesTypeScatter: {
121 case QSeries::SeriesTypeScatter: {
122 QScatterSeries* s = qobject_cast<QScatterSeries*>(series);
122 QScatterSeries* s = qobject_cast<QScatterSeries*>(series);
123 Q_ASSERT(s);
123 Q_ASSERT(s);
124 ScatterPresenter* i = static_cast<ScatterPresenter*>(item);
124 ScatterChartItem* i = static_cast<ScatterChartItem*>(item);
125 Q_ASSERT(i);
125 Q_ASSERT(i);
126 decorate(i, s, count);
126 decorate(i, s, count);
127 break;
127 break;
@@ -197,9 +197,9 void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,i
197 }
197 }
198 }
198 }
199
199
200 void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count)
200 void ChartTheme::decorate(ScatterChartItem* item, QScatterSeries* series, int count)
201 {
201 {
202 Q_ASSERT(presenter);
202 Q_ASSERT(item);
203 Q_ASSERT(series);
203 Q_ASSERT(series);
204
204
205 QColor color = m_seriesColor.at(count % m_seriesColor.size());
205 QColor color = m_seriesColor.at(count % m_seriesColor.size());
@@ -207,11 +207,11 void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, i
207 //color.setAlpha(120);
207 //color.setAlpha(120);
208
208
209 QBrush brush(color, Qt::SolidPattern);
209 QBrush brush(color, Qt::SolidPattern);
210 presenter->m_markerBrush = brush;
210 item->setBrush(Qt::blue);
211
211
212 QPen pen(brush, 3);
212 QPen pen(brush, 3);
213 pen.setColor(color);
213 pen.setColor(color);
214 presenter->m_markerPen = pen;
214 item->setPen(pen);
215 }
215 }
216
216
217 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
217 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
@@ -18,7 +18,7 class QStackedBarSeries;
18 class QPercentBarSeries;
18 class QPercentBarSeries;
19 class PercentBarPresenter;
19 class PercentBarPresenter;
20 class QScatterSeries;
20 class QScatterSeries;
21 class ScatterPresenter;
21 class ScatterChartItem;
22 class PiePresenter;
22 class PiePresenter;
23 class QPieSeries;
23 class QPieSeries;
24 class SplineChartItem;
24 class SplineChartItem;
@@ -40,7 +40,7 public:
40 void decorate(PercentBarPresenter* item, QPercentBarSeries* series,int count);
40 void decorate(PercentBarPresenter* item, QPercentBarSeries* series,int count);
41 void decorate(LineChartItem* item, QLineSeries* series,int count);
41 void decorate(LineChartItem* item, QLineSeries* series,int count);
42 void decorate(AreaChartItem* item, QAreaSeries* series,int count);
42 void decorate(AreaChartItem* item, QAreaSeries* series,int count);
43 void decorate(ScatterPresenter* presenter, QScatterSeries* series, int count);
43 void decorate(ScatterChartItem* presenter, QScatterSeries* series, int count);
44 void decorate(PiePresenter* item, QPieSeries* series, int count);
44 void decorate(PiePresenter* item, QPieSeries* series, int count);
45 void decorate(QChartAxis* axis,AxisItem* item);
45 void decorate(QChartAxis* axis,AxisItem* item);
46 void decorate(SplineChartItem* presenter, QSplineSeries* series, int count);
46 void decorate(SplineChartItem* presenter, QSplineSeries* series, int count);
@@ -14,7 +14,7 class LineChartItem : public XYChartItem
14 {
14 {
15 Q_OBJECT
15 Q_OBJECT
16 public:
16 public:
17 LineChartItem(QLineSeries* series,QGraphicsItem *parent = 0);
17 explicit LineChartItem(QLineSeries* series,QGraphicsItem *parent = 0);
18 ~ LineChartItem(){};
18 ~ LineChartItem(){};
19
19
20 //from QGraphicsItem
20 //from QGraphicsItem
@@ -25,9 +25,6 public:
25 void setPen(const QPen& pen);
25 void setPen(const QPen& pen);
26 void setPointsVisible(bool visible);
26 void setPointsVisible(bool visible);
27
27
28 void createPoints(int count);
29 void deletePoints(int count);
30
31 public slots:
28 public slots:
32 void handleUpdated();
29 void handleUpdated();
33
30
@@ -35,6 +32,10 protected:
35 virtual void setGeometry(QVector<QPointF>& points);
32 virtual void setGeometry(QVector<QPointF>& points);
36
33
37 private:
34 private:
35 void createPoints(int count);
36 void deletePoints(int count);
37
38 private:
38 QLineSeries* m_series;
39 QLineSeries* m_series;
39 QGraphicsItemGroup m_items;
40 QGraphicsItemGroup m_items;
40 QPainterPath m_path;
41 QPainterPath m_path;
@@ -30,53 +30,57 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 */
30 */
31
31
32 /*!
32 /*!
33 \fn QPen QLineSeries::pen() const
33 \fn bool QLineSeries::pointsVisible() const
34 \brief Returns the pen used to draw line for this series.
35 \sa setPen()
36 */
37
38 /*!
39 \fn bool QLineSeries::pointsVisible() const
40 \brief Returns if the points are drawn for this series.
34 \brief Returns if the points are drawn for this series.
41 \sa setPointsVisible()
35 \sa setPointsVisible()
42 */
36 */
43
37
44
45 /*!
46 \fn void QLineSeries::pointReplaced(int index)
47 \brief \internal \a index
48 */
49
50 /*!
38 /*!
51 \fn void QLineSeries::pointAdded(int index)
39 \fn QPen QLineSeries::linePen() const
52 \brief \internal \a index
40 \brief Returns the pen used to draw line connecting points.
41 \sa setPen()
53 */
42 */
54
43
55 /*!
44 /*!
56 \fn void QLineSeries::pointRemoved(int index)
45 Constructs empty series object which is a child of \a parent.
57 \brief \internal \a index
46 When series object is added to QChartView or QChart instance ownerships is transfered.
58 */
47 */
48 QLineSeries::QLineSeries(QObject* parent):QXYSeries(parent),
49 m_pointsVisible(false)
50 {
59
51
52 }
60 /*!
53 /*!
61 \fn void QLineSeries::updated()
54 Destroys the object. Series added to QChartView or QChart instances are owned by those,
62 \brief \internal
55 and are deleted when mentioned object are destroyed.
63 */
56 */
57 QLineSeries::~QLineSeries()
58 {
59 }
64
60
65 /*!
61 /*!
66 Constructs empty series object which is a child of \a parent.
62 Sets \a pen used for drawing line connecting points.
67 When series object is added to QChartView or QChart instance ownerships is transfered.
68 */
63 */
69 QLineSeries::QLineSeries(QObject* parent):QXYSeries(parent)
64 void QLineSeries::setLinePen(const QPen& pen)
70 {
65 {
66 if(pen!=m_pen){
67 m_pen=pen;
68 emit updated();
69 }
71 }
70 }
71
72 /*!
72 /*!
73 Destroys the object. Series added to QChartView or QChart instances are owned by those,
73 Sets if data points are \a visible and should be drawn on line.
74 and are deleted when mentioned object are destroyed.
75 */
74 */
76 QLineSeries::~QLineSeries()
75 void QLineSeries::setPointsVisible(bool visible)
77 {
76 {
77 if(m_pointsVisible!=visible){
78 m_pointsVisible=visible;
79 emit updated();
80 }
78 }
81 }
79
82
83
80 QDebug operator<< (QDebug debug, const QLineSeries series)
84 QDebug operator<< (QDebug debug, const QLineSeries series)
81 {
85 {
82 Q_ASSERT(series.m_x.size() == series.m_y.size());
86 Q_ASSERT(series.m_x.size() == series.m_y.size());
@@ -16,9 +16,18 public:
16 QLineSeries(QObject* parent=0);
16 QLineSeries(QObject* parent=0);
17 virtual ~QLineSeries();
17 virtual ~QLineSeries();
18
18
19 void setLinePen(const QPen& pen);
20 QPen linePen() const {return m_pen;}
21
22 void setPointsVisible(bool visible);
23 bool pointsVisible() const {return m_pointsVisible;}
24
19 public: // from QChartSeries
25 public: // from QChartSeries
20 virtual QSeriesType type() const {return QSeries::SeriesTypeLine;}
26 virtual QSeriesType type() const {return QSeries::SeriesTypeLine;}
21 friend QDebug operator<< (QDebug d, const QLineSeries series);
27 friend QDebug operator<< (QDebug d, const QLineSeries series);
28 private:
29 QPen m_pen;
30 bool m_pointsVisible;
22
31
23 };
32 };
24
33
@@ -1,5 +1,4
1 #include "qscatterseries.h"
1 #include "qscatterseries.h"
2 #include "scatterseries_p.h"
3 #include "qchart.h"
2 #include "qchart.h"
4
3
5 /*!
4 /*!
@@ -53,29 +52,13
53
52
54 QTCOMMERCIALCHART_BEGIN_NAMESPACE
53 QTCOMMERCIALCHART_BEGIN_NAMESPACE
55
54
56 QScatterSeriesPrivate::QScatterSeriesPrivate(QObject *parent) :
57 QObject(parent),
58 m_data(QList<QPointF>()),
59 m_markerPen(QPen(QColor::Invalid)),
60 m_markerBrush(QBrush(QColor::Invalid)),
61 m_markerShape(QScatterSeries::MarkerShapeDefault),
62 m_markerSize(9.0)
63 {
64 }
65
66 void QScatterSeriesPrivate::emitChanged()
67 {
68 emit changed();
69 }
70
71 #include "moc_scatterseries_p.cpp"
72
73 /*!
55 /*!
74 Constructs a series object which is a child of \a parent.
56 Constructs a series object which is a child of \a parent.
75 */
57 */
76 QScatterSeries::QScatterSeries(QObject *parent) :
58 QScatterSeries::QScatterSeries(QObject *parent) :
77 QSeries(parent),
59 QXYSeries(parent),
78 d(new QScatterSeriesPrivate(this))
60 m_shape(QScatterSeries::MarkerShapeDefault),
61 m_size(9.0)
79 {
62 {
80 }
63 }
81
64
@@ -84,35 +67,9 QScatterSeries::QScatterSeries(QObject *parent) :
84 */
67 */
85 QScatterSeries::~QScatterSeries()
68 QScatterSeries::~QScatterSeries()
86 {
69 {
87 delete d;
88 }
70 }
89
71
90 /*!
91 Add single data point with \a x and \a y coordinates to the series.
92 */
93 void QScatterSeries::add(qreal x, qreal y)
94 {
95 d->m_data.append(QPointF(x, y));
96 d->emitChanged();
97 }
98
99 /*!
100 Add single data point with \a value to the series.
101 */
102 void QScatterSeries::add(QPointF value)
103 {
104 d->m_data.append(value);
105 d->emitChanged();
106 }
107
72
108 /*!
109 Add list of \a points to the series.
110 */
111 void QScatterSeries::add(QList<QPointF> points)
112 {
113 d->m_data.append(points);
114 d->emitChanged();
115 }
116
73
117 /*!
74 /*!
118 Stream operator for adding a data point with \a value to the series.
75 Stream operator for adding a data point with \a value to the series.
@@ -121,93 +78,49 void QScatterSeries::add(QList<QPointF> points)
121 For example:
78 For example:
122 \snippet ../example/scatter/main.cpp 2
79 \snippet ../example/scatter/main.cpp 2
123 */
80 */
124 QScatterSeries& QScatterSeries::operator << (const QPointF &value)
81
125 {
126 d->m_data.append(value);
127 d->emitChanged();
128 return *this;
129 }
130
82
131 /*!
83 /*!
132 Stream operator for adding a list of points to the series.
84 Stream operator for adding a list of points to the series.
133 \sa add()
85 \sa add()
134 */
86 */
135 QScatterSeries& QScatterSeries::operator << (QList<QPointF> value)
87
136 {
137 d->m_data.append(value);
138 d->emitChanged();
139 return *this;
140 }
141
88
142 /*!
89 /*!
143 Replaces the data of the series with the given list of data \a points.
90 Replaces the data of the series with the given list of data \a points.
144 */
91 */
145 void QScatterSeries::setData(QList<QPointF> points)
92
146 {
147 d->m_data = points;
148 d->emitChanged();
149 }
150
93
151 /*!
94 /*!
152 Returns the current list of data points of the series.
95 Returns the current list of data points of the series.
153 */
96 */
154 QList<QPointF> QScatterSeries::data()
155 {
156 return d->m_data;
157 }
158
97
159 /*!
98 /*!
160 Replaces the point at \a index with \a newPoint. Returns true if \a index is a valid position
99 Replaces the point at \a index with \a newPoint. Returns true if \a index is a valid position
161 in the series data, false otherwise.
100 in the series data, false otherwise.
162 */
101 */
163 bool QScatterSeries::replace(int index, QPointF newPoint)
102
164 {
165 if (index >= 0 && index < d->m_data.count()) {
166 d->m_data.replace(index, newPoint);
167 d->emitChanged();
168 return true;
169 }
170 return false;
171 }
172
103
173 /*!
104 /*!
174 Remove the data point at \a index. Returns true if a point was removed, false if the point
105 Remove the data point at \a index. Returns true if a point was removed, false if the point
175 at \a index does not exist on the series.
106 at \a index does not exist on the series.
176 */
107 */
177 bool QScatterSeries::removeAt(int index)
108
178 {
179 if (index >=0 && index < d->m_data.count()) {
180 d->m_data.removeAt(index);
181 d->emitChanged();
182 return true;
183 }
184 return false;
185 }
186
109
187 /*!
110 /*!
188 Remove all occurrences of \a point from the series and returns the number of points removed.
111 Remove all occurrences of \a point from the series and returns the number of points removed.
189 */
112 */
190 int QScatterSeries::removeAll(QPointF point)
113
191 {
192 int count = d->m_data.removeAll(point);
193 d->emitChanged();
194 return count;
195 }
196
114
197 /*!
115 /*!
198 Remove all data points from the series.
116 Remove all data points from the series.
199 */
117 */
200 void QScatterSeries::clear()
201 {
202 d->m_data.clear();
203 d->emitChanged();
204 }
205
118
206 /*!
119 /*!
207 Returns the index of the data point that is closest to \a coordinate. If several data points
120 Returns the index of the data point that is closest to \a coordinate. If several data points
208 are at the same distance from the \a coordinate, returns the last one. If no points exist,
121 are at the same distance from the \a coordinate, returns the last one. If no points exist,
209 returns -1.
122 returns -1.
210 */
123
211 int QScatterSeries::closestPoint(QPointF coordinate)
124 int QScatterSeries::closestPoint(QPointF coordinate)
212 {
125 {
213 qreal distance(-1);
126 qreal distance(-1);
@@ -222,14 +135,12 int QScatterSeries::closestPoint(QPointF coordinate)
222 }
135 }
223 return pointIndex;
136 return pointIndex;
224 }
137 }
138 */
225
139
226 /*!
140 /*!
227 Returns the pen used for drawing markers.
141 Returns the pen used for drawing markers.
228 */
142 */
229 QPen QScatterSeries::pen() const
143
230 {
231 return d->m_markerPen;
232 }
233
144
234 /*!
145 /*!
235 Overrides the default pen used for drawing a marker item with a user defined \a pen. The
146 Overrides the default pen used for drawing a marker item with a user defined \a pen. The
@@ -238,19 +149,12 QPen QScatterSeries::pen() const
238 \sa setBrush()
149 \sa setBrush()
239 \sa QChart::setChartTheme()
150 \sa QChart::setChartTheme()
240 */
151 */
241 void QScatterSeries::setPen(const QPen &pen)
152
242 {
243 d->m_markerPen = pen;
244 d->emitChanged();
245 }
246
153
247 /*!
154 /*!
248 Returns the brush used for drawing markers.
155 Returns the brush used for drawing markers.
249 */
156 */
250 QBrush QScatterSeries::brush() const
157
251 {
252 return d->m_markerBrush;
253 }
254
158
255 /*!
159 /*!
256 Overrides the default brush of the marker items with a user defined \a brush. The default brush
160 Overrides the default brush of the marker items with a user defined \a brush. The default brush
@@ -259,18 +163,13 QBrush QScatterSeries::brush() const
259 \sa setPen()
163 \sa setPen()
260 \sa QChart::setChartTheme()
164 \sa QChart::setChartTheme()
261 */
165 */
262 void QScatterSeries::setBrush(const QBrush &brush)
263 {
264 d->m_markerBrush = brush;
265 d->emitChanged();
266 }
267
166
268 /*!
167 /*!
269 Returns the shape used for drawing markers.
168 Returns the shape used for drawing markers.
270 */
169 */
271 QScatterSeries::MarkerShape QScatterSeries::shape() const
170 QScatterSeries::MarkerShape QScatterSeries::shape() const
272 {
171 {
273 return (QScatterSeries::MarkerShape) d->m_markerShape;
172 return (QScatterSeries::MarkerShape) m_shape;
274 }
173 }
275
174
276 /*!
175 /*!
@@ -279,8 +178,8 QScatterSeries::MarkerShape QScatterSeries::shape() const
279 */
178 */
280 void QScatterSeries::setShape(MarkerShape shape)
179 void QScatterSeries::setShape(MarkerShape shape)
281 {
180 {
282 d->m_markerShape = shape;
181 m_shape = shape;
283 d->emitChanged();
182 emit updated();
284 }
183 }
285
184
286 /*!
185 /*!
@@ -288,7 +187,7 void QScatterSeries::setShape(MarkerShape shape)
288 */
187 */
289 qreal QScatterSeries::size() const
188 qreal QScatterSeries::size() const
290 {
189 {
291 return d->m_markerSize;
190 return m_size;
292 }
191 }
293
192
294 /*!
193 /*!
@@ -296,10 +195,12 qreal QScatterSeries::size() const
296 */
195 */
297 void QScatterSeries::setSize(qreal size)
196 void QScatterSeries::setSize(qreal size)
298 {
197 {
299 d->m_markerSize = size;
198 m_size = size;
300 d->emitChanged();
199 emit updated();
301 }
200 }
302
201
202
203
303 #include "moc_qscatterseries.cpp"
204 #include "moc_qscatterseries.cpp"
304
205
305 QTCOMMERCIALCHART_END_NAMESPACE
206 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,14 +1,15
1 #ifndef QSCATTERSERIES_H
1 #ifndef QSCATTERSERIES_H
2 #define QSCATTERSERIES_H
2 #define QSCATTERSERIES_H
3
3
4 #include "qseries.h"
4 #include "qchartglobal.h"
5 #include "qxyseries.h"
5 #include <QRectF>
6 #include <QRectF>
6 #include <QColor>
7 #include <QColor>
7
8
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 class QScatterSeriesPrivate;
10 class QScatterSeriesPrivate;
10
11
11 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QSeries
12 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QXYSeries
12 {
13 {
13 Q_OBJECT
14 Q_OBJECT
14
15
@@ -33,37 +34,19 public: // from QChartSeries
33 QSeriesType type() const { return QSeries::SeriesTypeScatter; }
34 QSeriesType type() const { return QSeries::SeriesTypeScatter; }
34
35
35 public:
36 public:
36 void add(qreal x, qreal y);
37 //int closestPoint(QPointF coordinate);
37 void add(QPointF value);
38 void add(QList<QPointF> points);
39 void setData(QList<QPointF> points);
40 QScatterSeries& operator << (const QPointF &value);
41 QScatterSeries& operator << (QList<QPointF> points);
42 QList<QPointF> data();
43 bool replace(int index, QPointF newPoint);
44 bool removeAt(int index);
45 int removeAll(QPointF point);
46 void clear();
47 int closestPoint(QPointF coordinate);
48 //TODO: insert, replace...?
49
38
50 QPen pen() const;
51 void setPen(const QPen &pen);
52 QBrush brush() const;
53 void setBrush(const QBrush &brush);
54 MarkerShape shape() const;
39 MarkerShape shape() const;
55 void setShape(MarkerShape shape);
40 void setShape(MarkerShape shape);
56 qreal size() const;
41 qreal size() const;
57 void setSize(qreal size);
42 void setSize(qreal size);
58
43
59 Q_SIGNALS:
44 signals:
60 void clicked(QPointF coordinate);
45 void clicked(QPointF coordinate);
61
46
62 private:
47 private:
63 Q_DECLARE_PRIVATE(QScatterSeries)
48 MarkerShape m_shape;
64 Q_DISABLE_COPY(QScatterSeries)
49 qreal m_size;
65 friend class ScatterPresenter;
66 QScatterSeriesPrivate *d;
67 };
50 };
68
51
69 QTCOMMERCIALCHART_END_NAMESPACE
52 QTCOMMERCIALCHART_END_NAMESPACE
@@ -3,11 +3,10 DEPENDPATH += $$PWD
3
3
4 SOURCES += \
4 SOURCES += \
5 $$PWD/qscatterseries.cpp \
5 $$PWD/qscatterseries.cpp \
6 $$PWD/scatterpresenter.cpp
6 $$PWD/scatterchartitem.cpp
7
7
8 PRIVATE_HEADERS += \
8 PRIVATE_HEADERS += \
9 $$PWD/scatterpresenter_p.h \
9 $$PWD/scatterchartitem_p.h
10 $$PWD/scatterseries_p.h
11
10
12 PUBLIC_HEADERS += \
11 PUBLIC_HEADERS += \
13 $$PWD/qscatterseries.h
12 $$PWD/qscatterseries.h
@@ -26,7 +26,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 */
26 */
27
27
28 QSplineSeries::QSplineSeries(QObject *parent) :
28 QSplineSeries::QSplineSeries(QObject *parent) :
29 QXYSeries(parent)
29 QLineSeries(parent)
30 {
30 {
31 connect(this,SIGNAL(pointAdded(int)), this, SLOT(updateControlPoints()));
31 connect(this,SIGNAL(pointAdded(int)), this, SLOT(updateControlPoints()));
32 connect(this,SIGNAL(pointRemoved(int)), this, SLOT(updateControlPoints()));
32 connect(this,SIGNAL(pointRemoved(int)), this, SLOT(updateControlPoints()));
@@ -3,13 +3,13
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include <QtGlobal>
5 #include <QtGlobal>
6 #include "qxyseries.h"
6 #include "qlineseries.h"
7 #include <QList>
7 #include <QList>
8 #include <QPointF>
8 #include <QPointF>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class QTCOMMERCIALCHART_EXPORT QSplineSeries : public QXYSeries
12 class QTCOMMERCIALCHART_EXPORT QSplineSeries : public QLineSeries
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 public:
15 public:
@@ -4,23 +4,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
4
5 /*!
5 /*!
6 \class QXYSeries
6 \class QXYSeries
7 \brief The QXYSeries class is used for making line charts.
7 \brief The QXYSeries class is a base class for line, spline and scatter series.
8
9 \mainclass
10
11 A line chart is used to show information as a series of data points
12 connected by straight lines.
13
14 \image linechart.png
15
16 Creating basic line chart is simple:
17 \code
18 QXYSeries* series = new QXYSeries();
19 series->add(0, 6);
20 series->add(2, 4);
21 ...
22 chartView->addSeries(series);
23 \endcode
24 */
8 */
25
9
26 /*!
10 /*!
@@ -31,18 +15,11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
15
32 /*!
16 /*!
33 \fn QPen QXYSeries::pen() const
17 \fn QPen QXYSeries::pen() const
34 \brief Returns the pen used to draw line for this series.
18 \brief Returns the pen used to draw points for this series.
35 \sa setPen()
19 \sa setPen()
36 */
20 */
37
21
38 /*!
22 /*!
39 \fn bool QXYSeries::pointsVisible() const
40 \brief Returns if the points are drawn for this series.
41 \sa setPointsVisible()
42 */
43
44
45 /*!
46 \fn void QXYSeries::pointReplaced(int index)
23 \fn void QXYSeries::pointReplaced(int index)
47 \brief \internal \a index
24 \brief \internal \a index
48 */
25 */
@@ -66,8 +43,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
66 Constructs empty series object which is a child of \a parent.
43 Constructs empty series object which is a child of \a parent.
67 When series object is added to QChartView or QChart instance ownerships is transfered.
44 When series object is added to QChartView or QChart instance ownerships is transfered.
68 */
45 */
69 QXYSeries::QXYSeries(QObject* parent):QSeries(parent),
46 QXYSeries::QXYSeries(QObject* parent):QSeries(parent)
70 m_pointsVisible(false)
71 {
47 {
72 }
48 }
73 /*!
49 /*!
@@ -174,24 +150,25 int QXYSeries::count() const
174 }
150 }
175
151
176 /*!
152 /*!
177 Sets \a pen used for drawing given series..
153 Sets \a pen used for points on the chart.
178 */
154 */
179 void QXYSeries::setPen(const QPen& pen)
155 void QXYSeries::setPen(const QPen& pen)
180 {
156 {
181 if(pen!=m_pen){
157 if(pen!=m_pen){
182 m_pen=pen;
158 m_pen=pen;
183 emit updated();
159 emit updated();
184 }
160 }
185 }
161 }
186
162
187 /*!
163 /*!
188 Sets if data points are \a visible and should be drawn on line.
164 Sets \a brush used for points on the chart.
189 */
165 */
190 void QXYSeries::setPointsVisible(bool visible)
166
167 void QXYSeries::setBrush(const QBrush& brush)
191 {
168 {
192 if(m_pointsVisible!=visible){
169 if(brush!=m_brush){
193 m_pointsVisible=visible;
170 m_brush=brush;
194 emit updated();
171 emit updated();
195 }
172 }
196 }
173 }
197
174
@@ -12,7 +12,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 class QTCOMMERCIALCHART_EXPORT QXYSeries : public QSeries
12 class QTCOMMERCIALCHART_EXPORT QXYSeries : public QSeries
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 public:
15 protected:
16 QXYSeries(QObject* parent=0);
16 QXYSeries(QObject* parent=0);
17 virtual ~QXYSeries();
17 virtual ~QXYSeries();
18
18
@@ -30,12 +30,18 public:
30 qreal y(int pos) const;
30 qreal y(int pos) const;
31
31
32 QXYSeries& operator << (const QPointF &point);
32 QXYSeries& operator << (const QPointF &point);
33 /*
34 void add(QList<QPointF> points);
35 void setData(QList<QPointF> points);
36 QScatterSeries& operator << (const QPointF &value);
37 QScatterSeries& operator << (QList<QPointF> points);
38 int removeAll(QPointF point);
39 */
33
40
34 void setPen(const QPen& pen);
41 void setPen(const QPen& pen);
35 QPen pen() const {return m_pen;}
42 QPen pen() const {return m_pen;}
36
43 void setBrush(const QBrush& pen);
37 void setPointsVisible(bool visible);
44 QBrush brush() const {return m_brush;}
38 bool pointsVisible() const {return m_pointsVisible;}
39
45
40 signals:
46 signals:
41 void updated();
47 void updated();
@@ -49,7 +55,7 protected:
49 QVector<qreal> m_y;
55 QVector<qreal> m_y;
50
56
51 QPen m_pen;
57 QPen m_pen;
52 bool m_pointsVisible;
58 QBrush m_brush;
53
59
54 };
60 };
55
61
@@ -14,7 +14,7 class XYChartItem : public QObject , public ChartItem
14 {
14 {
15 Q_OBJECT
15 Q_OBJECT
16 public:
16 public:
17 XYChartItem(QXYSeries* series,QGraphicsItem *parent = 0);
17 explicit XYChartItem(QXYSeries* series,QGraphicsItem *parent = 0);
18 ~ XYChartItem(){};
18 ~ XYChartItem(){};
19
19
20 QVector<QPointF> points() const {return m_points;}
20 QVector<QPointF> points() const {return m_points;}
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now