@@ -0,0 +1,189 | |||||
|
1 | #include "qxyseries.h" | |||
|
2 | ||||
|
3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |||
|
4 | ||||
|
5 | /*! | |||
|
6 | \class QXYSeries | |||
|
7 | \brief The QXYSeries class is used for making line charts. | |||
|
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 | */ | |||
|
25 | ||||
|
26 | /*! | |||
|
27 | \fn virtual QSeriesType QXYSeries::type() const | |||
|
28 | \brief Returns type of series. | |||
|
29 | \sa QSeries, QSeriesType | |||
|
30 | */ | |||
|
31 | ||||
|
32 | /*! | |||
|
33 | \fn QPen QXYSeries::pen() const | |||
|
34 | \brief Returns the pen used to draw line for this series. | |||
|
35 | \sa setPen() | |||
|
36 | */ | |||
|
37 | ||||
|
38 | /*! | |||
|
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) | |||
|
47 | \brief \internal \a index | |||
|
48 | */ | |||
|
49 | ||||
|
50 | /*! | |||
|
51 | \fn void QXYSeries::pointAdded(int index) | |||
|
52 | \brief \internal \a index | |||
|
53 | */ | |||
|
54 | ||||
|
55 | /*! | |||
|
56 | \fn void QXYSeries::pointRemoved(int index) | |||
|
57 | \brief \internal \a index | |||
|
58 | */ | |||
|
59 | ||||
|
60 | /*! | |||
|
61 | \fn void QXYSeries::updated() | |||
|
62 | \brief \internal | |||
|
63 | */ | |||
|
64 | ||||
|
65 | /*! | |||
|
66 | 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. | |||
|
68 | */ | |||
|
69 | QXYSeries::QXYSeries(QObject* parent):QSeries(parent) | |||
|
70 | { | |||
|
71 | } | |||
|
72 | /*! | |||
|
73 | Destroys the object. Series added to QChartView or QChart instances are owned by those, | |||
|
74 | and are deleted when mentioned object are destroyed. | |||
|
75 | */ | |||
|
76 | QXYSeries::~QXYSeries() | |||
|
77 | { | |||
|
78 | } | |||
|
79 | ||||
|
80 | /*! | |||
|
81 | Adds data point \a x \a y to the series. Points are connected with lines on the chart. | |||
|
82 | */ | |||
|
83 | void QXYSeries::add(qreal x,qreal y) | |||
|
84 | { | |||
|
85 | Q_ASSERT(m_x.size() == m_y.size()); | |||
|
86 | m_x<<x; | |||
|
87 | m_y<<y; | |||
|
88 | emit pointAdded(m_x.size()-1); | |||
|
89 | } | |||
|
90 | ||||
|
91 | /*! | |||
|
92 | This is an overloaded function. | |||
|
93 | Adds data \a point to the series. Points are connected with lines on the chart. | |||
|
94 | */ | |||
|
95 | void QXYSeries::add(const QPointF& point) | |||
|
96 | { | |||
|
97 | add(point.x(),point.y()); | |||
|
98 | } | |||
|
99 | ||||
|
100 | /*! | |||
|
101 | Modifies \a y value for given \a x a value. | |||
|
102 | */ | |||
|
103 | void QXYSeries::replace(qreal x,qreal y) | |||
|
104 | { | |||
|
105 | int index = m_x.indexOf(x); | |||
|
106 | m_x[index]=x; | |||
|
107 | m_y[index]=y; | |||
|
108 | emit pointReplaced(index); | |||
|
109 | } | |||
|
110 | ||||
|
111 | /*! | |||
|
112 | This is an overloaded function. | |||
|
113 | Replaces current y value of for given \a point x value with \a point y value. | |||
|
114 | */ | |||
|
115 | void QXYSeries::replace(const QPointF& point) | |||
|
116 | { | |||
|
117 | replace(point.x(),point.y()); | |||
|
118 | } | |||
|
119 | ||||
|
120 | /*! | |||
|
121 | Removes current \a x and y value. | |||
|
122 | */ | |||
|
123 | void QXYSeries::remove(qreal x) | |||
|
124 | { | |||
|
125 | int index = m_x.indexOf(x); | |||
|
126 | emit pointRemoved(index); | |||
|
127 | m_x.remove(index); | |||
|
128 | m_y.remove(index); | |||
|
129 | } | |||
|
130 | ||||
|
131 | /*! | |||
|
132 | Removes current \a point x value. Note \a point y value is ignored. | |||
|
133 | */ | |||
|
134 | void QXYSeries::remove(const QPointF& point) | |||
|
135 | { | |||
|
136 | remove(point.x()); | |||
|
137 | } | |||
|
138 | ||||
|
139 | /*! | |||
|
140 | Clears all the data. | |||
|
141 | */ | |||
|
142 | void QXYSeries::clear() | |||
|
143 | { | |||
|
144 | m_x.clear(); | |||
|
145 | m_y.clear(); | |||
|
146 | } | |||
|
147 | ||||
|
148 | /*! | |||
|
149 | \internal \a pos | |||
|
150 | */ | |||
|
151 | qreal QXYSeries::x(int pos) const | |||
|
152 | { | |||
|
153 | return m_x.at(pos); | |||
|
154 | } | |||
|
155 | ||||
|
156 | /*! | |||
|
157 | \internal \a pos | |||
|
158 | */ | |||
|
159 | qreal QXYSeries::y(int pos) const | |||
|
160 | { | |||
|
161 | return m_y.at(pos); | |||
|
162 | } | |||
|
163 | ||||
|
164 | /*! | |||
|
165 | Returns number of data points within series. | |||
|
166 | */ | |||
|
167 | int QXYSeries::count() const | |||
|
168 | { | |||
|
169 | Q_ASSERT(m_x.size() == m_y.size()); | |||
|
170 | ||||
|
171 | return m_x.size(); | |||
|
172 | ||||
|
173 | } | |||
|
174 | ||||
|
175 | /*! | |||
|
176 | Stream operator for adding a data \a point to the series. | |||
|
177 | \sa add() | |||
|
178 | */ | |||
|
179 | ||||
|
180 | QXYSeries& QXYSeries::operator<< (const QPointF &point) | |||
|
181 | { | |||
|
182 | add(point); | |||
|
183 | return *this; | |||
|
184 | } | |||
|
185 | ||||
|
186 | ||||
|
187 | #include "moc_qxyseries.cpp" | |||
|
188 | ||||
|
189 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -0,0 +1,49 | |||||
|
1 | #ifndef QXYSERIES_H_ | |||
|
2 | #define QXYSERIES_H_ | |||
|
3 | ||||
|
4 | #include "qchartglobal.h" | |||
|
5 | #include "qseries.h" | |||
|
6 | #include <QDebug> | |||
|
7 | #include <QPen> | |||
|
8 | #include <QBrush> | |||
|
9 | ||||
|
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |||
|
11 | ||||
|
12 | class QTCOMMERCIALCHART_EXPORT QXYSeries : public QSeries | |||
|
13 | { | |||
|
14 | Q_OBJECT | |||
|
15 | public: | |||
|
16 | QXYSeries(QObject* parent=0); | |||
|
17 | virtual ~QXYSeries(); | |||
|
18 | ||||
|
19 | public: | |||
|
20 | void add(qreal x, qreal y); | |||
|
21 | void add(const QPointF& point); | |||
|
22 | void replace(qreal x,qreal y); | |||
|
23 | void replace(const QPointF& point); | |||
|
24 | void remove(qreal x); | |||
|
25 | void remove(const QPointF& point); | |||
|
26 | void clear(); | |||
|
27 | ||||
|
28 | int count() const; | |||
|
29 | qreal x(int pos) const; | |||
|
30 | qreal y(int pos) const; | |||
|
31 | ||||
|
32 | QXYSeries& operator << (const QPointF &point); | |||
|
33 | ||||
|
34 | signals: | |||
|
35 | void updated(); | |||
|
36 | void pointReplaced(int index); | |||
|
37 | void pointRemoved(int index); | |||
|
38 | void pointAdded(int index); | |||
|
39 | ||||
|
40 | ||||
|
41 | protected: | |||
|
42 | QVector<qreal> m_x; | |||
|
43 | QVector<qreal> m_y; | |||
|
44 | ||||
|
45 | }; | |||
|
46 | ||||
|
47 | QTCOMMERCIALCHART_END_NAMESPACE | |||
|
48 | ||||
|
49 | #endif |
@@ -0,0 +1,12 | |||||
|
1 | INCLUDEPATH += $$PWD | |||
|
2 | DEPENDPATH += $$PWD | |||
|
3 | ||||
|
4 | SOURCES += \ | |||
|
5 | $$PWD/xychartitem.cpp \ | |||
|
6 | $$PWD/qxyseries.cpp | |||
|
7 | ||||
|
8 | PRIVATE_HEADERS += \ | |||
|
9 | $$PWD/xychartitem_p.h \ | |||
|
10 | ||||
|
11 | PUBLIC_HEADERS += \ | |||
|
12 | $$PWD/qxyseries.h No newline at end of file |
@@ -0,0 +1,143 | |||||
|
1 | #include "xychartitem_p.h" | |||
|
2 | #include "qxyseries.h" | |||
|
3 | #include "chartpresenter_p.h" | |||
|
4 | #include <QPainter> | |||
|
5 | ||||
|
6 | ||||
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |||
|
8 | ||||
|
9 | //TODO: optimize : remove points which are not visible | |||
|
10 | ||||
|
11 | XYChartItem::XYChartItem(QXYSeries* series,QGraphicsItem *parent):ChartItem(parent), | |||
|
12 | m_minX(0), | |||
|
13 | m_maxX(0), | |||
|
14 | m_minY(0), | |||
|
15 | m_maxY(0), | |||
|
16 | m_series(series) | |||
|
17 | { | |||
|
18 | QObject::connect(series,SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int))); | |||
|
19 | QObject::connect(series,SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int))); | |||
|
20 | QObject::connect(series,SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int))); | |||
|
21 | ||||
|
22 | } | |||
|
23 | ||||
|
24 | QPointF XYChartItem::calculateGeometryPoint(const QPointF& point) const | |||
|
25 | { | |||
|
26 | const qreal deltaX = m_size.width()/(m_maxX-m_minX); | |||
|
27 | const qreal deltaY = m_size.height()/(m_maxY-m_minY); | |||
|
28 | qreal x = (point.x() - m_minX)* deltaX; | |||
|
29 | qreal y = (point.y() - m_minY)*-deltaY + m_size.height(); | |||
|
30 | return QPointF(x,y); | |||
|
31 | } | |||
|
32 | ||||
|
33 | ||||
|
34 | QPointF XYChartItem::calculateGeometryPoint(int index) const | |||
|
35 | { | |||
|
36 | const qreal deltaX = m_size.width()/(m_maxX-m_minX); | |||
|
37 | const qreal deltaY = m_size.height()/(m_maxY-m_minY); | |||
|
38 | qreal x = (m_series->x(index) - m_minX)* deltaX; | |||
|
39 | qreal y = (m_series->y(index) - m_minY)*-deltaY + m_size.height(); | |||
|
40 | return QPointF(x,y); | |||
|
41 | } | |||
|
42 | ||||
|
43 | QVector<QPointF> XYChartItem::calculateGeometryPoints() const | |||
|
44 | { | |||
|
45 | const qreal deltaX = m_size.width()/(m_maxX-m_minX); | |||
|
46 | const qreal deltaY = m_size.height()/(m_maxY-m_minY); | |||
|
47 | ||||
|
48 | QVector<QPointF> points; | |||
|
49 | points.reserve(m_series->count()); | |||
|
50 | for (int i = 0; i < m_series->count(); ++i) { | |||
|
51 | qreal x = (m_series->x(i) - m_minX)* deltaX; | |||
|
52 | qreal y = (m_series->y(i) - m_minY)*-deltaY + m_size.height(); | |||
|
53 | points << QPointF(x,y); | |||
|
54 | } | |||
|
55 | return points; | |||
|
56 | } | |||
|
57 | ||||
|
58 | ||||
|
59 | void XYChartItem::updatePoints(QVector<QPointF>& points) | |||
|
60 | { | |||
|
61 | setGeometry(points); | |||
|
62 | } | |||
|
63 | ||||
|
64 | void XYChartItem::updatePoint(int index,QPointF& newPoint) | |||
|
65 | { | |||
|
66 | m_points.replace(index,newPoint); | |||
|
67 | setGeometry(m_points); | |||
|
68 | } | |||
|
69 | ||||
|
70 | void XYChartItem::setGeometry(QVector<QPointF>& points) | |||
|
71 | { | |||
|
72 | m_points = points; | |||
|
73 | } | |||
|
74 | ||||
|
75 | //handlers | |||
|
76 | ||||
|
77 | void XYChartItem::handlePointAdded(int index) | |||
|
78 | { | |||
|
79 | Q_ASSERT(index<m_series->count()); | |||
|
80 | Q_ASSERT(index>=0); | |||
|
81 | ||||
|
82 | QPointF point = calculateGeometryPoint(index); | |||
|
83 | QVector<QPointF> points = m_points; | |||
|
84 | points.insert(index,point); | |||
|
85 | updatePoints(points); | |||
|
86 | update(); | |||
|
87 | } | |||
|
88 | void XYChartItem::handlePointRemoved(int index) | |||
|
89 | { | |||
|
90 | Q_ASSERT(index<m_series->count()); | |||
|
91 | Q_ASSERT(index>=0); | |||
|
92 | ||||
|
93 | QPointF point = calculateGeometryPoint(index); | |||
|
94 | QVector<QPointF> points = m_points; | |||
|
95 | points.remove(index); | |||
|
96 | updatePoints(points); | |||
|
97 | update(); | |||
|
98 | } | |||
|
99 | ||||
|
100 | void XYChartItem::handlePointReplaced(int index) | |||
|
101 | { | |||
|
102 | Q_ASSERT(index<m_series->count()); | |||
|
103 | Q_ASSERT(index>=0); | |||
|
104 | QPointF point = calculateGeometryPoint(index); | |||
|
105 | updatePoint(index,point); | |||
|
106 | update(); | |||
|
107 | } | |||
|
108 | ||||
|
109 | void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) | |||
|
110 | { | |||
|
111 | m_minX=minX; | |||
|
112 | m_maxX=maxX; | |||
|
113 | m_minY=minY; | |||
|
114 | m_maxY=maxY; | |||
|
115 | ||||
|
116 | if(isEmpty()) return; | |||
|
117 | QVector<QPointF> points = calculateGeometryPoints(); | |||
|
118 | updatePoints(points); | |||
|
119 | update(); | |||
|
120 | } | |||
|
121 | ||||
|
122 | void XYChartItem::handleGeometryChanged(const QRectF& rect) | |||
|
123 | { | |||
|
124 | Q_ASSERT(rect.isValid()); | |||
|
125 | m_size=rect.size(); | |||
|
126 | m_clipRect=rect.translated(-rect.topLeft()); | |||
|
127 | setPos(rect.topLeft()); | |||
|
128 | ||||
|
129 | if(isEmpty()) return; | |||
|
130 | QVector<QPointF> points = calculateGeometryPoints(); | |||
|
131 | updatePoints(points); | |||
|
132 | update(); | |||
|
133 | } | |||
|
134 | ||||
|
135 | ||||
|
136 | bool XYChartItem::isEmpty() | |||
|
137 | { | |||
|
138 | return !m_clipRect.isValid() || m_maxX - m_minX == 0 || m_maxY - m_minY ==0 ; | |||
|
139 | } | |||
|
140 | ||||
|
141 | #include "moc_xychartitem_p.cpp" | |||
|
142 | ||||
|
143 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -0,0 +1,55 | |||||
|
1 | #ifndef XYCHARTITEM_H | |||
|
2 | #define XYCHARTITEM_H | |||
|
3 | ||||
|
4 | #include "qchartglobal.h" | |||
|
5 | #include "chartitem_p.h" | |||
|
6 | #include <QPen> | |||
|
7 | ||||
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |||
|
9 | ||||
|
10 | class ChartPresenter; | |||
|
11 | class QXYSeries; | |||
|
12 | ||||
|
13 | class XYChartItem : public QObject , public ChartItem | |||
|
14 | { | |||
|
15 | Q_OBJECT | |||
|
16 | public: | |||
|
17 | XYChartItem(QXYSeries* series,QGraphicsItem *parent = 0); | |||
|
18 | ~ XYChartItem(){}; | |||
|
19 | ||||
|
20 | QVector<QPointF> points() const {return m_points;} | |||
|
21 | QRectF clipRect() const { return m_clipRect;} | |||
|
22 | ||||
|
23 | public slots: | |||
|
24 | void handlePointAdded(int index); | |||
|
25 | void handlePointRemoved(int index); | |||
|
26 | void handlePointReplaced(int index); | |||
|
27 | void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY); | |||
|
28 | void handleGeometryChanged(const QRectF& size); | |||
|
29 | ||||
|
30 | protected: | |||
|
31 | virtual void updatePoints(QVector<QPointF>& points); | |||
|
32 | virtual void updatePoint(int index,QPointF& newPoint); | |||
|
33 | virtual void setGeometry(QVector<QPointF>& points); | |||
|
34 | QPointF calculateGeometryPoint(const QPointF& point) const; | |||
|
35 | QPointF calculateGeometryPoint(int index) const; | |||
|
36 | QVector<QPointF> calculateGeometryPoints() const; | |||
|
37 | ||||
|
38 | private: | |||
|
39 | inline bool isEmpty(); | |||
|
40 | ||||
|
41 | private: | |||
|
42 | qreal m_minX; | |||
|
43 | qreal m_maxX; | |||
|
44 | qreal m_minY; | |||
|
45 | qreal m_maxY; | |||
|
46 | QXYSeries* m_series; | |||
|
47 | QSizeF m_size; | |||
|
48 | QRectF m_clipRect; | |||
|
49 | QVector<QPointF> m_points; | |||
|
50 | ||||
|
51 | }; | |||
|
52 | ||||
|
53 | QTCOMMERCIALCHART_END_NAMESPACE | |||
|
54 | ||||
|
55 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now