##// END OF EJS Templates
QChartWidget now zooms only x axis and zoom is reset with right click
Tero Ahola -
r93:264fcc5c2ce8
parent child
Show More
@@ -1,394 +1,404
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 #include "qchartaxis.h"
7 #include "qchartaxis.h"
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_backgroundItem(0),
20 m_backgroundItem(0),
21 m_titleItem(0),
21 m_titleItem(0),
22 m_axisXItem(new AxisItem(AxisItem::X_AXIS,this)),
22 m_axisXItem(new AxisItem(AxisItem::X_AXIS,this)),
23 m_plotDataIndex(0),
23 m_plotDataIndex(0),
24 m_marginSize(0)
24 m_marginSize(0)
25 {
25 {
26 // TODO: the default theme?
26 // TODO: the default theme?
27 setTheme(QChart::ChartThemeDefault);
27 setTheme(QChart::ChartThemeDefault);
28
28
29 PlotDomain domain;
29 PlotDomain domain;
30 m_plotDomainList<<domain;
30 m_plotDomainList<<domain;
31 m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this);
31 m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this);
32 m_chartItems<<m_axisXItem;
32 m_chartItems<<m_axisXItem;
33 m_chartItems<<m_axisYItem.at(0);
33 m_chartItems<<m_axisYItem.at(0);
34 }
34 }
35
35
36 QChart::~QChart(){}
36 QChart::~QChart(){}
37
37
38 QRectF QChart::boundingRect() const
38 QRectF QChart::boundingRect() const
39 {
39 {
40 return m_rect;
40 return m_rect;
41 }
41 }
42
42
43 void QChart::addSeries(QChartSeries* series)
43 void QChart::addSeries(QChartSeries* series)
44 {
44 {
45 // TODO: we should check the series not already added
45 // TODO: we should check the series not already added
46
46
47 m_chartSeries << series;
47 m_chartSeries << series;
48
48
49 switch(series->type())
49 switch(series->type())
50 {
50 {
51 case QChartSeries::SeriesTypeLine: {
51 case QChartSeries::SeriesTypeLine: {
52
52
53 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
53 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
54 // Use color defined by theme in case the series does not define a custom color
54 // Use color defined by theme in case the series does not define a custom color
55
55
56 if (!xyseries->pen().color().isValid() && m_themeColors.count()) //TODO: wtf
56 if (!xyseries->pen().color().isValid() && m_themeColors.count()) //TODO: wtf
57 xyseries->setPen(nextColor());
57 xyseries->setPen(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 BarGroup* barGroup = new BarGroup(*barSeries,this);
86 BarGroup* barGroup = new BarGroup(*barSeries,this);
87
87
88 // Add some fugly colors for 5 fist series...
88 // Add some fugly colors for 5 fist series...
89 barGroup->addColor(QColor(255,0,0,128));
89 barGroup->addColor(QColor(255,0,0,128));
90 barGroup->addColor(QColor(255,255,0,128));
90 barGroup->addColor(QColor(255,255,0,128));
91 barGroup->addColor(QColor(0,255,0,128));
91 barGroup->addColor(QColor(0,255,0,128));
92 barGroup->addColor(QColor(0,0,255,128));
92 barGroup->addColor(QColor(0,0,255,128));
93 barGroup->addColor(QColor(255,128,0,128));
93 barGroup->addColor(QColor(255,128,0,128));
94
94
95 m_chartItems<<barGroup;
95 m_chartItems<<barGroup;
96 childItems().append(barGroup);
96 childItems().append(barGroup);
97 break;
97 break;
98 }
98 }
99 case QChartSeries::SeriesTypeScatter: {
99 case QChartSeries::SeriesTypeScatter: {
100 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
100 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
101 scatterSeries->d->setParentItem(this);
101 scatterSeries->d->setParentItem(this);
102 // Set pre-defined colors in case the series has no colors defined
102 // Set pre-defined colors in case the series has no colors defined
103 if (!scatterSeries->markerColor().isValid())
103 if (!scatterSeries->markerColor().isValid())
104 scatterSeries->setMarkerColor(nextColor());
104 scatterSeries->setMarkerColor(nextColor());
105 connect(this, SIGNAL(sizeChanged(QRectF)),
105 connect(this, SIGNAL(sizeChanged(QRectF)),
106 scatterSeries, SLOT(chartSizeChanged(QRectF)));
106 scatterSeries, SLOT(chartSizeChanged(QRectF)));
107 // QColor nextColor = m_themeColors.takeFirst();
107 // QColor nextColor = m_themeColors.takeFirst();
108 // nextColor.setAlpha(150); // TODO: default opacity?
108 // nextColor.setAlpha(150); // TODO: default opacity?
109 // scatterSeries->setMarkerColor(nextColor);
109 // scatterSeries->setMarkerColor(nextColor);
110 break;
110 break;
111 }
111 }
112 case QChartSeries::SeriesTypePie: {
112 case QChartSeries::SeriesTypePie: {
113 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
113 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
114 for (int i(0); i < pieSeries->sliceCount(); i++) {
114 for (int i(0); i < pieSeries->sliceCount(); i++) {
115 if (!pieSeries->sliceColor(i).isValid())
115 if (!pieSeries->sliceColor(i).isValid())
116 pieSeries->setSliceColor(i, nextColor());
116 pieSeries->setSliceColor(i, nextColor());
117 }
117 }
118 connect(this, SIGNAL(sizeChanged(QRectF)),
118 connect(this, SIGNAL(sizeChanged(QRectF)),
119 pieSeries, SLOT(chartSizeChanged(QRectF)));
119 pieSeries, SLOT(chartSizeChanged(QRectF)));
120
120
121 // Set pre-defined colors in case the series has no colors defined
121 // Set pre-defined colors in case the series has no colors defined
122 // TODO: how to define the color for all the slices of a pie?
122 // TODO: how to define the color for all the slices of a pie?
123 // for (int (i); i < pieSeries.sliceCount(); i++)
123 // for (int (i); i < pieSeries.sliceCount(); i++)
124 break;
124 break;
125 }
125 }
126 }
126 }
127 }
127 }
128
128
129 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
129 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
130 {
130 {
131 // TODO: support also other types; not only scatter and pie
131 // TODO: support also other types; not only scatter and pie
132
132
133 QChartSeries *series(0);
133 QChartSeries *series(0);
134
134
135 switch (type) {
135 switch (type) {
136 case QChartSeries::SeriesTypeLine: {
136 case QChartSeries::SeriesTypeLine: {
137 series = QXYChartSeries::create();
137 series = QXYChartSeries::create();
138 break;
138 break;
139 }
139 }
140 case QChartSeries::SeriesTypeBar: {
140 case QChartSeries::SeriesTypeBar: {
141 series = new BarChartSeries(this);
141 series = new BarChartSeries(this);
142 break;
142 break;
143 }
143 }
144 case QChartSeries::SeriesTypeScatter: {
144 case QChartSeries::SeriesTypeScatter: {
145 series = new QScatterSeries(this);
145 series = new QScatterSeries(this);
146 break;
146 break;
147 }
147 }
148 case QChartSeries::SeriesTypePie: {
148 case QChartSeries::SeriesTypePie: {
149 series = new QPieSeries(this);
149 series = new QPieSeries(this);
150 break;
150 break;
151 }
151 }
152 default:
152 default:
153 Q_ASSERT(false);
153 Q_ASSERT(false);
154 break;
154 break;
155 }
155 }
156
156
157 addSeries(series);
157 addSeries(series);
158 return series;
158 return series;
159 }
159 }
160
160
161 void QChart::setSize(const QSize& size)
161 void QChart::setSize(const QSize& size)
162 {
162 {
163 m_rect = QRect(QPoint(0,0),size);
163 m_rect = QRect(QPoint(0,0),size);
164 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
164 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
165
165
166 //recaculate title
166 //recaculate title
167 if(m_titleItem){
167 if(m_titleItem){
168 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
168 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
169 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
169 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
170
170
171 }
171 }
172
172
173 //recalculate background gradient
173 //recalculate background gradient
174 if(m_backgroundItem){
174 if(m_backgroundItem){
175 m_backgroundItem->setRect(rect);
175 m_backgroundItem->setRect(rect);
176 if(m_bacgroundOrinetation==HorizonatlGradientOrientation)
176 if(m_bacgroundOrinetation==HorizonatlGradientOrientation)
177 m_backgroundGradient.setFinalStop(m_backgroundItem->rect().width(),0);
177 m_backgroundGradient.setFinalStop(m_backgroundItem->rect().width(),0);
178 else
178 else
179 m_backgroundGradient.setFinalStop(0,m_backgroundItem->rect().height());
179 m_backgroundGradient.setFinalStop(0,m_backgroundItem->rect().height());
180
180
181 m_backgroundItem->setBrush(m_backgroundGradient);
181 m_backgroundItem->setBrush(m_backgroundGradient);
182 }
182 }
183
183
184 //resize elements
184 //resize elements
185 foreach (ChartItem* item ,m_chartItems) {
185 foreach (ChartItem* item ,m_chartItems) {
186 item->setPos(rect.topLeft());
186 item->setPos(rect.topLeft());
187 item->setSize(rect.size());
187 item->setSize(rect.size());
188
188
189 }
189 }
190 // TODO: TTD for setting scale
190 // TODO: TTD for setting scale
191 //emit scaleChanged(100, 100);
191 //emit scaleChanged(100, 100);
192 // TODO: calculate the origo
192 // TODO: calculate the origo
193 // TODO: not sure if emitting a signal here is the best from performance point of view
193 // TODO: not sure if emitting a signal here is the best from performance point of view
194 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
194 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
195
195
196 update();
196 update();
197 }
197 }
198
198
199 void QChart::setBackground(const QColor& startColor, const QColor& endColor, GradientOrientation orientation)
199 void QChart::setBackground(const QColor& startColor, const QColor& endColor, GradientOrientation orientation)
200 {
200 {
201
201
202 if(!m_backgroundItem){
202 if(!m_backgroundItem){
203 m_backgroundItem = new QGraphicsRectItem(this);
203 m_backgroundItem = new QGraphicsRectItem(this);
204 m_backgroundItem->setZValue(-1);
204 m_backgroundItem->setZValue(-1);
205 }
205 }
206
206
207 m_bacgroundOrinetation = orientation;
207 m_bacgroundOrinetation = orientation;
208 m_backgroundGradient.setColorAt( 0.0, startColor);
208 m_backgroundGradient.setColorAt( 0.0, startColor);
209 m_backgroundGradient.setColorAt( 1.0, endColor);
209 m_backgroundGradient.setColorAt( 1.0, endColor);
210 m_backgroundGradient.setStart(0,0);
210 m_backgroundGradient.setStart(0,0);
211
211
212 if(orientation == VerticalGradientOrientation){
212 if(orientation == VerticalGradientOrientation){
213 m_backgroundGradient.setFinalStop(0,m_rect.height());
213 m_backgroundGradient.setFinalStop(0,m_rect.height());
214 }else{
214 }else{
215 m_backgroundGradient.setFinalStop(m_rect.width(),0);
215 m_backgroundGradient.setFinalStop(m_rect.width(),0);
216 }
216 }
217
217
218 m_backgroundItem->setBrush(m_backgroundGradient);
218 m_backgroundItem->setBrush(m_backgroundGradient);
219 m_backgroundItem->setPen(Qt::NoPen);
219 m_backgroundItem->setPen(Qt::NoPen);
220 m_backgroundItem->update();
220 m_backgroundItem->update();
221 }
221 }
222
222
223 void QChart::setTitle(const QString& title,const QFont& font)
223 void QChart::setTitle(const QString& title,const QFont& font)
224 {
224 {
225 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
225 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
226 m_titleItem->setPlainText(title);
226 m_titleItem->setPlainText(title);
227 m_titleItem->setFont(font);
227 m_titleItem->setFont(font);
228 }
228 }
229
229
230 int QChart::margin() const
230 int QChart::margin() const
231 {
231 {
232 return m_marginSize;
232 return m_marginSize;
233 }
233 }
234
234
235 void QChart::setMargin(int margin)
235 void QChart::setMargin(int margin)
236 {
236 {
237 m_marginSize = margin;
237 m_marginSize = margin;
238 }
238 }
239
239
240 void QChart::setTheme(QChart::ChartThemeId theme)
240 void QChart::setTheme(QChart::ChartThemeId theme)
241 {
241 {
242 // if (theme != m_currentTheme) {
242 // if (theme != m_currentTheme) {
243 m_themeColors.clear();
243 m_themeColors.clear();
244
244
245 // TODO: define color themes
245 // TODO: define color themes
246 switch (theme) {
246 switch (theme) {
247 case QChart::ChartThemeDefault:
247 case QChart::ChartThemeDefault:
248 // TODO: define the default theme based on the OS
248 // TODO: define the default theme based on the OS
249 m_themeColors.append(QColor(QRgb(0xff000000)));
249 m_themeColors.append(QColor(QRgb(0xff000000)));
250 m_themeColors.append(QColor(QRgb(0xff707070)));
250 m_themeColors.append(QColor(QRgb(0xff707070)));
251 setBackground(QColor(QRgb(0xffffffff)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
251 setBackground(QColor(QRgb(0xffffffff)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
252 break;
252 break;
253 case QChart::ChartThemeVanilla:
253 case QChart::ChartThemeVanilla:
254 m_themeColors.append(QColor(217, 197, 116));
254 m_themeColors.append(QColor(217, 197, 116));
255 m_themeColors.append(QColor(214, 168, 150));
255 m_themeColors.append(QColor(214, 168, 150));
256 m_themeColors.append(QColor(160, 160, 113));
256 m_themeColors.append(QColor(160, 160, 113));
257 m_themeColors.append(QColor(210, 210, 52));
257 m_themeColors.append(QColor(210, 210, 52));
258 m_themeColors.append(QColor(136, 114, 58));
258 m_themeColors.append(QColor(136, 114, 58));
259
259
260 setBackground(QColor(QRgb(0xff9d844d)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
260 setBackground(QColor(QRgb(0xff9d844d)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
261 break;
261 break;
262 case QChart::ChartThemeIcy:
262 case QChart::ChartThemeIcy:
263 m_themeColors.append(QColor(0, 3, 165));
263 m_themeColors.append(QColor(0, 3, 165));
264 m_themeColors.append(QColor(49, 52, 123));
264 m_themeColors.append(QColor(49, 52, 123));
265 m_themeColors.append(QColor(71, 114, 187));
265 m_themeColors.append(QColor(71, 114, 187));
266 m_themeColors.append(QColor(48, 97, 87));
266 m_themeColors.append(QColor(48, 97, 87));
267 m_themeColors.append(QColor(19, 71, 90));
267 m_themeColors.append(QColor(19, 71, 90));
268 m_themeColors.append(QColor(110, 70, 228));
268 m_themeColors.append(QColor(110, 70, 228));
269
269
270 setBackground(QColor(QRgb(0xffe4ffff)), QColor(QRgb(0xffe4ffff)), VerticalGradientOrientation);
270 setBackground(QColor(QRgb(0xffe4ffff)), QColor(QRgb(0xffe4ffff)), VerticalGradientOrientation);
271 break;
271 break;
272 case QChart::ChartThemeGrayscale:
272 case QChart::ChartThemeGrayscale:
273 m_themeColors.append(QColor(0, 0, 0));
273 m_themeColors.append(QColor(0, 0, 0));
274 m_themeColors.append(QColor(50, 50, 50));
274 m_themeColors.append(QColor(50, 50, 50));
275 m_themeColors.append(QColor(100, 100, 100));
275 m_themeColors.append(QColor(100, 100, 100));
276 m_themeColors.append(QColor(140, 140, 140));
276 m_themeColors.append(QColor(140, 140, 140));
277 m_themeColors.append(QColor(180, 180, 180));
277 m_themeColors.append(QColor(180, 180, 180));
278
278
279 setBackground(QColor(QRgb(0xffffffff)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
279 setBackground(QColor(QRgb(0xffffffff)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
280 break;
280 break;
281 case QChart::ChartThemeUnnamed1:
281 case QChart::ChartThemeUnnamed1:
282 m_themeColors.append(QColor(QRgb(0xff3fa9f5)));
282 m_themeColors.append(QColor(QRgb(0xff3fa9f5)));
283 m_themeColors.append(QColor(QRgb(0xff7AC943)));
283 m_themeColors.append(QColor(QRgb(0xff7AC943)));
284 m_themeColors.append(QColor(QRgb(0xffFF931E)));
284 m_themeColors.append(QColor(QRgb(0xffFF931E)));
285 m_themeColors.append(QColor(QRgb(0xffFF1D25)));
285 m_themeColors.append(QColor(QRgb(0xffFF1D25)));
286 m_themeColors.append(QColor(QRgb(0xffFF7BAC)));
286 m_themeColors.append(QColor(QRgb(0xffFF7BAC)));
287
287
288 setBackground(QColor(QRgb(0xfff3dc9e)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
288 setBackground(QColor(QRgb(0xfff3dc9e)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
289 break;
289 break;
290 default:
290 default:
291 Q_ASSERT(false);
291 Q_ASSERT(false);
292 break;
292 break;
293 }
293 }
294
294
295 if(m_backgroundItem){
295 if(m_backgroundItem){
296 m_backgroundItem->setBrush(m_backgroundGradient);
296 m_backgroundItem->setBrush(m_backgroundGradient);
297 m_backgroundItem->setPen(Qt::NoPen);
297 m_backgroundItem->setPen(Qt::NoPen);
298 }
298 }
299
299
300 foreach(QChartSeries* series, m_chartSeries) {
300 foreach(QChartSeries* series, m_chartSeries) {
301 // TODO: other series interested on themes?
301 // TODO: other series interested on themes?
302 if (series->type() == QChartSeries::SeriesTypeLine) {
302 if (series->type() == QChartSeries::SeriesTypeLine) {
303 QXYChartSeries *lineseries = reinterpret_cast<QXYChartSeries *>(series);
303 QXYChartSeries *lineseries = reinterpret_cast<QXYChartSeries *>(series);
304 lineseries->setPen(nextColor());
304 lineseries->setPen(nextColor());
305 } else if (series->type() == QChartSeries::SeriesTypeScatter) {
305 } else if (series->type() == QChartSeries::SeriesTypeScatter) {
306 QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series);
306 QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series);
307 scatter->setMarkerColor(nextColor());
307 scatter->setMarkerColor(nextColor());
308 } else if (series->type() == QChartSeries::SeriesTypePie) {
308 } else if (series->type() == QChartSeries::SeriesTypePie) {
309 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
309 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
310 for (int i(0); i < pieSeries->sliceCount(); i++)
310 for (int i(0); i < pieSeries->sliceCount(); i++)
311 pieSeries->setSliceColor(i, nextColor());
311 pieSeries->setSliceColor(i, nextColor());
312 }
312 }
313 }
313 }
314 update();
314 update();
315 }
315 }
316
316
317 QColor QChart::nextColor()
317 QColor QChart::nextColor()
318 {
318 {
319 QColor nextColor = m_themeColors.first();
319 QColor nextColor = m_themeColors.first();
320 m_themeColors.move(0, m_themeColors.size() - 1);
320 m_themeColors.move(0, m_themeColors.size() - 1);
321 return nextColor;
321 return nextColor;
322 }
322 }
323
323
324 void QChart::zoomInToRect(const QRect& rectangle)
324 void QChart::zoomInToRect(const QRect& rectangle)
325 {
325 {
326
326
327 if(!rectangle.isValid()) return;
327 if(!rectangle.isValid()) return;
328
328
329 qreal margin = this->margin();
329 qreal margin = this->margin();
330
330
331 QRect rect = rectangle.normalized();
331 QRect rect = rectangle.normalized();
332 rect.translate(-margin, -margin);
332 rect.translate(-margin, -margin);
333
333
334 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
334 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
335
335
336 PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin);
336 PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin);
337
337
338 m_plotDomainList.resize(m_plotDataIndex + 1);
338 m_plotDomainList.resize(m_plotDataIndex + 1);
339 m_plotDomainList<<domain;
339 m_plotDomainList<<domain;
340 m_plotDataIndex++;
340 m_plotDataIndex++;
341
341
342 foreach (ChartItem* item ,m_chartItems)
342 foreach (ChartItem* item ,m_chartItems)
343 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
343 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
344 update();
344 update();
345 }
345 }
346
346
347 void QChart::zoomIn()
347 void QChart::zoomIn()
348 {
348 {
349 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
349 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
350 m_plotDataIndex++;
350 m_plotDataIndex++;
351 foreach (ChartItem* item ,m_chartItems)
351 foreach (ChartItem* item ,m_chartItems)
352 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
352 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
353 update();
353 update();
354 }else{
354 } else {
355 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
355 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
356 rect.setWidth(rect.width()/2);
356 rect.setWidth(rect.width()/2);
357 rect.setHeight(rect.height()/2);
357 rect.setHeight(rect.height()/2);
358 rect.moveCenter(m_rect.center());
358 rect.moveCenter(m_rect.center());
359 zoomInToRect(rect);
359 zoomInToRect(rect);
360 }
360 }
361 }
361 }
362
362
363 void QChart::zoomOut()
363 void QChart::zoomOut()
364 {
364 {
365 if (m_plotDataIndex > 0) {
365 if (m_plotDataIndex > 0) {
366 m_plotDataIndex--;
366 m_plotDataIndex--;
367 foreach (ChartItem* item ,m_chartItems)
367 foreach (ChartItem* item ,m_chartItems)
368 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
368 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
369 update();
369 update();
370 }
370 }
371 }
371 }
372
372
373 void QChart::zoomReset()
374 {
375 if (m_plotDataIndex > 0) {
376 m_plotDataIndex = 0;
377 foreach (ChartItem* item ,m_chartItems)
378 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
379 update();
380 }
381 }
382
373 void QChart::setAxisX(const QChartAxis& axis)
383 void QChart::setAxisX(const QChartAxis& axis)
374 {
384 {
375 setAxis(m_axisXItem,axis);
385 setAxis(m_axisXItem,axis);
376 }
386 }
377 void QChart::setAxisY(const QChartAxis& axis)
387 void QChart::setAxisY(const QChartAxis& axis)
378 {
388 {
379 setAxis(m_axisYItem.at(0),axis);
389 setAxis(m_axisYItem.at(0),axis);
380 }
390 }
381
391
382 void QChart::setAxisY(const QList<QChartAxis>& axis)
392 void QChart::setAxisY(const QList<QChartAxis>& axis)
383 {
393 {
384 //TODO not implemented
394 //TODO not implemented
385 }
395 }
386
396
387 void QChart::setAxis(AxisItem *item, const QChartAxis& axis)
397 void QChart::setAxis(AxisItem *item, const QChartAxis& axis)
388 {
398 {
389 item->setVisible(axis.isAxisVisible());
399 item->setVisible(axis.isAxisVisible());
390 }
400 }
391
401
392 #include "moc_qchart.cpp"
402 #include "moc_qchart.cpp"
393
403
394 QTCOMMERCIALCHART_END_NAMESPACE
404 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,103 +1,104
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 #include <QFont>
8 #include <QFont>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class AxisItem;
12 class AxisItem;
13 class QChartSeries;
13 class QChartSeries;
14 class PlotDomain;
14 class PlotDomain;
15 class ChartItem;
15 class ChartItem;
16 class BarGroup;
16 class BarGroup;
17 class QChartAxis;
17 class QChartAxis;
18
18
19 // TODO: We don't need to have QChart tied to QGraphicsItem:
19 // TODO: We don't need to have QChart tied to QGraphicsItem:
20 //class QTCOMMERCIALCHART_EXPORT QChart
20 //class QTCOMMERCIALCHART_EXPORT QChart
21 //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem {
21 //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem {
22 // public: QChartGraphicsItem(QChart &chart);
22 // public: QChartGraphicsItem(QChart &chart);
23
23
24 /*!
24 /*!
25 * TODO: define the responsibilities
25 * TODO: define the responsibilities
26 */
26 */
27 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject
27 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject
28 {
28 {
29 Q_OBJECT
29 Q_OBJECT
30 public:
30 public:
31 enum GradientOrientation {
31 enum GradientOrientation {
32 HorizonatlGradientOrientation,
32 HorizonatlGradientOrientation,
33 VerticalGradientOrientation
33 VerticalGradientOrientation
34 };
34 };
35 enum ChartThemeId {
35 enum ChartThemeId {
36 /*! The default theme follows the GUI style of the Operating System */
36 /*! The default theme follows the GUI style of the Operating System */
37 ChartThemeDefault = 0,
37 ChartThemeDefault = 0,
38 ChartThemeVanilla,
38 ChartThemeVanilla,
39 ChartThemeIcy,
39 ChartThemeIcy,
40 ChartThemeGrayscale,
40 ChartThemeGrayscale,
41 //ChartThemeScientific,
41 //ChartThemeScientific,
42 ChartThemeUnnamed1
42 ChartThemeUnnamed1
43 };
43 };
44
44
45 public:
45 public:
46 QChart(QGraphicsObject* parent = 0);
46 QChart(QGraphicsObject* parent = 0);
47 ~QChart();
47 ~QChart();
48
48
49 //from QGraphicsItem
49 //from QGraphicsItem
50 QRectF boundingRect() const;
50 QRectF boundingRect() const;
51 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
51 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
52
52
53 void addSeries(QChartSeries* series);
53 void addSeries(QChartSeries* series);
54 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
54 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
55 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
55 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
56 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
56 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
57
57
58 void setSize(const QSize& size);
58 void setSize(const QSize& size);
59 void setMargin(int margin);
59 void setMargin(int margin);
60 int margin() const;
60 int margin() const;
61 void setTheme(QChart::ChartThemeId theme);
61 void setTheme(QChart::ChartThemeId theme);
62
62
63 void setTitle(const QString& title,const QFont& font = QFont());
63 void setTitle(const QString& title,const QFont& font = QFont());
64 void setBackground(const QColor& startColor, const QColor& endColor = Qt::white, GradientOrientation orientation = VerticalGradientOrientation);
64 void setBackground(const QColor& startColor, const QColor& endColor = Qt::white, GradientOrientation orientation = VerticalGradientOrientation);
65
65
66 void zoomInToRect(const QRect& rectangle);
66 void zoomInToRect(const QRect& rectangle);
67 void zoomIn();
67 void zoomIn();
68 void zoomOut();
68 void zoomOut();
69 void zoomReset();
69
70
70 void setAxisX(const QChartAxis& axis);
71 void setAxisX(const QChartAxis& axis);
71 void setAxisY(const QChartAxis& axis);
72 void setAxisY(const QChartAxis& axis);
72 void setAxisY(const QList<QChartAxis>& axis);
73 void setAxisY(const QList<QChartAxis>& axis);
73
74
74 private:
75 private:
75 void setAxis(AxisItem *item, const QChartAxis& axis);
76 void setAxis(AxisItem *item, const QChartAxis& axis);
76
77
77 signals:
78 signals:
78 //TODO chage to const QSize& size
79 //TODO chage to const QSize& size
79 void sizeChanged(QRectF rect);
80 void sizeChanged(QRectF rect);
80 void scaleChanged(qreal xscale, qreal yscale);
81 void scaleChanged(qreal xscale, qreal yscale);
81
82
82 private:
83 private:
83 QColor nextColor();
84 QColor nextColor();
84
85
85 Q_DISABLE_COPY(QChart)
86 Q_DISABLE_COPY(QChart)
86 QGraphicsRectItem* m_backgroundItem;
87 QGraphicsRectItem* m_backgroundItem;
87 QLinearGradient m_backgroundGradient;
88 QLinearGradient m_backgroundGradient;
88 GradientOrientation m_bacgroundOrinetation;
89 GradientOrientation m_bacgroundOrinetation;
89 QGraphicsTextItem* m_titleItem;
90 QGraphicsTextItem* m_titleItem;
90 AxisItem* m_axisXItem;
91 AxisItem* m_axisXItem;
91 QList<AxisItem*> m_axisYItem;
92 QList<AxisItem*> m_axisYItem;
92 QRect m_rect;
93 QRect m_rect;
93 QList<QChartSeries*> m_chartSeries;
94 QList<QChartSeries*> m_chartSeries;
94 QVector<PlotDomain> m_plotDomainList;
95 QVector<PlotDomain> m_plotDomainList;
95 QList<ChartItem*> m_chartItems;
96 QList<ChartItem*> m_chartItems;
96 int m_plotDataIndex;
97 int m_plotDataIndex;
97 int m_marginSize;
98 int m_marginSize;
98 QList<QColor> m_themeColors;
99 QList<QColor> m_themeColors;
99 };
100 };
100
101
101 QTCOMMERCIALCHART_END_NAMESPACE
102 QTCOMMERCIALCHART_END_NAMESPACE
102
103
103 #endif
104 #endif
@@ -1,99 +1,100
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 m_rubberBand(QRubberBand::Rectangle, this),
12 m_originX(0)
12 {
13 {
13 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
14 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
14 m_scene = new QGraphicsScene();
15 m_scene = new QGraphicsScene();
15 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
16 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
16 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
17 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
17 setScene(m_scene);
18 setScene(m_scene);
18
19
19 m_chart = new QChart();
20 m_chart = new QChart();
20 m_scene->addItem(m_chart);
21 m_scene->addItem(m_chart);
21 m_rubberBand.setEnabled(true); // TODO: should zoom be enabled by default?
22 m_rubberBand.setEnabled(true); // TODO: should zoom be enabled by default?
22 show();
23 show();
23 }
24 }
24
25
25 QChartWidget::~QChartWidget()
26 QChartWidget::~QChartWidget()
26 {
27 {
27 }
28 }
28
29
29 void QChartWidget::resizeEvent(QResizeEvent *event)
30 void QChartWidget::resizeEvent(QResizeEvent *event)
30 {
31 {
31 m_scene->setSceneRect(0,0,size().width(),size().height());
32 m_scene->setSceneRect(0,0,size().width(),size().height());
32 m_chart->setSize(size());
33 m_chart->setSize(size());
33 QWidget::resizeEvent(event);
34 QWidget::resizeEvent(event);
34 }
35 }
35
36
36 QSize QChartWidget::sizeHint() const
37 QSize QChartWidget::sizeHint() const
37 {
38 {
38 // TODO: calculate size hint based on contents?
39 // TODO: calculate size hint based on contents?
39 return QSize(100, 100);
40 return QSize(100, 100);
40 }
41 }
41
42
42 void QChartWidget::mousePressEvent(QMouseEvent *event)
43 void QChartWidget::mousePressEvent(QMouseEvent *event)
43 {
44 {
44 if(m_rubberBand.isEnabled() && event->button() == Qt::LeftButton) {
45 if(m_rubberBand.isEnabled() && event->button() == Qt::LeftButton) {
45 int margin = m_chart->margin();
46 int margin = m_chart->margin();
46 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
47 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
47 m_origin = event->pos();
48
48
49 if (rect.contains(m_origin)) {
49 if (rect.contains(event->pos())) {
50 m_rubberBand.setGeometry(QRect(m_origin, QSize()));
50 m_originX = event->pos().x();
51 m_rubberBand.setGeometry(QRect(m_originX, 0, 0, height()));
51 m_rubberBand.show();
52 m_rubberBand.show();
52 event->accept();
53 event->accept();
53 }
54 }
54 }
55 }
55 }
56 }
56
57
57 void QChartWidget::mouseMoveEvent(QMouseEvent *event)
58 void QChartWidget::mouseMoveEvent(QMouseEvent *event)
58 {
59 {
59 if(m_rubberBand.isVisible())
60 if(m_rubberBand.isVisible())
60 m_rubberBand.setGeometry(QRect(m_origin, event->pos()).normalized());
61 m_rubberBand.setGeometry(QRect(m_originX, 0,
62 event->pos().x() - m_originX, height()).normalized());
61 }
63 }
62
64
63 void QChartWidget::mouseReleaseEvent(QMouseEvent *event)
65 void QChartWidget::mouseReleaseEvent(QMouseEvent *event)
64 {
66 {
65 if (event->button() == Qt::LeftButton && m_rubberBand.isVisible()) {
67 if (event->button() == Qt::LeftButton && m_rubberBand.isVisible()) {
66 m_rubberBand.hide();
68 m_rubberBand.hide();
67 QRect rect = m_rubberBand.geometry();
69 QRect rect = m_rubberBand.geometry();
68 m_chart->zoomInToRect(rect);
70 m_chart->zoomInToRect(rect);
69 event->accept();
71 event->accept();
70 }
72 }
71
73
72 if(event->button()==Qt::RightButton) {
74 if(event->button()==Qt::RightButton)
73 m_chart->zoomOut();
75 m_chart->zoomReset();
74 }
75 }
76 }
76
77
77 void QChartWidget::addSeries(QChartSeries* series)
78 void QChartWidget::addSeries(QChartSeries* series)
78 {
79 {
79 m_chart->addSeries(series);
80 m_chart->addSeries(series);
80 }
81 }
81
82
82 QChartSeries* QChartWidget::createSeries(QChartSeries::QChartSeriesType type)
83 QChartSeries* QChartWidget::createSeries(QChartSeries::QChartSeriesType type)
83 {
84 {
84 return m_chart->createSeries(type);
85 return m_chart->createSeries(type);
85 }
86 }
86
87
87 void QChartWidget::setTheme(QChart::ChartThemeId theme)
88 void QChartWidget::setTheme(QChart::ChartThemeId theme)
88 {
89 {
89 m_chart->setTheme(theme);
90 m_chart->setTheme(theme);
90 }
91 }
91
92
92 void QChartWidget::setZoomEnabled(bool enabled)
93 void QChartWidget::setZoomEnabled(bool enabled)
93 {
94 {
94 m_rubberBand.setEnabled(enabled);
95 m_rubberBand.setEnabled(enabled);
95 }
96 }
96
97
97 #include "moc_qchartwidget.cpp"
98 #include "moc_qchartwidget.cpp"
98
99
99 QTCOMMERCIALCHART_END_NAMESPACE
100 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,56 +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 #include <QRubberBand>
8
8
9 class QGraphicsScene;
9 class QGraphicsScene;
10 class QRubberBand;
10 class QRubberBand;
11
11
12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13
13
14 class QChartSeries;
14 class QChartSeries;
15 class QChartWidgetPrivate;
15 class QChartWidgetPrivate;
16
16
17 class QTCOMMERCIALCHART_EXPORT QChartWidget : public QGraphicsView
17 class QTCOMMERCIALCHART_EXPORT QChartWidget : public QGraphicsView
18 {
18 {
19 Q_OBJECT
19 Q_OBJECT
20 public:
20 public:
21 explicit QChartWidget(QWidget *parent = 0);
21 explicit QChartWidget(QWidget *parent = 0);
22 ~QChartWidget();
22 ~QChartWidget();
23
23
24 // TODO: addSeries and createSeries are optional solutions
24 // TODO: addSeries and createSeries are optional solutions
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)?
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:
26 public Q_SLOTS:
27 void addSeries(QChartSeries* series);
27 void addSeries(QChartSeries* series);
28 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
28 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
29
29
30 /*!
30 /*!
31 * 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
32 * the chart.
32 * the chart.
33 */
33 */
34 void setTheme(QChart::ChartThemeId theme);
34 void setTheme(QChart::ChartThemeId theme);
35
35
36 void setZoomEnabled(bool enabled);
36 void setZoomEnabled(bool enabled);
37
37
38 private: // From QWidget TODO: should these be protected instead? Is QChartWidget meant to be extened by the user?
38 private: // From QWidget TODO: should these be protected instead? Is QChartWidget meant to be extened by the user?
39 void resizeEvent(QResizeEvent *event);
39 void resizeEvent(QResizeEvent *event);
40 QSize sizeHint() const;
40 QSize sizeHint() const;
41 void mousePressEvent(QMouseEvent *event);
41 void mousePressEvent(QMouseEvent *event);
42 void mouseMoveEvent(QMouseEvent *event);
42 void mouseMoveEvent(QMouseEvent *event);
43 void mouseReleaseEvent(QMouseEvent *event);
43 void mouseReleaseEvent(QMouseEvent *event);
44
44
45 private:
45 private:
46 Q_DISABLE_COPY(QChartWidget)
46 Q_DISABLE_COPY(QChartWidget)
47 // TODO: move the following to pimpl
47 // TODO: move the following to pimpl
48 QGraphicsScene *m_scene;
48 QGraphicsScene *m_scene;
49 QChart* m_chart;
49 QChart* m_chart;
50 QRubberBand m_rubberBand;
50 QRubberBand m_rubberBand;
51 QPoint m_origin;
51 int m_originX;
52 };
52 };
53
53
54 QTCOMMERCIALCHART_END_NAMESPACE
54 QTCOMMERCIALCHART_END_NAMESPACE
55
55
56 #endif // QCHARTWIDGET_H
56 #endif // QCHARTWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now