##// END OF EJS Templates
Refactors click signal to line,area,spline,scatter charts
Michal Klocek -
r571:0bb609016fcc
parent child
Show More
@@ -1,86 +1,96
1 #include "chartview.h"
1 #include "chartview.h"
2 #include <qlineseries.h>
2 #include <qlineseries.h>
3 #include <qscatterseries.h>
3 #include <qscatterseries.h>
4 #include <qsplineseries.h>
4 #include <qsplineseries.h>
5 #include <qareaseries.h>
5 #include <qareaseries.h>
6 #include <QTime>
6 #include <QTime>
7
7
8 ChartView::ChartView(QWidget* parent):QChartView(parent),
8 ChartView::ChartView(QWidget* parent):QChartView(parent),
9 m_index(0)
9 m_index(0)
10 {
10 {
11 QTime now = QTime::currentTime();
11 QTime now = QTime::currentTime();
12 qsrand((uint)now.msec());
12 qsrand((uint)now.msec());
13 setChartTitle("Three random line charts");
13 setChartTitle("Three random line charts");
14
14
15 QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(handleTimeout()));
15 QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(handleTimeout()));
16 m_timer.setInterval(3000);
16 m_timer.setInterval(3000);
17
17
18 //![1]
18 //![1]
19 QLineSeries* series0 = new QLineSeries(this);
19 QLineSeries* series0 = new QLineSeries(this);
20 QPen blue(Qt::blue);
20 QPen blue(Qt::blue);
21 blue.setWidth(3);
21 blue.setWidth(3);
22 series0->setPen(blue);
22 series0->setPen(blue);
23
23
24 QScatterSeries* series1 = new QScatterSeries(this);
24 QScatterSeries* series1 = new QScatterSeries(this);
25 QPen red(Qt::red);
25 QPen red(Qt::red);
26 red.setWidth(3);
26 red.setWidth(3);
27 series1->setPen(red);
27 series1->setPen(red);
28 series1->setBrush(Qt::white);
28 series1->setBrush(Qt::white);
29
29
30 QSplineSeries* series2 = new QSplineSeries(this);
30 QSplineSeries* series2 = new QSplineSeries(this);
31 QPen green(Qt::green);
31 QPen green(Qt::green);
32 green.setWidth(3);
32 green.setWidth(3);
33 series2->setPen(green);
33 series2->setPen(green);
34
34
35 QAreaSeries* series3 = new QAreaSeries(series0);
35 QAreaSeries* series3 = new QAreaSeries(series0);
36 QPen yellow(Qt::black);
36 QPen yellow(Qt::black);
37 yellow.setWidth(3);
37 yellow.setWidth(3);
38 series3->setPen(yellow);
38 series3->setPen(yellow);
39 series3->setBrush(Qt::yellow);
39 series3->setBrush(Qt::yellow);
40 //![1]
40 //![1]
41
41
42 //![2]
42 //![2]
43 int numPoints = 10;
43 int numPoints = 10;
44
44
45 for (int x = 0; x <= numPoints; ++x) {
45 for (int x = 0; x <= numPoints; ++x) {
46 qreal y = qrand() % 100;
46 qreal y = qrand() % 100;
47 series0->add(x,y);
47 series0->add(x,y);
48 series1->add(x,y);
48 series1->add(x,y);
49 series2->add(x,y);
49 series2->add(x,y);
50 }
50 }
51 //![2]
51 //![2]
52
52
53 //![3]
53 //![3]
54 m_series<<series0;
54 m_series<<series0;
55 m_titles<<chartTitle()+": LineChart";
55 m_titles<<chartTitle()+": LineChart";
56 m_series<<series1;
56 m_series<<series1;
57 m_titles<<chartTitle()+": ScatterChart";
57 m_titles<<chartTitle()+": ScatterChart";
58 m_series<<series2;
58 m_series<<series2;
59 m_titles<<chartTitle()+": SplineChart";
59 m_titles<<chartTitle()+": SplineChart";
60 m_series<<series3;
60 m_series<<series3;
61 m_titles<<chartTitle()+": AreaChart";
61 m_titles<<chartTitle()+": AreaChart";
62 //![3]
62 //![3]
63
63 addSeries(series0);
64 addSeries(series0);
64 setChartTitle(m_titles.at(0));
65 setChartTitle(m_titles.at(0));
65
66
67 foreach (QSeries* series, m_series) {
68 QObject::connect(series,SIGNAL(clicked(const QPointF&)),this,SLOT(handlePointClicked(const QPointF&)));
69 }
70
66 m_timer.start();
71 m_timer.start();
67 }
72 }
68
73
69 ChartView::~ChartView()
74 ChartView::~ChartView()
70 {
75 {
71 if(m_series.size()==0) return;
76 if(m_series.size()==0) return;
72 removeSeries(m_series.at(m_index));
77 removeSeries(m_series.at(m_index));
73 qDeleteAll(m_series);
78 qDeleteAll(m_series);
74 }
79 }
75
80
76 //![4]
81 //![4]
77 void ChartView::handleTimeout()
82 void ChartView::handleTimeout()
78 {
83 {
79 if(m_series.size()==0) return;
84 if(m_series.size()==0) return;
80 removeSeries(m_series.at(m_index));
85 removeSeries(m_series.at(m_index));
81 m_index++;
86 m_index++;
82 m_index=m_index%m_series.size();
87 m_index=m_index%m_series.size();
83 addSeries(m_series.at(m_index));
88 addSeries(m_series.at(m_index));
84 setChartTitle(m_titles.at(m_index));
89 setChartTitle(m_titles.at(m_index));
85 }
90 }
86 //![4]
91 //![4]
92
93 void ChartView::handlePointClicked(const QPointF& point)
94 {
95 setChartTitle(m_titles.at(m_index) + QString(" x: %1 y: %2").arg(point.x()).arg(point.y()));
96 }
@@ -1,28 +1,29
1 #ifndef CHARTVIEW_H_
1 #ifndef CHARTVIEW_H_
2 #define CHARTVIEW_H_
2 #define CHARTVIEW_H_
3
3
4 #include <qchartview.h>
4 #include <qchartview.h>
5 #include <QTimer>
5 #include <QTimer>
6
6
7 QTCOMMERCIALCHART_USE_NAMESPACE
7 QTCOMMERCIALCHART_USE_NAMESPACE
8
8
9 //![1]
9 //![1]
10 class ChartView: public QChartView
10 class ChartView: public QChartView
11 {
11 {
12 Q_OBJECT
12 Q_OBJECT
13 public:
13 public:
14 ChartView(QWidget* parent=0);
14 ChartView(QWidget* parent=0);
15 virtual ~ChartView();
15 virtual ~ChartView();
16
16
17 public slots:
17 public slots:
18 void handleTimeout();
18 void handleTimeout();
19 void handlePointClicked(const QPointF& point);
19
20
20 private:
21 private:
21 QTimer m_timer;
22 QTimer m_timer;
22 QList<QSeries*> m_series;
23 QList<QSeries*> m_series;
23 QStringList m_titles;
24 QStringList m_titles;
24 int m_index;
25 int m_index;
25 };
26 };
26 //![1]
27 //![1]
27
28
28 #endif /* CHARTVIEW_H_ */
29 #endif /* CHARTVIEW_H_ */
@@ -1,113 +1,120
1 #include "areachartitem_p.h"
1 #include "areachartitem_p.h"
2 #include "qareaseries.h"
2 #include "qareaseries.h"
3 #include "qlineseries.h"
3 #include "qlineseries.h"
4 #include "chartpresenter_p.h"
4 #include "chartpresenter_p.h"
5 #include <QPainter>
5 #include <QPainter>
6 #include <QGraphicsSceneMouseEvent>
6
7
7
8
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10
10 //TODO: optimize : remove points which are not visible
11 //TODO: optimize : remove points which are not visible
11
12
12 AreaChartItem::AreaChartItem(QAreaSeries* areaSeries,QGraphicsItem *parent):ChartItem(parent),
13 AreaChartItem::AreaChartItem(QAreaSeries* areaSeries,QGraphicsItem *parent):ChartItem(parent),
13 m_series(areaSeries),
14 m_series(areaSeries),
14 m_upper(0),
15 m_upper(0),
15 m_lower(0),
16 m_lower(0),
16 m_pointsVisible(false)
17 m_pointsVisible(false)
17 {
18 {
18 setZValue(ChartPresenter::LineChartZValue);
19 setZValue(ChartPresenter::LineChartZValue);
19 m_upper = new AreaBoundItem(this,m_series->upperSeries());
20 m_upper = new AreaBoundItem(this,m_series->upperSeries());
20 if(m_series->lowerSeries()){
21 if(m_series->lowerSeries()){
21 m_lower = new AreaBoundItem(this,m_series->lowerSeries());
22 m_lower = new AreaBoundItem(this,m_series->lowerSeries());
22 }
23 }
23
24
24 QObject::connect(areaSeries,SIGNAL(updated()),this,SLOT(handleUpdated()));
25 QObject::connect(areaSeries,SIGNAL(updated()),this,SLOT(handleUpdated()));
26 QObject::connect(this,SIGNAL(clicked(const QPointF&)),areaSeries,SIGNAL(clicked(const QPointF&)));
25
27
26 handleUpdated();
28 handleUpdated();
27 }
29 }
28
30
29 AreaChartItem::~AreaChartItem()
31 AreaChartItem::~AreaChartItem()
30 {
32 {
31 delete m_upper;
33 delete m_upper;
32 delete m_lower;
34 delete m_lower;
33 };
35 };
34
36
35 QRectF AreaChartItem::boundingRect() const
37 QRectF AreaChartItem::boundingRect() const
36 {
38 {
37 return m_rect;
39 return m_rect;
38 }
40 }
39
41
40 QPainterPath AreaChartItem::shape() const
42 QPainterPath AreaChartItem::shape() const
41 {
43 {
42 return m_path;
44 return m_path;
43 }
45 }
44
46
45 void AreaChartItem::updatePath()
47 void AreaChartItem::updatePath()
46 {
48 {
47 QPainterPath path;
49 QPainterPath path;
48
50
49 path.connectPath(m_upper->shape());
51 path.connectPath(m_upper->shape());
50 if(m_lower){
52 if(m_lower){
51 path.connectPath(m_lower->shape().toReversed());
53 path.connectPath(m_lower->shape().toReversed());
52 }
54 }
53 else{
55 else{
54 QPointF first = path.pointAtPercent(0);
56 QPointF first = path.pointAtPercent(0);
55 QPointF last = path.pointAtPercent(1);
57 QPointF last = path.pointAtPercent(1);
56 path.lineTo(last.x(),m_clipRect.bottom());
58 path.lineTo(last.x(),m_clipRect.bottom());
57 path.lineTo(first.x(),m_clipRect.bottom());
59 path.lineTo(first.x(),m_clipRect.bottom());
58 }
60 }
59 path.closeSubpath();
61 path.closeSubpath();
60 prepareGeometryChange();
62 prepareGeometryChange();
61 m_path=path;
63 m_path=path;
62 m_rect=path.boundingRect();
64 m_rect=path.boundingRect();
63 update();
65 update();
64 }
66 }
65
67
66 void AreaChartItem::handleUpdated()
68 void AreaChartItem::handleUpdated()
67 {
69 {
68 m_pointsVisible = m_series->pointsVisible();
70 m_pointsVisible = m_series->pointsVisible();
69 m_linePen = m_series->pen();
71 m_linePen = m_series->pen();
70 m_brush = m_series->brush();
72 m_brush = m_series->brush();
71 m_pointPen = m_series->pen();
73 m_pointPen = m_series->pen();
72 m_pointPen.setWidthF(2*m_pointPen.width());
74 m_pointPen.setWidthF(2*m_pointPen.width());
73
75
74 update();
76 update();
75 }
77 }
76
78
77 void AreaChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
79 void AreaChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
78 {
80 {
79 m_upper->handleDomainChanged(minX,maxX,minY,maxY);
81 m_upper->handleDomainChanged(minX,maxX,minY,maxY);
80 if(m_lower)
82 if(m_lower)
81 m_lower->handleDomainChanged(minX,maxX,minY,maxY);
83 m_lower->handleDomainChanged(minX,maxX,minY,maxY);
82 }
84 }
83
85
84 void AreaChartItem::handleGeometryChanged(const QRectF& rect)
86 void AreaChartItem::handleGeometryChanged(const QRectF& rect)
85 {
87 {
86 m_clipRect=rect.translated(-rect.topLeft());
88 m_clipRect=rect.translated(-rect.topLeft());
87 setPos(rect.topLeft());
89 setPos(rect.topLeft());
88 m_upper->handleGeometryChanged(rect);
90 m_upper->handleGeometryChanged(rect);
89 if(m_lower)
91 if(m_lower)
90 m_lower->handleGeometryChanged(rect);
92 m_lower->handleGeometryChanged(rect);
91 }
93 }
92 //painter
94 //painter
93
95
94 void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
96 void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
95 {
97 {
96 Q_UNUSED(widget);
98 Q_UNUSED(widget);
97 Q_UNUSED(option);
99 Q_UNUSED(option);
98 painter->save();
100 painter->save();
99 painter->setPen(m_linePen);
101 painter->setPen(m_linePen);
100 painter->setBrush(m_brush);
102 painter->setBrush(m_brush);
101 painter->setClipRect(m_clipRect);
103 painter->setClipRect(m_clipRect);
102 painter->drawPath(m_path);
104 painter->drawPath(m_path);
103 if(m_pointsVisible){
105 if(m_pointsVisible){
104 painter->setPen(m_pointPen);
106 painter->setPen(m_pointPen);
105 painter->drawPoints(m_upper->points());
107 painter->drawPoints(m_upper->points());
106 if(m_lower) painter->drawPoints(m_lower->points());
108 if(m_lower) painter->drawPoints(m_lower->points());
107 }
109 }
108 painter->restore();
110 painter->restore();
109 }
111 }
110
112
113 void AreaChartItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
114 {
115 emit clicked(m_upper->calculateDomainPoint(event->pos()));
116 }
117
111 #include "moc_areachartitem_p.cpp"
118 #include "moc_areachartitem_p.cpp"
112
119
113 QTCOMMERCIALCHART_END_NAMESPACE
120 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,68 +1,75
1 #ifndef AREACHARTITEM_H
1 #ifndef AREACHARTITEM_H
2 #define AREACHARTITEM_H
2 #define AREACHARTITEM_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "linechartitem_p.h"
5 #include "linechartitem_p.h"
6 #include <QPen>
6 #include <QPen>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QAreaSeries;
10 class QAreaSeries;
11 class AreaChartItem;
11 class AreaChartItem;
12
12
13 class AreaChartItem : public QObject ,public ChartItem
13 class AreaChartItem : public QObject ,public ChartItem
14 {
14 {
15 Q_OBJECT
15 Q_OBJECT
16 public:
16 public:
17 AreaChartItem(QAreaSeries* areaSeries, QGraphicsItem *parent = 0);
17 AreaChartItem(QAreaSeries* areaSeries, QGraphicsItem *parent = 0);
18 ~ AreaChartItem();
18 ~ AreaChartItem();
19
19
20 //from QGraphicsItem
20 //from QGraphicsItem
21 QRectF boundingRect() const;
21 QRectF boundingRect() const;
22 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
22 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23 QPainterPath shape() const;
23 QPainterPath shape() const;
24
24
25 LineChartItem* upperLineItem() const { return m_upper ;}
25 LineChartItem* upperLineItem() const { return m_upper ;}
26 LineChartItem* lowerLineItem() const { return m_lower ;}
26 LineChartItem* lowerLineItem() const { return m_lower ;}
27
27
28 void updatePath();
28 void updatePath();
29
30 protected:
31 void mousePressEvent( QGraphicsSceneMouseEvent * event );
32
33 signals:
34 void clicked(const QPointF& point);
35
29 public slots:
36 public slots:
30 void handleUpdated();
37 void handleUpdated();
31 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
38 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
32 void handleGeometryChanged(const QRectF& size);
39 void handleGeometryChanged(const QRectF& size);
33
40
34 private:
41 private:
35 QAreaSeries* m_series;
42 QAreaSeries* m_series;
36 LineChartItem* m_upper;
43 LineChartItem* m_upper;
37 LineChartItem* m_lower;
44 LineChartItem* m_lower;
38 QPainterPath m_path;
45 QPainterPath m_path;
39 QRectF m_rect;
46 QRectF m_rect;
40 QRectF m_clipRect;
47 QRectF m_clipRect;
41 QPen m_linePen;
48 QPen m_linePen;
42 QPen m_pointPen;
49 QPen m_pointPen;
43 QBrush m_brush;
50 QBrush m_brush;
44 bool m_pointsVisible;
51 bool m_pointsVisible;
45
52
46 };
53 };
47
54
48 class AreaBoundItem : public LineChartItem
55 class AreaBoundItem : public LineChartItem
49 {
56 {
50 public:
57 public:
51 AreaBoundItem(AreaChartItem* item,QLineSeries* lineSeries):LineChartItem(lineSeries),
58 AreaBoundItem(AreaChartItem* item,QLineSeries* lineSeries):LineChartItem(lineSeries),
52 m_item(item){};
59 m_item(item){};
53
60
54 ~AreaBoundItem(){};
61 ~AreaBoundItem(){};
55
62
56 void setLayout(QVector<QPointF>& points){
63 void setLayout(QVector<QPointF>& points){
57 LineChartItem::setLayout(points);
64 LineChartItem::setLayout(points);
58 m_item->updatePath();
65 m_item->updatePath();
59 }
66 }
60
67
61 private:
68 private:
62 AreaChartItem* m_item;
69 AreaChartItem* m_item;
63
70
64 };
71 };
65
72
66 QTCOMMERCIALCHART_END_NAMESPACE
73 QTCOMMERCIALCHART_END_NAMESPACE
67
74
68 #endif
75 #endif
@@ -1,115 +1,124
1 #include "qareaseries.h"
1 #include "qareaseries.h"
2 #include "qlineseries.h"
2 #include "qlineseries.h"
3
3
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5
5
6 /*!
6 /*!
7 \class QAreaSeries
7 \class QAreaSeries
8 \brief The QAreaSeries class is used for making area charts.
8 \brief The QAreaSeries class is used for making area charts.
9
9
10 \mainclass
10 \mainclass
11
11
12 An area chart is used to show quantitative data. It is based on line chart, in the way that area between axis and the line
12 An area chart is used to show quantitative data. It is based on line chart, in the way that area between axis and the line
13 is emphasized with color. Since the area chart is based on line chart, QAreaSeries constructor needs QLineSeries instance,
13 is emphasized with color. Since the area chart is based on line chart, QAreaSeries constructor needs QLineSeries instance,
14 which defines "upper" boundary of the area. "Lower" boundary is defined by default by axis X. Instead of axis X "lower" boundary can be specified by other line.
14 which defines "upper" boundary of the area. "Lower" boundary is defined by default by axis X. Instead of axis X "lower" boundary can be specified by other line.
15 In that case QAreaSeries should be initiated with two QLineSerie instances. Please note terms "upper" and "lower" boundary can be misleading in cases
15 In that case QAreaSeries should be initiated with two QLineSerie instances. Please note terms "upper" and "lower" boundary can be misleading in cases
16 where "lower" boundary had bigger values than the "upper" one, however the main point that area between these two boundary lines will be filled.
16 where "lower" boundary had bigger values than the "upper" one, however the main point that area between these two boundary lines will be filled.
17
17
18 \image areachart.png
18 \image areachart.png
19
19
20 Creating basic area chart is simple:
20 Creating basic area chart is simple:
21 \code
21 \code
22 QLineSeries* lineSeries = new QLineSeries();
22 QLineSeries* lineSeries = new QLineSeries();
23 series->add(0, 6);
23 series->add(0, 6);
24 series->add(2, 4);
24 series->add(2, 4);
25 QAreaSeries* areaSeries = new QAreaSeries(lineSeries);
25 QAreaSeries* areaSeries = new QAreaSeries(lineSeries);
26 ...
26 ...
27 chartView->addSeries(areaSeries);
27 chartView->addSeries(areaSeries);
28 \endcode
28 \endcode
29 */
29 */
30
30
31 /*!
31 /*!
32 \fn virtual QSeriesType QAreaSeries::type() const
32 \fn virtual QSeriesType QAreaSeries::type() const
33 \brief Returns type of series.
33 \brief Returns type of series.
34 \sa QSeries, QSeriesType
34 \sa QSeries, QSeriesType
35 */
35 */
36
36
37 /*!
37 /*!
38 \fn QLineSeries* QAreaSeries::upperSeries() const
38 \fn QLineSeries* QAreaSeries::upperSeries() const
39 \brief Returns upperSeries used to define one of area boundaries.
39 \brief Returns upperSeries used to define one of area boundaries.
40 */
40 */
41
41
42 /*!
42 /*!
43 \fn QLineSeries* QAreaSeries::lowerSeries() const
43 \fn QLineSeries* QAreaSeries::lowerSeries() const
44 \brief Returns lowerSeries used to define one of area boundaries. Note if QAreaSeries where counstucted wihtout a\ lowerSeries
44 \brief Returns lowerSeries used to define one of area boundaries. Note if QAreaSeries where counstucted wihtout a\ lowerSeries
45 this function return Null pointer.
45 this function return Null pointer.
46 */
46 */
47
47
48 /*!
48 /*!
49 \fn QPen QAreaSeries::pen() const
49 \fn QPen QAreaSeries::pen() const
50 \brief Returns the pen used to draw line for this series.
50 \brief Returns the pen used to draw line for this series.
51 \sa setPen()
51 \sa setPen()
52 */
52 */
53
53
54 /*!
54 /*!
55 \fn QPen QAreaSeries::brush() const
55 \fn QPen QAreaSeries::brush() const
56 \brief Returns the brush used to draw line for this series.
56 \brief Returns the brush used to draw line for this series.
57 \sa setBrush()
57 \sa setBrush()
58 */
58 */
59
59
60 /*!
60 /*!
61 \fn bool QAreaSeries::pointsVisible() const
61 \fn bool QAreaSeries::pointsVisible() const
62 \brief Returns if the points are drawn for this series.
62 \brief Returns if the points are drawn for this series.
63 \sa setPointsVisible()
63 \sa setPointsVisible()
64 */
64 */
65
65
66 /*!
66 /*!
67 \fn void QAreaSeries::updated()
67 \fn void QAreaSeries::updated()
68 \brief \internal
68 \brief \internal
69 */
69 */
70
70
71 /*!
71 /*!
72 Constructs area series object which is a child of \a upperSeries. Area will be spanned between \a
72 Constructs area series object which is a child of \a upperSeries. Area will be spanned between \a
73 upperSeries line and \a lowerSeries line. If no \a lowerSeries is passed to constructor, area is specified by axis x (y=0) instead.
73 upperSeries line and \a lowerSeries line. If no \a lowerSeries is passed to constructor, area is specified by axis x (y=0) instead.
74 When series object is added to QChartView or QChart instance ownerships is transfered.
74 When series object is added to QChartView or QChart instance ownerships is transfered.
75 */
75 */
76 QAreaSeries::QAreaSeries(QLineSeries* upperSeries,QLineSeries* lowerSeries):QSeries(upperSeries),
76 QAreaSeries::QAreaSeries(QLineSeries* upperSeries,QLineSeries* lowerSeries):QSeries(upperSeries),
77 m_upperSeries(upperSeries),
77 m_upperSeries(upperSeries),
78 m_lowerSeries(lowerSeries),
78 m_lowerSeries(lowerSeries),
79 m_pointsVisible(false)
79 m_pointsVisible(false)
80 {
80 {
81 }
81 }
82 /*!
82 /*!
83 Destroys the object. Series added to QChartView or QChart instances are owned by those,
83 Destroys the object. Series added to QChartView or QChart instances are owned by those,
84 and are deleted when mentioned object are destroyed.
84 and are deleted when mentioned object are destroyed.
85 */
85 */
86 QAreaSeries::~QAreaSeries()
86 QAreaSeries::~QAreaSeries()
87 {
87 {
88 }
88 }
89
89
90 /*!
90 /*!
91 Sets \a pen used for drawing area outline.
91 Sets \a pen used for drawing area outline.
92 */
92 */
93 void QAreaSeries::setPen(const QPen& pen)
93 void QAreaSeries::setPen(const QPen& pen)
94 {
94 {
95 m_pen=pen;
95 if(m_pen!=pen){
96 m_pen=pen;
97 emit updated();
98 }
96 }
99 }
97
100
98 /*!
101 /*!
99 Sets \a brush used for filling the area.
102 Sets \a brush used for filling the area.
100 */
103 */
101 void QAreaSeries::setBrush(const QBrush& brush)
104 void QAreaSeries::setBrush(const QBrush& brush)
102 {
105 {
103 m_brush=brush;
106 if(m_brush!=brush){
107 m_brush=brush;
108 emit updated();
109 }
104 }
110 }
105 /*!
111 /*!
106 Sets if data points are \a visible and should be drawn on line.
112 Sets if data points are \a visible and should be drawn on line.
107 */
113 */
108 void QAreaSeries::setPointsVisible(bool visible)
114 void QAreaSeries::setPointsVisible(bool visible)
109 {
115 {
110 m_pointsVisible=visible;
116 if(m_pointsVisible!=visible){
117 m_pointsVisible=visible;
118 emit updated();
119 }
111 }
120 }
112
121
113 #include "moc_qareaseries.cpp"
122 #include "moc_qareaseries.cpp"
114
123
115 QTCOMMERCIALCHART_END_NAMESPACE
124 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,48 +1,49
1 #ifndef QAREASERIES_H_
1 #ifndef QAREASERIES_H_
2 #define QAREASERIES_H_
2 #define QAREASERIES_H_
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qseries.h"
5 #include "qseries.h"
6 #include <QDebug>
6 #include <QDebug>
7 #include <QPen>
7 #include <QPen>
8 #include <QBrush>
8 #include <QBrush>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 class QLineSeries;
11 class QLineSeries;
12
12
13 class QTCOMMERCIALCHART_EXPORT QAreaSeries : public QSeries
13 class QTCOMMERCIALCHART_EXPORT QAreaSeries : public QSeries
14 {
14 {
15 Q_OBJECT
15 Q_OBJECT
16 public:
16 public:
17 QAreaSeries(QLineSeries* upperSeries,QLineSeries* lowerSeries=0);
17 QAreaSeries(QLineSeries* upperSeries,QLineSeries* lowerSeries=0);
18 virtual ~QAreaSeries();
18 virtual ~QAreaSeries();
19
19
20 public: // from QChartSeries
20 public: // from QChartSeries
21 virtual QSeriesType type() const { return QSeries::SeriesTypeArea;}
21 virtual QSeriesType type() const { return QSeries::SeriesTypeArea;}
22
22
23 QLineSeries* upperSeries() const { return m_upperSeries;}
23 QLineSeries* upperSeries() const { return m_upperSeries;}
24 QLineSeries* lowerSeries() const { return m_lowerSeries;}
24 QLineSeries* lowerSeries() const { return m_lowerSeries;}
25
25
26 void setPen(const QPen& pen);
26 void setPen(const QPen& pen);
27 QPen pen() const { return m_pen;}
27 QPen pen() const { return m_pen;}
28
28
29 void setBrush(const QBrush& brush);
29 void setBrush(const QBrush& brush);
30 QBrush brush() const { return m_brush;}
30 QBrush brush() const { return m_brush;}
31
31
32 void setPointsVisible(bool visible);
32 void setPointsVisible(bool visible);
33 bool pointsVisible() const {return m_pointsVisible;}
33 bool pointsVisible() const {return m_pointsVisible;}
34
34
35 signals:
35 signals:
36 void updated();
36 void updated();
37 void clicked(const QPointF& point);
37
38
38 private:
39 private:
39 QBrush m_brush;
40 QBrush m_brush;
40 QPen m_pen;
41 QPen m_pen;
41 QLineSeries* m_upperSeries;
42 QLineSeries* m_upperSeries;
42 QLineSeries* m_lowerSeries;
43 QLineSeries* m_lowerSeries;
43 bool m_pointsVisible;
44 bool m_pointsVisible;
44 };
45 };
45
46
46 QTCOMMERCIALCHART_END_NAMESPACE
47 QTCOMMERCIALCHART_END_NAMESPACE
47
48
48 #endif
49 #endif
@@ -1,88 +1,81
1 #include "linechartitem_p.h"
1 #include "linechartitem_p.h"
2 #include "qlineseries.h"
2 #include "qlineseries.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include <QPainter>
4 #include <QPainter>
5 #include <QGraphicsSceneMouseEvent>
6
5
7
6
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
8
10 //TODO: optimize : remove points which are not visible
9 //TODO: optimize : remove points which are not visible
11
10
12 LineChartItem::LineChartItem(QLineSeries* series,QGraphicsItem *parent):XYChartItem(series,parent),
11 LineChartItem::LineChartItem(QLineSeries* series,QGraphicsItem *parent):XYChartItem(series,parent),
13 m_series(series),
12 m_series(series),
14 m_pointsVisible(false)
13 m_pointsVisible(false)
15 {
14 {
16 setZValue(ChartPresenter::LineChartZValue);
15 setZValue(ChartPresenter::LineChartZValue);
17 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
16 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
18 QObject::connect(this,SIGNAL(clicked(const QPointF&)),series,SIGNAL(clicked(const QPointF&)));
19 handleUpdated();
17 handleUpdated();
20 }
18 }
21
19
22 QRectF LineChartItem::boundingRect() const
20 QRectF LineChartItem::boundingRect() const
23 {
21 {
24 return m_rect;
22 return m_rect;
25 }
23 }
26
24
27 QPainterPath LineChartItem::shape() const
25 QPainterPath LineChartItem::shape() const
28 {
26 {
29 return m_path;
27 return m_path;
30 }
28 }
31
29
32 void LineChartItem::setLayout(QVector<QPointF>& points)
30 void LineChartItem::setLayout(QVector<QPointF>& points)
33 {
31 {
34 if(points.size()==0)
32 if(points.size()==0)
35 {
33 {
36 XYChartItem::setLayout(points);
34 XYChartItem::setLayout(points);
37 return;
35 return;
38 }
36 }
39
37
40 QList<QGraphicsItem*> items = m_items.childItems();
38 QList<QGraphicsItem*> items = m_items.childItems();
41
39
42 QPainterPath linePath(points.at(0));
40 QPainterPath linePath(points.at(0));
43
41
44 for(int i=1; i< points.size();i++) {
42 for(int i=1; i< points.size();i++) {
45 linePath.lineTo(points.at(i));
43 linePath.lineTo(points.at(i));
46 }
44 }
47
45
48 prepareGeometryChange();
46 prepareGeometryChange();
49 m_path = linePath;
47 m_path = linePath;
50 m_rect = linePath.boundingRect();
48 m_rect = linePath.boundingRect();
51
49
52 XYChartItem::setLayout(points);
50 XYChartItem::setLayout(points);
53 }
51 }
54
52
55 void LineChartItem::handleUpdated()
53 void LineChartItem::handleUpdated()
56 {
54 {
57 m_pointsVisible = m_series->pointsVisible();
55 m_pointsVisible = m_series->pointsVisible();
58 m_linePen = m_series->pen();
56 m_linePen = m_series->pen();
59 m_pointPen = m_series->pen();
57 m_pointPen = m_series->pen();
60 m_pointPen.setWidthF(2*m_pointPen.width());
58 m_pointPen.setWidthF(2*m_pointPen.width());
61 update();
59 update();
62 }
60 }
63
61
64 //painter
62 //painter
65
63
66 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
64 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
67 {
65 {
68 Q_UNUSED(widget);
66 Q_UNUSED(widget);
69 Q_UNUSED(option);
67 Q_UNUSED(option);
70 painter->save();
68 painter->save();
71 painter->setPen(m_linePen);
69 painter->setPen(m_linePen);
72 painter->setClipRect(clipRect());
70 painter->setClipRect(clipRect());
73 painter->drawPath(m_path);
71 painter->drawPath(m_path);
74 if(m_pointsVisible){
72 if(m_pointsVisible){
75 painter->setPen(m_pointPen);
73 painter->setPen(m_pointPen);
76 painter->drawPoints(points());
74 painter->drawPoints(points());
77 }
75 }
78 painter->restore();
76 painter->restore();
79 }
77 }
80
78
81 void LineChartItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
82 {
83 emit clicked(calculateDomainPoint(event->pos()));
84 }
85
86 #include "moc_linechartitem_p.cpp"
79 #include "moc_linechartitem_p.cpp"
87
80
88 QTCOMMERCIALCHART_END_NAMESPACE
81 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,47 +1,43
1 #ifndef LINECHARTITEM_H
1 #ifndef LINECHARTITEM_H
2 #define LINECHARTITEM_H
2 #define LINECHARTITEM_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "xychartitem_p.h"
5 #include "xychartitem_p.h"
6 #include <QPen>
6 #include <QPen>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QLineSeries;
10 class QLineSeries;
11
11
12 class LineChartItem : public XYChartItem
12 class LineChartItem : public XYChartItem
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 public:
15 public:
16 explicit LineChartItem(QLineSeries* series,QGraphicsItem *parent = 0);
16 explicit LineChartItem(QLineSeries* series,QGraphicsItem *parent = 0);
17 ~ LineChartItem(){};
17 ~ LineChartItem(){};
18
18
19 //from QGraphicsItem
19 //from QGraphicsItem
20 QRectF boundingRect() const;
20 QRectF boundingRect() const;
21 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
21 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
22 QPainterPath shape() const;
22 QPainterPath shape() const;
23
23
24 public slots:
24 public slots:
25 void handleUpdated();
25 void handleUpdated();
26
26
27 signals:
28 void clicked(const QPointF& point);
29
30 protected:
27 protected:
31 void setLayout(QVector<QPointF>& points);
28 void setLayout(QVector<QPointF>& points);
32 void mousePressEvent( QGraphicsSceneMouseEvent * event );
33
29
34 private:
30 private:
35 QLineSeries* m_series;
31 QLineSeries* m_series;
36 QGraphicsItemGroup m_items;
32 QGraphicsItemGroup m_items;
37 QPainterPath m_path;
33 QPainterPath m_path;
38 QRectF m_rect;
34 QRectF m_rect;
39 QPen m_linePen;
35 QPen m_linePen;
40 QPen m_pointPen;
36 QPen m_pointPen;
41 bool m_pointsVisible;
37 bool m_pointsVisible;
42
38
43 };
39 };
44
40
45 QTCOMMERCIALCHART_END_NAMESPACE
41 QTCOMMERCIALCHART_END_NAMESPACE
46
42
47 #endif
43 #endif
@@ -1,98 +1,96
1 #include "qlineseries.h"
1 #include "qlineseries.h"
2
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
4
5 /*!
5 /*!
6 \class QLineSeries
6 \class QLineSeries
7 \brief The QLineSeries class is used for making line charts.
7 \brief The QLineSeries class is used for making line charts.
8
8
9 \mainclass
9 \mainclass
10
10
11 A line chart is used to show information as a series of data points
11 A line chart is used to show information as a series of data points
12 connected by straight lines.
12 connected by straight lines.
13
13
14 \image linechart.png
14 \image linechart.png
15
15
16 Creating basic line chart is simple:
16 Creating basic line chart is simple:
17 \code
17 \code
18 QLineSeries* series = new QLineSeries();
18 QLineSeries* series = new QLineSeries();
19 series->add(0, 6);
19 series->add(0, 6);
20 series->add(2, 4);
20 series->add(2, 4);
21 ...
21 ...
22 chartView->addSeries(series);
22 chartView->addSeries(series);
23 \endcode
23 \endcode
24 */
24 */
25
25
26 /*!
26 /*!
27 \fn virtual QSeriesType QLineSeries::type() const
27 \fn virtual QSeriesType QLineSeries::type() const
28 \brief Returns type of series.
28 \brief Returns type of series.
29 \sa QSeries, QSeriesType
29 \sa QSeries, QSeriesType
30 */
30 */
31
31
32 /*!
32 /*!
33 \fn bool QLineSeries::pointsVisible() const
33 \fn bool QLineSeries::pointsVisible() const
34 \brief Returns if the points are drawn for this series.
34 \brief Returns if the points are drawn for this series.
35 \sa setPointsVisible()
35 \sa setPointsVisible()
36 */
36 */
37
37
38 /*!
38 /*!
39 \fn QPen QLineSeries::linePen() const
39 \fn QPen QLineSeries::linePen() const
40 \brief Returns the pen used to draw line connecting points.
40 \brief Returns the pen used to draw line connecting points.
41 \sa setPen()
41 \sa setPen()
42 */
42 */
43
43
44 /*!
44 /*!
45 Constructs empty series object which is a child of \a parent.
45 Constructs empty series object which is a child of \a parent.
46 When series object is added to QChartView or QChart instance ownerships is transfered.
46 When series object is added to QChartView or QChart instance ownerships is transfered.
47 */
47 */
48 QLineSeries::QLineSeries(QObject* parent):QXYSeries(parent),
48 QLineSeries::QLineSeries(QObject* parent):QXYSeries(parent),
49 m_pointsVisible(false)
49 m_pointsVisible(false)
50 {
50 {
51
51
52 }
52 }
53 /*!
53 /*!
54 Destroys the object. Series added to QChartView or QChart instances are owned by those,
54 Destroys the object. Series added to QChartView or QChart instances are owned by those,
55 and are deleted when mentioned object are destroyed.
55 and are deleted when mentioned object are destroyed.
56 */
56 */
57 QLineSeries::~QLineSeries()
57 QLineSeries::~QLineSeries()
58 {
58 {
59 }
59 }
60
60
61 /*!
61 /*!
62 Sets \a pen used for drawing line connecting points.
62 Sets \a pen used for drawing line connecting points.
63 */
63 */
64 void QLineSeries::setLinePen(const QPen& pen)
64 void QLineSeries::setLinePen(const QPen& pen)
65 {
65 {
66 if(pen!=m_pen){
66 if(pen!=m_pen){
67 m_pen=pen;
67 m_pen=pen;
68 emit updated();
68 emit QXYSeries::updated();
69 }
69 }
70 }
70 }
71
71
72 /*!
72 /*!
73 Sets if data points are \a visible and should be drawn on line.
73 Sets if data points are \a visible and should be drawn on line.
74 */
74 */
75 void QLineSeries::setPointsVisible(bool visible)
75 void QLineSeries::setPointsVisible(bool visible)
76 {
76 {
77 if(m_pointsVisible!=visible){
77 if(m_pointsVisible!=visible){
78 m_pointsVisible=visible;
78 m_pointsVisible=visible;
79 emit updated();
79 emit QXYSeries::updated();
80 }
80 }
81 }
81 }
82
82
83
83
84 QDebug operator<< (QDebug debug, const QLineSeries series)
84 QDebug operator<< (QDebug debug, const QLineSeries series)
85 {
85 {
86 Q_ASSERT(series.m_x.size() == series.m_y.size());
86 Q_ASSERT(series.m_x.size() == series.m_y.size());
87
87
88 int size = series.m_x.size();
88 int size = series.m_x.size();
89
89
90 for (int i=0;i<size;i++) {
90 for (int i=0;i<size;i++) {
91 debug.nospace() << "(" << series.m_x.at(i) << ','<< series.m_y.at(i) << ") ";
91 debug.nospace() << "(" << series.m_x.at(i) << ','<< series.m_y.at(i) << ") ";
92 }
92 }
93 return debug.space();
93 return debug.space();
94 }
94 }
95
95
96 #include "moc_qlineseries.cpp"
97
98 QTCOMMERCIALCHART_END_NAMESPACE
96 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,39 +1,35
1 #ifndef QLINESERIES_H_
1 #ifndef QLINESERIES_H_
2 #define QLINESERIES_H_
2 #define QLINESERIES_H_
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qxyseries.h"
5 #include "qxyseries.h"
6 #include <QDebug>
6 #include <QDebug>
7 #include <QPen>
7 #include <QPen>
8 #include <QBrush>
8 #include <QBrush>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class QTCOMMERCIALCHART_EXPORT QLineSeries : public QXYSeries
12 class QTCOMMERCIALCHART_EXPORT QLineSeries : public QXYSeries
13 {
13 {
14 Q_OBJECT
15 public:
14 public:
16 QLineSeries(QObject* parent=0);
15 QLineSeries(QObject* parent=0);
17 virtual ~QLineSeries();
16 virtual ~QLineSeries();
18
17
19 void setLinePen(const QPen& pen);
18 void setLinePen(const QPen& pen);
20 QPen linePen() const {return m_pen;}
19 QPen linePen() const {return m_pen;}
21
20
22 void setPointsVisible(bool visible);
21 void setPointsVisible(bool visible);
23 bool pointsVisible() const {return m_pointsVisible;}
22 bool pointsVisible() const {return m_pointsVisible;}
24
23
25 signals:
26 void clicked(const QPointF& point);
27
28 public: // from QChartSeries
24 public: // from QChartSeries
29 virtual QSeriesType type() const {return QSeries::SeriesTypeLine;}
25 virtual QSeriesType type() const {return QSeries::SeriesTypeLine;}
30 friend QDebug operator<< (QDebug d, const QLineSeries series);
26 friend QDebug operator<< (QDebug d, const QLineSeries series);
31 private:
27 private:
32 QPen m_pen;
28 QPen m_pen;
33 bool m_pointsVisible;
29 bool m_pointsVisible;
34
30
35 };
31 };
36
32
37 QTCOMMERCIALCHART_END_NAMESPACE
33 QTCOMMERCIALCHART_END_NAMESPACE
38
34
39 #endif
35 #endif
@@ -1,333 +1,342
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 #include "qlegend.h"
3 #include "qlegend.h"
4 #include "chartpresenter_p.h"
4 #include "chartpresenter_p.h"
5 #include "chartdataset_p.h"
5 #include "chartdataset_p.h"
6 #include <QGraphicsScene>
6 #include <QGraphicsScene>
7 #include <QGraphicsSceneResizeEvent>
7 #include <QGraphicsSceneResizeEvent>
8 #include <QDebug>
8 #include <QDebug>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 /*!
12 /*!
13 \enum QChart::ChartTheme
13 \enum QChart::ChartTheme
14
14
15 This enum describes the theme used by the chart.
15 This enum describes the theme used by the chart.
16
16
17 \value ChartThemeDefault
17 \value ChartThemeDefault
18 \value ChartThemeVanilla
18 \value ChartThemeVanilla
19 \value ChartThemeIcy
19 \value ChartThemeIcy
20 \value ChartThemeGrayscale
20 \value ChartThemeGrayscale
21 \value ChartThemeScientific
21 \value ChartThemeScientific
22 */
22 */
23
23
24 /*!
24 /*!
25 \enum QChart::AnimationOption
25 \enum QChart::AnimationOption
26
26
27 For enabling/disabling animations. Defaults to NoAnimation.
27 For enabling/disabling animations. Defaults to NoAnimation.
28
28
29 \value NoAnimation
29 \value NoAnimation
30 \value GridAxisAnimations
30 \value GridAxisAnimations
31 \value SeriesAnimations
31 \value SeriesAnimations
32 \value AllAnimations
32 \value AllAnimations
33 */
33 */
34
34
35 /*!
35 /*!
36 \class QChart
36 \class QChart
37 \brief QtCommercial chart API.
37 \brief QtCommercial chart API.
38
38
39 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
39 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
40 representation of different types of QChartSeries and other chart related objects like
40 representation of different types of QChartSeries and other chart related objects like
41 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
41 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
42 convenience class QChartView instead of QChart.
42 convenience class QChartView instead of QChart.
43 \sa QChartView
43 \sa QChartView
44 */
44 */
45
45
46 /*!
46 /*!
47 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
47 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
48 */
48 */
49 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
49 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
50 m_backgroundItem(0),
50 m_backgroundItem(0),
51 m_titleItem(0),
51 m_titleItem(0),
52 m_legend(new QLegend(this)),
52 m_legend(new QLegend(this)),
53 m_dataset(new ChartDataSet(this)),
53 m_dataset(new ChartDataSet(this)),
54 m_presenter(new ChartPresenter(this,m_dataset))
54 m_presenter(new ChartPresenter(this,m_dataset))
55 {
55 {
56 connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
56 connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
57 connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
57 connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
58 }
58 }
59
59
60 /*!
60 /*!
61 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
61 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
62 */
62 */
63 QChart::~QChart()
63 QChart::~QChart()
64 {
64 {
65 disconnect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
65 disconnect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
66 disconnect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
66 disconnect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
67 }
67 }
68
68
69 /*!
69 /*!
70 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
70 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
71 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
71 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
72 the y axis).
72 the y axis).
73 */
73 */
74 void QChart::addSeries(QSeries* series, QChartAxis* axisY)
74 void QChart::addSeries(QSeries* series, QChartAxis* axisY)
75 {
75 {
76 m_dataset->addSeries(series, axisY);
76 m_dataset->addSeries(series, axisY);
77 }
77 }
78
78
79 /*!
79 /*!
80 Removes the \a series specified in a perameter from the QChartView.
80 Removes the \a series specified in a perameter from the QChartView.
81 It releses its ownership of the specified QChartSeries object.
81 It releses its ownership of the specified QChartSeries object.
82 It does not delete the pointed QChartSeries data object
82 It does not delete the pointed QChartSeries data object
83 \sa addSeries(), removeAllSeries()
83 \sa addSeries(), removeAllSeries()
84 */
84 */
85 void QChart::removeSeries(QSeries* series)
85 void QChart::removeSeries(QSeries* series)
86 {
86 {
87 m_dataset->removeSeries(series);
87 m_dataset->removeSeries(series);
88 }
88 }
89
89
90 /*!
90 /*!
91 Removes all the QChartSeries that have been added to the QChartView
91 Removes all the QChartSeries that have been added to the QChartView
92 It also deletes the pointed QChartSeries data objects
92 It also deletes the pointed QChartSeries data objects
93 \sa addSeries(), removeSeries()
93 \sa addSeries(), removeSeries()
94 */
94 */
95 void QChart::removeAllSeries()
95 void QChart::removeAllSeries()
96 {
96 {
97 m_dataset->removeAllSeries();
97 m_dataset->removeAllSeries();
98 }
98 }
99
99
100 /*!
100 /*!
101 Sets the \a brush that is used for painting the background of the chart area.
101 Sets the \a brush that is used for painting the background of the chart area.
102 */
102 */
103 void QChart::setChartBackgroundBrush(const QBrush& brush)
103 void QChart::setChartBackgroundBrush(const QBrush& brush)
104 {
104 {
105 createChartBackgroundItem();
105 createChartBackgroundItem();
106 m_backgroundItem->setBrush(brush);
106 m_backgroundItem->setBrush(brush);
107 m_backgroundItem->update();
107 m_backgroundItem->update();
108 }
108 }
109
109
110 /*!
110 /*!
111 Sets the \a pen that is used for painting the background of the chart area.
111 Sets the \a pen that is used for painting the background of the chart area.
112 */
112 */
113 void QChart::setChartBackgroundPen(const QPen& pen)
113 void QChart::setChartBackgroundPen(const QPen& pen)
114 {
114 {
115 createChartBackgroundItem();
115 createChartBackgroundItem();
116 m_backgroundItem->setPen(pen);
116 m_backgroundItem->setPen(pen);
117 m_backgroundItem->update();
117 m_backgroundItem->update();
118 }
118 }
119
119
120 /*!
120 /*!
121 Sets the chart \a title. The description text that is drawn above the chart.
121 Sets the chart \a title. The description text that is drawn above the chart.
122 */
122 */
123 void QChart::setChartTitle(const QString& title)
123 void QChart::setChartTitle(const QString& title)
124 {
124 {
125 createChartTitleItem();
125 createChartTitleItem();
126 m_titleItem->setText(title);
126 m_titleItem->setText(title);
127 updateLayout();
127 }
128 }
128
129
129 /*!
130 /*!
130 Returns the chart title. The description text that is drawn above the chart.
131 Returns the chart title. The description text that is drawn above the chart.
131 */
132 */
132 QString QChart::chartTitle() const
133 QString QChart::chartTitle() const
133 {
134 {
134 if(m_titleItem)
135 if(m_titleItem)
135 return m_titleItem->text();
136 return m_titleItem->text();
136 else
137 else
137 return QString();
138 return QString();
138 }
139 }
139
140
140 /*!
141 /*!
141 Sets the \a font that is used for rendering the description text that is rendered above the chart.
142 Sets the \a font that is used for rendering the description text that is rendered above the chart.
142 */
143 */
143 void QChart::setChartTitleFont(const QFont& font)
144 void QChart::setChartTitleFont(const QFont& font)
144 {
145 {
145 createChartTitleItem();
146 createChartTitleItem();
146 m_titleItem->setFont(font);
147 m_titleItem->setFont(font);
148 updateLayout();
147 }
149 }
148
150
149 /*!
151 /*!
150 Sets the \a brush used for rendering the title text.
152 Sets the \a brush used for rendering the title text.
151 */
153 */
152 void QChart::setChartTitleBrush(const QBrush &brush)
154 void QChart::setChartTitleBrush(const QBrush &brush)
153 {
155 {
154 createChartTitleItem();
156 createChartTitleItem();
155 m_titleItem->setBrush(brush);
157 m_titleItem->setBrush(brush);
158 updateLayout();
156 }
159 }
157
160
158 /*!
161 /*!
159 Returns the brush used for rendering the title text.
162 Returns the brush used for rendering the title text.
160 */
163 */
161 QBrush QChart::chartTitleBrush()
164 QBrush QChart::chartTitleBrush()
162 {
165 {
163 createChartTitleItem();
166 createChartTitleItem();
164 return m_titleItem->brush();
167 return m_titleItem->brush();
165 }
168 }
166
169
167 void QChart::createChartBackgroundItem()
170 void QChart::createChartBackgroundItem()
168 {
171 {
169 if(!m_backgroundItem) {
172 if(!m_backgroundItem) {
170 m_backgroundItem = new QGraphicsRectItem(this);
173 m_backgroundItem = new QGraphicsRectItem(this);
171 m_backgroundItem->setPen(Qt::NoPen);
174 m_backgroundItem->setPen(Qt::NoPen);
172 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
175 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
173 }
176 }
174 }
177 }
175
178
176 void QChart::createChartTitleItem()
179 void QChart::createChartTitleItem()
177 {
180 {
178 if(!m_titleItem) {
181 if(!m_titleItem) {
179 m_titleItem = new QGraphicsSimpleTextItem(this);
182 m_titleItem = new QGraphicsSimpleTextItem(this);
180 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
183 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
181 }
184 }
182 }
185 }
183
186
184 /*!
187 /*!
185 Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
188 Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
186 \sa setMargin()
189 \sa setMargin()
187 */
190 */
188 int QChart::margin() const
191 int QChart::margin() const
189 {
192 {
190 return m_presenter->margin();
193 return m_presenter->margin();
191 }
194 }
192
195
193 /*!
196 /*!
194 Sets the chart \a margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
197 Sets the chart \a margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
195 \sa margin()
198 \sa margin()
196 */
199 */
197 void QChart::setMargin(int margin)
200 void QChart::setMargin(int margin)
198 {
201 {
199 m_presenter->setMargin(margin);
202 m_presenter->setMargin(margin);
203 updateLayout();
200 }
204 }
201
205
202 /*!
206 /*!
203 Sets the \a theme used by the chart for rendering the graphical representation of the data
207 Sets the \a theme used by the chart for rendering the graphical representation of the data
204 \sa ChartTheme, chartTheme()
208 \sa ChartTheme, chartTheme()
205 */
209 */
206 void QChart::setChartTheme(QChart::ChartTheme theme)
210 void QChart::setChartTheme(QChart::ChartTheme theme)
207 {
211 {
208 m_presenter->setChartTheme(theme);
212 m_presenter->setChartTheme(theme);
209 }
213 }
210
214
211 /*!
215 /*!
212 Returns the theme enum used by the chart.
216 Returns the theme enum used by the chart.
213 \sa ChartTheme, setChartTheme()
217 \sa ChartTheme, setChartTheme()
214 */
218 */
215 QChart::ChartTheme QChart::chartTheme() const
219 QChart::ChartTheme QChart::chartTheme() const
216 {
220 {
217 return m_presenter->chartTheme();
221 return m_presenter->chartTheme();
218 }
222 }
219
223
220 /*!
224 /*!
221 Zooms in the view by a factor of 2
225 Zooms in the view by a factor of 2
222 */
226 */
223 void QChart::zoomIn()
227 void QChart::zoomIn()
224 {
228 {
225 m_presenter->zoomIn();
229 m_presenter->zoomIn();
226 }
230 }
227
231
228 /*!
232 /*!
229 Zooms in the view to a maximum level at which \a rect is still fully visible.
233 Zooms in the view to a maximum level at which \a rect is still fully visible.
230 */
234 */
231 void QChart::zoomIn(const QRectF& rect)
235 void QChart::zoomIn(const QRectF& rect)
232 {
236 {
233
237
234 if(!rect.isValid()) return;
238 if(!rect.isValid()) return;
235 m_presenter->zoomIn(rect);
239 m_presenter->zoomIn(rect);
236 }
240 }
237
241
238 /*!
242 /*!
239 Restores the view zoom level to the previous one.
243 Restores the view zoom level to the previous one.
240 */
244 */
241 void QChart::zoomOut()
245 void QChart::zoomOut()
242 {
246 {
243 m_presenter->zoomOut();
247 m_presenter->zoomOut();
244 }
248 }
245
249
246 /*!
250 /*!
247 Resets to the default view.
251 Resets to the default view.
248 */
252 */
249 void QChart::zoomReset()
253 void QChart::zoomReset()
250 {
254 {
251 m_presenter->zoomReset();
255 m_presenter->zoomReset();
252 }
256 }
253
257
254 /*!
258 /*!
255 Returns the pointer to the x axis object of the chart
259 Returns the pointer to the x axis object of the chart
256 */
260 */
257 QChartAxis* QChart::axisX() const
261 QChartAxis* QChart::axisX() const
258 {
262 {
259 return m_dataset->axisX();
263 return m_dataset->axisX();
260 }
264 }
261
265
262 /*!
266 /*!
263 Returns the pointer to the y axis object of the chart
267 Returns the pointer to the y axis object of the chart
264 */
268 */
265 QChartAxis* QChart::axisY() const
269 QChartAxis* QChart::axisY() const
266 {
270 {
267 return m_dataset->axisY();
271 return m_dataset->axisY();
268 }
272 }
269
273
270 /*!
274 /*!
271 Returns the legend object of the chart
275 Returns the legend object of the chart
272 */
276 */
273 QLegend* QChart::legend()
277 QLegend* QChart::legend()
274 {
278 {
275 return m_legend;
279 return m_legend;
276 }
280 }
277
281
278 /*!
282 /*!
279 Resizes and updates the chart area using the \a event data
283 Resizes and updates the chart area using the \a event data
280 */
284 */
281 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
285 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
282 {
286 {
283
287
284 m_rect = QRectF(QPoint(0,0),event->newSize());
288 m_rect = QRectF(QPoint(0,0),event->newSize());
285 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
289 updateLayout();
286
287 // recalculate title position
288 if (m_titleItem) {
289 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
290 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
291 }
292
293 //recalculate background gradient
294 if (m_backgroundItem) {
295 m_backgroundItem->setRect(rect);
296 }
297
298 QGraphicsWidget::resizeEvent(event);
290 QGraphicsWidget::resizeEvent(event);
299 update();
291 update();
300 }
292 }
301
293
302 /*!
294 /*!
303 Sets animation \a options for the chart
295 Sets animation \a options for the chart
304 */
296 */
305 void QChart::setAnimationOptions(AnimationOptions options)
297 void QChart::setAnimationOptions(AnimationOptions options)
306 {
298 {
307 m_presenter->setAnimationOptions(options);
299 m_presenter->setAnimationOptions(options);
308 }
300 }
309
301
310 /*!
302 /*!
311 Returns animation options for the chart
303 Returns animation options for the chart
312 */
304 */
313 QChart::AnimationOptions QChart::animationOptions() const
305 QChart::AnimationOptions QChart::animationOptions() const
314 {
306 {
315 return m_presenter->animationOptions();
307 return m_presenter->animationOptions();
316 }
308 }
317
309
318 void QChart::scroll(int dx,int dy)
310 void QChart::scroll(int dx,int dy)
319 {
311 {
320 //temporary
312 //temporary
321 if(dx>0)
313 if(dx>0)
322 m_presenter->scroll(m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
314 m_presenter->scroll(m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
323 if(dx<0)
315 if(dx<0)
324 m_presenter->scroll(-m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
316 m_presenter->scroll(-m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
325 if(dy>0)
317 if(dy>0)
326 m_presenter->scroll(0,m_presenter->geometry().width()/(axisY()->ticksCount()-1));
318 m_presenter->scroll(0,m_presenter->geometry().width()/(axisY()->ticksCount()-1));
327 if(dy<0)
319 if(dy<0)
328 m_presenter->scroll(0,-m_presenter->geometry().width()/(axisY()->ticksCount()-1));
320 m_presenter->scroll(0,-m_presenter->geometry().width()/(axisY()->ticksCount()-1));
329 }
321 }
330
322
323 void QChart::updateLayout()
324 {
325 if(!m_rect.isValid()) return;
326
327 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
328
329 // recalculate title position
330 if (m_titleItem) {
331 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
332 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
333 }
334
335 //recalculate background gradient
336 if (m_backgroundItem) {
337 m_backgroundItem->setRect(rect);
338 }
339 }
331 #include "moc_qchart.cpp"
340 #include "moc_qchart.cpp"
332
341
333 QTCOMMERCIALCHART_END_NAMESPACE
342 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,103 +1,104
1 #ifndef QCHART_H
1 #ifndef QCHART_H
2 #define QCHART_H
2 #define QCHART_H
3
3
4 #include <qchartglobal.h>
4 #include <qchartglobal.h>
5 #include <qseries.h>
5 #include <qseries.h>
6 #include <QGraphicsWidget>
6 #include <QGraphicsWidget>
7 #include <QLinearGradient>
7 #include <QLinearGradient>
8 #include <QFont>
8 #include <QFont>
9
9
10 class QGraphicsSceneResizeEvent;
10 class QGraphicsSceneResizeEvent;
11
11
12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13
13
14 class AxisItem;
14 class AxisItem;
15 class QSeries;
15 class QSeries;
16 class PlotDomain;
16 class PlotDomain;
17 class BarPresenter;
17 class BarPresenter;
18 class QChartAxis;
18 class QChartAxis;
19 class ChartTheme;
19 class ChartTheme;
20 class ChartItem;
20 class ChartItem;
21 class ChartDataSet;
21 class ChartDataSet;
22 class ChartPresenter;
22 class ChartPresenter;
23 class QLegend;
23 class QLegend;
24
24
25 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
25 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
26 {
26 {
27 Q_OBJECT
27 Q_OBJECT
28 public:
28 public:
29 enum ChartTheme {
29 enum ChartTheme {
30 ChartThemeDefault,
30 ChartThemeDefault,
31 ChartThemeVanilla,
31 ChartThemeVanilla,
32 ChartThemeIcy,
32 ChartThemeIcy,
33 ChartThemeGrayscale,
33 ChartThemeGrayscale,
34 ChartThemeScientific
34 ChartThemeScientific
35 //ChartThemeUnnamed1
35 //ChartThemeUnnamed1
36 /*! The default theme follows the GUI style of the Operating System */
36 /*! The default theme follows the GUI style of the Operating System */
37 };
37 };
38
38
39 enum AnimationOption {
39 enum AnimationOption {
40 NoAnimation = 0x0,
40 NoAnimation = 0x0,
41 GridAxisAnimations = 0x1,
41 GridAxisAnimations = 0x1,
42 SeriesAnimations =0x2,
42 SeriesAnimations =0x2,
43 AllAnimations = 0x3
43 AllAnimations = 0x3
44 };
44 };
45 Q_DECLARE_FLAGS(AnimationOptions, AnimationOption)
45 Q_DECLARE_FLAGS(AnimationOptions, AnimationOption)
46
46
47 public:
47 public:
48 QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
48 QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
49 ~QChart();
49 ~QChart();
50
50
51 void addSeries(QSeries* series, QChartAxis* axisY = 0);
51 void addSeries(QSeries* series, QChartAxis* axisY = 0);
52 void removeSeries(QSeries* series); //returns ownership , deletes axis if no series attached
52 void removeSeries(QSeries* series); //returns ownership , deletes axis if no series attached
53 void removeAllSeries(); // deletes series and axis
53 void removeAllSeries(); // deletes series and axis
54
54
55 void setMargin(int margin);
55 void setMargin(int margin);
56 int margin() const;
56 int margin() const;
57 void setChartTheme(QChart::ChartTheme theme);
57 void setChartTheme(QChart::ChartTheme theme);
58 QChart::ChartTheme chartTheme() const;
58 QChart::ChartTheme chartTheme() const;
59
59
60 void setChartTitle(const QString& title);
60 void setChartTitle(const QString& title);
61 QString chartTitle() const;
61 QString chartTitle() const;
62 void setChartTitleFont(const QFont& font);
62 void setChartTitleFont(const QFont& font);
63 void setChartTitleBrush(const QBrush &brush);
63 void setChartTitleBrush(const QBrush &brush);
64 QBrush chartTitleBrush();
64 QBrush chartTitleBrush();
65 void setChartBackgroundBrush(const QBrush& brush);
65 void setChartBackgroundBrush(const QBrush& brush);
66 void setChartBackgroundPen(const QPen& pen);
66 void setChartBackgroundPen(const QPen& pen);
67
67
68 void setAnimationOptions(AnimationOptions options);
68 void setAnimationOptions(AnimationOptions options);
69 AnimationOptions animationOptions() const;
69 AnimationOptions animationOptions() const;
70
70
71 void zoomIn();
71 void zoomIn();
72 void zoomIn(const QRectF& rect);
72 void zoomIn(const QRectF& rect);
73 void zoomOut();
73 void zoomOut();
74 void zoomReset();
74 void zoomReset();
75 void scroll(int dx,int dy);
75 void scroll(int dx,int dy);
76
76
77 QChartAxis* axisX() const;
77 QChartAxis* axisX() const;
78 QChartAxis* axisY() const;
78 QChartAxis* axisY() const;
79
79
80 QLegend* legend();
80 QLegend* legend();
81
81
82 protected:
82 protected:
83 void resizeEvent(QGraphicsSceneResizeEvent *event);
83 void resizeEvent(QGraphicsSceneResizeEvent *event);
84
84
85 private:
85 private:
86 inline void createChartBackgroundItem();
86 inline void createChartBackgroundItem();
87 inline void createChartTitleItem();
87 inline void createChartTitleItem();
88 void updateLayout();
88
89
89 private:
90 private:
90 Q_DISABLE_COPY(QChart)
91 Q_DISABLE_COPY(QChart)
91 QGraphicsRectItem* m_backgroundItem;
92 QGraphicsRectItem* m_backgroundItem;
92 QGraphicsSimpleTextItem* m_titleItem;
93 QGraphicsSimpleTextItem* m_titleItem;
93 QRectF m_rect;
94 QRectF m_rect;
94 QLegend* m_legend;
95 QLegend* m_legend;
95 ChartDataSet *m_dataset;
96 ChartDataSet *m_dataset;
96 ChartPresenter *m_presenter;
97 ChartPresenter *m_presenter;
97 };
98 };
98
99
99 QTCOMMERCIALCHART_END_NAMESPACE
100 QTCOMMERCIALCHART_END_NAMESPACE
100
101
101 Q_DECLARE_OPERATORS_FOR_FLAGS(QTCOMMERCIALCHART_NAMESPACE::QChart::AnimationOptions)
102 Q_DECLARE_OPERATORS_FOR_FLAGS(QTCOMMERCIALCHART_NAMESPACE::QChart::AnimationOptions)
102
103
103 #endif
104 #endif
@@ -1,103 +1,99
1 #include "qscatterseries.h"
1 #include "qscatterseries.h"
2 #include "qchart.h"
2 #include "qchart.h"
3
3
4 /*!
4 /*!
5 \class QScatterSeries
5 \class QScatterSeries
6 \brief The QScatterSeries class is used for making scatter charts.
6 \brief The QScatterSeries class is used for making scatter charts.
7
7
8 \mainclass
8 \mainclass
9
9
10 The scatter data is displayed as a collection of points on the chart. Each point determines the position on the horizontal axis
10 The scatter data is displayed as a collection of points on the chart. Each point determines the position on the horizontal axis
11 and the vertical axis.
11 and the vertical axis.
12
12
13 \image scatterchart.png
13 \image scatterchart.png
14
14
15 Creating basic scatter chart is simple:
15 Creating basic scatter chart is simple:
16 \code
16 \code
17 QScatterSeries* series = new QScatterSeries();
17 QScatterSeries* series = new QScatterSeries();
18 series->add(0, 6);
18 series->add(0, 6);
19 series->add(2, 4);
19 series->add(2, 4);
20 ...
20 ...
21 chartView->addSeries(series);
21 chartView->addSeries(series);
22 \endcode
22 \endcode
23 */
23 */
24
24
25 /*!
25 /*!
26 \enum QScatterSeries::MarkerShape
26 \enum QScatterSeries::MarkerShape
27
27
28 This enum describes the shape used when rendering marker items.
28 This enum describes the shape used when rendering marker items.
29
29
30 \value MarkerShapeCircle
30 \value MarkerShapeCircle
31 \value MarkerShapeRectangle
31 \value MarkerShapeRectangle
32 */
32 */
33
33
34 /*!
34 /*!
35 \fn QChartSeriesType QScatterSeries::type() const
35 \fn QChartSeriesType QScatterSeries::type() const
36 \brief Returns QChartSeries::SeriesTypeScatter.
36 \brief Returns QChartSeries::SeriesTypeScatter.
37 \sa QSeries, QSeriesType
37 \sa QSeries, QSeriesType
38 */
38 */
39
39
40 /*!
40 /*!
41 \fn void QScatterSeries::clicked(const QPointF& point)
41 \fn void QScatterSeries::clicked(const QPointF& point)
42 \brief Signal is emitted when user clicks the \a point on scatter chart.
42 \brief Signal is emitted when user clicks the \a point on scatter chart.
43 */
43 */
44
44
45 QTCOMMERCIALCHART_BEGIN_NAMESPACE
45 QTCOMMERCIALCHART_BEGIN_NAMESPACE
46
46
47 /*!
47 /*!
48 Constructs a series object which is a child of \a parent.
48 Constructs a series object which is a child of \a parent.
49 */
49 */
50 QScatterSeries::QScatterSeries(QObject *parent) :
50 QScatterSeries::QScatterSeries(QObject *parent) :
51 QXYSeries(parent),
51 QXYSeries(parent),
52 m_shape(QScatterSeries::MarkerShapeCircle),
52 m_shape(QScatterSeries::MarkerShapeCircle),
53 m_size(9.0)
53 m_size(9.0)
54 {
54 {
55 }
55 }
56
56
57 /*!
57 /*!
58 Destroys the object. Note that adding series to QChart transfers the ownership to the chart.
58 Destroys the object. Note that adding series to QChart transfers the ownership to the chart.
59 */
59 */
60 QScatterSeries::~QScatterSeries()
60 QScatterSeries::~QScatterSeries()
61 {
61 {
62 }
62 }
63
63
64 /*!
64 /*!
65 Returns the shape used for drawing markers.
65 Returns the shape used for drawing markers.
66 */
66 */
67 QScatterSeries::MarkerShape QScatterSeries::shape() const
67 QScatterSeries::MarkerShape QScatterSeries::shape() const
68 {
68 {
69 return (QScatterSeries::MarkerShape) m_shape;
69 return (QScatterSeries::MarkerShape) m_shape;
70 }
70 }
71
71
72 /*!
72 /*!
73 Overrides the default shape of the marker items with a user defined \a shape. The default shape
73 Overrides the default shape of the marker items with a user defined \a shape. The default shape
74 is defined by chart theme setting.
74 is defined by chart theme setting.
75 */
75 */
76 void QScatterSeries::setShape(MarkerShape shape)
76 void QScatterSeries::setShape(MarkerShape shape)
77 {
77 {
78 m_shape = shape;
78 m_shape = shape;
79 emit updated();
79 emit updated();
80 }
80 }
81
81
82 /*!
82 /*!
83 Returns the size of the marker items.
83 Returns the size of the marker items.
84 */
84 */
85 qreal QScatterSeries::size() const
85 qreal QScatterSeries::size() const
86 {
86 {
87 return m_size;
87 return m_size;
88 }
88 }
89
89
90 /*!
90 /*!
91 Set the \a size of the marker items. The default size is 9.0.
91 Set the \a size of the marker items. The default size is 9.0.
92 */
92 */
93 void QScatterSeries::setSize(qreal size)
93 void QScatterSeries::setSize(qreal size)
94 {
94 {
95 m_size = size;
95 m_size = size;
96 emit updated();
96 emit updated();
97 }
97 }
98
98
99
100
101 #include "moc_qscatterseries.cpp"
102
103 QTCOMMERCIALCHART_END_NAMESPACE
99 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,45 +1,41
1 #ifndef QSCATTERSERIES_H
1 #ifndef QSCATTERSERIES_H
2 #define QSCATTERSERIES_H
2 #define QSCATTERSERIES_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qxyseries.h"
5 #include "qxyseries.h"
6 #include <QRectF>
6 #include <QRectF>
7 #include <QColor>
7 #include <QColor>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 class QScatterSeriesPrivate;
10 class QScatterSeriesPrivate;
11
11
12 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QXYSeries
12 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QXYSeries
13 {
13 {
14 Q_OBJECT
15
14
16 public:
15 public:
17 enum MarkerShape {
16 enum MarkerShape {
18 MarkerShapeCircle,
17 MarkerShapeCircle,
19 MarkerShapeRectangle
18 MarkerShapeRectangle
20 };
19 };
21
20
22 public:
21 public:
23 QScatterSeries(QObject *parent = 0);
22 QScatterSeries(QObject *parent = 0);
24 ~QScatterSeries();
23 ~QScatterSeries();
25
24
26 public: // from QChartSeries
25 public: // from QChartSeries
27 QSeriesType type() const { return QSeries::SeriesTypeScatter; }
26 QSeriesType type() const { return QSeries::SeriesTypeScatter; }
28
27
29 public:
28 public:
30 MarkerShape shape() const;
29 MarkerShape shape() const;
31 void setShape(MarkerShape shape);
30 void setShape(MarkerShape shape);
32 qreal size() const;
31 qreal size() const;
33 void setSize(qreal size);
32 void setSize(qreal size);
34
33
35 signals:
36 void clicked(const QPointF& point);
37
38 private:
34 private:
39 MarkerShape m_shape;
35 MarkerShape m_shape;
40 qreal m_size;
36 qreal m_size;
41 };
37 };
42
38
43 QTCOMMERCIALCHART_END_NAMESPACE
39 QTCOMMERCIALCHART_END_NAMESPACE
44
40
45 #endif // QSCATTERSERIES_H
41 #endif // QSCATTERSERIES_H
@@ -1,181 +1,184
1 #include "scatterchartitem_p.h"
1 #include "scatterchartitem_p.h"
2 #include "qscatterseries.h"
2 #include "qscatterseries.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include <QPainter>
4 #include <QPainter>
5 #include <QGraphicsScene>
5 #include <QGraphicsScene>
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15 ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent) :
15 ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent) :
16 XYChartItem(series,parent),
16 XYChartItem(series,parent),
17 m_series(series),
17 m_series(series),
18 m_items(this),
18 m_items(this),
19 m_shape(QScatterSeries::MarkerShapeRectangle),
19 m_shape(QScatterSeries::MarkerShapeRectangle),
20 m_size(10)
20 m_size(10)
21
21
22 {
22 {
23 Q_ASSERT(parent);
23 Q_ASSERT(parent);
24 Q_ASSERT(series);
24 Q_ASSERT(series);
25
25
26 QObject::connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated()));
26 QObject::connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated()));
27 QObject::connect(this,SIGNAL(clicked(const QPointF&)), m_series, SIGNAL(clicked(const QPointF&)));
28
27
29 setZValue(ChartPresenter::ScatterSeriesZValue);
28 setZValue(ChartPresenter::ScatterSeriesZValue);
30 setFlags(QGraphicsItem::ItemHasNoContents);
29 setFlags(QGraphicsItem::ItemHasNoContents);
31 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
30 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
32
31
33 handleUpdated();
32 handleUpdated();
34
33
35 m_items.setHandlesChildEvents(false);
34 m_items.setHandlesChildEvents(false);
36
35
37 // TODO: how to draw a drop shadow?
36 // TODO: how to draw a drop shadow?
38 // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect();
37 // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect();
39 // dropShadow->setOffset(2.0);
38 // dropShadow->setOffset(2.0);
40 // dropShadow->setBlurRadius(2.0);
39 // dropShadow->setBlurRadius(2.0);
41 // setGraphicsEffect(dropShadow);
40 // setGraphicsEffect(dropShadow);
42 }
41 }
43
42
44
43
45 QRectF ScatterChartItem::boundingRect() const
44 QRectF ScatterChartItem::boundingRect() const
46 {
45 {
47 return m_rect;
46 return m_rect;
48 }
47 }
49
48
50 void ScatterChartItem::createPoints(int count)
49 void ScatterChartItem::createPoints(int count)
51 {
50 {
52 for (int i = 0; i < count; ++i) {
51 for (int i = 0; i < count; ++i) {
53
52
54 QGraphicsItem *item;
53 QGraphicsItem *item;
55
54
56 switch (m_shape) {
55 switch (m_shape) {
57 case QScatterSeries::MarkerShapeCircle:{
56 case QScatterSeries::MarkerShapeCircle:{
58 QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size);
57 QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size);
59 const QRectF& rect = i->boundingRect();
58 const QRectF& rect = i->boundingRect();
60 i->setPos(-rect.width()/2,-rect.height()/2);
59 i->setPos(-rect.width()/2,-rect.height()/2);
61 item = new Marker(i,this);
60 item = new Marker(i,this);
62 break;
61 break;
63 }
62 }
64 case QScatterSeries::MarkerShapeRectangle:{
63 case QScatterSeries::MarkerShapeRectangle:{
65 QGraphicsRectItem* i = new QGraphicsRectItem(0,0,m_size,m_size);
64 QGraphicsRectItem* i = new QGraphicsRectItem(0,0,m_size,m_size);
66 i->setPos(-m_size/2,-m_size/2);
65 i->setPos(-m_size/2,-m_size/2);
67 item = new Marker(i,this);
66 item = new Marker(i,this);
68 break;
67 break;
69 }
68 }
70 default:
69 default:
71 qWarning()<<"Unsupported marker type";
70 qWarning()<<"Unsupported marker type";
72 break;
71 break;
73
72
74 }
73 }
75 m_items.addToGroup(item);
74 m_items.addToGroup(item);
76 }
75 }
77 }
76 }
78
77
79 void ScatterChartItem::deletePoints(int count)
78 void ScatterChartItem::deletePoints(int count)
80 {
79 {
81 QList<QGraphicsItem *> items = m_items.childItems();
80 QList<QGraphicsItem *> items = m_items.childItems();
82
81
83 for (int i = 0; i < count; ++i) {
82 for (int i = 0; i < count; ++i) {
84 delete(items.takeLast());
83 delete(items.takeLast());
85 }
84 }
86 }
85 }
87
86
88 void ScatterChartItem::markerSelected(Marker* marker)
87 void ScatterChartItem::markerSelected(Marker* marker)
89 {
88 {
90 emit clicked(QPointF(m_series->x(marker->index()), m_series->y(marker->index())));
89 emit XYChartItem::clicked(QPointF(m_series->x(marker->index()), m_series->y(marker->index())));
91 }
90 }
92
91
93 void ScatterChartItem::setLayout(QVector<QPointF>& points)
92 void ScatterChartItem::setLayout(QVector<QPointF>& points)
94 {
93 {
95 if(points.size()==0)
94 if(points.size()==0)
96 {
95 {
97 XYChartItem::setLayout(points);
96 XYChartItem::setLayout(points);
98 return;
97 return;
99 }
98 }
100
99
101 int diff = XYChartItem::points().size() - points.size();
100 int diff = XYChartItem::points().size() - points.size();
102
101
103 if(diff>0) {
102 if(diff>0) {
104 deletePoints(diff);
103 deletePoints(diff);
105 }
104 }
106 else if(diff<0) {
105 else if(diff<0) {
107 createPoints(-diff);
106 createPoints(-diff);
108 }
107 }
109
108
110 if(diff!=0) handleUpdated();
109 if(diff!=0) handleUpdated();
111
110
112 QList<QGraphicsItem*> items = m_items.childItems();
111 QList<QGraphicsItem*> items = m_items.childItems();
113
112
114 for(int i=0; i< points.size();i++) {
113 for(int i=0; i< points.size();i++) {
115 Marker* item = static_cast<Marker*>(items.at(i));
114 Marker* item = static_cast<Marker*>(items.at(i));
116 const QPointF& point = points.at(i);
115 const QPointF& point = points.at(i);
117 const QRectF& rect = item->boundingRect();
116 const QRectF& rect = item->boundingRect();
118 item->setIndex(i);
117 item->setIndex(i);
119 item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2);
118 item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2);
120 if(!clipRect().contains(point)) {
119 if(!clipRect().contains(point)) {
121 item->setVisible(false);
120 item->setVisible(false);
122 }
121 }
123 else {
122 else {
124 item->setVisible(true);
123 item->setVisible(true);
125 }
124 }
126 }
125 }
127
126
128 prepareGeometryChange();
127 prepareGeometryChange();
129 m_rect = clipRect();
128 m_rect = clipRect();
130 XYChartItem::setLayout(points);
129 XYChartItem::setLayout(points);
131 }
130 }
132
131
133
132
134 void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
133 void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
135 {
134 {
136 Q_UNUSED(painter);
135 Q_UNUSED(painter);
137 Q_UNUSED(option);
136 Q_UNUSED(option);
138 Q_UNUSED(widget);
137 Q_UNUSED(widget);
139 }
138 }
140
139
141 void ScatterChartItem::setPen(const QPen& pen)
140 void ScatterChartItem::setPen(const QPen& pen)
142 {
141 {
143 foreach(QGraphicsItem* item , m_items.childItems()) {
142 foreach(QGraphicsItem* item , m_items.childItems()) {
144 static_cast<Marker*>(item)->setPen(pen);
143 static_cast<Marker*>(item)->setPen(pen);
145 }
144 }
146 }
145 }
147
146
148 void ScatterChartItem::setBrush(const QBrush& brush)
147 void ScatterChartItem::setBrush(const QBrush& brush)
149 {
148 {
150 foreach(QGraphicsItem* item , m_items.childItems()) {
149 foreach(QGraphicsItem* item , m_items.childItems()) {
151 static_cast<Marker*>(item)->setBrush(brush);
150 static_cast<Marker*>(item)->setBrush(brush);
152 }
151 }
153 }
152 }
154
153
155 void ScatterChartItem::handleUpdated()
154 void ScatterChartItem::handleUpdated()
156 {
155 {
157
156
158 int count = m_items.childItems().count();
157 int count = m_items.childItems().count();
159
158
160 if(count==0) return;
159 if(count==0) return;
161
160
162 bool recreate = m_size != m_series->size() || m_shape != m_series->shape();
161 bool recreate = m_size != m_series->size() || m_shape != m_series->shape();
163
162
164 //TODO: only rewrite on size change
163 //TODO: only rewrite on size change
165
164
166 m_size = m_series->size();
165 m_size = m_series->size();
167 m_shape = m_series->shape();
166 m_shape = m_series->shape();
168
167
169 if(recreate){
168 if(recreate){
170 deletePoints(count);
169 deletePoints(count);
171 createPoints(count);
170 createPoints(count);
172 }
171 }
173
172
174 setPen(m_series->pen());
173 setPen(m_series->pen());
175 setBrush(m_series->brush());
174 setBrush(m_series->brush());
176
175
177 }
176 }
178
177
178 void ScatterChartItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
179 {
180 }
181
179 #include "moc_scatterchartitem_p.cpp"
182 #include "moc_scatterchartitem_p.cpp"
180
183
181 QTCOMMERCIALCHART_END_NAMESPACE
184 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,122 +1,120
1 #ifndef SCATTERPRESENTER_H
1 #ifndef SCATTERPRESENTER_H
2 #define SCATTERPRESENTER_H
2 #define SCATTERPRESENTER_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "xychartitem_p.h"
5 #include "xychartitem_p.h"
6 #include <QGraphicsEllipseItem>
6 #include <QGraphicsEllipseItem>
7 #include <QPen>
7 #include <QPen>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 class QScatterSeries;
11 class QScatterSeries;
12 class Marker;
12 class Marker;
13
13
14 class ScatterChartItem : public XYChartItem
14 class ScatterChartItem : public XYChartItem
15 {
15 {
16 Q_OBJECT
16 Q_OBJECT
17 public:
17 public:
18 explicit ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent = 0);
18 explicit ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent = 0);
19
19
20 public:
20 public:
21 //from QGraphicsItem
21 //from QGraphicsItem
22 QRectF boundingRect() const;
22 QRectF boundingRect() const;
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
24
24
25 void setPen(const QPen& pen);
25 void setPen(const QPen& pen);
26 void setBrush(const QBrush& brush);
26 void setBrush(const QBrush& brush);
27
27
28 void markerSelected(Marker* item);
28 void markerSelected(Marker* item);
29
29
30 signals:
31 void clicked(const QPointF& point);
32
33 public slots:
30 public slots:
34 void handleUpdated();
31 void handleUpdated();
35
32
36 private:
33 private:
37 void createPoints(int count);
34 void createPoints(int count);
38 void deletePoints(int count);
35 void deletePoints(int count);
39
36
40 protected:
37 protected:
41 void setLayout(QVector<QPointF>& points);
38 void setLayout(QVector<QPointF>& points);
39 void mousePressEvent( QGraphicsSceneMouseEvent * event );
42
40
43 private:
41 private:
44 QScatterSeries *m_series;
42 QScatterSeries *m_series;
45 QGraphicsItemGroup m_items;
43 QGraphicsItemGroup m_items;
46 int m_shape;
44 int m_shape;
47 int m_size;
45 int m_size;
48 QRectF m_rect;
46 QRectF m_rect;
49
47
50 };
48 };
51
49
52
50
53 class Marker: public QAbstractGraphicsShapeItem
51 class Marker: public QAbstractGraphicsShapeItem
54 {
52 {
55
53
56 public:
54 public:
57
55
58 Marker(QAbstractGraphicsShapeItem* item , ScatterChartItem* parent):QAbstractGraphicsShapeItem(0),m_item(item),m_parent(parent)
56 Marker(QAbstractGraphicsShapeItem* item , ScatterChartItem* parent):QAbstractGraphicsShapeItem(0),m_item(item),m_parent(parent)
59 {
57 {
60 };
58 };
61
59
62 ~Marker()
60 ~Marker()
63 {
61 {
64 delete m_item;
62 delete m_item;
65 }
63 }
66
64
67 void setIndex(int index)
65 void setIndex(int index)
68 {
66 {
69 m_index=index;
67 m_index=index;
70 }
68 }
71
69
72 int index() const
70 int index() const
73 {
71 {
74 return m_index;
72 return m_index;
75 }
73 }
76
74
77 QPainterPath shape() const
75 QPainterPath shape() const
78 {
76 {
79 return m_item->shape();
77 return m_item->shape();
80 }
78 }
81
79
82 QRectF boundingRect() const
80 QRectF boundingRect() const
83 {
81 {
84 return m_item->boundingRect();
82 return m_item->boundingRect();
85 }
83 }
86
84
87 bool contains(const QPointF &point) const
85 bool contains(const QPointF &point) const
88 {
86 {
89 return m_item->contains(point);
87 return m_item->contains(point);
90 }
88 }
91
89
92 void setPen(const QPen& pen)
90 void setPen(const QPen& pen)
93 {
91 {
94 m_item->setPen(pen);
92 m_item->setPen(pen);
95 }
93 }
96
94
97 void setBrush(const QBrush& brush)
95 void setBrush(const QBrush& brush)
98 {
96 {
99 m_item->setBrush(brush);
97 m_item->setBrush(brush);
100 }
98 }
101
99
102 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
100 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
103 {
101 {
104 m_item->paint(painter,option,widget);
102 m_item->paint(painter,option,widget);
105 }
103 }
106
104
107 protected:
105 protected:
108
106
109 void mousePressEvent( QGraphicsSceneMouseEvent * event )
107 void mousePressEvent( QGraphicsSceneMouseEvent * event )
110 {
108 {
111 m_parent->markerSelected(this);
109 m_parent->markerSelected(this);
112 }
110 }
113
111
114 private:
112 private:
115 QAbstractGraphicsShapeItem* m_item;
113 QAbstractGraphicsShapeItem* m_item;
116 ScatterChartItem* m_parent;
114 ScatterChartItem* m_parent;
117 int m_index;
115 int m_index;
118 };
116 };
119
117
120 QTCOMMERCIALCHART_END_NAMESPACE
118 QTCOMMERCIALCHART_END_NAMESPACE
121
119
122 #endif // SCATTERPRESENTER_H
120 #endif // SCATTERPRESENTER_H
@@ -1,42 +1,40
1 #ifndef SPLINECHARTITEM_P_H
1 #ifndef SPLINECHARTITEM_P_H
2 #define SPLINECHARTITEM_P_H
2 #define SPLINECHARTITEM_P_H
3
3
4 #include "qsplineseries.h"
4 #include "qsplineseries.h"
5 #include "xychartitem_p.h"
5 #include "xychartitem_p.h"
6 #include <QGraphicsItem>
6 #include <QGraphicsItem>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class SplineChartItem : public XYChartItem
10 class SplineChartItem : public XYChartItem
11 {
11 {
12 Q_OBJECT
12 Q_OBJECT
13 public:
13 public:
14 SplineChartItem(QSplineSeries* series, QGraphicsItem *parent = 0);
14 SplineChartItem(QSplineSeries* series, QGraphicsItem *parent = 0);
15
15
16 //from QGraphicsItem
16 //from QGraphicsItem
17 QRectF boundingRect() const;
17 QRectF boundingRect() const;
18 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
18 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
19 QPainterPath shape() const;
19 QPainterPath shape() const;
20
20
21 void setPointsVisible(bool visible);
22
23 public slots:
21 public slots:
24 void handleUpdated();
22 void handleUpdated();
25
23
26 protected:
24 protected:
27 void setLayout(QVector<QPointF>& points);
25 void setLayout(QVector<QPointF>& points);
28
26
29 private:
27 private:
30 QPointF calculateGeometryControlPoint(int index) const;
28 QPointF calculateGeometryControlPoint(int index) const;
31
29
32 private:
30 private:
33 QSplineSeries* m_series;
31 QSplineSeries* m_series;
34 QPainterPath m_path;
32 QPainterPath m_path;
35 QRectF m_rect;
33 QRectF m_rect;
36 QPen m_pen;
34 QPen m_pen;
37
35
38 };
36 };
39
37
40 QTCOMMERCIALCHART_END_NAMESPACE
38 QTCOMMERCIALCHART_END_NAMESPACE
41
39
42 #endif // SPLINECHARTITEM_P_H
40 #endif // SPLINECHARTITEM_P_H
@@ -1,75 +1,76
1 #ifndef QXYSERIES_H_
1 #ifndef QXYSERIES_H_
2 #define QXYSERIES_H_
2 #define QXYSERIES_H_
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qseries.h"
5 #include "qseries.h"
6 #include <QDebug>
6 #include <QDebug>
7 #include <QPen>
7 #include <QPen>
8 #include <QBrush>
8 #include <QBrush>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class QTCOMMERCIALCHART_EXPORT QXYSeries : public QSeries
12 class QTCOMMERCIALCHART_EXPORT QXYSeries : public QSeries
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 protected:
15 protected:
16 QXYSeries(QObject* parent=0);
16 QXYSeries(QObject* parent=0);
17 virtual ~QXYSeries();
17 virtual ~QXYSeries();
18
18
19 public:
19 public:
20 void add(qreal x, qreal y);
20 void add(qreal x, qreal y);
21 void add(const QPointF& point);
21 void add(const QPointF& point);
22 void add(const QList<QPointF> points);
22 void add(const QList<QPointF> points);
23 void replace(qreal x,qreal y);
23 void replace(qreal x,qreal y);
24 void replace(const QPointF& point);
24 void replace(const QPointF& point);
25 void remove(qreal x, qreal y);
25 void remove(qreal x, qreal y);
26 void remove(const QPointF& point);
26 void remove(const QPointF& point);
27 void removeAll();
27 void removeAll();
28
28
29 int count() const;
29 int count() const;
30 qreal x(int pos) const;
30 qreal x(int pos) const;
31 qreal y(int pos) const;
31 qreal y(int pos) const;
32 QList<QPointF> data();
32 QList<QPointF> data();
33
33
34 QXYSeries& operator << (const QPointF &point);
34 QXYSeries& operator << (const QPointF &point);
35 QXYSeries& operator << (const QList<QPointF> points);
35 QXYSeries& operator << (const QList<QPointF> points);
36
36
37 void setPen(const QPen& pen);
37 void setPen(const QPen& pen);
38 QPen pen() const {return m_pen;}
38 QPen pen() const {return m_pen;}
39 void setBrush(const QBrush& pen);
39 void setBrush(const QBrush& pen);
40 QBrush brush() const {return m_brush;}
40 QBrush brush() const {return m_brush;}
41
41
42 bool setModel(QAbstractItemModel* model);
42 bool setModel(QAbstractItemModel* model);
43 QAbstractItemModel* model() {return m_model;}
43 QAbstractItemModel* model() {return m_model;}
44
44
45 virtual void setModelMapping(int modelX, int modelY, Qt::Orientation orientation = Qt::Vertical);
45 virtual void setModelMapping(int modelX, int modelY, Qt::Orientation orientation = Qt::Vertical);
46 // void setModelMappingY(int modelLineIndex, Qt::Orientation orientation = Qt::Vertical);
46 // void setModelMappingY(int modelLineIndex, Qt::Orientation orientation = Qt::Vertical);
47
47
48 private slots:
48 private slots:
49 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
49 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
50 void modelDataAdded(QModelIndex parent, int start, int end);
50 void modelDataAdded(QModelIndex parent, int start, int end);
51 void modelDataRemoved(QModelIndex parent, int start, int end);
51 void modelDataRemoved(QModelIndex parent, int start, int end);
52
52
53 signals:
53 signals:
54 void clicked(const QPointF& point);
54 void updated();
55 void updated();
55 void pointReplaced(int index);
56 void pointReplaced(int index);
56 void pointRemoved(int index);
57 void pointRemoved(int index);
57 void pointAdded(int index);
58 void pointAdded(int index);
58
59
59 protected:
60 protected:
60 QVector<qreal> m_x;
61 QVector<qreal> m_x;
61 QVector<qreal> m_y;
62 QVector<qreal> m_y;
62
63
63 QPen m_pen;
64 QPen m_pen;
64 QBrush m_brush;
65 QBrush m_brush;
65
66
66 QAbstractItemModel* m_model;
67 QAbstractItemModel* m_model;
67 int m_mapX;
68 int m_mapX;
68 Qt::Orientation m_mapOrientation;
69 Qt::Orientation m_mapOrientation;
69 int m_mapY;
70 int m_mapY;
70 // Qt::Orientation m_mapYOrientation;
71 // Qt::Orientation m_mapYOrientation;
71 };
72 };
72
73
73 QTCOMMERCIALCHART_END_NAMESPACE
74 QTCOMMERCIALCHART_END_NAMESPACE
74
75
75 #endif
76 #endif
@@ -1,156 +1,162
1 #include "xychartitem_p.h"
1 #include "xychartitem_p.h"
2 #include "qxyseries.h"
2 #include "qxyseries.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include "chartanimator_p.h"
4 #include "chartanimator_p.h"
5 #include <QPainter>
5 #include <QPainter>
6 #include <QGraphicsSceneMouseEvent>
6
7
7
8
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10
10 //TODO: optimize : remove points which are not visible
11 //TODO: optimize : remove points which are not visible
11
12
12 XYChartItem::XYChartItem(QXYSeries* series, QGraphicsItem *parent):ChartItem(parent),
13 XYChartItem::XYChartItem(QXYSeries* series, QGraphicsItem *parent):ChartItem(parent),
13 m_minX(0),
14 m_minX(0),
14 m_maxX(0),
15 m_maxX(0),
15 m_minY(0),
16 m_minY(0),
16 m_maxY(0),
17 m_maxY(0),
17 m_series(series)
18 m_series(series)
18 {
19 {
19 QObject::connect(series,SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
20 QObject::connect(series,SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
20 QObject::connect(series,SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
21 QObject::connect(series,SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
21 QObject::connect(series,SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
22 QObject::connect(series,SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
22
23 QObject::connect(this,SIGNAL(clicked(const QPointF&)),series,SIGNAL(clicked(const QPointF&)));
23 }
24 }
24
25
25 QPointF XYChartItem::calculateGeometryPoint(const QPointF& point) const
26 QPointF XYChartItem::calculateGeometryPoint(const QPointF& point) const
26 {
27 {
27 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
28 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
28 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
29 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
29 qreal x = (point.x() - m_minX)* deltaX;
30 qreal x = (point.x() - m_minX)* deltaX;
30 qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
31 qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
31 return QPointF(x,y);
32 return QPointF(x,y);
32 }
33 }
33
34
34
35
35 QPointF XYChartItem::calculateGeometryPoint(int index) const
36 QPointF XYChartItem::calculateGeometryPoint(int index) const
36 {
37 {
37 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
38 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
38 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
39 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
39 qreal x = (m_series->x(index) - m_minX)* deltaX;
40 qreal x = (m_series->x(index) - m_minX)* deltaX;
40 qreal y = (m_series->y(index) - m_minY)*-deltaY + m_size.height();
41 qreal y = (m_series->y(index) - m_minY)*-deltaY + m_size.height();
41 return QPointF(x,y);
42 return QPointF(x,y);
42 }
43 }
43
44
44 QVector<QPointF> XYChartItem::calculateGeometryPoints() const
45 QVector<QPointF> XYChartItem::calculateGeometryPoints() const
45 {
46 {
46 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
47 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
47 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
48 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
48
49
49 QVector<QPointF> points;
50 QVector<QPointF> points;
50 points.reserve(m_series->count());
51 points.reserve(m_series->count());
51 for (int i = 0; i < m_series->count(); ++i) {
52 for (int i = 0; i < m_series->count(); ++i) {
52 qreal x = (m_series->x(i) - m_minX)* deltaX;
53 qreal x = (m_series->x(i) - m_minX)* deltaX;
53 qreal y = (m_series->y(i) - m_minY)*-deltaY + m_size.height();
54 qreal y = (m_series->y(i) - m_minY)*-deltaY + m_size.height();
54 points << QPointF(x,y);
55 points << QPointF(x,y);
55 }
56 }
56 return points;
57 return points;
57 }
58 }
58
59
59 QPointF XYChartItem::calculateDomainPoint(const QPointF& point) const
60 QPointF XYChartItem::calculateDomainPoint(const QPointF& point) const
60 {
61 {
61 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
62 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
62 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
63 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
63 qreal x = point.x()/deltaX +m_minX;
64 qreal x = point.x()/deltaX +m_minX;
64 qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
65 qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
65 return QPointF(x,y);
66 return QPointF(x,y);
66 }
67 }
67
68
68 void XYChartItem::applyLayout(QVector<QPointF>& points)
69 void XYChartItem::applyLayout(QVector<QPointF>& points)
69 {
70 {
70 if(m_animator){
71 if(m_animator){
71 m_animator->applyLayout(this,points);
72 m_animator->applyLayout(this,points);
72 }
73 }
73 else setLayout(points);
74 else setLayout(points);
74
75
75 }
76 }
76
77
77 void XYChartItem::updateLayout(QVector<QPointF>& points)
78 void XYChartItem::updateLayout(QVector<QPointF>& points)
78 {
79 {
79 setLayout(points);
80 setLayout(points);
80 }
81 }
81
82
82 void XYChartItem::setLayout(QVector<QPointF>& points)
83 void XYChartItem::setLayout(QVector<QPointF>& points)
83 {
84 {
84 m_points = points;
85 m_points = points;
85 }
86 }
86
87
87 //handlers
88 //handlers
88
89
89 void XYChartItem::handlePointAdded(int index)
90 void XYChartItem::handlePointAdded(int index)
90 {
91 {
91 Q_ASSERT(index<m_series->count());
92 Q_ASSERT(index<m_series->count());
92 Q_ASSERT(index>=0);
93 Q_ASSERT(index>=0);
93
94
94 QPointF point = calculateGeometryPoint(index);
95 QPointF point = calculateGeometryPoint(index);
95 QVector<QPointF> points = m_points;
96 QVector<QPointF> points = m_points;
96 points.insert(index,point);
97 points.insert(index,point);
97 updateLayout(points);
98 updateLayout(points);
98 update();
99 update();
99 }
100 }
100 void XYChartItem::handlePointRemoved(int index)
101 void XYChartItem::handlePointRemoved(int index)
101 {
102 {
102 Q_ASSERT(index<m_series->count() + 1);
103 Q_ASSERT(index<m_series->count() + 1);
103 Q_ASSERT(index>=0);
104 Q_ASSERT(index>=0);
104 // QPointF point = calculateGeometryPoint(index);
105 // QPointF point = calculateGeometryPoint(index);
105 QVector<QPointF> points = m_points;
106 QVector<QPointF> points = m_points;
106 points.remove(index);
107 points.remove(index);
107 updateLayout(points);
108 updateLayout(points);
108 update();
109 update();
109 }
110 }
110
111
111 void XYChartItem::handlePointReplaced(int index)
112 void XYChartItem::handlePointReplaced(int index)
112 {
113 {
113 Q_ASSERT(index<m_series->count());
114 Q_ASSERT(index<m_series->count());
114 Q_ASSERT(index>=0);
115 Q_ASSERT(index>=0);
115 QPointF point = calculateGeometryPoint(index);
116 QPointF point = calculateGeometryPoint(index);
116 QVector<QPointF> points = m_points;
117 QVector<QPointF> points = m_points;
117 points.replace(index,point);
118 points.replace(index,point);
118 updateLayout(points);
119 updateLayout(points);
119 update();
120 update();
120 }
121 }
121
122
122 void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
123 void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
123 {
124 {
124 m_minX=minX;
125 m_minX=minX;
125 m_maxX=maxX;
126 m_maxX=maxX;
126 m_minY=minY;
127 m_minY=minY;
127 m_maxY=maxY;
128 m_maxY=maxY;
128
129
129 if(isEmpty()) return;
130 if(isEmpty()) return;
130 QVector<QPointF> points = calculateGeometryPoints();
131 QVector<QPointF> points = calculateGeometryPoints();
131 applyLayout(points);
132 applyLayout(points);
132 update();
133 update();
133 }
134 }
134
135
135 void XYChartItem::handleGeometryChanged(const QRectF& rect)
136 void XYChartItem::handleGeometryChanged(const QRectF& rect)
136 {
137 {
137 Q_ASSERT(rect.isValid());
138 Q_ASSERT(rect.isValid());
138 m_size=rect.size();
139 m_size=rect.size();
139 m_clipRect=rect.translated(-rect.topLeft());
140 m_clipRect=rect.translated(-rect.topLeft());
140 setPos(rect.topLeft());
141 setPos(rect.topLeft());
141
142
142 if(isEmpty()) return;
143 if(isEmpty()) return;
143 QVector<QPointF> points = calculateGeometryPoints();
144 QVector<QPointF> points = calculateGeometryPoints();
144 applyLayout(points);
145 applyLayout(points);
145 update();
146 update();
146 }
147 }
147
148
148
149
149 bool XYChartItem::isEmpty()
150 bool XYChartItem::isEmpty()
150 {
151 {
151 return !m_clipRect.isValid() || m_maxX - m_minX == 0 || m_maxY - m_minY ==0 ;
152 return !m_clipRect.isValid() || m_maxX - m_minX == 0 || m_maxY - m_minY ==0 ;
152 }
153 }
153
154
155 void XYChartItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
156 {
157 emit clicked(calculateDomainPoint(event->pos()));
158 }
159
154 #include "moc_xychartitem_p.cpp"
160 #include "moc_xychartitem_p.cpp"
155
161
156 QTCOMMERCIALCHART_END_NAMESPACE
162 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,58 +1,63
1 #ifndef XYCHARTITEM_H
1 #ifndef XYCHARTITEM_H
2 #define XYCHARTITEM_H
2 #define XYCHARTITEM_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "chartitem_p.h"
5 #include "chartitem_p.h"
6 #include <QPen>
6 #include <QPen>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class ChartPresenter;
10 class ChartPresenter;
11 class QXYSeries;
11 class QXYSeries;
12
12
13 class XYChartItem : public QObject , public ChartItem
13 class XYChartItem : public QObject , public ChartItem
14 {
14 {
15 Q_OBJECT
15 Q_OBJECT
16 public:
16 public:
17 explicit 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;}
21 QRectF clipRect() const { return m_clipRect;}
21 QRectF clipRect() const { return m_clipRect;}
22
22
23 public slots:
23 public slots:
24 void handlePointAdded(int index);
24 void handlePointAdded(int index);
25 void handlePointRemoved(int index);
25 void handlePointRemoved(int index);
26 void handlePointReplaced(int index);
26 void handlePointReplaced(int index);
27 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
27 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
28 void handleGeometryChanged(const QRectF& size);
28 void handleGeometryChanged(const QRectF& size);
29
29
30 signals:
31 void clicked(const QPointF& point);
32
30 protected:
33 protected:
31 virtual void setLayout(QVector<QPointF>& points);
34 virtual void setLayout(QVector<QPointF>& points);
32 QPointF calculateGeometryPoint(const QPointF& point) const;
35 QPointF calculateGeometryPoint(const QPointF& point) const;
33 QPointF calculateGeometryPoint(int index) const;
36 QPointF calculateGeometryPoint(int index) const;
34 QPointF calculateDomainPoint(const QPointF& point) const;
37 QPointF calculateDomainPoint(const QPointF& point) const;
35 QVector<QPointF> calculateGeometryPoints() const;
38 QVector<QPointF> calculateGeometryPoints() const;
39 void mousePressEvent( QGraphicsSceneMouseEvent * event );
36
40
37 private:
41 private:
38 void applyLayout(QVector<QPointF>& points);
42 void applyLayout(QVector<QPointF>& points);
39 void updateLayout(QVector<QPointF>& points);
43 void updateLayout(QVector<QPointF>& points);
40 inline bool isEmpty();
44 inline bool isEmpty();
41
45
42 private:
46 private:
43 qreal m_minX;
47 qreal m_minX;
44 qreal m_maxX;
48 qreal m_maxX;
45 qreal m_minY;
49 qreal m_minY;
46 qreal m_maxY;
50 qreal m_maxY;
47 QXYSeries* m_series;
51 QXYSeries* m_series;
48 QSizeF m_size;
52 QSizeF m_size;
49 QRectF m_clipRect;
53 QRectF m_clipRect;
50 QVector<QPointF> m_points;
54 QVector<QPointF> m_points;
51
55
52 friend class XYAnimation;
56 friend class XYAnimation;
57 friend class AreaChartItem;
53
58
54 };
59 };
55
60
56 QTCOMMERCIALCHART_END_NAMESPACE
61 QTCOMMERCIALCHART_END_NAMESPACE
57
62
58 #endif
63 #endif
General Comments 0
You need to be logged in to leave comments. Login now