##// 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 98 // TODO: support also other types; not only scatter and pie
99 Q_ASSERT(type == QChartSeries::SeriesTypeScatter
100 || type == QChartSeries::SeriesTypePie);
101 99
102 100 switch (type) {
103 101 case QChartSeries::SeriesTypeScatter: {
104 QScatterSeries *scatterSeries = new QScatterSeries(x, y, this);
102 QScatterSeries *scatterSeries = new QScatterSeries(this);
105 103 connect(this, SIGNAL(sizeChanged(QRectF)),
106 104 scatterSeries, SLOT(chartSizeChanged(QRectF)));
107 105 scatterSeries->d->setParentItem(this);
@@ -110,12 +108,13 QChartSeries* QChart::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries:
110 108 case QChartSeries::SeriesTypePie: {
111 109 // TODO: we now have also a list of y values as a parameter, it is ignored
112 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 112 connect(this, SIGNAL(sizeChanged(QRectF)),
115 113 pieSeries, SLOT(chartSizeChanged(QRectF)));
116 114 return pieSeries;
117 115 }
118 116 default:
117 Q_ASSERT(false);
119 118 break;
120 119 }
121 120
@@ -36,7 +36,7 public:
36 36 void addSeries(QChartSeries* series);
37 37 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
38 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 41 virtual void setSize(const QSizeF& rect);
42 42 void setMargin(int margin);
@@ -25,11 +25,14 protected:
25 25 public:
26 26 virtual ~QChartSeries(){};
27 27
28 //factory method
28 // Factory method
29 29 static QChartSeries* create(QObject* parent = 0 ){ return 0;}
30 //pure virtual
30 // Pure virtual
31 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 38 QTCOMMERCIALCHART_END_NAMESPACE
@@ -40,10 +40,10 void QChartView::addSeries(QChartSeries* series)
40 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 49 void QChartView::mousePressEvent(QMouseEvent *event)
@@ -21,9 +21,9 public:
21 21 //implement from QWidget
22 22 void resizeEvent(QResizeEvent *event);
23 23
24 // TODO: addSeries and createSeries are optional solutions
25 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 28 protected:
29 29 void mouseMoveEvent (QMouseEvent *event);
@@ -14,7 +14,7 QGraphicsView(parent)
14 14 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
15 15 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
16 16 setScene(m_scene);
17
17
18 18 m_chart = new QChart();
19 19 m_scene->addItem(m_chart);
20 20 show();
@@ -42,9 +42,9 void QChartWidget::addSeries(QChartSeries* series)
42 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 49 #include "moc_qchartwidget.cpp"
50 50
@@ -26,7 +26,7 public:
26 26 // TODO: addSeries and createSeries are optional solutions
27 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 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 31 private:
32 32 Q_DISABLE_COPY(QChartWidget)
@@ -5,23 +5,34
5 5
6 6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 7
8 QPieSeries::QPieSeries(QList<qreal> x, QGraphicsObject *parent) :
8 QPieSeries::QPieSeries(QGraphicsObject *parent) :
9 9 QChartSeries(parent),
10 m_x(x),
11 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 24 // Create slices
14 25 qreal fullPie = 360;
15 26 qreal total = 0;
16 foreach (qreal value, m_x)
27 foreach (qreal value, m_data)
17 28 total += value;
18 29
19 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent);
30 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent());
20 31 Q_ASSERT(parentItem);
21 32 m_chartSize = parentItem->boundingRect();
22 33 qreal angle = 0;
23 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 36 qreal span = value / total * fullPie;
26 37 PieSlice *slice = new PieSlice(randomColor(), angle, span, parentItem->boundingRect());
27 38 slice->setParentItem(parentItem);
@@ -30,12 +41,7 QPieSeries::QPieSeries(QList<qreal> x, QGraphicsObject *parent) :
30 41 }
31 42
32 43 resizeSlices(m_chartSize);
33 }
34
35 QPieSeries::~QPieSeries()
36 {
37 while (m_slices.count())
38 delete m_slices.takeLast();
44 return true;
39 45 }
40 46
41 47 void QPieSeries::chartSizeChanged(QRectF chartRect)
@@ -16,7 +16,7 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QChartSeries
16 16
17 17 public:
18 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 20 ~QPieSeries();
21 21 QColor randomColor();
22 22 void setSizeFactor(qreal sizeFactor);
@@ -24,6 +24,7 public:
24 24
25 25 public: // from QChartSeries
26 26 QChartSeriesType type() const { return QChartSeries::SeriesTypePie; }
27 bool setData(QList<qreal> data);
27 28
28 29 public Q_SLOTS:
29 30 void chartSizeChanged(QRectF rect);
@@ -33,7 +34,7 private:
33 34 //Q_DECLARE_PRIVATE(QPieSeries)
34 35 Q_DISABLE_COPY(QPieSeries)
35 36 // TODO: move the followin to internal impl
36 QList<qreal> m_x;
37 QList<qreal> m_data;
37 38 QList<PieSlice*> m_slices;
38 39 QRectF m_chartSize;
39 40 qreal m_sizeFactor;
@@ -9,14 +9,11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 //#define QSeriesData QList<qreal>
11 11
12 QScatterSeriesPrivate::QScatterSeriesPrivate(QList<qreal> x, QList<qreal> y, QGraphicsItem *parent) :
12 QScatterSeriesPrivate::QScatterSeriesPrivate(QGraphicsItem *parent) :
13 13 QGraphicsItem(parent),
14 m_x(x),
15 m_y(y),
16 14 m_scalex(100), // TODO: let the use define the scale (or autoscaled)
17 15 m_scaley(100)
18 16 {
19 resize(parent->boundingRect());
20 17 }
21 18
22 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 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 75 void QScatterSeries::chartSizeChanged(QRectF rect)
68 76 {
69 77 // Recalculate scatter data point locations on the scene
@@ -12,11 +12,12 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QChartSeries
12 12 Q_OBJECT
13 13 public:
14 14 //QScatterSeries(QSeriesData *data, QObject *chart);
15 QScatterSeries(QList<qreal> x, QList<qreal> y, QObject *parent = 0);
15 QScatterSeries(QObject *parent = 0);
16 16 ~QScatterSeries();
17 17
18 18 public: // from QChartSeries
19 19 QChartSeriesType type() const { return QChartSeries::SeriesTypeScatter; }
20 bool setData(QList<qreal> x, QList<qreal> y);
20 21
21 22 public Q_SLOTS:
22 23 void chartSizeChanged(QRectF rect);
@@ -12,7 +12,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 12 class QScatterSeriesPrivate : public QGraphicsItem
13 13 {
14 14 public:
15 QScatterSeriesPrivate(QList<qreal> x, QList<qreal> y, QGraphicsItem *parent);
15 QScatterSeriesPrivate(QGraphicsItem *parent);
16 16
17 17 public: // from QGraphicsItem
18 18 void resize(QRectF rect);
@@ -120,7 +120,6 void MainWidget::addSeries(QString series, QString data)
120 120 {
121 121 qDebug() << "addSeries: " << series << " data: " << data;
122 122 m_defaultSeriesName = series;
123 QChartSeries *newSeries = QXYChartSeries::create();
124 123
125 124 // TODO: a dedicated data class for storing x and y values
126 125 QList<qreal> x;
@@ -131,40 +130,37 void MainWidget::addSeries(QString series, QString data)
131 130 x.append(i);
132 131 y.append(i);
133 132 }
134 for (int i = 0; i < 20; i++)
135 ((QXYChartSeries *)newSeries)->add(i, i);
136 133 } else if (data == "linear, 1M") {
137 134 for (int i = 0; i < 10000; i++) {
138 135 x.append(i);
139 136 y.append(20);
140 137 }
141 for (int i = 0; i < 1000000; i++)
142 ((QXYChartSeries *)newSeries)->add(i, 10);
143 138 } else if (data == "SIN") {
144 139 for (int i = 0; i < 100; i++) {
145 140 x.append(i);
146 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 143 } else if (data == "SIN + random") {
151 144 for (qreal i = 0; i < 100; i += 0.1) {
152 145 x.append(i + (rand() % 5));
153 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 148 } else {
158 149 // TODO: check if data has a valid file name
150 Q_ASSERT(false);
159 151 }
160 152
161 153 // TODO: color of the series
154 QChartSeries *newSeries = 0;
162 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 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 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 164 } else {
169 165 // TODO
170 166 }
General Comments 0
You need to be logged in to leave comments. Login now