##// END OF EJS Templates
adds missing files form previous commit
Michal Klocek -
r12:54a770e32f2a
parent child
Show More
@@ -0,0 +1,2
1 TEMPLATE = subdirs
2 SUBDIRS += linechart No newline at end of file
@@ -0,0 +1,22
1 #include "chartview.h"
2
3 ChartView::ChartView(QChartWidget* chartWidget,QWidget* parent):QGraphicsView(parent),
4 m_chartWidget(chartWidget),
5 m_scene(new QGraphicsScene(parent))
6 {
7 setScene(m_scene);
8 m_scene->addItem(chartWidget);
9 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
10 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
11 // get rid off ugly default 1 pixel border
12 setStyleSheet( "QGraphicsView { border-style: none; }" );
13 }
14
15 ChartView::~ChartView() {
16 // TODO Auto-generated destructor stub
17 }
18
19 void ChartView::resizeEvent(QResizeEvent *event){
20 m_chartWidget->resize(size());
21 QGraphicsView::resizeEvent(event);
22 }
@@ -0,0 +1,22
1 #ifndef CHARTVIEW_H_
2 #define CHARTVIEW_H_
3 #include <qchartwidget.h>
4 #include <QGraphicsView>
5
6 class QGraphicsScene;
7
8 QCHART_USE_NAMESPACE
9
10 class ChartView: public QGraphicsView
11 {
12 public:
13 ChartView (QChartWidget* chartWidget,QWidget* parent=0);
14 virtual ~ChartView();
15 void resizeEvent(QResizeEvent *event);
16
17 private:
18 QChartWidget* m_chartWidget;
19 QGraphicsScene* m_scene;
20 };
21
22 #endif /* CHARTVIEW_H_ */
@@ -0,0 +1,9
1 TARGET = lineChart
2 TEMPLATE = app
3
4 QT += core gui
5
6 CONFIG += charts
7
8 HEADERS += chartview.h
9 SOURCES += main.cpp chartview.cpp
@@ -0,0 +1,57
1 #include <QApplication>
2 #include <QMainWindow>
3 #include <qchartwidget.h>
4 #include <qchart.h>
5 #include "chartview.h"
6 #include <cmath>
7
8 QCHART_USE_NAMESPACE
9
10 #define PI 3.14159265358979
11
12 int main(int argc, char *argv[])
13 {
14 QApplication a(argc, argv);
15
16 QMainWindow window;
17
18 QXYSeries* series0 = new QXYSeries();
19 series0->setColor(Qt::blue);
20 QXYSeries* series1 = new QXYSeries();
21 series1->setColor(Qt::red);
22 QXYSeries* series2 = new QXYSeries();
23 series2->setColor(Qt::gray);
24 QXYSeries* series3 = new QXYSeries();
25 series3->setColor(Qt::green);
26
27 int numPoints = 100;
28
29 for (int x = 0; x < numPoints; ++x) {
30 series0->add(x,0);
31 series1->add(x, uint(sin(PI/25*x)*100) % 100);
32 series2->add(x, uint(cos(PI/25*x)*100) % 100);
33 series3->add(x,100);
34 }
35
36 QList<QXYSeries*> dataset;
37
38 qDebug()<<"Series 1:" << *series1;
39 qDebug()<<"Series 2:" << *series2;
40
41 dataset << series0;
42 dataset << series1;
43 dataset << series2;
44 dataset << series3;
45
46 QChart* chart = QChart::createXYLineChart(dataset);
47 chart->setMargin(50);
48 QChartWidget* chartWidget = new QChartWidget();
49 chartWidget->addChart(chart);
50
51 ChartView* view = new ChartView(chartWidget,&window);
52 window.setCentralWidget(view);
53 window.resize(400, 300);
54 window.show();
55
56 return a.exec();
57 }
@@ -0,0 +1,50
1 #include "axis_p.h"
2 #include <QPainter>
3 #include <QDebug>
4
5 QCHART_BEGIN_NAMESPACE
6
7 Axis::Axis(QGraphicsItem* parent): QGraphicsItem(parent)
8 {
9 }
10
11 Axis::~Axis()
12 {
13 }
14
15 void Axis::setLength(int length)
16 {
17 QPainterPath path;
18 path.moveTo(QPointF(0,0));
19 path.lineTo(length,0);
20 // path.lineTo(length-4,0);
21 // path.lineTo(length,3);
22 // path.lineTo(length-4,6);
23 // path.lineTo(length-4,4);
24 // path.lineTo(0,4);
25 // path.lineTo(0,2);
26 m_path=path;
27 update();
28 }
29
30 QRectF Axis::boundingRect() const
31 {
32 return m_path.boundingRect();
33 }
34
35 QPainterPath Axis::shape() const
36 {
37 return m_path;
38 }
39
40 void Axis::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
41 {
42 painter->save();
43 QPen pen(Qt::black);
44 //pen.setWidth(10);
45 painter->setPen(pen);
46 painter->drawPath(m_path);
47 painter->restore();
48 }
49
50 QCHART_END_NAMESPACE
@@ -0,0 +1,29
1 #ifndef AXIS_H_
2 #define AXIS_H_
3
4 #include <qchartconfig.h>
5 #include <QGraphicsItem>
6
7 QCHART_BEGIN_NAMESPACE
8
9 class Axis: public QGraphicsItem
10 {
11 public:
12 Axis(QGraphicsItem* parent = 0);
13 virtual ~Axis();
14
15 //from QGraphicsItem
16 virtual QPainterPath shape() const;
17 virtual QRectF boundingRect() const;
18 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
19
20 void setLength(int length);
21 void setWidth(int width);
22
23 private:
24 QPainterPath m_path;
25 };
26
27 QCHART_END_NAMESPACE
28
29 #endif /* AXIS_H_ */
@@ -0,0 +1,30
1 #include "qchart.h"
2 #include "xylinechart_p.h"
3
4 QCHART_BEGIN_NAMESPACE
5
6 QChart::QChart(QGraphicsItem* parent):QGraphicsItem(parent),
7 m_marginSize(0)
8 {
9 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
10 }
11
12 QChart::~QChart(){}
13
14
15 QChart* QChart::createXYLineChart(const QList<QXYSeries*>& dataset)
16 {
17
18 XYLineChart* chart = new XYLineChart();
19 foreach (const QXYSeries* series,dataset) {
20 chart->addXYSeries(series);
21 }
22 return chart;
23 }
24
25 void QChart::setMargin(int margin)
26 {
27 m_marginSize=margin;
28 }
29
30 QCHART_END_NAMESPACE
@@ -0,0 +1,31
1 #ifndef CHART_H
2 #define CHART_H
3
4 #include <qchartconfig.h>
5 #include <qxyseries.h>
6 #include <QGraphicsItem>
7
8 QCHART_BEGIN_NAMESPACE
9
10 class QCHART_EXPORT QChart : public QGraphicsItem
11 {
12
13 protected:
14 QChart(QGraphicsItem* parent =0);
15
16 public:
17 virtual ~QChart();
18
19 static QChart* createXYLineChart(const QList<QXYSeries*>& dataset);
20
21 virtual void setSize(const QSizeF& rect)=0;
22 void setMargin(int margin);
23 int margin() const { return m_marginSize;}
24
25 private:
26 int m_marginSize;
27 };
28
29 QCHART_END_NAMESPACE
30
31 #endif
@@ -0,0 +1,41
1 #include "qchartwidget.h"
2 #include "qchart.h"
3
4 QCHART_BEGIN_NAMESPACE
5
6
7 class QChartWidgetPrivate
8 {
9 public:
10 QChart* chart;
11 //Q_DECLARE_PUBLIC(ChartWidget)
12 QChartWidgetPrivate() {}
13 };
14
15 ////////////////////////////////////////////////////////////////////////////
16
17 QChartWidget::QChartWidget(QGraphicsItem *parent,Qt::WindowFlags wFlags)
18 : QGraphicsWidget(parent,wFlags),
19 d_ptr(new QChartWidgetPrivate())
20 {
21 }
22
23 QChartWidget::~QChartWidget()
24 {
25 delete d_ptr;
26 }
27
28
29 void QChartWidget::addChart(QChart *chart)
30 {
31 d_ptr->chart=chart;
32 chart->setParentItem(this);
33 }
34
35 void QChartWidget::setGeometry(const QRectF & rect)
36 {
37 d_ptr->chart->setSize(rect.size());
38 QGraphicsWidget::setGeometry(rect);
39 }
40
41 QCHART_END_NAMESPACE
@@ -0,0 +1,34
1 #ifndef QCHARTWIDGET_H
2 #define QCHARTWIDGET_H
3
4 #include <qchartconfig.h>
5 #include <QGraphicsWidget>
6
7 class QGraphicsItem;
8
9 QCHART_BEGIN_NAMESPACE
10
11 class QChart;
12 class QChartWidgetPrivate;
13
14 class QCHART_EXPORT QChartWidget : public QGraphicsWidget
15 {
16
17 public:
18 explicit QChartWidget(QGraphicsItem *parent = 0,Qt::WindowFlags wFlags = 0);
19 ~QChartWidget();
20
21 QChart* chart() const;
22 void addChart(QChart* chart);
23
24 virtual void setGeometry(const QRectF & rect);
25
26 private:
27 Q_DISABLE_COPY(QChartWidget)
28 Q_DECLARE_PRIVATE(QChartWidget)
29 QChartWidgetPrivate * const d_ptr;
30 };
31
32 QCHART_END_NAMESPACE
33
34 #endif
@@ -0,0 +1,59
1 #include "qxyseries.h"
2
3 QXYSeries::QXYSeries():
4 m_color(Qt::black)
5 {
6 }
7
8 QXYSeries::~QXYSeries()
9 {
10 }
11
12
13 void QXYSeries::setColor(const QColor& color)
14 {
15 m_color = color;
16 }
17
18
19 void QXYSeries::add(qreal x,qreal y)
20 {
21 m_x<<x;
22 m_y<<y;
23 }
24
25 void QXYSeries::clear()
26 {
27 m_x.clear();
28 m_y.clear();
29 }
30
31 qreal QXYSeries::x(int pos) const
32 {
33 return m_x.at(pos);
34 }
35
36 qreal QXYSeries::y(int pos) const
37 {
38 return m_y.at(pos);
39 }
40
41 int QXYSeries::count() const
42 {
43 Q_ASSERT(m_x.size() == m_y.size());
44
45 return m_x.size();
46
47 }
48
49 QDebug operator<< (QDebug debug, const QXYSeries series)
50 {
51 Q_ASSERT(series.m_x.size() == series.m_y.size());
52
53 int size = series.m_x.size();
54
55 for (int i=0;i<size;i++) {
56 debug.nospace() << "(" << series.m_x.at(i) << ','<< series.m_y.at(i) << ") ";
57 }
58 return debug.space();
59 }
@@ -0,0 +1,28
1 #ifndef QXYSERIES_H_
2 #define QXYSERIES_H_
3 #include <QDebug>
4 #include <QColor>
5
6
7 class QXYSeries
8 {
9 public:
10 QXYSeries();
11 virtual ~QXYSeries();
12 void add(qreal x, qreal y);
13 void clear();
14 void setColor(const QColor& color);
15 const QColor& color() const { return m_color;}
16 int count() const;
17 qreal x(int pos) const;
18 qreal y(int pos) const;
19 friend QDebug operator<< (QDebug d, const QXYSeries series);
20
21 private:
22 QColor m_color;
23 QList<qreal> m_x;
24 QList<qreal> m_y;
25
26 };
27
28 #endif
@@ -0,0 +1,67
1 #include "xygrid_p.h"
2 #include "xylinechart_p.h"
3 #include <QPainter>
4
5 QCHART_BEGIN_NAMESPACE
6
7 XYGrid::XYGrid(const XYLineChart* const chart, QGraphicsItem* parent):QGraphicsItem(parent),
8 m_chart(chart)
9 {
10 }
11
12 XYGrid::~XYGrid()
13 {
14 // TODO Auto-generated destructor stub
15 }
16
17 void XYGrid::setSize(const QSizeF& size) {
18 m_rect.setSize(size.toSize());
19 }
20
21
22 QRectF XYGrid::boundingRect() const
23 {
24 return m_rect;
25 }
26
27 void XYGrid::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
28 {
29 if (!m_rect.isValid())
30 return;
31
32 const XYPlotData& plotData = m_chart->xyPlotData();
33
34 const qreal deltaX = (m_rect.width() -1) / plotData.ticksX();
35 const qreal deltaY = (m_rect.height() - 1) / plotData.ticksY();
36
37 for (int i = 0; i <= plotData.ticksX(); ++i) {
38
39 int x = i * deltaX + m_rect.left();
40 qreal label = plotData.m_minX + (i * plotData.spanX()
41 / plotData.ticksX());
42 painter->drawLine(x, m_rect.top()+1, x, m_rect.bottom());
43 //painter->drawLine(x, m_rect.bottom(), x, m_rect.bottom() + 5);
44
45 painter->drawText(x - 50, m_rect.bottom() + 5, 100, 20,
46 Qt::AlignHCenter | Qt::AlignTop,
47 QString::number(label));
48 }
49
50 for (int j = 0; j <= plotData.ticksY(); ++j) {
51
52 int y = j * -deltaY + m_rect.bottom();
53 qreal label = plotData.m_minY + (j * plotData.spanY()
54 / plotData.ticksY());
55
56 painter->drawLine(m_rect.left(), y, m_rect.right()-1, y);
57 //painter->drawLine(m_rect.left() - 5, y, m_rect.left(), y);
58 //TODO : margin = 50 ;
59 painter->drawText(m_rect.left() - 50, y - 10, 50 - 5, 20,
60 Qt::AlignRight | Qt::AlignVCenter,
61 QString::number(label));
62 }
63
64 //painter->drawRect(m_rect.adjusted(0, 0, -1, -1));
65 }
66
67 QCHART_END_NAMESPACE
@@ -0,0 +1,31
1 #ifndef XYGRID_H_
2 #define XYGRID_H_
3
4 #include <qchartconfig.h>
5 #include <QGraphicsItem>
6
7 QCHART_BEGIN_NAMESPACE
8
9 class XYPlotData;
10 class XYLineChart;
11
12 class XYGrid : public QGraphicsItem
13 {
14 public:
15 XYGrid(const XYLineChart* const chart,QGraphicsItem* parent = 0);
16 virtual ~XYGrid();
17
18 //from QGraphicsItem
19 virtual QRectF boundingRect() const;
20 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
21
22 void setSize(const QSizeF& rect);
23
24 private:
25 QRectF m_rect;
26 const XYLineChart* const m_chart;
27 };
28
29 QCHART_END_NAMESPACE
30
31 #endif /* XYGRID_H_ */
@@ -0,0 +1,86
1 #include "xylinechart_p.h"
2 #include "axis_p.h"
3 #include "xygrid_p.h"
4 #include <QPainter>
5 #include <QStyleOptionGraphicsItem>
6 #include <QDebug>
7
8 QCHART_BEGIN_NAMESPACE
9
10 XYLineChart::XYLineChart(QGraphicsItem *parent):QChart(parent),
11 m_axisX(new Axis(this)),
12 m_axisY(new Axis(this)),
13 m_grid(new XYGrid(this,this)),
14 m_plotDataIndex(0)
15 {
16 // set axis
17 m_axisY->rotate(90);
18
19 XYPlotData data;
20 data.m_minX = 0.0;
21 data.m_maxX = 100.0;
22 data.m_minY = 0.0;
23 data.m_maxY = 100.0;
24 data.m_ticksX=4;
25 data.m_ticksY=4;
26
27 m_plotDataList.clear();
28 m_plotDataList << data;
29
30 }
31
32 void XYLineChart::setSize(const QSizeF& size) {
33
34 m_rect = QRect(QPoint(0,0),size.toSize());
35 m_rect.adjust(margin(),margin(),-margin(),-margin());
36 updateGeometry();
37 }
38
39 void XYLineChart::addXYSeries(const QXYSeries* series)
40 {
41 m_series<<series;
42 }
43
44 QRectF XYLineChart::boundingRect() const
45 {
46 return m_rect;
47 }
48
49 void XYLineChart::updateGeometry()
50 {
51 //m_axisX->setPos(m_rect.bottomLeft());
52 //m_axisX->setLength(m_rect.width());
53 //m_axisY->setLength(m_rect.height());
54 m_grid->setPos(m_rect.topLeft());
55 m_grid->setSize(m_rect.size());
56 update();
57 }
58
59 void XYLineChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
60 {
61 const XYPlotData& xyplotData = xyPlotData();
62
63 if (!m_rect.isValid())
64 return;
65
66 painter->setClipRect(m_rect.adjusted(+1, +1, -1, -1));
67
68 const qreal deltaX = (m_rect.width()-1)/xyplotData.spanX();
69 const qreal deltaY = (m_rect.height()-1)/xyplotData.spanY();
70
71 foreach (const QXYSeries* series, m_series) {
72
73 QPolygonF polyline(series->count());
74
75 for (int j = 0; j < series->count(); ++j) {
76 qreal dx = series->x(j) - xyplotData.m_minX;
77 qreal dy = series->y(j) - xyplotData.m_minY;
78 qreal x = (dx * deltaX) + m_rect.left();
79 qreal y = - (dy * deltaY) + m_rect.bottom();
80 polyline[j] = QPointF(x, y);
81 }
82 painter->setPen(series->color());
83 painter->drawPolyline(polyline);
84 }
85 }
86 QCHART_END_NAMESPACE
@@ -0,0 +1,52
1 #ifndef XYLINECHART_H
2 #define XYLINECHART_H
3
4 #include "qchartconfig.h"
5 #include "qchart.h"
6 #include "qxyseries.h"
7 #include "xyplotdata_p.h"
8
9 QCHART_BEGIN_NAMESPACE
10
11 class Axis;
12 class XYGrid;
13
14 class XYLineChart : public QChart
15 {
16
17 public:
18 XYLineChart(QGraphicsItem *parent = 0);
19 virtual ~XYLineChart(){};
20
21 //from QGraphicsItem
22 virtual QRectF boundingRect() const;
23 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
24
25 //from QChart
26 virtual void setSize(const QSizeF& size);
27
28 void addXYSeries(const QXYSeries* series);
29
30 const XYPlotData& xyPlotData() const { return m_plotDataList.at(m_plotDataIndex); }
31
32 private:
33 void updateGeometry();
34 void drawData(QPainter *painter);
35
36 private:
37 QRect m_rect;
38 QList<const QXYSeries*> m_series;
39 Axis* m_axisX;
40 Axis* m_axisY;
41 XYGrid* m_grid;
42 QPointF* m_origin;
43 QMap<int, QVector<QPointF> > curveMap;
44 QList<XYPlotData> m_plotDataList;
45 int m_plotDataIndex;
46
47
48 };
49
50 QCHART_END_NAMESPACE
51
52 #endif
@@ -0,0 +1,27
1 #include "xyplotdata_p.h"
2
3 QCHART_BEGIN_NAMESPACE
4
5 XYPlotData::XYPlotData()
6 {
7
8 }
9
10 XYPlotData::~XYPlotData()
11 {
12 // TODO Auto-generated destructor stub
13 }
14
15 qreal XYPlotData::spanX() const
16 {
17 Q_ASSERT(m_maxX >= m_minX);
18 return m_maxX - m_minX;
19 }
20
21 qreal XYPlotData::spanY() const
22 {
23 Q_ASSERT(m_maxY >= m_minY);
24 return m_maxY - m_minY;
25 }
26
27 QCHART_END_NAMESPACE
@@ -0,0 +1,31
1 #ifndef PLOTTER_H_
2 #define PLOTTER_H_
3 #include "qchartconfig.h"
4 #include <QtGlobal>
5
6 QCHART_BEGIN_NAMESPACE
7
8 class XYPlotData {
9 public:
10 XYPlotData();
11 virtual ~XYPlotData();
12
13 qreal spanX() const;
14 qreal spanY() const;
15 int ticksX() const { return m_ticksX; }
16 int ticksY() const { return m_ticksY; }
17
18 public:
19 int m_ticksX;
20 int m_ticksY;
21 qreal m_minX;
22 qreal m_maxX;
23 qreal m_minY;
24 qreal m_maxY;
25
26
27 };
28
29 QCHART_END_NAMESPACE
30
31 #endif /* PLOTTER_H_ */
General Comments 0
You need to be logged in to leave comments. Login now