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