@@ -1,158 +1,159 | |||
|
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 | 31 | QList<XYPlotDomain> m_plotDomainList; |
|
32 | 32 | QList<XYLineChartItem*> m_xyLineChartItems; |
|
33 | 33 | QList<QGraphicsItem*> m_items; |
|
34 | 34 | int m_plotDataIndex; |
|
35 | 35 | int m_marginSize; |
|
36 | 36 | |
|
37 | 37 | }; |
|
38 | 38 | |
|
39 | 39 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
40 | 40 | |
|
41 | 41 | QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent), |
|
42 | 42 | d(new QChartPrivate(this)) |
|
43 | 43 | { |
|
44 | 44 | // setFlags(QGraphicsItem::ItemClipsChildrenToShape); |
|
45 | 45 | // set axis |
|
46 | 46 | d->m_axisY->rotate(90); |
|
47 | 47 | } |
|
48 | 48 | |
|
49 | 49 | QChart::~QChart(){} |
|
50 | 50 | |
|
51 | 51 | QRectF QChart::boundingRect() const |
|
52 | 52 | { |
|
53 | 53 | return d->m_rect; |
|
54 | 54 | } |
|
55 | 55 | |
|
56 | 56 | void QChart::addSeries(QChartSeries* series) |
|
57 | 57 | { |
|
58 | 58 | // TODO: we should check the series not already added |
|
59 | 59 | |
|
60 | 60 | d->m_series<<series; |
|
61 | 61 | |
|
62 | 62 | switch(series->type()) |
|
63 | 63 | { |
|
64 | 64 | case QChartSeries::SeriesTypeLine: { |
|
65 | 65 | |
|
66 | 66 | QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series); |
|
67 | 67 | |
|
68 | 68 | XYPlotDomain domain; |
|
69 | 69 | //TODO "nice numbers algorithm" |
|
70 | 70 | domain.m_ticksX=4; |
|
71 | 71 | domain.m_ticksY=4; |
|
72 | 72 | |
|
73 | 73 | for (int i = 0 ; i < xyseries->count() ; i++) |
|
74 | 74 | { |
|
75 | 75 | qreal x = xyseries->x(i); |
|
76 | 76 | qreal y = xyseries->y(i); |
|
77 | 77 | domain.m_minX = qMin(domain.m_minX,x); |
|
78 | 78 | domain.m_minY = qMin(domain.m_minY,y); |
|
79 | 79 | domain.m_maxX = qMax(domain.m_maxX,x); |
|
80 | 80 | domain.m_maxY = qMax(domain.m_maxY,y); |
|
81 | 81 | } |
|
82 | 82 | |
|
83 | 83 | XYLineChartItem* item = new XYLineChartItem(xyseries,this); |
|
84 | 84 | item->updateXYPlotDomain(domain); |
|
85 | 85 | d->m_plotDomainList<<domain; |
|
86 | 86 | d->m_xyLineChartItems<<item; |
|
87 | 87 | break; |
|
88 | 88 | } |
|
89 | 89 | case QChartSeries::SeriesTypeScatter: { |
|
90 | 90 | QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series); |
|
91 | 91 | if (scatter) { |
|
92 | 92 | scatter->d->setParentItem(this); |
|
93 | 93 | scene()->addItem(scatter->d); |
|
94 | 94 | } |
|
95 | 95 | break; |
|
96 | 96 | } |
|
97 | 97 | } |
|
98 | 98 | } |
|
99 | 99 | |
|
100 | 100 | QChartSeries* QChart::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type) |
|
101 | 101 | { |
|
102 | 102 | // TODO: support also other types in addition to scatter |
|
103 | 103 | Q_ASSERT(type == QChartSeries::SeriesTypeScatter); |
|
104 | 104 | QChartSeries *series = 0; |
|
105 | 105 | |
|
106 | 106 | switch (type) { |
|
107 | 107 | case QChartSeries::SeriesTypeScatter: { |
|
108 | 108 | QScatterSeries *scatterSeries = new QScatterSeries(x, y, this); |
|
109 | 109 | scatterSeries->d->setParentItem(this); |
|
110 | 110 | break; |
|
111 | 111 | } |
|
112 | 112 | default: |
|
113 | 113 | break; |
|
114 | 114 | } |
|
115 | 115 | |
|
116 | 116 | return series; |
|
117 | 117 | } |
|
118 | 118 | |
|
119 | 119 | void QChart::setSize(const QSizeF& size) |
|
120 | 120 | { |
|
121 | 121 | d->m_rect = QRect(QPoint(0,0),size.toSize()); |
|
122 | 122 | d->m_rect.adjust(margin(),margin(), -margin(), -margin()); |
|
123 | 123 | d->m_grid->setPos(d->m_rect.topLeft()); |
|
124 | 124 | d->m_grid->setSize(d->m_rect.size()); |
|
125 | 125 | |
|
126 | 126 | // TODO: calculate the scale |
|
127 | 127 | // TODO: calculate the origo |
|
128 | 128 | // TODO: not sure if emitting a signal here is the best from performance point of view |
|
129 | 129 | const qreal xscale = size.width() / 100; |
|
130 | 130 | const qreal yscale = size.height() / 100; |
|
131 | 131 | emit sizeChanged(QRectF(0, 0, size.width(), size.height()), xscale, yscale); |
|
132 | 132 | |
|
133 | 133 | for (int i(0); i < d->m_plotDomainList.size(); i++) |
|
134 | 134 | d->m_plotDomainList[i].m_viewportRect = d->m_rect; |
|
135 | 135 | |
|
136 | 136 | // TODO: line chart items are updated separately as they don't support update |
|
137 | 137 | // via sizeChanged signal |
|
138 | 138 | foreach(XYLineChartItem* item , d->m_xyLineChartItems) |
|
139 | 139 | item->updateXYPlotDomain(d->m_plotDomainList.at(d->m_plotDataIndex)); |
|
140 | 140 | |
|
141 | d->m_grid->setXYPlotData(d->m_plotDomainList.at(d->m_plotDataIndex)); | |
|
141 | if (d->m_plotDomainList.count()) | |
|
142 | d->m_grid->setXYPlotData(d->m_plotDomainList.at(d->m_plotDataIndex)); | |
|
142 | 143 | update(); |
|
143 | 144 | } |
|
144 | 145 | |
|
145 | 146 | int QChart::margin() const |
|
146 | 147 | { |
|
147 | 148 | return d->m_marginSize; |
|
148 | 149 | } |
|
149 | 150 | |
|
150 | 151 | void QChart::setMargin(int margin) |
|
151 | 152 | { |
|
152 | 153 | d->m_marginSize = margin; |
|
153 | 154 | } |
|
154 | 155 | |
|
155 | 156 | #include "moc_qchart.cpp" |
|
156 | 157 | |
|
157 | 158 | |
|
158 | 159 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now