##// END OF EJS Templates
Chart title font color
Tero Ahola -
r495:5c5df42e5e34
parent child
Show More
@@ -1,95 +1,97
1 #include "mainwindow.h"
1 #include "mainwindow.h"
2 #include <qchartview.h>
2 #include <qchartview.h>
3 #include <qpieseries.h>
3 #include <qpieseries.h>
4 #include <qpieslice.h>
4 #include <qpieslice.h>
5 #include <qlineseries.h>
5 #include <qlineseries.h>
6 #include <qscatterseries.h>
6 #include <qscatterseries.h>
7 #include <qchartaxis.h>
7 #include <qchartaxis.h>
8 #include <QDebug>
8 #include <QDebug>
9
9
10 QTCOMMERCIALCHART_USE_NAMESPACE
10 QTCOMMERCIALCHART_USE_NAMESPACE
11
11
12 MainWindow::MainWindow(QWidget *parent)
12 MainWindow::MainWindow(QWidget *parent)
13 : QMainWindow(parent)
13 : QMainWindow(parent)
14 {
14 {
15 // Here's the set of company's colors used throughout the example
15 // Here's the set of company's colors used throughout the example
16 m_companyColor1 = "#b90020";
16 m_companyColor1 = "#b90020";
17 m_companyColor2 = "#6d0013";
17 m_companyColor2 = "#6d0013";
18 m_companyColor3 = "#d5d5d5";
18 m_companyColor3 = "#d5d5f5";
19 m_companyColor4 = "#fcfcfc";
19 m_companyColor4 = "#fcfcfc";
20
20
21 resize(400, 300);
21 resize(400, 300);
22 //setWindowFlags(Qt::FramelessWindowHint);
22 setWindowFlags(Qt::FramelessWindowHint);
23
23
24 // Create chart view
24 // Create chart view
25 m_chartView = new QChartView(this);
25 m_chartView = new QChartView(this);
26 setCentralWidget(m_chartView);
26 setCentralWidget(m_chartView);
27 m_chartView->setChartTitle("Custom colors example");
27 m_chartView->setChartTitle("Custom colors example");
28 m_chartView->setRenderHint(QPainter::Antialiasing);
28 m_chartView->setRenderHint(QPainter::Antialiasing);
29
29
30 // Create line series
30 // Create line series
31 m_line = new QLineSeries();
31 m_line = new QLineSeries();
32 m_line->add(0.0, 1.1);
32 m_line->add(0.0, 1.1);
33 m_line->add(1.0, 2.3);
33 m_line->add(1.0, 2.3);
34 m_line->add(2.0, 2.1);
34 m_line->add(2.0, 2.1);
35 m_line->add(3.0, 3.3);
35 m_line->add(3.0, 3.3);
36 m_chartView->addSeries(m_line);
36 m_chartView->addSeries(m_line);
37
37
38 // Create scatter series with the same data
38 // Create scatter series with the same data
39 m_scatter = new QScatterSeries();
39 m_scatter = new QScatterSeries();
40 m_scatter->add(m_line->data());
40 m_scatter->add(m_line->data());
41 m_chartView->addSeries(m_scatter);
41 m_chartView->addSeries(m_scatter);
42
42
43 // Create pie series using color 2; use different fill styles for each pie slice
43 // Create pie series with different data
44 m_pie = new QPieSeries();
44 m_pie = new QPieSeries();
45 m_pie->add(1.1, "1");
45 m_pie->add(1.1, "1");
46 m_pie->add(2.1, "2");
46 m_pie->add(2.1, "2");
47 m_pie->add(3.0, "3");
47 m_pie->add(3.0, "3");
48 m_pie->setPositionFactors(0.7, 0.7);
48 m_pie->setPositionFactors(0.7, 0.7);
49 m_pie->setSizeFactor(0.5);
49 m_pie->setSizeFactor(0.5);
50 m_chartView->addSeries(m_pie);
50 m_chartView->addSeries(m_pie);
51
51
52 connect(&m_timer, SIGNAL(timeout()), this, SLOT(customize()));
52 connect(&m_timer, SIGNAL(timeout()), this, SLOT(customize()));
53 m_timer.setInterval(1500);
53 m_timer.setInterval(1500);
54 m_timer.setSingleShot(false);
54 m_timer.setSingleShot(false);
55 m_timer.start();
55 m_timer.start();
56 }
56 }
57
57
58 MainWindow::~MainWindow()
58 MainWindow::~MainWindow()
59 {
59 {
60 }
60 }
61
61
62 void MainWindow::customize()
62 void MainWindow::customize()
63 {
63 {
64 // Customize chart background
64 // Customize chart background
65 // Use a gradient from color 3 to color 4 for chart background
65 // Use a gradient from color 3 to color 4 for chart background
66 QLinearGradient chartGradient(0, 0, 0, 300);
66 QLinearGradient chartGradient(0, 0, 0, 300);
67 chartGradient.setColorAt(0.0, m_companyColor3);
67 chartGradient.setColorAt(0.0, m_companyColor3);
68 chartGradient.setColorAt(0.5, m_companyColor4);
68 chartGradient.setColorAt(0.5, m_companyColor4);
69 chartGradient.setColorAt(1.0, m_companyColor3);
69 chartGradient.setColorAt(1.0, m_companyColor3);
70 m_chartView->setChartBackgroundBrush(chartGradient);
70 m_chartView->setChartBackgroundBrush(chartGradient);
71 m_chartView->setBackgroundBrush(m_companyColor4);
71 m_chartView->setBackgroundBrush(m_companyColor4);
72 m_chartView->setChartTitleBrush(m_companyColor1);
72
73
73 // Customize chart axis
74 // Customize chart axis
74 QPen color1Pen(m_companyColor1, 4.0);
75 QPen color1Pen(m_companyColor1, 4.0);
75 m_chartView->axisX()->setAxisPen(color1Pen);
76 m_chartView->axisX()->setAxisPen(color1Pen);
76 m_chartView->axisY()->setAxisPen(color1Pen);
77 m_chartView->axisY()->setAxisPen(color1Pen);
77
78
78 // Customize series
79 // Customize series
79 m_line->setPen(color1Pen);
80 m_line->setPen(color1Pen);
80 m_scatter->setPen(color1Pen);
81 m_scatter->setPen(color1Pen);
81 m_scatter->setBrush(m_companyColor3);
82 m_scatter->setBrush(m_companyColor3);
82 for (int i(0); i < m_pie->slices().count(); i++) {
83 for (int i(0); i < m_pie->slices().count(); i++) {
83 Qt::BrushStyle style = static_cast<Qt::BrushStyle>(i + 1);
84 Qt::BrushStyle style = static_cast<Qt::BrushStyle>(i + 1);
84 m_pie->slices().at(i)->setSliceBrush(QBrush(m_companyColor2, style));
85 m_pie->slices().at(i)->setSliceBrush(QBrush(m_companyColor2, style));
85 m_pie->slices().at(i)->setSlicePen(color1Pen);
86 m_pie->slices().at(i)->setSlicePen(color1Pen);
86 }
87 }
87
88
89
88 // Calculate new colors to be used on the next update for the series
90 // Calculate new colors to be used on the next update for the series
89 m_companyColor1.setRed((m_companyColor1.red() + 25) % 255);
91 m_companyColor1.setRed((m_companyColor1.red() + 25) % 255);
90 m_companyColor1.setGreen((m_companyColor1.green() + 25) % 255);
92 m_companyColor1.setGreen((m_companyColor1.green() + 25) % 255);
91 m_companyColor1.setBlue((m_companyColor1.blue() + 25) % 255);
93 m_companyColor1.setBlue((m_companyColor1.blue() + 25) % 255);
92 m_companyColor2.setRed((m_companyColor2.red() + 25) % 255);
94 m_companyColor2.setRed((m_companyColor2.red() + 25) % 255);
93 m_companyColor2.setGreen((m_companyColor2.green() + 25) % 255);
95 m_companyColor2.setGreen((m_companyColor2.green() + 25) % 255);
94 m_companyColor2.setBlue((m_companyColor2.blue() + 25) % 255);
96 m_companyColor2.setBlue((m_companyColor2.blue() + 25) % 255);
95 }
97 }
@@ -1,288 +1,306
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include "chartdataset_p.h"
4 #include "chartdataset_p.h"
5 #include <QGraphicsScene>
5 #include <QGraphicsScene>
6 #include <QGraphicsSceneResizeEvent>
6 #include <QGraphicsSceneResizeEvent>
7 #include <QDebug>
7 #include <QDebug>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 /*!
11 /*!
12 \enum QChart::ChartTheme
12 \enum QChart::ChartTheme
13
13
14 This enum describes the theme used by the chart.
14 This enum describes the theme used by the chart.
15
15
16 \value ChartThemeDefault
16 \value ChartThemeDefault
17 \value ChartThemeVanilla
17 \value ChartThemeVanilla
18 \value ChartThemeIcy
18 \value ChartThemeIcy
19 \value ChartThemeGrayscale
19 \value ChartThemeGrayscale
20 \value ChartThemeScientific
20 \value ChartThemeScientific
21 */
21 */
22
22
23 /*!
23 /*!
24 \enum QChart::AnimationOption
24 \enum QChart::AnimationOption
25
25
26 For enabling/disabling animations. Defaults to NoAnimation.
26 For enabling/disabling animations. Defaults to NoAnimation.
27
27
28 \value NoAnimation
28 \value NoAnimation
29 \value GridAxisAnimations
29 \value GridAxisAnimations
30 \value SeriesAnimations
30 \value SeriesAnimations
31 \value AllAnimations
31 \value AllAnimations
32 */
32 */
33
33
34 /*!
34 /*!
35 \class QChart
35 \class QChart
36 \brief QtCommercial chart API.
36 \brief QtCommercial chart API.
37
37
38 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
38 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
39 representation of different types of QChartSeries and other chart related objects like
39 representation of different types of QChartSeries and other chart related objects like
40 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
40 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
41 convenience class QChartView instead of QChart.
41 convenience class QChartView instead of QChart.
42 \sa QChartView
42 \sa QChartView
43 */
43 */
44
44
45 /*!
45 /*!
46 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
46 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
47 */
47 */
48 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
48 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
49 m_backgroundItem(0),
49 m_backgroundItem(0),
50 m_titleItem(0),
50 m_titleItem(0),
51 m_dataset(new ChartDataSet(this)),
51 m_dataset(new ChartDataSet(this)),
52 m_presenter(new ChartPresenter(this,m_dataset))
52 m_presenter(new ChartPresenter(this,m_dataset))
53 {
53 {
54 }
54 }
55
55
56 /*!
56 /*!
57 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
57 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
58 */
58 */
59 QChart::~QChart()
59 QChart::~QChart()
60 {
60 {
61 }
61 }
62
62
63 /*!
63 /*!
64 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
64 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
65 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
65 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
66 the y axis).
66 the y axis).
67 */
67 */
68 void QChart::addSeries(QSeries* series, QChartAxis* axisY)
68 void QChart::addSeries(QSeries* series, QChartAxis* axisY)
69 {
69 {
70 m_dataset->addSeries(series, axisY);
70 m_dataset->addSeries(series, axisY);
71 }
71 }
72
72
73 /*!
73 /*!
74 Removes the \a series specified in a perameter from the QChartView.
74 Removes the \a series specified in a perameter from the QChartView.
75 It releses its ownership of the specified QChartSeries object.
75 It releses its ownership of the specified QChartSeries object.
76 It does not delete the pointed QChartSeries data object
76 It does not delete the pointed QChartSeries data object
77 \sa addSeries(), removeAllSeries()
77 \sa addSeries(), removeAllSeries()
78 */
78 */
79 void QChart::removeSeries(QSeries* series)
79 void QChart::removeSeries(QSeries* series)
80 {
80 {
81 m_dataset->removeSeries(series);
81 m_dataset->removeSeries(series);
82 }
82 }
83
83
84 /*!
84 /*!
85 Removes all the QChartSeries that have been added to the QChartView
85 Removes all the QChartSeries that have been added to the QChartView
86 It also deletes the pointed QChartSeries data objects
86 It also deletes the pointed QChartSeries data objects
87 \sa addSeries(), removeSeries()
87 \sa addSeries(), removeSeries()
88 */
88 */
89 void QChart::removeAllSeries()
89 void QChart::removeAllSeries()
90 {
90 {
91 m_dataset->removeAllSeries();
91 m_dataset->removeAllSeries();
92 }
92 }
93
93
94 /*!
94 /*!
95 Sets the \a brush that is used for painting the background of the chart area.
95 Sets the \a brush that is used for painting the background of the chart area.
96 */
96 */
97 void QChart::setChartBackgroundBrush(const QBrush& brush)
97 void QChart::setChartBackgroundBrush(const QBrush& brush)
98 {
98 {
99 createChartBackgroundItem();
99 createChartBackgroundItem();
100 m_backgroundItem->setBrush(brush);
100 m_backgroundItem->setBrush(brush);
101 m_backgroundItem->update();
101 m_backgroundItem->update();
102 }
102 }
103
103
104 /*!
104 /*!
105 Sets the \a pen that is used for painting the background of the chart area.
105 Sets the \a pen that is used for painting the background of the chart area.
106 */
106 */
107 void QChart::setChartBackgroundPen(const QPen& pen)
107 void QChart::setChartBackgroundPen(const QPen& pen)
108 {
108 {
109 createChartBackgroundItem();
109 createChartBackgroundItem();
110 m_backgroundItem->setPen(pen);
110 m_backgroundItem->setPen(pen);
111 m_backgroundItem->update();
111 m_backgroundItem->update();
112 }
112 }
113
113
114 /*!
114 /*!
115 Sets the chart \a title. The description text that is drawn above the chart.
115 Sets the chart \a title. The description text that is drawn above the chart.
116 */
116 */
117 void QChart::setChartTitle(const QString& title)
117 void QChart::setChartTitle(const QString& title)
118 {
118 {
119 createChartTitleItem();
119 createChartTitleItem();
120 m_titleItem->setText(title);
120 m_titleItem->setText(title);
121 }
121 }
122
122
123 /*!
123 /*!
124 Returns the chart title. The description text that is drawn above the chart.
124 Returns the chart title. The description text that is drawn above the chart.
125 */
125 */
126 QString QChart::chartTitle() const
126 QString QChart::chartTitle() const
127 {
127 {
128 if(m_titleItem)
128 if(m_titleItem)
129 return m_titleItem->text();
129 return m_titleItem->text();
130 else
130 else
131 return QString();
131 return QString();
132 }
132 }
133
133
134 /*!
134 /*!
135 Sets the \a font that is used for rendering the description text that is rendered above the chart.
135 Sets the \a font that is used for rendering the description text that is rendered above the chart.
136 */
136 */
137 void QChart::setChartTitleFont(const QFont& font)
137 void QChart::setChartTitleFont(const QFont& font)
138 {
138 {
139 createChartTitleItem();
139 createChartTitleItem();
140 m_titleItem->setFont(font);
140 m_titleItem->setFont(font);
141 }
141 }
142
142
143 /*!
144 Sets the \a brush used for rendering the title text.
145 */
146 void QChart::setChartTitleBrush(const QBrush &brush)
147 {
148 createChartTitleItem();
149 m_titleItem->setBrush(brush);
150 }
151
152 /*!
153 Returns the brush used for rendering the title text.
154 */
155 QBrush QChart::chartTitleBrush()
156 {
157 createChartTitleItem();
158 return m_titleItem->brush();
159 }
160
143 void QChart::createChartBackgroundItem()
161 void QChart::createChartBackgroundItem()
144 {
162 {
145 if(!m_backgroundItem) {
163 if(!m_backgroundItem) {
146 m_backgroundItem = new QGraphicsRectItem(this);
164 m_backgroundItem = new QGraphicsRectItem(this);
147 m_backgroundItem->setPen(Qt::NoPen);
165 m_backgroundItem->setPen(Qt::NoPen);
148 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
166 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
149 }
167 }
150 }
168 }
151
169
152 void QChart::createChartTitleItem()
170 void QChart::createChartTitleItem()
153 {
171 {
154 if(!m_titleItem) {
172 if(!m_titleItem) {
155 m_titleItem = new QGraphicsSimpleTextItem(this);
173 m_titleItem = new QGraphicsSimpleTextItem(this);
156 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
174 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
157 }
175 }
158 }
176 }
159
177
160 /*!
178 /*!
161 Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
179 Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
162 \sa setMargin()
180 \sa setMargin()
163 */
181 */
164 int QChart::margin() const
182 int QChart::margin() const
165 {
183 {
166 return m_presenter->margin();
184 return m_presenter->margin();
167 }
185 }
168
186
169 /*!
187 /*!
170 Sets the chart \a margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
188 Sets the chart \a margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
171 \sa margin()
189 \sa margin()
172 */
190 */
173 void QChart::setMargin(int margin)
191 void QChart::setMargin(int margin)
174 {
192 {
175 m_presenter->setMargin(margin);
193 m_presenter->setMargin(margin);
176 }
194 }
177
195
178 /*!
196 /*!
179 Sets the \a theme used by the chart for rendering the graphical representation of the data
197 Sets the \a theme used by the chart for rendering the graphical representation of the data
180 \sa ChartTheme, chartTheme()
198 \sa ChartTheme, chartTheme()
181 */
199 */
182 void QChart::setChartTheme(QChart::ChartTheme theme)
200 void QChart::setChartTheme(QChart::ChartTheme theme)
183 {
201 {
184 m_presenter->setChartTheme(theme);
202 m_presenter->setChartTheme(theme);
185 }
203 }
186
204
187 /*!
205 /*!
188 Returns the theme enum used by the chart.
206 Returns the theme enum used by the chart.
189 \sa ChartTheme, setChartTheme()
207 \sa ChartTheme, setChartTheme()
190 */
208 */
191 QChart::ChartTheme QChart::chartTheme() const
209 QChart::ChartTheme QChart::chartTheme() const
192 {
210 {
193 return m_presenter->chartTheme();
211 return m_presenter->chartTheme();
194 }
212 }
195
213
196 /*!
214 /*!
197 Zooms in the view by a factor of 2
215 Zooms in the view by a factor of 2
198 */
216 */
199 void QChart::zoomIn()
217 void QChart::zoomIn()
200 {
218 {
201 m_presenter->zoomIn();
219 m_presenter->zoomIn();
202 }
220 }
203
221
204 /*!
222 /*!
205 Zooms in the view to a maximum level at which \a rect is still fully visible.
223 Zooms in the view to a maximum level at which \a rect is still fully visible.
206 */
224 */
207 void QChart::zoomIn(const QRectF& rect)
225 void QChart::zoomIn(const QRectF& rect)
208 {
226 {
209
227
210 if(!rect.isValid()) return;
228 if(!rect.isValid()) return;
211 m_presenter->zoomIn(rect);
229 m_presenter->zoomIn(rect);
212 }
230 }
213
231
214 /*!
232 /*!
215 Restores the view zoom level to the previous one.
233 Restores the view zoom level to the previous one.
216 */
234 */
217 void QChart::zoomOut()
235 void QChart::zoomOut()
218 {
236 {
219 m_presenter->zoomOut();
237 m_presenter->zoomOut();
220 }
238 }
221
239
222 /*!
240 /*!
223 Resets to the default view.
241 Resets to the default view.
224 */
242 */
225 void QChart::zoomReset()
243 void QChart::zoomReset()
226 {
244 {
227 m_presenter->zoomReset();
245 m_presenter->zoomReset();
228 }
246 }
229
247
230 /*!
248 /*!
231 Returns the pointer to the x axis object of the chart
249 Returns the pointer to the x axis object of the chart
232 */
250 */
233 QChartAxis* QChart::axisX() const
251 QChartAxis* QChart::axisX() const
234 {
252 {
235 return m_dataset->axisX();
253 return m_dataset->axisX();
236 }
254 }
237
255
238 /*!
256 /*!
239 Returns the pointer to the y axis object of the chart
257 Returns the pointer to the y axis object of the chart
240 */
258 */
241 QChartAxis* QChart::axisY() const
259 QChartAxis* QChart::axisY() const
242 {
260 {
243 return m_dataset->axisY();
261 return m_dataset->axisY();
244 }
262 }
245
263
246 /*!
264 /*!
247 Resizes and updates the chart area using the \a event data
265 Resizes and updates the chart area using the \a event data
248 */
266 */
249 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
267 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
250 {
268 {
251
269
252 m_rect = QRectF(QPoint(0,0),event->newSize());
270 m_rect = QRectF(QPoint(0,0),event->newSize());
253 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
271 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
254
272
255 // recalculate title position
273 // recalculate title position
256 if (m_titleItem) {
274 if (m_titleItem) {
257 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
275 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
258 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
276 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
259 }
277 }
260
278
261 //recalculate background gradient
279 //recalculate background gradient
262 if (m_backgroundItem) {
280 if (m_backgroundItem) {
263 m_backgroundItem->setRect(rect);
281 m_backgroundItem->setRect(rect);
264 }
282 }
265
283
266 QGraphicsWidget::resizeEvent(event);
284 QGraphicsWidget::resizeEvent(event);
267 update();
285 update();
268 }
286 }
269
287
270 /*!
288 /*!
271 Sets animation \a options for the chart
289 Sets animation \a options for the chart
272 */
290 */
273 void QChart::setAnimationOptions(AnimationOptions options)
291 void QChart::setAnimationOptions(AnimationOptions options)
274 {
292 {
275 m_presenter->setAnimationOptions(options);
293 m_presenter->setAnimationOptions(options);
276 }
294 }
277
295
278 /*!
296 /*!
279 Returns animation options for the chart
297 Returns animation options for the chart
280 */
298 */
281 QChart::AnimationOptions QChart::animationOptions() const
299 QChart::AnimationOptions QChart::animationOptions() const
282 {
300 {
283 return m_presenter->animationOptions();
301 return m_presenter->animationOptions();
284 }
302 }
285
303
286 #include "moc_qchart.cpp"
304 #include "moc_qchart.cpp"
287
305
288 QTCOMMERCIALCHART_END_NAMESPACE
306 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,96 +1,98
1 #ifndef QCHART_H
1 #ifndef QCHART_H
2 #define QCHART_H
2 #define QCHART_H
3
3
4 #include <qchartglobal.h>
4 #include <qchartglobal.h>
5 #include <qseries.h>
5 #include <qseries.h>
6 #include <QGraphicsWidget>
6 #include <QGraphicsWidget>
7 #include <QLinearGradient>
7 #include <QLinearGradient>
8 #include <QFont>
8 #include <QFont>
9
9
10 class QGraphicsSceneResizeEvent;
10 class QGraphicsSceneResizeEvent;
11
11
12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13
13
14 class AxisItem;
14 class AxisItem;
15 class QSeries;
15 class QSeries;
16 class PlotDomain;
16 class PlotDomain;
17 class BarPresenter;
17 class BarPresenter;
18 class QChartAxis;
18 class QChartAxis;
19 class ChartTheme;
19 class ChartTheme;
20 class ChartItem;
20 class ChartItem;
21 class ChartDataSet;
21 class ChartDataSet;
22 class ChartPresenter;
22 class ChartPresenter;
23
23
24 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
24 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
25 {
25 {
26 Q_OBJECT
26 Q_OBJECT
27 public:
27 public:
28 enum ChartTheme {
28 enum ChartTheme {
29 ChartThemeDefault,
29 ChartThemeDefault,
30 ChartThemeVanilla,
30 ChartThemeVanilla,
31 ChartThemeIcy,
31 ChartThemeIcy,
32 ChartThemeGrayscale,
32 ChartThemeGrayscale,
33 ChartThemeScientific
33 ChartThemeScientific
34 //ChartThemeUnnamed1
34 //ChartThemeUnnamed1
35 /*! The default theme follows the GUI style of the Operating System */
35 /*! The default theme follows the GUI style of the Operating System */
36 };
36 };
37
37
38 enum AnimationOption {
38 enum AnimationOption {
39 NoAnimation = 0x0,
39 NoAnimation = 0x0,
40 GridAxisAnimations = 0x1,
40 GridAxisAnimations = 0x1,
41 SeriesAnimations =0x2,
41 SeriesAnimations =0x2,
42 AllAnimations = 0x3
42 AllAnimations = 0x3
43 };
43 };
44 Q_DECLARE_FLAGS(AnimationOptions, AnimationOption)
44 Q_DECLARE_FLAGS(AnimationOptions, AnimationOption)
45
45
46 public:
46 public:
47 QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
47 QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
48 ~QChart();
48 ~QChart();
49
49
50 void addSeries(QSeries* series, QChartAxis* axisY = 0);
50 void addSeries(QSeries* series, QChartAxis* axisY = 0);
51 void removeSeries(QSeries* series); //returns ownership , deletes axis if no series attached
51 void removeSeries(QSeries* series); //returns ownership , deletes axis if no series attached
52 void removeAllSeries(); // deletes series and axis
52 void removeAllSeries(); // deletes series and axis
53
53
54 void setMargin(int margin);
54 void setMargin(int margin);
55 int margin() const;
55 int margin() const;
56 void setChartTheme(QChart::ChartTheme theme);
56 void setChartTheme(QChart::ChartTheme theme);
57 QChart::ChartTheme chartTheme() const;
57 QChart::ChartTheme chartTheme() const;
58
58
59 void setChartTitle(const QString& title);
59 void setChartTitle(const QString& title);
60 QString chartTitle() const;
60 QString chartTitle() const;
61 void setChartTitleFont(const QFont& font);
61 void setChartTitleFont(const QFont& font);
62 void setChartTitleBrush(const QBrush &brush);
63 QBrush chartTitleBrush();
62 void setChartBackgroundBrush(const QBrush& brush);
64 void setChartBackgroundBrush(const QBrush& brush);
63 void setChartBackgroundPen(const QPen& pen);
65 void setChartBackgroundPen(const QPen& pen);
64
66
65 void setAnimationOptions(AnimationOptions options);
67 void setAnimationOptions(AnimationOptions options);
66 AnimationOptions animationOptions() const;
68 AnimationOptions animationOptions() const;
67
69
68 void zoomIn();
70 void zoomIn();
69 void zoomIn(const QRectF& rect);
71 void zoomIn(const QRectF& rect);
70 void zoomOut();
72 void zoomOut();
71 void zoomReset();
73 void zoomReset();
72
74
73 QChartAxis* axisX() const;
75 QChartAxis* axisX() const;
74 QChartAxis* axisY() const;
76 QChartAxis* axisY() const;
75
77
76 protected:
78 protected:
77 void resizeEvent(QGraphicsSceneResizeEvent *event);
79 void resizeEvent(QGraphicsSceneResizeEvent *event);
78
80
79 private:
81 private:
80 inline void createChartBackgroundItem();
82 inline void createChartBackgroundItem();
81 inline void createChartTitleItem();
83 inline void createChartTitleItem();
82
84
83 private:
85 private:
84 Q_DISABLE_COPY(QChart)
86 Q_DISABLE_COPY(QChart)
85 QGraphicsRectItem* m_backgroundItem;
87 QGraphicsRectItem* m_backgroundItem;
86 QGraphicsSimpleTextItem* m_titleItem;
88 QGraphicsSimpleTextItem* m_titleItem;
87 QRectF m_rect;
89 QRectF m_rect;
88 ChartDataSet *m_dataset;
90 ChartDataSet *m_dataset;
89 ChartPresenter *m_presenter;
91 ChartPresenter *m_presenter;
90 };
92 };
91
93
92 QTCOMMERCIALCHART_END_NAMESPACE
94 QTCOMMERCIALCHART_END_NAMESPACE
93
95
94 Q_DECLARE_OPERATORS_FOR_FLAGS(QTCOMMERCIALCHART_NAMESPACE::QChart::AnimationOptions)
96 Q_DECLARE_OPERATORS_FOR_FLAGS(QTCOMMERCIALCHART_NAMESPACE::QChart::AnimationOptions)
95
97
96 #endif
98 #endif
@@ -1,359 +1,375
1 #include "qchartview.h"
1 #include "qchartview.h"
2 #include "qchart.h"
2 #include "qchart.h"
3 #include "qchartaxis.h"
3 #include "qchartaxis.h"
4 #include <QGraphicsView>
4 #include <QGraphicsView>
5 #include <QGraphicsScene>
5 #include <QGraphicsScene>
6 #include <QRubberBand>
6 #include <QRubberBand>
7 #include <QResizeEvent>
7 #include <QResizeEvent>
8 #include <QDebug>
8 #include <QDebug>
9
9
10 /*!
10 /*!
11 \enum QChartView::RubberBandPolicy
11 \enum QChartView::RubberBandPolicy
12
12
13 This enum describes the different types of rubber bands that can be used for zoom rect selection
13 This enum describes the different types of rubber bands that can be used for zoom rect selection
14
14
15 \value NoRubberBand
15 \value NoRubberBand
16 \value VerticalRubberBand
16 \value VerticalRubberBand
17 \value HorizonalRubberBand
17 \value HorizonalRubberBand
18 \value RectangleRubberBand
18 \value RectangleRubberBand
19 */
19 */
20
20
21 /*!
21 /*!
22 \class QChartView
22 \class QChartView
23 \brief Standalone charting widget.
23 \brief Standalone charting widget.
24
24
25 QChartView is a standalone widget that can display charts. It does not require separate
25 QChartView is a standalone widget that can display charts. It does not require separate
26 QGraphicsScene to work. It manages the graphical representation of different types of
26 QGraphicsScene to work. It manages the graphical representation of different types of
27 QChartSeries and other chart related objects like QChartAxis and QChartLegend. If you want to
27 QChartSeries and other chart related objects like QChartAxis and QChartLegend. If you want to
28 display a chart in your existing QGraphicsScene, you can use the QChart class instead.
28 display a chart in your existing QGraphicsScene, you can use the QChart class instead.
29
29
30 \sa QChart
30 \sa QChart
31 */
31 */
32
32
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34
34
35 /*!
35 /*!
36 Constructs a chartView object which is a child of a\a parent.
36 Constructs a chartView object which is a child of a\a parent.
37 */
37 */
38 QChartView::QChartView(QWidget *parent) :
38 QChartView::QChartView(QWidget *parent) :
39 QGraphicsView(parent),
39 QGraphicsView(parent),
40 m_scene(new QGraphicsScene(this)),
40 m_scene(new QGraphicsScene(this)),
41 m_chart(new QChart()),
41 m_chart(new QChart()),
42 m_rubberBand(0),
42 m_rubberBand(0),
43 m_verticalRubberBand(false),
43 m_verticalRubberBand(false),
44 m_horizonalRubberBand(false)
44 m_horizonalRubberBand(false)
45 {
45 {
46 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
46 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
47 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
47 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
48 setScene(m_scene);
48 setScene(m_scene);
49 m_chart->setMargin(50);
49 m_chart->setMargin(50);
50 m_scene->addItem(m_chart);
50 m_scene->addItem(m_chart);
51 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
51 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
52 }
52 }
53
53
54
54
55 /*!
55 /*!
56 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
56 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
57 */
57 */
58 QChartView::~QChartView()
58 QChartView::~QChartView()
59 {
59 {
60 }
60 }
61
61
62 /*!
62 /*!
63 Resizes and updates the chart area using the \a event data
63 Resizes and updates the chart area using the \a event data
64 */
64 */
65 void QChartView::resizeEvent(QResizeEvent *event)
65 void QChartView::resizeEvent(QResizeEvent *event)
66 {
66 {
67 m_scene->setSceneRect(0,0,size().width(),size().height());
67 m_scene->setSceneRect(0,0,size().width(),size().height());
68 m_chart->resize(size());
68 m_chart->resize(size());
69 QWidget::resizeEvent(event);
69 QWidget::resizeEvent(event);
70 }
70 }
71
71
72 /*!
72 /*!
73 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
73 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
74 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
74 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
75 the y axis).
75 the y axis).
76 \sa removeSeries(), removeAllSeries()
76 \sa removeSeries(), removeAllSeries()
77 */
77 */
78 void QChartView::addSeries(QSeries* series,QChartAxis *axisY)
78 void QChartView::addSeries(QSeries* series,QChartAxis *axisY)
79 {
79 {
80 m_chart->addSeries(series,axisY);
80 m_chart->addSeries(series,axisY);
81 }
81 }
82
82
83 /*!
83 /*!
84 Removes the \a series specified in a perameter from the QChartView.
84 Removes the \a series specified in a perameter from the QChartView.
85 It releses its ownership of the specified QChartSeries object.
85 It releses its ownership of the specified QChartSeries object.
86 It does not delete the pointed QChartSeries data object
86 It does not delete the pointed QChartSeries data object
87 \sa addSeries(), removeAllSeries()
87 \sa addSeries(), removeAllSeries()
88 */
88 */
89 void QChartView::removeSeries(QSeries* series)
89 void QChartView::removeSeries(QSeries* series)
90 {
90 {
91 m_chart->removeSeries(series);
91 m_chart->removeSeries(series);
92 }
92 }
93
93
94 /*!
94 /*!
95 Removes all the QChartSeries that have been added to the QChartView
95 Removes all the QChartSeries that have been added to the QChartView
96 It also deletes the pointed QChartSeries data objects
96 It also deletes the pointed QChartSeries data objects
97 \sa addSeries(), removeSeries()
97 \sa addSeries(), removeSeries()
98 */
98 */
99 void QChartView::removeAllSeries()
99 void QChartView::removeAllSeries()
100 {
100 {
101 m_chart->removeAllSeries();
101 m_chart->removeAllSeries();
102 }
102 }
103
103
104 /*!
104 /*!
105 Zooms in the view by a factor of 2
105 Zooms in the view by a factor of 2
106 */
106 */
107 void QChartView::zoomIn()
107 void QChartView::zoomIn()
108 {
108 {
109 m_chart->zoomIn();
109 m_chart->zoomIn();
110 }
110 }
111
111
112 /*!
112 /*!
113 Zooms in the view to a maximum level at which \a rect is still fully visible.
113 Zooms in the view to a maximum level at which \a rect is still fully visible.
114 */
114 */
115 void QChartView::zoomIn(const QRect& rect)
115 void QChartView::zoomIn(const QRect& rect)
116 {
116 {
117 m_chart->zoomIn(rect);
117 m_chart->zoomIn(rect);
118 }
118 }
119
119
120 /*!
120 /*!
121 Restores the view zoom level to the previous one.
121 Restores the view zoom level to the previous one.
122 */
122 */
123 void QChartView::zoomOut()
123 void QChartView::zoomOut()
124 {
124 {
125 m_chart->zoomOut();
125 m_chart->zoomOut();
126 }
126 }
127
127
128 /*!
128 /*!
129 Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
129 Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
130 */
130 */
131 int QChartView::margin() const
131 int QChartView::margin() const
132 {
132 {
133 return m_chart->margin();
133 return m_chart->margin();
134 }
134 }
135
135
136 /*!
136 /*!
137 Sets the chart \a title. A description text that is drawn above the chart.
137 Sets the chart \a title. A description text that is drawn above the chart.
138 */
138 */
139 void QChartView::setChartTitle(const QString& title)
139 void QChartView::setChartTitle(const QString& title)
140 {
140 {
141 m_chart->setChartTitle(title);
141 m_chart->setChartTitle(title);
142 }
142 }
143
143
144 /*!
144 /*!
145 Returns the chart's title. A description text that is drawn above the chart.
145 Returns the chart's title. A description text that is drawn above the chart.
146 */
146 */
147 QString QChartView::chartTitle() const
147 QString QChartView::chartTitle() const
148 {
148 {
149 return m_chart->chartTitle();
149 return m_chart->chartTitle();
150 }
150 }
151
151
152 /*!
152 /*!
153 Sets the \a font that is used for rendering the description text that is rendered above the chart.
153 Sets the \a font that is used for rendering the description text that is rendered above the chart.
154 */
154 */
155 void QChartView::setChartTitleFont(const QFont& font)
155 void QChartView::setChartTitleFont(const QFont& font)
156 {
156 {
157 m_chart->setChartTitleFont(font);
157 m_chart->setChartTitleFont(font);
158 }
158 }
159
159
160 /*!
160 /*!
161 Sets the \a brush used for rendering the title text.
162 */
163 void QChartView::setChartTitleBrush(const QBrush &brush)
164 {
165 m_chart->setChartTitleBrush(brush);
166 }
167
168 /*!
169 Returns the brush used for rendering the title text.
170 */
171 QBrush QChartView::chartTitleBrush()
172 {
173 return m_chart->chartTitleBrush();
174 }
175
176 /*!
161 Sets the \a brush that is used for painting the background of the chart area of the QChartView widget.
177 Sets the \a brush that is used for painting the background of the chart area of the QChartView widget.
162 */
178 */
163 void QChartView::setChartBackgroundBrush(const QBrush& brush)
179 void QChartView::setChartBackgroundBrush(const QBrush& brush)
164 {
180 {
165 m_chart->setChartBackgroundBrush(brush);
181 m_chart->setChartBackgroundBrush(brush);
166 }
182 }
167
183
168 /*!
184 /*!
169 Sets the \a pen that is used for painting the background of the chart area of the QChartView widget.
185 Sets the \a pen that is used for painting the background of the chart area of the QChartView widget.
170 */
186 */
171 void QChartView::setChartBackgroundPen(const QPen& pen)
187 void QChartView::setChartBackgroundPen(const QPen& pen)
172 {
188 {
173 m_chart->setChartBackgroundPen(pen);
189 m_chart->setChartBackgroundPen(pen);
174 }
190 }
175
191
176 /*!
192 /*!
177 Sets the RubberBandPlicy to \a policy. Selected policy determines the way zooming is performed.
193 Sets the RubberBandPlicy to \a policy. Selected policy determines the way zooming is performed.
178 */
194 */
179 void QChartView::setRubberBandPolicy(const RubberBandPolicy policy)
195 void QChartView::setRubberBandPolicy(const RubberBandPolicy policy)
180 {
196 {
181 switch(policy) {
197 switch(policy) {
182 case VerticalRubberBand:
198 case VerticalRubberBand:
183 m_verticalRubberBand = true;
199 m_verticalRubberBand = true;
184 m_horizonalRubberBand = false;
200 m_horizonalRubberBand = false;
185 break;
201 break;
186 case HorizonalRubberBand:
202 case HorizonalRubberBand:
187 m_verticalRubberBand = false;
203 m_verticalRubberBand = false;
188 m_horizonalRubberBand = true;
204 m_horizonalRubberBand = true;
189 break;
205 break;
190 case RectangleRubberBand:
206 case RectangleRubberBand:
191 m_verticalRubberBand = true;
207 m_verticalRubberBand = true;
192 m_horizonalRubberBand = true;
208 m_horizonalRubberBand = true;
193 break;
209 break;
194 case NoRubberBand:
210 case NoRubberBand:
195 default:
211 default:
196 delete m_rubberBand;
212 delete m_rubberBand;
197 m_rubberBand=0;
213 m_rubberBand=0;
198 m_horizonalRubberBand = false;
214 m_horizonalRubberBand = false;
199 m_verticalRubberBand = false;
215 m_verticalRubberBand = false;
200 return;
216 return;
201 }
217 }
202 if(!m_rubberBand) {
218 if(!m_rubberBand) {
203 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
219 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
204 m_rubberBand->setEnabled(true);
220 m_rubberBand->setEnabled(true);
205 }
221 }
206 }
222 }
207
223
208 /*!
224 /*!
209 Returns the RubberBandPolicy that is currently being used by the widget.
225 Returns the RubberBandPolicy that is currently being used by the widget.
210 */
226 */
211 QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const
227 QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const
212 {
228 {
213 if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand;
229 if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand;
214 if(m_horizonalRubberBand) return HorizonalRubberBand;
230 if(m_horizonalRubberBand) return HorizonalRubberBand;
215 if(m_verticalRubberBand) return VerticalRubberBand;
231 if(m_verticalRubberBand) return VerticalRubberBand;
216 return NoRubberBand;
232 return NoRubberBand;
217 }
233 }
218
234
219 /*!
235 /*!
220 If Left mouse button is pressed and the RubberBandPolicy is enabled the \a event is accepted and the rubber band is displayed on the screen allowing the user to select the zoom area.
236 If Left mouse button is pressed and the RubberBandPolicy is enabled the \a event is accepted and the rubber band is displayed on the screen allowing the user to select the zoom area.
221 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation.
237 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation.
222 */
238 */
223 void QChartView::mousePressEvent(QMouseEvent *event)
239 void QChartView::mousePressEvent(QMouseEvent *event)
224 {
240 {
225 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
241 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
226
242
227 int margin = m_chart->margin();
243 int margin = m_chart->margin();
228 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
244 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
229
245
230 if (rect.contains(event->pos())) {
246 if (rect.contains(event->pos())) {
231 m_rubberBandOrigin = event->pos();
247 m_rubberBandOrigin = event->pos();
232 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize()));
248 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize()));
233 m_rubberBand->show();
249 m_rubberBand->show();
234 event->accept();
250 event->accept();
235 }
251 }
236 }
252 }
237 else {
253 else {
238 QGraphicsView::mousePressEvent(event);
254 QGraphicsView::mousePressEvent(event);
239 }
255 }
240 }
256 }
241
257
242 /*!
258 /*!
243 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
259 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
244 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
260 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
245 */
261 */
246 void QChartView::mouseMoveEvent(QMouseEvent *event)
262 void QChartView::mouseMoveEvent(QMouseEvent *event)
247 {
263 {
248 if(m_rubberBand && m_rubberBand->isVisible()) {
264 if(m_rubberBand && m_rubberBand->isVisible()) {
249 int margin = m_chart->margin();
265 int margin = m_chart->margin();
250 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
266 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
251 int width = event->pos().x() - m_rubberBandOrigin.x();
267 int width = event->pos().x() - m_rubberBandOrigin.x();
252 int height = event->pos().y() - m_rubberBandOrigin.y();
268 int height = event->pos().y() - m_rubberBandOrigin.y();
253 if(!m_verticalRubberBand) {
269 if(!m_verticalRubberBand) {
254 m_rubberBandOrigin.setY(rect.top());
270 m_rubberBandOrigin.setY(rect.top());
255 height = rect.height();
271 height = rect.height();
256 }
272 }
257 if(!m_horizonalRubberBand) {
273 if(!m_horizonalRubberBand) {
258 m_rubberBandOrigin.setX(rect.left());
274 m_rubberBandOrigin.setX(rect.left());
259 width= rect.width();
275 width= rect.width();
260 }
276 }
261 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized());
277 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized());
262 }
278 }
263 else {
279 else {
264 QGraphicsView::mouseMoveEvent(event);
280 QGraphicsView::mouseMoveEvent(event);
265 }
281 }
266 }
282 }
267
283
268 /*!
284 /*!
269 If left mouse button is release and RubberBand is enabled then \a event is accepted and the view is zoomed in to rect specified by RubberBand
285 If left mouse button is release and RubberBand is enabled then \a event is accepted and the view is zoomed in to rect specified by RubberBand
270 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
286 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
271 */
287 */
272 void QChartView::mouseReleaseEvent(QMouseEvent *event)
288 void QChartView::mouseReleaseEvent(QMouseEvent *event)
273 {
289 {
274 if(m_rubberBand) {
290 if(m_rubberBand) {
275 if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) {
291 if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) {
276 m_rubberBand->hide();
292 m_rubberBand->hide();
277 QRect rect = m_rubberBand->geometry();
293 QRect rect = m_rubberBand->geometry();
278 m_chart->zoomIn(rect);
294 m_chart->zoomIn(rect);
279 event->accept();
295 event->accept();
280 }
296 }
281
297
282 if(event->button()==Qt::RightButton)
298 if(event->button()==Qt::RightButton)
283 m_chart->zoomReset();
299 m_chart->zoomReset();
284 }
300 }
285 else {
301 else {
286 QGraphicsView::mouseReleaseEvent(event);
302 QGraphicsView::mouseReleaseEvent(event);
287 }
303 }
288 }
304 }
289
305
290 /*!
306 /*!
291 Pressing + and - keys performs zoomIn() and zoomOut() respectivly.
307 Pressing + and - keys performs zoomIn() and zoomOut() respectivly.
292 In other \a event is passed to the QGraphicsView::keyPressEvent() implementation
308 In other \a event is passed to the QGraphicsView::keyPressEvent() implementation
293 */
309 */
294 void QChartView::keyPressEvent(QKeyEvent *event)
310 void QChartView::keyPressEvent(QKeyEvent *event)
295 {
311 {
296 switch (event->key()) {
312 switch (event->key()) {
297 case Qt::Key_Plus:
313 case Qt::Key_Plus:
298 zoomIn();
314 zoomIn();
299 break;
315 break;
300 case Qt::Key_Minus:
316 case Qt::Key_Minus:
301 zoomOut();
317 zoomOut();
302 break;
318 break;
303 default:
319 default:
304 QGraphicsView::keyPressEvent(event);
320 QGraphicsView::keyPressEvent(event);
305 break;
321 break;
306 }
322 }
307 }
323 }
308
324
309 /*!
325 /*!
310 Sets the \a theme used by the chart for rendering the graphical representation of the data
326 Sets the \a theme used by the chart for rendering the graphical representation of the data
311 \sa QChart::ChartTheme, chartTheme()
327 \sa QChart::ChartTheme, chartTheme()
312 */
328 */
313 void QChartView::setChartTheme(QChart::ChartTheme theme)
329 void QChartView::setChartTheme(QChart::ChartTheme theme)
314 {
330 {
315 m_chart->setChartTheme(theme);
331 m_chart->setChartTheme(theme);
316 }
332 }
317
333
318 /*!
334 /*!
319 Returns the theme enum used by the chart.
335 Returns the theme enum used by the chart.
320 \sa setChartTheme()
336 \sa setChartTheme()
321 */
337 */
322 QChart::ChartTheme QChartView::chartTheme() const
338 QChart::ChartTheme QChartView::chartTheme() const
323 {
339 {
324 return m_chart->chartTheme();
340 return m_chart->chartTheme();
325 }
341 }
326
342
327 /*!
343 /*!
328 Returns the pointer to the x axis object of the chart
344 Returns the pointer to the x axis object of the chart
329 */
345 */
330 QChartAxis* QChartView::axisX() const
346 QChartAxis* QChartView::axisX() const
331 {
347 {
332 return m_chart->axisX();
348 return m_chart->axisX();
333 }
349 }
334
350
335 /*!
351 /*!
336 Returns the pointer to the y axis object of the chart
352 Returns the pointer to the y axis object of the chart
337 */
353 */
338 QChartAxis* QChartView::axisY() const
354 QChartAxis* QChartView::axisY() const
339 {
355 {
340 return m_chart->axisY();
356 return m_chart->axisY();
341 }
357 }
342
358
343 /*!
359 /*!
344 Sets animation \a options for the chart
360 Sets animation \a options for the chart
345 */
361 */
346 void QChartView::setAnimationOptions(QChart::AnimationOptions options)
362 void QChartView::setAnimationOptions(QChart::AnimationOptions options)
347 {
363 {
348 m_chart->setAnimationOptions(options);
364 m_chart->setAnimationOptions(options);
349 }
365 }
350
366
351 /*!
367 /*!
352 Returns animation options for the chart
368 Returns animation options for the chart
353 */
369 */
354 QChart::AnimationOptions QChartView::animationOptions() const
370 QChart::AnimationOptions QChartView::animationOptions() const
355 {
371 {
356 return m_chart->animationOptions();
372 return m_chart->animationOptions();
357 }
373 }
358
374
359 QTCOMMERCIALCHART_END_NAMESPACE
375 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,75 +1,77
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 "qseries.h"
5 #include "qseries.h"
6 #include "qchart.h"
6 #include "qchart.h"
7 #include <QGraphicsView>
7 #include <QGraphicsView>
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 QChart;
14 class QChart;
15
15
16 class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView
16 class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView
17 {
17 {
18 public:
18 public:
19 enum RubberBandPolicy { NoRubberBand, VerticalRubberBand, HorizonalRubberBand, RectangleRubberBand };
19 enum RubberBandPolicy { NoRubberBand, VerticalRubberBand, HorizonalRubberBand, RectangleRubberBand };
20
20
21 explicit QChartView(QWidget *parent = 0);
21 explicit QChartView(QWidget *parent = 0);
22 ~QChartView();
22 ~QChartView();
23
23
24 //implement from QWidget
24 //implement from QWidget
25 void resizeEvent(QResizeEvent *event);
25 void resizeEvent(QResizeEvent *event);
26
26
27 void addSeries(QSeries* series,QChartAxis* axisY=0);// takes series ownership , takes axis ownership
27 void addSeries(QSeries* series,QChartAxis* axisY=0);// takes series ownership , takes axis ownership
28 void removeSeries(QSeries* series); //returns ownership , deletes axis if no series attached
28 void removeSeries(QSeries* series); //returns ownership , deletes axis if no series attached
29 void removeAllSeries(); // deletes series and axis
29 void removeAllSeries(); // deletes series and axis
30 int margin() const;
30 int margin() const;
31
31
32 void setChartTitle(const QString& title);
32 void setChartTitle(const QString& title);
33 QString chartTitle() const;
33 QString chartTitle() const;
34 void setChartTitleFont(const QFont& font);
34 void setChartTitleFont(const QFont& font);
35 void setChartTitleBrush(const QBrush &brush);
36 QBrush chartTitleBrush();
35 void setChartBackgroundBrush(const QBrush& brush);
37 void setChartBackgroundBrush(const QBrush& brush);
36 void setChartBackgroundPen(const QPen& pen);
38 void setChartBackgroundPen(const QPen& pen);
37
39
38 void zoomIn();
40 void zoomIn();
39 void zoomIn(const QRect& rect);
41 void zoomIn(const QRect& rect);
40 void zoomOut();
42 void zoomOut();
41
43
42 void setRubberBandPolicy(const RubberBandPolicy );
44 void setRubberBandPolicy(const RubberBandPolicy );
43 RubberBandPolicy rubberBandPolicy() const;
45 RubberBandPolicy rubberBandPolicy() const;
44
46
45 void setChartTheme(QChart::ChartTheme theme);
47 void setChartTheme(QChart::ChartTheme theme);
46 QChart::ChartTheme chartTheme() const;
48 QChart::ChartTheme chartTheme() const;
47
49
48 void setAnimationOptions(QChart::AnimationOptions options);
50 void setAnimationOptions(QChart::AnimationOptions options);
49 QChart::AnimationOptions animationOptions() const;
51 QChart::AnimationOptions animationOptions() const;
50
52
51 QChartAxis* axisX() const;
53 QChartAxis* axisX() const;
52 QChartAxis* axisY() const;
54 QChartAxis* axisY() const;
53
55
54 protected:
56 protected:
55 void mousePressEvent(QMouseEvent *event);
57 void mousePressEvent(QMouseEvent *event);
56 void mouseMoveEvent(QMouseEvent *event);
58 void mouseMoveEvent(QMouseEvent *event);
57 void mouseReleaseEvent(QMouseEvent *event);
59 void mouseReleaseEvent(QMouseEvent *event);
58 void keyPressEvent(QKeyEvent *event);
60 void keyPressEvent(QKeyEvent *event);
59
61
60
62
61 private:
63 private:
62 QGraphicsScene *m_scene;
64 QGraphicsScene *m_scene;
63 QChart* m_chart;
65 QChart* m_chart;
64 QPoint m_rubberBandOrigin;
66 QPoint m_rubberBandOrigin;
65 QRubberBand* m_rubberBand;
67 QRubberBand* m_rubberBand;
66 bool m_verticalRubberBand;
68 bool m_verticalRubberBand;
67 bool m_horizonalRubberBand;
69 bool m_horizonalRubberBand;
68 Q_DISABLE_COPY(QChartView)
70 Q_DISABLE_COPY(QChartView)
69
71
70
72
71 };
73 };
72
74
73 QTCOMMERCIALCHART_END_NAMESPACE
75 QTCOMMERCIALCHART_END_NAMESPACE
74
76
75 #endif // QCHARTWIDGET_H
77 #endif // QCHARTWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now