@@ -1,76 +1,77 | |||
|
1 | 1 | #include "pieslice.h" |
|
2 | 2 | #include <QPainter> |
|
3 | 3 | #include <QDebug> |
|
4 | 4 | |
|
5 | 5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | 6 | |
|
7 | 7 | PieSlice::PieSlice(const QColor& color, qreal startAngle, qreal span, QRectF rect) |
|
8 | 8 | : m_color(color), |
|
9 | 9 | m_startAngle(startAngle), |
|
10 | 10 | m_span(span), |
|
11 | 11 | m_rect(rect) |
|
12 | 12 | { |
|
13 | 13 | setAcceptHoverEvents(true); |
|
14 | 14 | } |
|
15 | 15 | |
|
16 | 16 | PieSlice::~PieSlice() |
|
17 | 17 | { |
|
18 | 18 | } |
|
19 | 19 | |
|
20 | 20 | QRectF PieSlice::boundingRect() const |
|
21 | 21 | { |
|
22 | 22 | return m_rect; |
|
23 | 23 | } |
|
24 | 24 | |
|
25 | 25 | QPainterPath PieSlice::shape() const |
|
26 | 26 | { |
|
27 | 27 | qreal angle = (-m_startAngle) + (90); |
|
28 | 28 | qreal span = -m_span; |
|
29 | 29 | |
|
30 | 30 | QPainterPath path; |
|
31 | 31 | path.moveTo(boundingRect().center()); |
|
32 | 32 | path.arcTo(boundingRect(), angle, span); |
|
33 | 33 | |
|
34 | 34 | // TODO: draw the shape so that it might have a hole in the center |
|
35 | 35 | // - Sin & Cos will be needed to find inner/outer arc endpoints |
|
36 | 36 | |
|
37 | 37 | // dx, dy are offsets from the center |
|
38 | 38 | //qreal l = boundingRect().height(); |
|
39 | 39 | //qreal dx = qSin(angle*(M_PI/180)) * l; |
|
40 | 40 | //qreal dy = qCos(angle*(M_PI/180)) * l; |
|
41 | 41 | |
|
42 | 42 | // TODO: exploded slice? |
|
43 | 43 | |
|
44 | 44 | return path; |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | 47 | void PieSlice::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
48 | 48 | { |
|
49 | 49 | // Setup painter |
|
50 | 50 | painter->setBrush(m_color); |
|
51 | painter->setRenderHint(QPainter::Antialiasing); | |
|
51 | 52 | QPen pen; |
|
52 | 53 | //pen.setColor(m_color.darker()); |
|
53 | 54 | pen.setColor(Qt::gray); |
|
54 | 55 | pen.setWidth(1); |
|
55 | 56 | painter->setPen(pen); |
|
56 | 57 | |
|
57 | 58 | // From Qt docs: |
|
58 | 59 | // The startAngle and spanAngle must be specified in 1/16th of a degree, i.e. a full circle equals 5760 (16 * 360). |
|
59 | 60 | // Positive values for the angles mean counter-clockwise while negative values mean the clockwise direction. |
|
60 | 61 | // Zero degrees is at the 3 o'clock position. |
|
61 | 62 | // |
|
62 | 63 | // For sake of simplicity convert this so that zero degrees is at 12 o'clock and full circle is 360. |
|
63 | 64 | //qreal angle = (-m_startAngle*16) + (90*16); |
|
64 | 65 | //qreal span = -m_span*16; |
|
65 | 66 | //painter->drawPie(boundingRect(), angle, span); |
|
66 | 67 | |
|
67 | 68 | painter->drawPath(shape()); |
|
68 | 69 | } |
|
69 | 70 | |
|
70 | 71 | void PieSlice::hoverEnterEvent(QGraphicsSceneHoverEvent * event) |
|
71 | 72 | { |
|
72 | 73 | QGraphicsItem::hoverEnterEvent(event); |
|
73 | 74 | qDebug() << "hover" << m_color << m_startAngle << m_span; |
|
74 | 75 | } |
|
75 | 76 | |
|
76 | 77 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,312 +1,334 | |||
|
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 | ||
|
217 | m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xff9d844d))); | |
|
218 | m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf))); | |
|
216 | 219 | break; |
|
217 | 220 | case QChart::ChartThemeIcy: |
|
218 | 221 | m_themeColors.append(QColor(0, 3, 165)); |
|
219 | 222 | m_themeColors.append(QColor(49, 52, 123)); |
|
220 | 223 | m_themeColors.append(QColor(71, 114, 187)); |
|
221 | 224 | m_themeColors.append(QColor(48, 97, 87)); |
|
222 | 225 | m_themeColors.append(QColor(19, 71, 90)); |
|
223 | 226 | m_themeColors.append(QColor(110, 70, 228)); |
|
227 | ||
|
228 | m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xffe4ffff))); | |
|
229 | m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf))); | |
|
224 | 230 | break; |
|
225 | 231 | case QChart::ChartThemeGrayscale: |
|
226 | 232 | m_themeColors.append(QColor(0, 0, 0)); |
|
227 | 233 | m_themeColors.append(QColor(50, 50, 50)); |
|
228 | 234 | m_themeColors.append(QColor(100, 100, 100)); |
|
229 | 235 | m_themeColors.append(QColor(140, 140, 140)); |
|
230 | 236 | m_themeColors.append(QColor(180, 180, 180)); |
|
237 | ||
|
238 | m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xffffffff))); | |
|
239 | m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf))); | |
|
240 | break; | |
|
241 | case QChart::ChartThemeUnnamed1: | |
|
242 | m_themeColors.append(QColor(QRgb(0xff3fa9f5))); | |
|
243 | m_themeColors.append(QColor(QRgb(0xff7AC943))); | |
|
244 | m_themeColors.append(QColor(QRgb(0xffFF931E))); | |
|
245 | m_themeColors.append(QColor(QRgb(0xffFF1D25))); | |
|
246 | m_themeColors.append(QColor(QRgb(0xffFF7BAC))); | |
|
247 | ||
|
248 | m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xfff3dc9e))); | |
|
249 | m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf))); | |
|
231 | 250 | break; |
|
232 | 251 | default: |
|
233 | 252 | Q_ASSERT(false); |
|
234 | 253 | break; |
|
235 | 254 | } |
|
236 | 255 | |
|
256 | m_background->setBrush(m_backgroundGradient); | |
|
257 | m_background->setPen(Qt::NoPen); | |
|
258 | ||
|
237 | 259 | foreach(QChartSeries* series, m_chartSeries) { |
|
238 | 260 | // TODO: other series interested on themes? |
|
239 | 261 | if (series->type() == QChartSeries::SeriesTypeLine) { |
|
240 | 262 | QXYChartSeries *lineseries = reinterpret_cast<QXYChartSeries *>(series); |
|
241 | 263 | lineseries->setColor(nextColor()); |
|
242 | 264 | } else if (series->type() == QChartSeries::SeriesTypeScatter) { |
|
243 | 265 | QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series); |
|
244 | 266 | scatter->setMarkerColor(nextColor()); |
|
245 | 267 | } else if (series->type() == QChartSeries::SeriesTypePie) { |
|
246 | 268 | QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series); |
|
247 | 269 | for (int i(0); i < pieSeries->sliceCount(); i++) |
|
248 | 270 | pieSeries->setSliceColor(i, nextColor()); |
|
249 | 271 | } |
|
250 | 272 | } |
|
251 | 273 | update(); |
|
252 | 274 | } |
|
253 | 275 | |
|
254 | 276 | QColor QChart::nextColor() |
|
255 | 277 | { |
|
256 | 278 | QColor nextColor = m_themeColors.first(); |
|
257 | 279 | m_themeColors.move(0, m_themeColors.size() - 1); |
|
258 | 280 | return nextColor; |
|
259 | 281 | } |
|
260 | 282 | |
|
261 | 283 | void QChart::zoomInToRect(const QRect& rectangle) |
|
262 | 284 | { |
|
263 | 285 | |
|
264 | 286 | if(!rectangle.isValid()) return; |
|
265 | 287 | |
|
266 | 288 | qreal margin = this->margin(); |
|
267 | 289 | |
|
268 | 290 | QRect rect = rectangle.normalized(); |
|
269 | 291 | rect.translate(-margin, -margin); |
|
270 | 292 | |
|
271 | 293 | PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex]; |
|
272 | 294 | |
|
273 | 295 | PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin); |
|
274 | 296 | |
|
275 | 297 | m_plotDomainList.resize(m_plotDataIndex + 1); |
|
276 | 298 | m_plotDomainList<<domain; |
|
277 | 299 | m_plotDataIndex++; |
|
278 | 300 | |
|
279 | 301 | foreach (ChartItem* item ,m_chartItems) |
|
280 | 302 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); |
|
281 | 303 | update(); |
|
282 | 304 | } |
|
283 | 305 | |
|
284 | 306 | void QChart::zoomIn() |
|
285 | 307 | { |
|
286 | 308 | if (m_plotDataIndex < m_plotDomainList.count() - 1) { |
|
287 | 309 | m_plotDataIndex++; |
|
288 | 310 | foreach (ChartItem* item ,m_chartItems) |
|
289 | 311 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); |
|
290 | 312 | update(); |
|
291 | 313 | }else{ |
|
292 | 314 | QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); |
|
293 | 315 | rect.setWidth(rect.width()/2); |
|
294 | 316 | rect.setHeight(rect.height()/2); |
|
295 | 317 | rect.moveCenter(m_rect.center()); |
|
296 | 318 | zoomInToRect(rect); |
|
297 | 319 | } |
|
298 | 320 | } |
|
299 | 321 | |
|
300 | 322 | void QChart::zoomOut() |
|
301 | 323 | { |
|
302 | 324 | if (m_plotDataIndex > 0) { |
|
303 | 325 | m_plotDataIndex--; |
|
304 | 326 | foreach (ChartItem* item ,m_chartItems) |
|
305 | 327 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); |
|
306 | 328 | update(); |
|
307 | 329 | } |
|
308 | 330 | } |
|
309 | 331 | |
|
310 | 332 | #include "moc_qchart.cpp" |
|
311 | 333 | |
|
312 | 334 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,91 +1,92 | |||
|
1 | 1 | #ifndef CHART_H |
|
2 | 2 | #define CHART_H |
|
3 | 3 | |
|
4 | 4 | #include <qchartglobal.h> |
|
5 | 5 | #include <qchartseries.h> |
|
6 | 6 | #include <QGraphicsObject> |
|
7 | 7 | #include <QLinearGradient> |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | 10 | |
|
11 | 11 | class AxisItem; |
|
12 | 12 | class QChartSeries; |
|
13 | 13 | class PlotDomain; |
|
14 | 14 | class ChartItem; |
|
15 | 15 | class BarGroup; |
|
16 | 16 | class QChartAxis; |
|
17 | 17 | |
|
18 | 18 | // TODO: We don't need to have QChart tied to QGraphicsItem: |
|
19 | 19 | //class QTCOMMERCIALCHART_EXPORT QChart |
|
20 | 20 | //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem { |
|
21 | 21 | // public: QChartGraphicsItem(QChart &chart); |
|
22 | 22 | |
|
23 | 23 | /*! |
|
24 | 24 | * TODO: define the responsibilities |
|
25 | 25 | */ |
|
26 | 26 | class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject |
|
27 | 27 | { |
|
28 | 28 | Q_OBJECT |
|
29 | 29 | public: |
|
30 | 30 | enum ChartThemeId { |
|
31 | 31 | ChartThemeVanilla = 0, |
|
32 | 32 | ChartThemeIcy, |
|
33 | ChartThemeGrayscale | |
|
33 | ChartThemeGrayscale, | |
|
34 | ChartThemeUnnamed1 | |
|
34 | 35 | }; |
|
35 | 36 | |
|
36 | 37 | public: |
|
37 | 38 | QChart(QGraphicsObject* parent = 0); |
|
38 | 39 | ~QChart(); |
|
39 | 40 | |
|
40 | 41 | //from QGraphicsItem |
|
41 | 42 | QRectF boundingRect() const; |
|
42 | 43 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){}; |
|
43 | 44 | |
|
44 | 45 | void addSeries(QChartSeries* series); |
|
45 | 46 | //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type); |
|
46 | 47 | // TODO: who owns the series now? maybe owned by chart and returned a reference instead... |
|
47 | 48 | QChartSeries* createSeries(QChartSeries::QChartSeriesType type); |
|
48 | 49 | |
|
49 | 50 | void setSize(const QSize& size); |
|
50 | 51 | void setMargin(int margin); |
|
51 | 52 | int margin() const; |
|
52 | 53 | void setTheme(QChart::ChartThemeId theme); |
|
53 | 54 | |
|
54 | 55 | void setTitle(const QString& title); |
|
55 | 56 | void setBackgroundColor(const QColor& color); |
|
56 | 57 | |
|
57 | 58 | void zoomInToRect(const QRect& rectangle); |
|
58 | 59 | void zoomIn(); |
|
59 | 60 | void zoomOut(); |
|
60 | 61 | |
|
61 | 62 | void setAxisX(QChartAxis* axis){}; |
|
62 | 63 | void setAxisY(QChartAxis* axis){}; |
|
63 | 64 | void setAxisY(QList<QChartAxis*> axis){}; |
|
64 | 65 | |
|
65 | 66 | |
|
66 | 67 | signals: |
|
67 | 68 | //TODO chage to const QSize& size |
|
68 | 69 | void sizeChanged(QRectF rect); |
|
69 | 70 | void scaleChanged(qreal xscale, qreal yscale); |
|
70 | 71 | |
|
71 | 72 | private: |
|
72 | 73 | QColor nextColor(); |
|
73 | 74 | |
|
74 | 75 | Q_DISABLE_COPY(QChart) |
|
75 | 76 | QGraphicsRectItem* m_background; |
|
76 | 77 | QLinearGradient m_backgroundGradient; |
|
77 | 78 | QGraphicsTextItem* m_title; |
|
78 | 79 | AxisItem* m_axisX; |
|
79 | 80 | AxisItem* m_axisY; |
|
80 | 81 | QRect m_rect; |
|
81 | 82 | QList<QChartSeries*> m_chartSeries; |
|
82 | 83 | QVector<PlotDomain> m_plotDomainList; |
|
83 | 84 | QList<ChartItem*> m_chartItems; |
|
84 | 85 | int m_plotDataIndex; |
|
85 | 86 | int m_marginSize; |
|
86 | 87 | QList<QColor> m_themeColors; |
|
87 | 88 | }; |
|
88 | 89 | |
|
89 | 90 | QTCOMMERCIALCHART_END_NAMESPACE |
|
90 | 91 | |
|
91 | 92 | #endif |
@@ -1,57 +1,99 | |||
|
1 | 1 | #include "qchartwidget.h" |
|
2 | 2 | #include "qchartseries.h" |
|
3 | 3 | #include <QGraphicsView> |
|
4 | 4 | #include <QGraphicsScene> |
|
5 | 5 | #include <QResizeEvent> |
|
6 | 6 | |
|
7 | 7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | 8 | |
|
9 | 9 | QChartWidget::QChartWidget(QWidget *parent) : |
|
10 | QGraphicsView(parent) | |
|
10 | QGraphicsView(parent), | |
|
11 | m_rubberBand(QRubberBand::Rectangle, this) | |
|
11 | 12 | { |
|
12 | 13 | setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
|
13 | 14 | m_scene = new QGraphicsScene(); |
|
14 | 15 | setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
15 | 16 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
16 | 17 | setScene(m_scene); |
|
17 | 18 | |
|
18 | 19 | m_chart = new QChart(); |
|
19 | 20 | m_scene->addItem(m_chart); |
|
21 | m_rubberBand.setEnabled(true); // TODO: should zoom be enabled by default? | |
|
20 | 22 | show(); |
|
21 | 23 | } |
|
22 | 24 | |
|
23 | 25 | QChartWidget::~QChartWidget() |
|
24 | 26 | { |
|
25 | 27 | } |
|
26 | 28 | |
|
27 | 29 | void QChartWidget::resizeEvent(QResizeEvent *event) |
|
28 | 30 | { |
|
29 | 31 | m_scene->setSceneRect(0,0,size().width(),size().height()); |
|
30 | 32 | m_chart->setSize(size()); |
|
31 | 33 | QWidget::resizeEvent(event); |
|
32 | 34 | } |
|
33 | 35 | |
|
34 | 36 | QSize QChartWidget::sizeHint() const |
|
35 | 37 | { |
|
36 | 38 | // TODO: calculate size hint based on contents? |
|
37 | 39 | return QSize(100, 100); |
|
38 | 40 | } |
|
39 | 41 | |
|
42 | void QChartWidget::mousePressEvent(QMouseEvent *event) | |
|
43 | { | |
|
44 | if(m_rubberBand.isEnabled() && event->button() == Qt::LeftButton) { | |
|
45 | int margin = m_chart->margin(); | |
|
46 | QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin); | |
|
47 | m_origin = event->pos(); | |
|
48 | ||
|
49 | if (rect.contains(m_origin)) { | |
|
50 | m_rubberBand.setGeometry(QRect(m_origin, QSize())); | |
|
51 | m_rubberBand.show(); | |
|
52 | event->accept(); | |
|
53 | } | |
|
54 | } | |
|
55 | } | |
|
56 | ||
|
57 | void QChartWidget::mouseMoveEvent(QMouseEvent *event) | |
|
58 | { | |
|
59 | if(m_rubberBand.isVisible()) | |
|
60 | m_rubberBand.setGeometry(QRect(m_origin, event->pos()).normalized()); | |
|
61 | } | |
|
62 | ||
|
63 | void QChartWidget::mouseReleaseEvent(QMouseEvent *event) | |
|
64 | { | |
|
65 | if (event->button() == Qt::LeftButton && m_rubberBand.isVisible()) { | |
|
66 | m_rubberBand.hide(); | |
|
67 | QRect rect = m_rubberBand.geometry(); | |
|
68 | m_chart->zoomInToRect(rect); | |
|
69 | event->accept(); | |
|
70 | } | |
|
71 | ||
|
72 | if(event->button()==Qt::RightButton) { | |
|
73 | m_chart->zoomOut(); | |
|
74 | } | |
|
75 | } | |
|
76 | ||
|
40 | 77 | void QChartWidget::addSeries(QChartSeries* series) |
|
41 | 78 | { |
|
42 | 79 | m_chart->addSeries(series); |
|
43 | 80 | } |
|
44 | 81 | |
|
45 | 82 | QChartSeries* QChartWidget::createSeries(QChartSeries::QChartSeriesType type) |
|
46 | 83 | { |
|
47 | 84 | return m_chart->createSeries(type); |
|
48 | 85 | } |
|
49 | 86 | |
|
50 | 87 | void QChartWidget::setTheme(QChart::ChartThemeId theme) |
|
51 | 88 | { |
|
52 | 89 | m_chart->setTheme(theme); |
|
53 | 90 | } |
|
54 | 91 | |
|
92 | void QChartWidget::setZoomEnabled(bool enabled) | |
|
93 | { | |
|
94 | m_rubberBand.setEnabled(enabled); | |
|
95 | } | |
|
96 | ||
|
55 | 97 | #include "moc_qchartwidget.cpp" |
|
56 | 98 | |
|
57 | 99 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,46 +1,56 | |||
|
1 | 1 | #ifndef QCHARTWIDGET_H |
|
2 | 2 | #define QCHARTWIDGET_H |
|
3 | 3 | |
|
4 | 4 | #include "qchartglobal.h" |
|
5 | 5 | #include "qchart.h" |
|
6 | 6 | #include <QGraphicsView> |
|
7 | #include <QRubberBand> | |
|
7 | 8 | |
|
8 | 9 | class QGraphicsScene; |
|
10 | class QRubberBand; | |
|
9 | 11 | |
|
10 | 12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
11 | 13 | |
|
12 | 14 | class QChartSeries; |
|
13 | 15 | class QChartWidgetPrivate; |
|
14 | 16 | |
|
15 | 17 | class QTCOMMERCIALCHART_EXPORT QChartWidget : public QGraphicsView |
|
16 | 18 | { |
|
17 | 19 | Q_OBJECT |
|
18 | 20 | public: |
|
19 | 21 | explicit QChartWidget(QWidget *parent = 0); |
|
20 | 22 | ~QChartWidget(); |
|
21 | 23 | |
|
22 | //implement from QWidget | |
|
23 | void resizeEvent(QResizeEvent *event); | |
|
24 | QSize sizeHint() const; | |
|
25 | ||
|
26 | 24 | // TODO: addSeries and createSeries are optional solutions |
|
27 | 25 | // TODO: currently createSeries assumes x, y value pairs. This isn't case with all charts. So is there another createSeries for other types (for example one list of ints)? |
|
26 | public Q_SLOTS: | |
|
28 | 27 | void addSeries(QChartSeries* series); |
|
29 | 28 | QChartSeries* createSeries(QChartSeries::QChartSeriesType type); |
|
30 | 29 | |
|
31 | 30 | /*! |
|
32 | 31 | * Set color theme for the chart. Themes define harmonic colors for the graphical elements of |
|
33 | 32 | * the chart. |
|
34 | 33 | */ |
|
35 | 34 | void setTheme(QChart::ChartThemeId theme); |
|
36 | 35 | |
|
36 | void setZoomEnabled(bool enabled); | |
|
37 | ||
|
38 | private: // From QWidget TODO: should these be protected instead? Is QChartWidget meant to be extened by the user? | |
|
39 | void resizeEvent(QResizeEvent *event); | |
|
40 | QSize sizeHint() const; | |
|
41 | void mousePressEvent(QMouseEvent *event); | |
|
42 | void mouseMoveEvent(QMouseEvent *event); | |
|
43 | void mouseReleaseEvent(QMouseEvent *event); | |
|
44 | ||
|
37 | 45 | private: |
|
38 | 46 | Q_DISABLE_COPY(QChartWidget) |
|
47 | // TODO: move the following to pimpl | |
|
39 | 48 | QGraphicsScene *m_scene; |
|
40 | 49 | QChart* m_chart; |
|
41 | ||
|
50 | QRubberBand m_rubberBand; | |
|
51 | QPoint m_origin; | |
|
42 | 52 | }; |
|
43 | 53 | |
|
44 | 54 | QTCOMMERCIALCHART_END_NAMESPACE |
|
45 | 55 | |
|
46 | 56 | #endif // QCHARTWIDGET_H |
@@ -1,106 +1,148 | |||
|
1 | 1 | #include "qpieseries.h" |
|
2 | 2 | #include "pieslice.h" |
|
3 | 3 | #include <QGraphicsObject> |
|
4 | 4 | #include <QDebug> |
|
5 | 5 | |
|
6 | 6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | 7 | |
|
8 | 8 | QPieSeries::QPieSeries(QGraphicsObject *parent) : |
|
9 | 9 | QChartSeries(parent), |
|
10 | m_sizeFactor(1.0) | |
|
10 | m_sizeFactor(1.0), | |
|
11 | m_position(PiePositionMaximized) | |
|
11 | 12 | { |
|
12 | 13 | } |
|
13 | 14 | |
|
14 | 15 | QPieSeries::~QPieSeries() |
|
15 | 16 | { |
|
16 | 17 | while (m_slices.count()) |
|
17 | 18 | delete m_slices.takeLast(); |
|
18 | 19 | } |
|
19 | 20 | |
|
20 | 21 | bool QPieSeries::setData(QList<qreal> data) |
|
21 | 22 | { |
|
22 | 23 | m_data = data; |
|
23 | 24 | |
|
24 | 25 | // Create slices |
|
25 | 26 | qreal fullPie = 360; |
|
26 | 27 | qreal total = 0; |
|
27 | 28 | foreach (qreal value, m_data) |
|
28 | 29 | total += value; |
|
29 | 30 | |
|
30 | 31 | QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent()); |
|
31 | 32 | Q_ASSERT(parentItem); |
|
32 | 33 | m_chartSize = parentItem->boundingRect(); |
|
33 | 34 | qreal angle = 0; |
|
34 | 35 | // TODO: no need to create new slices in case size changed; we should re-use the existing ones |
|
35 | 36 | foreach (qreal value, m_data) { |
|
36 | 37 | qreal span = value / total * fullPie; |
|
37 | 38 | PieSlice *slice = new PieSlice(QColor(), angle, span, parentItem->boundingRect()); |
|
38 | 39 | slice->setParentItem(parentItem); |
|
39 | 40 | m_slices.append(slice); |
|
40 | 41 | angle += span; |
|
41 | 42 | } |
|
42 | 43 | |
|
43 | 44 | resizeSlices(m_chartSize); |
|
44 | 45 | return true; |
|
45 | 46 | } |
|
46 | 47 | |
|
47 | 48 | void QPieSeries::setSliceColor(int index, QColor color) |
|
48 | 49 | { |
|
49 | 50 | if (index >= 0 && index < m_slices.count()) |
|
50 | 51 | m_slices.at(index)->m_color = color; |
|
51 | 52 | } |
|
52 | 53 | |
|
53 | 54 | QColor QPieSeries::sliceColor(int index) |
|
54 | 55 | { |
|
55 | 56 | if (index >= 0 && index < m_slices.count()) |
|
56 | 57 | return m_slices.at(index)->m_color; |
|
57 | 58 | else |
|
58 | 59 | return QColor(); |
|
59 | 60 | } |
|
60 | 61 | |
|
61 | 62 | int QPieSeries::sliceCount() |
|
62 | 63 | { |
|
63 | 64 | return m_slices.count(); |
|
64 | 65 | } |
|
65 | 66 | |
|
66 | 67 | void QPieSeries::chartSizeChanged(QRectF chartRect) |
|
67 | 68 | { |
|
68 | 69 | // TODO: allow user setting the size? |
|
69 | 70 | // TODO: allow user defining the margins? |
|
70 | 71 | m_chartSize = chartRect; |
|
71 | 72 | resizeSlices(m_chartSize); |
|
72 | 73 | } |
|
73 | 74 | |
|
74 | 75 | void QPieSeries::resizeSlices(QRectF rect) |
|
75 | 76 | { |
|
76 | 77 | QRectF tempRect = rect; |
|
77 | 78 | if (tempRect.width() < tempRect.height()) { |
|
78 | 79 | tempRect.setWidth(tempRect.width() * m_sizeFactor); |
|
79 | 80 | tempRect.setHeight(tempRect.width()); |
|
80 | 81 | tempRect.moveCenter(rect.center()); |
|
81 | 82 | } else { |
|
82 | 83 | tempRect.setHeight(tempRect.height() * m_sizeFactor); |
|
83 | 84 | tempRect.setWidth(tempRect.height()); |
|
84 | 85 | tempRect.moveCenter(rect.center()); |
|
85 | 86 | } |
|
86 | 87 | |
|
88 | switch (m_position) { | |
|
89 | case PiePositionTopLeft: { | |
|
90 | tempRect.setHeight(tempRect.height() / 2); | |
|
91 | tempRect.setWidth(tempRect.height()); | |
|
92 | tempRect.moveCenter(QPointF(rect.center().x() / 2, rect.center().y() / 2)); | |
|
93 | break; | |
|
94 | } | |
|
95 | case PiePositionTopRight: { | |
|
96 | tempRect.setHeight(tempRect.height() / 2); | |
|
97 | tempRect.setWidth(tempRect.height()); | |
|
98 | tempRect.moveCenter(QPointF((rect.center().x() / 2) * 3, rect.center().y() / 2)); | |
|
99 | break; | |
|
100 | } | |
|
101 | case PiePositionBottomLeft: { | |
|
102 | tempRect.setHeight(tempRect.height() / 2); | |
|
103 | tempRect.setWidth(tempRect.height()); | |
|
104 | tempRect.moveCenter(QPointF(rect.center().x() / 2, (rect.center().y() / 2) * 3)); | |
|
105 | break; | |
|
106 | } | |
|
107 | case PiePositionBottomRight: { | |
|
108 | tempRect.setHeight(tempRect.height() / 2); | |
|
109 | tempRect.setWidth(tempRect.height()); | |
|
110 | tempRect.moveCenter(QPointF((rect.center().x() / 2) * 3, (rect.center().y() / 2) * 3)); | |
|
111 | break; | |
|
112 | } | |
|
113 | default: | |
|
114 | break; | |
|
115 | } | |
|
116 | ||
|
87 | 117 | foreach (PieSlice *slice, m_slices) |
|
88 | 118 | slice->m_rect = tempRect; |
|
89 | 119 | } |
|
90 | 120 | |
|
91 | 121 | void QPieSeries::setSizeFactor(qreal factor) |
|
92 | 122 | { |
|
93 | 123 | if (factor > 0.0) |
|
94 | 124 | m_sizeFactor = factor; |
|
95 | 125 | resizeSlices(m_chartSize); |
|
96 | 126 | |
|
97 | 127 | // Initiate update via the parent graphics item |
|
98 | 128 | // TODO: potential issue: what if this function is called from the parent context? |
|
99 | 129 | QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent()); |
|
100 | 130 | Q_ASSERT(parentItem); |
|
101 | 131 | parentItem->update(); |
|
102 | 132 | } |
|
103 | 133 | |
|
134 | void QPieSeries::setPosition(PiePosition position) | |
|
135 | { | |
|
136 | m_position = position; | |
|
137 | resizeSlices(m_chartSize); | |
|
138 | ||
|
139 | // Initiate update via the parent graphics item | |
|
140 | // TODO: potential issue: what if this function is called from the parent context? | |
|
141 | QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent()); | |
|
142 | Q_ASSERT(parentItem); | |
|
143 | parentItem->update(); | |
|
144 | } | |
|
145 | ||
|
104 | 146 | #include "moc_qpieseries.cpp" |
|
105 | 147 | |
|
106 | 148 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,50 +1,61 | |||
|
1 | 1 | #ifndef PIESERIES_H |
|
2 | 2 | #define PIESERIES_H |
|
3 | 3 | |
|
4 | 4 | #include "qchartseries.h" |
|
5 | 5 | #include <QObject> |
|
6 | 6 | #include <QRectF> |
|
7 | 7 | #include <QColor> |
|
8 | 8 | |
|
9 | 9 | class QGraphicsObject; |
|
10 | 10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
11 | 11 | class PieSlice; |
|
12 | 12 | |
|
13 | 13 | class QTCOMMERCIALCHART_EXPORT QPieSeries : public QChartSeries |
|
14 | 14 | { |
|
15 | 15 | Q_OBJECT |
|
16 | 16 | |
|
17 | 17 | public: |
|
18 | enum PiePosition { | |
|
19 | PiePositionMaximized = 0, | |
|
20 | PiePositionTopLeft, | |
|
21 | PiePositionTopRight, | |
|
22 | PiePositionBottomLeft, | |
|
23 | PiePositionBottomRight | |
|
24 | }; | |
|
25 | ||
|
26 | public: | |
|
18 | 27 | // TODO: use a generic data class instead of x and y |
|
19 | 28 | QPieSeries(QGraphicsObject *parent = 0); |
|
20 | 29 | ~QPieSeries(); |
|
21 | void setSizeFactor(qreal sizeFactor); | |
|
22 | qreal sizeFactor() { return m_sizeFactor; } | |
|
23 | 30 | |
|
24 | 31 | public: // from QChartSeries |
|
25 | 32 | QChartSeriesType type() const { return QChartSeries::SeriesTypePie; } |
|
26 | 33 | bool setData(QList<qreal> data); |
|
27 | 34 | |
|
28 | 35 | public: |
|
29 | 36 | void setSliceColor(int index, QColor color); |
|
30 | 37 | QColor sliceColor(int index); |
|
31 | 38 | int sliceCount(); |
|
39 | void setSizeFactor(qreal sizeFactor); | |
|
40 | qreal sizeFactor() { return m_sizeFactor; } | |
|
41 | void setPosition(PiePosition position); | |
|
32 | 42 | |
|
33 | 43 | public Q_SLOTS: |
|
34 | 44 | void chartSizeChanged(QRectF rect); |
|
35 | 45 | |
|
36 | 46 | private: |
|
37 | 47 | void resizeSlices(QRectF rect); |
|
38 | 48 | //Q_DECLARE_PRIVATE(QPieSeries) |
|
39 | 49 | Q_DISABLE_COPY(QPieSeries) |
|
40 | 50 | friend class QChart; |
|
41 | 51 | // TODO: move the followin to internal impl |
|
42 | 52 | QList<qreal> m_data; |
|
43 | 53 | QList<PieSlice*> m_slices; |
|
44 | 54 | QRectF m_chartSize; |
|
45 | 55 | qreal m_sizeFactor; |
|
56 | PiePosition m_position; | |
|
46 | 57 | }; |
|
47 | 58 | |
|
48 | 59 | QTCOMMERCIALCHART_END_NAMESPACE |
|
49 | 60 | |
|
50 | 61 | #endif // PIESERIES_H |
@@ -1,314 +1,338 | |||
|
1 | 1 | #include "mainwidget.h" |
|
2 | 2 | #include "dataseriedialog.h" |
|
3 | 3 | #include "qchartseries.h" |
|
4 | 4 | #include "qpieseries.h" |
|
5 | 5 | #include <qxychartseries.h> |
|
6 | 6 | #include <barchartseries.h> |
|
7 | 7 | #include <QPushButton> |
|
8 | 8 | #include <QComboBox> |
|
9 | 9 | #include <QSpinBox> |
|
10 | 10 | #include <QCheckBox> |
|
11 | 11 | #include <QGridLayout> |
|
12 | 12 | #include <QHBoxLayout> |
|
13 | 13 | #include <QLabel> |
|
14 | 14 | #include <QSpacerItem> |
|
15 | 15 | #include <QMessageBox> |
|
16 | 16 | #include <cmath> |
|
17 | 17 | #include <QDebug> |
|
18 | 18 | |
|
19 | 19 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
20 | 20 | |
|
21 | 21 | MainWidget::MainWidget(QWidget *parent) : |
|
22 | 22 | QWidget(parent) |
|
23 | 23 | { |
|
24 | m_chartWidget = new QChartWidget(this); | |
|
25 | ||
|
24 | 26 | QPushButton *addSeriesButton = new QPushButton("Add series"); |
|
25 | 27 | connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries())); |
|
26 | 28 | |
|
27 | 29 | // Chart background |
|
28 | 30 | QComboBox *backgroundCombo = new QComboBox(this); |
|
29 |
backgroundCombo->addItem(" |
|
|
30 |
backgroundCombo->addItem(" |
|
|
31 |
backgroundCombo->addItem(" |
|
|
31 | backgroundCombo->addItem("Color"); | |
|
32 | backgroundCombo->addItem("Gradient"); | |
|
33 | backgroundCombo->addItem("Image"); | |
|
32 | 34 | connect(backgroundCombo, SIGNAL(currentIndexChanged(int)), |
|
33 | 35 | this, SLOT(backgroundChanged(int))); |
|
34 | 36 | |
|
35 | 37 | // Axis |
|
36 | 38 | // TODO: multiple axes? |
|
37 | 39 | m_autoScaleCheck = new QCheckBox("Automatic scaling"); |
|
38 | 40 | connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int))); |
|
39 | 41 | // Allow setting also non-sense values (like -2147483648 and 2147483647) |
|
40 | 42 | m_xMinSpin = new QSpinBox(); |
|
41 | 43 | m_xMinSpin->setMinimum(INT_MIN); |
|
42 | 44 | m_xMinSpin->setMaximum(INT_MAX); |
|
43 | 45 | m_xMinSpin->setValue(0); |
|
44 | 46 | connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int))); |
|
45 | 47 | m_xMaxSpin = new QSpinBox(); |
|
46 | 48 | m_xMaxSpin->setMinimum(INT_MIN); |
|
47 | 49 | m_xMaxSpin->setMaximum(INT_MAX); |
|
48 | 50 | m_xMaxSpin->setValue(10); |
|
49 | 51 | connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int))); |
|
50 | 52 | m_yMinSpin = new QSpinBox(); |
|
51 | 53 | m_yMinSpin->setMinimum(INT_MIN); |
|
52 | 54 | m_yMinSpin->setMaximum(INT_MAX); |
|
53 | 55 | m_yMinSpin->setValue(0); |
|
54 | 56 | connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int))); |
|
55 | 57 | m_yMaxSpin = new QSpinBox(); |
|
56 | 58 | m_yMaxSpin->setMinimum(INT_MIN); |
|
57 | 59 | m_yMaxSpin->setMaximum(INT_MAX); |
|
58 | 60 | m_yMaxSpin->setValue(10); |
|
59 | 61 | connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int))); |
|
60 | 62 | |
|
61 | 63 | QComboBox *chartTheme = new QComboBox(); |
|
62 | 64 | chartTheme->addItem("Vanilla"); |
|
63 | 65 | chartTheme->addItem("Icy"); |
|
64 | 66 | chartTheme->addItem("Grayscale"); |
|
65 |
chartTheme->addItem(" |
|
|
67 | chartTheme->addItem("Unnamed1"); | |
|
66 | 68 | connect(chartTheme, SIGNAL(currentIndexChanged(int)), |
|
67 | 69 | this, SLOT(changeChartTheme(int))); |
|
68 | 70 | |
|
71 | QCheckBox *zoomCheckBox = new QCheckBox("Zoom enabled"); | |
|
72 | connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartWidget, SLOT(setZoomEnabled(bool))); | |
|
73 | zoomCheckBox->setChecked(true); | |
|
74 | ||
|
69 | 75 | QGridLayout *grid = new QGridLayout(); |
|
70 | 76 | QGridLayout *mainLayout = new QGridLayout(); |
|
71 | //grid->addWidget(new QLabel("Add series:"), 0, 0); | |
|
72 | 77 | grid->addWidget(addSeriesButton, 0, 1); |
|
73 | 78 | grid->addWidget(new QLabel("Background:"), 2, 0); |
|
74 | 79 | grid->addWidget(backgroundCombo, 2, 1); |
|
75 | 80 | grid->addWidget(m_autoScaleCheck, 3, 0); |
|
76 | 81 | grid->addWidget(new QLabel("x min:"), 4, 0); |
|
77 | 82 | grid->addWidget(m_xMinSpin, 4, 1); |
|
78 | 83 | grid->addWidget(new QLabel("x max:"), 5, 0); |
|
79 | 84 | grid->addWidget(m_xMaxSpin, 5, 1); |
|
80 | 85 | grid->addWidget(new QLabel("y min:"), 6, 0); |
|
81 | 86 | grid->addWidget(m_yMinSpin, 6, 1); |
|
82 | 87 | grid->addWidget(new QLabel("y max:"), 7, 0); |
|
83 | 88 | grid->addWidget(m_yMaxSpin, 7, 1); |
|
84 | 89 | grid->addWidget(new QLabel("Chart theme:"), 8, 0); |
|
85 | 90 | grid->addWidget(chartTheme, 8, 1); |
|
91 | grid->addWidget(zoomCheckBox, 9, 0); | |
|
86 | 92 | // add row with empty label to make all the other rows static |
|
87 |
grid->addWidget(new QLabel(""), |
|
|
88 |
grid->setRowStretch( |
|
|
93 | grid->addWidget(new QLabel(""), 10, 0); | |
|
94 | grid->setRowStretch(10, 1); | |
|
89 | 95 | |
|
90 | 96 | mainLayout->addLayout(grid, 0, 0); |
|
91 | 97 | |
|
92 | 98 | // Scatter specific settings |
|
93 | 99 | m_scatterLayout = new QGridLayout(); |
|
94 | 100 | m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0); |
|
95 | 101 | m_scatterLayout->setEnabled(false); |
|
96 | 102 | |
|
97 | 103 | // Pie specific settings |
|
98 | m_pieLayout = new QGridLayout(); | |
|
99 | m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0); | |
|
104 | // Pie size factory | |
|
100 | 105 | QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox(); |
|
101 | 106 | pieSizeSpin->setMinimum(LONG_MIN); |
|
102 | 107 | pieSizeSpin->setMaximum(LONG_MAX); |
|
103 | 108 | pieSizeSpin->setValue(1.0); |
|
104 | 109 | pieSizeSpin->setSingleStep(0.1); |
|
105 | 110 | connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double))); |
|
111 | // Pie position | |
|
112 | QComboBox *piePosCombo = new QComboBox(this); | |
|
113 | piePosCombo->addItem("Maximized"); | |
|
114 | piePosCombo->addItem("Top left"); | |
|
115 | piePosCombo->addItem("Top right"); | |
|
116 | piePosCombo->addItem("Bottom left"); | |
|
117 | piePosCombo->addItem("Bottom right"); | |
|
118 | connect(piePosCombo, SIGNAL(currentIndexChanged(int)), | |
|
119 | this, SLOT(setPiePosition(int))); | |
|
120 | m_pieLayout = new QGridLayout(); | |
|
106 | 121 | m_pieLayout->setEnabled(false); |
|
122 | m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0); | |
|
107 | 123 | m_pieLayout->addWidget(pieSizeSpin, 0, 1); |
|
124 | m_pieLayout->addWidget(new QLabel("Pie position"), 1, 0); | |
|
125 | m_pieLayout->addWidget(piePosCombo, 1, 1); | |
|
108 | 126 | |
|
109 | 127 | mainLayout->addLayout(m_scatterLayout, 1, 0); |
|
110 | 128 | mainLayout->addLayout(m_pieLayout, 2, 0); |
|
111 | 129 | |
|
112 | m_chartWidget = new QChartWidget(this); | |
|
113 | 130 | //m_chartWidget->setColor(Qt::red); |
|
114 | 131 | mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1); |
|
115 | 132 | // hbox->setStretch(1, 1); |
|
116 | 133 | |
|
117 | 134 | setLayout(mainLayout); |
|
118 | 135 | |
|
119 | 136 | m_autoScaleCheck->setChecked(true); |
|
120 | 137 | testDataChanged(0); |
|
121 | 138 | } |
|
122 | 139 | |
|
123 | 140 | void MainWidget::addSeries() |
|
124 | 141 | { |
|
125 | 142 | DataSerieDialog dialog(m_defaultSeriesName, this); |
|
126 | 143 | connect(&dialog, SIGNAL(accepted(QString, QString)), this, SLOT(addSeries(QString, QString))); |
|
127 | 144 | dialog.exec(); |
|
128 | 145 | } |
|
129 | 146 | |
|
130 | 147 | void MainWidget::addSeries(QString series, QString data) |
|
131 | 148 | { |
|
132 | 149 | qDebug() << "addSeries: " << series << " data: " << data; |
|
133 | 150 | m_defaultSeriesName = series; |
|
134 | 151 | |
|
135 | 152 | // TODO: a dedicated data class for storing x and y values |
|
136 | 153 | QList<qreal> x; |
|
137 | 154 | QList<qreal> y; |
|
138 | 155 | |
|
139 | 156 | if (data == "linear") { |
|
140 | 157 | for (int i = 0; i < 20; i++) { |
|
141 | 158 | x.append(i); |
|
142 | 159 | y.append(i); |
|
143 | 160 | } |
|
144 | 161 | } else if (data == "linear, 1M") { |
|
145 | 162 | for (int i = 0; i < 10000; i++) { |
|
146 | 163 | x.append(i); |
|
147 | 164 | y.append(20); |
|
148 | 165 | } |
|
149 | 166 | } else if (data == "SIN") { |
|
150 | 167 | for (int i = 0; i < 100; i++) { |
|
151 | 168 | x.append(i); |
|
152 | 169 | y.append(abs(sin(3.14159265358979 / 50 * i) * 100)); |
|
153 | 170 | } |
|
154 | 171 | } else if (data == "SIN + random") { |
|
155 | 172 | for (qreal i = 0; i < 100; i += 0.1) { |
|
156 | 173 | x.append(i + (rand() % 5)); |
|
157 | 174 | y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5)); |
|
158 | 175 | } |
|
159 | 176 | } else { |
|
160 | 177 | // TODO: check if data has a valid file name |
|
161 | 178 | Q_ASSERT(false); |
|
162 | 179 | } |
|
163 | 180 | |
|
164 | 181 | // TODO: color of the series |
|
165 | 182 | QChartSeries *newSeries = 0; |
|
166 | 183 | if (series == "Scatter") { |
|
167 | 184 | newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeScatter); |
|
168 | 185 | Q_ASSERT(newSeries->setData(x, y)); |
|
169 | 186 | } else if (series == "Pie") { |
|
170 | 187 | newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie); |
|
171 | 188 | Q_ASSERT(newSeries->setData(y)); |
|
172 | 189 | } else if (series == "Line") { |
|
173 | 190 | // TODO: adding data to an existing line series does not give any visuals for some reason |
|
174 | 191 | // newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeLine); |
|
175 | 192 | // QXYChartSeries *lineSeries = static_cast<QXYChartSeries *>(newSeries); |
|
176 | 193 | // lineSeries->setColor(Qt::blue); |
|
177 | 194 | // for (int i(0); i < x.count() && i < y.count(); i++) { |
|
178 | 195 | // lineSeries->add(x.at(i), y.at(i)); |
|
179 | 196 | // } |
|
180 | 197 | //Q_ASSERT(newSeries->setData(x, y)); |
|
181 | 198 | QXYChartSeries* series0 = QXYChartSeries::create(); |
|
182 | 199 | for (int i(0); i < x.count() && i < y.count(); i++) |
|
183 | 200 | series0->add(x.at(i), y.at(i)); |
|
184 | 201 | m_chartWidget->addSeries(series0); |
|
185 | 202 | newSeries = series0; |
|
186 | 203 | } else { |
|
187 | 204 | // TODO |
|
188 | 205 | } |
|
189 | 206 | |
|
190 | 207 | // BarChart |
|
191 | 208 | if (series == "Bar") { |
|
192 | 209 | // This is the another way of creating series. Should we create test cases for both ways, if we support them? |
|
193 | 210 | qDebug() << "Bar chart series"; |
|
194 | 211 | newSeries = QChartSeries::create(QChartSeries::SeriesTypeBar, this); |
|
195 | 212 | QList<int> barData; |
|
196 | 213 | barData << 1; |
|
197 | 214 | barData << 12; |
|
198 | 215 | barData << 5; |
|
199 | 216 | barData << 8; |
|
200 | 217 | barData << 17; |
|
201 | 218 | barData << 9; |
|
202 | 219 | newSeries->setData(barData); |
|
203 | 220 | m_chartWidget->addSeries(newSeries); |
|
204 | 221 | } |
|
205 | 222 | |
|
206 | 223 | setCurrentSeries(newSeries); |
|
207 | 224 | } |
|
208 | 225 | |
|
209 | 226 | void MainWidget::setCurrentSeries(QChartSeries *series) |
|
210 | 227 | { |
|
211 | 228 | m_currentSeries = series; |
|
212 | 229 | switch (m_currentSeries->type()) { |
|
213 | 230 | case QChartSeries::SeriesTypeLine: |
|
214 | 231 | break; |
|
215 | 232 | case QChartSeries::SeriesTypeScatter: |
|
216 | 233 | break; |
|
217 | 234 | case QChartSeries::SeriesTypePie: |
|
218 | 235 | break; |
|
219 | 236 | case QChartSeries::SeriesTypeBar: |
|
220 | 237 | qDebug() << "setCurrentSeries (bar)"; |
|
221 | 238 | break; |
|
222 | 239 | default: |
|
223 | 240 | Q_ASSERT(false); |
|
224 | 241 | break; |
|
225 | 242 | } |
|
226 | 243 | } |
|
227 | 244 | |
|
228 | 245 | void MainWidget::testDataChanged(int itemIndex) |
|
229 | 246 | { |
|
230 | 247 | qDebug() << "testDataChanged: " << itemIndex; |
|
231 | 248 | |
|
232 | 249 | // switch (itemIndex) { |
|
233 | 250 | // case 0: { |
|
234 | 251 | // QList<QChartDataPoint> data; |
|
235 | 252 | // for (int x = 0; x < 20; x++) { |
|
236 | 253 | // data.append(QChartDataPoint() << x << x / 2); |
|
237 | 254 | // } |
|
238 | 255 | // m_chartWidget->setData(data); |
|
239 | 256 | // break; |
|
240 | 257 | // } |
|
241 | 258 | // case 1: { |
|
242 | 259 | // QList<QChartDataPoint> data; |
|
243 | 260 | // for (int x = 0; x < 100; x++) { |
|
244 | 261 | // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100)); |
|
245 | 262 | // } |
|
246 | 263 | // m_chartWidget->setData(data); |
|
247 | 264 | // break; |
|
248 | 265 | // } |
|
249 | 266 | // case 2: { |
|
250 | 267 | // QList<QChartDataPoint> data; |
|
251 | 268 | // for (int x = 0; x < 1000; x++) { |
|
252 | 269 | // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2)); |
|
253 | 270 | // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2)); |
|
254 | 271 | // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2)); |
|
255 | 272 | // } |
|
256 | 273 | // m_chartWidget->setData(data); |
|
257 | 274 | // break; |
|
258 | 275 | // } |
|
259 | 276 | // default: |
|
260 | 277 | // break; |
|
261 | 278 | // } |
|
262 | 279 | } |
|
263 | 280 | |
|
264 | 281 | void MainWidget::backgroundChanged(int itemIndex) |
|
265 | 282 | { |
|
266 | 283 | qDebug() << "backgroundChanged: " << itemIndex; |
|
267 | 284 | } |
|
268 | 285 | |
|
269 | 286 | void MainWidget::autoScaleChanged(int value) |
|
270 | 287 | { |
|
271 | 288 | if (value) { |
|
272 | 289 | // TODO: enable auto scaling |
|
273 | 290 | } else { |
|
274 | 291 | // TODO: set scaling manually (and disable auto scaling) |
|
275 | 292 | } |
|
276 | 293 | |
|
277 | 294 | m_xMinSpin->setEnabled(!value); |
|
278 | 295 | m_xMaxSpin->setEnabled(!value); |
|
279 | 296 | m_yMinSpin->setEnabled(!value); |
|
280 | 297 | m_yMaxSpin->setEnabled(!value); |
|
281 | 298 | } |
|
282 | 299 | |
|
283 | 300 | void MainWidget::xMinChanged(int value) |
|
284 | 301 | { |
|
285 | 302 | qDebug() << "xMinChanged: " << value; |
|
286 | 303 | } |
|
287 | 304 | |
|
288 | 305 | void MainWidget::xMaxChanged(int value) |
|
289 | 306 | { |
|
290 | 307 | qDebug() << "xMaxChanged: " << value; |
|
291 | 308 | } |
|
292 | 309 | |
|
293 | 310 | void MainWidget::yMinChanged(int value) |
|
294 | 311 | { |
|
295 | 312 | qDebug() << "yMinChanged: " << value; |
|
296 | 313 | } |
|
297 | 314 | |
|
298 | 315 | void MainWidget::yMaxChanged(int value) |
|
299 | 316 | { |
|
300 | 317 | qDebug() << "yMaxChanged: " << value; |
|
301 | 318 | } |
|
302 | 319 | |
|
303 | 320 | void MainWidget::changeChartTheme(int themeIndex) |
|
304 | 321 | { |
|
305 | 322 | qDebug() << "changeChartTheme: " << themeIndex; |
|
306 | 323 | m_chartWidget->setTheme((QChart::ChartThemeId) themeIndex); |
|
307 | 324 | } |
|
308 | 325 | |
|
309 | 326 | void MainWidget::setPieSizeFactor(double size) |
|
310 | 327 | { |
|
311 | 328 | QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries); |
|
312 | 329 | if (pie) |
|
313 | 330 | pie->setSizeFactor(qreal(size)); |
|
314 | 331 | } |
|
332 | ||
|
333 | void MainWidget::setPiePosition(int position) | |
|
334 | { | |
|
335 | QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries); | |
|
336 | if (pie) | |
|
337 | pie->setPosition((QPieSeries::PiePosition) position); | |
|
338 | } |
@@ -1,49 +1,50 | |||
|
1 | 1 | #ifndef MAINWIDGET_H |
|
2 | 2 | #define MAINWIDGET_H |
|
3 | 3 | |
|
4 | 4 | #include <qchartglobal.h> |
|
5 | 5 | #include <qchartwidget.h> |
|
6 | 6 | #include <QWidget> |
|
7 | 7 | |
|
8 | 8 | class QSpinBox; |
|
9 | 9 | class QCheckBox; |
|
10 | 10 | class QGridLayout; |
|
11 | 11 | |
|
12 | 12 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
13 | 13 | |
|
14 | 14 | class MainWidget : public QWidget |
|
15 | 15 | { |
|
16 | 16 | Q_OBJECT |
|
17 | 17 | public: |
|
18 | 18 | explicit MainWidget(QWidget *parent = 0); |
|
19 | 19 | |
|
20 | 20 | signals: |
|
21 | 21 | |
|
22 | 22 | private slots: |
|
23 | 23 | void addSeries(); |
|
24 | 24 | void addSeries(QString series, QString data); |
|
25 | 25 | void testDataChanged(int itemIndex); |
|
26 | 26 | void backgroundChanged(int itemIndex); |
|
27 | 27 | void autoScaleChanged(int value); |
|
28 | 28 | void xMinChanged(int value); |
|
29 | 29 | void xMaxChanged(int value); |
|
30 | 30 | void yMinChanged(int value); |
|
31 | 31 | void yMaxChanged(int value); |
|
32 | 32 | void setCurrentSeries(QChartSeries *series); |
|
33 | 33 | void changeChartTheme(int themeIndex); |
|
34 | 34 | void setPieSizeFactor(double margin); |
|
35 | void setPiePosition(int position); | |
|
35 | 36 | |
|
36 | 37 | private: |
|
37 | 38 | QChartWidget *m_chartWidget; |
|
38 | 39 | QCheckBox *m_autoScaleCheck; |
|
39 | 40 | QSpinBox *m_xMinSpin; |
|
40 | 41 | QSpinBox *m_xMaxSpin; |
|
41 | 42 | QSpinBox *m_yMinSpin; |
|
42 | 43 | QSpinBox *m_yMaxSpin; |
|
43 | 44 | QString m_defaultSeriesName; |
|
44 | 45 | QChartSeries *m_currentSeries; |
|
45 | 46 | QGridLayout *m_scatterLayout; |
|
46 | 47 | QGridLayout *m_pieLayout; |
|
47 | 48 | }; |
|
48 | 49 | |
|
49 | 50 | #endif // MAINWIDGET_H |
General Comments 0
You need to be logged in to leave comments.
Login now