##// END OF EJS Templates
Add background to chart...
Michal Klocek -
r69:c7c3c4960b21
parent child
Show More
@@ -0,0 +1,15
1 !include( ../../common.pri ) {
2 error( "Couldn't find the common.pri file!" )
3 }
4
5 !include( ../../integrated.pri ) {
6 error( "Couldn't find the integrated.pri file !")
7 }
8
9 TARGET = colorlineChart
10 TEMPLATE = app
11 QT += core gui
12 SOURCES += main.cpp
13
14
15
@@ -0,0 +1,40
1 #include <QApplication>
2 #include <QMainWindow>
3 #include <qchartview.h>
4 #include <qxychartseries.h>
5 #include <qchart.h>
6 #include <cmath>
7
8 QTCOMMERCIALCHART_USE_NAMESPACE
9
10 #define PI 3.14159265358979
11
12 int main(int argc, char *argv[])
13 {
14 QApplication a(argc, argv);
15
16 QMainWindow window;
17
18 QXYChartSeries* series0 = QXYChartSeries::create();
19 series0->setColor(Qt::blue);
20 QXYChartSeries* series1 = QXYChartSeries::create();
21 series1->setColor(Qt::red);
22
23 int numPoints = 100;
24
25 for (int x = 0; x <= numPoints; ++x) {
26 series0->add(x, abs(sin(PI/50*x)*100));
27 series1->add(x, abs(cos(PI/50*x)*100));
28 }
29
30 QChartView* chartView = new QChartView(&window);
31 chartView->addSeries(series0);
32 chartView->addSeries(series1);
33 chartView->setBackgroundColor(Qt::yellow);
34
35 window.setCentralWidget(chartView);
36 window.resize(400, 300);
37 window.show();
38
39 return a.exec();
40 }
@@ -1,3 +1,4
1 TEMPLATE = subdirs
1 TEMPLATE = subdirs
2 SUBDIRS += linechart \
2 SUBDIRS += linechart \
3 zoomlinechart
3 zoomlinechart \
4 colorlinechart
@@ -1,264 +1,287
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qchartseries.h"
2 #include "qchartseries.h"
3 #include "qscatterseries.h"
3 #include "qscatterseries.h"
4 #include "qscatterseries_p.h"
4 #include "qscatterseries_p.h"
5 #include "qpieseries.h"
5 #include "qpieseries.h"
6 #include "qxychartseries.h"
6 #include "qxychartseries.h"
7
7
8 #include "barchartseries.h"
8 #include "barchartseries.h"
9 #include "bargroup.h"
9 #include "bargroup.h"
10
10
11 #include "xylinechartitem_p.h"
11 #include "xylinechartitem_p.h"
12 #include "plotdomain_p.h"
12 #include "plotdomain_p.h"
13 #include "axisitem_p.h"
13 #include "axisitem_p.h"
14 #include <QGraphicsScene>
14 #include <QGraphicsScene>
15 #include <QDebug>
15 #include <QDebug>
16
16
17 QTCOMMERCIALCHART_BEGIN_NAMESPACE
17 QTCOMMERCIALCHART_BEGIN_NAMESPACE
18
18
19 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
19 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
20 m_background(new QGraphicsRectItem(this)),
21 m_title(new QGraphicsTextItem(this)),
20 m_axisX(new AxisItem(AxisItem::X_AXIS,this)),
22 m_axisX(new AxisItem(AxisItem::X_AXIS,this)),
21 m_axisY(new AxisItem(AxisItem::Y_AXIS,this)),
23 m_axisY(new AxisItem(AxisItem::Y_AXIS,this)),
22 m_plotDataIndex(0),
24 m_plotDataIndex(0),
23 m_marginSize(0)
25 m_marginSize(0)
24 {
26 {
25 // TODO: the default theme?
27 // TODO: the default theme?
26 setTheme(QChart::ChartThemeVanilla);
28 setTheme(QChart::ChartThemeVanilla);
27 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
29 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
28 PlotDomain domain;
30 PlotDomain domain;
29 m_plotDomainList<<domain;
31 m_plotDomainList<<domain;
30
32
31 m_chartItems<<m_axisX;
33 m_chartItems<<m_axisX;
32 m_chartItems<<m_axisY;
34 m_chartItems<<m_axisY;
33 }
35 }
34
36
35 QChart::~QChart(){}
37 QChart::~QChart(){}
36
38
37 QRectF QChart::boundingRect() const
39 QRectF QChart::boundingRect() const
38 {
40 {
39 return m_rect;
41 return m_rect;
40 }
42 }
41
43
42 void QChart::addSeries(QChartSeries* series)
44 void QChart::addSeries(QChartSeries* series)
43 {
45 {
44 // TODO: we should check the series not already added
46 // TODO: we should check the series not already added
45
47
46 m_series<<series;
48 m_series<<series;
47
49
48 switch(series->type())
50 switch(series->type())
49 {
51 {
50 case QChartSeries::SeriesTypeLine: {
52 case QChartSeries::SeriesTypeLine: {
51
53
52 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
54 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
53 // Use color defined by theme in case the series does not define a custom color
55 // Use color defined by theme in case the series does not define a custom color
54 if (!xyseries->color().isValid() && m_themeColors.count())
56 if (!xyseries->color().isValid() && m_themeColors.count())
55 xyseries->setColor(m_themeColors.takeFirst());
57 xyseries->setColor(m_themeColors.takeFirst());
56
58
57 m_plotDataIndex = 0 ;
59 m_plotDataIndex = 0 ;
58 m_plotDomainList.resize(1);
60 m_plotDomainList.resize(1);
59
61
60 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
62 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
61
63
62 for (int i = 0 ; i < xyseries->count() ; i++)
64 for (int i = 0 ; i < xyseries->count() ; i++)
63 {
65 {
64 qreal x = xyseries->x(i);
66 qreal x = xyseries->x(i);
65 qreal y = xyseries->y(i);
67 qreal y = xyseries->y(i);
66 domain.m_minX = qMin(domain.m_minX,x);
68 domain.m_minX = qMin(domain.m_minX,x);
67 domain.m_minY = qMin(domain.m_minY,y);
69 domain.m_minY = qMin(domain.m_minY,y);
68 domain.m_maxX = qMax(domain.m_maxX,x);
70 domain.m_maxX = qMax(domain.m_maxX,x);
69 domain.m_maxY = qMax(domain.m_maxY,y);
71 domain.m_maxY = qMax(domain.m_maxY,y);
70 }
72 }
71
73
72 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
74 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
73
75
74 m_chartItems<<item;
76 m_chartItems<<item;
75
77
76 foreach(ChartItem* i ,m_chartItems)
78 foreach(ChartItem* i ,m_chartItems)
77 i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
79 i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
78
80
79 break;
81 break;
80 }
82 }
81 case QChartSeries::SeriesTypeBar: {
83 case QChartSeries::SeriesTypeBar: {
82
84
83 qDebug() << "barSeries added";
85 qDebug() << "barSeries added";
84 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
86 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
85
87
86 // Who owns the series?
88 // Who owns the series?
87 BarGroup* group = new BarGroup(*barSeries, this);
89 BarGroup* group = new BarGroup(*barSeries, this);
88 scene()->addItem(group);
90 scene()->addItem(group);
89 m_BarGroupItems.append(group); // If we need to access group later
91 m_BarGroupItems.append(group); // If we need to access group later
90 break;
92 break;
91 }
93 }
92 case QChartSeries::SeriesTypeScatter: {
94 case QChartSeries::SeriesTypeScatter: {
93 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
95 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
94 connect(this, SIGNAL(sizeChanged(QRectF)),
96 connect(this, SIGNAL(sizeChanged(QRectF)),
95 scatterSeries, SLOT(chartSizeChanged(QRectF)));
97 scatterSeries, SLOT(chartSizeChanged(QRectF)));
96 scatterSeries->d->setParentItem(this);
98 scatterSeries->d->setParentItem(this);
97 QColor nextColor = m_themeColors.takeFirst();
99 QColor nextColor = m_themeColors.takeFirst();
98 nextColor.setAlpha(150); // TODO: default opacity?
100 nextColor.setAlpha(150); // TODO: default opacity?
99 scatterSeries->setMarkerColor(nextColor);
101 scatterSeries->setMarkerColor(nextColor);
100 }
102 }
101 case QChartSeries::SeriesTypePie: {
103 case QChartSeries::SeriesTypePie: {
102 // TODO: we now have also a list of y values as a parameter, it is ignored
104 // TODO: we now have also a list of y values as a parameter, it is ignored
103 // we should use a generic data class instead of list of x and y values
105 // we should use a generic data class instead of list of x and y values
104 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
106 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
105 connect(this, SIGNAL(sizeChanged(QRectF)),
107 connect(this, SIGNAL(sizeChanged(QRectF)),
106 pieSeries, SLOT(chartSizeChanged(QRectF)));
108 pieSeries, SLOT(chartSizeChanged(QRectF)));
107 // TODO: how to define the color for all the slices of a pie?
109 // TODO: how to define the color for all the slices of a pie?
108 }
110 }
109 }
111 }
110 }
112 }
111
113
112 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
114 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
113 {
115 {
114 // TODO: support also other types; not only scatter and pie
116 // TODO: support also other types; not only scatter and pie
115
117
116 QChartSeries *series(0);
118 QChartSeries *series(0);
117
119
118 switch (type) {
120 switch (type) {
119 case QChartSeries::SeriesTypeLine: {
121 case QChartSeries::SeriesTypeLine: {
120 series = QXYChartSeries::create();
122 series = QXYChartSeries::create();
121 break;
123 break;
122 }
124 }
123 case QChartSeries::SeriesTypeBar: {
125 case QChartSeries::SeriesTypeBar: {
124 series = new BarChartSeries(this);
126 series = new BarChartSeries(this);
125 break;
127 break;
126 }
128 }
127 case QChartSeries::SeriesTypeScatter: {
129 case QChartSeries::SeriesTypeScatter: {
128 series = new QScatterSeries(this);
130 series = new QScatterSeries(this);
129 break;
131 break;
130 }
132 }
131 case QChartSeries::SeriesTypePie: {
133 case QChartSeries::SeriesTypePie: {
132 series = new QPieSeries(this);
134 series = new QPieSeries(this);
133 break;
135 break;
134 }
136 }
135 default:
137 default:
136 Q_ASSERT(false);
138 Q_ASSERT(false);
137 break;
139 break;
138 }
140 }
139
141
140 addSeries(series);
142 addSeries(series);
141 return series;
143 return series;
142 }
144 }
143
145
144 void QChart::setSize(const QSize& size)
146 void QChart::setSize(const QSize& size)
145 {
147 {
146 m_rect = QRect(QPoint(0,0),size);
148 m_rect = QRect(QPoint(0,0),size);
147 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
149 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
148
150
151
152 //recalculate background gradient
153 m_background->setRect(rect);
154 m_backgroundGradient.setFinalStop(0,m_background->rect().height());
155 m_background->setBrush(m_backgroundGradient);
156
157 //resize elements
149 foreach (ChartItem* item ,m_chartItems) {
158 foreach (ChartItem* item ,m_chartItems) {
150 item->setPos(rect.topLeft());
159 item->setPos(rect.topLeft());
151 item->setSize(rect.size());
160 item->setSize(rect.size());
152
161
153 }
162 }
154 // TODO: TTD for setting scale
163 // TODO: TTD for setting scale
155 //emit scaleChanged(100, 100);
164 //emit scaleChanged(100, 100);
156 // TODO: calculate the origo
165 // TODO: calculate the origo
157 // TODO: not sure if emitting a signal here is the best from performance point of view
166 // TODO: not sure if emitting a signal here is the best from performance point of view
158 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
167 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
159
168
160 update();
169 update();
161 }
170 }
162
171
172 void QChart::setBackgroundColor(const QColor& color)
173 {
174 m_backgroundGradient.setColorAt( 0.0, Qt::white);
175 m_backgroundGradient.setColorAt( 1.0, color);
176 m_background->setBrush(m_backgroundGradient);
177 m_background->setPen(Qt::NoPen);
178 m_background->update();
179 }
180
181 void QChart::setTitle(const QString& title)
182 {
183 m_title->setPlainText(title);
184 }
185
163 int QChart::margin() const
186 int QChart::margin() const
164 {
187 {
165 return m_marginSize;
188 return m_marginSize;
166 }
189 }
167
190
168 void QChart::setMargin(int margin)
191 void QChart::setMargin(int margin)
169 {
192 {
170 m_marginSize = margin;
193 m_marginSize = margin;
171 }
194 }
172
195
173 void QChart::setTheme(QChart::ChartTheme theme)
196 void QChart::setTheme(QChart::ChartTheme theme)
174 {
197 {
175 // TODO: define color themes
198 // TODO: define color themes
176 switch (theme) {
199 switch (theme) {
177 case ChartThemeVanilla:
200 case ChartThemeVanilla:
178 m_themeColors.append(QColor(255, 238, 174));
201 m_themeColors.append(QColor(255, 238, 174));
179 m_themeColors.append(QColor(228, 228, 160));
202 m_themeColors.append(QColor(228, 228, 160));
180 m_themeColors.append(QColor(228, 179, 160));
203 m_themeColors.append(QColor(228, 179, 160));
181 m_themeColors.append(QColor(180, 151, 18));
204 m_themeColors.append(QColor(180, 151, 18));
182 m_themeColors.append(QColor(252, 252, 37));
205 m_themeColors.append(QColor(252, 252, 37));
183 break;
206 break;
184 case ChartThemeIcy:
207 case ChartThemeIcy:
185 m_themeColors.append(QColor(255, 238, 174));
208 m_themeColors.append(QColor(255, 238, 174));
186 m_themeColors.append(QColor(228, 228, 160));
209 m_themeColors.append(QColor(228, 228, 160));
187 m_themeColors.append(QColor(228, 179, 160));
210 m_themeColors.append(QColor(228, 179, 160));
188 m_themeColors.append(QColor(180, 151, 18));
211 m_themeColors.append(QColor(180, 151, 18));
189 m_themeColors.append(QColor(252, 252, 37));
212 m_themeColors.append(QColor(252, 252, 37));
190 break;
213 break;
191 case ChartThemeGrayscale:
214 case ChartThemeGrayscale:
192 m_themeColors.append(QColor(255, 238, 174));
215 m_themeColors.append(QColor(255, 238, 174));
193 m_themeColors.append(QColor(228, 228, 160));
216 m_themeColors.append(QColor(228, 228, 160));
194 m_themeColors.append(QColor(228, 179, 160));
217 m_themeColors.append(QColor(228, 179, 160));
195 m_themeColors.append(QColor(180, 151, 18));
218 m_themeColors.append(QColor(180, 151, 18));
196 m_themeColors.append(QColor(252, 252, 37));
219 m_themeColors.append(QColor(252, 252, 37));
197 break;
220 break;
198 default:
221 default:
199 Q_ASSERT(false);
222 Q_ASSERT(false);
200 break;
223 break;
201 }
224 }
202
225
203 // TODO: update coloring of different elements to match the selected theme
226 // TODO: update coloring of different elements to match the selected theme
204 }
227 }
205
228
206 void QChart::zoomInToRect(const QRect& rectangle)
229 void QChart::zoomInToRect(const QRect& rectangle)
207 {
230 {
208
231
209 if(!rectangle.isValid()) return;
232 if(!rectangle.isValid()) return;
210
233
211 qreal margin = this->margin();
234 qreal margin = this->margin();
212
235
213 QRect rect = rectangle.normalized();
236 QRect rect = rectangle.normalized();
214 rect.translate(-margin, -margin);
237 rect.translate(-margin, -margin);
215
238
216 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
239 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
217 PlotDomain newDomain;
240 PlotDomain newDomain;
218
241
219 qreal dx = oldDomain.spanX() / (m_rect.width() - 2 * margin);
242 qreal dx = oldDomain.spanX() / (m_rect.width() - 2 * margin);
220 qreal dy = oldDomain.spanY() / (m_rect.height() - 2 * margin);
243 qreal dy = oldDomain.spanY() / (m_rect.height() - 2 * margin);
221
244
222 newDomain.m_minX = oldDomain.m_minX + dx * rect.left();
245 newDomain.m_minX = oldDomain.m_minX + dx * rect.left();
223 newDomain.m_maxX = oldDomain.m_minX + dx * rect.right();
246 newDomain.m_maxX = oldDomain.m_minX + dx * rect.right();
224 newDomain.m_minY = oldDomain.m_maxY - dy * rect.bottom();
247 newDomain.m_minY = oldDomain.m_maxY - dy * rect.bottom();
225 newDomain.m_maxY = oldDomain.m_maxY - dy * rect.top();
248 newDomain.m_maxY = oldDomain.m_maxY - dy * rect.top();
226
249
227 m_plotDomainList.resize(m_plotDataIndex + 1);
250 m_plotDomainList.resize(m_plotDataIndex + 1);
228 m_plotDomainList<<newDomain;
251 m_plotDomainList<<newDomain;
229 m_plotDataIndex++;
252 m_plotDataIndex++;
230
253
231 foreach (ChartItem* item ,m_chartItems)
254 foreach (ChartItem* item ,m_chartItems)
232 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
255 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
233 update();
256 update();
234 }
257 }
235
258
236 void QChart::zoomIn()
259 void QChart::zoomIn()
237 {
260 {
238 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
261 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
239 m_plotDataIndex++;
262 m_plotDataIndex++;
240 foreach (ChartItem* item ,m_chartItems)
263 foreach (ChartItem* item ,m_chartItems)
241 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
264 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
242 update();
265 update();
243 }else{
266 }else{
244 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
267 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
245 rect.setWidth(rect.width()/2);
268 rect.setWidth(rect.width()/2);
246 rect.setHeight(rect.height()/2);
269 rect.setHeight(rect.height()/2);
247 rect.moveCenter(m_rect.center());
270 rect.moveCenter(m_rect.center());
248 zoomInToRect(rect);
271 zoomInToRect(rect);
249 }
272 }
250 }
273 }
251
274
252 void QChart::zoomOut()
275 void QChart::zoomOut()
253 {
276 {
254 if (m_plotDataIndex > 0) {
277 if (m_plotDataIndex > 0) {
255 m_plotDataIndex--;
278 m_plotDataIndex--;
256 foreach (ChartItem* item ,m_chartItems)
279 foreach (ChartItem* item ,m_chartItems)
257 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
280 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
258 update();
281 update();
259 }
282 }
260 }
283 }
261
284
262 #include "moc_qchart.cpp"
285 #include "moc_qchart.cpp"
263
286
264 QTCOMMERCIALCHART_END_NAMESPACE
287 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,80 +1,86
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
8
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10
10 class AxisItem;
11 class AxisItem;
11 class QChartSeries;
12 class QChartSeries;
12 class PlotDomain;
13 class PlotDomain;
13 class ChartItem;
14 class ChartItem;
14 class BarGroup;
15 class BarGroup;
15
16
16 // TODO: We don't need to have QChart tied to QGraphicsItem:
17 // TODO: We don't need to have QChart tied to QGraphicsItem:
17 //class QTCOMMERCIALCHART_EXPORT QChart
18 //class QTCOMMERCIALCHART_EXPORT QChart
18 //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem {
19 //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem {
19 // public: QChartGraphicsItem(QChart &chart);
20 // public: QChartGraphicsItem(QChart &chart);
20
21
21 /*!
22 /*!
22 * TODO: define the responsibilities
23 * TODO: define the responsibilities
23 */
24 */
24 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject
25 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject
25 {
26 {
26 Q_OBJECT
27 Q_OBJECT
27 public:
28 public:
28 enum ChartTheme {
29 enum ChartTheme {
29 ChartThemeVanilla = 0,
30 ChartThemeVanilla = 0,
30 ChartThemeIcy,
31 ChartThemeIcy,
31 ChartThemeGrayscale
32 ChartThemeGrayscale
32 };
33 };
33
34
34 public:
35 public:
35 QChart(QGraphicsObject* parent = 0);
36 QChart(QGraphicsObject* parent = 0);
36 ~QChart();
37 ~QChart();
37
38
38 //from QGraphicsItem
39 //from QGraphicsItem
39 QRectF boundingRect() const;
40 QRectF boundingRect() const;
40 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
41 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
41
42
42 void addSeries(QChartSeries* series);
43 void addSeries(QChartSeries* series);
43 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
44 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
44 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
45 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
45 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
46 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
46
47
47 void setSize(const QSize& size);
48 void setSize(const QSize& size);
48 void setMargin(int margin);
49 void setMargin(int margin);
49 int margin() const;
50 int margin() const;
50 void setTheme(QChart::ChartTheme theme);
51 void setTheme(QChart::ChartTheme theme);
51
52
53 void setTitle(const QString& title);
54 void setBackgroundColor(const QColor& color);
55
52 void zoomInToRect(const QRect& rectangle);
56 void zoomInToRect(const QRect& rectangle);
53 void zoomIn();
57 void zoomIn();
54 void zoomOut();
58 void zoomOut();
55
59
56 signals:
60 signals:
57 //TODO chage to const QSize& size
61 //TODO chage to const QSize& size
58 void sizeChanged(QRectF rect);
62 void sizeChanged(QRectF rect);
59 void scaleChanged(qreal xscale, qreal yscale);
63 void scaleChanged(qreal xscale, qreal yscale);
60
64
61 private:
65 private:
62 Q_DISABLE_COPY(QChart)
66 Q_DISABLE_COPY(QChart)
67 QGraphicsRectItem* m_background;
68 QLinearGradient m_backgroundGradient;
69 QGraphicsTextItem* m_title;
63 AxisItem* m_axisX;
70 AxisItem* m_axisX;
64 AxisItem* m_axisY;
71 AxisItem* m_axisY;
65 QRect m_rect;
72 QRect m_rect;
66 QList<const QChartSeries*> m_series;
73 QList<const QChartSeries*> m_series;
67 QVector<PlotDomain> m_plotDomainList;
74 QVector<PlotDomain> m_plotDomainList;
68 QList<ChartItem*> m_chartItems;
75 QList<ChartItem*> m_chartItems;
69 //TODO: remove
76 //TODO: remove
70 QList<QGraphicsItem*> m_items;
77 QList<QGraphicsItem*> m_items;
71 int m_plotDataIndex;
78 int m_plotDataIndex;
72 int m_marginSize;
79 int m_marginSize;
73 QList<QColor> m_themeColors;
80 QList<QColor> m_themeColors;
74
75 QList<BarGroup*> m_BarGroupItems;
81 QList<BarGroup*> m_BarGroupItems;
76 };
82 };
77
83
78 QTCOMMERCIALCHART_END_NAMESPACE
84 QTCOMMERCIALCHART_END_NAMESPACE
79
85
80 #endif
86 #endif
@@ -1,66 +1,77
1 #include "qchartview.h"
1 #include "qchartview.h"
2 #include "qchart.h"
2 #include "qchart.h"
3 #include <QGraphicsView>
3 #include <QGraphicsView>
4 #include <QGraphicsScene>
4 #include <QGraphicsScene>
5 #include <QRubberBand>
5 #include <QRubberBand>
6 #include <QResizeEvent>
6 #include <QResizeEvent>
7 #include <QDebug>
7 #include <QDebug>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 QChartView::QChartView(QWidget *parent) :
11 QChartView::QChartView(QWidget *parent) :
12 QGraphicsView(parent),
12 QGraphicsView(parent),
13 m_scene(new QGraphicsScene()),
13 m_scene(new QGraphicsScene()),
14 m_chart(new QChart())
14 m_chart(new QChart())
15 {
15 {
16 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
16 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
17 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
17 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
18 setScene(m_scene);
18 setScene(m_scene);
19 m_chart->setMargin(50);
19 m_chart->setMargin(50);
20 m_scene->addItem(m_chart);
20 m_scene->addItem(m_chart);
21 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
21 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
22 }
22 }
23
23
24 QChartView::~QChartView()
24 QChartView::~QChartView()
25 {
25 {
26 }
26 }
27
27
28 void QChartView::resizeEvent(QResizeEvent *event)
28 void QChartView::resizeEvent(QResizeEvent *event)
29 {
29 {
30 m_scene->setSceneRect(0,0,size().width(),size().height());
30 m_scene->setSceneRect(0,0,size().width(),size().height());
31 m_chart->setSize(size());
31 m_chart->setSize(size());
32 QWidget::resizeEvent(event);
32 QWidget::resizeEvent(event);
33 }
33 }
34
34
35
35
36 void QChartView::addSeries(QChartSeries* series)
36 void QChartView::addSeries(QChartSeries* series)
37 {
37 {
38 m_chart->addSeries(series);
38 m_chart->addSeries(series);
39 }
39 }
40
40
41 QChartSeries* QChartView::createSeries(QChartSeries::QChartSeriesType type)
41 QChartSeries* QChartView::createSeries(QChartSeries::QChartSeriesType type)
42 {
42 {
43
43
44 return m_chart->createSeries(type);
44 return m_chart->createSeries(type);
45 }
45 }
46
46
47 void QChartView::zoomInToRect(const QRect& rectangle)
47 void QChartView::zoomInToRect(const QRect& rectangle)
48 {
48 {
49 m_chart->zoomInToRect(rectangle);
49 m_chart->zoomInToRect(rectangle);
50 }
50 }
51
51
52 void QChartView::zoomIn()
52 void QChartView::zoomIn()
53 {
53 {
54 m_chart->zoomIn();
54 m_chart->zoomIn();
55 }
55 }
56
56
57 void QChartView::zoomOut()
57 void QChartView::zoomOut()
58 {
58 {
59 m_chart->zoomOut();
59 m_chart->zoomOut();
60 }
60 }
61
61
62 int QChartView::margin() const
62 int QChartView::margin() const
63 {
63 {
64 return m_chart->margin();
64 return m_chart->margin();
65 }
65 }
66
67 void QChartView::setTitle(const QString& title)
68 {
69 m_chart->setTitle(title);
70 }
71
72 void QChartView::setBackgroundColor(const QColor& color)
73 {
74 m_chart->setBackgroundColor(color);
75 }
76
66 QTCOMMERCIALCHART_END_NAMESPACE
77 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,43 +1,45
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 "qchartseries.h"
5 #include "qchartseries.h"
6 #include <QGraphicsView>
6 #include <QGraphicsView>
7
7
8 class QGraphicsScene;
8 class QGraphicsScene;
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class QChart;
12 class QChart;
13
13
14 class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView
14 class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView
15 {
15 {
16 public:
16 public:
17 explicit QChartView(QWidget *parent = 0);
17 explicit QChartView(QWidget *parent = 0);
18 ~QChartView();
18 ~QChartView();
19
19
20 //implement from QWidget
20 //implement from QWidget
21 void resizeEvent(QResizeEvent *event);
21 void resizeEvent(QResizeEvent *event);
22
22
23 void addSeries(QChartSeries* series);
23 void addSeries(QChartSeries* series);
24 // Convenience function
24 // Convenience function
25 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
25 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
26
26
27 int margin() const;
27 int margin() const;
28 void setTitle(const QString& title);
29 void setBackgroundColor(const QColor& color);
28 void zoomInToRect(const QRect& rectangle);
30 void zoomInToRect(const QRect& rectangle);
29 void zoomIn();
31 void zoomIn();
30 void zoomOut();
32 void zoomOut();
31
33
32 private:
34 private:
33 QGraphicsScene *m_scene;
35 QGraphicsScene *m_scene;
34 QChart* m_chart;
36 QChart* m_chart;
35 QPoint m_origin;
37 QPoint m_origin;
36 Q_DISABLE_COPY(QChartView)
38 Q_DISABLE_COPY(QChartView)
37
39
38
40
39 };
41 };
40
42
41 QTCOMMERCIALCHART_END_NAMESPACE
43 QTCOMMERCIALCHART_END_NAMESPACE
42
44
43 #endif // QCHARTWIDGET_H
45 #endif // QCHARTWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now