##// 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
@@ -161,6 +161,18 int QXYSeries::count() const
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()
@@ -11,47 +11,45 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
General Comments 0
You need to be logged in to leave comments. Login now