##// END OF EJS Templates
Adds title support
Michal Klocek -
r87:efb0f73ff869
parent child
Show More
@@ -1,44 +1,45
1 #include <QApplication>
1 #include <QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <qchartview.h>
3 #include <qchartview.h>
4 #include <qxychartseries.h>
4 #include <qxychartseries.h>
5 #include <qchart.h>
5 #include <qchart.h>
6 #include <cmath>
6 #include <cmath>
7
7
8 QTCOMMERCIALCHART_USE_NAMESPACE
8 QTCOMMERCIALCHART_USE_NAMESPACE
9
9
10 #define PI 3.14159265358979
10 #define PI 3.14159265358979
11
11
12 int main(int argc, char *argv[])
12 int main(int argc, char *argv[])
13 {
13 {
14 QApplication a(argc, argv);
14 QApplication a(argc, argv);
15
15
16 QMainWindow window;
16 QMainWindow window;
17
17
18 QXYChartSeries* series0 = QXYChartSeries::create();
18 QXYChartSeries* series0 = QXYChartSeries::create();
19 QPen blue(Qt::blue);
19 QPen blue(Qt::blue);
20 blue.setWidth(3);
20 blue.setWidth(3);
21 series0->setPen(blue);
21 series0->setPen(blue);
22 QXYChartSeries* series1 = QXYChartSeries::create();
22 QXYChartSeries* series1 = QXYChartSeries::create();
23 QPen red(Qt::red);
23 QPen red(Qt::red);
24 red.setWidth(3);
24 red.setWidth(3);
25 series1->setPen(red);
25 series1->setPen(red);
26
26
27 int numPoints = 100;
27 int numPoints = 100;
28
28
29 for (int x = 0; x <= numPoints; ++x) {
29 for (int x = 0; x <= numPoints; ++x) {
30 series0->add(x, abs(sin(PI/50*x)*100));
30 series0->add(x, abs(sin(PI/50*x)*100));
31 series1->add(x, abs(cos(PI/50*x)*100));
31 series1->add(x, abs(cos(PI/50*x)*100));
32 }
32 }
33
33
34 QChartView* chartView = new QChartView(&window);
34 QChartView* chartView = new QChartView(&window);
35 chartView->setRenderHint(QPainter::Antialiasing);
35 chartView->setRenderHint(QPainter::Antialiasing);
36 chartView->setTitle("Example: sin & cos functions");
36 chartView->addSeries(series0);
37 chartView->addSeries(series0);
37 chartView->addSeries(series1);
38 chartView->addSeries(series1);
38
39
39 window.setCentralWidget(chartView);
40 window.setCentralWidget(chartView);
40 window.resize(400, 300);
41 window.resize(400, 300);
41 window.show();
42 window.show();
42
43
43 return a.exec();
44 return a.exec();
44 }
45 }
@@ -1,392 +1,395
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_background(0),
20 m_backgroundItem(0),
21 m_title(0),
21 m_titleItem(0),
22 m_axisX(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::ChartThemeVanilla);
27 //setTheme(QChart::ChartThemeVanilla);
28
28
29 PlotDomain domain;
29 PlotDomain domain;
30 m_plotDomainList<<domain;
30 m_plotDomainList<<domain;
31 m_axisY << new AxisItem(AxisItem::Y_AXIS,this);
31 m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this);
32 m_chartItems<<m_axisX;
32 m_chartItems<<m_axisXItem;
33 m_chartItems<<m_axisY.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_title){
167 if(m_titleItem){
168 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
169 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
168
170
169 }
171 }
170
172
171 //recalculate background gradient
173 //recalculate background gradient
172 if(m_background){
174 if(m_backgroundItem){
173 m_background->setRect(rect);
175 m_backgroundItem->setRect(rect);
174 if(m_bacgroundOrinetation==HorizonatlGradientOrientation)
176 if(m_bacgroundOrinetation==HorizonatlGradientOrientation)
175 m_backgroundGradient.setFinalStop(m_background->rect().width(),0);
177 m_backgroundGradient.setFinalStop(m_backgroundItem->rect().width(),0);
176 else
178 else
177 m_backgroundGradient.setFinalStop(0,m_background->rect().height());
179 m_backgroundGradient.setFinalStop(0,m_backgroundItem->rect().height());
178
180
179 m_background->setBrush(m_backgroundGradient);
181 m_backgroundItem->setBrush(m_backgroundGradient);
180 }
182 }
181
183
182 //resize elements
184 //resize elements
183 foreach (ChartItem* item ,m_chartItems) {
185 foreach (ChartItem* item ,m_chartItems) {
184 item->setPos(rect.topLeft());
186 item->setPos(rect.topLeft());
185 item->setSize(rect.size());
187 item->setSize(rect.size());
186
188
187 }
189 }
188 // TODO: TTD for setting scale
190 // TODO: TTD for setting scale
189 //emit scaleChanged(100, 100);
191 //emit scaleChanged(100, 100);
190 // TODO: calculate the origo
192 // TODO: calculate the origo
191 // 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
192 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
194 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
193
195
194 update();
196 update();
195 }
197 }
196
198
197 void QChart::setBackground(const QColor& startColor, const QColor& endColor, GradientOrientation orientation)
199 void QChart::setBackground(const QColor& startColor, const QColor& endColor, GradientOrientation orientation)
198 {
200 {
199
201
200 if(!m_background){
202 if(!m_backgroundItem){
201 m_background = new QGraphicsRectItem(this);
203 m_backgroundItem = new QGraphicsRectItem(this);
202 m_background->setZValue(-1);
204 m_backgroundItem->setZValue(-1);
203 }
205 }
204
206
205 m_bacgroundOrinetation = orientation;
207 m_bacgroundOrinetation = orientation;
206 m_backgroundGradient.setColorAt( 0.0, startColor);
208 m_backgroundGradient.setColorAt( 0.0, startColor);
207 m_backgroundGradient.setColorAt( 1.0, endColor);
209 m_backgroundGradient.setColorAt( 1.0, endColor);
208 m_backgroundGradient.setStart(0,0);
210 m_backgroundGradient.setStart(0,0);
209
211
210 if(orientation == VerticalGradientOrientation){
212 if(orientation == VerticalGradientOrientation){
211 m_backgroundGradient.setFinalStop(0,m_rect.height());
213 m_backgroundGradient.setFinalStop(0,m_rect.height());
212 }else{
214 }else{
213 m_backgroundGradient.setFinalStop(m_rect.width(),0);
215 m_backgroundGradient.setFinalStop(m_rect.width(),0);
214 }
216 }
215
217
216 m_background->setBrush(m_backgroundGradient);
218 m_backgroundItem->setBrush(m_backgroundGradient);
217 m_background->setPen(Qt::NoPen);
219 m_backgroundItem->setPen(Qt::NoPen);
218 m_background->update();
220 m_backgroundItem->update();
219 }
221 }
220
222
221 void QChart::setTitle(const QString& title)
223 void QChart::setTitle(const QString& title,const QFont& font)
222 {
224 {
223 if(!m_title) m_title = new QGraphicsTextItem(this);
225 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
224 m_title->setPlainText(title);
226 m_titleItem->setPlainText(title);
227 m_titleItem->setFont(font);
225 }
228 }
226
229
227 int QChart::margin() const
230 int QChart::margin() const
228 {
231 {
229 return m_marginSize;
232 return m_marginSize;
230 }
233 }
231
234
232 void QChart::setMargin(int margin)
235 void QChart::setMargin(int margin)
233 {
236 {
234 m_marginSize = margin;
237 m_marginSize = margin;
235 }
238 }
236
239
237 void QChart::setTheme(QChart::ChartThemeId theme)
240 void QChart::setTheme(QChart::ChartThemeId theme)
238 {
241 {
239 // if (theme != m_currentTheme) {
242 // if (theme != m_currentTheme) {
240 m_themeColors.clear();
243 m_themeColors.clear();
241
244
242 // TODO: define color themes
245 // TODO: define color themes
243 switch (theme) {
246 switch (theme) {
244 case QChart::ChartThemeDefault:
247 case QChart::ChartThemeDefault:
245 // TODO: define the default theme based on the OS
248 // TODO: define the default theme based on the OS
246 // For now we just fallthrough to "vanilla"
249 // For now we just fallthrough to "vanilla"
247 case QChart::ChartThemeVanilla:
250 case QChart::ChartThemeVanilla:
248 m_themeColors.append(QColor(217, 197, 116));
251 m_themeColors.append(QColor(217, 197, 116));
249 m_themeColors.append(QColor(214, 168, 150));
252 m_themeColors.append(QColor(214, 168, 150));
250 m_themeColors.append(QColor(160, 160, 113));
253 m_themeColors.append(QColor(160, 160, 113));
251 m_themeColors.append(QColor(210, 210, 52));
254 m_themeColors.append(QColor(210, 210, 52));
252 m_themeColors.append(QColor(136, 114, 58));
255 m_themeColors.append(QColor(136, 114, 58));
253
256
254 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xff9d844d)));
257 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xff9d844d)));
255 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf)));
258 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf)));
256 break;
259 break;
257 case QChart::ChartThemeIcy:
260 case QChart::ChartThemeIcy:
258 m_themeColors.append(QColor(0, 3, 165));
261 m_themeColors.append(QColor(0, 3, 165));
259 m_themeColors.append(QColor(49, 52, 123));
262 m_themeColors.append(QColor(49, 52, 123));
260 m_themeColors.append(QColor(71, 114, 187));
263 m_themeColors.append(QColor(71, 114, 187));
261 m_themeColors.append(QColor(48, 97, 87));
264 m_themeColors.append(QColor(48, 97, 87));
262 m_themeColors.append(QColor(19, 71, 90));
265 m_themeColors.append(QColor(19, 71, 90));
263 m_themeColors.append(QColor(110, 70, 228));
266 m_themeColors.append(QColor(110, 70, 228));
264
267
265 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xffe4ffff)));
268 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xffe4ffff)));
266 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffe4ffff)));
269 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffe4ffff)));
267 break;
270 break;
268 case QChart::ChartThemeGrayscale:
271 case QChart::ChartThemeGrayscale:
269 m_themeColors.append(QColor(0, 0, 0));
272 m_themeColors.append(QColor(0, 0, 0));
270 m_themeColors.append(QColor(50, 50, 50));
273 m_themeColors.append(QColor(50, 50, 50));
271 m_themeColors.append(QColor(100, 100, 100));
274 m_themeColors.append(QColor(100, 100, 100));
272 m_themeColors.append(QColor(140, 140, 140));
275 m_themeColors.append(QColor(140, 140, 140));
273 m_themeColors.append(QColor(180, 180, 180));
276 m_themeColors.append(QColor(180, 180, 180));
274
277
275 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xffffffff)));
278 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xffffffff)));
276 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf)));
279 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf)));
277 break;
280 break;
278 case QChart::ChartThemeUnnamed1:
281 case QChart::ChartThemeUnnamed1:
279 m_themeColors.append(QColor(QRgb(0xff3fa9f5)));
282 m_themeColors.append(QColor(QRgb(0xff3fa9f5)));
280 m_themeColors.append(QColor(QRgb(0xff7AC943)));
283 m_themeColors.append(QColor(QRgb(0xff7AC943)));
281 m_themeColors.append(QColor(QRgb(0xffFF931E)));
284 m_themeColors.append(QColor(QRgb(0xffFF931E)));
282 m_themeColors.append(QColor(QRgb(0xffFF1D25)));
285 m_themeColors.append(QColor(QRgb(0xffFF1D25)));
283 m_themeColors.append(QColor(QRgb(0xffFF7BAC)));
286 m_themeColors.append(QColor(QRgb(0xffFF7BAC)));
284
287
285 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xfff3dc9e)));
288 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xfff3dc9e)));
286 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf)));
289 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf)));
287 break;
290 break;
288 default:
291 default:
289 Q_ASSERT(false);
292 Q_ASSERT(false);
290 break;
293 break;
291 }
294 }
292
295
293 if(m_background){
296 if(m_backgroundItem){
294 m_background->setBrush(m_backgroundGradient);
297 m_backgroundItem->setBrush(m_backgroundGradient);
295 m_background->setPen(Qt::NoPen);
298 m_backgroundItem->setPen(Qt::NoPen);
296 }
299 }
297
300
298 foreach(QChartSeries* series, m_chartSeries) {
301 foreach(QChartSeries* series, m_chartSeries) {
299 // TODO: other series interested on themes?
302 // TODO: other series interested on themes?
300 if (series->type() == QChartSeries::SeriesTypeLine) {
303 if (series->type() == QChartSeries::SeriesTypeLine) {
301 QXYChartSeries *lineseries = reinterpret_cast<QXYChartSeries *>(series);
304 QXYChartSeries *lineseries = reinterpret_cast<QXYChartSeries *>(series);
302 lineseries->setPen(nextColor());
305 lineseries->setPen(nextColor());
303 } else if (series->type() == QChartSeries::SeriesTypeScatter) {
306 } else if (series->type() == QChartSeries::SeriesTypeScatter) {
304 QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series);
307 QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series);
305 scatter->setMarkerColor(nextColor());
308 scatter->setMarkerColor(nextColor());
306 } else if (series->type() == QChartSeries::SeriesTypePie) {
309 } else if (series->type() == QChartSeries::SeriesTypePie) {
307 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
310 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
308 for (int i(0); i < pieSeries->sliceCount(); i++)
311 for (int i(0); i < pieSeries->sliceCount(); i++)
309 pieSeries->setSliceColor(i, nextColor());
312 pieSeries->setSliceColor(i, nextColor());
310 }
313 }
311 }
314 }
312 update();
315 update();
313 }
316 }
314
317
315 QColor QChart::nextColor()
318 QColor QChart::nextColor()
316 {
319 {
317 QColor nextColor = m_themeColors.first();
320 QColor nextColor = m_themeColors.first();
318 m_themeColors.move(0, m_themeColors.size() - 1);
321 m_themeColors.move(0, m_themeColors.size() - 1);
319 return nextColor;
322 return nextColor;
320 }
323 }
321
324
322 void QChart::zoomInToRect(const QRect& rectangle)
325 void QChart::zoomInToRect(const QRect& rectangle)
323 {
326 {
324
327
325 if(!rectangle.isValid()) return;
328 if(!rectangle.isValid()) return;
326
329
327 qreal margin = this->margin();
330 qreal margin = this->margin();
328
331
329 QRect rect = rectangle.normalized();
332 QRect rect = rectangle.normalized();
330 rect.translate(-margin, -margin);
333 rect.translate(-margin, -margin);
331
334
332 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
335 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
333
336
334 PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin);
337 PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin);
335
338
336 m_plotDomainList.resize(m_plotDataIndex + 1);
339 m_plotDomainList.resize(m_plotDataIndex + 1);
337 m_plotDomainList<<domain;
340 m_plotDomainList<<domain;
338 m_plotDataIndex++;
341 m_plotDataIndex++;
339
342
340 foreach (ChartItem* item ,m_chartItems)
343 foreach (ChartItem* item ,m_chartItems)
341 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
344 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
342 update();
345 update();
343 }
346 }
344
347
345 void QChart::zoomIn()
348 void QChart::zoomIn()
346 {
349 {
347 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
350 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
348 m_plotDataIndex++;
351 m_plotDataIndex++;
349 foreach (ChartItem* item ,m_chartItems)
352 foreach (ChartItem* item ,m_chartItems)
350 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
353 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
351 update();
354 update();
352 }else{
355 }else{
353 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
356 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
354 rect.setWidth(rect.width()/2);
357 rect.setWidth(rect.width()/2);
355 rect.setHeight(rect.height()/2);
358 rect.setHeight(rect.height()/2);
356 rect.moveCenter(m_rect.center());
359 rect.moveCenter(m_rect.center());
357 zoomInToRect(rect);
360 zoomInToRect(rect);
358 }
361 }
359 }
362 }
360
363
361 void QChart::zoomOut()
364 void QChart::zoomOut()
362 {
365 {
363 if (m_plotDataIndex > 0) {
366 if (m_plotDataIndex > 0) {
364 m_plotDataIndex--;
367 m_plotDataIndex--;
365 foreach (ChartItem* item ,m_chartItems)
368 foreach (ChartItem* item ,m_chartItems)
366 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
369 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
367 update();
370 update();
368 }
371 }
369 }
372 }
370
373
371 void QChart::setAxisX(const QChartAxis& axis)
374 void QChart::setAxisX(const QChartAxis& axis)
372 {
375 {
373 setAxis(m_axisX,axis);
376 setAxis(m_axisXItem,axis);
374 }
377 }
375 void QChart::setAxisY(const QChartAxis& axis)
378 void QChart::setAxisY(const QChartAxis& axis)
376 {
379 {
377 setAxis(m_axisY.at(0),axis);
380 setAxis(m_axisYItem.at(0),axis);
378 }
381 }
379
382
380 void QChart::setAxisY(const QList<QChartAxis>& axis)
383 void QChart::setAxisY(const QList<QChartAxis>& axis)
381 {
384 {
382 //TODO not implemented
385 //TODO not implemented
383 }
386 }
384
387
385 void QChart::setAxis(AxisItem *item, const QChartAxis& axis)
388 void QChart::setAxis(AxisItem *item, const QChartAxis& axis)
386 {
389 {
387 item->setVisible(axis.isAxisVisible());
390 item->setVisible(axis.isAxisVisible());
388 }
391 }
389
392
390 #include "moc_qchart.cpp"
393 #include "moc_qchart.cpp"
391
394
392 QTCOMMERCIALCHART_END_NAMESPACE
395 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,102 +1,103
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
9
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
11
11 class AxisItem;
12 class AxisItem;
12 class QChartSeries;
13 class QChartSeries;
13 class PlotDomain;
14 class PlotDomain;
14 class ChartItem;
15 class ChartItem;
15 class BarGroup;
16 class BarGroup;
16 class QChartAxis;
17 class QChartAxis;
17
18
18 // TODO: We don't need to have QChart tied to QGraphicsItem:
19 // TODO: We don't need to have QChart tied to QGraphicsItem:
19 //class QTCOMMERCIALCHART_EXPORT QChart
20 //class QTCOMMERCIALCHART_EXPORT QChart
20 //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem {
21 //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem {
21 // public: QChartGraphicsItem(QChart &chart);
22 // public: QChartGraphicsItem(QChart &chart);
22
23
23 /*!
24 /*!
24 * TODO: define the responsibilities
25 * TODO: define the responsibilities
25 */
26 */
26 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject
27 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject
27 {
28 {
28 Q_OBJECT
29 Q_OBJECT
29 public:
30 public:
30 enum GradientOrientation {
31 enum GradientOrientation {
31 HorizonatlGradientOrientation,
32 HorizonatlGradientOrientation,
32 VerticalGradientOrientation
33 VerticalGradientOrientation
33 };
34 };
34 enum ChartThemeId {
35 enum ChartThemeId {
35 /*! The default theme follows the GUI style of the Operating System */
36 /*! The default theme follows the GUI style of the Operating System */
36 ChartThemeDefault = 0,
37 ChartThemeDefault = 0,
37 ChartThemeVanilla,
38 ChartThemeVanilla,
38 ChartThemeIcy,
39 ChartThemeIcy,
39 ChartThemeGrayscale,
40 ChartThemeGrayscale,
40 //ChartThemeScientific,
41 //ChartThemeScientific,
41 ChartThemeUnnamed1
42 ChartThemeUnnamed1
42 };
43 };
43
44
44 public:
45 public:
45 QChart(QGraphicsObject* parent = 0);
46 QChart(QGraphicsObject* parent = 0);
46 ~QChart();
47 ~QChart();
47
48
48 //from QGraphicsItem
49 //from QGraphicsItem
49 QRectF boundingRect() const;
50 QRectF boundingRect() const;
50 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
51 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
51
52
52 void addSeries(QChartSeries* series);
53 void addSeries(QChartSeries* series);
53 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
54 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
54 // 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...
55 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
56 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
56
57
57 void setSize(const QSize& size);
58 void setSize(const QSize& size);
58 void setMargin(int margin);
59 void setMargin(int margin);
59 int margin() const;
60 int margin() const;
60 void setTheme(QChart::ChartThemeId theme);
61 void setTheme(QChart::ChartThemeId theme);
61
62
62 void setTitle(const QString& title);
63 void setTitle(const QString& title,const QFont& font = QFont());
63 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);
64
65
65 void zoomInToRect(const QRect& rectangle);
66 void zoomInToRect(const QRect& rectangle);
66 void zoomIn();
67 void zoomIn();
67 void zoomOut();
68 void zoomOut();
68
69
69 void setAxisX(const QChartAxis& axis);
70 void setAxisX(const QChartAxis& axis);
70 void setAxisY(const QChartAxis& axis);
71 void setAxisY(const QChartAxis& axis);
71 void setAxisY(const QList<QChartAxis>& axis);
72 void setAxisY(const QList<QChartAxis>& axis);
72
73
73 private:
74 private:
74 void setAxis(AxisItem *item, const QChartAxis& axis);
75 void setAxis(AxisItem *item, const QChartAxis& axis);
75
76
76 signals:
77 signals:
77 //TODO chage to const QSize& size
78 //TODO chage to const QSize& size
78 void sizeChanged(QRectF rect);
79 void sizeChanged(QRectF rect);
79 void scaleChanged(qreal xscale, qreal yscale);
80 void scaleChanged(qreal xscale, qreal yscale);
80
81
81 private:
82 private:
82 QColor nextColor();
83 QColor nextColor();
83
84
84 Q_DISABLE_COPY(QChart)
85 Q_DISABLE_COPY(QChart)
85 QGraphicsRectItem* m_background;
86 QGraphicsRectItem* m_backgroundItem;
86 QLinearGradient m_backgroundGradient;
87 QLinearGradient m_backgroundGradient;
87 GradientOrientation m_bacgroundOrinetation;
88 GradientOrientation m_bacgroundOrinetation;
88 QGraphicsTextItem* m_title;
89 QGraphicsTextItem* m_titleItem;
89 AxisItem* m_axisX;
90 AxisItem* m_axisXItem;
90 QList<AxisItem*> m_axisY;
91 QList<AxisItem*> m_axisYItem;
91 QRect m_rect;
92 QRect m_rect;
92 QList<QChartSeries*> m_chartSeries;
93 QList<QChartSeries*> m_chartSeries;
93 QVector<PlotDomain> m_plotDomainList;
94 QVector<PlotDomain> m_plotDomainList;
94 QList<ChartItem*> m_chartItems;
95 QList<ChartItem*> m_chartItems;
95 int m_plotDataIndex;
96 int m_plotDataIndex;
96 int m_marginSize;
97 int m_marginSize;
97 QList<QColor> m_themeColors;
98 QList<QColor> m_themeColors;
98 };
99 };
99
100
100 QTCOMMERCIALCHART_END_NAMESPACE
101 QTCOMMERCIALCHART_END_NAMESPACE
101
102
102 #endif
103 #endif
General Comments 0
You need to be logged in to leave comments. Login now