##// END OF EJS Templates
Added data getter for XY series; example for customizing the looks
Tero Ahola -
r491:4417e87c4247
parent child
Show More
@@ -0,0 +1,11
1 !include( ../examples.pri ) {
2 error( "Couldn't find the examples.pri file!" )
3 }
4
5 TARGET = customcolors
6 TEMPLATE = app
7
8 SOURCES += main.cpp\
9 mainwindow.cpp
10
11 HEADERS += mainwindow.h
@@ -0,0 +1,11
1 #include <QtGui/QApplication>
2 #include "mainwindow.h"
3
4 int main(int argc, char *argv[])
5 {
6 QApplication a(argc, argv);
7 MainWindow w;
8 w.show();
9
10 return a.exec();
11 }
@@ -0,0 +1,95
1 #include "mainwindow.h"
2 #include <qchartview.h>
3 #include <qpieseries.h>
4 #include <qpieslice.h>
5 #include <qlineseries.h>
6 #include <qscatterseries.h>
7 #include <qchartaxis.h>
8 #include <QDebug>
9
10 QTCOMMERCIALCHART_USE_NAMESPACE
11
12 MainWindow::MainWindow(QWidget *parent)
13 : QMainWindow(parent)
14 {
15 // Here's the set of company's colors used throughout the example
16 m_companyColor1 = "#b90020";
17 m_companyColor2 = "#6d0013";
18 m_companyColor3 = "#d5d5d5";
19 m_companyColor4 = "#fcfcfc";
20
21 resize(400, 300);
22 //setWindowFlags(Qt::FramelessWindowHint);
23
24 // Create chart view
25 m_chartView = new QChartView(this);
26 setCentralWidget(m_chartView);
27 m_chartView->setChartTitle("Custom colors example");
28 m_chartView->setRenderHint(QPainter::Antialiasing);
29
30 // Create line series
31 m_line = new QLineSeries();
32 m_line->add(0.0, 1.1);
33 m_line->add(1.0, 2.3);
34 m_line->add(2.0, 2.1);
35 m_line->add(3.0, 3.3);
36 m_chartView->addSeries(m_line);
37
38 // Create scatter series with the same data
39 m_scatter = new QScatterSeries();
40 m_scatter->add(m_line->data());
41 m_chartView->addSeries(m_scatter);
42
43 // Create pie series using color 2; use different fill styles for each pie slice
44 m_pie = new QPieSeries();
45 m_pie->add(1.1, "1");
46 m_pie->add(2.1, "2");
47 m_pie->add(3.0, "3");
48 m_pie->setPositionFactors(0.7, 0.7);
49 m_pie->setSizeFactor(0.5);
50 m_chartView->addSeries(m_pie);
51
52 connect(&m_timer, SIGNAL(timeout()), this, SLOT(customize()));
53 m_timer.setInterval(1500);
54 m_timer.setSingleShot(false);
55 m_timer.start();
56 }
57
58 MainWindow::~MainWindow()
59 {
60 }
61
62 void MainWindow::customize()
63 {
64 // Customize chart background
65 // Use a gradient from color 3 to color 4 for chart background
66 QLinearGradient chartGradient(0, 0, 0, 300);
67 chartGradient.setColorAt(0.0, m_companyColor3);
68 chartGradient.setColorAt(0.5, m_companyColor4);
69 chartGradient.setColorAt(1.0, m_companyColor3);
70 m_chartView->setChartBackgroundBrush(chartGradient);
71 m_chartView->setBackgroundBrush(m_companyColor4);
72
73 // Customize chart axis
74 QPen color1Pen(m_companyColor1, 4.0);
75 m_chartView->axisX()->setAxisPen(color1Pen);
76 m_chartView->axisY()->setAxisPen(color1Pen);
77
78 // Customize series
79 m_line->setPen(color1Pen);
80 m_scatter->setPen(color1Pen);
81 m_scatter->setBrush(m_companyColor3);
82 for (int i(0); i < m_pie->slices().count(); i++) {
83 Qt::BrushStyle style = static_cast<Qt::BrushStyle>(i + 1);
84 m_pie->slices().at(i)->setSliceBrush(QBrush(m_companyColor2, style));
85 m_pie->slices().at(i)->setSlicePen(color1Pen);
86 }
87
88 // Calculate new colors to be used on the next update for the series
89 m_companyColor1.setRed((m_companyColor1.red() + 25) % 255);
90 m_companyColor1.setGreen((m_companyColor1.green() + 25) % 255);
91 m_companyColor1.setBlue((m_companyColor1.blue() + 25) % 255);
92 m_companyColor2.setRed((m_companyColor2.red() + 25) % 255);
93 m_companyColor2.setGreen((m_companyColor2.green() + 25) % 255);
94 m_companyColor2.setBlue((m_companyColor2.blue() + 25) % 255);
95 }
@@ -0,0 +1,40
1 #ifndef MAINWINDOW_H
2 #define MAINWINDOW_H
3
4 #include <qchartglobal.h>
5 #include <QtGui/QMainWindow>
6 #include <QTimer>
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 class QChartView;
10 class QLineSeries;
11 class QScatterSeries;
12 class QPieSeries;
13 QTCOMMERCIALCHART_END_NAMESPACE
14
15 QTCOMMERCIALCHART_USE_NAMESPACE
16
17 class MainWindow : public QMainWindow
18 {
19 Q_OBJECT
20
21 public:
22 MainWindow(QWidget *parent = 0);
23 ~MainWindow();
24
25 private Q_SLOTS:
26 void customize();
27
28 private:
29 QColor m_companyColor1;
30 QColor m_companyColor2;
31 QColor m_companyColor3;
32 QColor m_companyColor4;
33 QChartView *m_chartView;
34 QLineSeries *m_line;
35 QScatterSeries *m_scatter;
36 QPieSeries *m_pie;
37 QTimer m_timer;
38 };
39
40 #endif // MAINWINDOW_H
@@ -1,217 +1,229
1 #include "qxyseries.h"
1 #include "qxyseries.h"
2
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
4
5 /*!
5 /*!
6 \class QXYSeries
6 \class QXYSeries
7 \brief The QXYSeries class is a base class for line, spline and scatter series.
7 \brief The QXYSeries class is a base class for line, spline and scatter series.
8 */
8 */
9
9
10 /*!
10 /*!
11 \fn QPen QXYSeries::pen() const
11 \fn QPen QXYSeries::pen() const
12 \brief Returns pen used to draw points for series.
12 \brief Returns pen used to draw points for series.
13 \sa setPen()
13 \sa setPen()
14 */
14 */
15
15
16 /*!
16 /*!
17 \fn QBrush QXYSeries::brush() const
17 \fn QBrush QXYSeries::brush() const
18 \brief Returns brush used to draw points for series.
18 \brief Returns brush used to draw points for series.
19 \sa setBrush()
19 \sa setBrush()
20 */
20 */
21
21
22 /*!
22 /*!
23 \fn void QXYSeries::pointReplaced(int index)
23 \fn void QXYSeries::pointReplaced(int index)
24 \brief \internal \a index
24 \brief \internal \a index
25 */
25 */
26
26
27 /*!
27 /*!
28 \fn void QXYSeries::pointAdded(int index)
28 \fn void QXYSeries::pointAdded(int index)
29 \brief \internal \a index
29 \brief \internal \a index
30 */
30 */
31
31
32 /*!
32 /*!
33 \fn void QXYSeries::pointRemoved(int index)
33 \fn void QXYSeries::pointRemoved(int index)
34 \brief \internal \a index
34 \brief \internal \a index
35 */
35 */
36
36
37 /*!
37 /*!
38 \fn void QXYSeries::updated()
38 \fn void QXYSeries::updated()
39 \brief \internal
39 \brief \internal
40 */
40 */
41
41
42 /*!
42 /*!
43 Constructs empty series object which is a child of \a parent.
43 Constructs empty series object which is a child of \a parent.
44 When series object is added to QChartView or QChart instance ownerships is transfered.
44 When series object is added to QChartView or QChart instance ownerships is transfered.
45 */
45 */
46 QXYSeries::QXYSeries(QObject* parent):QSeries(parent)
46 QXYSeries::QXYSeries(QObject* parent):QSeries(parent)
47 {
47 {
48 }
48 }
49 /*!
49 /*!
50 Destroys the object. Series added to QChartView or QChart instances are owned by those,
50 Destroys the object. Series added to QChartView or QChart instances are owned by those,
51 and are deleted when mentioned object are destroyed.
51 and are deleted when mentioned object are destroyed.
52 */
52 */
53 QXYSeries::~QXYSeries()
53 QXYSeries::~QXYSeries()
54 {
54 {
55 }
55 }
56
56
57 /*!
57 /*!
58 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
58 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
59 */
59 */
60 void QXYSeries::add(qreal x,qreal y)
60 void QXYSeries::add(qreal x,qreal y)
61 {
61 {
62 Q_ASSERT(m_x.size() == m_y.size());
62 Q_ASSERT(m_x.size() == m_y.size());
63 m_x<<x;
63 m_x<<x;
64 m_y<<y;
64 m_y<<y;
65 emit pointAdded(m_x.size()-1);
65 emit pointAdded(m_x.size()-1);
66 }
66 }
67
67
68 /*!
68 /*!
69 This is an overloaded function.
69 This is an overloaded function.
70 Adds data \a point to the series. Points are connected with lines on the chart.
70 Adds data \a point to the series. Points are connected with lines on the chart.
71 */
71 */
72 void QXYSeries::add(const QPointF& point)
72 void QXYSeries::add(const QPointF& point)
73 {
73 {
74 add(point.x(),point.y());
74 add(point.x(),point.y());
75 }
75 }
76
76
77 /*!
77 /*!
78 This is an overloaded function.
78 This is an overloaded function.
79 Adds list of data \a points to the series. Points are connected with lines on the chart.
79 Adds list of data \a points to the series. Points are connected with lines on the chart.
80 */
80 */
81 void QXYSeries::add(const QList<QPointF> points)
81 void QXYSeries::add(const QList<QPointF> points)
82 {
82 {
83 foreach(const QPointF& point , points) {
83 foreach(const QPointF& point , points) {
84 add(point.x(),point.y());
84 add(point.x(),point.y());
85 }
85 }
86 }
86 }
87
87
88 /*!
88 /*!
89 Modifies \a y value for given \a x a value.
89 Modifies \a y value for given \a x a value.
90 */
90 */
91 void QXYSeries::replace(qreal x,qreal y)
91 void QXYSeries::replace(qreal x,qreal y)
92 {
92 {
93 int index = m_x.indexOf(x);
93 int index = m_x.indexOf(x);
94 m_x[index]=x;
94 m_x[index]=x;
95 m_y[index]=y;
95 m_y[index]=y;
96 emit pointReplaced(index);
96 emit pointReplaced(index);
97 }
97 }
98
98
99 /*!
99 /*!
100 This is an overloaded function.
100 This is an overloaded function.
101 Replaces current y value of for given \a point x value with \a point y value.
101 Replaces current y value of for given \a point x value with \a point y value.
102 */
102 */
103 void QXYSeries::replace(const QPointF& point)
103 void QXYSeries::replace(const QPointF& point)
104 {
104 {
105 replace(point.x(),point.y());
105 replace(point.x(),point.y());
106 }
106 }
107
107
108 /*!
108 /*!
109 Removes current \a x and y value.
109 Removes current \a x and y value.
110 */
110 */
111 void QXYSeries::remove(qreal x)
111 void QXYSeries::remove(qreal x)
112 {
112 {
113 int index = m_x.indexOf(x);
113 int index = m_x.indexOf(x);
114 emit pointRemoved(index);
114 emit pointRemoved(index);
115 m_x.remove(index);
115 m_x.remove(index);
116 m_y.remove(index);
116 m_y.remove(index);
117 }
117 }
118
118
119 /*!
119 /*!
120 Removes current \a point x value. Note \a point y value is ignored.
120 Removes current \a point x value. Note \a point y value is ignored.
121 */
121 */
122 void QXYSeries::remove(const QPointF& point)
122 void QXYSeries::remove(const QPointF& point)
123 {
123 {
124 remove(point.x());
124 remove(point.x());
125 }
125 }
126
126
127 /*!
127 /*!
128 Removes all data points from the series.
128 Removes all data points from the series.
129 */
129 */
130 void QXYSeries::removeAll()
130 void QXYSeries::removeAll()
131 {
131 {
132 m_x.clear();
132 m_x.clear();
133 m_y.clear();
133 m_y.clear();
134 }
134 }
135
135
136 /*!
136 /*!
137 \internal \a pos
137 \internal \a pos
138 */
138 */
139 qreal QXYSeries::x(int pos) const
139 qreal QXYSeries::x(int pos) const
140 {
140 {
141 return m_x.at(pos);
141 return m_x.at(pos);
142 }
142 }
143
143
144 /*!
144 /*!
145 \internal \a pos
145 \internal \a pos
146 */
146 */
147 qreal QXYSeries::y(int pos) const
147 qreal QXYSeries::y(int pos) const
148 {
148 {
149 return m_y.at(pos);
149 return m_y.at(pos);
150 }
150 }
151
151
152 /*!
152 /*!
153 Returns number of data points within series.
153 Returns number of data points within series.
154 */
154 */
155 int QXYSeries::count() const
155 int QXYSeries::count() const
156 {
156 {
157 Q_ASSERT(m_x.size() == m_y.size());
157 Q_ASSERT(m_x.size() == m_y.size());
158
158
159 return m_x.size();
159 return m_x.size();
160
160
161 }
161 }
162
162
163 /*!
163 /*!
164 Returns the data points of the series.
165 */
166 QList<QPointF> QXYSeries::data()
167 {
168 QList<QPointF> data;
169 for (int i(0); i < m_x.count() && i < m_y.count(); i++)
170 data.append(QPointF(m_x.at(i), m_y.at(i)));
171 return data;
172 }
173
174
175 /*!
164 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
176 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
165 pen from chart theme is used.
177 pen from chart theme is used.
166 \sa QChart::setChartTheme()
178 \sa QChart::setChartTheme()
167 */
179 */
168 void QXYSeries::setPen(const QPen& pen)
180 void QXYSeries::setPen(const QPen& pen)
169 {
181 {
170 if(pen!=m_pen){
182 if(pen!=m_pen){
171 m_pen=pen;
183 m_pen=pen;
172 emit updated();
184 emit updated();
173 }
185 }
174 }
186 }
175
187
176 /*!
188 /*!
177 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
189 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
178 from chart theme setting is used.
190 from chart theme setting is used.
179 \sa QChart::setChartTheme()
191 \sa QChart::setChartTheme()
180 */
192 */
181
193
182 void QXYSeries::setBrush(const QBrush& brush)
194 void QXYSeries::setBrush(const QBrush& brush)
183 {
195 {
184 if(brush!=m_brush){
196 if(brush!=m_brush){
185 m_brush=brush;
197 m_brush=brush;
186 emit updated();
198 emit updated();
187 }
199 }
188 }
200 }
189
201
190
202
191 /*!
203 /*!
192 Stream operator for adding a data \a point to the series.
204 Stream operator for adding a data \a point to the series.
193 \sa add()
205 \sa add()
194 */
206 */
195
207
196 QXYSeries& QXYSeries::operator<< (const QPointF &point)
208 QXYSeries& QXYSeries::operator<< (const QPointF &point)
197 {
209 {
198 add(point);
210 add(point);
199 return *this;
211 return *this;
200 }
212 }
201
213
202
214
203 /*!
215 /*!
204 Stream operator for adding a list of \a points to the series.
216 Stream operator for adding a list of \a points to the series.
205 \sa add()
217 \sa add()
206 */
218 */
207
219
208 QXYSeries& QXYSeries::operator<< (const QList<QPointF> points)
220 QXYSeries& QXYSeries::operator<< (const QList<QPointF> points)
209 {
221 {
210 add(points);
222 add(points);
211 return *this;
223 return *this;
212 }
224 }
213
225
214
226
215 #include "moc_qxyseries.cpp"
227 #include "moc_qxyseries.cpp"
216
228
217 QTCOMMERCIALCHART_END_NAMESPACE
229 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,59 +1,57
1 #ifndef QXYSERIES_H_
1 #ifndef QXYSERIES_H_
2 #define QXYSERIES_H_
2 #define QXYSERIES_H_
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qseries.h"
5 #include "qseries.h"
6 #include <QDebug>
6 #include <QDebug>
7 #include <QPen>
7 #include <QPen>
8 #include <QBrush>
8 #include <QBrush>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class QTCOMMERCIALCHART_EXPORT QXYSeries : public QSeries
12 class QTCOMMERCIALCHART_EXPORT QXYSeries : public QSeries
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 protected:
15 protected:
16 QXYSeries(QObject* parent=0);
16 QXYSeries(QObject* parent=0);
17 virtual ~QXYSeries();
17 virtual ~QXYSeries();
18
18
19 public:
19 public:
20 void add(qreal x, qreal y);
20 void add(qreal x, qreal y);
21 void add(const QPointF& point);
21 void add(const QPointF& point);
22 void add(const QList<QPointF> points);
22 void add(const QList<QPointF> points);
23 void replace(qreal x,qreal y);
23 void replace(qreal x,qreal y);
24 void replace(const QPointF& point);
24 void replace(const QPointF& point);
25 void remove(qreal x);
25 void remove(qreal x);
26 void remove(const QPointF& point);
26 void remove(const QPointF& point);
27 void removeAll();
27 void removeAll();
28
28
29 int count() const;
29 int count() const;
30 qreal x(int pos) const;
30 qreal x(int pos) const;
31 qreal y(int pos) const;
31 qreal y(int pos) const;
32
32 QList<QPointF> data();
33 QXYSeries& operator << (const QPointF &point);
33
34 QXYSeries& operator << (const QList<QPointF> points);
34 QXYSeries& operator << (const QPointF &point);
35
35 QXYSeries& operator << (const QList<QPointF> points);
36 void setPen(const QPen& pen);
36
37 QPen pen() const {return m_pen;}
37 void setPen(const QPen& pen);
38 QPen pen() const {return m_pen;}
38 void setBrush(const QBrush& pen);
39 void setBrush(const QBrush& pen);
39 QBrush brush() const {return m_brush;}
40 QBrush brush() const {return m_brush;}
40
41
41 signals:
42 signals:
42 void updated();
43 void updated();
43 void pointReplaced(int index);
44 void pointReplaced(int index);
44 void pointRemoved(int index);
45 void pointRemoved(int index);
45 void pointAdded(int index);
46 void pointAdded(int index);
46
47
47
48 protected:
48 protected:
49 QVector<qreal> m_x;
49 QVector<qreal> m_x;
50 QVector<qreal> m_y;
50 QVector<qreal> m_y;
51
51 QPen m_pen;
52 QPen m_pen;
52 QBrush m_brush;
53 QBrush m_brush;
54
55 };
53 };
56
54
57 QTCOMMERCIALCHART_END_NAMESPACE
55 QTCOMMERCIALCHART_END_NAMESPACE
58
56
59 #endif
57 #endif
General Comments 0
You need to be logged in to leave comments. Login now