##// END OF EJS Templates
Integrated scatter type series...
Tero Ahola -
r42:5fcd249308db
parent child
Show More
@@ -0,0 +1,67
1 #include "qscatterseries.h"
2 #include "qscatterseries_p.h"
3 #include "qchart.h"
4 #include <QPainter>
5 #include <QGraphicsScene>
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
9 //#define QSeriesData QList<qreal>
10
11 QScatterSeriesPrivate::QScatterSeriesPrivate(QList<qreal> x, QList<qreal> y, QGraphicsItem *parent) :
12 QGraphicsItem(parent),
13 m_x(x),
14 m_y(y)
15 {
16 }
17
18 void QScatterSeriesPrivate::setSize()
19 {
20 }
21
22 QRectF QScatterSeriesPrivate::boundingRect() const
23 {
24 return QRectF(0, 0, 100, 100);
25 }
26
27 void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
28 {
29 QPen pen = painter->pen();
30 QBrush brush = pen.brush();
31 // TODO: The opacity should be user definable...
32 brush.setColor(QColor(255, 82, 0, 50));
33 pen.setBrush(brush);
34 pen.setWidth(4);
35 painter->setPen(pen);
36 QTransform transform = painter->transform();
37
38 // TODO: get min and max values of the axes from the QChart (or some dedicated class)
39 const qreal xmin = 0.0;
40 const qreal xmax = 100.0;
41 const qreal xscale = scene()->width() / (xmax - xmin);
42 const qreal ymin = 0.0;
43 const qreal ymax = 100.0;
44 const qreal yscale = scene()->height() / (ymax - ymin);
45
46 for (int i(0); i < m_x.count() && i < m_y.count(); i++) {
47 transform.reset();
48 transform.translate(m_x.at(i) * xscale, m_y.at(i) * yscale);
49 painter->setTransform(transform);
50 painter->drawArc(0, 0, 4, 4, 0, 5760);
51 }
52 }
53
54 QScatterSeries::QScatterSeries(QList<qreal> x, QList<qreal> y, QObject *parent) :
55 QChartSeries(parent),
56 d(new QScatterSeriesPrivate(x, y, qobject_cast<QGraphicsItem *> (parent)))
57 {
58 }
59
60 QScatterSeries::~QScatterSeries()
61 {
62 delete d;
63 }
64
65 #include "moc_qscatterseries.cpp"
66
67 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,29
1 #ifndef QSCATTERSERIES_H
2 #define QSCATTERSERIES_H
3
4 #include "qchartseries.h"
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 class QScatterSeriesPrivate;
8
9 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QChartSeries
10 {
11 Q_OBJECT
12 public:
13 //QScatterSeries(QSeriesData *data, QObject *chart);
14 QScatterSeries(QList<qreal> x, QList<qreal> y, QObject *chart = 0);
15 ~QScatterSeries();
16
17 public: // from QChartSeries
18 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeScatter; }
19
20 private:
21 Q_DECLARE_PRIVATE(QScatterSeries)
22 Q_DISABLE_COPY(QScatterSeries)
23 friend class QChart;
24 QScatterSeriesPrivate *const d;
25 };
26
27 QTCOMMERCIALCHART_END_NAMESPACE
28
29 #endif // QSCATTERSERIES_H
@@ -0,0 +1,29
1 #ifndef QSCATTERSERIESPRIVATE_H
2 #define QSCATTERSERIESPRIVATE_H
3
4 #include "qchartseries.h"
5 #include <QGraphicsItem>
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
9 /*!
10 * The PIMPL class of QScatterSeries.
11 */
12 class QScatterSeriesPrivate : public QGraphicsItem
13 {
14 public:
15 QScatterSeriesPrivate(QList<qreal> x, QList<qreal> y, QGraphicsItem *parent);
16
17 public: // from QGraphicsItem
18 void setSize();
19 QRectF boundingRect() const;
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
21
22 // TODO: use the chart data class instead of list of x and y values?
23 QList<qreal> m_x;
24 QList<qreal> m_y;
25 };
26
27 QTCOMMERCIALCHART_END_NAMESPACE
28
29 #endif // QSCATTERSERIES_H
@@ -1,9 +1,12
1 1 #include "qchart.h"
2 2 #include "qchartseries.h"
3 #include "qscatterseries.h"
4 #include "qscatterseries_p.h"
3 5 #include "xylinechartitem_p.h"
4 6 #include "xyplotdomain_p.h"
5 7 #include "axis_p.h"
6 8 #include "xygrid_p.h"
9 #include <QGraphicsScene>
7 10 #include <QDebug>
8 11
9 12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -84,6 +87,28 void QChart::addSeries(QChartSeries* series)
84 87 }
85 88 }
86 89
90 QChartSeries* QChart::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type)
91 {
92 Q_D(QChart);
93
94 // TODO: support also other types
95 Q_ASSERT(type == QChartSeries::SeriesTypeScatter);
96 QChartSeries *series = 0;
97
98 switch (type) {
99 case QChartSeries::SeriesTypeScatter: {
100 QScatterSeries *scatterSeries = new QScatterSeries(x, y, this);
101 d->m_items.append(scatterSeries->d);
102 scene()->addItem(scatterSeries->d);
103 //series = qobject_cast<QChartSeries *>(scatterSeries);
104 break;
105 }
106 default:
107 break;
108 }
109
110 return series;
111 }
87 112 void QChart::setSize(const QSizeF& size)
88 113 {
89 114 Q_D(QChart);
@@ -93,10 +118,11 void QChart::setSize(const QSizeF& size)
93 118 d->m_grid->setPos(d->m_rect.topLeft());
94 119 d->m_grid->setSize(d->m_rect.size());
95 120 d->m_plotDataList[0].m_viewportRect = d->m_rect;
121 // TODO: line chart items would need to be updated separately as they don't support update
122 // via paint method
96 123 foreach(QGraphicsItem* item , d->m_items)
97 124 reinterpret_cast<XYLineChartItem*>(item)->updateXYPlotData(d->m_plotDataList.at(0));
98 125 update();
99
100 126 }
101 127
102 128 int QChart::margin() const
@@ -2,6 +2,7
2 2 #define CHART_H
3 3
4 4 #include <qchartglobal.h>
5 #include <qchartseries.h>
5 6 #include <QGraphicsItem>
6 7
7 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -12,7 +13,15 class QChartSeries;
12 13 class XYPlotDomain;
13 14 class QChartPrivate;
14 15
15 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsItem
16 // TODO: We don't need to have QChart tied to QGraphicsItem:
17 //class QTCOMMERCIALCHART_EXPORT QChart
18 //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem {
19 // public: QChartGraphicsItem(QChart &chart);
20
21 /*!
22 * TODO: define the responsibilities
23 */
24 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsItem, public QObject
16 25 {
17 26
18 27 public:
@@ -24,6 +33,9 public:
24 33 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
25 34
26 35 void addSeries(QChartSeries* series);
36 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
37 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
38 QChartSeries* createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type);
27 39
28 40 virtual void setSize(const QSizeF& rect);
29 41 void setMargin(int margin);
@@ -19,6 +19,7 public:
19 19 m_view->setScene(m_scene);
20 20 m_chart = new QChart();
21 21 m_scene->addItem(m_chart);
22 m_view->show();
22 23 }
23 24
24 25 ~QChartWidgetPrivate() {
@@ -64,6 +65,11 void QChartWidget::addSeries(QChartSeries* series)
64 65 d->m_chart->addSeries(series);
65 66 }
66 67
68 QChartSeries* QChartWidget::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type)
69 {
70 Q_D(QChartWidget);
71 return d->m_chart->createSeries(x, y, type);
72 }
67 73 #include "moc_qchartwidget.cpp"
68 74
69 75 QTCOMMERCIALCHART_END_NAMESPACE
@@ -21,7 +21,9 public:
21 21 void resizeEvent(QResizeEvent *event);
22 22 QSize sizeHint() const;
23 23
24 // TODO: addSeries and createSeries are optional solutions
24 25 void addSeries(QChartSeries* series);
26 QChartSeries* createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type);
25 27 protected:
26 28 QChartWidgetPrivate * const d_ptr;
27 29
@@ -17,6 +17,7 SOURCES += \
17 17 xylinechart/xylinechartitem.cpp \
18 18 xylinechart/xygrid.cpp \
19 19 xylinechart/xyplotdomain.cpp \
20 qscatterseries.cpp \
20 21 qchart.cpp \
21 22 axis.cpp \
22 23 qchartwidget.cpp
@@ -25,10 +26,12 PRIVATE_HEADERS += \
25 26 xylinechart/xylinechartitem_p.h \
26 27 xylinechart/xyplotdomain_p.h \
27 28 xylinechart/xygrid_p.h \
29 qscatterseries_p.h \
28 30 axis_p.h
29 31
30 32 PUBLIC_HEADERS += \
31 33 qchartseries.h \
34 qscatterseries.h \
32 35 qchart.h \
33 36 qchartwidget.h \
34 37 qchartglobal.h \
@@ -100,39 +100,45 void MainWidget::addSeries(QString series, QString data)
100 100 qDebug() << "addSeries: " << series << " data: " << data;
101 101 m_defaultSeries = series;
102 102
103 QXYChartSeries* series0 = 0;
104
105 // TODO: color of the series
106 if (series == "Scatter") {
107 series0 = QXYChartSeries::create();
108 } else if (series == "Line") {
109 series0 = QXYChartSeries::create();
110 } else {
111 // TODO
112 series0 = QXYChartSeries::create();
113 }
103 // TODO: a dedicated data class for storing x and y values
104 QList<qreal> x;
105 QList<qreal> y;
114 106
115 107 if (data == "linear") {
116 for (int i = 0; i < 10; i++)
117 series0->add(i, 10);
108 for (int i = 0; i < 10; i++) {
109 x.append(i);
110 y.append(10);
111 }
118 112 } else if (data == "linear, 1M") {
119 for (int i = 0; i < 1000000; i++)
120 series0->add(i, 20);
113 for (int i = 0; i < 10000; i++) {
114 x.append(i);
115 y.append(20);
116 }
121 117 } else if (data == "SIN") {
122 for (int x = 0; x < 100; x++)
123 series0->add(x, abs(sin(3.14159265358979 / 50 * x) * 100));
124 QList<QXYChartSeries*> dataset;
125 dataset << series0;
118 for (int i = 0; i < 100; i++) {
119 x.append(i);
120 y.append(abs(sin(3.14159265358979 / 50 * i) * 100));
121 }
126 122 } else if (data == "SIN + random") {
127 for (qreal x = 0; x < 100; x += 0.1) {
128 series0->add(x + (rand() % 5),
129 abs(sin(3.14159265358979 / 50 * x) * 100) + (rand() % 5));
123 for (qreal i = 0; i < 100; i += 0.1) {
124 x.append(i + (rand() % 5));
125 y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
130 126 }
131 127 } else {
132 128 // TODO: check if data has a valid file name
133 129 }
134 130
131 // TODO: color of the series
132 if (series == "Scatter") {
133 /*QChartSeries* scatterSeries = */m_chartWidget->createSeries(x, y, QChartSeries::SeriesTypeScatter);
134 } else if (series == "Line") {
135 QXYChartSeries* series0 = QXYChartSeries::create();
136 for (int i = 0; i < 1000000; i++)
137 series0->add(i, 20);
135 138 m_chartWidget->addSeries(series0);
139 } else {
140 // TODO
141 }
136 142 }
137 143
138 144 void MainWidget::chartTypeChanged(int itemIndex)
General Comments 0
You need to be logged in to leave comments. Login now