@@ -1,140 +1,142 | |||
|
1 | 1 | #include "qchart.h" |
|
2 | 2 | #include "qchartseries.h" |
|
3 | 3 | #include "qscatterseries.h" |
|
4 | 4 | #include "qscatterseries_p.h" |
|
5 | #include "qxychartseries.h" | |
|
5 | 6 | #include "xylinechartitem_p.h" |
|
6 | 7 | #include "xyplotdomain_p.h" |
|
7 | 8 | #include "axis_p.h" |
|
8 | 9 | #include "xygrid_p.h" |
|
9 | 10 | #include <QGraphicsScene> |
|
10 | 11 | #include <QDebug> |
|
11 | 12 | |
|
12 | 13 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
13 | 14 | |
|
14 | 15 | class QChartPrivate |
|
15 | 16 | { |
|
16 | 17 | public: |
|
17 | 18 | |
|
18 | 19 | QChartPrivate(QChart* parent): |
|
19 | 20 | m_axisX(new Axis(parent)), |
|
20 | 21 | m_axisY(new Axis(parent)), |
|
21 | 22 | m_grid(new XYGrid(parent)), |
|
22 | 23 | m_plotDataIndex(0), |
|
23 | 24 | m_marginSize(0){} |
|
24 | 25 | |
|
25 | 26 | Axis* m_axisX; |
|
26 | 27 | Axis* m_axisY; |
|
27 | 28 | XYGrid* m_grid; |
|
28 | 29 | QRect m_rect; |
|
29 | 30 | QList<const QChartSeries*> m_series; |
|
30 | 31 | QList<XYPlotDomain> m_plotDataList; |
|
31 | 32 | QList<QGraphicsItem*> m_items; |
|
32 | 33 | int m_plotDataIndex; |
|
33 | 34 | int m_marginSize; |
|
34 | 35 | |
|
35 | 36 | }; |
|
36 | 37 | |
|
37 | 38 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
38 | 39 | |
|
39 | 40 | QChart::QChart(QGraphicsItem* parent):QGraphicsItem(parent), |
|
40 | 41 | d_ptr(new QChartPrivate(this)) |
|
41 | 42 | { |
|
42 | 43 | // setFlags(QGraphicsItem::ItemClipsChildrenToShape); |
|
43 | 44 | // set axis |
|
44 | 45 | Q_D(QChart); |
|
45 | 46 | d->m_axisY->rotate(90); |
|
46 | 47 | |
|
47 | 48 | //TODO hardcoded values , to removed soon |
|
48 | 49 | XYPlotDomain data; |
|
49 | 50 | data.m_minX = 0.0; |
|
50 | 51 | data.m_maxX = 100.0; |
|
51 | 52 | data.m_minY = 0.0; |
|
52 | 53 | data.m_maxY = 100.0; |
|
53 | 54 | data.m_ticksX=4; |
|
54 | 55 | data.m_ticksY=4; |
|
55 | 56 | |
|
56 | 57 | d->m_plotDataList.clear(); |
|
57 | 58 | d->m_plotDataList << data; |
|
58 | 59 | |
|
59 | 60 | d->m_grid->setZValue(10); |
|
60 | 61 | d->m_grid->setXYPlotData(d->m_plotDataList.at(0)); |
|
61 | 62 | } |
|
62 | 63 | |
|
63 | 64 | QChart::~QChart(){} |
|
64 | 65 | |
|
65 | 66 | QRectF QChart::boundingRect() const |
|
66 | 67 | { |
|
67 | 68 | Q_D(const QChart); |
|
68 | 69 | return d->m_rect; |
|
69 | 70 | } |
|
70 | 71 | |
|
71 | 72 | void QChart::addSeries(QChartSeries* series) |
|
72 | 73 | { |
|
73 | 74 | Q_D(QChart); |
|
75 | ||
|
74 | 76 | d->m_series<<series; |
|
75 | 77 | |
|
76 | 78 | switch(series->type()) |
|
77 | 79 | { |
|
78 | 80 | case QChartSeries::SeriesTypeLine: { |
|
79 |
XYLineChartItem* item = new XYLineChartItem( |
|
|
81 | XYLineChartItem* item = new XYLineChartItem(static_cast<QXYChartSeries*>(series),this); | |
|
80 | 82 | item->updateXYPlotData(d->m_plotDataList.at(0)); |
|
81 | 83 | d->m_items<<item; |
|
82 | 84 | break; |
|
83 | 85 | } |
|
84 | 86 | case QChartSeries::SeriesTypeScatter: { |
|
85 | 87 | break; |
|
86 | 88 | } |
|
87 | 89 | } |
|
88 | 90 | } |
|
89 | 91 | |
|
90 | 92 | QChartSeries* QChart::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type) |
|
91 | 93 | { |
|
92 | 94 | Q_D(QChart); |
|
93 | 95 | |
|
94 | 96 | // TODO: support also other types |
|
95 | 97 | Q_ASSERT(type == QChartSeries::SeriesTypeScatter); |
|
96 | 98 | QChartSeries *series = 0; |
|
97 | 99 | |
|
98 | 100 | switch (type) { |
|
99 | 101 | case QChartSeries::SeriesTypeScatter: { |
|
100 | 102 | QScatterSeries *scatterSeries = new QScatterSeries(x, y, this); |
|
101 | 103 | d->m_items.append(scatterSeries->d); |
|
102 | 104 | scene()->addItem(scatterSeries->d); |
|
103 | 105 | //series = qobject_cast<QChartSeries *>(scatterSeries); |
|
104 | 106 | break; |
|
105 | 107 | } |
|
106 | 108 | default: |
|
107 | 109 | break; |
|
108 | 110 | } |
|
109 | 111 | |
|
110 | 112 | return series; |
|
111 | 113 | } |
|
112 | 114 | void QChart::setSize(const QSizeF& size) |
|
113 | 115 | { |
|
114 | 116 | Q_D(QChart); |
|
115 | 117 | //TODO refactor to setGeometry |
|
116 | 118 | d->m_rect = QRect(QPoint(0,0),size.toSize()); |
|
117 | 119 | d->m_rect.adjust(margin(),margin(),-margin(),-margin()); |
|
118 | 120 | d->m_grid->setPos(d->m_rect.topLeft()); |
|
119 | 121 | d->m_grid->setSize(d->m_rect.size()); |
|
120 | 122 | d->m_plotDataList[0].m_viewportRect = d->m_rect; |
|
121 | 123 | // TODO: line chart items would need to be updated separately as they don't support update |
|
122 | 124 | // via paint method |
|
123 | 125 | foreach(QGraphicsItem* item , d->m_items) |
|
124 | 126 | reinterpret_cast<XYLineChartItem*>(item)->updateXYPlotData(d->m_plotDataList.at(0)); |
|
125 | 127 | update(); |
|
126 | 128 | } |
|
127 | 129 | |
|
128 | 130 | int QChart::margin() const |
|
129 | 131 | { |
|
130 | 132 | Q_D(const QChart); |
|
131 | 133 | return d->m_marginSize; |
|
132 | 134 | } |
|
133 | 135 | |
|
134 | 136 | void QChart::setMargin(int margin) |
|
135 | 137 | { |
|
136 | 138 | Q_D(QChart); |
|
137 | 139 | d->m_marginSize = margin; |
|
138 | 140 | } |
|
139 | 141 | |
|
140 | 142 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now