From 54a770e32f2a53ea86b5e94e83c79d22a45c50e4 2012-01-16 08:13:25 From: Michal Klocek Date: 2012-01-16 08:13:25 Subject: [PATCH] adds missing files form previous commit --- diff --git a/example/example.pro b/example/example.pro new file mode 100644 index 0000000..1c2d9eb --- /dev/null +++ b/example/example.pro @@ -0,0 +1,2 @@ +TEMPLATE = subdirs +SUBDIRS += linechart \ No newline at end of file diff --git a/example/linechart/chartview.cpp b/example/linechart/chartview.cpp new file mode 100644 index 0000000..78b8fdf --- /dev/null +++ b/example/linechart/chartview.cpp @@ -0,0 +1,22 @@ +#include "chartview.h" + +ChartView::ChartView(QChartWidget* chartWidget,QWidget* parent):QGraphicsView(parent), +m_chartWidget(chartWidget), +m_scene(new QGraphicsScene(parent)) +{ + setScene(m_scene); + m_scene->addItem(chartWidget); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + // get rid off ugly default 1 pixel border + setStyleSheet( "QGraphicsView { border-style: none; }" ); +} + +ChartView::~ChartView() { + // TODO Auto-generated destructor stub +} + +void ChartView::resizeEvent(QResizeEvent *event){ + m_chartWidget->resize(size()); + QGraphicsView::resizeEvent(event); +} diff --git a/example/linechart/chartview.h b/example/linechart/chartview.h new file mode 100644 index 0000000..48f62b6 --- /dev/null +++ b/example/linechart/chartview.h @@ -0,0 +1,22 @@ +#ifndef CHARTVIEW_H_ +#define CHARTVIEW_H_ +#include +#include + +class QGraphicsScene; + +QCHART_USE_NAMESPACE + +class ChartView: public QGraphicsView +{ +public: + ChartView (QChartWidget* chartWidget,QWidget* parent=0); + virtual ~ChartView(); + void resizeEvent(QResizeEvent *event); + +private: + QChartWidget* m_chartWidget; + QGraphicsScene* m_scene; +}; + +#endif /* CHARTVIEW_H_ */ diff --git a/example/linechart/linechart.pro b/example/linechart/linechart.pro new file mode 100644 index 0000000..60a293b --- /dev/null +++ b/example/linechart/linechart.pro @@ -0,0 +1,9 @@ +TARGET = lineChart +TEMPLATE = app + +QT += core gui + +CONFIG += charts + +HEADERS += chartview.h +SOURCES += main.cpp chartview.cpp diff --git a/example/linechart/main.cpp b/example/linechart/main.cpp new file mode 100644 index 0000000..4919fb0 --- /dev/null +++ b/example/linechart/main.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include "chartview.h" +#include + +QCHART_USE_NAMESPACE + +#define PI 3.14159265358979 + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + + QMainWindow window; + + QXYSeries* series0 = new QXYSeries(); + series0->setColor(Qt::blue); + QXYSeries* series1 = new QXYSeries(); + series1->setColor(Qt::red); + QXYSeries* series2 = new QXYSeries(); + series2->setColor(Qt::gray); + QXYSeries* series3 = new QXYSeries(); + series3->setColor(Qt::green); + + int numPoints = 100; + + for (int x = 0; x < numPoints; ++x) { + series0->add(x,0); + series1->add(x, uint(sin(PI/25*x)*100) % 100); + series2->add(x, uint(cos(PI/25*x)*100) % 100); + series3->add(x,100); + } + + QList dataset; + + qDebug()<<"Series 1:" << *series1; + qDebug()<<"Series 2:" << *series2; + + dataset << series0; + dataset << series1; + dataset << series2; + dataset << series3; + + QChart* chart = QChart::createXYLineChart(dataset); + chart->setMargin(50); + QChartWidget* chartWidget = new QChartWidget(); + chartWidget->addChart(chart); + + ChartView* view = new ChartView(chartWidget,&window); + window.setCentralWidget(view); + window.resize(400, 300); + window.show(); + + return a.exec(); +} diff --git a/src/xylinechart/axis.cpp b/src/xylinechart/axis.cpp new file mode 100644 index 0000000..c40fea3 --- /dev/null +++ b/src/xylinechart/axis.cpp @@ -0,0 +1,50 @@ +#include "axis_p.h" +#include +#include + +QCHART_BEGIN_NAMESPACE + +Axis::Axis(QGraphicsItem* parent): QGraphicsItem(parent) +{ +} + +Axis::~Axis() +{ +} + +void Axis::setLength(int length) +{ + QPainterPath path; + path.moveTo(QPointF(0,0)); + path.lineTo(length,0); + // path.lineTo(length-4,0); + // path.lineTo(length,3); + // path.lineTo(length-4,6); + // path.lineTo(length-4,4); + // path.lineTo(0,4); + // path.lineTo(0,2); + m_path=path; + update(); +} + +QRectF Axis::boundingRect() const +{ + return m_path.boundingRect(); +} + +QPainterPath Axis::shape() const +{ + return m_path; +} + +void Axis::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget) +{ + painter->save(); + QPen pen(Qt::black); + //pen.setWidth(10); + painter->setPen(pen); + painter->drawPath(m_path); + painter->restore(); +} + +QCHART_END_NAMESPACE diff --git a/src/xylinechart/axis_p.h b/src/xylinechart/axis_p.h new file mode 100644 index 0000000..55c9495 --- /dev/null +++ b/src/xylinechart/axis_p.h @@ -0,0 +1,29 @@ +#ifndef AXIS_H_ +#define AXIS_H_ + +#include +#include + +QCHART_BEGIN_NAMESPACE + +class Axis: public QGraphicsItem +{ +public: + Axis(QGraphicsItem* parent = 0); + virtual ~Axis(); + + //from QGraphicsItem + virtual QPainterPath shape() const; + virtual QRectF boundingRect() const; + virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + + void setLength(int length); + void setWidth(int width); + +private: + QPainterPath m_path; +}; + +QCHART_END_NAMESPACE + +#endif /* AXIS_H_ */ diff --git a/src/xylinechart/qchart.cpp b/src/xylinechart/qchart.cpp new file mode 100644 index 0000000..30fa184 --- /dev/null +++ b/src/xylinechart/qchart.cpp @@ -0,0 +1,30 @@ +#include "qchart.h" +#include "xylinechart_p.h" + +QCHART_BEGIN_NAMESPACE + +QChart::QChart(QGraphicsItem* parent):QGraphicsItem(parent), +m_marginSize(0) +{ + // setFlags(QGraphicsItem::ItemClipsChildrenToShape); +} + +QChart::~QChart(){} + + +QChart* QChart::createXYLineChart(const QList& dataset) +{ + + XYLineChart* chart = new XYLineChart(); + foreach (const QXYSeries* series,dataset) { + chart->addXYSeries(series); + } + return chart; +} + +void QChart::setMargin(int margin) +{ + m_marginSize=margin; +} + +QCHART_END_NAMESPACE diff --git a/src/xylinechart/qchart.h b/src/xylinechart/qchart.h new file mode 100644 index 0000000..9c57036 --- /dev/null +++ b/src/xylinechart/qchart.h @@ -0,0 +1,31 @@ +#ifndef CHART_H +#define CHART_H + +#include +#include +#include + +QCHART_BEGIN_NAMESPACE + +class QCHART_EXPORT QChart : public QGraphicsItem +{ + +protected: + QChart(QGraphicsItem* parent =0); + +public: + virtual ~QChart(); + + static QChart* createXYLineChart(const QList& dataset); + + virtual void setSize(const QSizeF& rect)=0; + void setMargin(int margin); + int margin() const { return m_marginSize;} + +private: + int m_marginSize; +}; + +QCHART_END_NAMESPACE + +#endif diff --git a/src/xylinechart/qchartwidget.cpp b/src/xylinechart/qchartwidget.cpp new file mode 100644 index 0000000..1c39c6b --- /dev/null +++ b/src/xylinechart/qchartwidget.cpp @@ -0,0 +1,41 @@ +#include "qchartwidget.h" +#include "qchart.h" + +QCHART_BEGIN_NAMESPACE + + +class QChartWidgetPrivate +{ +public: + QChart* chart; + //Q_DECLARE_PUBLIC(ChartWidget) + QChartWidgetPrivate() {} +}; + +//////////////////////////////////////////////////////////////////////////// + +QChartWidget::QChartWidget(QGraphicsItem *parent,Qt::WindowFlags wFlags) + : QGraphicsWidget(parent,wFlags), + d_ptr(new QChartWidgetPrivate()) +{ +} + +QChartWidget::~QChartWidget() +{ + delete d_ptr; +} + + +void QChartWidget::addChart(QChart *chart) +{ + d_ptr->chart=chart; + chart->setParentItem(this); +} + +void QChartWidget::setGeometry(const QRectF & rect) +{ + d_ptr->chart->setSize(rect.size()); + QGraphicsWidget::setGeometry(rect); +} + +QCHART_END_NAMESPACE diff --git a/src/xylinechart/qchartwidget.h b/src/xylinechart/qchartwidget.h new file mode 100644 index 0000000..716b711 --- /dev/null +++ b/src/xylinechart/qchartwidget.h @@ -0,0 +1,34 @@ +#ifndef QCHARTWIDGET_H +#define QCHARTWIDGET_H + +#include +#include + +class QGraphicsItem; + +QCHART_BEGIN_NAMESPACE + +class QChart; +class QChartWidgetPrivate; + +class QCHART_EXPORT QChartWidget : public QGraphicsWidget +{ + +public: + explicit QChartWidget(QGraphicsItem *parent = 0,Qt::WindowFlags wFlags = 0); + ~QChartWidget(); + + QChart* chart() const; + void addChart(QChart* chart); + + virtual void setGeometry(const QRectF & rect); + +private: + Q_DISABLE_COPY(QChartWidget) + Q_DECLARE_PRIVATE(QChartWidget) + QChartWidgetPrivate * const d_ptr; +}; + +QCHART_END_NAMESPACE + +#endif diff --git a/src/xylinechart/qxyseries.cpp b/src/xylinechart/qxyseries.cpp new file mode 100644 index 0000000..84853de --- /dev/null +++ b/src/xylinechart/qxyseries.cpp @@ -0,0 +1,59 @@ +#include "qxyseries.h" + +QXYSeries::QXYSeries(): +m_color(Qt::black) +{ +} + +QXYSeries::~QXYSeries() +{ +} + + +void QXYSeries::setColor(const QColor& color) +{ + m_color = color; +} + + +void QXYSeries::add(qreal x,qreal y) +{ + m_x< +#include + + +class QXYSeries +{ +public: + QXYSeries(); + virtual ~QXYSeries(); + void add(qreal x, qreal y); + void clear(); + void setColor(const QColor& color); + const QColor& color() const { return m_color;} + int count() const; + qreal x(int pos) const; + qreal y(int pos) const; + friend QDebug operator<< (QDebug d, const QXYSeries series); + +private: + QColor m_color; + QList m_x; + QList m_y; + +}; + +#endif diff --git a/src/xylinechart/xygrid.cpp b/src/xylinechart/xygrid.cpp new file mode 100644 index 0000000..fb11f92 --- /dev/null +++ b/src/xylinechart/xygrid.cpp @@ -0,0 +1,67 @@ +#include "xygrid_p.h" +#include "xylinechart_p.h" +#include + +QCHART_BEGIN_NAMESPACE + +XYGrid::XYGrid(const XYLineChart* const chart, QGraphicsItem* parent):QGraphicsItem(parent), +m_chart(chart) +{ +} + +XYGrid::~XYGrid() +{ + // TODO Auto-generated destructor stub +} + +void XYGrid::setSize(const QSizeF& size) { + m_rect.setSize(size.toSize()); +} + + +QRectF XYGrid::boundingRect() const +{ + return m_rect; +} + +void XYGrid::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget) +{ + if (!m_rect.isValid()) + return; + + const XYPlotData& plotData = m_chart->xyPlotData(); + + const qreal deltaX = (m_rect.width() -1) / plotData.ticksX(); + const qreal deltaY = (m_rect.height() - 1) / plotData.ticksY(); + + for (int i = 0; i <= plotData.ticksX(); ++i) { + + int x = i * deltaX + m_rect.left(); + qreal label = plotData.m_minX + (i * plotData.spanX() + / plotData.ticksX()); + painter->drawLine(x, m_rect.top()+1, x, m_rect.bottom()); + //painter->drawLine(x, m_rect.bottom(), x, m_rect.bottom() + 5); + + painter->drawText(x - 50, m_rect.bottom() + 5, 100, 20, + Qt::AlignHCenter | Qt::AlignTop, + QString::number(label)); + } + + for (int j = 0; j <= plotData.ticksY(); ++j) { + + int y = j * -deltaY + m_rect.bottom(); + qreal label = plotData.m_minY + (j * plotData.spanY() + / plotData.ticksY()); + + painter->drawLine(m_rect.left(), y, m_rect.right()-1, y); + //painter->drawLine(m_rect.left() - 5, y, m_rect.left(), y); + //TODO : margin = 50 ; + painter->drawText(m_rect.left() - 50, y - 10, 50 - 5, 20, + Qt::AlignRight | Qt::AlignVCenter, + QString::number(label)); + } + + //painter->drawRect(m_rect.adjusted(0, 0, -1, -1)); +} + +QCHART_END_NAMESPACE diff --git a/src/xylinechart/xygrid_p.h b/src/xylinechart/xygrid_p.h new file mode 100644 index 0000000..fe55a2b --- /dev/null +++ b/src/xylinechart/xygrid_p.h @@ -0,0 +1,31 @@ +#ifndef XYGRID_H_ +#define XYGRID_H_ + +#include +#include + +QCHART_BEGIN_NAMESPACE + +class XYPlotData; +class XYLineChart; + +class XYGrid : public QGraphicsItem +{ +public: + XYGrid(const XYLineChart* const chart,QGraphicsItem* parent = 0); + virtual ~XYGrid(); + + //from QGraphicsItem + virtual QRectF boundingRect() const; + virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + + void setSize(const QSizeF& rect); + +private: + QRectF m_rect; + const XYLineChart* const m_chart; +}; + +QCHART_END_NAMESPACE + +#endif /* XYGRID_H_ */ diff --git a/src/xylinechart/xylinechart.cpp b/src/xylinechart/xylinechart.cpp new file mode 100644 index 0000000..fcce19b --- /dev/null +++ b/src/xylinechart/xylinechart.cpp @@ -0,0 +1,86 @@ +#include "xylinechart_p.h" +#include "axis_p.h" +#include "xygrid_p.h" +#include +#include +#include + +QCHART_BEGIN_NAMESPACE + +XYLineChart::XYLineChart(QGraphicsItem *parent):QChart(parent), +m_axisX(new Axis(this)), +m_axisY(new Axis(this)), +m_grid(new XYGrid(this,this)), +m_plotDataIndex(0) +{ + // set axis + m_axisY->rotate(90); + + XYPlotData data; + data.m_minX = 0.0; + data.m_maxX = 100.0; + data.m_minY = 0.0; + data.m_maxY = 100.0; + data.m_ticksX=4; + data.m_ticksY=4; + + m_plotDataList.clear(); + m_plotDataList << data; + +} + +void XYLineChart::setSize(const QSizeF& size) { + + m_rect = QRect(QPoint(0,0),size.toSize()); + m_rect.adjust(margin(),margin(),-margin(),-margin()); + updateGeometry(); +} + +void XYLineChart::addXYSeries(const QXYSeries* series) +{ + m_series<setPos(m_rect.bottomLeft()); + //m_axisX->setLength(m_rect.width()); + //m_axisY->setLength(m_rect.height()); + m_grid->setPos(m_rect.topLeft()); + m_grid->setSize(m_rect.size()); + update(); +} + +void XYLineChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget) +{ + const XYPlotData& xyplotData = xyPlotData(); + + if (!m_rect.isValid()) + return; + + painter->setClipRect(m_rect.adjusted(+1, +1, -1, -1)); + + const qreal deltaX = (m_rect.width()-1)/xyplotData.spanX(); + const qreal deltaY = (m_rect.height()-1)/xyplotData.spanY(); + + foreach (const QXYSeries* series, m_series) { + + QPolygonF polyline(series->count()); + + for (int j = 0; j < series->count(); ++j) { + qreal dx = series->x(j) - xyplotData.m_minX; + qreal dy = series->y(j) - xyplotData.m_minY; + qreal x = (dx * deltaX) + m_rect.left(); + qreal y = - (dy * deltaY) + m_rect.bottom(); + polyline[j] = QPointF(x, y); + } + painter->setPen(series->color()); + painter->drawPolyline(polyline); + } +} +QCHART_END_NAMESPACE diff --git a/src/xylinechart/xylinechart_p.h b/src/xylinechart/xylinechart_p.h new file mode 100644 index 0000000..f310a59 --- /dev/null +++ b/src/xylinechart/xylinechart_p.h @@ -0,0 +1,52 @@ +#ifndef XYLINECHART_H +#define XYLINECHART_H + +#include "qchartconfig.h" +#include "qchart.h" +#include "qxyseries.h" +#include "xyplotdata_p.h" + +QCHART_BEGIN_NAMESPACE + +class Axis; +class XYGrid; + +class XYLineChart : public QChart +{ + +public: + XYLineChart(QGraphicsItem *parent = 0); + virtual ~XYLineChart(){}; + + //from QGraphicsItem + virtual QRectF boundingRect() const; + virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + + //from QChart + virtual void setSize(const QSizeF& size); + + void addXYSeries(const QXYSeries* series); + + const XYPlotData& xyPlotData() const { return m_plotDataList.at(m_plotDataIndex); } + +private: + void updateGeometry(); + void drawData(QPainter *painter); + +private: + QRect m_rect; + QList m_series; + Axis* m_axisX; + Axis* m_axisY; + XYGrid* m_grid; + QPointF* m_origin; + QMap > curveMap; + QList m_plotDataList; + int m_plotDataIndex; + + +}; + +QCHART_END_NAMESPACE + +#endif diff --git a/src/xylinechart/xyplotdata.cpp b/src/xylinechart/xyplotdata.cpp new file mode 100644 index 0000000..50fe34d --- /dev/null +++ b/src/xylinechart/xyplotdata.cpp @@ -0,0 +1,27 @@ +#include "xyplotdata_p.h" + +QCHART_BEGIN_NAMESPACE + +XYPlotData::XYPlotData() +{ + +} + +XYPlotData::~XYPlotData() +{ + // TODO Auto-generated destructor stub +} + +qreal XYPlotData::spanX() const +{ + Q_ASSERT(m_maxX >= m_minX); + return m_maxX - m_minX; +} + +qreal XYPlotData::spanY() const +{ + Q_ASSERT(m_maxY >= m_minY); + return m_maxY - m_minY; +} + +QCHART_END_NAMESPACE diff --git a/src/xylinechart/xyplotdata_p.h b/src/xylinechart/xyplotdata_p.h new file mode 100644 index 0000000..c8dc6a4 --- /dev/null +++ b/src/xylinechart/xyplotdata_p.h @@ -0,0 +1,31 @@ +#ifndef PLOTTER_H_ +#define PLOTTER_H_ +#include "qchartconfig.h" +#include + +QCHART_BEGIN_NAMESPACE + +class XYPlotData { +public: + XYPlotData(); + virtual ~XYPlotData(); + + qreal spanX() const; + qreal spanY() const; + int ticksX() const { return m_ticksX; } + int ticksY() const { return m_ticksY; } + +public: + int m_ticksX; + int m_ticksY; + qreal m_minX; + qreal m_maxX; + qreal m_minY; + qreal m_maxY; + + +}; + +QCHART_END_NAMESPACE + +#endif /* PLOTTER_H_ */