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