##// END OF EJS Templates
Add gradient bacground support...
Michal Klocek -
r86:84e7b4b4f9e0
parent child
Show More
@@ -1,44 +1,45
1 1 #include <QApplication>
2 2 #include <QMainWindow>
3 3 #include <qchartview.h>
4 4 #include <qxychartseries.h>
5 5 #include <qchart.h>
6 6 #include <cmath>
7 7
8 8 QTCOMMERCIALCHART_USE_NAMESPACE
9 9
10 10 #define PI 3.14159265358979
11 11
12 12 int main(int argc, char *argv[])
13 13 {
14 14 QApplication a(argc, argv);
15 15
16 16 QMainWindow window;
17 17
18 18 QXYChartSeries* series0 = QXYChartSeries::create();
19 19 QPen blue(Qt::blue);
20 20 blue.setWidth(3);
21 21 series0->setPen(blue);
22 22 QXYChartSeries* series1 = QXYChartSeries::create();
23 23 QPen red(Qt::red);
24 24 red.setWidth(3);
25 25 series1->setPen(red);
26 26
27 27 int numPoints = 100;
28 28
29 29 for (int x = 0; x <= numPoints; ++x) {
30 30 series0->add(x, abs(sin(PI/50*x)*100));
31 31 series1->add(x, abs(cos(PI/50*x)*100));
32 32 }
33 33
34 34 QChartView* chartView = new QChartView(&window);
35 chartView->setRenderHint(QPainter::Antialiasing);
35 36 chartView->addSeries(series0);
36 37 chartView->addSeries(series1);
37 //chartView->setBackgroundColor(Qt::yellow);
38 chartView->setBackground(Qt::blue,Qt::yellow,QChart::HorizonatlGradientOrientation);
38 39
39 40 window.setCentralWidget(chartView);
40 41 window.resize(400, 300);
41 42 window.show();
42 43
43 44 return a.exec();
44 45 }
@@ -1,170 +1,162
1 1 #include "axisitem_p.h"
2 2 #include <QPainter>
3 3 #include <QDebug>
4 4
5 5 #define LABEL_PADDING 5
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent): ChartItem(parent),
10 10 m_ticks(4),
11 11 m_type(type)
12 12 {
13 13 }
14 14
15 15 AxisItem::~AxisItem()
16 16 {
17 17 }
18 18
19 19 void AxisItem::setSize(const QSize& size)
20 20 {
21 21 m_rect = QRectF(QPoint(0,0),size);
22 22 createItems();
23 23 }
24 24
25 25 void AxisItem::setLength(int length)
26 26 {
27 27 QPainterPath path;
28 28 path.moveTo(QPointF(0,0));
29 29 path.lineTo(length,0);
30 30 // path.lineTo(length-4,0);
31 31 // path.lineTo(length,3);
32 32 // path.lineTo(length-4,6);
33 33 // path.lineTo(length-4,4);
34 34 // path.lineTo(0,4);
35 35 // path.lineTo(0,2);
36 36 m_path=path;
37 37 update();
38 38 }
39 39
40 40 QRectF AxisItem::boundingRect() const
41 41 {
42 42 return m_rect;
43 43 }
44 44
45 45 void AxisItem::setPlotDomain(const PlotDomain& plotDomain)
46 46 {
47 47 m_plotDomain = plotDomain;
48 48 createItems();
49 49 }
50 50 /*
51 51 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
52 52 {
53 53 if (!m_rect.isValid())
54 54 return;
55 55
56 56 if(m_type==X_AXIS) {
57 57
58 58 const qreal deltaX = m_rect.width() / m_ticks;
59 59
60 60 for (int i = 0; i <= m_ticks; ++i) {
61 61
62 62 int x = i * deltaX + m_rect.left();
63 63
64 64 if(i==0) x--;
65 65 if(i==m_ticks) x++;
66 66
67 67 qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()
68 68 / m_ticks);
69 69 painter->drawLine(x, m_rect.top()-1, x, m_rect.bottom()+1);
70 70 // painter->drawLine(x, m_rect.bottom()-1, x, m_rect.bottom()-1 + 5);
71 71
72 72 painter->drawText(x - 50, m_rect.bottom() + 5, 100, 20,Qt::AlignHCenter | Qt::AlignTop, QString::number(label));
73 73 }
74 74 }
75 75
76 76 if(m_type==Y_AXIS) {
77 77
78 78 const qreal deltaY = (m_rect.height()) / m_ticks;
79 79
80 80 for (int j = 0; j <= m_ticks; ++j) {
81 81
82 82 int y = j * -deltaY + m_rect.bottom();
83 83
84 84 if(j==0) y++;
85 85 if(j==m_ticks) y--;
86 86
87 87 qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY()
88 88 / m_ticks);
89 89
90 90 painter->drawLine(m_rect.left()-1, y, m_rect.right()+1, y);
91 91 //painter->drawLine(m_rect.left() - 5, y, m_rect.left(), y);
92 92 //TODO : margin = 50 ;
93 93 painter->drawText(m_rect.left() - 50, y - 10, 50 - 5, 20,
94 94 Qt::AlignRight | Qt::AlignVCenter,
95 95 QString::number(label));
96 96 }
97 97 }
98 98
99 99 //painter->drawRect(m_rect.adjusted(0, 0, -1, -1));
100 100 }
101 101 */
102 102 void AxisItem::createItems()
103 103 {
104 104
105 105 //TODO: this is very inefficient handling
106 106
107 107 qDeleteAll(m_shades);
108 108 m_shades.clear();
109 109 qDeleteAll(m_grid);
110 110 m_grid.clear();
111 111 qDeleteAll(m_labels);
112 112 m_labels.clear();
113 113
114 114
115 115 if(m_type==X_AXIS) {
116 116
117 117 const qreal deltaX = m_rect.width() / m_ticks;
118 118
119 119 for (int i = 0; i <= m_ticks; ++i) {
120 120
121 121 int x = i * deltaX + m_rect.left();
122 122
123 //last grid outside chart rect
124 if(i==0) x--;
125 if(i==m_ticks) x++;
126
127 123 qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()/ m_ticks);
128 124
129 m_grid<<new QGraphicsLineItem(x, m_rect.top()-1, x, m_rect.bottom()+1+LABEL_PADDING,this);
125 m_grid<<new QGraphicsLineItem(x, m_rect.top(), x, m_rect.bottom(),this);
130 126
131 127 QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this);
132 128 QPointF center = text->boundingRect().center();
133 129 text->setPos(x - center.x(), m_rect.bottom() + LABEL_PADDING);
134 130 //text->rotate(-45);
135 131 m_labels<<text;
136 132 }
137 133 }
138 134
139 135 if(m_type==Y_AXIS) {
140 136
141 137 const qreal deltaY = m_rect.height()/ m_ticks;
142 138
143 139 for (int j = 0; j <= m_ticks; ++j) {
144 140
145 141 int y = j * -deltaY + m_rect.bottom();
146 142
147 //last grid outside chart rect
148 if(j==0) y++;
149 if(j==m_ticks) y--;
150
151 143 qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY()
152 144 / m_ticks);
153 145
154 m_grid<<new QGraphicsLineItem(m_rect.left()- 1 - LABEL_PADDING , y, m_rect.right()+1, y,this);
146 m_grid<<new QGraphicsLineItem(m_rect.left() , y, m_rect.right(), y,this);
155 147 QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this);
156 148 QPointF center = text->boundingRect().center();
157 149
158 150 text->setPos(m_rect.left() - text->boundingRect().width() - LABEL_PADDING , y-center.y());
159 151 //text->rotate(-45);
160 152 m_labels<<text;
161 153
162 154 }
163 155 }
164 156
165 157 //painter->drawRect(m_rect.adjusted(0, 0, -1, -1));
166 158 }
167 159
168 160 //TODO "nice numbers algorithm"
169 161
170 162 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,366 +1,392
1 1 #include "qchart.h"
2 2 #include "qchartseries.h"
3 3 #include "qscatterseries.h"
4 4 #include "qscatterseries_p.h"
5 5 #include "qpieseries.h"
6 6 #include "qxychartseries.h"
7 7 #include "qchartaxis.h"
8 8 #include "barchartseries.h"
9 9 #include "bargroup.h"
10 10
11 11 #include "xylinechartitem_p.h"
12 12 #include "plotdomain_p.h"
13 13 #include "axisitem_p.h"
14 14 #include <QGraphicsScene>
15 15 #include <QDebug>
16 16
17 17 QTCOMMERCIALCHART_BEGIN_NAMESPACE
18 18
19 19 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
20 m_background(new QGraphicsRectItem()),
21 m_title(new QGraphicsTextItem(this)),
20 m_background(0),
21 m_title(0),
22 22 m_axisX(new AxisItem(AxisItem::X_AXIS,this)),
23 23 m_plotDataIndex(0),
24 24 m_marginSize(0)
25 25 {
26 26 // TODO: the default theme?
27 27 //setTheme(QChart::ChartThemeVanilla);
28 m_backgroundGradient.setColorAt(0.0, Qt::white);
29 28
30 29 PlotDomain domain;
31 30 m_plotDomainList<<domain;
32 31 m_axisY << new AxisItem(AxisItem::Y_AXIS,this);
33 32 m_chartItems<<m_axisX;
34 33 m_chartItems<<m_axisY.at(0);
35 34 }
36 35
37 36 QChart::~QChart(){}
38 37
39 38 QRectF QChart::boundingRect() const
40 39 {
41 40 return m_rect;
42 41 }
43 42
44 43 void QChart::addSeries(QChartSeries* series)
45 44 {
46 45 // TODO: we should check the series not already added
47 46
48 47 m_chartSeries << series;
49 48
50 49 switch(series->type())
51 50 {
52 51 case QChartSeries::SeriesTypeLine: {
53 52
54 53 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
55 54 // Use color defined by theme in case the series does not define a custom color
56 55
57 56 if (!xyseries->pen().color().isValid() && m_themeColors.count()) //TODO: wtf
58 57 xyseries->setPen(nextColor());
59 58
60 59 m_plotDataIndex = 0 ;
61 60 m_plotDomainList.resize(1);
62 61
63 62 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
64 63
65 64 for (int i = 0 ; i < xyseries->count() ; i++)
66 65 {
67 66 qreal x = xyseries->x(i);
68 67 qreal y = xyseries->y(i);
69 68 domain.m_minX = qMin(domain.m_minX,x);
70 69 domain.m_minY = qMin(domain.m_minY,y);
71 70 domain.m_maxX = qMax(domain.m_maxX,x);
72 71 domain.m_maxY = qMax(domain.m_maxY,y);
73 72 }
74 73
75 74 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
76 75 m_chartItems<<item;
77 76
78 77 foreach(ChartItem* i ,m_chartItems)
79 78 i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
80 79
81 80 break;
82 81 }
83 82 case QChartSeries::SeriesTypeBar: {
84 83
85 84 qDebug() << "barSeries added";
86 85 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
87 86 BarGroup* barGroup = new BarGroup(*barSeries,this);
88 87
89 88 // Add some fugly colors for 5 fist series...
90 89 barGroup->addColor(QColor(255,0,0,128));
91 90 barGroup->addColor(QColor(255,255,0,128));
92 91 barGroup->addColor(QColor(0,255,0,128));
93 92 barGroup->addColor(QColor(0,0,255,128));
94 93 barGroup->addColor(QColor(255,128,0,128));
95 94
96 95 m_chartItems<<barGroup;
97 96 childItems().append(barGroup);
98 97 break;
99 98 }
100 99 case QChartSeries::SeriesTypeScatter: {
101 100 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
102 101 scatterSeries->d->setParentItem(this);
103 102 // Set pre-defined colors in case the series has no colors defined
104 103 if (!scatterSeries->markerColor().isValid())
105 104 scatterSeries->setMarkerColor(nextColor());
106 105 connect(this, SIGNAL(sizeChanged(QRectF)),
107 106 scatterSeries, SLOT(chartSizeChanged(QRectF)));
108 107 // QColor nextColor = m_themeColors.takeFirst();
109 108 // nextColor.setAlpha(150); // TODO: default opacity?
110 109 // scatterSeries->setMarkerColor(nextColor);
111 110 break;
112 111 }
113 112 case QChartSeries::SeriesTypePie: {
114 113 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
115 114 for (int i(0); i < pieSeries->sliceCount(); i++) {
116 115 if (!pieSeries->sliceColor(i).isValid())
117 116 pieSeries->setSliceColor(i, nextColor());
118 117 }
119 118 connect(this, SIGNAL(sizeChanged(QRectF)),
120 119 pieSeries, SLOT(chartSizeChanged(QRectF)));
121 120
122 121 // Set pre-defined colors in case the series has no colors defined
123 122 // TODO: how to define the color for all the slices of a pie?
124 123 // for (int (i); i < pieSeries.sliceCount(); i++)
125 124 break;
126 125 }
127 126 }
128 127 }
129 128
130 129 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
131 130 {
132 131 // TODO: support also other types; not only scatter and pie
133 132
134 133 QChartSeries *series(0);
135 134
136 135 switch (type) {
137 136 case QChartSeries::SeriesTypeLine: {
138 137 series = QXYChartSeries::create();
139 138 break;
140 139 }
141 140 case QChartSeries::SeriesTypeBar: {
142 141 series = new BarChartSeries(this);
143 142 break;
144 143 }
145 144 case QChartSeries::SeriesTypeScatter: {
146 145 series = new QScatterSeries(this);
147 146 break;
148 147 }
149 148 case QChartSeries::SeriesTypePie: {
150 149 series = new QPieSeries(this);
151 150 break;
152 151 }
153 152 default:
154 153 Q_ASSERT(false);
155 154 break;
156 155 }
157 156
158 157 addSeries(series);
159 158 return series;
160 159 }
161 160
162 161 void QChart::setSize(const QSize& size)
163 162 {
164 163 m_rect = QRect(QPoint(0,0),size);
165 164 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
166 165
166 //recaculate title
167 if(m_title){
168
169 }
167 170
168 171 //recalculate background gradient
169 m_background->setRect(rect);
170 m_backgroundGradient.setFinalStop(0,m_background->rect().height());
171 m_background->setBrush(m_backgroundGradient);
172 m_background->setPen(Qt::NoPen);
172 if(m_background){
173 m_background->setRect(rect);
174 if(m_bacgroundOrinetation==HorizonatlGradientOrientation)
175 m_backgroundGradient.setFinalStop(m_background->rect().width(),0);
176 else
177 m_backgroundGradient.setFinalStop(0,m_background->rect().height());
178
179 m_background->setBrush(m_backgroundGradient);
180 }
173 181
174 182 //resize elements
175 183 foreach (ChartItem* item ,m_chartItems) {
176 184 item->setPos(rect.topLeft());
177 185 item->setSize(rect.size());
178 186
179 187 }
180 188 // TODO: TTD for setting scale
181 189 //emit scaleChanged(100, 100);
182 190 // TODO: calculate the origo
183 191 // TODO: not sure if emitting a signal here is the best from performance point of view
184 192 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
185 193
186 194 update();
187 195 }
188 196
189 void QChart::setBackgroundColor(const QColor& color)
197 void QChart::setBackground(const QColor& startColor, const QColor& endColor, GradientOrientation orientation)
190 198 {
191 m_backgroundGradient.setColorAt( 0.0, Qt::white);
192 m_backgroundGradient.setColorAt( 1.0, color);
199
200 if(!m_background){
201 m_background = new QGraphicsRectItem(this);
202 m_background->setZValue(-1);
203 }
204
205 m_bacgroundOrinetation = orientation;
206 m_backgroundGradient.setColorAt( 0.0, startColor);
207 m_backgroundGradient.setColorAt( 1.0, endColor);
208 m_backgroundGradient.setStart(0,0);
209
210 if(orientation == VerticalGradientOrientation){
211 m_backgroundGradient.setFinalStop(0,m_rect.height());
212 }else{
213 m_backgroundGradient.setFinalStop(m_rect.width(),0);
214 }
215
193 216 m_background->setBrush(m_backgroundGradient);
194 217 m_background->setPen(Qt::NoPen);
195 218 m_background->update();
196 219 }
197 220
198 221 void QChart::setTitle(const QString& title)
199 222 {
223 if(!m_title) m_title = new QGraphicsTextItem(this);
200 224 m_title->setPlainText(title);
201 225 }
202 226
203 227 int QChart::margin() const
204 228 {
205 229 return m_marginSize;
206 230 }
207 231
208 232 void QChart::setMargin(int margin)
209 233 {
210 234 m_marginSize = margin;
211 235 }
212 236
213 237 void QChart::setTheme(QChart::ChartThemeId theme)
214 238 {
215 239 // if (theme != m_currentTheme) {
216 240 m_themeColors.clear();
217 241
218 242 // TODO: define color themes
219 243 switch (theme) {
220 244 case QChart::ChartThemeDefault:
221 245 // TODO: define the default theme based on the OS
222 246 // For now we just fallthrough to "vanilla"
223 247 case QChart::ChartThemeVanilla:
224 248 m_themeColors.append(QColor(217, 197, 116));
225 249 m_themeColors.append(QColor(214, 168, 150));
226 250 m_themeColors.append(QColor(160, 160, 113));
227 251 m_themeColors.append(QColor(210, 210, 52));
228 252 m_themeColors.append(QColor(136, 114, 58));
229 253
230 254 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xff9d844d)));
231 255 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf)));
232 256 break;
233 257 case QChart::ChartThemeIcy:
234 258 m_themeColors.append(QColor(0, 3, 165));
235 259 m_themeColors.append(QColor(49, 52, 123));
236 260 m_themeColors.append(QColor(71, 114, 187));
237 261 m_themeColors.append(QColor(48, 97, 87));
238 262 m_themeColors.append(QColor(19, 71, 90));
239 263 m_themeColors.append(QColor(110, 70, 228));
240 264
241 265 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xffe4ffff)));
242 266 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffe4ffff)));
243 267 break;
244 268 case QChart::ChartThemeGrayscale:
245 269 m_themeColors.append(QColor(0, 0, 0));
246 270 m_themeColors.append(QColor(50, 50, 50));
247 271 m_themeColors.append(QColor(100, 100, 100));
248 272 m_themeColors.append(QColor(140, 140, 140));
249 273 m_themeColors.append(QColor(180, 180, 180));
250 274
251 275 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xffffffff)));
252 276 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf)));
253 277 break;
254 278 case QChart::ChartThemeUnnamed1:
255 279 m_themeColors.append(QColor(QRgb(0xff3fa9f5)));
256 280 m_themeColors.append(QColor(QRgb(0xff7AC943)));
257 281 m_themeColors.append(QColor(QRgb(0xffFF931E)));
258 282 m_themeColors.append(QColor(QRgb(0xffFF1D25)));
259 283 m_themeColors.append(QColor(QRgb(0xffFF7BAC)));
260 284
261 285 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xfff3dc9e)));
262 286 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf)));
263 287 break;
264 288 default:
265 289 Q_ASSERT(false);
266 290 break;
267 291 }
268 292
293 if(m_background){
269 294 m_background->setBrush(m_backgroundGradient);
270 295 m_background->setPen(Qt::NoPen);
296 }
271 297
272 298 foreach(QChartSeries* series, m_chartSeries) {
273 299 // TODO: other series interested on themes?
274 300 if (series->type() == QChartSeries::SeriesTypeLine) {
275 301 QXYChartSeries *lineseries = reinterpret_cast<QXYChartSeries *>(series);
276 302 lineseries->setPen(nextColor());
277 303 } else if (series->type() == QChartSeries::SeriesTypeScatter) {
278 304 QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series);
279 305 scatter->setMarkerColor(nextColor());
280 306 } else if (series->type() == QChartSeries::SeriesTypePie) {
281 307 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
282 308 for (int i(0); i < pieSeries->sliceCount(); i++)
283 309 pieSeries->setSliceColor(i, nextColor());
284 310 }
285 311 }
286 312 update();
287 313 }
288 314
289 315 QColor QChart::nextColor()
290 316 {
291 317 QColor nextColor = m_themeColors.first();
292 318 m_themeColors.move(0, m_themeColors.size() - 1);
293 319 return nextColor;
294 320 }
295 321
296 322 void QChart::zoomInToRect(const QRect& rectangle)
297 323 {
298 324
299 325 if(!rectangle.isValid()) return;
300 326
301 327 qreal margin = this->margin();
302 328
303 329 QRect rect = rectangle.normalized();
304 330 rect.translate(-margin, -margin);
305 331
306 332 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
307 333
308 334 PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin);
309 335
310 336 m_plotDomainList.resize(m_plotDataIndex + 1);
311 337 m_plotDomainList<<domain;
312 338 m_plotDataIndex++;
313 339
314 340 foreach (ChartItem* item ,m_chartItems)
315 341 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
316 342 update();
317 343 }
318 344
319 345 void QChart::zoomIn()
320 346 {
321 347 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
322 348 m_plotDataIndex++;
323 349 foreach (ChartItem* item ,m_chartItems)
324 350 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
325 351 update();
326 352 }else{
327 353 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
328 354 rect.setWidth(rect.width()/2);
329 355 rect.setHeight(rect.height()/2);
330 356 rect.moveCenter(m_rect.center());
331 357 zoomInToRect(rect);
332 358 }
333 359 }
334 360
335 361 void QChart::zoomOut()
336 362 {
337 363 if (m_plotDataIndex > 0) {
338 364 m_plotDataIndex--;
339 365 foreach (ChartItem* item ,m_chartItems)
340 366 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
341 367 update();
342 368 }
343 369 }
344 370
345 371 void QChart::setAxisX(const QChartAxis& axis)
346 372 {
347 373 setAxis(m_axisX,axis);
348 374 }
349 375 void QChart::setAxisY(const QChartAxis& axis)
350 376 {
351 377 setAxis(m_axisY.at(0),axis);
352 378 }
353 379
354 380 void QChart::setAxisY(const QList<QChartAxis>& axis)
355 381 {
356 382 //TODO not implemented
357 383 }
358 384
359 385 void QChart::setAxis(AxisItem *item, const QChartAxis& axis)
360 386 {
361 387 item->setVisible(axis.isAxisVisible());
362 388 }
363 389
364 390 #include "moc_qchart.cpp"
365 391
366 392 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,97 +1,102
1 1 #ifndef CHART_H
2 2 #define CHART_H
3 3
4 4 #include <qchartglobal.h>
5 5 #include <qchartseries.h>
6 6 #include <QGraphicsObject>
7 7 #include <QLinearGradient>
8 8
9 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 11 class AxisItem;
12 12 class QChartSeries;
13 13 class PlotDomain;
14 14 class ChartItem;
15 15 class BarGroup;
16 16 class QChartAxis;
17 17
18 18 // TODO: We don't need to have QChart tied to QGraphicsItem:
19 19 //class QTCOMMERCIALCHART_EXPORT QChart
20 20 //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem {
21 21 // public: QChartGraphicsItem(QChart &chart);
22 22
23 23 /*!
24 24 * TODO: define the responsibilities
25 25 */
26 26 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject
27 27 {
28 28 Q_OBJECT
29 29 public:
30 enum GradientOrientation {
31 HorizonatlGradientOrientation,
32 VerticalGradientOrientation
33 };
30 34 enum ChartThemeId {
31 35 /*! The default theme follows the GUI style of the Operating System */
32 36 ChartThemeDefault = 0,
33 37 ChartThemeVanilla,
34 38 ChartThemeIcy,
35 39 ChartThemeGrayscale,
36 40 //ChartThemeScientific,
37 41 ChartThemeUnnamed1
38 42 };
39 43
40 44 public:
41 45 QChart(QGraphicsObject* parent = 0);
42 46 ~QChart();
43 47
44 48 //from QGraphicsItem
45 49 QRectF boundingRect() const;
46 50 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
47 51
48 52 void addSeries(QChartSeries* series);
49 53 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
50 54 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
51 55 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
52 56
53 57 void setSize(const QSize& size);
54 58 void setMargin(int margin);
55 59 int margin() const;
56 60 void setTheme(QChart::ChartThemeId theme);
57 61
58 62 void setTitle(const QString& title);
59 void setBackgroundColor(const QColor& color);
63 void setBackground(const QColor& startColor, const QColor& endColor = Qt::white, GradientOrientation orientation = VerticalGradientOrientation);
60 64
61 65 void zoomInToRect(const QRect& rectangle);
62 66 void zoomIn();
63 67 void zoomOut();
64 68
65 69 void setAxisX(const QChartAxis& axis);
66 70 void setAxisY(const QChartAxis& axis);
67 71 void setAxisY(const QList<QChartAxis>& axis);
68 72
69 73 private:
70 74 void setAxis(AxisItem *item, const QChartAxis& axis);
71 75
72 76 signals:
73 77 //TODO chage to const QSize& size
74 78 void sizeChanged(QRectF rect);
75 79 void scaleChanged(qreal xscale, qreal yscale);
76 80
77 81 private:
78 82 QColor nextColor();
79 83
80 84 Q_DISABLE_COPY(QChart)
81 85 QGraphicsRectItem* m_background;
82 86 QLinearGradient m_backgroundGradient;
87 GradientOrientation m_bacgroundOrinetation;
83 88 QGraphicsTextItem* m_title;
84 89 AxisItem* m_axisX;
85 90 QList<AxisItem*> m_axisY;
86 91 QRect m_rect;
87 92 QList<QChartSeries*> m_chartSeries;
88 93 QVector<PlotDomain> m_plotDomainList;
89 94 QList<ChartItem*> m_chartItems;
90 95 int m_plotDataIndex;
91 96 int m_marginSize;
92 97 QList<QColor> m_themeColors;
93 98 };
94 99
95 100 QTCOMMERCIALCHART_END_NAMESPACE
96 101
97 102 #endif
@@ -1,77 +1,77
1 1 #include "qchartview.h"
2 2 #include "qchart.h"
3 3 #include <QGraphicsView>
4 4 #include <QGraphicsScene>
5 5 #include <QRubberBand>
6 6 #include <QResizeEvent>
7 7 #include <QDebug>
8 8
9 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 11 QChartView::QChartView(QWidget *parent) :
12 12 QGraphicsView(parent),
13 13 m_scene(new QGraphicsScene()),
14 14 m_chart(new QChart())
15 15 {
16 16 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
17 17 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
18 18 setScene(m_scene);
19 19 m_chart->setMargin(50);
20 20 m_scene->addItem(m_chart);
21 21 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
22 22 }
23 23
24 24 QChartView::~QChartView()
25 25 {
26 26 }
27 27
28 28 void QChartView::resizeEvent(QResizeEvent *event)
29 29 {
30 30 m_scene->setSceneRect(0,0,size().width(),size().height());
31 31 m_chart->setSize(size());
32 32 QWidget::resizeEvent(event);
33 33 }
34 34
35 35
36 36 void QChartView::addSeries(QChartSeries* series)
37 37 {
38 38 m_chart->addSeries(series);
39 39 }
40 40
41 41 QChartSeries* QChartView::createSeries(QChartSeries::QChartSeriesType type)
42 42 {
43 43
44 44 return m_chart->createSeries(type);
45 45 }
46 46
47 47 void QChartView::zoomInToRect(const QRect& rectangle)
48 48 {
49 49 m_chart->zoomInToRect(rectangle);
50 50 }
51 51
52 52 void QChartView::zoomIn()
53 53 {
54 54 m_chart->zoomIn();
55 55 }
56 56
57 57 void QChartView::zoomOut()
58 58 {
59 59 m_chart->zoomOut();
60 60 }
61 61
62 62 int QChartView::margin() const
63 63 {
64 64 return m_chart->margin();
65 65 }
66 66
67 67 void QChartView::setTitle(const QString& title)
68 68 {
69 69 m_chart->setTitle(title);
70 70 }
71 71
72 void QChartView::setBackgroundColor(const QColor& color)
72 void QChartView::setBackground(const QColor& startColor, const QColor& endColor, QChart::GradientOrientation orientation)
73 73 {
74 m_chart->setBackgroundColor(color);
74 m_chart->setBackground(startColor,endColor,orientation);
75 75 }
76 76
77 77 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,45 +1,46
1 1 #ifndef QCHARTWIDGET_H
2 2 #define QCHARTWIDGET_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include "qchartseries.h"
6 #include "qchart.h"
6 7 #include <QGraphicsView>
7 8
8 9 class QGraphicsScene;
9 10
10 11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 12
12 13 class QChart;
13 14
14 15 class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView
15 16 {
16 17 public:
17 18 explicit QChartView(QWidget *parent = 0);
18 19 ~QChartView();
19 20
20 21 //implement from QWidget
21 22 void resizeEvent(QResizeEvent *event);
22 23
23 24 void addSeries(QChartSeries* series);
24 25 // Convenience function
25 26 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
26 27
27 28 int margin() const;
28 29 void setTitle(const QString& title);
29 void setBackgroundColor(const QColor& color);
30 void setBackground(const QColor& startColor, const QColor& endColor = Qt::white, QChart::GradientOrientation orientation = QChart::VerticalGradientOrientation);
30 31 void zoomInToRect(const QRect& rectangle);
31 32 void zoomIn();
32 33 void zoomOut();
33 34
34 35 private:
35 36 QGraphicsScene *m_scene;
36 37 QChart* m_chart;
37 38 QPoint m_origin;
38 39 Q_DISABLE_COPY(QChartView)
39 40
40 41
41 42 };
42 43
43 44 QTCOMMERCIALCHART_END_NAMESPACE
44 45
45 46 #endif // QCHARTWIDGET_H
@@ -1,72 +1,68
1 1 #include "qxychartseries.h"
2 2
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 5 QXYChartSeries::QXYChartSeries(QObject* parent):QChartSeries(parent)
6 6 {
7 7 }
8 8
9 9 QXYChartSeries::~QXYChartSeries()
10 10 {
11 11 }
12 12
13 13 QXYChartSeries* QXYChartSeries::create(QObject* parent)
14 14 {
15 15 //TODO: here we take QChartData when it is ready
16 16 // return null if malformed;
17 17 return new QXYChartSeries(parent);
18 18 }
19 19
20 20 void QXYChartSeries::add(qreal x,qreal y)
21 21 {
22 22 m_x<<x;
23 23 m_y<<y;
24 24 }
25 25
26 26 void QXYChartSeries::clear()
27 27 {
28 28 m_x.clear();
29 29 m_y.clear();
30 30 }
31 31
32 32 qreal QXYChartSeries::x(int pos) const
33 33 {
34 34 return m_x.at(pos);
35 35 }
36 36
37 37 qreal QXYChartSeries::y(int pos) const
38 38 {
39 39 return m_y.at(pos);
40 40 }
41 41
42 42 int QXYChartSeries::count() const
43 43 {
44 44 Q_ASSERT(m_x.size() == m_y.size());
45 45
46 46 return m_x.size();
47 47
48 48 }
49 49
50 50 void QXYChartSeries::setPen(const QPen& pen)
51 51 {
52 52 m_pen=pen;
53 53 }
54 54
55 void QXYChartSeries::setBrush(const QBrush& brush)
56 {
57 m_brush=brush;
58 }
59 55
60 56 QDebug operator<< (QDebug debug, const QXYChartSeries series)
61 57 {
62 58 Q_ASSERT(series.m_x.size() == series.m_y.size());
63 59
64 60 int size = series.m_x.size();
65 61
66 62 for (int i=0;i<size;i++) {
67 63 debug.nospace() << "(" << series.m_x.at(i) << ','<< series.m_y.at(i) << ") ";
68 64 }
69 65 return debug.space();
70 66 }
71 67
72 68 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,48 +1,45
1 1 #ifndef QXYSERIES_H_
2 2 #define QXYSERIES_H_
3 3
4 4 #include "qchartglobal.h"
5 5 #include "qchartseries.h"
6 6 #include <QDebug>
7 7 #include <QPen>
8 8 #include <QBrush>
9 9
10 10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 11
12 12 class QTCOMMERCIALCHART_EXPORT QXYChartSeries : public QChartSeries
13 13 {
14 14 //TODO:
15 15 // Q_OBJECT
16 16 private:
17 17 QXYChartSeries(QObject* parent=0);
18 18 public:
19 19 virtual ~QXYChartSeries();
20 20
21 21 //implemented from QChartSeries
22 22 static QXYChartSeries* create(QObject* parent=0);
23 23 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeLine;}
24 24
25 25 void add(qreal x, qreal y);
26 26 void clear();
27 27
28 28 void setPen(const QPen& pen);
29 29 const QPen& pen() const { return m_pen;}
30 void setBrush(const QBrush& brush);
31 const QBrush& brush() const { return m_brush;}
32 30
33 31 int count() const;
34 32 qreal x(int pos) const;
35 33 qreal y(int pos) const;
36 34 friend QDebug operator<< (QDebug d, const QXYChartSeries series);
37 35
38 36 private:
39 37 QList<qreal> m_x;
40 38 QList<qreal> m_y;
41 QBrush m_brush;
42 39 QPen m_pen;
43 40
44 41 };
45 42
46 43 QTCOMMERCIALCHART_END_NAMESPACE
47 44
48 45 #endif
@@ -1,67 +1,67
1 1 #include "xylinechartitem_p.h"
2 2 #include "axisitem_p.h"
3 3 #include "qxychartseries.h"
4 4 #include <QPainter>
5 5 #include <QStyleOptionGraphicsItem>
6 6 #include <QDebug>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 XYLineChartItem::XYLineChartItem(QXYChartSeries* series,QGraphicsItem *parent):ChartItem(parent),
11 11 m_series(series),
12 12 m_pathItem(new QGraphicsPathItem(this))
13 13 {
14 14 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
15 15 }
16 16
17 17 void XYLineChartItem::setSize(const QSize& size)
18 18 {
19 19 m_rect=QRect(0,0,size.width(),size.height());
20 20 prepareGeometryChange();
21 21 updateGeometry();
22 22
23 23 }
24 24
25 25 void XYLineChartItem::setPlotDomain(const PlotDomain& data)
26 26 {
27 27 m_plotDomain=data;
28 28 prepareGeometryChange();
29 29 updateGeometry();
30 30
31 31 }
32 32
33 33 QRectF XYLineChartItem::boundingRect() const
34 34 {
35 35 return m_rect;
36 36 }
37 37 /*
38 38 QPainterPath XYLineChartItem::shape() const
39 39 {
40 40 return m_pathItem->shape();
41 41 }
42 42 */
43 43 void XYLineChartItem::updateGeometry()
44 44 {
45 45 if (!m_rect.isValid()) return;
46 46
47 47 const qreal deltaX = m_rect.width()/m_plotDomain.spanX();
48 48 const qreal deltaY = m_rect.height()/m_plotDomain.spanY();
49 49
50 50 QPainterPath path;
51 51
52 52 for (int j = 0; j < m_series->count(); ++j) {
53 53 qreal dx = m_series->x(j) - m_plotDomain.m_minX;
54 54 qreal dy = m_series->y(j) - m_plotDomain.m_minY;
55 55 qreal x = (dx * deltaX) + m_rect.left();
56 56 qreal y = - (dy * deltaY) + m_rect.bottom();
57 57 if(j==0) path.moveTo(x,y);
58 58 else path.lineTo(x,y);
59 59 }
60 60
61 61 m_pathItem->setPath(path);
62 62 m_pathItem->setPen(m_series->pen());
63 m_pathItem->setBrush(m_series->brush());
63 m_pathItem->setBrush(Qt::NoBrush);
64 64 }
65 65
66 66
67 67 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now