##// END OF EJS Templates
Refactor current draft to fit int current design specs...
Michal Klocek -
r21:f4dbcb0551ab
parent child
Show More
@@ -0,0 +1,69
1 #include "xylinechartitem_p.h"
2 #include "axis_p.h"
3 #include "xygrid_p.h"
4 #include "qxychartseries.h"
5 #include <QPainter>
6 #include <QStyleOptionGraphicsItem>
7 #include <QDebug>
8
9 QCHART_BEGIN_NAMESPACE
10
11 XYLineChartItem::XYLineChartItem(QXYChartSeries* series,QGraphicsItem *parent):QGraphicsItem(parent),
12 m_series(series),
13 m_dirtyGeometry(true)
14 {
15
16 }
17
18 void XYLineChartItem::setChartSize(const QRectF& rect)
19 {
20 m_rect = rect;
21 m_dirtyGeometry = true;
22 }
23
24 void XYLineChartItem::setXYPlotData(const XYPlotData& data){
25 m_xyPlotData=data;
26 m_dirtyGeometry = true;
27 }
28
29 QRectF XYLineChartItem::boundingRect() const
30 {
31 return m_polyline.boundingRect();
32 }
33
34
35 void XYLineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
36 {
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));
64 painter->setPen(m_series->color());
65 painter->drawPolyline(m_polyline);
66
67 }
68
69 QCHART_END_NAMESPACE
@@ -0,0 +1,37
1 #ifndef XYLINECHARTITEM_H
2 #define XYLINECHARTITEM_H
3
4 #include "qchartconfig.h"
5 #include "qchart.h"
6 #include "xyplotdata_p.h"
7
8 QCHART_BEGIN_NAMESPACE
9
10 class QXYChartSeries;
11
12 class XYLineChartItem : public QGraphicsItem
13 {
14
15 public:
16 XYLineChartItem(QXYChartSeries* m_series,QGraphicsItem *parent = 0);
17 virtual ~ XYLineChartItem(){};
18
19 //from QGraphicsItem
20 virtual QRectF boundingRect() const;
21 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
22
23 //TODO: this is just temporary interface
24 void setChartSize(const QRectF& size);
25 void setXYPlotData(const XYPlotData& data);
26
27 private:
28 QRectF m_rect;
29 QPolygonF m_polyline;
30 QXYChartSeries* m_series;
31 XYPlotData m_xyPlotData;
32 bool m_dirtyGeometry;
33 };
34
35 QCHART_END_NAMESPACE
36
37 #endif
@@ -1,9 +1,8
1 1 TARGET = lineChart
2 2 TEMPLATE = app
3 3
4 4 QT += core gui
5 5
6 6 CONFIG += charts
7 7
8 HEADERS += chartview.h
9 SOURCES += main.cpp chartview.cpp
8 SOURCES += main.cpp
@@ -1,57 +1,56
1 1 #include <QApplication>
2 2 #include <QMainWindow>
3 3 #include <qchartwidget.h>
4 #include <qxychartseries.h>
4 5 #include <qchart.h>
5 6 #include "chartview.h"
6 7 #include <cmath>
7 8
8 9 QCHART_USE_NAMESPACE
9 10
10 11 #define PI 3.14159265358979
11 12
12 13 int main(int argc, char *argv[])
13 14 {
14 15 QApplication a(argc, argv);
15 16
16 17 QMainWindow window;
17 18
18 QXYSeries* series0 = new QXYSeries();
19 QXYChartSeries* series0 = QXYChartSeries::create();
19 20 series0->setColor(Qt::blue);
20 QXYSeries* series1 = new QXYSeries();
21 QXYChartSeries* series1 = QXYChartSeries::create();
21 22 series1->setColor(Qt::red);
22 QXYSeries* series2 = new QXYSeries();
23 QXYChartSeries* series2 = QXYChartSeries::create();
23 24 series2->setColor(Qt::gray);
24 QXYSeries* series3 = new QXYSeries();
25 QXYChartSeries* series3 = QXYChartSeries::create();
25 26 series3->setColor(Qt::green);
26 27
27 28 int numPoints = 100;
28 29
29 30 for (int x = 0; x < numPoints; ++x) {
30 31 series0->add(x,0);
31 32 series1->add(x, abs(sin(PI/50*x)*100));
32 33 series2->add(x, abs(cos(PI/50*x)*100));
33 34 series3->add(x,100);
34 35 }
35 36
36 QList<QXYSeries*> dataset;
37 QList<QXYChartSeries*> dataset;
37 38
38 qDebug()<<"Series 1:" << *series1;
39 qDebug()<<"Series 2:" << *series2;
39 //qDebug()<<"Series 1:" << *series1;
40 //qDebug()<<"Series 2:" << *series2;
40 41
41 42 dataset << series0;
42 43 dataset << series1;
43 44 dataset << series2;
44 45 dataset << series3;
45 46
46 QChart* chart = QChart::createXYLineChart(dataset);
47 chart->setMargin(50);
48 QChartWidget* chartWidget = new QChartWidget();
49 chartWidget->addChart(chart);
47 QChartWidget* chartWidget = new QChartWidget(&window);
48 chartWidget->addSeries(series1);
49 chartWidget->addSeries(series2);
50 50
51 ChartView* view = new ChartView(chartWidget,&window);
52 window.setCentralWidget(view);
51 window.setCentralWidget(chartWidget);
53 52 window.resize(400, 300);
54 53 window.show();
55 54
56 55 return a.exec();
57 56 }
@@ -1,29 +1,75
1 1 #include "qchart.h"
2 #include "xylinechart_p.h"
2 #include "qchartseries.h"
3 #include "xylinechartitem_p.h"
4 #include "axis_p.h"
5 #include "xygrid_p.h"
6 #include <QDebug>
3 7
4 8 QCHART_BEGIN_NAMESPACE
5 9
6 10 QChart::QChart(QGraphicsItem* parent):QGraphicsItem(parent),
7 m_marginSize(0)
11 m_marginSize(0),
12 m_axisX(new Axis(this)),
13 m_axisY(new Axis(this)),
14 m_grid(new XYGrid(this)),
15 m_plotDataIndex(0)
8 16 {
9 17 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
18 // set axis
19 m_axisY->rotate(90);
20
21 XYPlotData data;
22 data.m_minX = 0.0;
23 data.m_maxX = 100.0;
24 data.m_minY = 0.0;
25 data.m_maxY = 100.0;
26 data.m_ticksX=4;
27 data.m_ticksY=4;
28
29 m_plotDataList.clear();
30 m_plotDataList << data;
31
32 m_grid->setZValue(10);
33 m_grid->setXYPlotData(m_plotDataList.at(0));
10 34 }
11 35
12 36 QChart::~QChart(){}
13 37
38 QRectF QChart::boundingRect() const
39 {
40 return m_rect;
41 }
42
43 void QChart::addSeries(QChartSeries* series)
44 {
45 m_series<<series;
14 46
15 QChart* QChart::createXYLineChart(const QList<QXYSeries*>& dataset)
47 switch(series->type())
16 48 {
17 XYLineChart* chart = new XYLineChart();
18 foreach (const QXYSeries* series,dataset) {
19 chart->addXYSeries(series);
49 case QChartSeries::LINE:
50 qDebug()<<"xyline added";
51 XYLineChartItem* item = new XYLineChartItem(reinterpret_cast<QXYChartSeries*>(series),this);
52 item->setXYPlotData(m_plotDataList.at(0));
53 m_items<<item;
54 break;
55 }
20 56 }
21 return chart;
57
58 void QChart::setSize(const QSizeF& size) {
59 //TODO refactor to setGeometry
60 m_rect = QRect(QPoint(0,0),size.toSize());
61 m_rect.adjust(margin(),margin(),-margin(),-margin());
62 m_grid->setPos(m_rect.topLeft());
63 m_grid->setSize(m_rect.size());
64 foreach(QGraphicsItem* item , m_items)
65 reinterpret_cast<XYLineChartItem*>(item)->setChartSize(m_rect);
66 update();
67
22 68 }
23 69
24 70 void QChart::setMargin(int margin)
25 71 {
26 72 m_marginSize = margin;
27 73 }
28 74
29 75 QCHART_END_NAMESPACE
@@ -1,40 +1,46
1 1 #ifndef CHART_H
2 2 #define CHART_H
3 3
4 4 #include <qchartconfig.h>
5 #include <qxyseries.h>
5 //TODO: temporary class
6 #include <xyplotdata_p.h>
6 7 #include <QGraphicsItem>
7 8
8 9 QCHART_BEGIN_NAMESPACE
9 10
11 class Axis;
12 class XYGrid;
13 class QChartSeries;
14
10 15 class QCHART_EXPORT QChart : public QGraphicsItem
11 16 {
12 public:
13 enum DataSeriesType {
14 DataSeriesTypeLine = 0,
15 DataSeriesTypeArea,
16 DataSeriesTypeBar,
17 DataSeriesTypePie,
18 DataSeriesTypeScatter,
19 DataSeriesTypeSpline
20 };
21
22 protected:
23 QChart(QGraphicsItem* parent =0);
24 17
25 18 public:
19 QChart(QGraphicsItem* parent = 0);
26 20 virtual ~QChart();
27 21
28 static QChart* createXYLineChart(const QList<QXYSeries*>& dataset);
22 //from QGraphicsItem
23 virtual QRectF boundingRect() const;
24 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
25
26 void addSeries(QChartSeries* series);
29 27
30 virtual void setSize(const QSizeF& rect)=0;
28 virtual void setSize(const QSizeF& rect);
31 29 void setMargin(int margin);
32 30 int margin() const { return m_marginSize;}
33 31
34 32 private:
33 QRect m_rect;
34 QList<const QChartSeries*> m_series;
35 Axis* m_axisX;
36 Axis* m_axisY;
37 XYGrid* m_grid;
38 QList<XYPlotData> m_plotDataList;
39 QList<QGraphicsItem*> m_items;
40 int m_plotDataIndex;
35 41 int m_marginSize;
36 42 };
37 43
38 44 QCHART_END_NAMESPACE
39 45
40 46 #endif
@@ -1,53 +1,65
1 1 #include "qchartwidget.h"
2 #include "qxyseries.h"
3 #include "xylinechart_p.h"
2 #include "qchartseries.h"
4 3 #include <QGraphicsView>
5 4 #include <QGraphicsScene>
5 #include <QResizeEvent>
6 6
7 7 QCHART_BEGIN_NAMESPACE
8 8
9 9 class QChartWidgetPrivate
10 10 {
11 11 public:
12 QChartWidgetPrivate(QWidget *parent) : m_view(0), m_scene(0), m_chart(0) {
12 QChartWidgetPrivate(QChartWidget *parent) :
13 m_view(0),
14 m_scene(0),
15 m_chart(0),
16 q_ptr( parent )
17 {
13 18 m_scene = new QGraphicsScene();
14 m_view = new QGraphicsView(m_scene, parent);
15 m_view->resize(490, 300);
16 m_view->show();
19 m_view = new QGraphicsView(parent);
20 m_view->setScene(m_scene);
21 m_chart = new QChart();
22 m_scene->addItem(m_chart);
17 23 }
24
18 25 ~QChartWidgetPrivate() {
19 delete m_view;
20 delete m_scene;
21 26 }
22 27
23 28 QGraphicsView *m_view;
24 29 QGraphicsScene *m_scene;
25 30 QChart* m_chart;
31 QChartWidget * const q_ptr;
32 Q_DECLARE_PUBLIC(QChartWidget);
26 33 };
27 34
28 35 QChartWidget::QChartWidget(QWidget *parent) :
29 36 QWidget(parent),
30 d(new QChartWidgetPrivate(this))
37 d_ptr(new QChartWidgetPrivate(this))
31 38 {
32 setMinimumSize(d->m_view->size());
39
33 40 }
34 41
35 42 QChartWidget::~QChartWidget()
36 43 {
37 delete d;
44 delete d_ptr;
38 45 }
39 46
40 void QChartWidget::addDataSeries(
41 QChart::DataSeriesType dataSeriesType,
42 QList<QXYSeries*> dataset)
47 void QChartWidget::resizeEvent(QResizeEvent *event)
43 48 {
44 // TODO: implement management of several data series of different types
49 Q_D(QChartWidget);
50 d->m_view->resize(size().width(),size().height());
51 d->m_scene->setSceneRect(0,0,size().width(),size().height());
52 d->m_chart->setSize(size());
53 QWidget::resizeEvent(event);
54 }
55
45 56
46 d->m_chart = QChart::createXYLineChart(dataset);
47 d->m_scene->addItem(d->m_chart);
48 d->m_chart->setSize(this->size());
57 void QChartWidget::addSeries(QChartSeries* series)
58 {
59 Q_D(QChartWidget);
60 d->m_chart->addSeries(series);
49 61 }
50 62
51 63 #include "moc_qchartwidget.cpp"
52 64
53 65 QCHART_END_NAMESPACE
@@ -1,36 +1,35
1 1 #ifndef QCHARTWIDGET_H
2 2 #define QCHARTWIDGET_H
3 3
4 4 #include "qchartconfig.h"
5 5 #include "qchart.h"
6 6 #include <QWidget>
7 7
8 8 QCHART_BEGIN_NAMESPACE
9 9
10 class QXYSeries;
10 class QChartSeries;
11 11 class QChartWidgetPrivate;
12 12
13 13 class QCHART_EXPORT QChartWidget : public QWidget
14 14 {
15 15 Q_OBJECT
16 16 public:
17 17 explicit QChartWidget(QWidget *parent = 0);
18 18 ~QChartWidget();
19 // TODO: replace QXYSeries list with a charts data API
20 // TODO: return QChartDataSeries
21 void addDataSeries(QChart::DataSeriesType dataSeriesType, QList<QXYSeries*> dataset);
22 19
23 signals:
20 //implement from QWidget
21 void resizeEvent(QResizeEvent *event);
24 22
25 public slots:
23 void addSeries(QChartSeries* series);
24 protected:
25 QChartWidgetPrivate * const d_ptr;
26 26
27 27 private:
28 friend class QChartWidgetPrivate;
29 28 Q_DISABLE_COPY(QChartWidget)
30 // Q_DECLARE_PRIVATE(QChartWidget)
31 QChartWidgetPrivate * const d;
29 Q_DECLARE_PRIVATE(QChartWidget)
30
32 31 };
33 32
34 33 QCHART_END_NAMESPACE
35 34
36 35 #endif // QCHARTWIDGET_H
@@ -1,45 +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 xylinechart/qchartgraphicswidget.cpp \
10 xylinechart/qxyseries.cpp \
11 xylinechart/xylinechart.cpp \
9 xylinechart/qxychartseries.cpp \
10 xylinechart/xylinechartitem.cpp \
12 11 xylinechart/xygrid.cpp \
13 12 xylinechart/xyplotdata.cpp \
14 13 qchart.cpp \
15 14 axis.cpp \
16 15 qchartwidget.cpp
17 16
18 17 PRIVATE_HEADERS += \
19 xylinechart/xylinechart_p.h \
18 xylinechart/xylinechartitem_p.h \
20 19 xylinechart/xygrid_p.h \
21 xylinechart/xyplotdata_p.h \
22 20 axis_p.h
21
23 22 PUBLIC_HEADERS += \
24 xylinechart/qxyseries.h \
25 xylinechart/qchartgraphicswidget.h \
23 qchartseries.h \
26 24 qchart.h \
27 25 qchartwidget.h \
28 qchartconfig.h
26 qchartconfig.h \
27 xylinechart/qxychartseries.h \
28 xylinechart/xyplotdata_p.h # to be removed
29
29 30 HEADERS += $$PUBLIC_HEADERS
30 31 HEADERS += $$PRIVATE_HEADERS
31 32
32 33 INCLUDEPATH += xylinechart \
33 34 .
34 35
35 36 OBJECTS_DIR = ../build/lib
36 37 MOC_DIR = ../build/lib
37 38 UI_DIR = ../build/lib
38 39 RCC_DIR = ../build/lib
39 40 DEFINES += QCHART_LIBRARY
40 41
41 42 public_headers.path = $$[QT_INSTALL_HEADERS]/QCharts
42 43 public_headers.files = $$PUBLIC_HEADERS
43 44 target.path = $$[QT_INSTALL_LIBS]
44 45 INSTALLS += target \
45 46 public_headers
@@ -1,63 +1,68
1 #include "qxyseries.h"
1 #include "qxychartseries.h"
2 2
3 3 QCHART_BEGIN_NAMESPACE
4 4
5 QXYSeries::QXYSeries():
5 QXYChartSeries::QXYChartSeries(QObject* parent):QChartSeries(parent),
6 6 m_color(Qt::black)
7 7 {
8 8 }
9 9
10 QXYSeries::~QXYSeries()
10 QXYChartSeries::~QXYChartSeries()
11 11 {
12 12 }
13 13
14 QXYChartSeries* QXYChartSeries::create(QObject* parent)
15 {
16 //TODO: here we take QChartData when it is ready
17 // return null if malformed;
18 return new QXYChartSeries(parent);
19 }
14 20
15 void QXYSeries::setColor(const QColor& color)
21 void QXYChartSeries::setColor(const QColor& color)
16 22 {
17 23 m_color = color;
18 24 }
19 25
20
21 void QXYSeries::add(qreal x,qreal y)
26 void QXYChartSeries::add(qreal x,qreal y)
22 27 {
23 28 m_x<<x;
24 29 m_y<<y;
25 30 }
26 31
27 void QXYSeries::clear()
32 void QXYChartSeries::clear()
28 33 {
29 34 m_x.clear();
30 35 m_y.clear();
31 36 }
32 37
33 qreal QXYSeries::x(int pos) const
38 qreal QXYChartSeries::x(int pos) const
34 39 {
35 40 return m_x.at(pos);
36 41 }
37 42
38 qreal QXYSeries::y(int pos) const
43 qreal QXYChartSeries::y(int pos) const
39 44 {
40 45 return m_y.at(pos);
41 46 }
42 47
43 int QXYSeries::count() const
48 int QXYChartSeries::count() const
44 49 {
45 50 Q_ASSERT(m_x.size() == m_y.size());
46 51
47 52 return m_x.size();
48 53
49 54 }
50 55
51 QDebug operator<< (QDebug debug, const QXYSeries series)
56 QDebug operator<< (QDebug debug, const QXYChartSeries series)
52 57 {
53 58 Q_ASSERT(series.m_x.size() == series.m_y.size());
54 59
55 60 int size = series.m_x.size();
56 61
57 62 for (int i=0;i<size;i++) {
58 63 debug.nospace() << "(" << series.m_x.at(i) << ','<< series.m_y.at(i) << ") ";
59 64 }
60 65 return debug.space();
61 66 }
62 67
63 68 QCHART_END_NAMESPACE
@@ -1,33 +1,40
1 1 #ifndef QXYSERIES_H_
2 2 #define QXYSERIES_H_
3 3
4 4 #include "qchartconfig.h"
5 #include "qchartseries.h"
5 6 #include <QDebug>
6 7 #include <QColor>
7 8
8 9 QCHART_BEGIN_NAMESPACE
9 10
10 class QCHART_EXPORT QXYSeries
11 class QCHART_EXPORT QXYChartSeries : public QChartSeries
11 12 {
13 private:
14 QXYChartSeries(QObject* parent=0);
12 15 public:
13 QXYSeries();
14 virtual ~QXYSeries();
16 virtual ~QXYChartSeries();
17
18 //implemented from QChartSeries
19 static QXYChartSeries* create(QObject* parent=0);
20 virtual QChartSeriesType type() const { return QChartSeries::LINE;};
21
15 22 void add(qreal x, qreal y);
16 23 void clear();
17 24 void setColor(const QColor& color);
18 25 const QColor& color() const { return m_color;}
19 26 int count() const;
20 27 qreal x(int pos) const;
21 28 qreal y(int pos) const;
22 friend QDebug operator<< (QDebug d, const QXYSeries series);
29 friend QDebug operator<< (QDebug d, const QXYChartSeries series);
23 30
24 31 private:
25 32 QColor m_color;
26 33 QList<qreal> m_x;
27 34 QList<qreal> m_y;
28 35
29 36 };
30 37
31 38 QCHART_END_NAMESPACE
32 39
33 40 #endif
@@ -1,67 +1,70
1 1 #include "xygrid_p.h"
2 #include "xylinechart_p.h"
3 2 #include <QPainter>
3 #include <QDebug>
4 4
5 5 QCHART_BEGIN_NAMESPACE
6 6
7 XYGrid::XYGrid(const XYLineChart* const chart, QGraphicsItem* parent):QGraphicsItem(parent),
8 m_chart(chart)
7 XYGrid::XYGrid(QGraphicsItem* parent):QGraphicsItem(parent)
9 8 {
10 9 }
11 10
12 11 XYGrid::~XYGrid()
13 12 {
14 13 // TODO Auto-generated destructor stub
15 14 }
16 15
17 void XYGrid::setSize(const QSizeF& size) {
16 void XYGrid::setSize(const QSizeF& size)
17 {
18 18 m_rect.setSize(size.toSize());
19 19 }
20 20
21 void XYGrid::setXYPlotData(const XYPlotData& xyPlotData)
22 {
23 m_xyPlotData = xyPlotData;
24 }
21 25
22 26 QRectF XYGrid::boundingRect() const
23 27 {
24 28 return m_rect;
25 29 }
26 30
27 31 void XYGrid::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
28 32 {
33
29 34 if (!m_rect.isValid())
30 35 return;
31 36
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();
37 const qreal deltaX = (m_rect.width() -1) / m_xyPlotData.ticksX();
38 const qreal deltaY = (m_rect.height() - 1) / m_xyPlotData.ticksY();
36 39
37 for (int i = 0; i <= plotData.ticksX(); ++i) {
40 for (int i = 0; i <= m_xyPlotData.ticksX(); ++i) {
38 41
39 42 int x = i * deltaX + m_rect.left();
40 qreal label = plotData.m_minX + (i * plotData.spanX()
41 / plotData.ticksX());
43 qreal label = m_xyPlotData.m_minX + (i * m_xyPlotData.spanX()
44 / m_xyPlotData.ticksX());
42 45 painter->drawLine(x, m_rect.top()+1, x, m_rect.bottom());
43 46 //painter->drawLine(x, m_rect.bottom(), x, m_rect.bottom() + 5);
44 47
45 48 painter->drawText(x - 50, m_rect.bottom() + 5, 100, 20,
46 49 Qt::AlignHCenter | Qt::AlignTop,
47 50 QString::number(label));
48 51 }
49 52
50 for (int j = 0; j <= plotData.ticksY(); ++j) {
53 for (int j = 0; j <= m_xyPlotData.ticksY(); ++j) {
51 54
52 55 int y = j * -deltaY + m_rect.bottom();
53 qreal label = plotData.m_minY + (j * plotData.spanY()
54 / plotData.ticksY());
56 qreal label = m_xyPlotData.m_minY + (j * m_xyPlotData.spanY()
57 / m_xyPlotData.ticksY());
55 58
56 59 painter->drawLine(m_rect.left(), y, m_rect.right()-1, y);
57 60 //painter->drawLine(m_rect.left() - 5, y, m_rect.left(), y);
58 61 //TODO : margin = 50 ;
59 62 painter->drawText(m_rect.left() - 50, y - 10, 50 - 5, 20,
60 63 Qt::AlignRight | Qt::AlignVCenter,
61 64 QString::number(label));
62 65 }
63 66
64 67 //painter->drawRect(m_rect.adjusted(0, 0, -1, -1));
65 68 }
66 69
67 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 6 #include <QGraphicsItem>
6 7
7 8 QCHART_BEGIN_NAMESPACE
8 9
9 class XYPlotData;
10 class XYLineChart;
11
12 10 class XYGrid : public QGraphicsItem
13 11 {
14 12 public:
15 XYGrid(const XYLineChart* const chart,QGraphicsItem* parent = 0);
13 XYGrid(QGraphicsItem* parent = 0);
16 14 virtual ~XYGrid();
17 15
18 16 //from QGraphicsItem
19 17 virtual QRectF boundingRect() const;
20 18 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
21 19
20 //TODO: this is just temporary interface
21 void setXYPlotData(const XYPlotData& xyPlotData);
22 22 void setSize(const QSizeF& rect);
23 23
24 24 private:
25 25 QRectF m_rect;
26 const XYLineChart* const m_chart;
26 XYPlotData m_xyPlotData;
27 27 };
28 28
29 29 QCHART_END_NAMESPACE
30 30
31 31 #endif /* XYGRID_H_ */
@@ -1,222 +1,222
1 1 #include "mainwidget.h"
2 2 #include "dataseriedialog.h"
3 3 #include <qxyseries.h>
4 4 #include <QPushButton>
5 5 #include <QComboBox>
6 6 #include <QSpinBox>
7 7 #include <QCheckBox>
8 8 #include <QGridLayout>
9 9 #include <QHBoxLayout>
10 10 #include <QLabel>
11 11 #include <QSpacerItem>
12 12 #include <QMessageBox>
13 13 #include <cmath>
14 14 #include <QDebug>
15 15
16 16 QCHART_USE_NAMESPACE
17 17
18 18 MainWidget::MainWidget(QWidget *parent) :
19 19 QWidget(parent)
20 20 {
21 21 m_chartWidget = new QChartWidget(this);
22 22 // m_chartWidget->resize(QSize(200,200));
23 23 // m_chartWidget->setColor(Qt::red);
24 24
25 25 QPushButton *addSeriesButton = new QPushButton("Add series");
26 26 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
27 27
28 28 // Chart background
29 29 QComboBox *backgroundCombo = new QComboBox(this);
30 30 backgroundCombo->addItem("None");
31 31 backgroundCombo->addItem("TODO Grid");
32 32 backgroundCombo->addItem("TODO Image");
33 33 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
34 34 this, SLOT(backgroundChanged(int)));
35 35
36 36 // Axis
37 37 // TODO: multiple axes?
38 38 m_autoScaleCheck = new QCheckBox("Automatic scaling");
39 39 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
40 40 // Allow setting also non-sense values (like -2147483648 and 2147483647)
41 41 m_xMinSpin = new QSpinBox();
42 42 m_xMinSpin->setMinimum(INT_MIN);
43 43 m_xMinSpin->setMaximum(INT_MAX);
44 44 m_xMinSpin->setValue(0);
45 45 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
46 46 m_xMaxSpin = new QSpinBox();
47 47 m_xMaxSpin->setMinimum(INT_MIN);
48 48 m_xMaxSpin->setMaximum(INT_MAX);
49 49 m_xMaxSpin->setValue(10);
50 50 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
51 51 m_yMinSpin = new QSpinBox();
52 52 m_yMinSpin->setMinimum(INT_MIN);
53 53 m_yMinSpin->setMaximum(INT_MAX);
54 54 m_yMinSpin->setValue(0);
55 55 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
56 56 m_yMaxSpin = new QSpinBox();
57 57 m_yMaxSpin->setMinimum(INT_MIN);
58 58 m_yMaxSpin->setMaximum(INT_MAX);
59 59 m_yMaxSpin->setValue(10);
60 60 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
61 61
62 62 QGridLayout *grid = new QGridLayout();
63 63 QHBoxLayout *hbox = new QHBoxLayout();
64 64 //grid->addWidget(new QLabel("Add series:"), 0, 0);
65 65 grid->addWidget(addSeriesButton, 0, 1);
66 66 grid->addWidget(new QLabel("Background:"), 2, 0);
67 67 grid->addWidget(backgroundCombo, 2, 1);
68 68 grid->addWidget(m_autoScaleCheck, 3, 0);
69 69 grid->addWidget(new QLabel("x min:"), 4, 0);
70 70 grid->addWidget(m_xMinSpin, 4, 1);
71 71 grid->addWidget(new QLabel("x max:"), 5, 0);
72 72 grid->addWidget(m_xMaxSpin, 5, 1);
73 73 grid->addWidget(new QLabel("y min:"), 6, 0);
74 74 grid->addWidget(m_yMinSpin, 6, 1);
75 75 grid->addWidget(new QLabel("y max:"), 7, 0);
76 76 grid->addWidget(m_yMaxSpin, 7, 1);
77 77 // add row with empty label to make all the other rows static
78 78 grid->addWidget(new QLabel(""), 8, 0);
79 79 grid->setRowStretch(8, 1);
80 80
81 81 hbox->addLayout(grid);
82 82 hbox->addWidget(m_chartWidget);
83 83 hbox->setStretch(1, 1);
84 84
85 85 setLayout(hbox);
86 86
87 87 m_autoScaleCheck->setChecked(true);
88 88 chartTypeChanged(4);
89 89 testDataChanged(0);
90 90 }
91 91
92 92 void MainWidget::addSeries()
93 93 {
94 94 DataSerieDialog dialog(this);
95 95 connect(&dialog, SIGNAL(accepted(QString, QString)), this, SLOT(addSeries(QString, QString)));
96 96 dialog.exec();
97 97 }
98 98
99 99 void MainWidget::addSeries(QString series, QString data)
100 100 {
101 101 qDebug() << "addSeries: " << series << " data: " << data;
102 102
103 QXYSeries* series0 = new QXYSeries();
103 QXYChartSeries* series0 = new QXYChartSeries();
104 104
105 105 if (data == "linear") {
106 106 // TODO
107 107 } else if (data == "SIN") {
108 108 series0->setColor(Qt::blue);
109 109 for (int x = 0; x < 100; x++)
110 110 series0->add(x, abs(sin(3.14159265358979 / 50 * x) * 100));
111 QList<QXYSeries*> dataset;
111 QList<QXYChartSeries*> dataset;
112 112 dataset << series0;
113 113 } else if (data == "SIN + random") {
114 114 series0->setColor(Qt::blue);
115 115 for (qreal x = 0; x < 100; x += 0.1) {
116 116 series0->add(x + (rand() % 5),
117 117 abs(sin(3.14159265358979 / 50 * x) * 100) + (rand() % 5));
118 118 }
119 119 } else {
120 120 // TODO: check if data has a valid file name
121 121 }
122 122
123 QList<QXYSeries*> dataset;
123 QList<QXYChartSeries*> dataset;
124 124 dataset << series0;
125 125
126 126 if (series == "Scatter") {
127 127 m_chartWidget->addDataSeries(QChart::DataSeriesTypeScatter, dataset);
128 128 } else if (series == "Line") {
129 129 m_chartWidget->addDataSeries(QChart::DataSeriesTypeLine, dataset);
130 130 } else {
131 131 // TODO
132 132 }
133 133 }
134 134
135 135 void MainWidget::chartTypeChanged(int itemIndex)
136 136 {
137 137 // TODO: change chart type
138 138 switch (itemIndex) {
139 139 case 4:
140 140 //m_chartWidget->setType(4);
141 141 break;
142 142 default: {
143 143 //m_chartWidget->setType(0);
144 144 break;
145 145 }
146 146 }
147 147 }
148 148
149 149 void MainWidget::testDataChanged(int itemIndex)
150 150 {
151 151 qDebug() << "testDataChanged: " << itemIndex;
152 152
153 153 // switch (itemIndex) {
154 154 // case 0: {
155 155 // QList<QChartDataPoint> data;
156 156 // for (int x = 0; x < 20; x++) {
157 157 // data.append(QChartDataPoint() << x << x / 2);
158 158 // }
159 159 // m_chartWidget->setData(data);
160 160 // break;
161 161 // }
162 162 // case 1: {
163 163 // QList<QChartDataPoint> data;
164 164 // for (int x = 0; x < 100; x++) {
165 165 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100));
166 166 // }
167 167 // m_chartWidget->setData(data);
168 168 // break;
169 169 // }
170 170 // case 2: {
171 171 // QList<QChartDataPoint> data;
172 172 // for (int x = 0; x < 1000; x++) {
173 173 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
174 174 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
175 175 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
176 176 // }
177 177 // m_chartWidget->setData(data);
178 178 // break;
179 179 // }
180 180 // default:
181 181 // break;
182 182 // }
183 183 }
184 184
185 185 void MainWidget::backgroundChanged(int itemIndex)
186 186 {
187 187 qDebug() << "backgroundChanged: " << itemIndex;
188 188 }
189 189
190 190 void MainWidget::autoScaleChanged(int value)
191 191 {
192 192 if (value) {
193 193 // TODO: enable auto scaling
194 194 } else {
195 195 // TODO: set scaling manually (and disable auto scaling)
196 196 }
197 197
198 198 m_xMinSpin->setEnabled(!value);
199 199 m_xMaxSpin->setEnabled(!value);
200 200 m_yMinSpin->setEnabled(!value);
201 201 m_yMaxSpin->setEnabled(!value);
202 202 }
203 203
204 204 void MainWidget::xMinChanged(int value)
205 205 {
206 206 qDebug() << "xMinChanged: " << value;
207 207 }
208 208
209 209 void MainWidget::xMaxChanged(int value)
210 210 {
211 211 qDebug() << "xMaxChanged: " << value;
212 212 }
213 213
214 214 void MainWidget::yMinChanged(int value)
215 215 {
216 216 qDebug() << "yMinChanged: " << value;
217 217 }
218 218
219 219 void MainWidget::yMaxChanged(int value)
220 220 {
221 221 qDebug() << "yMaxChanged: " << value;
222 222 }
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now