##// END OF EJS Templates
Refactor xyplotdata...
Michal Klocek -
r25:9ee7e26e06fe
parent child
Show More
@@ -1,77 +1,78
1 1 #include "qchart.h"
2 2 #include "qchartseries.h"
3 3 #include "xylinechartitem_p.h"
4 #include "xyplotdata_p.h"
4 #include "xyplotdomain_p.h"
5 5 #include "axis_p.h"
6 6 #include "xygrid_p.h"
7 7 #include <QDebug>
8 8
9 9 QCHART_BEGIN_NAMESPACE
10 10
11 11 QChart::QChart(QGraphicsItem* parent):QGraphicsItem(parent),
12 12 m_marginSize(0),
13 13 m_axisX(new Axis(this)),
14 14 m_axisY(new Axis(this)),
15 15 m_grid(new XYGrid(this)),
16 16 m_plotDataIndex(0)
17 17 {
18 18 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
19 19 // set axis
20 20 m_axisY->rotate(90);
21 21
22 22
23 23 //TODO hardcoded values , to removed soon
24 XYPlotData* data = new XYPlotData();
24 XYPlotDomain* data = new XYPlotDomain();
25 25 data->m_minX = 0.0;
26 26 data->m_maxX = 100.0;
27 27 data->m_minY = 0.0;
28 28 data->m_maxY = 100.0;
29 29 data->m_ticksX=4;
30 30 data->m_ticksY=4;
31 31
32 32 m_plotDataList.clear();
33 33 m_plotDataList << data;
34 34
35 35 m_grid->setZValue(10);
36 36 m_grid->setXYPlotData(*m_plotDataList.at(0));
37 37 }
38 38
39 39 QChart::~QChart(){}
40 40
41 41 QRectF QChart::boundingRect() const
42 42 {
43 43 return m_rect;
44 44 }
45 45
46 46 void QChart::addSeries(QChartSeries* series)
47 47 {
48 48 m_series<<series;
49 49
50 50 switch(series->type())
51 51 {
52 52 case QChartSeries::LINE:
53 53 XYLineChartItem* item = new XYLineChartItem(reinterpret_cast<QXYChartSeries*>(series),this);
54 item->setXYPlotData(*m_plotDataList.at(0));
54 item->updateXYPlotData(*m_plotDataList.at(0));
55 55 m_items<<item;
56 56 break;
57 57 }
58 58 }
59 59
60 60 void QChart::setSize(const QSizeF& size) {
61 61 //TODO refactor to setGeometry
62 62 m_rect = QRect(QPoint(0,0),size.toSize());
63 63 m_rect.adjust(margin(),margin(),-margin(),-margin());
64 64 m_grid->setPos(m_rect.topLeft());
65 65 m_grid->setSize(m_rect.size());
66 m_plotDataList.at(0)->m_viewportRect = m_rect;
66 67 foreach(QGraphicsItem* item , m_items)
67 reinterpret_cast<XYLineChartItem*>(item)->setChartSize(m_rect);
68 reinterpret_cast<XYLineChartItem*>(item)->updateXYPlotData(*m_plotDataList.at(0));
68 69 update();
69 70
70 71 }
71 72
72 73 void QChart::setMargin(int margin)
73 74 {
74 75 m_marginSize = margin;
75 76 }
76 77
77 78 QCHART_END_NAMESPACE
@@ -1,45 +1,45
1 1 #ifndef CHART_H
2 2 #define CHART_H
3 3
4 4 #include <qchartconfig.h>
5 5 #include <QGraphicsItem>
6 6
7 7 QCHART_BEGIN_NAMESPACE
8 8
9 9 class Axis;
10 10 class XYGrid;
11 11 class QChartSeries;
12 class XYPlotData;
12 class XYPlotDomain;
13 13
14 14 class QCHART_EXPORT QChart : public QGraphicsItem
15 15 {
16 16
17 17 public:
18 18 QChart(QGraphicsItem* parent = 0);
19 19 virtual ~QChart();
20 20
21 21 //from QGraphicsItem
22 22 virtual QRectF boundingRect() const;
23 23 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
24 24
25 25 void addSeries(QChartSeries* series);
26 26
27 27 virtual void setSize(const QSizeF& rect);
28 28 void setMargin(int margin);
29 29 int margin() const { return m_marginSize;}
30 30
31 31 private:
32 32 QRect m_rect;
33 33 QList<const QChartSeries*> m_series;
34 34 Axis* m_axisX;
35 35 Axis* m_axisY;
36 36 XYGrid* m_grid;
37 QList<XYPlotData*> m_plotDataList;
37 QList<XYPlotDomain*> m_plotDataList;
38 38 QList<QGraphicsItem*> m_items;
39 39 int m_plotDataIndex;
40 40 int m_marginSize;
41 41 };
42 42
43 43 QCHART_END_NAMESPACE
44 44
45 45 #endif
@@ -1,46 +1,46
1 1 TARGET = QChart
2 2 TEMPLATE = lib
3 3 QT += core \
4 4 gui
5 5 CONFIG += debug_and_release
6 6 CONFIG(debug, debug|release):TARGET = QChartd
7 7
8 8 SOURCES += \
9 9 xylinechart/qxychartseries.cpp \
10 10 xylinechart/xylinechartitem.cpp \
11 11 xylinechart/xygrid.cpp \
12 xylinechart/xyplotdata.cpp \
12 xylinechart/xyplotdomain.cpp \
13 13 qchart.cpp \
14 14 axis.cpp \
15 15 qchartwidget.cpp
16 16
17 17 PRIVATE_HEADERS += \
18 18 xylinechart/xylinechartitem_p.h \
19 xylinechart/xyplotdata_p.h \# to be removed
19 xylinechart/xyplotdomain_p.h \
20 20 xylinechart/xygrid_p.h \
21 21 axis_p.h
22 22
23 23 PUBLIC_HEADERS += \
24 24 qchartseries.h \
25 25 qchart.h \
26 26 qchartwidget.h \
27 27 qchartconfig.h \
28 28 xylinechart/qxychartseries.h
29 29
30 30 HEADERS += $$PUBLIC_HEADERS
31 31 HEADERS += $$PRIVATE_HEADERS
32 32
33 33 INCLUDEPATH += xylinechart \
34 34 .
35 35
36 36 OBJECTS_DIR = ../build/lib
37 37 MOC_DIR = ../build/lib
38 38 UI_DIR = ../build/lib
39 39 RCC_DIR = ../build/lib
40 40 DEFINES += QCHART_LIBRARY
41 41
42 42 public_headers.path = $$[QT_INSTALL_HEADERS]/QCharts
43 43 public_headers.files = $$PUBLIC_HEADERS
44 44 target.path = $$[QT_INSTALL_LIBS]
45 45 INSTALLS += target \
46 46 public_headers
@@ -1,70 +1,70
1 1 #include "xygrid_p.h"
2 2 #include <QPainter>
3 3 #include <QDebug>
4 4
5 5 QCHART_BEGIN_NAMESPACE
6 6
7 7 XYGrid::XYGrid(QGraphicsItem* parent):QGraphicsItem(parent)
8 8 {
9 9 }
10 10
11 11 XYGrid::~XYGrid()
12 12 {
13 13 // TODO Auto-generated destructor stub
14 14 }
15 15
16 16 void XYGrid::setSize(const QSizeF& size)
17 17 {
18 18 m_rect.setSize(size.toSize());
19 19 }
20 20
21 void XYGrid::setXYPlotData(const XYPlotData& xyPlotData)
21 void XYGrid::setXYPlotData(const XYPlotDomain& xyPlotData)
22 22 {
23 23 m_xyPlotData = xyPlotData;
24 24 }
25 25
26 26 QRectF XYGrid::boundingRect() const
27 27 {
28 28 return m_rect;
29 29 }
30 30
31 31 void XYGrid::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
32 32 {
33 33
34 34 if (!m_rect.isValid())
35 35 return;
36 36
37 37 const qreal deltaX = (m_rect.width() -1) / m_xyPlotData.ticksX();
38 38 const qreal deltaY = (m_rect.height() - 1) / m_xyPlotData.ticksY();
39 39
40 40 for (int i = 0; i <= m_xyPlotData.ticksX(); ++i) {
41 41
42 42 int x = i * deltaX + m_rect.left();
43 43 qreal label = m_xyPlotData.m_minX + (i * m_xyPlotData.spanX()
44 44 / m_xyPlotData.ticksX());
45 45 painter->drawLine(x, m_rect.top()+1, x, m_rect.bottom());
46 46 //painter->drawLine(x, m_rect.bottom(), x, m_rect.bottom() + 5);
47 47
48 48 painter->drawText(x - 50, m_rect.bottom() + 5, 100, 20,
49 49 Qt::AlignHCenter | Qt::AlignTop,
50 50 QString::number(label));
51 51 }
52 52
53 53 for (int j = 0; j <= m_xyPlotData.ticksY(); ++j) {
54 54
55 55 int y = j * -deltaY + m_rect.bottom();
56 56 qreal label = m_xyPlotData.m_minY + (j * m_xyPlotData.spanY()
57 57 / m_xyPlotData.ticksY());
58 58
59 59 painter->drawLine(m_rect.left(), y, m_rect.right()-1, y);
60 60 //painter->drawLine(m_rect.left() - 5, y, m_rect.left(), y);
61 61 //TODO : margin = 50 ;
62 62 painter->drawText(m_rect.left() - 50, y - 10, 50 - 5, 20,
63 63 Qt::AlignRight | Qt::AlignVCenter,
64 64 QString::number(label));
65 65 }
66 66
67 67 //painter->drawRect(m_rect.adjusted(0, 0, -1, -1));
68 68 }
69 69
70 70 QCHART_END_NAMESPACE
@@ -1,31 +1,31
1 1 #ifndef XYGRID_H_
2 2 #define XYGRID_H_
3 3
4 4 #include <qchartconfig.h>
5 #include <xyplotdata_p.h>
5 #include <xyplotdomain_p.h>
6 6 #include <QGraphicsItem>
7 7
8 8 QCHART_BEGIN_NAMESPACE
9 9
10 10 class XYGrid : public QGraphicsItem
11 11 {
12 12 public:
13 13 XYGrid(QGraphicsItem* parent = 0);
14 14 virtual ~XYGrid();
15 15
16 16 //from QGraphicsItem
17 17 virtual QRectF boundingRect() const;
18 18 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
19 19
20 20 //TODO: this is just temporary interface
21 void setXYPlotData(const XYPlotData& xyPlotData);
21 void setXYPlotData(const XYPlotDomain& xyPlotData);
22 22 void setSize(const QSizeF& rect);
23 23
24 24 private:
25 25 QRectF m_rect;
26 XYPlotData m_xyPlotData;
26 XYPlotDomain m_xyPlotData;
27 27 };
28 28
29 29 QCHART_END_NAMESPACE
30 30
31 31 #endif /* XYGRID_H_ */
@@ -1,69 +1,56
1 1 #include "xylinechartitem_p.h"
2 2 #include "axis_p.h"
3 3 #include "xygrid_p.h"
4 4 #include "qxychartseries.h"
5 5 #include <QPainter>
6 6 #include <QStyleOptionGraphicsItem>
7 7 #include <QDebug>
8 8
9 9 QCHART_BEGIN_NAMESPACE
10 10
11 11 XYLineChartItem::XYLineChartItem(QXYChartSeries* series,QGraphicsItem *parent):QGraphicsItem(parent),
12 m_series(series),
13 m_dirtyGeometry(true)
12 m_series(series)
14 13 {
15 14
16 15 }
17 16
18 void XYLineChartItem::setChartSize(const QRectF& rect)
17 void XYLineChartItem::updateXYPlotData(const XYPlotDomain& data)
19 18 {
20 m_rect = rect;
21 m_dirtyGeometry = true;
22 }
23
24 void XYLineChartItem::setXYPlotData(const XYPlotData& data){
25 19 m_xyPlotData=data;
26 m_dirtyGeometry = true;
20
21 if (!m_xyPlotData.m_viewportRect.isValid())
22 return;
23
24 const QRect& rect = m_xyPlotData.m_viewportRect;
25
26 const qreal deltaX = (rect.width()-1)/m_xyPlotData.spanX();
27 const qreal deltaY = (rect.height()-1)/m_xyPlotData.spanY();
28
29 m_polyline.clear();
30 m_polyline.resize(m_series->count());
31
32 for (int j = 0; j < m_series->count(); ++j) {
33 qreal dx = m_series->x(j) - m_xyPlotData.m_minX;
34 qreal dy = m_series->y(j) - m_xyPlotData.m_minY;
35 qreal x = (dx * deltaX) + rect.left();
36 qreal y = - (dy * deltaY) + rect.bottom();
37 m_polyline[j] = QPointF(x, y);
38 }
39
27 40 }
28 41
29 42 QRectF XYLineChartItem::boundingRect() const
30 43 {
31 44 return m_polyline.boundingRect();
32 45 }
33 46
34 47
35 48 void XYLineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
36 49 {
37 if(m_dirtyGeometry) {
38
39 m_dirtyGeometry=false;
40
41 if (!m_rect.isValid())
42 return;
43
44 painter->setClipRect(m_rect.adjusted(+1, +1, -1, -1));
45
46 const qreal deltaX = (m_rect.width()-1)/m_xyPlotData.spanX();
47 const qreal deltaY = (m_rect.height()-1)/m_xyPlotData.spanY();
48
49 m_polyline.clear();
50 m_polyline.resize(m_series->count());
51
52 for (int j = 0; j < m_series->count(); ++j) {
53 qreal dx = m_series->x(j) - m_xyPlotData.m_minX;
54 qreal dy = m_series->y(j) - m_xyPlotData.m_minY;
55 qreal x = (dx * deltaX) + m_rect.left();
56 qreal y = - (dy * deltaY) + m_rect.bottom();
57 m_polyline[j] = QPointF(x, y);
58 }
59 painter->setPen(m_series->color());
60 painter->drawPolyline(m_polyline);
61 }
62
63 painter->setClipRect(m_rect.adjusted(+1, +1, -1, -1));
50 painter->setClipRect(m_xyPlotData.m_viewportRect.adjusted(+1, +1, -1, -1));
64 51 painter->setPen(m_series->color());
65 52 painter->drawPolyline(m_polyline);
66 53
67 54 }
68 55
69 56 QCHART_END_NAMESPACE
@@ -1,37 +1,33
1 1 #ifndef XYLINECHARTITEM_H
2 2 #define XYLINECHARTITEM_H
3 3
4 4 #include "qchartconfig.h"
5 5 #include "qchart.h"
6 #include "xyplotdata_p.h"
6 #include "xyplotdomain_p.h"
7 7
8 8 QCHART_BEGIN_NAMESPACE
9 9
10 10 class QXYChartSeries;
11 11
12 12 class XYLineChartItem : public QGraphicsItem
13 13 {
14 14
15 15 public:
16 16 XYLineChartItem(QXYChartSeries* m_series,QGraphicsItem *parent = 0);
17 17 virtual ~ XYLineChartItem(){};
18 18
19 19 //from QGraphicsItem
20 20 virtual QRectF boundingRect() const;
21 21 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
22 22
23 //TODO: this is just temporary interface
24 void setChartSize(const QRectF& size);
25 void setXYPlotData(const XYPlotData& data);
23 void updateXYPlotData(const XYPlotDomain& data);
26 24
27 25 private:
28 QRectF m_rect;
29 26 QPolygonF m_polyline;
30 27 QXYChartSeries* m_series;
31 XYPlotData m_xyPlotData;
32 bool m_dirtyGeometry;
28 XYPlotDomain m_xyPlotData;
33 29 };
34 30
35 31 QCHART_END_NAMESPACE
36 32
37 33 #endif
@@ -1,27 +1,33
1 #include "xyplotdata_p.h"
1 #include "xyplotdomain_p.h"
2 2
3 3 QCHART_BEGIN_NAMESPACE
4 4
5 XYPlotData::XYPlotData()
5 XYPlotDomain::XYPlotDomain():
6 m_ticksX(0),
7 m_ticksY(0),
8 m_minX(0),
9 m_maxX(0),
10 m_minY(0),
11 m_maxY(0)
6 12 {
7 13
8 14 }
9 15
10 XYPlotData::~XYPlotData()
16 XYPlotDomain::~XYPlotDomain()
11 17 {
12 18 // TODO Auto-generated destructor stub
13 19 }
14 20
15 qreal XYPlotData::spanX() const
21 qreal XYPlotDomain::spanX() const
16 22 {
17 23 Q_ASSERT(m_maxX >= m_minX);
18 24 return m_maxX - m_minX;
19 25 }
20 26
21 qreal XYPlotData::spanY() const
27 qreal XYPlotDomain::spanY() const
22 28 {
23 29 Q_ASSERT(m_maxY >= m_minY);
24 30 return m_maxY - m_minY;
25 31 }
26 32
27 33 QCHART_END_NAMESPACE
@@ -1,31 +1,30
1 #ifndef PLOTTER_H_
2 #define PLOTTER_H_
1 #ifndef PLOTDOMAIN_H_
2 #define PLOTDOMAIN_H_
3 3 #include "qchartconfig.h"
4 #include <QtGlobal>
4 #include <QRect>
5 5
6 6 QCHART_BEGIN_NAMESPACE
7 7
8 class XYPlotData {
8 class XYPlotDomain {
9 9 public:
10 XYPlotData();
11 virtual ~XYPlotData();
10 XYPlotDomain();
11 virtual ~XYPlotDomain();
12 12
13 13 qreal spanX() const;
14 14 qreal spanY() const;
15 15 int ticksX() const { return m_ticksX; }
16 16 int ticksY() const { return m_ticksY; }
17 17
18 18 public:
19 19 int m_ticksX;
20 20 int m_ticksY;
21 21 qreal m_minX;
22 22 qreal m_maxX;
23 23 qreal m_minY;
24 24 qreal m_maxY;
25
26
25 QRect m_viewportRect;
27 26 };
28 27
29 28 QCHART_END_NAMESPACE
30 29
31 30 #endif /* PLOTTER_H_ */
General Comments 0
You need to be logged in to leave comments. Login now