@@ -0,0 +1,100 | |||
|
1 | #ifndef XYCHARTANIMATIONITEM_P_H_ | |
|
2 | #define XYCHARTANIMATIONITEM_P_H_ | |
|
3 | #include "qchartglobal.h" | |
|
4 | #include "xychartanimator_p.h" | |
|
5 | #include <QPointF> | |
|
6 | #include <QTimer> | |
|
7 | #include <QGraphicsItem> | |
|
8 | ||
|
9 | Q_DECLARE_METATYPE(QVector<QPointF>) | |
|
10 | ||
|
11 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
|
12 | ||
|
13 | ||
|
14 | const static int duration = 1000; | |
|
15 | ||
|
16 | template <class T , class U> | |
|
17 | class XYChartAnimationItem : public T { | |
|
18 | ||
|
19 | public: | |
|
20 | XYChartAnimationItem(U *series, QGraphicsItem *parent = 0); | |
|
21 | virtual ~XYChartAnimationItem(); | |
|
22 | ||
|
23 | void animationStarted(); | |
|
24 | ||
|
25 | protected: | |
|
26 | virtual void updatePoints(QVector<QPointF>& newPoints); | |
|
27 | virtual void updatePoint(QVector<QPointF>& newPoints); | |
|
28 | ||
|
29 | private: | |
|
30 | XYChartAnimator<T,U> *m_animation; | |
|
31 | QVector<QPointF> m_points; | |
|
32 | bool m_dirty; | |
|
33 | }; | |
|
34 | ||
|
35 | template <class T, class U> | |
|
36 | XYChartAnimationItem<T,U>::XYChartAnimationItem(U *series,QGraphicsItem *parent): | |
|
37 | T(series,parent), | |
|
38 | m_animation(new XYChartAnimator<T,U>(this,this)), | |
|
39 | m_dirty(false) | |
|
40 | { | |
|
41 | } | |
|
42 | ||
|
43 | template <class T, class U> | |
|
44 | XYChartAnimationItem<T,U>::~XYChartAnimationItem() | |
|
45 | { | |
|
46 | } | |
|
47 | ||
|
48 | template <class T, class U> | |
|
49 | void XYChartAnimationItem<T,U>::updatePoints(QVector<QPointF>& newPoints) | |
|
50 | { | |
|
51 | QVector<QPointF> oldPoints = T::points(); | |
|
52 | ||
|
53 | if(newPoints.count()==0) return; | |
|
54 | oldPoints.resize(newPoints.size()); | |
|
55 | ||
|
56 | if(m_animation->state()!=QAbstractAnimation::Stopped){ | |
|
57 | m_animation->stop(); | |
|
58 | } | |
|
59 | ||
|
60 | m_animation->setDuration(duration); | |
|
61 | m_animation->setEasingCurve(QEasingCurve::InOutBack); | |
|
62 | m_animation->setKeyValueAt(0.0, qVariantFromValue(oldPoints)); | |
|
63 | m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints)); | |
|
64 | QTimer::singleShot(0,m_animation,SLOT(start())); | |
|
65 | ||
|
66 | m_points = newPoints; | |
|
67 | m_dirty=false; | |
|
68 | } | |
|
69 | ||
|
70 | template <class T, class U> | |
|
71 | void XYChartAnimationItem<T,U>::updatePoint(QVector<QPointF>& newPoints) | |
|
72 | { | |
|
73 | ||
|
74 | if(m_animation->state()!=QAbstractAnimation::Stopped) { | |
|
75 | m_animation->stop(); | |
|
76 | m_dirty=true; | |
|
77 | } | |
|
78 | ||
|
79 | if(m_dirty) { | |
|
80 | m_points=newPoints; | |
|
81 | m_dirty=false; | |
|
82 | } | |
|
83 | ||
|
84 | m_animation->setDuration(duration); | |
|
85 | m_animation->setEasingCurve(QEasingCurve::InOutBack); | |
|
86 | m_animation->setKeyValueAt(0.0, qVariantFromValue(m_points)); | |
|
87 | m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints)); | |
|
88 | ||
|
89 | QTimer::singleShot(0,m_animation,SLOT(start())); | |
|
90 | } | |
|
91 | ||
|
92 | template <class T, class U> | |
|
93 | void XYChartAnimationItem<T,U>::animationStarted() | |
|
94 | { | |
|
95 | m_dirty=true; | |
|
96 | } | |
|
97 | ||
|
98 | QTCOMMERCIALCHART_END_NAMESPACE | |
|
99 | ||
|
100 | #endif |
@@ -0,0 +1,96 | |||
|
1 | #ifndef XYCHARTANIMATOR_P_H_ | |
|
2 | #define XYCHARTANIMATOR_P_H_ | |
|
3 | #include "qchartglobal.h" | |
|
4 | #include <QVariantAnimation> | |
|
5 | #include <QPointF> | |
|
6 | ||
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
|
8 | ||
|
9 | template <class T, class U> | |
|
10 | class XYChartAnimationItem; | |
|
11 | ||
|
12 | ||
|
13 | template <class T, class U> | |
|
14 | class XYChartAnimator : public QVariantAnimation | |
|
15 | { | |
|
16 | public: | |
|
17 | enum Animation { LineDrawAnimation, MoveDownAnimation, MoveUpAnimation }; | |
|
18 | XYChartAnimator(XYChartAnimationItem<T,U> *item, QObject *parent = 0 ); | |
|
19 | ~XYChartAnimator(); | |
|
20 | ||
|
21 | protected: | |
|
22 | QVariant interpolated(const QVariant &start, const QVariant & end, qreal progress ) const; | |
|
23 | void updateCurrentValue (const QVariant & value ); | |
|
24 | void updateState ( QAbstractAnimation::State newState, QAbstractAnimation::State oldState); | |
|
25 | ||
|
26 | private: | |
|
27 | XYChartAnimationItem<T,U> *m_item; | |
|
28 | Animation m_type; | |
|
29 | }; | |
|
30 | ||
|
31 | template <class T, class U> | |
|
32 | XYChartAnimator<T,U>::XYChartAnimator(XYChartAnimationItem<T,U> *item , QObject *parent):QVariantAnimation(parent), | |
|
33 | m_item(item), | |
|
34 | m_type(MoveDownAnimation) | |
|
35 | { | |
|
36 | } | |
|
37 | ||
|
38 | template <class T,class U> | |
|
39 | XYChartAnimator<T,U>::~XYChartAnimator() | |
|
40 | { | |
|
41 | } | |
|
42 | ||
|
43 | template <class T, class U> | |
|
44 | QVariant XYChartAnimator<T,U>::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const | |
|
45 | { | |
|
46 | QVector<QPointF> startVector = qVariantValue<QVector<QPointF> >(start); | |
|
47 | QVector<QPointF> endVector = qVariantValue<QVector<QPointF> >(end); | |
|
48 | QVector<QPointF> result; | |
|
49 | ||
|
50 | switch(m_type) { | |
|
51 | ||
|
52 | case MoveDownAnimation: { | |
|
53 | ||
|
54 | Q_ASSERT(startVector.count() == endVector.count()); | |
|
55 | for(int i =0;i< startVector.count();i++) { | |
|
56 | qreal x = startVector[i].x() + ((endVector[i].x()- startVector[i].x()) * progress); | |
|
57 | qreal y = startVector[i].y() + ((endVector[i].y()- startVector[i].y()) * progress); | |
|
58 | result << QPointF(x,y); | |
|
59 | } | |
|
60 | ||
|
61 | } | |
|
62 | break; | |
|
63 | case LineDrawAnimation:{ | |
|
64 | for(int i =0;i< endVector.count()* qBound(0.0, progress, 1.0);i++) { | |
|
65 | result << endVector[i]; | |
|
66 | } | |
|
67 | } | |
|
68 | break; | |
|
69 | default: | |
|
70 | qWarning()<<"Unknow type of animation"; | |
|
71 | break; | |
|
72 | } | |
|
73 | ||
|
74 | return qVariantFromValue(result); | |
|
75 | } | |
|
76 | ||
|
77 | template <class T, class U> | |
|
78 | void XYChartAnimator<T,U>::updateCurrentValue (const QVariant & value ) | |
|
79 | { | |
|
80 | QVector<QPointF> vector = qVariantValue<QVector<QPointF> >(value); | |
|
81 | if(state()!=QAbstractAnimation::Stopped){ //workaround | |
|
82 | m_item->setGeometry(vector); | |
|
83 | } | |
|
84 | } | |
|
85 | ||
|
86 | template <class T, class U> | |
|
87 | void XYChartAnimator<T,U>::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState) | |
|
88 | { | |
|
89 | Q_UNUSED(oldState) | |
|
90 | if (newState==QAbstractAnimation::Running) m_item->animationStarted(); | |
|
91 | QVariantAnimation::updateState(newState,oldState); | |
|
92 | } | |
|
93 | ||
|
94 | QTCOMMERCIALCHART_END_NAMESPACE | |
|
95 | ||
|
96 | #endif /* XYCHARTANIMATOR_P_H_ */ |
@@ -1,10 +1,14 | |||
|
1 | 1 | #include "chartview.h" |
|
2 | 2 | #include <qlineseries.h> |
|
3 | #include <qscatterseries.h> | |
|
4 | #include <qsplineseries.h> | |
|
3 | 5 | #include <QTime> |
|
4 | 6 | |
|
5 | 7 | ChartView::ChartView(QWidget* parent):QChartView(parent), |
|
6 | 8 | m_index(0) |
|
7 | 9 | { |
|
10 | setChartTitle("Three random line charts"); | |
|
11 | ||
|
8 | 12 | QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(handleTimeout())); |
|
9 | 13 | m_timer.setInterval(3000); |
|
10 | 14 | |
@@ -15,11 +19,12 m_index(0) | |||
|
15 | 19 | QPen blue(Qt::blue); |
|
16 | 20 | blue.setWidth(3); |
|
17 | 21 | series0->setPen(blue); |
|
18 |
Q |
|
|
22 | QScatterSeries* series1 = new QScatterSeries(this); | |
|
19 | 23 | QPen red(Qt::red); |
|
20 | 24 | red.setWidth(3); |
|
21 | 25 | series1->setPen(red); |
|
22 | QLineSeries* series2 = new QLineSeries(this); | |
|
26 | series1->setBrush(Qt::white); | |
|
27 | QSplineSeries* series2 = new QSplineSeries(this); | |
|
23 | 28 | QPen green(Qt::green); |
|
24 | 29 | green.setWidth(3); |
|
25 | 30 | series2->setPen(green); |
@@ -27,16 +32,21 m_index(0) | |||
|
27 | 32 | int numPoints = 10; |
|
28 | 33 | |
|
29 | 34 | for (int x = 0; x <= numPoints; ++x) { |
|
30 |
|
|
|
31 |
series |
|
|
32 |
series |
|
|
35 | qreal y = qrand() % 100; | |
|
36 | series0->add(x,y); | |
|
37 | series1->add(x,y); | |
|
38 | series2->add(x,y); | |
|
33 | 39 | } |
|
34 | 40 | |
|
35 | addSeries(series0); | |
|
36 | ||
|
37 | 41 | m_series<<series0; |
|
42 | m_titles<<chartTitle()+": LineChart"; | |
|
38 | 43 | m_series<<series1; |
|
44 | m_titles<<chartTitle()+": ScatterChart"; | |
|
39 | 45 | m_series<<series2; |
|
46 | m_titles<<chartTitle()+": SplineChart"; | |
|
47 | ||
|
48 | addSeries(series0); | |
|
49 | setChartTitle(m_titles.at(0)); | |
|
40 | 50 | |
|
41 | 51 | m_timer.start(); |
|
42 | 52 | } |
@@ -56,4 +66,5 void ChartView::handleTimeout() | |||
|
56 | 66 | m_index++; |
|
57 | 67 | m_index=m_index%m_series.size(); |
|
58 | 68 | addSeries(m_series.at(m_index)); |
|
69 | setChartTitle(m_titles.at(m_index)); | |
|
59 | 70 | } |
@@ -19,6 +19,7 public slots: | |||
|
19 | 19 | private: |
|
20 | 20 | QTimer m_timer; |
|
21 | 21 | QList<QSeries*> m_series; |
|
22 | QStringList m_titles; | |
|
22 | 23 | int m_index; |
|
23 | 24 | }; |
|
24 | 25 |
@@ -8,7 +8,6 int main(int argc, char *argv[]) | |||
|
8 | 8 | QMainWindow window; |
|
9 | 9 | ChartView chartView(&window); |
|
10 | 10 | chartView.setRenderHint(QPainter::Antialiasing); |
|
11 | chartView.setChartTitle("Three random line charts"); | |
|
12 | 11 | chartView.setAnimationOptions(QChart::AllAnimations); |
|
13 | 12 | window.setCentralWidget(&chartView); |
|
14 | 13 | window.resize(400, 300); |
@@ -20,7 +20,6 | |||
|
20 | 20 | #include "stackedbarpresenter_p.h" |
|
21 | 21 | #include "percentbarpresenter_p.h" |
|
22 | 22 | #include "linechartitem_p.h" |
|
23 | #include "linechartanimationitem_p.h" | |
|
24 | 23 | #include "piepresenter_p.h" |
|
25 | 24 | #include "scatterchartitem_p.h" |
|
26 | 25 | #include "splinechartitem_p.h" |
@@ -143,7 +142,7 void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) | |||
|
143 | 142 | QLineSeries* lineSeries = static_cast<QLineSeries*>(series); |
|
144 | 143 | LineChartItem* item; |
|
145 | 144 | if(m_options.testFlag(QChart::SeriesAnimations)){ |
|
146 | item = new LineChartAnimationItem(lineSeries,m_chart); | |
|
145 | item = new LineChartAnimationItem(lineSeries,m_chart); | |
|
147 | 146 | }else{ |
|
148 | 147 | item = new LineChartItem(lineSeries,m_chart); |
|
149 | 148 | } |
@@ -215,19 +214,27 void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) | |||
|
215 | 214 | break; |
|
216 | 215 | } |
|
217 | 216 | case QSeries::SeriesTypeScatter: { |
|
218 |
|
|
|
219 | ScatterChartItem *scatterPresenter = new ScatterChartItem(scatterSeries, m_chart); | |
|
220 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), | |
|
221 | scatterPresenter, SLOT(handleGeometryChanged(const QRectF&))); | |
|
222 | QObject::connect(domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)), | |
|
223 | scatterPresenter, SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | |
|
224 | m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count()); | |
|
225 | m_chartItems.insert(scatterSeries, scatterPresenter); | |
|
226 | if (m_rect.isValid()) | |
|
227 | scatterPresenter->handleGeometryChanged(m_rect); | |
|
228 | scatterPresenter->handleDomainChanged(domain->minX(), domain->maxX(), domain->minY(), domain->maxY()); | |
|
229 | break; | |
|
230 | } | |
|
217 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); | |
|
218 | ScatterChartItem *item; | |
|
219 | if(m_options.testFlag(QChart::SeriesAnimations)) { | |
|
220 | item = new ScatterChartAnimationItem(scatterSeries,m_chart); | |
|
221 | } else { | |
|
222 | item = new ScatterChartItem(scatterSeries, m_chart); | |
|
223 | } | |
|
224 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), | |
|
225 | item, SLOT(handleGeometryChanged(const QRectF&))); | |
|
226 | QObject::connect(domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)), | |
|
227 | item, SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | |
|
228 | //initialize | |
|
229 | if (m_rect.isValid()) | |
|
230 | item->handleGeometryChanged(m_rect); | |
|
231 | item->handleDomainChanged(domain->minX(), domain->maxX(), domain->minY(), domain->maxY()); | |
|
232 | //decorate | |
|
233 | m_chartTheme->decorate(item, scatterSeries, m_chartItems.count()); | |
|
234 | m_chartItems.insert(scatterSeries, item); | |
|
235 | ||
|
236 | break; | |
|
237 | } | |
|
231 | 238 | case QSeries::SeriesTypePie: { |
|
232 | 239 | QPieSeries *s = qobject_cast<QPieSeries *>(series); |
|
233 | 240 | PiePresenter* pie = new PiePresenter(m_chart, s); |
@@ -254,16 +261,24 void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) | |||
|
254 | 261 | } |
|
255 | 262 | |
|
256 | 263 | case QSeries::SeriesTypeSpline: { |
|
257 | QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(series); | |
|
258 | SplineChartItem* splinePresenter = new SplineChartItem(splineSeries, m_chart); | |
|
259 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), splinePresenter, SLOT(handleGeometryChanged(const QRectF&))); | |
|
260 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),splinePresenter,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | |
|
261 | //initialize | |
|
262 | splinePresenter->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); | |
|
263 | m_chartTheme->decorate(splinePresenter, splineSeries, m_chartItems.count()); | |
|
264 | m_chartItems.insert(splineSeries, splinePresenter); | |
|
265 | break; | |
|
266 | } | |
|
264 | ||
|
265 | QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(series); | |
|
266 | SplineChartItem* item; | |
|
267 | if(m_options.testFlag(QChart::SeriesAnimations)) { | |
|
268 | item = new SplineChartAnimationItem(splineSeries, m_chart); | |
|
269 | } else { | |
|
270 | item = new SplineChartItem(splineSeries, m_chart); | |
|
271 | } | |
|
272 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), item, SLOT(handleGeometryChanged(const QRectF&))); | |
|
273 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | |
|
274 | //initialize | |
|
275 | item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); | |
|
276 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); | |
|
277 | //decorate | |
|
278 | m_chartTheme->decorate(item, splineSeries, m_chartItems.count()); | |
|
279 | m_chartItems.insert(splineSeries, item); | |
|
280 | break; | |
|
281 | } | |
|
267 | 282 | default: { |
|
268 | 283 | qDebug()<< "Series type" << series->type() << "not implemented."; |
|
269 | 284 | break; |
@@ -165,12 +165,12 void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int count) | |||
|
165 | 165 | { |
|
166 | 166 | QPen pen; |
|
167 | 167 | if(pen != series->pen()){ |
|
168 | item->setPen(series->pen()); | |
|
168 | item->setLinePen(series->pen()); | |
|
169 | 169 | return; |
|
170 | 170 | } |
|
171 | 171 | pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); |
|
172 | 172 | pen.setWidthF(2); |
|
173 | item->setPen(pen); | |
|
173 | item->setLinePen(pen); | |
|
174 | 174 | } |
|
175 | 175 | |
|
176 | 176 | void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int count) |
@@ -305,13 +305,14 void ChartTheme::decorate(SplineChartItem* item, QSplineSeries* series, int coun | |||
|
305 | 305 | Q_ASSERT(series); |
|
306 | 306 | |
|
307 | 307 | QPen pen; |
|
308 | ||
|
308 | 309 | if(pen != series->pen()){ |
|
309 | item->setPen(series->pen()); | |
|
310 | return; | |
|
310 | item->setLinePen(series->pen()); | |
|
311 | }else{ | |
|
312 | pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); | |
|
313 | pen.setWidthF(series->pen().widthF()); | |
|
314 | item->setLinePen(series->pen()); | |
|
311 | 315 | } |
|
312 | pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); | |
|
313 | pen.setWidthF(series->pen().widthF()); | |
|
314 | item->setPen(pen); | |
|
315 | 316 | |
|
316 | 317 | // QColor color = m_seriesColor.at(count % m_seriesColor.size()); |
|
317 | 318 | // TODO: define alpha in the theme? or in the series? |
@@ -2,13 +2,11 INCLUDEPATH += $$PWD | |||
|
2 | 2 | DEPENDPATH += $$PWD |
|
3 | 3 | |
|
4 | 4 | SOURCES += \ |
|
5 | $$PWD/linechartanimationitem.cpp \ | |
|
6 | 5 | $$PWD/linechartitem.cpp \ |
|
7 | 6 | $$PWD/qlineseries.cpp |
|
8 | 7 | |
|
9 | 8 | PRIVATE_HEADERS += \ |
|
10 |
$$PWD/linechartitem_p.h |
|
|
11 | $$PWD/linechartanimationitem_p.h | |
|
9 | $$PWD/linechartitem_p.h | |
|
12 | 10 | |
|
13 | 11 | PUBLIC_HEADERS += \ |
|
14 | 12 | $$PWD/qlineseries.h No newline at end of file |
@@ -38,7 +38,6 void LineChartAnimationItem::updatePoints(QVector<QPointF>& newPoints) | |||
|
38 | 38 | m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints)); |
|
39 | 39 | QTimer::singleShot(0,m_animation,SLOT(start())); |
|
40 | 40 | |
|
41 | ||
|
42 | 41 | m_points = newPoints; |
|
43 | 42 | m_dirty=false; |
|
44 | 43 |
@@ -93,16 +93,15 void LineChartItem::setGeometry(QVector<QPointF>& points) | |||
|
93 | 93 | XYChartItem::setGeometry(points); |
|
94 | 94 | } |
|
95 | 95 | |
|
96 | void LineChartItem::setPen(const QPen& pen) | |
|
96 | void LineChartItem::setLinePen(const QPen& pen) | |
|
97 | 97 | { |
|
98 | 98 | m_pen = pen; |
|
99 | 99 | } |
|
100 | 100 | |
|
101 | ||
|
102 | 101 | void LineChartItem::handleUpdated() |
|
103 | 102 | { |
|
104 | 103 | m_items.setVisible(m_series->pointsVisible()); |
|
105 | setPen(m_series->pen()); | |
|
104 | setLinePen(m_series->pen()); | |
|
106 | 105 | update(); |
|
107 | 106 | } |
|
108 | 107 |
@@ -3,6 +3,7 | |||
|
3 | 3 | |
|
4 | 4 | #include "qchartglobal.h" |
|
5 | 5 | #include "xychartitem_p.h" |
|
6 | #include "xychartanimationitem_p.h" | |
|
6 | 7 | #include <QPen> |
|
7 | 8 | |
|
8 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
@@ -22,7 +23,7 public: | |||
|
22 | 23 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
23 | 24 | QPainterPath shape() const; |
|
24 | 25 | |
|
25 | void setPen(const QPen& pen); | |
|
26 | void setLinePen(const QPen& pen); | |
|
26 | 27 | void setPointsVisible(bool visible); |
|
27 | 28 | |
|
28 | 29 | public slots: |
@@ -42,10 +43,12 private: | |||
|
42 | 43 | QRectF m_rect; |
|
43 | 44 | QPen m_pen; |
|
44 | 45 | |
|
45 |
friend class |
|
|
46 | template<class,class> friend class XYChartAnimator; | |
|
46 | 47 | |
|
47 | 48 | }; |
|
48 | 49 | |
|
50 | typedef XYChartAnimationItem<LineChartItem,QLineSeries> LineChartAnimationItem; | |
|
51 | ||
|
49 | 52 | QTCOMMERCIALCHART_END_NAMESPACE |
|
50 | 53 | |
|
51 | 54 | #endif |
@@ -117,7 +117,18 void QChart::setChartBackgroundPen(const QPen& pen) | |||
|
117 | 117 | void QChart::setChartTitle(const QString& title) |
|
118 | 118 | { |
|
119 | 119 | createChartTitleItem(); |
|
120 |
m_titleItem->set |
|
|
120 | m_titleItem->setText(title); | |
|
121 | } | |
|
122 | ||
|
123 | /*! | |
|
124 | Gets the chart \a title. The description text that is rendered above the chart. | |
|
125 | */ | |
|
126 | QString QChart::chartTitle() const | |
|
127 | { | |
|
128 | if(m_titleItem) | |
|
129 | return m_titleItem->text(); | |
|
130 | else | |
|
131 | return QString(); | |
|
121 | 132 | } |
|
122 | 133 | |
|
123 | 134 | /*! |
@@ -141,7 +152,7 void QChart::createChartBackgroundItem() | |||
|
141 | 152 | void QChart::createChartTitleItem() |
|
142 | 153 | { |
|
143 | 154 | if(!m_titleItem) { |
|
144 | m_titleItem = new QGraphicsTextItem(this); | |
|
155 | m_titleItem = new QGraphicsSimpleTextItem(this); | |
|
145 | 156 | m_titleItem->setZValue(ChartPresenter::BackgroundZValue); |
|
146 | 157 | } |
|
147 | 158 | } |
@@ -57,6 +57,7 public: | |||
|
57 | 57 | QChart::ChartTheme chartTheme() const; |
|
58 | 58 | |
|
59 | 59 | void setChartTitle(const QString& title); |
|
60 | QString chartTitle() const; | |
|
60 | 61 | void setChartTitleFont(const QFont& font); |
|
61 | 62 | void setChartBackgroundBrush(const QBrush& brush); |
|
62 | 63 | void setChartBackgroundPen(const QPen& pen); |
@@ -82,7 +83,7 private: | |||
|
82 | 83 | private: |
|
83 | 84 | Q_DISABLE_COPY(QChart) |
|
84 | 85 | QGraphicsRectItem* m_backgroundItem; |
|
85 | QGraphicsTextItem* m_titleItem; | |
|
86 | QGraphicsSimpleTextItem* m_titleItem; | |
|
86 | 87 | QRectF m_rect; |
|
87 | 88 | ChartDataSet *m_dataset; |
|
88 | 89 | ChartPresenter *m_presenter; |
@@ -142,6 +142,14 void QChartView::setChartTitle(const QString& title) | |||
|
142 | 142 | } |
|
143 | 143 | |
|
144 | 144 | /*! |
|
145 | Gets the chart \a title. A description text that is rendered above the chart. | |
|
146 | */ | |
|
147 | QString QChartView::chartTitle() const | |
|
148 | { | |
|
149 | return m_chart->chartTitle(); | |
|
150 | } | |
|
151 | ||
|
152 | /*! | |
|
145 | 153 | Sets the \a font that is used for rendering the description text that is rendered above the chart. |
|
146 | 154 | */ |
|
147 | 155 | void QChartView::setChartTitleFont(const QFont& font) |
@@ -30,6 +30,7 public: | |||
|
30 | 30 | int margin() const; |
|
31 | 31 | |
|
32 | 32 | void setChartTitle(const QString& title); |
|
33 | QString chartTitle() const; | |
|
33 | 34 | void setChartTitleFont(const QFont& font); |
|
34 | 35 | void setChartBackgroundBrush(const QBrush& brush); |
|
35 | 36 | void setChartBackgroundPen(const QPen& pen); |
@@ -6,7 +6,7 | |||
|
6 | 6 | |
|
7 | 7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | 8 | |
|
9 |
ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphics |
|
|
9 | ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent) : | |
|
10 | 10 | XYChartItem(series,parent), |
|
11 | 11 | m_series(series), |
|
12 | 12 | m_items(this), |
@@ -3,7 +3,8 | |||
|
3 | 3 | |
|
4 | 4 | #include "qchartglobal.h" |
|
5 | 5 | #include "xychartitem_p.h" |
|
6 | #include <QObject> | |
|
6 | #include "xychartanimationitem_p.h" | |
|
7 | #include <QGraphicsItem> | |
|
7 | 8 | #include <QPen> |
|
8 | 9 | |
|
9 | 10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
@@ -14,7 +15,7 class ScatterChartItem : public XYChartItem | |||
|
14 | 15 | { |
|
15 | 16 | Q_OBJECT |
|
16 | 17 | public: |
|
17 |
explicit ScatterChartItem(QScatterSeries *series, QGraphics |
|
|
18 | explicit ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent = 0); | |
|
18 | 19 | |
|
19 | 20 | public: |
|
20 | 21 | //from QGraphicsItem |
@@ -43,8 +44,12 private: | |||
|
43 | 44 | int m_shape; |
|
44 | 45 | int m_size; |
|
45 | 46 | QRectF m_rect; |
|
47 | ||
|
48 | template<class,class> friend class XYChartAnimator; | |
|
46 | 49 | }; |
|
47 | 50 | |
|
51 | typedef XYChartAnimationItem<ScatterChartItem,QScatterSeries> ScatterChartAnimationItem; | |
|
52 | ||
|
48 | 53 | QTCOMMERCIALCHART_END_NAMESPACE |
|
49 | 54 | |
|
50 | 55 | #endif // SCATTERPRESENTER_H |
@@ -1,12 +1,16 | |||
|
1 | 1 | #include "splinechartitem_p.h" |
|
2 | #include "chartpresenter_p.h" | |
|
2 | 3 | #include <QPainter> |
|
3 | 4 | |
|
4 | 5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
5 | 6 | |
|
6 |
SplineChartItem::SplineChartItem(QSplineSeries* series, QGraphics |
|
|
7 | SplineChartItem::SplineChartItem(QSplineSeries* series, QGraphicsItem *parent) : | |
|
7 | 8 | XYChartItem(series, parent), |
|
8 | 9 | m_series(series) |
|
9 | 10 | { |
|
11 | setZValue(ChartPresenter::LineChartZValue); | |
|
12 | QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated())); | |
|
13 | handleUpdated(); | |
|
10 | 14 | } |
|
11 | 15 | |
|
12 | 16 | QRectF SplineChartItem::boundingRect() const |
@@ -26,6 +30,7 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const | |||
|
26 | 30 | |
|
27 | 31 | void SplineChartItem::setGeometry(QVector<QPointF>& points) |
|
28 | 32 | { |
|
33 | ||
|
29 | 34 | if(points.size()==0) return; |
|
30 | 35 | |
|
31 | 36 | QPainterPath splinePath; |
@@ -44,11 +49,22 void SplineChartItem::setGeometry(QVector<QPointF>& points) | |||
|
44 | 49 | XYChartItem::setGeometry(points); |
|
45 | 50 | } |
|
46 | 51 | |
|
47 | void SplineChartItem::setPen(const QPen& pen) | |
|
52 | void SplineChartItem::setLinePen(const QPen& pen) | |
|
48 | 53 | { |
|
49 | 54 | m_pen = pen; |
|
55 | qDebug()<<pen; | |
|
50 | 56 | } |
|
51 | 57 | |
|
58 | //handlers | |
|
59 | ||
|
60 | void SplineChartItem::handleUpdated() | |
|
61 | { | |
|
62 | //m_items.setVisible(m_series->pointsVisible()); | |
|
63 | setLinePen(m_series->pen()); | |
|
64 | update(); | |
|
65 | } | |
|
66 | ||
|
67 | //painter | |
|
52 | 68 | |
|
53 | 69 | void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
54 | 70 | { |
@@ -56,6 +72,7 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *o | |||
|
56 | 72 | Q_UNUSED(option); |
|
57 | 73 | painter->save(); |
|
58 | 74 | painter->setClipRect(clipRect()); |
|
75 | painter->setPen(m_pen); | |
|
59 | 76 | painter->drawPath(m_path); |
|
60 | 77 | |
|
61 | 78 | const QVector<QPointF> points = XYChartItem::points(); |
@@ -79,6 +96,8 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *o | |||
|
79 | 96 | painter->restore(); |
|
80 | 97 | } |
|
81 | 98 | |
|
99 | ||
|
100 | ||
|
82 | 101 | #include "moc_splinechartitem_p.cpp" |
|
83 | 102 | |
|
84 | 103 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,10 +1,10 | |||
|
1 | 1 | #ifndef SPLINECHARTITEM_P_H |
|
2 | 2 | #define SPLINECHARTITEM_P_H |
|
3 | 3 | |
|
4 | #include "chartitem_p.h" | |
|
5 | #include <QObject> | |
|
6 | 4 | #include "qsplineseries.h" |
|
7 | 5 | #include "xychartitem_p.h" |
|
6 | #include "xychartanimationitem_p.h" | |
|
7 | #include <QGraphicsItem> | |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | 10 | |
@@ -12,16 +12,19 class SplineChartItem : public XYChartItem | |||
|
12 | 12 | { |
|
13 | 13 | Q_OBJECT |
|
14 | 14 | public: |
|
15 |
SplineChartItem(QSplineSeries* series, QGraphics |
|
|
15 | SplineChartItem(QSplineSeries* series, QGraphicsItem *parent = 0); | |
|
16 | 16 | |
|
17 | 17 | //from QGraphicsItem |
|
18 | 18 | QRectF boundingRect() const; |
|
19 | 19 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
20 | 20 | QPainterPath shape() const; |
|
21 | 21 | |
|
22 | void setPen(const QPen& pen); | |
|
22 | void setLinePen(const QPen& pen); | |
|
23 | 23 | void setPointsVisible(bool visible); |
|
24 | 24 | |
|
25 | public slots: | |
|
26 | void handleUpdated(); | |
|
27 | ||
|
25 | 28 | protected: |
|
26 | 29 | void setGeometry(QVector<QPointF>& points); |
|
27 | 30 | |
@@ -33,8 +36,12 private: | |||
|
33 | 36 | QPainterPath m_path; |
|
34 | 37 | QRectF m_rect; |
|
35 | 38 | QPen m_pen; |
|
39 | ||
|
40 | template<class,class> friend class XYChartAnimator; | |
|
36 | 41 | }; |
|
37 | 42 | |
|
43 | typedef XYChartAnimationItem<SplineChartItem,QSplineSeries> SplineChartAnimationItem; | |
|
44 | ||
|
38 | 45 | QTCOMMERCIALCHART_END_NAMESPACE |
|
39 | 46 | |
|
40 | 47 | #endif // SPLINECHARTITEM_P_H |
@@ -3,10 +3,12 DEPENDPATH += $$PWD | |||
|
3 | 3 | |
|
4 | 4 | SOURCES += \ |
|
5 | 5 | $$PWD/xychartitem.cpp \ |
|
6 | $$PWD/qxyseries.cpp | |
|
6 | $$PWD/qxyseries.cpp | |
|
7 | 7 | |
|
8 | 8 | PRIVATE_HEADERS += \ |
|
9 | 9 | $$PWD/xychartitem_p.h \ |
|
10 | $$PWD/xychartanimationitem_p.h \ | |
|
11 | $$PWD/xychartanimator_p.h | |
|
10 | 12 | |
|
11 | 13 | PUBLIC_HEADERS += \ |
|
12 | 14 | $$PWD/qxyseries.h No newline at end of file |
@@ -55,16 +55,14 QVector<QPointF> XYChartItem::calculateGeometryPoints() const | |||
|
55 | 55 | return points; |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | ||
|
59 | 58 | void XYChartItem::updatePoints(QVector<QPointF>& points) |
|
60 | 59 | { |
|
61 | 60 | setGeometry(points); |
|
62 | 61 | } |
|
63 | 62 | |
|
64 |
void XYChartItem::updatePoint( |
|
|
63 | void XYChartItem::updatePoint(QVector<QPointF>& points) | |
|
65 | 64 | { |
|
66 | m_points.replace(index,newPoint); | |
|
67 | setGeometry(m_points); | |
|
65 | setGeometry(points); | |
|
68 | 66 | } |
|
69 | 67 | |
|
70 | 68 | void XYChartItem::setGeometry(QVector<QPointF>& points) |
@@ -89,7 +87,6 void XYChartItem::handlePointRemoved(int index) | |||
|
89 | 87 | { |
|
90 | 88 | Q_ASSERT(index<m_series->count()); |
|
91 | 89 | Q_ASSERT(index>=0); |
|
92 | ||
|
93 | 90 | QPointF point = calculateGeometryPoint(index); |
|
94 | 91 | QVector<QPointF> points = m_points; |
|
95 | 92 | points.remove(index); |
@@ -102,7 +99,9 void XYChartItem::handlePointReplaced(int index) | |||
|
102 | 99 | Q_ASSERT(index<m_series->count()); |
|
103 | 100 | Q_ASSERT(index>=0); |
|
104 | 101 | QPointF point = calculateGeometryPoint(index); |
|
105 | updatePoint(index,point); | |
|
102 | QVector<QPointF> points = m_points; | |
|
103 | m_points.replace(index,point); | |
|
104 | updatePoint(points); | |
|
106 | 105 | update(); |
|
107 | 106 | } |
|
108 | 107 |
@@ -29,8 +29,9 public slots: | |||
|
29 | 29 | |
|
30 | 30 | protected: |
|
31 | 31 | virtual void updatePoints(QVector<QPointF>& points); |
|
32 |
virtual void updatePoint( |
|
|
32 | virtual void updatePoint(QVector<QPointF>& points); | |
|
33 | 33 | virtual void setGeometry(QVector<QPointF>& points); |
|
34 | ||
|
34 | 35 | QPointF calculateGeometryPoint(const QPointF& point) const; |
|
35 | 36 | QPointF calculateGeometryPoint(int index) const; |
|
36 | 37 | QVector<QPointF> calculateGeometryPoints() const; |
@@ -48,6 +49,8 private: | |||
|
48 | 49 | QRectF m_clipRect; |
|
49 | 50 | QVector<QPointF> m_points; |
|
50 | 51 | |
|
52 | template<class,class> friend class XYChartAnimator; | |
|
53 | ||
|
51 | 54 | }; |
|
52 | 55 | |
|
53 | 56 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now