##// END OF EJS Templates
Removes hardcoded values for xydomain
Michal Klocek -
r47:ad450388bdcb
parent child
Show More
@@ -1,142 +1,153
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qchartseries.h"
2 #include "qchartseries.h"
3 #include "qscatterseries.h"
3 #include "qscatterseries.h"
4 #include "qscatterseries_p.h"
4 #include "qscatterseries_p.h"
5 #include "qxychartseries.h"
5 #include "qxychartseries.h"
6 #include "xylinechartitem_p.h"
6 #include "xylinechartitem_p.h"
7 #include "xyplotdomain_p.h"
7 #include "xyplotdomain_p.h"
8 #include "axis_p.h"
8 #include "axis_p.h"
9 #include "xygrid_p.h"
9 #include "xygrid_p.h"
10 #include <QGraphicsScene>
10 #include <QGraphicsScene>
11 #include <QDebug>
11 #include <QDebug>
12
12
13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
14
14
15 class QChartPrivate
15 class QChartPrivate
16 {
16 {
17 public:
17 public:
18
18
19 QChartPrivate(QChart* parent):
19 QChartPrivate(QChart* parent):
20 m_axisX(new Axis(parent)),
20 m_axisX(new Axis(parent)),
21 m_axisY(new Axis(parent)),
21 m_axisY(new Axis(parent)),
22 m_grid(new XYGrid(parent)),
22 m_grid(new XYGrid(parent)),
23 m_plotDataIndex(0),
23 m_plotDataIndex(0),
24 m_marginSize(0){}
24 m_marginSize(0){}
25
25
26 Axis* m_axisX;
26 Axis* m_axisX;
27 Axis* m_axisY;
27 Axis* m_axisY;
28 XYGrid* m_grid;
28 XYGrid* m_grid;
29 QRect m_rect;
29 QRect m_rect;
30 QList<const QChartSeries*> m_series;
30 QList<const QChartSeries*> m_series;
31 QList<XYPlotDomain> m_plotDataList;
31 QList<XYPlotDomain> m_plotDomainList;
32 QList<XYLineChartItem*> m_xyLineChartItems;
32 QList<QGraphicsItem*> m_items;
33 QList<QGraphicsItem*> m_items;
33 int m_plotDataIndex;
34 int m_plotDataIndex;
34 int m_marginSize;
35 int m_marginSize;
35
36
36 };
37 };
37
38
38 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
39 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
39
40
40 QChart::QChart(QGraphicsItem* parent):QGraphicsItem(parent),
41 QChart::QChart(QGraphicsItem* parent):QGraphicsItem(parent),
41 d_ptr(new QChartPrivate(this))
42 d_ptr(new QChartPrivate(this))
42 {
43 {
43 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
44 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
44 // set axis
45 // set axis
45 Q_D(QChart);
46 Q_D(QChart);
46 d->m_axisY->rotate(90);
47 d->m_axisY->rotate(90);
47
48 //TODO hardcoded values , to removed soon
49 XYPlotDomain data;
50 data.m_minX = 0.0;
51 data.m_maxX = 100.0;
52 data.m_minY = 0.0;
53 data.m_maxY = 100.0;
54 data.m_ticksX=4;
55 data.m_ticksY=4;
56
57 d->m_plotDataList.clear();
58 d->m_plotDataList << data;
59
60 d->m_grid->setZValue(10);
61 d->m_grid->setXYPlotData(d->m_plotDataList.at(0));
62 }
48 }
63
49
64 QChart::~QChart(){}
50 QChart::~QChart(){}
65
51
66 QRectF QChart::boundingRect() const
52 QRectF QChart::boundingRect() const
67 {
53 {
68 Q_D(const QChart);
54 Q_D(const QChart);
69 return d->m_rect;
55 return d->m_rect;
70 }
56 }
71
57
72 void QChart::addSeries(QChartSeries* series)
58 void QChart::addSeries(QChartSeries* series)
73 {
59 {
74 Q_D(QChart);
60 Q_D(QChart);
75
61
76 d->m_series<<series;
62 d->m_series<<series;
77
63
78 switch(series->type())
64 switch(series->type())
79 {
65 {
80 case QChartSeries::SeriesTypeLine: {
66 case QChartSeries::SeriesTypeLine: {
81 XYLineChartItem* item = new XYLineChartItem(static_cast<QXYChartSeries*>(series),this);
67
82 item->updateXYPlotData(d->m_plotDataList.at(0));
68 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
83 d->m_items<<item;
69
70 XYPlotDomain domain;
71 //TODO "nice numbers algorithm"
72 domain.m_ticksX=4;
73 domain.m_ticksY=4;
74
75 for (int i = 0 ; i < xyseries->count() ; i++)
76 {
77 qreal x = xyseries->x(i);
78 qreal y = xyseries->y(i);
79 domain.m_minX = qMin(domain.m_minX,x);
80 domain.m_minY = qMin(domain.m_minY,y);
81 domain.m_maxX = qMax(domain.m_maxX,x);
82 domain.m_maxY = qMax(domain.m_maxY,y);
83 }
84
85 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
86 item->updateXYPlotDomain(domain);
87 d->m_plotDomainList<<domain;
88 d->m_xyLineChartItems<<item;
84 break;
89 break;
85 }
90 }
86 case QChartSeries::SeriesTypeScatter: {
91 case QChartSeries::SeriesTypeScatter: {
87 break;
92 break;
88 }
93 }
89 }
94 }
90 }
95 }
91
96
92 QChartSeries* QChart::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type)
97 QChartSeries* QChart::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type)
93 {
98 {
94 Q_D(QChart);
99 Q_D(QChart);
95
100
96 // TODO: support also other types
101 // TODO: support also other types
97 Q_ASSERT(type == QChartSeries::SeriesTypeScatter);
102 Q_ASSERT(type == QChartSeries::SeriesTypeScatter);
98 QChartSeries *series = 0;
103 QChartSeries *series = 0;
99
104
100 switch (type) {
105 switch (type) {
101 case QChartSeries::SeriesTypeScatter: {
106 case QChartSeries::SeriesTypeScatter: {
102 QScatterSeries *scatterSeries = new QScatterSeries(x, y, this);
107 QScatterSeries *scatterSeries = new QScatterSeries(x, y, this);
103 d->m_items.append(scatterSeries->d);
108 d->m_items.append(scatterSeries->d);
104 scene()->addItem(scatterSeries->d);
109 scene()->addItem(scatterSeries->d);
105 //series = qobject_cast<QChartSeries *>(scatterSeries);
110 //series = qobject_cast<QChartSeries *>(scatterSeries);
106 break;
111 break;
107 }
112 }
108 default:
113 default:
109 break;
114 break;
110 }
115 }
111
116
112 return series;
117 return series;
113 }
118 }
114 void QChart::setSize(const QSizeF& size)
119 void QChart::setSize(const QSizeF& size)
115 {
120 {
116 Q_D(QChart);
121 Q_D(QChart);
117 //TODO refactor to setGeometry
118 d->m_rect = QRect(QPoint(0,0),size.toSize());
122 d->m_rect = QRect(QPoint(0,0),size.toSize());
119 d->m_rect.adjust(margin(),margin(),-margin(),-margin());
123 d->m_rect.adjust(margin(),margin(),-margin(),-margin());
120 d->m_grid->setPos(d->m_rect.topLeft());
124 d->m_grid->setPos(d->m_rect.topLeft());
121 d->m_grid->setSize(d->m_rect.size());
125 d->m_grid->setSize(d->m_rect.size());
122 d->m_plotDataList[0].m_viewportRect = d->m_rect;
123 // TODO: line chart items would need to be updated separately as they don't support update
126 // TODO: line chart items would need to be updated separately as they don't support update
124 // via paint method
127 // via paint method
125 foreach(QGraphicsItem* item , d->m_items)
128 for (int i =0; i< d->m_plotDomainList.size();i++)
126 reinterpret_cast<XYLineChartItem*>(item)->updateXYPlotData(d->m_plotDataList.at(0));
129 {
130 d->m_plotDomainList[i].m_viewportRect = d->m_rect;
131
132 }
133
134 foreach(XYLineChartItem* item , d->m_xyLineChartItems)
135 item->updateXYPlotDomain(d->m_plotDomainList.at(d->m_plotDataIndex));
136
137 d->m_grid->setXYPlotData(d->m_plotDomainList.at(d->m_plotDataIndex));
127 update();
138 update();
128 }
139 }
129
140
130 int QChart::margin() const
141 int QChart::margin() const
131 {
142 {
132 Q_D(const QChart);
143 Q_D(const QChart);
133 return d->m_marginSize;
144 return d->m_marginSize;
134 }
145 }
135
146
136 void QChart::setMargin(int margin)
147 void QChart::setMargin(int margin)
137 {
148 {
138 Q_D(QChart);
149 Q_D(QChart);
139 d->m_marginSize = margin;
150 d->m_marginSize = margin;
140 }
151 }
141
152
142 QTCOMMERCIALCHART_END_NAMESPACE
153 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,56 +1,56
1 #include "xylinechartitem_p.h"
1 #include "xylinechartitem_p.h"
2 #include "axis_p.h"
2 #include "axis_p.h"
3 #include "xygrid_p.h"
3 #include "xygrid_p.h"
4 #include "qxychartseries.h"
4 #include "qxychartseries.h"
5 #include <QPainter>
5 #include <QPainter>
6 #include <QStyleOptionGraphicsItem>
6 #include <QStyleOptionGraphicsItem>
7 #include <QDebug>
7 #include <QDebug>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 XYLineChartItem::XYLineChartItem(QXYChartSeries* series,QGraphicsItem *parent):QGraphicsItem(parent),
11 XYLineChartItem::XYLineChartItem(QXYChartSeries* series,QGraphicsItem *parent):QGraphicsItem(parent),
12 m_series(series)
12 m_series(series)
13 {
13 {
14
14
15 }
15 }
16
16
17 void XYLineChartItem::updateXYPlotData(const XYPlotDomain& data)
17 void XYLineChartItem::updateXYPlotDomain(const XYPlotDomain& data)
18 {
18 {
19 m_xyPlotData=data;
19 m_xyPlotData=data;
20
20
21 if (!m_xyPlotData.m_viewportRect.isValid())
21 if (!m_xyPlotData.m_viewportRect.isValid())
22 return;
22 return;
23
23
24 const QRect& rect = m_xyPlotData.m_viewportRect;
24 const QRect& rect = m_xyPlotData.m_viewportRect;
25
25
26 const qreal deltaX = (rect.width()-1)/m_xyPlotData.spanX();
26 const qreal deltaX = (rect.width()-1)/m_xyPlotData.spanX();
27 const qreal deltaY = (rect.height()-1)/m_xyPlotData.spanY();
27 const qreal deltaY = (rect.height()-1)/m_xyPlotData.spanY();
28
28
29 m_polyline.clear();
29 m_polyline.clear();
30 m_polyline.resize(m_series->count());
30 m_polyline.resize(m_series->count());
31
31
32 for (int j = 0; j < m_series->count(); ++j) {
32 for (int j = 0; j < m_series->count(); ++j) {
33 qreal dx = m_series->x(j) - m_xyPlotData.m_minX;
33 qreal dx = m_series->x(j) - m_xyPlotData.m_minX;
34 qreal dy = m_series->y(j) - m_xyPlotData.m_minY;
34 qreal dy = m_series->y(j) - m_xyPlotData.m_minY;
35 qreal x = (dx * deltaX) + rect.left();
35 qreal x = (dx * deltaX) + rect.left();
36 qreal y = - (dy * deltaY) + rect.bottom();
36 qreal y = - (dy * deltaY) + rect.bottom();
37 m_polyline[j] = QPointF(x, y);
37 m_polyline[j] = QPointF(x, y);
38 }
38 }
39
39
40 }
40 }
41
41
42 QRectF XYLineChartItem::boundingRect() const
42 QRectF XYLineChartItem::boundingRect() const
43 {
43 {
44 return m_polyline.boundingRect();
44 return m_polyline.boundingRect();
45 }
45 }
46
46
47
47
48 void XYLineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
48 void XYLineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
49 {
49 {
50 painter->setClipRect(m_xyPlotData.m_viewportRect.adjusted(+1, +1, -1, -1));
50 painter->setClipRect(m_xyPlotData.m_viewportRect.adjusted(+1, +1, -1, -1));
51 painter->setPen(m_series->color());
51 painter->setPen(m_series->color());
52 painter->drawPolyline(m_polyline);
52 painter->drawPolyline(m_polyline);
53
53
54 }
54 }
55
55
56 QTCOMMERCIALCHART_END_NAMESPACE
56 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,33 +1,33
1 #ifndef XYLINECHARTITEM_H
1 #ifndef XYLINECHARTITEM_H
2 #define XYLINECHARTITEM_H
2 #define XYLINECHARTITEM_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qchart.h"
5 #include "qchart.h"
6 #include "xyplotdomain_p.h"
6 #include "xyplotdomain_p.h"
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QXYChartSeries;
10 class QXYChartSeries;
11
11
12 class XYLineChartItem : public QGraphicsItem
12 class XYLineChartItem : public QGraphicsItem
13 {
13 {
14
14
15 public:
15 public:
16 XYLineChartItem(QXYChartSeries* m_series,QGraphicsItem *parent = 0);
16 XYLineChartItem(QXYChartSeries* m_series,QGraphicsItem *parent = 0);
17 virtual ~ XYLineChartItem(){};
17 virtual ~ XYLineChartItem(){};
18
18
19 //from QGraphicsItem
19 //from QGraphicsItem
20 virtual QRectF boundingRect() const;
20 virtual QRectF boundingRect() const;
21 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
21 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
22
22
23 void updateXYPlotData(const XYPlotDomain& data);
23 void updateXYPlotDomain(const XYPlotDomain& data);
24
24
25 private:
25 private:
26 QPolygonF m_polyline;
26 QPolygonF m_polyline;
27 QXYChartSeries* m_series;
27 QXYChartSeries* m_series;
28 XYPlotDomain m_xyPlotData;
28 XYPlotDomain m_xyPlotData;
29 };
29 };
30
30
31 QTCOMMERCIALCHART_END_NAMESPACE
31 QTCOMMERCIALCHART_END_NAMESPACE
32
32
33 #endif
33 #endif
General Comments 0
You need to be logged in to leave comments. Login now