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