@@ -1,299 +1,300 | |||
|
1 | 1 | #include "axisitem_p.h" |
|
2 | 2 | #include "qchartaxis.h" |
|
3 | #include "chartpresenter_p.h" | |
|
3 | 4 | #include <QPainter> |
|
4 | 5 | #include <QDebug> |
|
5 | 6 | |
|
6 | 7 | static int label_padding = 5; |
|
7 | 8 | |
|
8 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 10 | |
|
10 | 11 | AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) : |
|
11 | 12 | ChartItem(parent), |
|
12 | 13 | m_type(type), |
|
13 | 14 | m_labelsAngle(0), |
|
14 | 15 | m_shadesEnabled(true), |
|
15 | 16 | m_grid(parent), |
|
16 | 17 | m_shades(parent), |
|
17 | m_labels(parent) | |
|
18 | m_labels(parent), | |
|
19 | m_origin(0,0) | |
|
18 | 20 | { |
|
19 | 21 | //initial initialization |
|
20 |
m_shades.setZValue( |
|
|
21 |
m_grid.setZValue( |
|
|
22 | m_shades.setZValue(ChartPresenter::ShadesZValue); | |
|
23 | m_grid.setZValue(ChartPresenter::GridZValue); | |
|
22 | 24 | } |
|
23 | 25 | |
|
24 | 26 | AxisItem::~AxisItem() |
|
25 | 27 | { |
|
26 | 28 | } |
|
27 | 29 | |
|
28 | 30 | QRectF AxisItem::boundingRect() const |
|
29 | 31 | { |
|
30 | 32 | return m_rect; |
|
31 | 33 | } |
|
32 | 34 | |
|
33 | 35 | void AxisItem::createItems(int count) |
|
34 | 36 | { |
|
35 | 37 | for (int i = 0; i < count; ++i) { |
|
36 | 38 | m_grid.addToGroup(new QGraphicsLineItem(this)); |
|
37 | 39 | m_labels.addToGroup(new QGraphicsSimpleTextItem(this)); |
|
38 | 40 | if(i%2) m_shades.addToGroup(new QGraphicsRectItem(this)); |
|
39 | 41 | } |
|
40 | 42 | } |
|
41 | 43 | |
|
42 | 44 | void AxisItem::clear() |
|
43 | 45 | { |
|
44 | 46 | foreach(QGraphicsItem* item , m_shades.childItems()) { |
|
45 | 47 | delete item; |
|
46 | 48 | } |
|
47 | 49 | |
|
48 | 50 | foreach(QGraphicsItem* item , m_grid.childItems()) { |
|
49 | 51 | delete item; |
|
50 | 52 | } |
|
51 | 53 | |
|
52 | 54 | foreach(QGraphicsItem* item , m_labels.childItems()) { |
|
53 | 55 | delete item; |
|
54 | 56 | } |
|
55 | 57 | |
|
56 | 58 | m_thicksList.clear(); |
|
57 | 59 | |
|
58 | 60 | } |
|
59 | 61 | |
|
60 | 62 | void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
61 | 63 | { |
|
62 | 64 | |
|
63 | 65 | } |
|
64 | 66 | |
|
65 | 67 | void AxisItem::updateItem(int count) |
|
66 | 68 | { |
|
67 | 69 | |
|
68 | 70 | QList<QGraphicsItem *> lines = m_grid.childItems(); |
|
69 | 71 | QList<QGraphicsItem *> labels = m_labels.childItems(); |
|
70 | 72 | QList<QGraphicsItem *> shades = m_shades.childItems(); |
|
71 | 73 | |
|
72 | 74 | switch (m_type) |
|
73 | 75 | { |
|
74 | 76 | case X_AXIS: |
|
75 | 77 | { |
|
76 | 78 | const qreal deltaX = m_rect.width() / (count-1); |
|
77 | 79 | |
|
78 | 80 | m_axis.setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); |
|
79 | 81 | |
|
80 | 82 | for (int i = 0; i < count; ++i) { |
|
81 | 83 | int x = i * deltaX + m_rect.left(); |
|
82 | 84 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); |
|
83 | 85 | lineItem->setLine(x, m_rect.top(), x, m_rect.bottom()); |
|
84 | 86 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); |
|
85 | 87 | labelItem->setText(m_thicksList.at(i)); |
|
86 | 88 | QPointF center = labelItem->boundingRect().center(); |
|
87 | 89 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
88 | 90 | labelItem->setPos(x - center.x(), m_rect.bottom() + label_padding); |
|
89 | 91 | |
|
90 | 92 | if(i%2){ |
|
91 | 93 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2)); |
|
92 | 94 | rectItem->setRect(x,m_rect.top(),deltaX,m_rect.height()); |
|
93 | 95 | } |
|
94 | 96 | } |
|
95 | 97 | } |
|
96 | 98 | break; |
|
97 | 99 | |
|
98 | 100 | case Y_AXIS: |
|
99 | 101 | { |
|
100 | 102 | const qreal deltaY = m_rect.height()/ (count-1); |
|
101 | 103 | |
|
102 | 104 | m_axis.setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom()); |
|
103 | 105 | |
|
104 | 106 | for (int i = 0; i < count; ++i) { |
|
105 | 107 | int y = i * -deltaY + m_rect.bottom(); |
|
106 | 108 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); |
|
107 | 109 | lineItem->setLine(m_rect.left() , y, m_rect.right(), y); |
|
108 | 110 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); |
|
109 | 111 | labelItem->setText(m_thicksList.at(i)); |
|
110 | 112 | QPointF center = labelItem->boundingRect().center(); |
|
111 | 113 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
112 | 114 | labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , y-center.y()); |
|
113 | 115 | if(i%2){ |
|
114 | 116 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2)); |
|
115 | 117 | rectItem->setRect(m_rect.left(),y,m_rect.width(),deltaY); |
|
116 | 118 | } |
|
117 | 119 | } |
|
118 | 120 | } |
|
119 | 121 | break; |
|
120 | 122 | default: |
|
121 | 123 | qDebug()<<"Unknown axis type"; |
|
122 | 124 | break; |
|
123 | 125 | } |
|
124 | 126 | } |
|
125 | 127 | |
|
126 | 128 | void AxisItem::handleAxisUpdate(QChartAxis* axis) |
|
127 | 129 | { |
|
128 | 130 | if(axis->isAxisVisible()) { |
|
129 | 131 | setAxisOpacity(100); |
|
130 | 132 | } |
|
131 | 133 | else { |
|
132 | 134 | setAxisOpacity(0); |
|
133 | 135 | } |
|
134 | 136 | |
|
135 | 137 | if(axis->isGridVisible()) { |
|
136 | 138 | setGridOpacity(100); |
|
137 | 139 | } |
|
138 | 140 | else { |
|
139 | 141 | setGridOpacity(0); |
|
140 | 142 | } |
|
141 | 143 | |
|
142 | 144 | if(axis->isLabelsVisible()) |
|
143 | 145 | { |
|
144 | 146 | setLabelsOpacity(100); |
|
145 | 147 | } |
|
146 | 148 | else { |
|
147 | 149 | setLabelsOpacity(0); |
|
148 | 150 | } |
|
149 | 151 | |
|
150 | 152 | if(axis->isShadesVisible()) { |
|
151 | 153 | setShadesOpacity(axis->shadesOpacity()); |
|
152 | 154 | } |
|
153 | 155 | else { |
|
154 | 156 | setShadesOpacity(0); |
|
155 | 157 | } |
|
156 | 158 | |
|
157 | 159 | setLabelsAngle(axis->labelsAngle()); |
|
158 | 160 | setAxisPen(axis->axisPen()); |
|
159 | 161 | setLabelsPen(axis->labelsPen()); |
|
160 | 162 | setLabelsBrush(axis->labelsBrush()); |
|
161 | 163 | setLabelsFont(axis->labelFont()); |
|
162 | 164 | setGridPen(axis->gridPen()); |
|
163 | 165 | setShadesPen(axis->shadesPen()); |
|
164 | 166 | setShadesBrush(axis->shadesBrush()); |
|
165 | 167 | } |
|
166 | 168 | |
|
167 | 169 | void AxisItem::handleLabelsChanged(QChartAxis* axis,const QStringList& labels) |
|
168 | 170 | { |
|
169 | 171 | m_thicksList=labels; |
|
170 | 172 | QList<QGraphicsItem*> items = m_labels.childItems(); |
|
171 | 173 | if(items.size()!=m_thicksList.size()){ |
|
172 | 174 | clear(); |
|
173 | 175 | m_thicksList=labels; |
|
174 | 176 | createItems(m_thicksList.size()); |
|
175 | 177 | updateItem(m_thicksList.size()); |
|
176 | 178 | items = m_labels.childItems(); |
|
177 | 179 | handleAxisUpdate(axis); |
|
178 | 180 | } |
|
179 | 181 | |
|
180 | 182 | Q_ASSERT(items.size()==m_thicksList.size()); |
|
181 | 183 | |
|
182 | 184 | int i=0; |
|
183 | 185 | foreach(QGraphicsItem* item, items){ |
|
184 | 186 | static_cast<QGraphicsSimpleTextItem*>(item)->setText(m_thicksList.at(i)); |
|
185 | 187 | i++; |
|
186 | 188 | } |
|
187 | 189 | update(); |
|
188 | 190 | } |
|
189 | 191 | |
|
190 | 192 | void AxisItem::handleGeometryChanged(const QRectF& rect) |
|
191 | 193 | { |
|
192 | 194 | m_rect = rect; |
|
193 | 195 | updateItem(m_thicksList.size()); |
|
194 | 196 | update(); |
|
195 | 197 | } |
|
196 | 198 | |
|
197 | 199 | void AxisItem::setAxisOpacity(qreal opacity) |
|
198 | 200 | { |
|
199 | 201 | m_axis.setOpacity(opacity); |
|
200 | 202 | } |
|
201 | 203 | |
|
202 | 204 | qreal AxisItem::axisOpacity() const |
|
203 | 205 | { |
|
204 | 206 | return m_axis.opacity(); |
|
205 | 207 | } |
|
206 | 208 | |
|
207 | 209 | void AxisItem::setGridOpacity(qreal opacity) |
|
208 | 210 | { |
|
209 | 211 | m_grid.setOpacity(opacity); |
|
210 | 212 | } |
|
211 | 213 | |
|
212 | ||
|
213 | 214 | qreal AxisItem::gridOpacity() const |
|
214 | 215 | { |
|
215 | 216 | return m_grid.opacity(); |
|
216 | 217 | } |
|
217 | 218 | |
|
218 | 219 | void AxisItem::setLabelsOpacity(qreal opacity) |
|
219 | 220 | { |
|
220 | 221 | m_labels.setOpacity(opacity); |
|
221 | 222 | } |
|
222 | 223 | |
|
223 | 224 | qreal AxisItem::labelsOpacity() const |
|
224 | 225 | { |
|
225 | 226 | return m_labels.opacity(); |
|
226 | 227 | } |
|
227 | 228 | |
|
228 | 229 | void AxisItem::setShadesOpacity(qreal opacity) |
|
229 | 230 | { |
|
230 | 231 | m_shades.setOpacity(opacity); |
|
231 | 232 | } |
|
232 | 233 | |
|
233 | 234 | qreal AxisItem::shadesOpacity() const |
|
234 | 235 | { |
|
235 | 236 | return m_shades.opacity(); |
|
236 | 237 | } |
|
237 | 238 | |
|
238 | 239 | void AxisItem::setLabelsAngle(int angle) |
|
239 | 240 | { |
|
240 | 241 | foreach(QGraphicsItem* item , m_labels.childItems()) { |
|
241 | 242 | QPointF center = item->boundingRect().center(); |
|
242 | 243 | item->setRotation(angle); |
|
243 | 244 | } |
|
244 | 245 | |
|
245 | 246 | m_labelsAngle=angle; |
|
246 | 247 | } |
|
247 | 248 | |
|
248 | 249 | void AxisItem::setLabelsPen(const QPen& pen) |
|
249 | 250 | { |
|
250 | 251 | foreach(QGraphicsItem* item , m_labels.childItems()) { |
|
251 | 252 | static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen); |
|
252 | 253 | } |
|
253 | 254 | } |
|
254 | 255 | |
|
255 | 256 | void AxisItem::setLabelsBrush(const QBrush& brush) |
|
256 | 257 | { |
|
257 | 258 | foreach(QGraphicsItem* item , m_labels.childItems()) { |
|
258 | 259 | static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush); |
|
259 | 260 | } |
|
260 | 261 | } |
|
261 | 262 | |
|
262 | 263 | void AxisItem::setLabelsFont(const QFont& font) |
|
263 | 264 | { |
|
264 | 265 | foreach(QGraphicsItem* item , m_labels.childItems()) { |
|
265 | 266 | static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font); |
|
266 | 267 | } |
|
267 | 268 | } |
|
268 | 269 | |
|
269 | 270 | void AxisItem::setShadesBrush(const QBrush& brush) |
|
270 | 271 | { |
|
271 | 272 | foreach(QGraphicsItem* item , m_shades.childItems()) { |
|
272 | 273 | static_cast<QGraphicsRectItem*>(item)->setBrush(brush); |
|
273 | 274 | } |
|
274 | 275 | } |
|
275 | 276 | |
|
276 | 277 | void AxisItem::setShadesPen(const QPen& pen) |
|
277 | 278 | { |
|
278 | 279 | foreach(QGraphicsItem* item , m_shades.childItems()) { |
|
279 | 280 | static_cast<QGraphicsRectItem*>(item)->setPen(pen); |
|
280 | 281 | } |
|
281 | 282 | } |
|
282 | 283 | |
|
283 | 284 | void AxisItem::setAxisPen(const QPen& pen) |
|
284 | 285 | { |
|
285 | 286 | m_axis.setPen(pen); |
|
286 | 287 | } |
|
287 | 288 | |
|
288 | 289 | void AxisItem::setGridPen(const QPen& pen) |
|
289 | 290 | { |
|
290 | 291 | foreach(QGraphicsItem* item , m_grid.childItems()) { |
|
291 | 292 | static_cast<QGraphicsLineItem*>(item)->setPen(pen); |
|
292 | 293 | } |
|
293 | 294 | } |
|
294 | 295 | |
|
295 | 296 | |
|
296 | 297 | //TODO "nice numbers algorithm" |
|
297 | 298 | #include "moc_axisitem_p.cpp" |
|
298 | 299 | |
|
299 | 300 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,76 +1,77 | |||
|
1 | 1 | #ifndef AXISITEM_H_ |
|
2 | 2 | #define AXISITEM_H_ |
|
3 | 3 | |
|
4 | 4 | #include "domain_p.h" |
|
5 | 5 | #include "chartitem_p.h" |
|
6 | 6 | #include <QGraphicsItem> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | class QChartAxis; |
|
11 | 11 | |
|
12 | 12 | class AxisItem : public QObject, public ChartItem |
|
13 | 13 | { |
|
14 | 14 | Q_OBJECT |
|
15 | 15 | public: |
|
16 | 16 | enum AxisType{X_AXIS,Y_AXIS}; |
|
17 | 17 | |
|
18 | 18 | AxisItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0); |
|
19 | 19 | ~AxisItem(); |
|
20 | 20 | |
|
21 | 21 | //from QGraphicsItem |
|
22 | 22 | QRectF boundingRect() const; |
|
23 | 23 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
24 | 24 | |
|
25 | 25 | AxisType axisType() const {return m_type;}; |
|
26 | 26 | |
|
27 | 27 | void setAxisOpacity(qreal opacity); |
|
28 | 28 | qreal axisOpacity() const; |
|
29 | 29 | |
|
30 | 30 | void setGridOpacity(qreal opacity); |
|
31 | 31 | qreal gridOpacity() const; |
|
32 | 32 | |
|
33 | 33 | void setLabelsOpacity(qreal opacity); |
|
34 | 34 | qreal labelsOpacity() const; |
|
35 | 35 | |
|
36 | 36 | void setShadesOpacity(qreal opacity); |
|
37 | 37 | qreal shadesOpacity() const; |
|
38 | 38 | |
|
39 | 39 | void setLabelsAngle(int angle); |
|
40 | 40 | int labelsAngle()const { return m_labelsAngle; } |
|
41 | 41 | |
|
42 | 42 | void setShadesBrush(const QBrush& brush); |
|
43 | 43 | void setShadesPen(const QPen& pen); |
|
44 | 44 | |
|
45 | 45 | void setAxisPen(const QPen& pen); |
|
46 | 46 | void setGridPen(const QPen& pen); |
|
47 | 47 | |
|
48 | 48 | void setLabelsPen(const QPen& pen); |
|
49 | 49 | void setLabelsBrush(const QBrush& brush); |
|
50 | 50 | void setLabelsFont(const QFont& font); |
|
51 | 51 | |
|
52 | 52 | public slots: |
|
53 | void handleAxisUpdate(QChartAxis* axis); | |
|
54 | void handleLabelsChanged(QChartAxis* axis,const QStringList& labels); | |
|
55 | void handleGeometryChanged(const QRectF& size); | |
|
53 | void handleAxisUpdate(QChartAxis* axis); //look and feel | |
|
54 | void handleLabelsChanged(QChartAxis* axis,const QStringList& labels); //labels from dataset | |
|
55 | void handleGeometryChanged(const QRectF& size); // geometry from presenter | |
|
56 | 56 | protected: |
|
57 | 57 | void updateItem(int count); |
|
58 | 58 | private: |
|
59 | 59 | void clear(); |
|
60 | 60 | void createItems(int count); |
|
61 | 61 | private: |
|
62 | 62 | AxisType m_type; |
|
63 | 63 | QRectF m_rect; |
|
64 | 64 | int m_labelsAngle; |
|
65 | 65 | bool m_shadesEnabled; |
|
66 | 66 | QGraphicsItemGroup m_grid; |
|
67 | 67 | QGraphicsItemGroup m_shades; |
|
68 | 68 | QGraphicsItemGroup m_labels; |
|
69 | 69 | QGraphicsLineItem m_axis; |
|
70 | 70 | QStringList m_thicksList; |
|
71 | QPointF m_origin; | |
|
71 | 72 | |
|
72 | 73 | }; |
|
73 | 74 | |
|
74 | 75 | QTCOMMERCIALCHART_END_NAMESPACE |
|
75 | 76 | |
|
76 | 77 | #endif /* AXISITEM_H_ */ |
@@ -1,61 +1,63 | |||
|
1 | 1 | #ifndef CHARTPRESENTER_H_ |
|
2 | 2 | #define CHARTPRESENTER_H_ |
|
3 | 3 | |
|
4 | 4 | #include "qchartglobal.h" |
|
5 | 5 | #include "qchart.h" //becouse of QChart::ChartThemeId //TODO |
|
6 | 6 | #include "qchartaxis.h" |
|
7 | 7 | #include <QRectF> |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | 10 | |
|
11 | 11 | class ChartItem; |
|
12 | 12 | class QChartSeries; |
|
13 | 13 | class ChartDataSet; |
|
14 | 14 | //class QChart; |
|
15 | 15 | class Domain; |
|
16 | 16 | class AxisItem; |
|
17 | 17 | class ChartTheme; |
|
18 | 18 | |
|
19 | 19 | class ChartPresenter: public QObject |
|
20 | 20 | { |
|
21 | 21 | Q_OBJECT |
|
22 | 22 | public: |
|
23 | enum ZValues { BackgroundZValue = -1 , ShadesZValue, GridZValue, AxisZValue , LineChartZValue }; | |
|
24 | ||
|
23 | 25 | ChartPresenter(QChart* chart,ChartDataSet *dataset); |
|
24 | 26 | virtual ~ChartPresenter(); |
|
25 | 27 | |
|
26 | 28 | void setMargin(int margin); |
|
27 | 29 | int margin() const; |
|
28 | 30 | |
|
29 | 31 | QRectF geometry() const; |
|
30 | 32 | |
|
31 | 33 | void setChartTheme(QChart::ChartTheme theme); |
|
32 | 34 | QChart::ChartTheme chartTheme(); |
|
33 | 35 | |
|
34 | 36 | private: |
|
35 | 37 | void createConnections(); |
|
36 | 38 | |
|
37 | 39 | public slots: |
|
38 | 40 | void handleSeriesAdded(QChartSeries* series); |
|
39 | 41 | void handleSeriesRemoved(QChartSeries* series); |
|
40 | 42 | void handleAxisAdded(QChartAxis* axis); |
|
41 | 43 | void handleAxisRemoved(QChartAxis* axis); |
|
42 | 44 | void handleSeriesDomainChanged(QChartSeries* series, const Domain& domain); |
|
43 | 45 | void handleAxisLabelsChanged(QChartAxis* axis, const QStringList& labels); |
|
44 | 46 | void handleSeriesChanged(QChartSeries* series); |
|
45 | 47 | void handleGeometryChanged(); |
|
46 | 48 | signals: |
|
47 | 49 | void geometryChanged(const QRectF& rect); |
|
48 | 50 | private: |
|
49 | 51 | QMap<QChartSeries*,ChartItem*> m_chartItems; |
|
50 | 52 | QMap<QChartAxis*,AxisItem*> m_axisItems; |
|
51 | 53 | QChart* m_chart; |
|
52 | 54 | ChartDataSet* m_dataset; |
|
53 | 55 | ChartTheme *m_chartTheme; |
|
54 | 56 | int m_marginSize; |
|
55 | 57 | QRectF m_rect; |
|
56 | 58 | |
|
57 | 59 | }; |
|
58 | 60 | |
|
59 | 61 | QTCOMMERCIALCHART_END_NAMESPACE |
|
60 | 62 | |
|
61 | 63 | #endif /* CHARTPRESENTER_H_ */ |
@@ -1,210 +1,210 | |||
|
1 | 1 | #include "linechartitem_p.h" |
|
2 | 2 | #include "qlinechartseries.h" |
|
3 | 3 | #include "chartpresenter_p.h" |
|
4 | 4 | #include <QPainter> |
|
5 | 5 | |
|
6 | 6 | |
|
7 | 7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | 8 | |
|
9 | 9 | //TODO: optimazie : remove points which are not visible |
|
10 | 10 | |
|
11 | 11 | LineChartItem::LineChartItem(ChartPresenter* presenter, QLineChartSeries* series,QGraphicsItem *parent):ChartItem(parent), |
|
12 | 12 | m_presenter(presenter), |
|
13 | 13 | m_series(series), |
|
14 | 14 | m_dirtyData(false), |
|
15 | 15 | m_dirtyGeometry(false), |
|
16 | 16 | m_dirtyDomain(false) |
|
17 | 17 | { |
|
18 | ||
|
18 | setZValue(ChartPresenter::LineChartZValue); | |
|
19 | 19 | } |
|
20 | 20 | |
|
21 | 21 | QRectF LineChartItem::boundingRect() const |
|
22 | 22 | { |
|
23 | 23 | return m_rect; |
|
24 | 24 | } |
|
25 | 25 | |
|
26 | 26 | QPainterPath LineChartItem::shape() const |
|
27 | 27 | { |
|
28 | 28 | return m_path; |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | |
|
32 | 32 | void LineChartItem::addPoints(const QVector<QPointF>& points) |
|
33 | 33 | { |
|
34 | 34 | m_data = points; |
|
35 | 35 | for(int i=0; i<m_data.size();i++){ |
|
36 | 36 | const QPointF& point =m_data[i]; |
|
37 | 37 | QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this); |
|
38 | 38 | item->setPos(point.x()-1,point.y()-1);; |
|
39 | 39 | if(!m_clipRect.contains(point) || !m_series->isPointsVisible()) item->setVisible(false); |
|
40 | 40 | m_points << item; |
|
41 | 41 | } |
|
42 | 42 | } |
|
43 | 43 | |
|
44 | 44 | void LineChartItem::addPoint(const QPointF& point) |
|
45 | 45 | { |
|
46 | 46 | m_data << point; |
|
47 | 47 | QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this); |
|
48 | 48 | m_clipRect.contains(point); |
|
49 | 49 | item->setPos(point.x()-1,point.y()-1); |
|
50 | 50 | if(!m_clipRect.contains(point) || !m_series->isPointsVisible()) item->setVisible(false); |
|
51 | 51 | m_points << item; |
|
52 | 52 | } |
|
53 | 53 | |
|
54 | 54 | void LineChartItem::removePoint(const QPointF& point) |
|
55 | 55 | { |
|
56 | 56 | Q_ASSERT(m_data.count() == m_points.count()); |
|
57 | 57 | int index = m_data.lastIndexOf(point,0); |
|
58 | 58 | m_data.remove(index); |
|
59 | 59 | delete(m_points.takeAt(index)); |
|
60 | 60 | } |
|
61 | 61 | |
|
62 | 62 | void LineChartItem::setPoint(const QPointF& oldPoint,const QPointF& newPoint) |
|
63 | 63 | { |
|
64 | 64 | Q_ASSERT(m_data.count() == m_points.count()); |
|
65 | 65 | int index = m_data.lastIndexOf(oldPoint,0); |
|
66 | 66 | |
|
67 | 67 | if(index > -1){ |
|
68 | 68 | m_data.replace(index,newPoint); |
|
69 | 69 | QGraphicsItem* item = m_points.at(index); |
|
70 | 70 | item->setPos(newPoint.x()-1,newPoint.y()-1); |
|
71 | 71 | } |
|
72 | 72 | } |
|
73 | 73 | |
|
74 | 74 | void LineChartItem::setPoint(int index,const QPointF& point) |
|
75 | 75 | { |
|
76 | 76 | Q_ASSERT(m_data.count() == m_points.count()); |
|
77 | 77 | Q_ASSERT(index>=0); |
|
78 | 78 | |
|
79 | 79 | m_data.replace(index,point); |
|
80 | 80 | QGraphicsItem* item = m_points.at(index); |
|
81 | 81 | item->setPos(point.x()-1,point.y()-1); |
|
82 | 82 | } |
|
83 | 83 | |
|
84 | 84 | void LineChartItem::clear() |
|
85 | 85 | { |
|
86 | 86 | qDeleteAll(m_points); |
|
87 | 87 | m_points.clear(); |
|
88 | 88 | m_hash.clear(); |
|
89 | 89 | m_path = QPainterPath(); |
|
90 | 90 | m_rect = QRect(); |
|
91 | 91 | m_data.clear(); |
|
92 | 92 | } |
|
93 | 93 | |
|
94 | 94 | void LineChartItem::clearView() |
|
95 | 95 | { |
|
96 | 96 | qDeleteAll(m_points); |
|
97 | 97 | m_points.clear(); |
|
98 | 98 | m_path = QPainterPath(); |
|
99 | 99 | m_rect = QRect(); |
|
100 | 100 | m_data.clear(); |
|
101 | 101 | } |
|
102 | 102 | |
|
103 | 103 | void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
104 | 104 | { |
|
105 | 105 | Q_UNUSED(widget); |
|
106 | 106 | Q_UNUSED(option); |
|
107 | 107 | painter->save(); |
|
108 | 108 | painter->setPen(m_pen); |
|
109 | 109 | painter->setClipRect(m_clipRect); |
|
110 | 110 | painter->drawPath(m_path); |
|
111 | 111 | painter->restore(); |
|
112 | 112 | } |
|
113 | 113 | |
|
114 | 114 | void LineChartItem::calculatePoint(QPointF& point, int index, const QLineChartSeries* series,const QSizeF& size, const Domain& domain) const |
|
115 | 115 | { |
|
116 | 116 | const qreal deltaX = size.width()/domain.spanX(); |
|
117 | 117 | const qreal deltaY = size.height()/domain.spanY(); |
|
118 | 118 | qreal x = (series->x(index) - domain.m_minX)* deltaX; |
|
119 | 119 | qreal y = (series->y(index) - domain.m_minY)*-deltaY + size.height(); |
|
120 | 120 | point.setX(x); |
|
121 | 121 | point.setY(y); |
|
122 | 122 | } |
|
123 | 123 | |
|
124 | 124 | |
|
125 | 125 | void LineChartItem::calculatePoints(QVector<QPointF>& points, QHash<int,int>& hash,const QLineChartSeries* series,const QSizeF& size, const Domain& domain) const |
|
126 | 126 | { |
|
127 | 127 | const qreal deltaX = size.width()/domain.spanX(); |
|
128 | 128 | const qreal deltaY = size.height()/domain.spanY(); |
|
129 | 129 | |
|
130 | 130 | for (int i = 0; i < series->count(); ++i) { |
|
131 | 131 | qreal x = (series->x(i) - domain.m_minX)* deltaX; |
|
132 | 132 | qreal y = (series->y(i) - domain.m_minY)*-deltaY + size.height(); |
|
133 | 133 | hash[i] = points.size(); |
|
134 | 134 | points << QPointF(x,y); |
|
135 | 135 | } |
|
136 | 136 | } |
|
137 | 137 | |
|
138 | 138 | void LineChartItem::updateDomain() |
|
139 | 139 | { |
|
140 | 140 | clear(); |
|
141 | 141 | prepareGeometryChange(); |
|
142 | 142 | calculatePoints(m_data,m_hash,m_series,m_size, m_domain); |
|
143 | 143 | addPoints(m_data); |
|
144 | 144 | } |
|
145 | 145 | |
|
146 | 146 | void LineChartItem::updateData() |
|
147 | 147 | { |
|
148 | 148 | //for now the same |
|
149 | 149 | updateDomain(); |
|
150 | 150 | } |
|
151 | 151 | |
|
152 | 152 | void LineChartItem::updateGeometry() |
|
153 | 153 | { |
|
154 | 154 | |
|
155 | 155 | if(m_data.size()==0) return; |
|
156 | 156 | |
|
157 | 157 | prepareGeometryChange(); |
|
158 | 158 | QPainterPath path; |
|
159 | 159 | const QPointF& point = m_data.at(0); |
|
160 | 160 | path.moveTo(point); |
|
161 | 161 | |
|
162 | 162 | foreach( const QPointF& point , m_data) { |
|
163 | 163 | path.lineTo(point); |
|
164 | 164 | } |
|
165 | 165 | |
|
166 | 166 | m_path = path; |
|
167 | 167 | m_rect = path.boundingRect(); |
|
168 | 168 | } |
|
169 | 169 | |
|
170 | 170 | void LineChartItem::setPen(const QPen& pen) |
|
171 | 171 | { |
|
172 | 172 | m_pen = pen; |
|
173 | 173 | } |
|
174 | 174 | |
|
175 | 175 | //handlers |
|
176 | 176 | |
|
177 | 177 | void LineChartItem::handleModelChanged(int index) |
|
178 | 178 | { |
|
179 | 179 | Q_ASSERT(index<m_series->count()); |
|
180 | 180 | if(m_hash.contains(index)){ |
|
181 | 181 | int i = m_hash.value(index); |
|
182 | 182 | QPointF point; |
|
183 | 183 | calculatePoint(point,index,m_series,m_size,m_domain); |
|
184 | 184 | setPoint(i,point); |
|
185 | 185 | } |
|
186 | 186 | update(); |
|
187 | 187 | } |
|
188 | 188 | |
|
189 | 189 | void LineChartItem::handleDomainChanged(const Domain& domain) |
|
190 | 190 | { |
|
191 | 191 | m_domain = domain; |
|
192 | 192 | updateDomain(); |
|
193 | 193 | update(); |
|
194 | 194 | } |
|
195 | 195 | |
|
196 | 196 | void LineChartItem::handleGeometryChanged(const QRectF& rect) |
|
197 | 197 | { |
|
198 | 198 | Q_ASSERT(rect.isValid()); |
|
199 | 199 | m_size=rect.size(); |
|
200 | 200 | m_clipRect=rect.translated(-rect.topLeft()); |
|
201 | 201 | updateDomain(); |
|
202 | 202 | updateGeometry(); |
|
203 | 203 | setPos(rect.topLeft()); |
|
204 | 204 | update(); |
|
205 | 205 | } |
|
206 | 206 | |
|
207 | 207 | |
|
208 | 208 | #include "moc_linechartitem_p.cpp" |
|
209 | 209 | |
|
210 | 210 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,158 +1,161 | |||
|
1 | 1 | #include "qchart.h" |
|
2 | 2 | #include "qchartaxis.h" |
|
3 | 3 | #include "chartpresenter_p.h" |
|
4 | 4 | #include "chartdataset_p.h" |
|
5 | 5 | #include <QGraphicsScene> |
|
6 | 6 | #include <QGraphicsSceneResizeEvent> |
|
7 | 7 | #include <QDebug> |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | 10 | |
|
11 | 11 | QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), |
|
12 | 12 | m_backgroundItem(0), |
|
13 | 13 | m_titleItem(0), |
|
14 | 14 | m_dataset(new ChartDataSet(this)), |
|
15 | 15 | m_presenter(new ChartPresenter(this,m_dataset)) |
|
16 | 16 | { |
|
17 | 17 | } |
|
18 | 18 | |
|
19 | 19 | QChart::~QChart() {} |
|
20 | 20 | |
|
21 | 21 | void QChart::addSeries(QChartSeries* series,QChartAxis* axisY) |
|
22 | 22 | { |
|
23 | 23 | m_dataset->addSeries(series,axisY); |
|
24 | 24 | } |
|
25 | 25 | |
|
26 | 26 | void QChart::removeSeries(QChartSeries* series) |
|
27 | 27 | { |
|
28 | 28 | m_dataset->removeSeries(series); |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | void QChart::removeAllSeries() |
|
32 | 32 | { |
|
33 | 33 | m_dataset->removeAllSeries(); |
|
34 | 34 | } |
|
35 | 35 | |
|
36 | 36 | void QChart::setChartBackgroundBrush(const QBrush& brush) |
|
37 | 37 | { |
|
38 | 38 | createChartBackgroundItem(); |
|
39 | 39 | m_backgroundItem->setBrush(brush); |
|
40 | 40 | m_backgroundItem->update(); |
|
41 | 41 | } |
|
42 | 42 | |
|
43 | 43 | void QChart::setChartBackgroundPen(const QPen& pen) |
|
44 | 44 | { |
|
45 | 45 | createChartBackgroundItem(); |
|
46 | 46 | m_backgroundItem->setPen(pen); |
|
47 | 47 | m_backgroundItem->update(); |
|
48 | 48 | } |
|
49 | 49 | |
|
50 | 50 | void QChart::setChartTitle(const QString& title) |
|
51 | 51 | { |
|
52 | 52 | createChartTitleItem(); |
|
53 | 53 | m_titleItem->setPlainText(title); |
|
54 | 54 | } |
|
55 | 55 | |
|
56 | 56 | void QChart::setChartTitleFont(const QFont& font) |
|
57 | 57 | { |
|
58 | 58 | createChartTitleItem(); |
|
59 | 59 | m_titleItem->setFont(font); |
|
60 | 60 | } |
|
61 | 61 | |
|
62 | 62 | void QChart::createChartBackgroundItem() |
|
63 | 63 | { |
|
64 | 64 | if(!m_backgroundItem) { |
|
65 | 65 | m_backgroundItem = new QGraphicsRectItem(this); |
|
66 |
m_backgroundItem->setZValue( |
|
|
66 | m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); | |
|
67 | 67 | } |
|
68 | 68 | } |
|
69 | 69 | |
|
70 | 70 | void QChart::createChartTitleItem() |
|
71 | 71 | { |
|
72 | if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this); | |
|
72 | if(!m_titleItem) { | |
|
73 | m_titleItem = new QGraphicsTextItem(this); | |
|
74 | m_titleItem->setZValue(ChartPresenter::BackgroundZValue); | |
|
75 | } | |
|
73 | 76 | } |
|
74 | 77 | |
|
75 | 78 | int QChart::margin() const |
|
76 | 79 | { |
|
77 | 80 | return m_presenter->margin(); |
|
78 | 81 | } |
|
79 | 82 | |
|
80 | 83 | void QChart::setMargin(int margin) |
|
81 | 84 | { |
|
82 | 85 | m_presenter->setMargin(margin); |
|
83 | 86 | } |
|
84 | 87 | |
|
85 | 88 | void QChart::setChartTheme(QChart::ChartTheme theme) |
|
86 | 89 | { |
|
87 | 90 | m_presenter->setChartTheme(theme); |
|
88 | 91 | } |
|
89 | 92 | |
|
90 | 93 | QChart::ChartTheme QChart::chartTheme() const |
|
91 | 94 | { |
|
92 | 95 | return m_presenter->chartTheme(); |
|
93 | 96 | } |
|
94 | 97 | |
|
95 | 98 | void QChart::zoomIn() |
|
96 | 99 | { |
|
97 | 100 | if (!m_dataset->nextDomain()) { |
|
98 | 101 | QRectF rect = m_presenter->geometry(); |
|
99 | 102 | rect.setWidth(rect.width()/2); |
|
100 | 103 | rect.setHeight(rect.height()/2); |
|
101 | 104 | rect.moveCenter(m_presenter->geometry().center()); |
|
102 | 105 | zoomIn(rect); |
|
103 | 106 | } |
|
104 | 107 | } |
|
105 | 108 | |
|
106 | 109 | void QChart::zoomIn(const QRectF& rect) |
|
107 | 110 | { |
|
108 | 111 | if(!rect.isValid()) return; |
|
109 | 112 | QRectF r = rect.normalized(); |
|
110 | 113 | int margin = m_presenter->margin(); |
|
111 | 114 | r.translate(-margin, -margin); |
|
112 | 115 | m_dataset->addDomain(r,m_presenter->geometry()); |
|
113 | 116 | } |
|
114 | 117 | |
|
115 | 118 | void QChart::zoomOut() |
|
116 | 119 | { |
|
117 | 120 | m_dataset->previousDomain(); |
|
118 | 121 | } |
|
119 | 122 | |
|
120 | 123 | void QChart::zoomReset() |
|
121 | 124 | { |
|
122 | 125 | m_dataset->clearDomains(); |
|
123 | 126 | } |
|
124 | 127 | |
|
125 | 128 | QChartAxis* QChart::axisX() const |
|
126 | 129 | { |
|
127 | 130 | return m_dataset->axisX(); |
|
128 | 131 | } |
|
129 | 132 | |
|
130 | 133 | QChartAxis* QChart::axisY() const |
|
131 | 134 | { |
|
132 | 135 | return m_dataset->axisY(); |
|
133 | 136 | } |
|
134 | 137 | |
|
135 | 138 | void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
136 | 139 | { |
|
137 | 140 | |
|
138 | 141 | m_rect = QRectF(QPoint(0,0),event->newSize()); |
|
139 | 142 | QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); |
|
140 | 143 | |
|
141 | 144 | // recalculate title position |
|
142 | 145 | if (m_titleItem) { |
|
143 | 146 | QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); |
|
144 | 147 | m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2); |
|
145 | 148 | } |
|
146 | 149 | |
|
147 | 150 | //recalculate background gradient |
|
148 | 151 | if (m_backgroundItem) { |
|
149 | 152 | m_backgroundItem->setRect(rect); |
|
150 | 153 | } |
|
151 | 154 | |
|
152 | 155 | QGraphicsWidget::resizeEvent(event); |
|
153 | 156 | update(); |
|
154 | 157 | } |
|
155 | 158 | |
|
156 | 159 | #include "moc_qchart.cpp" |
|
157 | 160 | |
|
158 | 161 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now