##// END OF EJS Templates
More examples on QChartView qdoc
Tero Ahola -
r321:13ac9d78995f
parent child
Show More
1 NO CONTENT: new file 100644, binary diff hidden
NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
NO CONTENT: modified file, binary diff hidden
@@ -1,40 +1,73
1 #include <QtGui/QApplication>
1 #include <QtGui/QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <qchartglobal.h>
3 #include <qchartglobal.h>
4 #include <qchartview.h>
4 #include <qchartview.h>
5 #include <qlinechartseries.h>
5 #include <qlinechartseries.h>
6 #include <qscatterseries.h>
7 #include <qbarchartseries.h>
8 #include <qbarset.h>
9 #include <qbarcategory.h>
10 #include <qpieseries.h>
6
11
7 QTCOMMERCIALCHART_USE_NAMESPACE
12 QTCOMMERCIALCHART_USE_NAMESPACE
8
13
9 int main(int argc, char *argv[])
14 int main(int argc, char *argv[])
10 {
15 {
11 QApplication a(argc, argv);
16 QApplication a(argc, argv);
12
17
13 //! [1]
18 //! [1]
14 // Create chart view
19 // Create chart view
15 QChartView *chartView = new QChartView();
20 QChartView *chartView = new QChartView();
16 chartView->setChartTheme(QChart::ChartThemeIcy);
21 // Add series to the chart
22 QLineChartSeries *line = new QLineChartSeries();
23 line->add(0.0, 0.8);
24 line->add(1.1, 1.1);
25 line->add(2.0, 2.5);
26 chartView->addSeries(line);
17 //! [1]
27 //! [1]
18
28
19 //! [2]
29 //! [2]
20 // Add series to the chart
30 // Change theme
21 QLineChartSeries *series = new QLineChartSeries();
31 chartView->setChartTheme(QChart::ChartThemeScientific);
22 series->add(0.0, 0.8);
23 series->add(1.1, 1.1);
24 series->add(1.6, 1.8);
25 series->add(2.0, 2.5);
26 chartView->addSeries(series);
27 //! [2]
32 //! [2]
28
33
29 //! [3]
34 //! [3]
30 // Change theme
35 // Add pie series
31 chartView->setChartTheme(QChart::ChartThemeScientific);
36 QPieSeries *pie = new QPieSeries();
37 pie->add(3.4, "slice1");
38 pie->add(6.7, "slice2");
39 chartView->addSeries(pie);
32 //! [3]
40 //! [3]
33
41
42 //! [4]
43 // Add scatter series
44 QScatterSeries *scatter = new QScatterSeries();
45 for (qreal x(0); x < 100; x += 0.5) {
46 qreal y = rand() % 100;
47 *(scatter) << QPointF(x, y);
48 }
49 chartView->addSeries(scatter);
50 //! [4]
51
52 //! [5]
53 // Add bar series
54 QBarCategory *barCategory = new QBarCategory();
55 *barCategory << "Jan"
56 << "Feb"
57 << "Mar";
58 QBarChartSeries *bar = new QBarChartSeries(barCategory);
59 QBarSet *barSet = new QBarSet("Sales");
60 *barSet << 123.2
61 << 301.3
62 << 285.8;
63 bar->addBarSet(barSet);
64 chartView->addSeries(bar);
65 //! [5]
66
34 QMainWindow w;
67 QMainWindow w;
35 w.resize(640, 480);
68 w.resize(350, 250);
36 w.setCentralWidget(chartView);
69 w.setCentralWidget(chartView);
37 w.show();
70 w.show();
38
71
39 return a.exec();
72 return a.exec();
40 }
73 }
@@ -1,229 +1,229
1 #include "charttheme_p.h"
1 #include "charttheme_p.h"
2 #include "qchart.h"
2 #include "qchart.h"
3 #include "qchartaxis.h"
3 #include "qchartaxis.h"
4
4
5
5
6 //series
6 //series
7 #include "qbarset.h"
7 #include "qbarset.h"
8 #include "qbarchartseries.h"
8 #include "qbarchartseries.h"
9 #include "qstackedbarchartseries.h"
9 #include "qstackedbarchartseries.h"
10 #include "qpercentbarchartseries.h"
10 #include "qpercentbarchartseries.h"
11 #include "qlinechartseries.h"
11 #include "qlinechartseries.h"
12 #include "qscatterseries.h"
12 #include "qscatterseries.h"
13 #include "qpieseries.h"
13 #include "qpieseries.h"
14 #include "qpieslice.h"
14 #include "qpieslice.h"
15
15
16 //items
16 //items
17 #include "axisitem_p.h"
17 #include "axisitem_p.h"
18 #include "barpresenter.h"
18 #include "barpresenter.h"
19 #include "stackedbarpresenter.h"
19 #include "stackedbarpresenter.h"
20 #include "linechartitem_p.h"
20 #include "linechartitem_p.h"
21 #include "percentbarpresenter.h"
21 #include "percentbarpresenter.h"
22 #include "scatterpresenter_p.h"
22 #include "scatterpresenter_p.h"
23 #include "piepresenter.h"
23 #include "piepresenter.h"
24
24
25 //themes
25 //themes
26 #include "chartthemevanilla_p.h"
26 #include "chartthemevanilla_p.h"
27 #include "chartthemeicy_p.h"
27 #include "chartthemeicy_p.h"
28 #include "chartthemegrayscale_p.h"
28 #include "chartthemegrayscale_p.h"
29 #include "chartthemescientific_p.h"
29 #include "chartthemescientific_p.h"
30
30
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 /* TODO
34 /* TODO
35 case QChart::ChartThemeUnnamed1:
35 case QChart::ChartThemeUnnamed1:
36 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2));
36 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2));
37 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2));
37 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2));
38 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2));
38 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2));
39 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2));
39 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2));
40 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2));
40 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2));
41
41
42 m_gradientStartColor = QColor(QRgb(0xfff3dc9e));
42 m_gradientStartColor = QColor(QRgb(0xfff3dc9e));
43 m_gradientEndColor = QColor(QRgb(0xffafafaf));
43 m_gradientEndColor = QColor(QRgb(0xffafafaf));
44 */
44 */
45
45
46 ChartTheme::ChartTheme(QChart::ChartTheme id)
46 ChartTheme::ChartTheme(QChart::ChartTheme id)
47 {
47 {
48 m_id = id;
48 m_id = id;
49 m_seriesColor.append(QRgb(0xff000000));
49 m_seriesColor.append(QRgb(0xff000000));
50 m_seriesColor.append(QRgb(0xff707070));
50 m_seriesColor.append(QRgb(0xff707070));
51 m_gradientStartColor = QColor(QRgb(0xffffffff));
51 m_gradientStartColor = QColor(QRgb(0xffffffff));
52 m_gradientEndColor = QColor(QRgb(0xffafafaf));
52 m_gradientEndColor = QColor(QRgb(0xffafafaf));
53 }
53 }
54
54
55
55
56 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
56 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
57 {
57 {
58 switch(theme) {
58 switch(theme) {
59 case QChart::ChartThemeDefault:
59 case QChart::ChartThemeDefault:
60 return new ChartTheme();
60 return new ChartThemeIcy();
61 case QChart::ChartThemeVanilla:
61 case QChart::ChartThemeVanilla:
62 return new ChartThemeVanilla();
62 return new ChartThemeVanilla();
63 case QChart::ChartThemeIcy:
63 case QChart::ChartThemeIcy:
64 return new ChartThemeIcy();
64 return new ChartThemeIcy();
65 case QChart::ChartThemeGrayscale:
65 case QChart::ChartThemeGrayscale:
66 return new ChartThemeGrayscale();
66 return new ChartThemeGrayscale();
67 case QChart::ChartThemeScientific:
67 case QChart::ChartThemeScientific:
68 return new ChartThemeScientific();
68 return new ChartThemeScientific();
69 }
69 }
70 }
70 }
71
71
72 void ChartTheme::decorate(QChart* chart)
72 void ChartTheme::decorate(QChart* chart)
73 {
73 {
74 QLinearGradient backgroundGradient;
74 QLinearGradient backgroundGradient;
75 backgroundGradient.setColorAt(0.0, m_gradientStartColor);
75 backgroundGradient.setColorAt(0.0, m_gradientStartColor);
76 backgroundGradient.setColorAt(1.0, m_gradientEndColor);
76 backgroundGradient.setColorAt(1.0, m_gradientEndColor);
77 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
77 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
78 chart->setChartBackgroundBrush(backgroundGradient);
78 chart->setChartBackgroundBrush(backgroundGradient);
79 }
79 }
80 //TODO helper to by removed later
80 //TODO helper to by removed later
81 void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count)
81 void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count)
82 {
82 {
83 switch(series->type())
83 switch(series->type())
84 {
84 {
85 case QChartSeries::SeriesTypeLine: {
85 case QChartSeries::SeriesTypeLine: {
86 QLineChartSeries* s = static_cast<QLineChartSeries*>(series);
86 QLineChartSeries* s = static_cast<QLineChartSeries*>(series);
87 LineChartItem* i = static_cast<LineChartItem*>(item);
87 LineChartItem* i = static_cast<LineChartItem*>(item);
88 decorate(i,s,count);
88 decorate(i,s,count);
89 break;
89 break;
90 }
90 }
91 case QChartSeries::SeriesTypeBar: {
91 case QChartSeries::SeriesTypeBar: {
92 QBarChartSeries* b = static_cast<QBarChartSeries*>(series);
92 QBarChartSeries* b = static_cast<QBarChartSeries*>(series);
93 BarPresenter* i = static_cast<BarPresenter*>(item);
93 BarPresenter* i = static_cast<BarPresenter*>(item);
94 decorate(i,b,count);
94 decorate(i,b,count);
95 break;
95 break;
96 }
96 }
97 case QChartSeries::SeriesTypeStackedBar: {
97 case QChartSeries::SeriesTypeStackedBar: {
98 QStackedBarChartSeries* s = static_cast<QStackedBarChartSeries*>(series);
98 QStackedBarChartSeries* s = static_cast<QStackedBarChartSeries*>(series);
99 StackedBarPresenter* i = static_cast<StackedBarPresenter*>(item);
99 StackedBarPresenter* i = static_cast<StackedBarPresenter*>(item);
100 decorate(i,s,count);
100 decorate(i,s,count);
101 break;
101 break;
102 }
102 }
103 case QChartSeries::SeriesTypePercentBar: {
103 case QChartSeries::SeriesTypePercentBar: {
104 QPercentBarChartSeries* s = static_cast<QPercentBarChartSeries*>(series);
104 QPercentBarChartSeries* s = static_cast<QPercentBarChartSeries*>(series);
105 PercentBarPresenter* i = static_cast<PercentBarPresenter*>(item);
105 PercentBarPresenter* i = static_cast<PercentBarPresenter*>(item);
106 decorate(i,s,count);
106 decorate(i,s,count);
107 break;
107 break;
108 }
108 }
109 case QChartSeries::SeriesTypeScatter: {
109 case QChartSeries::SeriesTypeScatter: {
110 QScatterSeries* s = qobject_cast<QScatterSeries*>(series);
110 QScatterSeries* s = qobject_cast<QScatterSeries*>(series);
111 Q_ASSERT(s);
111 Q_ASSERT(s);
112 ScatterPresenter* i = static_cast<ScatterPresenter*>(item);
112 ScatterPresenter* i = static_cast<ScatterPresenter*>(item);
113 Q_ASSERT(i);
113 Q_ASSERT(i);
114 decorate(i, s, count);
114 decorate(i, s, count);
115 break;
115 break;
116 }
116 }
117 case QChartSeries::SeriesTypePie: {
117 case QChartSeries::SeriesTypePie: {
118 QPieSeries* s = static_cast<QPieSeries*>(series);
118 QPieSeries* s = static_cast<QPieSeries*>(series);
119 PiePresenter* i = static_cast<PiePresenter*>(item);
119 PiePresenter* i = static_cast<PiePresenter*>(item);
120 decorate(i,s,count);
120 decorate(i,s,count);
121 break;
121 break;
122 }
122 }
123 default:
123 default:
124 qDebug()<<"Wrong item to be decorated by theme";
124 qDebug()<<"Wrong item to be decorated by theme";
125 break;
125 break;
126 }
126 }
127
127
128 }
128 }
129
129
130 void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int count)
130 void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int count)
131 {
131 {
132 QPen pen;
132 QPen pen;
133 if(pen != series->pen()){
133 if(pen != series->pen()){
134 item->setPen(series->pen());
134 item->setPen(series->pen());
135 return;
135 return;
136 }
136 }
137 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
137 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
138 pen.setWidthF(2);
138 pen.setWidthF(2);
139 item->setPen(pen);
139 item->setPen(pen);
140 }
140 }
141
141
142 void ChartTheme::decorate(BarPresenter* item, QBarChartSeries* series,int count)
142 void ChartTheme::decorate(BarPresenter* item, QBarChartSeries* series,int count)
143 {
143 {
144 for (int i=0; i<series->countSets(); i++) {
144 for (int i=0; i<series->countSets(); i++) {
145 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
145 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
146 }
146 }
147 }
147 }
148
148
149 void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarChartSeries* series,int count)
149 void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarChartSeries* series,int count)
150 {
150 {
151 for (int i=0; i<series->countSets(); i++) {
151 for (int i=0; i<series->countSets(); i++) {
152 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
152 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
153 }
153 }
154 }
154 }
155
155
156 void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarChartSeries* series,int count)
156 void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarChartSeries* series,int count)
157 {
157 {
158 for (int i=0; i<series->countSets(); i++) {
158 for (int i=0; i<series->countSets(); i++) {
159 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
159 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
160 }
160 }
161 }
161 }
162
162
163 void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count)
163 void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count)
164 {
164 {
165 Q_ASSERT(presenter);
165 Q_ASSERT(presenter);
166 Q_ASSERT(series);
166 Q_ASSERT(series);
167
167
168 QColor color = m_seriesColor.at(count % m_seriesColor.size());
168 QColor color = m_seriesColor.at(count % m_seriesColor.size());
169 // TODO: define alpha in the theme? or in the series?
169 // TODO: define alpha in the theme? or in the series?
170 //color.setAlpha(120);
170 //color.setAlpha(120);
171
171
172 QBrush brush(color, Qt::SolidPattern);
172 QBrush brush(color, Qt::SolidPattern);
173 presenter->m_markerBrush = brush;
173 presenter->m_markerBrush = brush;
174
174
175 QPen pen(brush, 3);
175 QPen pen(brush, 3);
176 pen.setColor(color);
176 pen.setColor(color);
177 presenter->m_markerPen = pen;
177 presenter->m_markerPen = pen;
178 }
178 }
179
179
180 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
180 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
181 {
181 {
182 // create a list of slice colors based on current theme
182 // create a list of slice colors based on current theme
183 int i = 0;
183 int i = 0;
184 QList<QColor> colors;
184 QList<QColor> colors;
185 while (colors.count() < series->count()) {
185 while (colors.count() < series->count()) {
186
186
187 // get base color
187 // get base color
188 QColor c = m_seriesColor[i++];
188 QColor c = m_seriesColor[i++];
189 i = i % m_seriesColor.count();
189 i = i % m_seriesColor.count();
190
190
191 // -1 means achromatic color -> cannot manipulate lightness
191 // -1 means achromatic color -> cannot manipulate lightness
192 // TODO: find a better way to randomize lightness
192 // TODO: find a better way to randomize lightness
193 if (c.toHsv().hue() == -1)
193 if (c.toHsv().hue() == -1)
194 qWarning() << "ChartTheme::decorate() warning: achromatic theme color";
194 qWarning() << "ChartTheme::decorate() warning: achromatic theme color";
195
195
196 // randomize lightness
196 // randomize lightness
197 qreal f = 50 + (qrand() % 100); // 50 is 50% darker, 100 is the same, 150 is 50% lighter
197 qreal f = 50 + (qrand() % 100); // 50 is 50% darker, 100 is the same, 150 is 50% lighter
198 c = c.lighter(f);
198 c = c.lighter(f);
199
199
200 // find duplicates
200 // find duplicates
201 bool isUnique = true;
201 bool isUnique = true;
202 foreach (QColor color, colors) {
202 foreach (QColor color, colors) {
203 if (c == color)
203 if (c == color)
204 isUnique = false;
204 isUnique = false;
205 }
205 }
206
206
207 // add to array if unique
207 // add to array if unique
208 //if (isUnique)
208 //if (isUnique)
209 colors << c;
209 colors << c;
210 }
210 }
211
211
212 // finally update colors
212 // finally update colors
213 foreach (QPieSlice* s, series->slices()) {
213 foreach (QPieSlice* s, series->slices()) {
214 s->setPen(QPen(Qt::black)); // TODO: get from theme
214 s->setPen(QPen(Qt::black)); // TODO: get from theme
215 s->setBrush(colors.takeFirst());
215 s->setBrush(colors.takeFirst());
216 }
216 }
217 }
217 }
218
218
219
219
220 void ChartTheme::decorate(QChartAxis* axis,AxisItem* item)
220 void ChartTheme::decorate(QChartAxis* axis,AxisItem* item)
221 {
221 {
222 //TODO: dummy defults for now
222 //TODO: dummy defults for now
223 axis->setLabelsBrush(Qt::black);
223 axis->setLabelsBrush(Qt::black);
224 axis->setLabelsPen(Qt::NoPen);
224 axis->setLabelsPen(Qt::NoPen);
225 axis->setShadesPen(Qt::NoPen);
225 axis->setShadesPen(Qt::NoPen);
226 axis->setShadesOpacity(0.5);
226 axis->setShadesOpacity(0.5);
227 }
227 }
228
228
229 QTCOMMERCIALCHART_END_NAMESPACE
229 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,362 +1,372
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 QGraphicsScene to work. It manages the graphical
25 QChartView is a standalone widget that can display charts. It does not require separate
26 representation of different types of QChartSeries and other chart related objects like
26 QGraphicsScene to work. It manages the graphical representation of different types of
27 QChartAxis and QChartLegend. If you want to display a chart in your existing QGraphicsScene, you can use the QChart class instead.
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
29
29 For example, to create an empty chart in a widget based application:
30 For example, to create a chart with line series using a widget based application:
30 \snippet ../example/chartview/main.cpp 1
31 \snippet ../example/chartview/main.cpp 1
31 \image chartview_example.jpg
32 \image chartview_example.jpg
32
33
33 To add a line series:
34 Showing a few more series:
34 \snippet ../example/chartview/main.cpp 2
35 \image chartview_example_series.jpg
36
37 To modify the visual appearance of the chart, you can use the pre-defined themes:
38 \snippet ../example/chartview/main.cpp 3
35 \snippet ../example/chartview/main.cpp 3
36 \codeline
37 \snippet ../example/chartview/main.cpp 4
38 \codeline
39 \snippet ../example/chartview/main.cpp 5
40
41 And the corresponding results:
42 \image chartview_example_pie.jpg
43 \image chartview_example_scatter.jpg
44 \image chartview_example_bar.jpg
45
46 If you need to give a more professional touch to your chart you can switch to one of the
47 pre-defined themes:
48 \snippet ../example/chartview/main.cpp 2
39 \image chartview_example_theme.jpg
49 \image chartview_example_theme.jpg
40
50
41 \sa QChart
51 \sa QChart
42 */
52 */
43
53
44 QTCOMMERCIALCHART_BEGIN_NAMESPACE
54 QTCOMMERCIALCHART_BEGIN_NAMESPACE
45
55
46 /*!
56 /*!
47 Constructs a chartView object which is a child of a\a parent.
57 Constructs a chartView object which is a child of a\a parent.
48 */
58 */
49 QChartView::QChartView(QWidget *parent) :
59 QChartView::QChartView(QWidget *parent) :
50 QGraphicsView(parent),
60 QGraphicsView(parent),
51 m_scene(new QGraphicsScene(this)),
61 m_scene(new QGraphicsScene(this)),
52 m_chart(new QChart()),
62 m_chart(new QChart()),
53 m_rubberBand(0),
63 m_rubberBand(0),
54 m_verticalRubberBand(false),
64 m_verticalRubberBand(false),
55 m_horizonalRubberBand(false)
65 m_horizonalRubberBand(false)
56 {
66 {
57 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
67 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
58 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
68 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
59 setScene(m_scene);
69 setScene(m_scene);
60 m_chart->setMargin(50);
70 m_chart->setMargin(50);
61 m_scene->addItem(m_chart);
71 m_scene->addItem(m_chart);
62 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
72 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
63 }
73 }
64
74
65
75
66 /*!
76 /*!
67 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
77 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
68 */
78 */
69 QChartView::~QChartView()
79 QChartView::~QChartView()
70 {
80 {
71 }
81 }
72
82
73 /*!
83 /*!
74 Resizes and updates the chart area using the \a event data
84 Resizes and updates the chart area using the \a event data
75 */
85 */
76 void QChartView::resizeEvent(QResizeEvent *event)
86 void QChartView::resizeEvent(QResizeEvent *event)
77 {
87 {
78 m_scene->setSceneRect(0,0,size().width(),size().height());
88 m_scene->setSceneRect(0,0,size().width(),size().height());
79 m_chart->resize(size());
89 m_chart->resize(size());
80 QWidget::resizeEvent(event);
90 QWidget::resizeEvent(event);
81 }
91 }
82
92
83 /*!
93 /*!
84 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
94 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
85 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
95 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
86 the y axis).
96 the y axis).
87 \sa removeSeries(), removeAllSeries()
97 \sa removeSeries(), removeAllSeries()
88 */
98 */
89 void QChartView::addSeries(QChartSeries* series,QChartAxis *axisY)
99 void QChartView::addSeries(QChartSeries* series,QChartAxis *axisY)
90 {
100 {
91 m_chart->addSeries(series,axisY);
101 m_chart->addSeries(series,axisY);
92 }
102 }
93
103
94 /*!
104 /*!
95 Removes the \a series specified in a perameter from the QChartView.
105 Removes the \a series specified in a perameter from the QChartView.
96 It releses its ownership of the specified QChartSeries object.
106 It releses its ownership of the specified QChartSeries object.
97 It does not delete the pointed QChartSeries data object
107 It does not delete the pointed QChartSeries data object
98 \sa addSeries(), removeAllSeries()
108 \sa addSeries(), removeAllSeries()
99 */
109 */
100 void QChartView::removeSeries(QChartSeries* series)
110 void QChartView::removeSeries(QChartSeries* series)
101 {
111 {
102 m_chart->removeSeries(series);
112 m_chart->removeSeries(series);
103 }
113 }
104
114
105 /*!
115 /*!
106 Removes all the QChartSeries that have been added to the QChartView
116 Removes all the QChartSeries that have been added to the QChartView
107 It also deletes the pointed QChartSeries data objects
117 It also deletes the pointed QChartSeries data objects
108 \sa addSeries(), removeSeries()
118 \sa addSeries(), removeSeries()
109 */
119 */
110 void QChartView::removeAllSeries()
120 void QChartView::removeAllSeries()
111 {
121 {
112 m_chart->removeAllSeries();
122 m_chart->removeAllSeries();
113 }
123 }
114
124
115 /*!
125 /*!
116 Zooms in the view by a factor of 2
126 Zooms in the view by a factor of 2
117 */
127 */
118 void QChartView::zoomIn()
128 void QChartView::zoomIn()
119 {
129 {
120 m_chart->zoomIn();
130 m_chart->zoomIn();
121 }
131 }
122
132
123 /*!
133 /*!
124 Zooms in the view to a maximum level at which \a rect is still fully visible.
134 Zooms in the view to a maximum level at which \a rect is still fully visible.
125 */
135 */
126 void QChartView::zoomIn(const QRect& rect)
136 void QChartView::zoomIn(const QRect& rect)
127 {
137 {
128 m_chart->zoomIn(rect);
138 m_chart->zoomIn(rect);
129 }
139 }
130
140
131 /*!
141 /*!
132 Restores the view zoom level to the previous one.
142 Restores the view zoom level to the previous one.
133 */
143 */
134 void QChartView::zoomOut()
144 void QChartView::zoomOut()
135 {
145 {
136 m_chart->zoomOut();
146 m_chart->zoomOut();
137 }
147 }
138
148
139 /*!
149 /*!
140 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.
150 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.
141 */
151 */
142 int QChartView::margin() const
152 int QChartView::margin() const
143 {
153 {
144 return m_chart->margin();
154 return m_chart->margin();
145 }
155 }
146
156
147 /*!
157 /*!
148 Sets the chart \a title. A description text that is rendered above the chart.
158 Sets the chart \a title. A description text that is rendered above the chart.
149 */
159 */
150 void QChartView::setChartTitle(const QString& title)
160 void QChartView::setChartTitle(const QString& title)
151 {
161 {
152 m_chart->setChartTitle(title);
162 m_chart->setChartTitle(title);
153 }
163 }
154
164
155 /*!
165 /*!
156 Sets the \a font that is used for rendering the description text that is rendered above the chart.
166 Sets the \a font that is used for rendering the description text that is rendered above the chart.
157 */
167 */
158 void QChartView::setChartTitleFont(const QFont& font)
168 void QChartView::setChartTitleFont(const QFont& font)
159 {
169 {
160 m_chart->setChartTitleFont(font);
170 m_chart->setChartTitleFont(font);
161 }
171 }
162
172
163 /*!
173 /*!
164 Sets the \a brush that is used for painting the background of the chart area of the QChartView widget.
174 Sets the \a brush that is used for painting the background of the chart area of the QChartView widget.
165 */
175 */
166 void QChartView::setChartBackgroundBrush(const QBrush& brush)
176 void QChartView::setChartBackgroundBrush(const QBrush& brush)
167 {
177 {
168 m_chart->setChartBackgroundBrush(brush);
178 m_chart->setChartBackgroundBrush(brush);
169 }
179 }
170
180
171 /*!
181 /*!
172 Sets the \a pen that is used for painting the background of the chart area of the QChartView widget.
182 Sets the \a pen that is used for painting the background of the chart area of the QChartView widget.
173 */
183 */
174 void QChartView::setChartBackgroundPen(const QPen& pen)
184 void QChartView::setChartBackgroundPen(const QPen& pen)
175 {
185 {
176 m_chart->setChartBackgroundPen(pen);
186 m_chart->setChartBackgroundPen(pen);
177 }
187 }
178
188
179 /*!
189 /*!
180 Sets the RubberBandPlicy to \a policy. Selected policy determines the way zooming is performed.
190 Sets the RubberBandPlicy to \a policy. Selected policy determines the way zooming is performed.
181 */
191 */
182 void QChartView::setRubberBandPolicy(const RubberBandPolicy policy)
192 void QChartView::setRubberBandPolicy(const RubberBandPolicy policy)
183 {
193 {
184 switch(policy) {
194 switch(policy) {
185 case VerticalRubberBand:
195 case VerticalRubberBand:
186 m_verticalRubberBand = true;
196 m_verticalRubberBand = true;
187 m_horizonalRubberBand = false;
197 m_horizonalRubberBand = false;
188 break;
198 break;
189 case HorizonalRubberBand:
199 case HorizonalRubberBand:
190 m_verticalRubberBand = false;
200 m_verticalRubberBand = false;
191 m_horizonalRubberBand = true;
201 m_horizonalRubberBand = true;
192 break;
202 break;
193 case RectangleRubberBand:
203 case RectangleRubberBand:
194 m_verticalRubberBand = true;
204 m_verticalRubberBand = true;
195 m_horizonalRubberBand = true;
205 m_horizonalRubberBand = true;
196 break;
206 break;
197 case NoRubberBand:
207 case NoRubberBand:
198 default:
208 default:
199 delete m_rubberBand;
209 delete m_rubberBand;
200 m_rubberBand=0;
210 m_rubberBand=0;
201 m_horizonalRubberBand = false;
211 m_horizonalRubberBand = false;
202 m_verticalRubberBand = false;
212 m_verticalRubberBand = false;
203 return;
213 return;
204 }
214 }
205 if(!m_rubberBand) {
215 if(!m_rubberBand) {
206 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
216 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
207 m_rubberBand->setEnabled(true);
217 m_rubberBand->setEnabled(true);
208 }
218 }
209 }
219 }
210
220
211 /*!
221 /*!
212 Returns the RubberBandPolicy that is currently being used by the widget.
222 Returns the RubberBandPolicy that is currently being used by the widget.
213 */
223 */
214 QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const
224 QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const
215 {
225 {
216 if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand;
226 if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand;
217 if(m_horizonalRubberBand) return HorizonalRubberBand;
227 if(m_horizonalRubberBand) return HorizonalRubberBand;
218 if(m_verticalRubberBand) return VerticalRubberBand;
228 if(m_verticalRubberBand) return VerticalRubberBand;
219 return NoRubberBand;
229 return NoRubberBand;
220 }
230 }
221
231
222 /*!
232 /*!
223 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.
233 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.
224 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation is called.
234 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation is called.
225 */
235 */
226 void QChartView::mousePressEvent(QMouseEvent *event)
236 void QChartView::mousePressEvent(QMouseEvent *event)
227 {
237 {
228 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
238 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
229
239
230 int margin = m_chart->margin();
240 int margin = m_chart->margin();
231 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
241 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
232
242
233 if (rect.contains(event->pos())) {
243 if (rect.contains(event->pos())) {
234 m_rubberBandOrigin = event->pos();
244 m_rubberBandOrigin = event->pos();
235 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize()));
245 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize()));
236 m_rubberBand->show();
246 m_rubberBand->show();
237 event->accept();
247 event->accept();
238 }
248 }
239 }
249 }
240 else {
250 else {
241 QGraphicsView::mousePressEvent(event);
251 QGraphicsView::mousePressEvent(event);
242 }
252 }
243 }
253 }
244
254
245 /*!
255 /*!
246 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
256 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
247 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
257 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
248 */
258 */
249 void QChartView::mouseMoveEvent(QMouseEvent *event)
259 void QChartView::mouseMoveEvent(QMouseEvent *event)
250 {
260 {
251 if(m_rubberBand && m_rubberBand->isVisible()) {
261 if(m_rubberBand && m_rubberBand->isVisible()) {
252 int margin = m_chart->margin();
262 int margin = m_chart->margin();
253 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
263 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
254 int width = event->pos().x() - m_rubberBandOrigin.x();
264 int width = event->pos().x() - m_rubberBandOrigin.x();
255 int height = event->pos().y() - m_rubberBandOrigin.y();
265 int height = event->pos().y() - m_rubberBandOrigin.y();
256 if(!m_verticalRubberBand) {
266 if(!m_verticalRubberBand) {
257 m_rubberBandOrigin.setY(rect.top());
267 m_rubberBandOrigin.setY(rect.top());
258 height = rect.height();
268 height = rect.height();
259 }
269 }
260 if(!m_horizonalRubberBand) {
270 if(!m_horizonalRubberBand) {
261 m_rubberBandOrigin.setX(rect.left());
271 m_rubberBandOrigin.setX(rect.left());
262 width= rect.width();
272 width= rect.width();
263 }
273 }
264 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized());
274 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized());
265 }
275 }
266 else {
276 else {
267 QGraphicsView::mouseMoveEvent(event);
277 QGraphicsView::mouseMoveEvent(event);
268 }
278 }
269 }
279 }
270
280
271 /*!
281 /*!
272 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
282 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
273 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
283 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
274 */
284 */
275 void QChartView::mouseReleaseEvent(QMouseEvent *event)
285 void QChartView::mouseReleaseEvent(QMouseEvent *event)
276 {
286 {
277 if(m_rubberBand) {
287 if(m_rubberBand) {
278 if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) {
288 if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) {
279 m_rubberBand->hide();
289 m_rubberBand->hide();
280 QRect rect = m_rubberBand->geometry();
290 QRect rect = m_rubberBand->geometry();
281 m_chart->zoomIn(rect);
291 m_chart->zoomIn(rect);
282 event->accept();
292 event->accept();
283 }
293 }
284
294
285 if(event->button()==Qt::RightButton)
295 if(event->button()==Qt::RightButton)
286 m_chart->zoomReset();
296 m_chart->zoomReset();
287 }
297 }
288 else {
298 else {
289 QGraphicsView::mouseReleaseEvent(event);
299 QGraphicsView::mouseReleaseEvent(event);
290 }
300 }
291 }
301 }
292
302
293 /*!
303 /*!
294 Pressing + and - keys performs zoomIn() and zoomOut() respectivly.
304 Pressing + and - keys performs zoomIn() and zoomOut() respectivly.
295 In other \a event is passed to the QGraphicsView::keyPressEvent() implementation
305 In other \a event is passed to the QGraphicsView::keyPressEvent() implementation
296 */
306 */
297 void QChartView::keyPressEvent(QKeyEvent *event)
307 void QChartView::keyPressEvent(QKeyEvent *event)
298 {
308 {
299 switch (event->key()) {
309 switch (event->key()) {
300 case Qt::Key_Plus:
310 case Qt::Key_Plus:
301 zoomIn();
311 zoomIn();
302 break;
312 break;
303 case Qt::Key_Minus:
313 case Qt::Key_Minus:
304 zoomOut();
314 zoomOut();
305 break;
315 break;
306 default:
316 default:
307 QGraphicsView::keyPressEvent(event);
317 QGraphicsView::keyPressEvent(event);
308 break;
318 break;
309 }
319 }
310 }
320 }
311
321
312 /*!
322 /*!
313 Sets the \a theme used by the chart for rendering the graphical representation of the data
323 Sets the \a theme used by the chart for rendering the graphical representation of the data
314 \sa QChart::ChartTheme, chartTheme()
324 \sa QChart::ChartTheme, chartTheme()
315 */
325 */
316 void QChartView::setChartTheme(QChart::ChartTheme theme)
326 void QChartView::setChartTheme(QChart::ChartTheme theme)
317 {
327 {
318 m_chart->setChartTheme(theme);
328 m_chart->setChartTheme(theme);
319 }
329 }
320
330
321 /*!
331 /*!
322 Returns the theme enum used by the chart.
332 Returns the theme enum used by the chart.
323 \sa setChartTheme()
333 \sa setChartTheme()
324 */
334 */
325 QChart::ChartTheme QChartView::chartTheme() const
335 QChart::ChartTheme QChartView::chartTheme() const
326 {
336 {
327 return m_chart->chartTheme();
337 return m_chart->chartTheme();
328 }
338 }
329
339
330 /*!
340 /*!
331 Returns the pointer to the x axis object of the chart
341 Returns the pointer to the x axis object of the chart
332 */
342 */
333 QChartAxis* QChartView::axisX() const
343 QChartAxis* QChartView::axisX() const
334 {
344 {
335 return m_chart->axisX();
345 return m_chart->axisX();
336 }
346 }
337
347
338 /*!
348 /*!
339 Returns the pointer to the y axis object of the chart
349 Returns the pointer to the y axis object of the chart
340 */
350 */
341 QChartAxis* QChartView::axisY() const
351 QChartAxis* QChartView::axisY() const
342 {
352 {
343 return m_chart->axisY();
353 return m_chart->axisY();
344 }
354 }
345
355
346 /*!
356 /*!
347 Sets animation \a options for the chart
357 Sets animation \a options for the chart
348 */
358 */
349 void QChartView::setAnimationOptions(QChart::AnimationOptions options)
359 void QChartView::setAnimationOptions(QChart::AnimationOptions options)
350 {
360 {
351 m_chart->setAnimationOptions(options);
361 m_chart->setAnimationOptions(options);
352 }
362 }
353
363
354 /*!
364 /*!
355 Returns animation options for the chart
365 Returns animation options for the chart
356 */
366 */
357 QChart::AnimationOptions QChartView::animationOptions() const
367 QChart::AnimationOptions QChartView::animationOptions() const
358 {
368 {
359 return m_chart->animationOptions();
369 return m_chart->animationOptions();
360 }
370 }
361
371
362 QTCOMMERCIALCHART_END_NAMESPACE
372 QTCOMMERCIALCHART_END_NAMESPACE
1 NO CONTENT: file was removed, binary diff hidden
NO CONTENT: file was removed, binary diff hidden
General Comments 0
You need to be logged in to leave comments. Login now