@@ -1,31 +1,45 | |||
|
1 | 1 | #include "plotdomain_p.h" |
|
2 | 2 | |
|
3 | 3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
4 | 4 | |
|
5 | 5 | PlotDomain::PlotDomain(): |
|
6 | 6 | m_minX(0), |
|
7 | 7 | m_maxX(0), |
|
8 | 8 | m_minY(0), |
|
9 | 9 | m_maxY(0) |
|
10 | 10 | { |
|
11 | 11 | |
|
12 | 12 | } |
|
13 | 13 | |
|
14 | 14 | PlotDomain::~PlotDomain() |
|
15 | 15 | { |
|
16 | 16 | // TODO Auto-generated destructor stub |
|
17 | 17 | } |
|
18 | 18 | |
|
19 | 19 | qreal PlotDomain::spanX() const |
|
20 | 20 | { |
|
21 | 21 | Q_ASSERT(m_maxX >= m_minX); |
|
22 | 22 | return m_maxX - m_minX; |
|
23 | 23 | } |
|
24 | 24 | |
|
25 | 25 | qreal PlotDomain::spanY() const |
|
26 | 26 | { |
|
27 | 27 | Q_ASSERT(m_maxY >= m_minY); |
|
28 | 28 | return m_maxY - m_minY; |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | PlotDomain PlotDomain::subDomain(const QRect& rect, qreal maxWidth,qreal maxHeight) const | |
|
32 | { | |
|
33 | PlotDomain domain; | |
|
34 | ||
|
35 | qreal dx = spanX() / maxWidth; | |
|
36 | qreal dy = spanY() / maxHeight; | |
|
37 | ||
|
38 | domain.m_minX = m_minX + dx * rect.left(); | |
|
39 | domain.m_maxX = m_minX + dx * rect.right(); | |
|
40 | domain.m_minY = m_maxY - dy * rect.bottom(); | |
|
41 | domain.m_maxY = m_maxY - dy * rect.top(); | |
|
42 | ||
|
43 | return domain; | |
|
44 | } | |
|
31 | 45 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,25 +1,28 | |||
|
1 | 1 | #ifndef PLOTDOMAIN_H_ |
|
2 | 2 | #define PLOTDOMAIN_H_ |
|
3 | 3 | #include "qchartglobal.h" |
|
4 | 4 | #include <QRect> |
|
5 | 5 | |
|
6 | 6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | 7 | |
|
8 | 8 | class PlotDomain { |
|
9 | 9 | public: |
|
10 | 10 | PlotDomain(); |
|
11 | 11 | virtual ~PlotDomain(); |
|
12 | 12 | |
|
13 | 13 | qreal spanX() const; |
|
14 | 14 | qreal spanY() const; |
|
15 | 15 | |
|
16 | PlotDomain subDomain(const QRect& rect, qreal maxWidth, qreal maxHeight) const; | |
|
17 | ||
|
18 | ||
|
16 | 19 | public: |
|
17 | 20 | qreal m_minX; |
|
18 | 21 | qreal m_maxX; |
|
19 | 22 | qreal m_minY; |
|
20 | 23 | qreal m_maxY; |
|
21 | 24 | }; |
|
22 | 25 | |
|
23 | 26 | QTCOMMERCIALCHART_END_NAMESPACE |
|
24 | 27 | |
|
25 | 28 | #endif /* PLOTTER_H_ */ |
@@ -1,319 +1,312 | |||
|
1 | 1 | #include "qchart.h" |
|
2 | 2 | #include "qchartseries.h" |
|
3 | 3 | #include "qscatterseries.h" |
|
4 | 4 | #include "qscatterseries_p.h" |
|
5 | 5 | #include "qpieseries.h" |
|
6 | 6 | #include "qxychartseries.h" |
|
7 | 7 | |
|
8 | 8 | #include "barchartseries.h" |
|
9 | 9 | #include "bargroup.h" |
|
10 | 10 | |
|
11 | 11 | #include "xylinechartitem_p.h" |
|
12 | 12 | #include "plotdomain_p.h" |
|
13 | 13 | #include "axisitem_p.h" |
|
14 | 14 | #include <QGraphicsScene> |
|
15 | 15 | #include <QDebug> |
|
16 | 16 | |
|
17 | 17 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
18 | 18 | |
|
19 | 19 | QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent), |
|
20 | 20 | m_background(new QGraphicsRectItem(this)), |
|
21 | 21 | m_title(new QGraphicsTextItem(this)), |
|
22 | 22 | m_axisX(new AxisItem(AxisItem::X_AXIS,this)), |
|
23 | 23 | m_axisY(new AxisItem(AxisItem::Y_AXIS,this)), |
|
24 | 24 | m_plotDataIndex(0), |
|
25 | 25 | m_marginSize(0) |
|
26 | 26 | { |
|
27 | 27 | // TODO: the default theme? |
|
28 | 28 | setTheme(QChart::ChartThemeVanilla); |
|
29 | 29 | // setFlags(QGraphicsItem::ItemClipsChildrenToShape); |
|
30 | 30 | PlotDomain domain; |
|
31 | 31 | m_plotDomainList<<domain; |
|
32 | 32 | |
|
33 | 33 | m_chartItems<<m_axisX; |
|
34 | 34 | m_chartItems<<m_axisY; |
|
35 | 35 | } |
|
36 | 36 | |
|
37 | 37 | QChart::~QChart(){} |
|
38 | 38 | |
|
39 | 39 | QRectF QChart::boundingRect() const |
|
40 | 40 | { |
|
41 | 41 | return m_rect; |
|
42 | 42 | } |
|
43 | 43 | |
|
44 | 44 | void QChart::addSeries(QChartSeries* series) |
|
45 | 45 | { |
|
46 | 46 | // TODO: we should check the series not already added |
|
47 | 47 | |
|
48 | 48 | m_chartSeries << series; |
|
49 | 49 | |
|
50 | 50 | switch(series->type()) |
|
51 | 51 | { |
|
52 | 52 | case QChartSeries::SeriesTypeLine: { |
|
53 | 53 | |
|
54 | 54 | QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series); |
|
55 | 55 | // Use color defined by theme in case the series does not define a custom color |
|
56 | 56 | if (!xyseries->color().isValid() && m_themeColors.count()) |
|
57 | 57 | xyseries->setColor(nextColor()); |
|
58 | 58 | |
|
59 | 59 | m_plotDataIndex = 0 ; |
|
60 | 60 | m_plotDomainList.resize(1); |
|
61 | 61 | |
|
62 | 62 | PlotDomain& domain = m_plotDomainList[m_plotDataIndex]; |
|
63 | 63 | |
|
64 | 64 | for (int i = 0 ; i < xyseries->count() ; i++) |
|
65 | 65 | { |
|
66 | 66 | qreal x = xyseries->x(i); |
|
67 | 67 | qreal y = xyseries->y(i); |
|
68 | 68 | domain.m_minX = qMin(domain.m_minX,x); |
|
69 | 69 | domain.m_minY = qMin(domain.m_minY,y); |
|
70 | 70 | domain.m_maxX = qMax(domain.m_maxX,x); |
|
71 | 71 | domain.m_maxY = qMax(domain.m_maxY,y); |
|
72 | 72 | } |
|
73 | 73 | |
|
74 | 74 | XYLineChartItem* item = new XYLineChartItem(xyseries,this); |
|
75 | 75 | m_chartItems<<item; |
|
76 | 76 | |
|
77 | 77 | foreach(ChartItem* i ,m_chartItems) |
|
78 | 78 | i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex)); |
|
79 | 79 | |
|
80 | 80 | break; |
|
81 | 81 | } |
|
82 | 82 | case QChartSeries::SeriesTypeBar: { |
|
83 | 83 | |
|
84 | 84 | qDebug() << "barSeries added"; |
|
85 | 85 | BarChartSeries* barSeries = static_cast<BarChartSeries*>(series); |
|
86 | 86 | connect(this, SIGNAL(sizeChanged(QRectF)), |
|
87 | 87 | barSeries, SLOT(chartSizeChanged(QRectF))); |
|
88 | 88 | |
|
89 | 89 | break; |
|
90 | 90 | } |
|
91 | 91 | case QChartSeries::SeriesTypeScatter: { |
|
92 | 92 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); |
|
93 | 93 | scatterSeries->d->setParentItem(this); |
|
94 | 94 | // Set pre-defined colors in case the series has no colors defined |
|
95 | 95 | if (!scatterSeries->markerColor().isValid()) |
|
96 | 96 | scatterSeries->setMarkerColor(nextColor()); |
|
97 | 97 | connect(this, SIGNAL(sizeChanged(QRectF)), |
|
98 | 98 | scatterSeries, SLOT(chartSizeChanged(QRectF))); |
|
99 | 99 | // QColor nextColor = m_themeColors.takeFirst(); |
|
100 | 100 | // nextColor.setAlpha(150); // TODO: default opacity? |
|
101 | 101 | // scatterSeries->setMarkerColor(nextColor); |
|
102 | 102 | break; |
|
103 | 103 | } |
|
104 | 104 | case QChartSeries::SeriesTypePie: { |
|
105 | 105 | QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series); |
|
106 | 106 | for (int i(0); i < pieSeries->sliceCount(); i++) { |
|
107 | 107 | if (!pieSeries->sliceColor(i).isValid()) |
|
108 | 108 | pieSeries->setSliceColor(i, nextColor()); |
|
109 | 109 | } |
|
110 | 110 | connect(this, SIGNAL(sizeChanged(QRectF)), |
|
111 | 111 | pieSeries, SLOT(chartSizeChanged(QRectF))); |
|
112 | 112 | |
|
113 | 113 | // Set pre-defined colors in case the series has no colors defined |
|
114 | 114 | // TODO: how to define the color for all the slices of a pie? |
|
115 | 115 | // for (int (i); i < pieSeries.sliceCount(); i++) |
|
116 | 116 | break; |
|
117 | 117 | } |
|
118 | 118 | } |
|
119 | 119 | } |
|
120 | 120 | |
|
121 | 121 | QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type) |
|
122 | 122 | { |
|
123 | 123 | // TODO: support also other types; not only scatter and pie |
|
124 | 124 | |
|
125 | 125 | QChartSeries *series(0); |
|
126 | 126 | |
|
127 | 127 | switch (type) { |
|
128 | 128 | case QChartSeries::SeriesTypeLine: { |
|
129 | 129 | series = QXYChartSeries::create(); |
|
130 | 130 | break; |
|
131 | 131 | } |
|
132 | 132 | case QChartSeries::SeriesTypeBar: { |
|
133 | 133 | series = new BarChartSeries(this); |
|
134 | 134 | break; |
|
135 | 135 | } |
|
136 | 136 | case QChartSeries::SeriesTypeScatter: { |
|
137 | 137 | series = new QScatterSeries(this); |
|
138 | 138 | break; |
|
139 | 139 | } |
|
140 | 140 | case QChartSeries::SeriesTypePie: { |
|
141 | 141 | series = new QPieSeries(this); |
|
142 | 142 | break; |
|
143 | 143 | } |
|
144 | 144 | default: |
|
145 | 145 | Q_ASSERT(false); |
|
146 | 146 | break; |
|
147 | 147 | } |
|
148 | 148 | |
|
149 | 149 | addSeries(series); |
|
150 | 150 | return series; |
|
151 | 151 | } |
|
152 | 152 | |
|
153 | 153 | void QChart::setSize(const QSize& size) |
|
154 | 154 | { |
|
155 | 155 | m_rect = QRect(QPoint(0,0),size); |
|
156 | 156 | QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); |
|
157 | 157 | |
|
158 | 158 | |
|
159 | 159 | //recalculate background gradient |
|
160 | 160 | m_background->setRect(rect); |
|
161 | 161 | m_backgroundGradient.setFinalStop(0,m_background->rect().height()); |
|
162 | 162 | m_background->setBrush(m_backgroundGradient); |
|
163 | 163 | |
|
164 | 164 | //resize elements |
|
165 | 165 | foreach (ChartItem* item ,m_chartItems) { |
|
166 | 166 | item->setPos(rect.topLeft()); |
|
167 | 167 | item->setSize(rect.size()); |
|
168 | 168 | |
|
169 | 169 | } |
|
170 | 170 | // TODO: TTD for setting scale |
|
171 | 171 | //emit scaleChanged(100, 100); |
|
172 | 172 | // TODO: calculate the origo |
|
173 | 173 | // TODO: not sure if emitting a signal here is the best from performance point of view |
|
174 | 174 | emit sizeChanged(QRectF(0, 0, size.width(), size.height())); |
|
175 | 175 | |
|
176 | 176 | update(); |
|
177 | 177 | } |
|
178 | 178 | |
|
179 | 179 | void QChart::setBackgroundColor(const QColor& color) |
|
180 | 180 | { |
|
181 | 181 | m_backgroundGradient.setColorAt( 0.0, Qt::white); |
|
182 | 182 | m_backgroundGradient.setColorAt( 1.0, color); |
|
183 | 183 | m_background->setBrush(m_backgroundGradient); |
|
184 | 184 | m_background->setPen(Qt::NoPen); |
|
185 | 185 | m_background->update(); |
|
186 | 186 | } |
|
187 | 187 | |
|
188 | 188 | void QChart::setTitle(const QString& title) |
|
189 | 189 | { |
|
190 | 190 | m_title->setPlainText(title); |
|
191 | 191 | } |
|
192 | 192 | |
|
193 | 193 | int QChart::margin() const |
|
194 | 194 | { |
|
195 | 195 | return m_marginSize; |
|
196 | 196 | } |
|
197 | 197 | |
|
198 | 198 | void QChart::setMargin(int margin) |
|
199 | 199 | { |
|
200 | 200 | m_marginSize = margin; |
|
201 | 201 | } |
|
202 | 202 | |
|
203 | 203 | void QChart::setTheme(QChart::ChartThemeId theme) |
|
204 | 204 | { |
|
205 | 205 | // if (theme != m_currentTheme) { |
|
206 | 206 | m_themeColors.clear(); |
|
207 | 207 | |
|
208 | 208 | // TODO: define color themes |
|
209 | 209 | switch (theme) { |
|
210 | 210 | case QChart::ChartThemeVanilla: |
|
211 | 211 | m_themeColors.append(QColor(217, 197, 116)); |
|
212 | 212 | m_themeColors.append(QColor(214, 168, 150)); |
|
213 | 213 | m_themeColors.append(QColor(160, 160, 113)); |
|
214 | 214 | m_themeColors.append(QColor(210, 210, 52)); |
|
215 | 215 | m_themeColors.append(QColor(136, 114, 58)); |
|
216 | 216 | break; |
|
217 | 217 | case QChart::ChartThemeIcy: |
|
218 | 218 | m_themeColors.append(QColor(0, 3, 165)); |
|
219 | 219 | m_themeColors.append(QColor(49, 52, 123)); |
|
220 | 220 | m_themeColors.append(QColor(71, 114, 187)); |
|
221 | 221 | m_themeColors.append(QColor(48, 97, 87)); |
|
222 | 222 | m_themeColors.append(QColor(19, 71, 90)); |
|
223 | 223 | m_themeColors.append(QColor(110, 70, 228)); |
|
224 | 224 | break; |
|
225 | 225 | case QChart::ChartThemeGrayscale: |
|
226 | 226 | m_themeColors.append(QColor(0, 0, 0)); |
|
227 | 227 | m_themeColors.append(QColor(50, 50, 50)); |
|
228 | 228 | m_themeColors.append(QColor(100, 100, 100)); |
|
229 | 229 | m_themeColors.append(QColor(140, 140, 140)); |
|
230 | 230 | m_themeColors.append(QColor(180, 180, 180)); |
|
231 | 231 | break; |
|
232 | 232 | default: |
|
233 | 233 | Q_ASSERT(false); |
|
234 | 234 | break; |
|
235 | 235 | } |
|
236 | 236 | |
|
237 | 237 | foreach(QChartSeries* series, m_chartSeries) { |
|
238 | 238 | // TODO: other series interested on themes? |
|
239 | 239 | if (series->type() == QChartSeries::SeriesTypeLine) { |
|
240 | 240 | QXYChartSeries *lineseries = reinterpret_cast<QXYChartSeries *>(series); |
|
241 | 241 | lineseries->setColor(nextColor()); |
|
242 | 242 | } else if (series->type() == QChartSeries::SeriesTypeScatter) { |
|
243 | 243 | QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series); |
|
244 | 244 | scatter->setMarkerColor(nextColor()); |
|
245 | 245 | } else if (series->type() == QChartSeries::SeriesTypePie) { |
|
246 | 246 | QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series); |
|
247 | 247 | for (int i(0); i < pieSeries->sliceCount(); i++) |
|
248 | 248 | pieSeries->setSliceColor(i, nextColor()); |
|
249 | 249 | } |
|
250 | 250 | } |
|
251 | 251 | update(); |
|
252 | 252 | } |
|
253 | 253 | |
|
254 | 254 | QColor QChart::nextColor() |
|
255 | 255 | { |
|
256 | 256 | QColor nextColor = m_themeColors.first(); |
|
257 | 257 | m_themeColors.move(0, m_themeColors.size() - 1); |
|
258 | 258 | return nextColor; |
|
259 | 259 | } |
|
260 | 260 | |
|
261 | 261 | void QChart::zoomInToRect(const QRect& rectangle) |
|
262 | 262 | { |
|
263 | 263 | |
|
264 | 264 | if(!rectangle.isValid()) return; |
|
265 | 265 | |
|
266 | 266 | qreal margin = this->margin(); |
|
267 | 267 | |
|
268 | 268 | QRect rect = rectangle.normalized(); |
|
269 | 269 | rect.translate(-margin, -margin); |
|
270 | 270 | |
|
271 | 271 | PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex]; |
|
272 | PlotDomain newDomain; | |
|
273 | 272 | |
|
274 |
|
|
|
275 | qreal dy = oldDomain.spanY() / (m_rect.height() - 2 * margin); | |
|
276 | ||
|
277 | newDomain.m_minX = oldDomain.m_minX + dx * rect.left(); | |
|
278 | newDomain.m_maxX = oldDomain.m_minX + dx * rect.right(); | |
|
279 | newDomain.m_minY = oldDomain.m_maxY - dy * rect.bottom(); | |
|
280 | newDomain.m_maxY = oldDomain.m_maxY - dy * rect.top(); | |
|
273 | PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin); | |
|
281 | 274 | |
|
282 | 275 | m_plotDomainList.resize(m_plotDataIndex + 1); |
|
283 |
m_plotDomainList<< |
|
|
276 | m_plotDomainList<<domain; | |
|
284 | 277 | m_plotDataIndex++; |
|
285 | 278 | |
|
286 | 279 | foreach (ChartItem* item ,m_chartItems) |
|
287 | 280 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); |
|
288 | 281 | update(); |
|
289 | 282 | } |
|
290 | 283 | |
|
291 | 284 | void QChart::zoomIn() |
|
292 | 285 | { |
|
293 | 286 | if (m_plotDataIndex < m_plotDomainList.count() - 1) { |
|
294 | 287 | m_plotDataIndex++; |
|
295 | 288 | foreach (ChartItem* item ,m_chartItems) |
|
296 | 289 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); |
|
297 | 290 | update(); |
|
298 | 291 | }else{ |
|
299 | 292 | QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); |
|
300 | 293 | rect.setWidth(rect.width()/2); |
|
301 | 294 | rect.setHeight(rect.height()/2); |
|
302 | 295 | rect.moveCenter(m_rect.center()); |
|
303 | 296 | zoomInToRect(rect); |
|
304 | 297 | } |
|
305 | 298 | } |
|
306 | 299 | |
|
307 | 300 | void QChart::zoomOut() |
|
308 | 301 | { |
|
309 | 302 | if (m_plotDataIndex > 0) { |
|
310 | 303 | m_plotDataIndex--; |
|
311 | 304 | foreach (ChartItem* item ,m_chartItems) |
|
312 | 305 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); |
|
313 | 306 | update(); |
|
314 | 307 | } |
|
315 | 308 | } |
|
316 | 309 | |
|
317 | 310 | #include "moc_qchart.cpp" |
|
318 | 311 | |
|
319 | 312 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now