@@ -1,122 +1,121 | |||
|
1 | 1 | #include "areachartitem_p.h" |
|
2 | 2 | #include "qareaseries.h" |
|
3 | 3 | #include "qlineseries.h" |
|
4 | 4 | #include "chartpresenter_p.h" |
|
5 | 5 | #include <QPainter> |
|
6 | 6 | #include <QGraphicsSceneMouseEvent> |
|
7 | 7 | |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | 10 | |
|
11 | 11 | //TODO: optimize : remove points which are not visible |
|
12 | 12 | |
|
13 |
AreaChartItem::AreaChartItem(QAreaSeries *areaSeries,ChartPresenter *presenter) |
|
|
13 | AreaChartItem::AreaChartItem(QAreaSeries *areaSeries, ChartPresenter *presenter) | |
|
14 | : ChartItem(presenter), | |
|
14 | 15 | m_series(areaSeries), |
|
15 | 16 | m_upper(0), |
|
16 | 17 | m_lower(0), |
|
17 | 18 | m_pointsVisible(false) |
|
18 | 19 | { |
|
19 | 20 | setZValue(ChartPresenter::LineChartZValue); |
|
20 | 21 | m_upper = new AreaBoundItem(this,m_series->upperSeries()); |
|
21 |
if (m_series->lowerSeries()) |
|
|
22 | if (m_series->lowerSeries()) | |
|
22 | 23 | m_lower = new AreaBoundItem(this,m_series->lowerSeries()); |
|
23 | } | |
|
24 | 24 | |
|
25 | 25 | connect(areaSeries,SIGNAL(updated()),this,SLOT(handleUpdated())); |
|
26 | 26 | connect(this,SIGNAL(clicked(const QPointF&)),areaSeries,SIGNAL(clicked(const QPointF&))); |
|
27 | 27 | |
|
28 | 28 | handleUpdated(); |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | AreaChartItem::~AreaChartItem() |
|
32 | 32 | { |
|
33 | 33 | delete m_upper; |
|
34 | 34 | delete m_lower; |
|
35 |
} |
|
|
35 | } | |
|
36 | 36 | |
|
37 | 37 | QRectF AreaChartItem::boundingRect() const |
|
38 | 38 | { |
|
39 | 39 | return m_rect; |
|
40 | 40 | } |
|
41 | 41 | |
|
42 | 42 | QPainterPath AreaChartItem::shape() const |
|
43 | 43 | { |
|
44 | 44 | return m_path; |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | 47 | void AreaChartItem::updatePath() |
|
48 | 48 | { |
|
49 | 49 | QPainterPath path; |
|
50 | 50 | |
|
51 | 51 | path = m_upper->shape(); |
|
52 | 52 | |
|
53 | 53 | if (m_lower) { |
|
54 | 54 | path.connectPath(m_lower->shape().toReversed()); |
|
55 | 55 | } else { |
|
56 | 56 | QPointF first = path.pointAtPercent(0); |
|
57 | 57 | QPointF last = path.pointAtPercent(1); |
|
58 | 58 | path.lineTo(last.x(),m_clipRect.bottom()); |
|
59 | 59 | path.lineTo(first.x(),m_clipRect.bottom()); |
|
60 | 60 | } |
|
61 | 61 | path.closeSubpath(); |
|
62 | 62 | prepareGeometryChange(); |
|
63 | m_path=path; | |
|
64 | m_rect=path.boundingRect(); | |
|
63 | m_path = path; | |
|
64 | m_rect = path.boundingRect(); | |
|
65 | 65 | update(); |
|
66 | 66 | } |
|
67 | 67 | |
|
68 | 68 | void AreaChartItem::handleUpdated() |
|
69 | 69 | { |
|
70 | 70 | m_pointsVisible = m_series->pointsVisible(); |
|
71 | 71 | m_linePen = m_series->pen(); |
|
72 | 72 | m_brush = m_series->brush(); |
|
73 | 73 | m_pointPen = m_series->pen(); |
|
74 | m_pointPen.setWidthF(2*m_pointPen.width()); | |
|
74 | m_pointPen.setWidthF(2 * m_pointPen.width()); | |
|
75 | 75 | |
|
76 | 76 | update(); |
|
77 | 77 | } |
|
78 | 78 | |
|
79 | 79 | void AreaChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) |
|
80 | 80 | { |
|
81 | 81 | m_upper->handleDomainChanged(minX,maxX,minY,maxY); |
|
82 | 82 | if (m_lower) |
|
83 | 83 | m_lower->handleDomainChanged(minX,maxX,minY,maxY); |
|
84 | 84 | } |
|
85 | 85 | |
|
86 | 86 | void AreaChartItem::handleGeometryChanged(const QRectF &rect) |
|
87 | 87 | { |
|
88 | 88 | m_clipRect=rect.translated(-rect.topLeft()); |
|
89 | 89 | setPos(rect.topLeft()); |
|
90 | 90 | m_upper->handleGeometryChanged(rect); |
|
91 | 91 | if (m_lower) |
|
92 | 92 | m_lower->handleGeometryChanged(rect); |
|
93 | 93 | } |
|
94 | //painter | |
|
95 | 94 | |
|
96 | 95 | void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
97 | 96 | { |
|
98 | 97 | Q_UNUSED(widget) |
|
99 | 98 | Q_UNUSED(option) |
|
100 | 99 | |
|
101 | 100 | painter->save(); |
|
102 | 101 | painter->setPen(m_linePen); |
|
103 | 102 | painter->setBrush(m_brush); |
|
104 | 103 | painter->setClipRect(m_clipRect); |
|
105 | 104 | painter->drawPath(m_path); |
|
106 | 105 | if (m_pointsVisible) { |
|
107 | 106 | painter->setPen(m_pointPen); |
|
108 | 107 | painter->drawPoints(m_upper->points()); |
|
109 | 108 | if (m_lower) |
|
110 | 109 | painter->drawPoints(m_lower->points()); |
|
111 | 110 | } |
|
112 | 111 | painter->restore(); |
|
113 | 112 | } |
|
114 | 113 | |
|
115 | 114 | void AreaChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
116 | 115 | { |
|
117 | 116 | emit clicked(m_upper->calculateDomainPoint(event->pos())); |
|
118 | 117 | } |
|
119 | 118 | |
|
120 | 119 | #include "moc_areachartitem_p.cpp" |
|
121 | 120 | |
|
122 | 121 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,73 +1,72 | |||
|
1 | 1 | #ifndef AREACHARTITEM_H |
|
2 | 2 | #define AREACHARTITEM_H |
|
3 | 3 | |
|
4 | 4 | #include "qchartglobal.h" |
|
5 | 5 | #include "linechartitem_p.h" |
|
6 | 6 | #include <QPen> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | class QAreaSeries; |
|
11 | 11 | class AreaChartItem; |
|
12 | 12 | |
|
13 | 13 | class AreaChartItem : public ChartItem |
|
14 | 14 | { |
|
15 | 15 | Q_OBJECT |
|
16 | 16 | public: |
|
17 | 17 | AreaChartItem(QAreaSeries *areaSeries, ChartPresenter *presenter); |
|
18 |
~ |
|
|
18 | ~AreaChartItem(); | |
|
19 | 19 | |
|
20 | 20 | //from QGraphicsItem |
|
21 | 21 | QRectF boundingRect() const; |
|
22 | 22 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
23 | 23 | QPainterPath shape() const; |
|
24 | 24 | |
|
25 | 25 | LineChartItem* upperLineItem() const { return m_upper; } |
|
26 | 26 | LineChartItem* lowerLineItem() const { return m_lower; } |
|
27 | 27 | |
|
28 | 28 | void updatePath(); |
|
29 | 29 | |
|
30 | 30 | protected: |
|
31 | 31 | void mousePressEvent(QGraphicsSceneMouseEvent *event); |
|
32 | 32 | |
|
33 | 33 | signals: |
|
34 | 34 | void clicked(const QPointF &point); |
|
35 | 35 | |
|
36 | 36 | public slots: |
|
37 | 37 | void handleUpdated(); |
|
38 | 38 | void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY); |
|
39 | 39 | void handleGeometryChanged(const QRectF &size); |
|
40 | 40 | |
|
41 | 41 | private: |
|
42 | 42 | QAreaSeries* m_series; |
|
43 | 43 | LineChartItem* m_upper; |
|
44 | 44 | LineChartItem* m_lower; |
|
45 | 45 | QPainterPath m_path; |
|
46 | 46 | QRectF m_rect; |
|
47 | 47 | QRectF m_clipRect; |
|
48 | 48 | QPen m_linePen; |
|
49 | 49 | QPen m_pointPen; |
|
50 | 50 | QBrush m_brush; |
|
51 | 51 | bool m_pointsVisible; |
|
52 | 52 | |
|
53 | 53 | }; |
|
54 | 54 | |
|
55 | 55 | class AreaBoundItem : public LineChartItem |
|
56 | 56 | { |
|
57 | 57 | public: |
|
58 | 58 | AreaBoundItem(AreaChartItem *item,QLineSeries *lineSeries) : LineChartItem(lineSeries, 0), m_item(item) {} |
|
59 | ~AreaBoundItem(){} | |
|
59 | ~AreaBoundItem() {} | |
|
60 | 60 | |
|
61 | 61 | void setLayout(QVector<QPointF> &points) { |
|
62 | 62 | LineChartItem::setLayout(points); |
|
63 | 63 | m_item->updatePath(); |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | private: |
|
67 | 67 | AreaChartItem* m_item; |
|
68 | ||
|
69 | 68 | }; |
|
70 | 69 | |
|
71 | 70 | QTCOMMERCIALCHART_END_NAMESPACE |
|
72 | 71 | |
|
73 | 72 | #endif |
@@ -1,148 +1,150 | |||
|
1 | 1 | #include "qareaseries.h" |
|
2 | 2 | #include "qlineseries.h" |
|
3 | 3 | |
|
4 | 4 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
5 | 5 | |
|
6 | 6 | /*! |
|
7 | 7 | \class QAreaSeries |
|
8 | 8 | \brief The QAreaSeries class is used for making area charts. |
|
9 | 9 | |
|
10 | 10 | \mainclass |
|
11 | 11 | |
|
12 | 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 | 13 | is emphasized with color. Since the area chart is based on line chart, QAreaSeries constructor needs QLineSeries instance, |
|
14 | 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 | 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 | 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 | 18 | \image areachart.png |
|
19 | 19 | |
|
20 | 20 | Creating basic area chart is simple: |
|
21 | 21 | \code |
|
22 | 22 | QLineSeries* lineSeries = new QLineSeries(); |
|
23 | 23 | series->add(0, 6); |
|
24 | 24 | series->add(2, 4); |
|
25 | 25 | QAreaSeries* areaSeries = new QAreaSeries(lineSeries); |
|
26 | 26 | ... |
|
27 | 27 | chartView->addSeries(areaSeries); |
|
28 | 28 | \endcode |
|
29 | 29 | */ |
|
30 | 30 | |
|
31 | 31 | /*! |
|
32 | 32 | \fn virtual QSeriesType QAreaSeries::type() const |
|
33 | 33 | \brief Returns type of series. |
|
34 | 34 | \sa QSeries, QSeriesType |
|
35 | 35 | */ |
|
36 | 36 | |
|
37 | 37 | /*! |
|
38 | 38 | \fn QLineSeries* QAreaSeries::upperSeries() const |
|
39 | 39 | \brief Returns upperSeries used to define one of area boundaries. |
|
40 | 40 | */ |
|
41 | 41 | |
|
42 | 42 | /*! |
|
43 | 43 | \fn QLineSeries* QAreaSeries::lowerSeries() const |
|
44 | 44 | \brief Returns lowerSeries used to define one of area boundaries. Note if QAreaSeries where counstucted wihtout a\ lowerSeries |
|
45 | 45 | this function return Null pointer. |
|
46 | 46 | */ |
|
47 | 47 | |
|
48 | 48 | /*! |
|
49 | 49 | \fn QPen QAreaSeries::pen() const |
|
50 | 50 | \brief Returns the pen used to draw line for this series. |
|
51 | 51 | \sa setPen() |
|
52 | 52 | */ |
|
53 | 53 | |
|
54 | 54 | /*! |
|
55 | 55 | \fn QPen QAreaSeries::brush() const |
|
56 | 56 | \brief Returns the brush used to draw line for this series. |
|
57 | 57 | \sa setBrush() |
|
58 | 58 | */ |
|
59 | 59 | |
|
60 | 60 | /*! |
|
61 | 61 | \fn bool QAreaSeries::pointsVisible() const |
|
62 | 62 | \brief Returns if the points are drawn for this series. |
|
63 | 63 | \sa setPointsVisible() |
|
64 | 64 | */ |
|
65 | 65 | |
|
66 | 66 | /*! |
|
67 | 67 | \fn void QAreaSeries::clicked(const QPointF& point) |
|
68 | 68 | \brief Signal is emitted when user clicks the \a point on area chart. |
|
69 | 69 | */ |
|
70 | 70 | |
|
71 | 71 | /*! |
|
72 | 72 | \fn void QAreaSeries::updated() |
|
73 | 73 | \brief \internal |
|
74 | 74 | */ |
|
75 | 75 | |
|
76 | 76 | /*! |
|
77 | 77 | Constructs area series object which is a child of \a upperSeries. Area will be spanned between \a |
|
78 | 78 | upperSeries line and \a lowerSeries line. If no \a lowerSeries is passed to constructor, area is specified by axis x (y=0) instead. |
|
79 | 79 | When series object is added to QChartView or QChart instance ownerships is transfered. |
|
80 | 80 | */ |
|
81 |
QAreaSeries::QAreaSeries(QLineSeries* |
|
|
82 |
|
|
|
83 |
|
|
|
84 | m_pointsVisible(false) | |
|
81 | QAreaSeries::QAreaSeries(QLineSeries *upperSeries, QLineSeries *lowerSeries) | |
|
82 | : QSeries(upperSeries), | |
|
83 | m_upperSeries(upperSeries), | |
|
84 | m_lowerSeries(lowerSeries), | |
|
85 | m_pointsVisible(false) | |
|
85 | 86 | { |
|
86 | 87 | } |
|
88 | ||
|
87 | 89 | /*! |
|
88 | 90 | Destroys the object. Series added to QChartView or QChart instances are owned by those, |
|
89 | 91 | and are deleted when mentioned object are destroyed. |
|
90 | 92 | */ |
|
91 | 93 | QAreaSeries::~QAreaSeries() |
|
92 | 94 | { |
|
93 | 95 | } |
|
94 | 96 | |
|
95 | 97 | /*! |
|
96 | 98 | Sets \a pen used for drawing area outline. |
|
97 | 99 | */ |
|
98 |
void QAreaSeries::setPen(const QPen& |
|
|
100 | void QAreaSeries::setPen(const QPen &pen) | |
|
99 | 101 | { |
|
100 | 102 | if (m_pen != pen) { |
|
101 | 103 | m_pen = pen; |
|
102 | 104 | emit updated(); |
|
103 | 105 | } |
|
104 | 106 | } |
|
105 | 107 | |
|
106 | 108 | /*! |
|
107 | 109 | Sets \a brush used for filling the area. |
|
108 | 110 | */ |
|
109 | 111 | void QAreaSeries::setBrush(const QBrush &brush) |
|
110 | 112 | { |
|
111 | 113 | if (m_brush != brush) { |
|
112 | 114 | m_brush = brush; |
|
113 | 115 | emit updated(); |
|
114 | 116 | } |
|
115 | 117 | } |
|
116 | 118 | /*! |
|
117 | 119 | Sets if data points are \a visible and should be drawn on line. |
|
118 | 120 | */ |
|
119 | 121 | void QAreaSeries::setPointsVisible(bool visible) |
|
120 | 122 | { |
|
121 | 123 | if (m_pointsVisible != visible) { |
|
122 | 124 | m_pointsVisible = visible; |
|
123 | 125 | emit updated(); |
|
124 | 126 | } |
|
125 | 127 | } |
|
126 | 128 | |
|
127 | 129 | //bool QAreaSeries::setModel(QAbstractItemModel* model) |
|
128 | 130 | //{ |
|
129 | 131 | // m_upperSeries->setModel(model); |
|
130 | 132 | // if (m_lowerSeries) |
|
131 | 133 | // m_lowerSeries->setModel(model); |
|
132 | 134 | // return true; |
|
133 | 135 | //} |
|
134 | 136 | |
|
135 | 137 | //void QAreaSeries::setModelMappingUpper(int modelX, int modelY, Qt::Orientation orientation) |
|
136 | 138 | //{ |
|
137 | 139 | // m_upperSeries->setModelMapping(modelX, modelY, orientation); |
|
138 | 140 | //} |
|
139 | 141 | |
|
140 | 142 | //void QAreaSeries::setModelMappingLower(int modelX, int modelY, Qt::Orientation orientation) |
|
141 | 143 | //{ |
|
142 | 144 | // if (m_lowerSeries) |
|
143 | 145 | // m_lowerSeries->setModelMapping(modelX, modelY, orientation); |
|
144 | 146 | //} |
|
145 | 147 | |
|
146 | 148 | #include "moc_qareaseries.cpp" |
|
147 | 149 | |
|
148 | 150 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,80 +1,80 | |||
|
1 | 1 | #ifndef QXYSERIES_H_ |
|
2 | 2 | #define QXYSERIES_H_ |
|
3 | 3 | |
|
4 | 4 | #include <qchartglobal.h> |
|
5 | 5 | #include <qseries.h> |
|
6 | 6 | #include <QPen> |
|
7 | 7 | #include <QBrush> |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | 10 | |
|
11 | 11 | class QTCOMMERCIALCHART_EXPORT QXYSeries : public QSeries |
|
12 | 12 | { |
|
13 | 13 | Q_OBJECT |
|
14 | 14 | protected: |
|
15 | QXYSeries(QObject *parent=0); | |
|
15 | QXYSeries(QObject *parent = 0); | |
|
16 | 16 | virtual ~QXYSeries(); |
|
17 | 17 | |
|
18 | 18 | public: |
|
19 | 19 | void add(qreal x, qreal y); |
|
20 | 20 | void add(const QPointF &point); |
|
21 | 21 | void add(const QList<QPointF> points); |
|
22 | 22 | void replace(qreal x,qreal y); |
|
23 | 23 | void replace(const QPointF &point); |
|
24 | 24 | void remove(qreal x); |
|
25 | 25 | void remove(qreal x, qreal y); |
|
26 | 26 | void remove(const QPointF &point); |
|
27 | 27 | void removeAll(); |
|
28 | 28 | |
|
29 | 29 | int count() const; |
|
30 | 30 | qreal x(int pos) const; |
|
31 | 31 | qreal y(int pos) const; |
|
32 | 32 | QList<QPointF> data(); |
|
33 | 33 | |
|
34 | 34 | QXYSeries& operator << (const QPointF &point); |
|
35 | 35 | QXYSeries& operator << (const QList<QPointF> points); |
|
36 | 36 | |
|
37 | 37 | void setPen(const QPen &pen); |
|
38 | 38 | QPen pen() const {return m_pen;} |
|
39 |
void setBrush(const QBrush & |
|
|
39 | void setBrush(const QBrush &brush); | |
|
40 | 40 | QBrush brush() const {return m_brush;} |
|
41 | 41 | |
|
42 | 42 | bool setModel(QAbstractItemModel *model); |
|
43 | QAbstractItemModel* model() {return m_model;} | |
|
43 | QAbstractItemModel* model() { return m_model; } | |
|
44 | 44 | |
|
45 | 45 | virtual void setModelMapping(int modelX, int modelY, Qt::Orientation orientation = Qt::Vertical); |
|
46 | 46 | virtual void setModelMappingShift(int first, int count = 0); |
|
47 | 47 | |
|
48 |
|
|
|
49 |
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
|
|
|
48 | private slots: | |
|
49 | void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight); | |
|
50 | void modelDataAboutToBeAdded(QModelIndex parent, int start, int end); | |
|
51 | void modelDataAdded(QModelIndex parent, int start, int end); | |
|
52 | void modelDataAboutToBeRemoved(QModelIndex parent, int start, int end); | |
|
53 | void modelDataRemoved(QModelIndex parent, int start, int end); | |
|
54 | 54 | |
|
55 | 55 | signals: |
|
56 | 56 | void clicked(const QPointF &point); |
|
57 | 57 | void updated(); |
|
58 | 58 | void pointReplaced(int index); |
|
59 | 59 | void pointRemoved(int index); |
|
60 | 60 | void pointAdded(int index); |
|
61 | 61 | |
|
62 | 62 | protected: |
|
63 | 63 | QVector<qreal> m_x; |
|
64 | 64 | QVector<qreal> m_y; |
|
65 | 65 | |
|
66 | 66 | QPen m_pen; |
|
67 | 67 | QBrush m_brush; |
|
68 | 68 | |
|
69 | 69 | int m_mapX; |
|
70 | 70 | int m_mapY; |
|
71 | 71 | int m_mapFirst; |
|
72 | 72 | int m_mapCount; |
|
73 | 73 | bool m_mapLimited; |
|
74 | 74 | Qt::Orientation m_mapOrientation; |
|
75 | 75 | int tempItemsRemoved; |
|
76 | 76 | }; |
|
77 | 77 | |
|
78 | 78 | QTCOMMERCIALCHART_END_NAMESPACE |
|
79 | 79 | |
|
80 | 80 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now