@@ -1,284 +1,286 | |||
|
1 | 1 | #include "qchart.h" |
|
2 | 2 | #include "qchartaxis.h" |
|
3 | 3 | #include "chartpresenter_p.h" |
|
4 | 4 | #include "chartdataset_p.h" |
|
5 | 5 | #include "charttheme_p.h" |
|
6 | 6 | //series |
|
7 | 7 | #include "qbarseries.h" |
|
8 | 8 | #include "qstackedbarseries.h" |
|
9 | 9 | #include "qpercentbarseries.h" |
|
10 | 10 | #include "qlineseries.h" |
|
11 | 11 | #include "qpieseries.h" |
|
12 | 12 | #include "qscatterseries.h" |
|
13 | 13 | //items |
|
14 | 14 | #include "axisitem_p.h" |
|
15 | 15 | #include "axisanimationitem_p.h" |
|
16 | 16 | #include "barpresenter_p.h" |
|
17 | 17 | #include "stackedbarpresenter_p.h" |
|
18 | 18 | #include "percentbarpresenter_p.h" |
|
19 | 19 | #include "linechartitem_p.h" |
|
20 | 20 | #include "linechartanimationitem_p.h" |
|
21 | 21 | #include "piepresenter_p.h" |
|
22 | 22 | #include "scatterpresenter_p.h" |
|
23 | 23 | |
|
24 | 24 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
25 | 25 | |
|
26 | 26 | ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), |
|
27 | 27 | m_chart(chart), |
|
28 | 28 | m_dataset(dataset), |
|
29 | 29 | m_chartTheme(0), |
|
30 | 30 | m_marginSize(0), |
|
31 | 31 | m_rect(QRectF(QPoint(0,0),m_chart->size())), |
|
32 | 32 | m_options(0) |
|
33 | 33 | { |
|
34 | 34 | createConnections(); |
|
35 | 35 | setChartTheme(QChart::ChartThemeDefault); |
|
36 | 36 | |
|
37 | 37 | } |
|
38 | 38 | |
|
39 | 39 | ChartPresenter::~ChartPresenter() |
|
40 | 40 | { |
|
41 | 41 | } |
|
42 | 42 | |
|
43 | 43 | void ChartPresenter::createConnections() |
|
44 | 44 | { |
|
45 | 45 | QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged())); |
|
46 | 46 | QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*)),this,SLOT(handleSeriesAdded(QSeries*))); |
|
47 | 47 | QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),this,SLOT(handleSeriesRemoved(QSeries*))); |
|
48 | 48 | QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*)),this,SLOT(handleAxisAdded(QChartAxis*))); |
|
49 | 49 | QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*))); |
|
50 | 50 | QObject::connect(m_dataset,SIGNAL(seriesDomainChanged(QSeries*,const Domain&)),this,SLOT(handleSeriesDomainChanged(QSeries*,const Domain&))); |
|
51 | 51 | QObject::connect(m_dataset,SIGNAL(axisLabelsChanged(QChartAxis*,const QStringList&)),this,SLOT(handleAxisLabelsChanged(QChartAxis*,const QStringList&))); |
|
52 | 52 | } |
|
53 | 53 | |
|
54 | 54 | |
|
55 | 55 | QRectF ChartPresenter::geometry() const |
|
56 | 56 | { |
|
57 | 57 | return m_rect; |
|
58 | 58 | } |
|
59 | 59 | |
|
60 | 60 | void ChartPresenter::handleGeometryChanged() |
|
61 | 61 | { |
|
62 | 62 | m_rect = QRectF(QPoint(0,0),m_chart->size()); |
|
63 | 63 | m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize); |
|
64 | 64 | Q_ASSERT(m_rect.isValid()); |
|
65 | 65 | emit geometryChanged(m_rect); |
|
66 | 66 | } |
|
67 | 67 | |
|
68 | 68 | int ChartPresenter::margin() const |
|
69 | 69 | { |
|
70 | 70 | return m_marginSize; |
|
71 | 71 | } |
|
72 | 72 | |
|
73 | 73 | void ChartPresenter::setMargin(int margin) |
|
74 | 74 | { |
|
75 | 75 | m_marginSize = margin; |
|
76 | 76 | } |
|
77 | 77 | |
|
78 | 78 | void ChartPresenter::handleAxisAdded(QChartAxis* axis) |
|
79 | 79 | { |
|
80 | 80 | |
|
81 | 81 | AxisItem* item ; |
|
82 | 82 | |
|
83 | 83 | if(!m_options.testFlag(QChart::GridAxisAnimations)) |
|
84 | 84 | { |
|
85 | 85 | item = new AxisItem(axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart); |
|
86 | 86 | }else{ |
|
87 | 87 | item = new AxisAnimationItem(axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart); |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
91 | 91 | QObject::connect(axis,SIGNAL(update(QChartAxis*)),item,SLOT(handleAxisUpdate(QChartAxis*))); |
|
92 | 92 | |
|
93 | 93 | item->handleAxisUpdate(axis); |
|
94 | 94 | item->handleGeometryChanged(m_rect); |
|
95 | 95 | m_chartTheme->decorate(axis,item); |
|
96 | 96 | m_axisItems.insert(axis,item); |
|
97 | 97 | } |
|
98 | 98 | |
|
99 | 99 | void ChartPresenter::handleAxisRemoved(QChartAxis* axis) |
|
100 | 100 | { |
|
101 | 101 | AxisItem* item = m_axisItems.take(axis); |
|
102 | 102 | Q_ASSERT(item); |
|
103 | 103 | delete item; |
|
104 | 104 | } |
|
105 | 105 | |
|
106 | 106 | |
|
107 | 107 | void ChartPresenter::handleSeriesAdded(QSeries* series) |
|
108 | 108 | { |
|
109 | 109 | switch(series->type()) |
|
110 | 110 | { |
|
111 | 111 | case QSeries::SeriesTypeLine: { |
|
112 | 112 | QLineSeries* lineSeries = static_cast<QLineSeries*>(series); |
|
113 | 113 | LineChartItem* item; |
|
114 | 114 | if(m_options.testFlag(QChart::SeriesAnimations)){ |
|
115 | 115 | item = new LineChartAnimationItem(this,lineSeries,m_chart); |
|
116 | 116 | }else{ |
|
117 | 117 | item = new LineChartItem(this,lineSeries,m_chart); |
|
118 | 118 | } |
|
119 | 119 | m_chartTheme->decorate(item,lineSeries,m_chartItems.count()); |
|
120 | 120 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
121 | 121 | QObject::connect(lineSeries,SIGNAL(pointReplaced(int)),item,SLOT(handlePointReplaced(int))); |
|
122 | 122 | QObject::connect(lineSeries,SIGNAL(pointAdded(int)),item,SLOT(handlePointAdded(int))); |
|
123 | 123 | QObject::connect(lineSeries,SIGNAL(pointRemoved(int)),item,SLOT(handlePointRemoved(int))); |
|
124 | QObject::connect(lineSeries,SIGNAL(updated()),item,SLOT(handleUpdated())); | |
|
124 | 125 | m_chartItems.insert(series,item); |
|
125 | 126 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); |
|
127 | item->handleUpdated(); | |
|
126 | 128 | break; |
|
127 | 129 | } |
|
128 | 130 | |
|
129 | 131 | case QSeries::SeriesTypeBar: { |
|
130 | 132 | QBarSeries* barSeries = static_cast<QBarSeries*>(series); |
|
131 | 133 | BarPresenter* item = new BarPresenter(barSeries,m_chart); |
|
132 | 134 | m_chartTheme->decorate(item,barSeries,m_chartItems.count()); |
|
133 | 135 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
134 | 136 | QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
135 | 137 | m_chartItems.insert(series,item); |
|
136 | 138 | // m_axisXItem->setVisible(false); |
|
137 | 139 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); |
|
138 | 140 | break; |
|
139 | 141 | } |
|
140 | 142 | |
|
141 | 143 | case QSeries::SeriesTypeStackedBar: { |
|
142 | 144 | |
|
143 | 145 | QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); |
|
144 | 146 | StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries,m_chart); |
|
145 | 147 | m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count()); |
|
146 | 148 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
147 | 149 | QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
148 | 150 | m_chartItems.insert(series,item); |
|
149 | 151 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); |
|
150 | 152 | break; |
|
151 | 153 | } |
|
152 | 154 | |
|
153 | 155 | case QSeries::SeriesTypePercentBar: { |
|
154 | 156 | |
|
155 | 157 | QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); |
|
156 | 158 | PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries,m_chart); |
|
157 | 159 | m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count()); |
|
158 | 160 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
159 | 161 | QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
160 | 162 | m_chartItems.insert(series,item); |
|
161 | 163 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); |
|
162 | 164 | break; |
|
163 | 165 | } |
|
164 | 166 | case QSeries::SeriesTypeScatter: { |
|
165 | 167 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); |
|
166 | 168 | ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart); |
|
167 | 169 | QObject::connect(scatterPresenter, SIGNAL(clicked()), scatterSeries, SIGNAL(clicked())); |
|
168 | 170 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), |
|
169 | 171 | scatterPresenter, SLOT(handleGeometryChanged(const QRectF&))); |
|
170 | 172 | m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count()); |
|
171 | 173 | m_chartItems.insert(scatterSeries, scatterPresenter); |
|
172 | 174 | if(m_rect.isValid()) scatterPresenter->handleGeometryChanged(m_rect); |
|
173 | 175 | break; |
|
174 | 176 | } |
|
175 | 177 | case QSeries::SeriesTypePie: { |
|
176 | 178 | QPieSeries *s = qobject_cast<QPieSeries *>(series); |
|
177 | 179 | PiePresenter* pie = new PiePresenter(m_chart, s); |
|
178 | 180 | m_chartTheme->decorate(pie, s, m_chartItems.count()); |
|
179 | 181 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&))); |
|
180 | 182 | |
|
181 | 183 | // Hide all from background when there is only piechart |
|
182 | 184 | // TODO: refactor this ugly code... should be one setting for this |
|
183 | 185 | if (m_chartItems.count() == 0) { |
|
184 | 186 | m_chart->axisX()->setAxisVisible(false); |
|
185 | 187 | m_chart->axisY()->setAxisVisible(false); |
|
186 | 188 | m_chart->axisX()->setGridVisible(false); |
|
187 | 189 | m_chart->axisY()->setGridVisible(false); |
|
188 | 190 | m_chart->axisX()->setLabelsVisible(false); |
|
189 | 191 | m_chart->axisY()->setLabelsVisible(false); |
|
190 | 192 | m_chart->axisX()->setShadesVisible(false); |
|
191 | 193 | m_chart->axisY()->setShadesVisible(false); |
|
192 | 194 | m_chart->setChartBackgroundBrush(Qt::transparent); |
|
193 | 195 | } |
|
194 | 196 | |
|
195 | 197 | m_chartItems.insert(series, pie); |
|
196 | 198 | pie->handleGeometryChanged(m_rect); |
|
197 | 199 | break; |
|
198 | 200 | } |
|
199 | 201 | default: { |
|
200 | 202 | qDebug()<< "Series type" << series->type() << "not implemented."; |
|
201 | 203 | break; |
|
202 | 204 | } |
|
203 | 205 | } |
|
204 | 206 | } |
|
205 | 207 | |
|
206 | 208 | void ChartPresenter::handleSeriesRemoved(QSeries* series) |
|
207 | 209 | { |
|
208 | 210 | ChartItem* item = m_chartItems.take(series); |
|
209 | 211 | delete item; |
|
210 | 212 | } |
|
211 | 213 | |
|
212 | 214 | void ChartPresenter::handleSeriesDomainChanged(QSeries* series, const Domain& domain) |
|
213 | 215 | { |
|
214 | 216 | m_chartItems.value(series)->handleDomainChanged(domain); |
|
215 | 217 | } |
|
216 | 218 | |
|
217 | 219 | void ChartPresenter::handleAxisLabelsChanged(QChartAxis* axis,const QStringList& labels) |
|
218 | 220 | { |
|
219 | 221 | m_axisItems.value(axis)->handleLabelsChanged(axis,labels); |
|
220 | 222 | } |
|
221 | 223 | |
|
222 | 224 | void ChartPresenter::setChartTheme(QChart::ChartTheme theme) |
|
223 | 225 | { |
|
224 | 226 | delete m_chartTheme; |
|
225 | 227 | |
|
226 | 228 | m_chartTheme = ChartTheme::createTheme(theme); |
|
227 | 229 | |
|
228 | 230 | m_chartTheme->decorate(m_chart); |
|
229 | 231 | QMapIterator<QSeries*,ChartItem*> i(m_chartItems); |
|
230 | 232 | |
|
231 | 233 | int index=0; |
|
232 | 234 | while (i.hasNext()) { |
|
233 | 235 | i.next(); |
|
234 | 236 | m_chartTheme->decorate(i.value(),i.key(),index); |
|
235 | 237 | index++; |
|
236 | 238 | } |
|
237 | 239 | |
|
238 | 240 | QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems); |
|
239 | 241 | while (j.hasNext()) { |
|
240 | 242 | j.next(); |
|
241 | 243 | m_chartTheme->decorate(j.key(),j.value()); |
|
242 | 244 | } |
|
243 | 245 | } |
|
244 | 246 | |
|
245 | 247 | QChart::ChartTheme ChartPresenter::chartTheme() |
|
246 | 248 | { |
|
247 | 249 | return m_chartTheme->id(); |
|
248 | 250 | } |
|
249 | 251 | |
|
250 | 252 | void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options) |
|
251 | 253 | { |
|
252 | 254 | if(m_options!=options) { |
|
253 | 255 | |
|
254 | 256 | m_options=options; |
|
255 | 257 | |
|
256 | 258 | //recreate elements |
|
257 | 259 | |
|
258 | 260 | QList<QChartAxis*> axisList = m_axisItems.uniqueKeys(); |
|
259 | 261 | QList<QSeries*> seriesList = m_chartItems.uniqueKeys(); |
|
260 | 262 | |
|
261 | 263 | foreach(QChartAxis* axis, axisList) { |
|
262 | 264 | handleAxisRemoved(axis); |
|
263 | 265 | handleAxisAdded(axis); |
|
264 | 266 | } |
|
265 | 267 | foreach(QSeries* series, seriesList) { |
|
266 | 268 | handleSeriesRemoved(series); |
|
267 | 269 | handleSeriesAdded(series); |
|
268 | 270 | } |
|
269 | 271 | |
|
270 | 272 | //now reintialize view data |
|
271 | 273 | //TODO: make it more nice |
|
272 | 274 | m_dataset->setDomain(m_dataset->domainIndex()); |
|
273 | 275 | } |
|
274 | 276 | } |
|
275 | 277 | |
|
276 | 278 | QChart::AnimationOptions ChartPresenter::animationOptions() const |
|
277 | 279 | { |
|
278 | 280 | return m_options; |
|
279 | 281 | } |
|
280 | 282 | |
|
281 | 283 | |
|
282 | 284 | #include "moc_chartpresenter_p.cpp" |
|
283 | 285 | |
|
284 | 286 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,221 +1,219 | |||
|
1 | 1 | #include "linechartitem_p.h" |
|
2 | 2 | #include "qlineseries.h" |
|
3 | 3 | #include "chartpresenter_p.h" |
|
4 | 4 | #include <QPainter> |
|
5 | 5 | |
|
6 | 6 | |
|
7 | 7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | 8 | |
|
9 | 9 | //TODO: optimize : remove points which are not visible |
|
10 | 10 | |
|
11 | 11 | LineChartItem::LineChartItem(ChartPresenter* presenter, QLineSeries* series,QGraphicsItem *parent):ChartItem(parent), |
|
12 | 12 | m_presenter(presenter), |
|
13 | 13 | m_series(series), |
|
14 | 14 | m_items(this) |
|
15 | 15 | { |
|
16 | 16 | //m_items.setZValue(ChartPresenter::LineChartZValue); |
|
17 | 17 | setZValue(ChartPresenter::LineChartZValue); |
|
18 | 18 | } |
|
19 | 19 | |
|
20 | 20 | QRectF LineChartItem::boundingRect() const |
|
21 | 21 | { |
|
22 | 22 | return m_rect; |
|
23 | 23 | } |
|
24 | 24 | |
|
25 | 25 | QPainterPath LineChartItem::shape() const |
|
26 | 26 | { |
|
27 | 27 | return m_path; |
|
28 | 28 | } |
|
29 | 29 | |
|
30 | 30 | void LineChartItem::createPoints(int count) |
|
31 | 31 | { |
|
32 | 32 | for (int i = 0; i < count; ++i) { |
|
33 | 33 | QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3); |
|
34 | 34 | m_items.addToGroup(item); |
|
35 | 35 | } |
|
36 | 36 | } |
|
37 | 37 | |
|
38 | 38 | void LineChartItem::clearPoints(int count) |
|
39 | 39 | { |
|
40 | 40 | QList<QGraphicsItem *> items = m_items.childItems(); |
|
41 | 41 | |
|
42 | 42 | for (int i = 0; i < count; ++i) { |
|
43 | 43 | delete(items.takeLast()); |
|
44 | 44 | } |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | 47 | QPointF LineChartItem::calculateGeometryPoint(int index) const |
|
48 | 48 | { |
|
49 | 49 | const qreal deltaX = m_size.width()/m_domain.spanX(); |
|
50 | 50 | const qreal deltaY = m_size.height()/m_domain.spanY(); |
|
51 | 51 | qreal x = (m_series->x(index) - m_domain.m_minX)* deltaX; |
|
52 | 52 | qreal y = (m_series->y(index) - m_domain.m_minY)*-deltaY + m_size.height(); |
|
53 | 53 | return QPointF(x,y); |
|
54 | 54 | } |
|
55 | 55 | |
|
56 | 56 | QVector<QPointF> LineChartItem::calculateGeometryPoints() const |
|
57 | 57 | { |
|
58 | 58 | const qreal deltaX = m_size.width()/m_domain.spanX(); |
|
59 | 59 | const qreal deltaY = m_size.height()/m_domain.spanY(); |
|
60 | 60 | |
|
61 | 61 | QVector<QPointF> points; |
|
62 | 62 | points.reserve(m_series->count()); |
|
63 | 63 | for (int i = 0; i < m_series->count(); ++i) { |
|
64 | 64 | qreal x = (m_series->x(i) - m_domain.m_minX)* deltaX; |
|
65 | 65 | qreal y = (m_series->y(i) - m_domain.m_minY)*-deltaY + m_size.height(); |
|
66 | 66 | points << QPointF(x,y); |
|
67 | 67 | } |
|
68 | 68 | return points; |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | void LineChartItem::updateItem(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints) |
|
72 | 72 | { |
|
73 | 73 | applyGeometry(newPoints); |
|
74 | 74 | oldPoints = newPoints; |
|
75 | 75 | } |
|
76 | 76 | |
|
77 | 77 | void LineChartItem::updateItem(QVector<QPointF>& oldPoints,int index,QPointF& newPoint) |
|
78 | 78 | { |
|
79 | 79 | oldPoints.replace(index,newPoint); |
|
80 | 80 | applyGeometry(oldPoints); |
|
81 | 81 | } |
|
82 | 82 | |
|
83 | 83 | void LineChartItem::applyGeometry(QVector<QPointF>& points) |
|
84 | 84 | { |
|
85 | 85 | if(points.size()==0) return; |
|
86 | 86 | |
|
87 | 87 | QList<QGraphicsItem*> items = m_items.childItems(); |
|
88 | 88 | |
|
89 | 89 | QPainterPath path; |
|
90 | 90 | const QPointF& point = points.at(0); |
|
91 | 91 | path.moveTo(point); |
|
92 | 92 | QGraphicsItem* item = items.at(0); |
|
93 | 93 | item->setPos(point.x()-1,point.y()-1); |
|
94 | 94 | if(!m_clipRect.contains(point)) item->setVisible(false); |
|
95 | 95 | |
|
96 | 96 | for(int i=1 ; i< points.size();i++) { |
|
97 | 97 | QGraphicsItem* item = items.at(i); |
|
98 | 98 | const QPointF& point = points.at(i); |
|
99 | 99 | item->setPos(point.x()-1,point.y()-1); |
|
100 | 100 | if(!m_clipRect.contains(point)) item->setVisible(false); |
|
101 | 101 | path.lineTo(point); |
|
102 | 102 | } |
|
103 | 103 | |
|
104 | 104 | prepareGeometryChange(); |
|
105 | 105 | m_path = path; |
|
106 | 106 | m_rect = path.boundingRect(); |
|
107 | 107 | } |
|
108 | 108 | |
|
109 | 109 | void LineChartItem::setPen(const QPen& pen) |
|
110 | 110 | { |
|
111 | 111 | m_pen = pen; |
|
112 | 112 | } |
|
113 | 113 | |
|
114 | 114 | //handlers |
|
115 | 115 | |
|
116 | 116 | void LineChartItem::handlePointAdded(int index) |
|
117 | 117 | { |
|
118 | 118 | Q_ASSERT(index<m_series->count()); |
|
119 | 119 | Q_ASSERT(index>=0); |
|
120 | 120 | |
|
121 | 121 | QPointF point = calculateGeometryPoint(index); |
|
122 | 122 | createPoints(1); |
|
123 | 123 | QVector<QPointF> points = m_points; |
|
124 | 124 | points.insert(index,point); |
|
125 | 125 | updateItem(m_points,points); |
|
126 | 126 | update(); |
|
127 | 127 | } |
|
128 | 128 | void LineChartItem::handlePointRemoved(int index) |
|
129 | 129 | { |
|
130 | 130 | Q_ASSERT(index<m_series->count()); |
|
131 | 131 | Q_ASSERT(index>=0); |
|
132 | 132 | QPointF point = calculateGeometryPoint(index); |
|
133 | 133 | clearPoints(1); |
|
134 | 134 | QVector<QPointF> points = m_points; |
|
135 | 135 | points.remove(index); |
|
136 | 136 | updateItem(m_points,points); |
|
137 | 137 | update(); |
|
138 | 138 | } |
|
139 | 139 | |
|
140 | 140 | void LineChartItem::handlePointReplaced(int index) |
|
141 | 141 | { |
|
142 | 142 | Q_ASSERT(index<m_series->count()); |
|
143 | 143 | Q_ASSERT(index>=0); |
|
144 | 144 | QPointF point = calculateGeometryPoint(index); |
|
145 | 145 | updateItem(m_points,index,point); |
|
146 | 146 | update(); |
|
147 | 147 | } |
|
148 | 148 | |
|
149 | 149 | void LineChartItem::handleDomainChanged(const Domain& domain) |
|
150 | 150 | { |
|
151 | 151 | |
|
152 | 152 | m_domain = domain; |
|
153 | 153 | |
|
154 | 154 | if(m_domain.isEmpty()) return; |
|
155 | 155 | if(!m_clipRect.isValid()) return; |
|
156 | 156 | |
|
157 | 157 | QVector<QPointF> points = calculateGeometryPoints(); |
|
158 | 158 | |
|
159 | 159 | int diff = m_points.size() - points.size(); |
|
160 | 160 | |
|
161 | 161 | if(diff>0) { |
|
162 | 162 | clearPoints(diff); |
|
163 | 163 | } |
|
164 | 164 | else if(diff<0) { |
|
165 | 165 | createPoints(-diff); |
|
166 | 166 | } |
|
167 | 167 | |
|
168 | 168 | updateItem(m_points,points); |
|
169 | 169 | update(); |
|
170 | 170 | } |
|
171 | 171 | |
|
172 | 172 | void LineChartItem::handleGeometryChanged(const QRectF& rect) |
|
173 | 173 | { |
|
174 | 174 | Q_ASSERT(rect.isValid()); |
|
175 | 175 | m_size=rect.size(); |
|
176 | 176 | m_clipRect=rect.translated(-rect.topLeft()); |
|
177 | 177 | setPos(rect.topLeft()); |
|
178 | 178 | |
|
179 | 179 | if(m_domain.isEmpty()) return; |
|
180 | 180 | |
|
181 | 181 | QVector<QPointF> points = calculateGeometryPoints(); |
|
182 | 182 | |
|
183 | 183 | int diff = m_points.size() - points.size(); |
|
184 | 184 | |
|
185 | 185 | if(diff>0) { |
|
186 | 186 | clearPoints(diff); |
|
187 | 187 | } |
|
188 | 188 | else if(diff<0) { |
|
189 | 189 | createPoints(-diff); |
|
190 | 190 | } |
|
191 | 191 | |
|
192 | 192 | updateItem(m_points,points); |
|
193 | 193 | update(); |
|
194 | 194 | } |
|
195 | 195 | |
|
196 |
void LineChartItem::handle |
|
|
196 | void LineChartItem::handleUpdated() | |
|
197 | 197 | { |
|
198 | if(m_points.count()==0) return; | |
|
199 | ||
|
200 | 198 | m_items.setVisible(m_series->pointsVisible()); |
|
201 | 199 | setPen(m_series->pen()); |
|
202 | 200 | update(); |
|
203 | 201 | } |
|
204 | 202 | |
|
205 | 203 | //painter |
|
206 | 204 | |
|
207 | 205 | void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
208 | 206 | { |
|
209 | 207 | Q_UNUSED(widget); |
|
210 | 208 | Q_UNUSED(option); |
|
211 | 209 | painter->save(); |
|
212 | 210 | painter->setPen(m_pen); |
|
213 | 211 | painter->setClipRect(m_clipRect); |
|
214 | 212 | painter->drawPath(m_path); |
|
215 | 213 | painter->restore(); |
|
216 | 214 | } |
|
217 | 215 | |
|
218 | 216 | |
|
219 | 217 | #include "moc_linechartitem_p.cpp" |
|
220 | 218 | |
|
221 | 219 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,61 +1,61 | |||
|
1 | 1 | #ifndef LINECHARTITEM_H |
|
2 | 2 | #define LINECHARTITEM_H |
|
3 | 3 | |
|
4 | 4 | #include "qchartglobal.h" |
|
5 | 5 | #include "chartitem_p.h" |
|
6 | 6 | #include <QPen> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | class ChartPresenter; |
|
11 | 11 | class QLineSeries; |
|
12 | 12 | |
|
13 | 13 | class LineChartItem : public QObject , public ChartItem |
|
14 | 14 | { |
|
15 | 15 | Q_OBJECT |
|
16 | 16 | public: |
|
17 | 17 | LineChartItem(ChartPresenter* presenter, QLineSeries* series,QGraphicsItem *parent = 0); |
|
18 | 18 | ~ LineChartItem(){}; |
|
19 | 19 | |
|
20 | 20 | //from QGraphicsItem |
|
21 | 21 | QRectF boundingRect() const; |
|
22 | 22 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
23 | 23 | QPainterPath shape() const; |
|
24 | 24 | |
|
25 | 25 | void setPen(const QPen& pen); |
|
26 | 26 | void setPointsVisible(bool visible); |
|
27 | 27 | |
|
28 | 28 | public slots: |
|
29 | 29 | void handlePointAdded(int index); |
|
30 | 30 | void handlePointRemoved(int index); |
|
31 | 31 | void handlePointReplaced(int index); |
|
32 |
void handle |
|
|
32 | void handleUpdated(); | |
|
33 | 33 | void handleDomainChanged(const Domain& domain); |
|
34 | 34 | void handleGeometryChanged(const QRectF& size); |
|
35 | 35 | |
|
36 | 36 | public: |
|
37 | 37 | virtual void updateItem(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints); |
|
38 | 38 | virtual void updateItem(QVector<QPointF>& oldPoints,int index,QPointF& newPoint); |
|
39 | 39 | void applyGeometry(QVector<QPointF>& points); |
|
40 | 40 | void createPoints(int count); |
|
41 | 41 | void clearPoints(int count); |
|
42 | 42 | QPointF calculateGeometryPoint(int index) const; |
|
43 | 43 | QVector<QPointF> calculateGeometryPoints() const; |
|
44 | 44 | |
|
45 | 45 | private: |
|
46 | 46 | ChartPresenter* m_presenter; |
|
47 | 47 | QPainterPath m_path; |
|
48 | QLineSeries* m_series; | |
|
48 | 49 | QSizeF m_size; |
|
49 | 50 | QRectF m_rect; |
|
50 | 51 | QRectF m_clipRect; |
|
51 | 52 | Domain m_domain; |
|
52 | 53 | QGraphicsItemGroup m_items; |
|
53 | 54 | QVector<QPointF> m_points; |
|
54 | QLineSeries* m_series; | |
|
55 | 55 | QPen m_pen; |
|
56 | 56 | |
|
57 | 57 | }; |
|
58 | 58 | |
|
59 | 59 | QTCOMMERCIALCHART_END_NAMESPACE |
|
60 | 60 | |
|
61 | 61 | #endif |
@@ -1,213 +1,224 | |||
|
1 | 1 | #include "qlineseries.h" |
|
2 | 2 | |
|
3 | 3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
4 | 4 | |
|
5 | 5 | /*! |
|
6 | 6 | \class QLineSeries |
|
7 | 7 | \brief The QLineSeries class is used for making line charts. |
|
8 | 8 | |
|
9 | 9 | \mainclass |
|
10 | 10 | |
|
11 | 11 | A line chart is used to show information as a series of data points |
|
12 | 12 | connected by straight lines. |
|
13 | 13 | |
|
14 | 14 | \image linechart.png |
|
15 | 15 | |
|
16 | 16 | Creating basic line chart is simple: |
|
17 | 17 | \code |
|
18 | 18 | QLineSeries* series = new QLineSeries(); |
|
19 | 19 | series->add(0, 6); |
|
20 | 20 | series->add(2, 4); |
|
21 | 21 | ... |
|
22 | 22 | chartView->addSeries(series); |
|
23 | 23 | \endcode |
|
24 | 24 | */ |
|
25 | 25 | |
|
26 | 26 | /*! |
|
27 | 27 | \fn virtual QSeriesType QLineSeries::type() const |
|
28 | 28 | \brief Returns type of series. |
|
29 | 29 | \sa QSeries, QSeriesType |
|
30 | 30 | */ |
|
31 | 31 | |
|
32 | 32 | /*! |
|
33 | 33 | \fn QPen QLineSeries::pen() const |
|
34 | 34 | \brief Returns the pen used to draw line for this series. |
|
35 | 35 | \sa setPen() |
|
36 | 36 | */ |
|
37 | 37 | |
|
38 | 38 | /*! |
|
39 | 39 | \fn bool QLineSeries::pointsVisible() const |
|
40 | 40 | \brief Returns if the points are drawn for this series. |
|
41 | 41 | \sa setPointsVisible() |
|
42 | 42 | */ |
|
43 | 43 | |
|
44 | 44 | |
|
45 | 45 | /*! |
|
46 | 46 | \fn void QLineSeries::pointReplaced(int index) |
|
47 | 47 | \brief \internal \a index |
|
48 | 48 | */ |
|
49 | 49 | |
|
50 | 50 | /*! |
|
51 | 51 | \fn void QLineSeries::pointAdded(int index) |
|
52 | 52 | \brief \internal \a index |
|
53 | 53 | */ |
|
54 | 54 | |
|
55 | 55 | /*! |
|
56 | 56 | \fn void QLineSeries::pointRemoved(int index) |
|
57 | 57 | \brief \internal \a index |
|
58 | 58 | */ |
|
59 | 59 | |
|
60 | 60 | /*! |
|
61 | \fn void QLineSeries::updated(int index) | |
|
62 | \brief \internal | |
|
63 | */ | |
|
64 | ||
|
65 | /*! | |
|
61 | 66 | Constructs empty series object which is a child of \a parent. |
|
62 | 67 | When series object is added to QChartView or QChart instance ownerships is transfered. |
|
63 | 68 | */ |
|
64 | 69 | QLineSeries::QLineSeries(QObject* parent):QSeries(parent), |
|
65 | 70 | m_pointsVisible(false) |
|
66 | 71 | { |
|
67 | 72 | } |
|
68 | 73 | /*! |
|
69 | 74 | Destroys the object. Series added to QChartView or QChart instances are owned by those, |
|
70 | 75 | and are deleted when mentioned object are destroyed. |
|
71 | 76 | */ |
|
72 | 77 | QLineSeries::~QLineSeries() |
|
73 | 78 | { |
|
74 | 79 | } |
|
75 | 80 | |
|
76 | 81 | /*! |
|
77 | 82 | Adds data point \a x \a y to the series. Points are connected with lines on the chart. |
|
78 | 83 | */ |
|
79 | 84 | void QLineSeries::add(qreal x,qreal y) |
|
80 | 85 | { |
|
81 | 86 | Q_ASSERT(m_x.size() == m_y.size()); |
|
82 | 87 | m_x<<x; |
|
83 | 88 | m_y<<y; |
|
84 | 89 | emit pointAdded(m_x.size()-1); |
|
85 | 90 | } |
|
86 | 91 | |
|
87 | 92 | /*! |
|
88 | 93 | This is an overloaded function. |
|
89 | 94 | Adds data \a point to the series. Points are connected with lines on the chart. |
|
90 | 95 | */ |
|
91 | 96 | void QLineSeries::add(const QPointF& point) |
|
92 | 97 | { |
|
93 | 98 | add(point.x(),point.y()); |
|
94 | 99 | } |
|
95 | 100 | |
|
96 | 101 | /*! |
|
97 | 102 | Modifies \a y value for given \a x a value. |
|
98 | 103 | */ |
|
99 | 104 | void QLineSeries::replace(qreal x,qreal y) |
|
100 | 105 | { |
|
101 | 106 | int index = m_x.indexOf(x); |
|
102 | 107 | m_x[index]=x; |
|
103 | 108 | m_y[index]=y; |
|
104 | 109 | emit pointReplaced(index); |
|
105 | 110 | } |
|
106 | 111 | |
|
107 | 112 | /*! |
|
108 | 113 | This is an overloaded function. |
|
109 | 114 | Replaces current y value of for given \a point x value with \a point y value. |
|
110 | 115 | */ |
|
111 | 116 | void QLineSeries::replace(const QPointF& point) |
|
112 | 117 | { |
|
113 | 118 | replace(point.x(),point.y()); |
|
114 | 119 | } |
|
115 | 120 | |
|
116 | 121 | /*! |
|
117 | 122 | Removes current \a x and y value. |
|
118 | 123 | */ |
|
119 | 124 | void QLineSeries::remove(qreal x) |
|
120 | 125 | { |
|
121 | 126 | int index = m_x.indexOf(x); |
|
122 | 127 | m_x.remove(index); |
|
123 | 128 | m_y.remove(index); |
|
124 | 129 | emit pointRemoved(index); |
|
125 | 130 | } |
|
126 | 131 | |
|
127 | 132 | /*! |
|
128 | 133 | Removes current \a point x value. Note \a point y value is ignored. |
|
129 | 134 | */ |
|
130 | 135 | void QLineSeries::remove(const QPointF& point) |
|
131 | 136 | { |
|
132 | 137 | remove(point.x()); |
|
133 | 138 | } |
|
134 | 139 | |
|
135 | 140 | /*! |
|
136 | 141 | Clears all the data. |
|
137 | 142 | */ |
|
138 | 143 | void QLineSeries::clear() |
|
139 | 144 | { |
|
140 | 145 | m_x.clear(); |
|
141 | 146 | m_y.clear(); |
|
142 | 147 | } |
|
143 | 148 | |
|
144 | 149 | /*! |
|
145 | 150 | \internal \a pos |
|
146 | 151 | */ |
|
147 | 152 | qreal QLineSeries::x(int pos) const |
|
148 | 153 | { |
|
149 | 154 | return m_x.at(pos); |
|
150 | 155 | } |
|
151 | 156 | |
|
152 | 157 | /*! |
|
153 | 158 | \internal \a pos |
|
154 | 159 | */ |
|
155 | 160 | qreal QLineSeries::y(int pos) const |
|
156 | 161 | { |
|
157 | 162 | return m_y.at(pos); |
|
158 | 163 | } |
|
159 | 164 | |
|
160 | 165 | /*! |
|
161 | 166 | Returns number of data points within series. |
|
162 | 167 | */ |
|
163 | 168 | int QLineSeries::count() const |
|
164 | 169 | { |
|
165 | 170 | Q_ASSERT(m_x.size() == m_y.size()); |
|
166 | 171 | |
|
167 | 172 | return m_x.size(); |
|
168 | 173 | |
|
169 | 174 | } |
|
170 | 175 | |
|
171 | 176 | /*! |
|
172 | 177 | Sets \a pen used for drawing given series.. |
|
173 | 178 | */ |
|
174 | 179 | void QLineSeries::setPen(const QPen& pen) |
|
175 | 180 | { |
|
181 | if(pen!=m_pen){ | |
|
176 | 182 | m_pen=pen; |
|
183 | emit updated(); | |
|
184 | } | |
|
177 | 185 | } |
|
178 | 186 | |
|
179 | 187 | /*! |
|
180 | 188 | Sets if data points are \a visible and should be drawn on line. |
|
181 | 189 | */ |
|
182 | 190 | void QLineSeries::setPointsVisible(bool visible) |
|
183 | 191 | { |
|
192 | if(m_pointsVisible!=visible){ | |
|
184 | 193 | m_pointsVisible=visible; |
|
194 | emit updated(); | |
|
195 | } | |
|
185 | 196 | } |
|
186 | 197 | |
|
187 | 198 | QDebug operator<< (QDebug debug, const QLineSeries series) |
|
188 | 199 | { |
|
189 | 200 | Q_ASSERT(series.m_x.size() == series.m_y.size()); |
|
190 | 201 | |
|
191 | 202 | int size = series.m_x.size(); |
|
192 | 203 | |
|
193 | 204 | for (int i=0;i<size;i++) { |
|
194 | 205 | debug.nospace() << "(" << series.m_x.at(i) << ','<< series.m_y.at(i) << ") "; |
|
195 | 206 | } |
|
196 | 207 | return debug.space(); |
|
197 | 208 | } |
|
198 | 209 | |
|
199 | 210 | /*! |
|
200 | 211 | Stream operator for adding a data \a point to the series. |
|
201 | 212 | \sa add() |
|
202 | 213 | */ |
|
203 | 214 | |
|
204 | 215 | QLineSeries& QLineSeries::operator<< (const QPointF &point) |
|
205 | 216 | { |
|
206 | 217 | add(point); |
|
207 | 218 | return *this; |
|
208 | 219 | } |
|
209 | 220 | |
|
210 | 221 | |
|
211 | 222 | #include "moc_qlineseries.cpp" |
|
212 | 223 | |
|
213 | 224 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,56 +1,57 | |||
|
1 | 1 | #ifndef QLINESERIES_H_ |
|
2 | 2 | #define QLINESERIES_H_ |
|
3 | 3 | |
|
4 | 4 | #include "qchartglobal.h" |
|
5 | 5 | #include "qseries.h" |
|
6 | 6 | #include <QDebug> |
|
7 | 7 | #include <QPen> |
|
8 | 8 | #include <QBrush> |
|
9 | 9 | |
|
10 | 10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
11 | 11 | |
|
12 | 12 | class QTCOMMERCIALCHART_EXPORT QLineSeries : public QSeries |
|
13 | 13 | { |
|
14 | 14 | Q_OBJECT |
|
15 | 15 | public: |
|
16 | 16 | QLineSeries(QObject* parent=0); |
|
17 | 17 | virtual ~QLineSeries(); |
|
18 | 18 | |
|
19 | 19 | public: // from QChartSeries |
|
20 | 20 | virtual QSeriesType type() const { return QSeries::SeriesTypeLine;} |
|
21 | 21 | void add(qreal x, qreal y); |
|
22 | 22 | void add(const QPointF& point); |
|
23 | 23 | void replace(qreal x,qreal y); |
|
24 | 24 | void replace(const QPointF& point); |
|
25 | 25 | void remove(qreal x); |
|
26 | 26 | void remove(const QPointF& point); |
|
27 | 27 | void clear(); |
|
28 | 28 | |
|
29 | 29 | void setPen(const QPen& pen); |
|
30 | 30 | QPen pen() const { return m_pen;} |
|
31 | 31 | |
|
32 | 32 | void setPointsVisible(bool visible); |
|
33 | 33 | bool pointsVisible() const {return m_pointsVisible;} |
|
34 | 34 | |
|
35 | 35 | int count() const; |
|
36 | 36 | qreal x(int pos) const; |
|
37 | 37 | qreal y(int pos) const; |
|
38 | 38 | |
|
39 | 39 | QLineSeries& operator << (const QPointF &point); |
|
40 | 40 | friend QDebug operator<< (QDebug d, const QLineSeries series); |
|
41 | 41 | |
|
42 | 42 | signals: |
|
43 | 43 | void pointReplaced(int index); |
|
44 | 44 | void pointRemoved(int index); |
|
45 | 45 | void pointAdded(int index); |
|
46 | void updated(); | |
|
46 | 47 | |
|
47 | 48 | private: |
|
48 | 49 | QVector<qreal> m_x; |
|
49 | 50 | QVector<qreal> m_y; |
|
50 | 51 | QPen m_pen; |
|
51 | 52 | bool m_pointsVisible; |
|
52 | 53 | }; |
|
53 | 54 | |
|
54 | 55 | QTCOMMERCIALCHART_END_NAMESPACE |
|
55 | 56 | |
|
56 | 57 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now