@@ -96,12 +96,13 void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int index) | |||
|
96 | 96 | { |
|
97 | 97 | QPen pen; |
|
98 | 98 | if(pen != series->pen()){ |
|
99 |
|
|
|
99 | series->setPen(series->pen()); | |
|
100 | 100 | return; |
|
101 | }else{ | |
|
102 | pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); | |
|
103 | pen.setWidthF(2); | |
|
104 | series->setPen(pen); | |
|
101 | 105 | } |
|
102 | pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); | |
|
103 | pen.setWidthF(2); | |
|
104 | item->setLinePen(pen); | |
|
105 | 106 | } |
|
106 | 107 | |
|
107 | 108 | void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int index) |
@@ -2,6 +2,7 | |||
|
2 | 2 | #include "qlineseries.h" |
|
3 | 3 | #include "chartpresenter_p.h" |
|
4 | 4 | #include <QPainter> |
|
5 | #include <QGraphicsSceneMouseEvent> | |
|
5 | 6 | |
|
6 | 7 | |
|
7 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
@@ -10,11 +11,11 QTCOMMERCIALCHART_BEGIN_NAMESPACE | |||
|
10 | 11 | |
|
11 | 12 | LineChartItem::LineChartItem(QLineSeries* series,QGraphicsItem *parent):XYChartItem(series,parent), |
|
12 | 13 | m_series(series), |
|
13 | m_items(this) | |
|
14 | m_pointsVisible(false) | |
|
14 | 15 | { |
|
15 | 16 | setZValue(ChartPresenter::LineChartZValue); |
|
16 | 17 | QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated())); |
|
17 | QObject::connect(this,SIGNAL(clicked()),series,SIGNAL(clicked())); | |
|
18 | QObject::connect(this,SIGNAL(clicked(const QPointF&)),series,SIGNAL(clicked(const QPointF&))); | |
|
18 | 19 | handleUpdated(); |
|
19 | 20 | } |
|
20 | 21 | |
@@ -28,79 +29,31 QPainterPath LineChartItem::shape() const | |||
|
28 | 29 | return m_path; |
|
29 | 30 | } |
|
30 | 31 | |
|
31 | void LineChartItem::createPoints(int count) | |
|
32 | { | |
|
33 | for (int i = 0; i < count; ++i) { | |
|
34 | QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3); | |
|
35 | m_items.addToGroup(item); | |
|
36 | } | |
|
37 | } | |
|
38 | ||
|
39 | void LineChartItem::deletePoints(int count) | |
|
40 | { | |
|
41 | QList<QGraphicsItem *> items = m_items.childItems(); | |
|
42 | ||
|
43 | for (int i = 0; i < count; ++i) { | |
|
44 | delete(items.takeLast()); | |
|
45 | } | |
|
46 | } | |
|
47 | ||
|
48 | 32 | void LineChartItem::setGeometry(QVector<QPointF>& points) |
|
49 | 33 | { |
|
50 | 34 | if(points.size()==0) return; |
|
51 | 35 | |
|
52 | int diff = XYChartItem::points().size() - points.size(); | |
|
53 | ||
|
54 | if(diff>0) { | |
|
55 | deletePoints(diff); | |
|
56 | } | |
|
57 | else if(diff<0) { | |
|
58 | createPoints(-diff); | |
|
59 | } | |
|
60 | ||
|
61 | 36 | QList<QGraphicsItem*> items = m_items.childItems(); |
|
62 | 37 | |
|
63 |
QPainterPath |
|
|
64 | const QPointF& point = points.at(0); | |
|
65 | path.moveTo(point); | |
|
66 | QGraphicsItem* item = items.at(0); | |
|
67 | item->setPos(point.x()-1,point.y()-1); | |
|
68 | if(!clipRect().contains(point)) { | |
|
69 | item->setVisible(false); | |
|
70 | } | |
|
71 | else { | |
|
72 | item->setVisible(true); | |
|
73 | } | |
|
38 | QPainterPath linePath(points.at(0)); | |
|
74 | 39 | |
|
75 | 40 | for(int i=1; i< points.size();i++) { |
|
76 | QGraphicsItem* item = items.at(i); | |
|
77 | const QPointF& point = points.at(i); | |
|
78 | item->setPos(point.x()-1,point.y()-1); | |
|
79 | if(!clipRect().contains(point)) { | |
|
80 | item->setVisible(false); | |
|
81 | } | |
|
82 | else { | |
|
83 | item->setVisible(true); | |
|
84 | } | |
|
85 | path.lineTo(point); | |
|
41 | linePath.lineTo(points.at(i)); | |
|
86 | 42 | } |
|
87 | 43 | |
|
88 | 44 | prepareGeometryChange(); |
|
89 |
m_path = |
|
|
90 |
m_rect = |
|
|
45 | m_path = linePath; | |
|
46 | m_rect = linePath.boundingRect(); | |
|
91 | 47 | |
|
92 | 48 | XYChartItem::setGeometry(points); |
|
93 | 49 | } |
|
94 | 50 | |
|
95 | void LineChartItem::setLinePen(const QPen& pen) | |
|
96 | { | |
|
97 | m_pen = pen; | |
|
98 | } | |
|
99 | ||
|
100 | 51 | void LineChartItem::handleUpdated() |
|
101 | 52 | { |
|
102 |
m_it |
|
|
103 |
|
|
|
53 | m_pointsVisible = m_series->pointsVisible(); | |
|
54 | m_linePen = m_series->pen(); | |
|
55 | m_pointPen = m_series->pen(); | |
|
56 | m_pointPen.setWidthF(2*m_pointPen.width()); | |
|
104 | 57 | update(); |
|
105 | 58 | } |
|
106 | 59 | |
@@ -111,15 +64,19 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt | |||
|
111 | 64 | Q_UNUSED(widget); |
|
112 | 65 | Q_UNUSED(option); |
|
113 | 66 | painter->save(); |
|
114 |
painter->setPen(m_ |
|
|
67 | painter->setPen(m_linePen); | |
|
115 | 68 | painter->setClipRect(clipRect()); |
|
116 | 69 | painter->drawPath(m_path); |
|
70 | if(m_pointsVisible){ | |
|
71 | painter->setPen(m_pointPen); | |
|
72 | painter->drawPoints(points()); | |
|
73 | } | |
|
117 | 74 | painter->restore(); |
|
118 | 75 | } |
|
119 | 76 | |
|
120 | 77 | void LineChartItem::mousePressEvent( QGraphicsSceneMouseEvent * event ) |
|
121 | 78 | { |
|
122 | emit clicked(); | |
|
79 | emit clicked(calculateDomainPoint(event->pos())); | |
|
123 | 80 | } |
|
124 | 81 | |
|
125 | 82 | #include "moc_linechartitem_p.cpp" |
@@ -21,29 +21,24 public: | |||
|
21 | 21 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
22 | 22 | QPainterPath shape() const; |
|
23 | 23 | |
|
24 | void setLinePen(const QPen& pen); | |
|
25 | void setPointsVisible(bool visible); | |
|
26 | ||
|
27 | 24 | public slots: |
|
28 | 25 | void handleUpdated(); |
|
29 | 26 | |
|
30 | 27 | signals: |
|
31 | void clicked(); | |
|
28 | void clicked(const QPointF& point); | |
|
32 | 29 | |
|
33 | 30 | protected: |
|
34 | 31 | virtual void setGeometry(QVector<QPointF>& points); |
|
35 | 32 | void mousePressEvent( QGraphicsSceneMouseEvent * event ); |
|
36 | 33 | |
|
37 | 34 | private: |
|
38 | void createPoints(int count); | |
|
39 | void deletePoints(int count); | |
|
40 | ||
|
41 | private: | |
|
42 | 35 | QLineSeries* m_series; |
|
43 | 36 | QGraphicsItemGroup m_items; |
|
44 | 37 | QPainterPath m_path; |
|
45 | 38 | QRectF m_rect; |
|
46 |
QPen m_ |
|
|
39 | QPen m_linePen; | |
|
40 | QPen m_pointPen; | |
|
41 | bool m_pointsVisible; | |
|
47 | 42 | |
|
48 | 43 | }; |
|
49 | 44 |
@@ -23,7 +23,7 public: | |||
|
23 | 23 | bool pointsVisible() const {return m_pointsVisible;} |
|
24 | 24 | |
|
25 | 25 | signals: |
|
26 | void clicked(); | |
|
26 | void clicked(const QPointF& point); | |
|
27 | 27 | |
|
28 | 28 | public: // from QChartSeries |
|
29 | 29 | virtual QSeriesType type() const {return QSeries::SeriesTypeLine;} |
@@ -56,6 +56,15 QVector<QPointF> XYChartItem::calculateGeometryPoints() const | |||
|
56 | 56 | return points; |
|
57 | 57 | } |
|
58 | 58 | |
|
59 | QPointF XYChartItem::calculateDomainPoint(const QPointF& point) const | |
|
60 | { | |
|
61 | const qreal deltaX = m_size.width()/(m_maxX-m_minX); | |
|
62 | const qreal deltaY = m_size.height()/(m_maxY-m_minY); | |
|
63 | qreal x = point.x()/deltaX +m_minX; | |
|
64 | qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY; | |
|
65 | return QPointF(x,y); | |
|
66 | } | |
|
67 | ||
|
59 | 68 | void XYChartItem::updatePoints(QVector<QPointF>& points) |
|
60 | 69 | { |
|
61 | 70 | if(m_animator){ |
@@ -31,6 +31,7 protected: | |||
|
31 | 31 | virtual void setGeometry(QVector<QPointF>& points); |
|
32 | 32 | QPointF calculateGeometryPoint(const QPointF& point) const; |
|
33 | 33 | QPointF calculateGeometryPoint(int index) const; |
|
34 | QPointF calculateDomainPoint(const QPointF& point) const; | |
|
34 | 35 | QVector<QPointF> calculateGeometryPoints() const; |
|
35 | 36 | |
|
36 | 37 | private: |
General Comments 0
You need to be logged in to leave comments.
Login now