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