##// END OF EJS Templates
Refactored series creation with QChart
Tero Ahola -
r61:45d3de5e814a
parent child
Show More
@@ -93,15 +93,13 void QChart::addSeries(QChartSeries* series)
93 }
93 }
94 }
94 }
95
95
96 QChartSeries* QChart::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type)
96 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
97 {
97 {
98 // TODO: support also other types; not only scatter and pie
98 // TODO: support also other types; not only scatter and pie
99 Q_ASSERT(type == QChartSeries::SeriesTypeScatter
100 || type == QChartSeries::SeriesTypePie);
101
99
102 switch (type) {
100 switch (type) {
103 case QChartSeries::SeriesTypeScatter: {
101 case QChartSeries::SeriesTypeScatter: {
104 QScatterSeries *scatterSeries = new QScatterSeries(x, y, this);
102 QScatterSeries *scatterSeries = new QScatterSeries(this);
105 connect(this, SIGNAL(sizeChanged(QRectF)),
103 connect(this, SIGNAL(sizeChanged(QRectF)),
106 scatterSeries, SLOT(chartSizeChanged(QRectF)));
104 scatterSeries, SLOT(chartSizeChanged(QRectF)));
107 scatterSeries->d->setParentItem(this);
105 scatterSeries->d->setParentItem(this);
@@ -110,12 +108,13 QChartSeries* QChart::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries:
110 case QChartSeries::SeriesTypePie: {
108 case QChartSeries::SeriesTypePie: {
111 // TODO: we now have also a list of y values as a parameter, it is ignored
109 // TODO: we now have also a list of y values as a parameter, it is ignored
112 // we should use a generic data class instead of list of x and y values
110 // we should use a generic data class instead of list of x and y values
113 QPieSeries *pieSeries = new QPieSeries(x, this);
111 QPieSeries *pieSeries = new QPieSeries(this);
114 connect(this, SIGNAL(sizeChanged(QRectF)),
112 connect(this, SIGNAL(sizeChanged(QRectF)),
115 pieSeries, SLOT(chartSizeChanged(QRectF)));
113 pieSeries, SLOT(chartSizeChanged(QRectF)));
116 return pieSeries;
114 return pieSeries;
117 }
115 }
118 default:
116 default:
117 Q_ASSERT(false);
119 break;
118 break;
120 }
119 }
121
120
@@ -36,7 +36,7 public:
36 void addSeries(QChartSeries* series);
36 void addSeries(QChartSeries* series);
37 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
37 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
38 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
38 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
39 QChartSeries* createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type);
39 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
40
40
41 virtual void setSize(const QSizeF& rect);
41 virtual void setSize(const QSizeF& rect);
42 void setMargin(int margin);
42 void setMargin(int margin);
@@ -25,11 +25,14 protected:
25 public:
25 public:
26 virtual ~QChartSeries(){};
26 virtual ~QChartSeries(){};
27
27
28 //factory method
28 // Factory method
29 static QChartSeries* create(QObject* parent = 0 ){ return 0;}
29 static QChartSeries* create(QObject* parent = 0 ){ return 0;}
30 //pure virtual
30 // Pure virtual
31 virtual QChartSeriesType type() const = 0;
31 virtual QChartSeriesType type() const = 0;
32
32
33 virtual bool setData(QList<int> data) { return false; }
34 virtual bool setData(QList<qreal> data) { return false; }
35 virtual bool setData(QList<qreal> x, QList<qreal> y){ return false; }
33 };
36 };
34
37
35 QTCOMMERCIALCHART_END_NAMESPACE
38 QTCOMMERCIALCHART_END_NAMESPACE
@@ -40,10 +40,10 void QChartView::addSeries(QChartSeries* series)
40 m_chart->addSeries(series);
40 m_chart->addSeries(series);
41 }
41 }
42
42
43 QChartSeries* QChartView::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type)
43 QChartSeries* QChartView::createSeries(QChartSeries::QChartSeriesType type)
44 {
44 {
45
45
46 return m_chart->createSeries(x, y, type);
46 return m_chart->createSeries(type);
47 }
47 }
48
48
49 void QChartView::mousePressEvent(QMouseEvent *event)
49 void QChartView::mousePressEvent(QMouseEvent *event)
@@ -21,9 +21,9 public:
21 //implement from QWidget
21 //implement from QWidget
22 void resizeEvent(QResizeEvent *event);
22 void resizeEvent(QResizeEvent *event);
23
23
24 // TODO: addSeries and createSeries are optional solutions
25 void addSeries(QChartSeries* series);
24 void addSeries(QChartSeries* series);
26 QChartSeries* createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type);
25 // Convenience function
26 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
27
27
28 protected:
28 protected:
29 void mouseMoveEvent (QMouseEvent *event);
29 void mouseMoveEvent (QMouseEvent *event);
@@ -42,9 +42,9 void QChartWidget::addSeries(QChartSeries* series)
42 m_chart->addSeries(series);
42 m_chart->addSeries(series);
43 }
43 }
44
44
45 QChartSeries* QChartWidget::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type)
45 QChartSeries* QChartWidget::createSeries(QChartSeries::QChartSeriesType type)
46 {
46 {
47 return m_chart->createSeries(x, y, type);
47 return m_chart->createSeries(type);
48 }
48 }
49 #include "moc_qchartwidget.cpp"
49 #include "moc_qchartwidget.cpp"
50
50
@@ -26,7 +26,7 public:
26 // TODO: addSeries and createSeries are optional solutions
26 // TODO: addSeries and createSeries are optional solutions
27 // TODO: currently createSeries assumes x, y value pairs. This isn't case with all charts. So is there another createSeries for other types (for example one list of ints)?
27 // TODO: currently createSeries assumes x, y value pairs. This isn't case with all charts. So is there another createSeries for other types (for example one list of ints)?
28 void addSeries(QChartSeries* series);
28 void addSeries(QChartSeries* series);
29 QChartSeries* createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type);
29 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
30
30
31 private:
31 private:
32 Q_DISABLE_COPY(QChartWidget)
32 Q_DISABLE_COPY(QChartWidget)
@@ -5,23 +5,34
5
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
7
8 QPieSeries::QPieSeries(QList<qreal> x, QGraphicsObject *parent) :
8 QPieSeries::QPieSeries(QGraphicsObject *parent) :
9 QChartSeries(parent),
9 QChartSeries(parent),
10 m_x(x),
11 m_sizeFactor(1.0)
10 m_sizeFactor(1.0)
12 {
11 {
12 }
13
14 QPieSeries::~QPieSeries()
15 {
16 while (m_slices.count())
17 delete m_slices.takeLast();
18 }
19
20 bool QPieSeries::setData(QList<qreal> data)
21 {
22 m_data = data;
23
13 // Create slices
24 // Create slices
14 qreal fullPie = 360;
25 qreal fullPie = 360;
15 qreal total = 0;
26 qreal total = 0;
16 foreach (qreal value, m_x)
27 foreach (qreal value, m_data)
17 total += value;
28 total += value;
18
29
19 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent);
30 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent());
20 Q_ASSERT(parentItem);
31 Q_ASSERT(parentItem);
21 m_chartSize = parentItem->boundingRect();
32 m_chartSize = parentItem->boundingRect();
22 qreal angle = 0;
33 qreal angle = 0;
23 // TODO: no need to create new slices in case size changed; we should re-use the existing ones
34 // TODO: no need to create new slices in case size changed; we should re-use the existing ones
24 foreach (qreal value, m_x) {
35 foreach (qreal value, m_data) {
25 qreal span = value / total * fullPie;
36 qreal span = value / total * fullPie;
26 PieSlice *slice = new PieSlice(randomColor(), angle, span, parentItem->boundingRect());
37 PieSlice *slice = new PieSlice(randomColor(), angle, span, parentItem->boundingRect());
27 slice->setParentItem(parentItem);
38 slice->setParentItem(parentItem);
@@ -30,12 +41,7 QPieSeries::QPieSeries(QList<qreal> x, QGraphicsObject *parent) :
30 }
41 }
31
42
32 resizeSlices(m_chartSize);
43 resizeSlices(m_chartSize);
33 }
44 return true;
34
35 QPieSeries::~QPieSeries()
36 {
37 while (m_slices.count())
38 delete m_slices.takeLast();
39 }
45 }
40
46
41 void QPieSeries::chartSizeChanged(QRectF chartRect)
47 void QPieSeries::chartSizeChanged(QRectF chartRect)
@@ -16,7 +16,7 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QChartSeries
16
16
17 public:
17 public:
18 // TODO: use a generic data class instead of x and y
18 // TODO: use a generic data class instead of x and y
19 QPieSeries(QList<qreal> x, QGraphicsObject *parent = 0);
19 QPieSeries(QGraphicsObject *parent = 0);
20 ~QPieSeries();
20 ~QPieSeries();
21 QColor randomColor();
21 QColor randomColor();
22 void setSizeFactor(qreal sizeFactor);
22 void setSizeFactor(qreal sizeFactor);
@@ -24,6 +24,7 public:
24
24
25 public: // from QChartSeries
25 public: // from QChartSeries
26 QChartSeriesType type() const { return QChartSeries::SeriesTypePie; }
26 QChartSeriesType type() const { return QChartSeries::SeriesTypePie; }
27 bool setData(QList<qreal> data);
27
28
28 public Q_SLOTS:
29 public Q_SLOTS:
29 void chartSizeChanged(QRectF rect);
30 void chartSizeChanged(QRectF rect);
@@ -33,7 +34,7 private:
33 //Q_DECLARE_PRIVATE(QPieSeries)
34 //Q_DECLARE_PRIVATE(QPieSeries)
34 Q_DISABLE_COPY(QPieSeries)
35 Q_DISABLE_COPY(QPieSeries)
35 // TODO: move the followin to internal impl
36 // TODO: move the followin to internal impl
36 QList<qreal> m_x;
37 QList<qreal> m_data;
37 QList<PieSlice*> m_slices;
38 QList<PieSlice*> m_slices;
38 QRectF m_chartSize;
39 QRectF m_chartSize;
39 qreal m_sizeFactor;
40 qreal m_sizeFactor;
@@ -9,14 +9,11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 //#define QSeriesData QList<qreal>
10 //#define QSeriesData QList<qreal>
11
11
12 QScatterSeriesPrivate::QScatterSeriesPrivate(QList<qreal> x, QList<qreal> y, QGraphicsItem *parent) :
12 QScatterSeriesPrivate::QScatterSeriesPrivate(QGraphicsItem *parent) :
13 QGraphicsItem(parent),
13 QGraphicsItem(parent),
14 m_x(x),
15 m_y(y),
16 m_scalex(100), // TODO: let the use define the scale (or autoscaled)
14 m_scalex(100), // TODO: let the use define the scale (or autoscaled)
17 m_scaley(100)
15 m_scaley(100)
18 {
16 {
19 resize(parent->boundingRect());
20 }
17 }
21
18
22 void QScatterSeriesPrivate::resize(QRectF rect)
19 void QScatterSeriesPrivate::resize(QRectF rect)
@@ -58,12 +55,23 void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsI
58 }
55 }
59 }
56 }
60
57
61 QScatterSeries::QScatterSeries(QList<qreal> x, QList<qreal> y, QObject *parent) :
58 QScatterSeries::QScatterSeries(QObject *parent) :
62 QChartSeries(parent),
59 QChartSeries(parent),
63 d(new QScatterSeriesPrivate(x, y, qobject_cast<QGraphicsItem *> (parent)))
60 d(new QScatterSeriesPrivate(qobject_cast<QGraphicsItem *> (parent)))
64 {
61 {
65 }
62 }
66
63
64 bool QScatterSeries::setData(QList<qreal> x, QList<qreal> y)
65 {
66 // TODO: validate data
67 d->m_x = x;
68 d->m_y = y;
69 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent());
70 Q_ASSERT(parentItem);
71 d->resize(parentItem->boundingRect());
72 return true;
73 }
74
67 void QScatterSeries::chartSizeChanged(QRectF rect)
75 void QScatterSeries::chartSizeChanged(QRectF rect)
68 {
76 {
69 // Recalculate scatter data point locations on the scene
77 // Recalculate scatter data point locations on the scene
@@ -12,11 +12,12 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QChartSeries
12 Q_OBJECT
12 Q_OBJECT
13 public:
13 public:
14 //QScatterSeries(QSeriesData *data, QObject *chart);
14 //QScatterSeries(QSeriesData *data, QObject *chart);
15 QScatterSeries(QList<qreal> x, QList<qreal> y, QObject *parent = 0);
15 QScatterSeries(QObject *parent = 0);
16 ~QScatterSeries();
16 ~QScatterSeries();
17
17
18 public: // from QChartSeries
18 public: // from QChartSeries
19 QChartSeriesType type() const { return QChartSeries::SeriesTypeScatter; }
19 QChartSeriesType type() const { return QChartSeries::SeriesTypeScatter; }
20 bool setData(QList<qreal> x, QList<qreal> y);
20
21
21 public Q_SLOTS:
22 public Q_SLOTS:
22 void chartSizeChanged(QRectF rect);
23 void chartSizeChanged(QRectF rect);
@@ -12,7 +12,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 class QScatterSeriesPrivate : public QGraphicsItem
12 class QScatterSeriesPrivate : public QGraphicsItem
13 {
13 {
14 public:
14 public:
15 QScatterSeriesPrivate(QList<qreal> x, QList<qreal> y, QGraphicsItem *parent);
15 QScatterSeriesPrivate(QGraphicsItem *parent);
16
16
17 public: // from QGraphicsItem
17 public: // from QGraphicsItem
18 void resize(QRectF rect);
18 void resize(QRectF rect);
@@ -120,7 +120,6 void MainWidget::addSeries(QString series, QString data)
120 {
120 {
121 qDebug() << "addSeries: " << series << " data: " << data;
121 qDebug() << "addSeries: " << series << " data: " << data;
122 m_defaultSeriesName = series;
122 m_defaultSeriesName = series;
123 QChartSeries *newSeries = QXYChartSeries::create();
124
123
125 // TODO: a dedicated data class for storing x and y values
124 // TODO: a dedicated data class for storing x and y values
126 QList<qreal> x;
125 QList<qreal> x;
@@ -131,40 +130,37 void MainWidget::addSeries(QString series, QString data)
131 x.append(i);
130 x.append(i);
132 y.append(i);
131 y.append(i);
133 }
132 }
134 for (int i = 0; i < 20; i++)
135 ((QXYChartSeries *)newSeries)->add(i, i);
136 } else if (data == "linear, 1M") {
133 } else if (data == "linear, 1M") {
137 for (int i = 0; i < 10000; i++) {
134 for (int i = 0; i < 10000; i++) {
138 x.append(i);
135 x.append(i);
139 y.append(20);
136 y.append(20);
140 }
137 }
141 for (int i = 0; i < 1000000; i++)
142 ((QXYChartSeries *)newSeries)->add(i, 10);
143 } else if (data == "SIN") {
138 } else if (data == "SIN") {
144 for (int i = 0; i < 100; i++) {
139 for (int i = 0; i < 100; i++) {
145 x.append(i);
140 x.append(i);
146 y.append(abs(sin(3.14159265358979 / 50 * i) * 100));
141 y.append(abs(sin(3.14159265358979 / 50 * i) * 100));
147 }
142 }
148 for (int i = 0; i < 100; i++)
149 ((QXYChartSeries *)newSeries)->add(i, abs(sin(3.14159265358979 / 50 * i) * 100));
150 } else if (data == "SIN + random") {
143 } else if (data == "SIN + random") {
151 for (qreal i = 0; i < 100; i += 0.1) {
144 for (qreal i = 0; i < 100; i += 0.1) {
152 x.append(i + (rand() % 5));
145 x.append(i + (rand() % 5));
153 y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
146 y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
154 }
147 }
155 for (qreal i = 0; i < 100; i += 0.1)
156 ((QXYChartSeries *)newSeries)->add(i + (rand() % 5), abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
157 } else {
148 } else {
158 // TODO: check if data has a valid file name
149 // TODO: check if data has a valid file name
150 Q_ASSERT(false);
159 }
151 }
160
152
161 // TODO: color of the series
153 // TODO: color of the series
154 QChartSeries *newSeries = 0;
162 if (series == "Scatter") {
155 if (series == "Scatter") {
163 newSeries = m_chartWidget->createSeries(x, y, QChartSeries::SeriesTypeScatter);
156 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeScatter);
157 Q_ASSERT(newSeries->setData(x, y));
164 } else if (series == "Pie") {
158 } else if (series == "Pie") {
165 newSeries = m_chartWidget->createSeries(x, y, QChartSeries::SeriesTypePie);
159 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie);
160 Q_ASSERT(newSeries->setData(y));
166 } else if (series == "Line") {
161 } else if (series == "Line") {
167 m_chartWidget->addSeries(newSeries);
162 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie);
163 Q_ASSERT(newSeries->setData(x, y));
168 } else {
164 } else {
169 // TODO
165 // TODO
170 }
166 }
General Comments 0
You need to be logged in to leave comments. Login now