diff --git a/src/qchart.cpp b/src/qchart.cpp index 8b3a3e2..bf53b4d 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -1,7 +1,7 @@ #include "qchart.h" #include "qchartseries.h" #include "xylinechartitem_p.h" -#include "xyplotdata_p.h" +#include "xyplotdomain_p.h" #include "axis_p.h" #include "xygrid_p.h" #include @@ -21,7 +21,7 @@ m_plotDataIndex(0) //TODO hardcoded values , to removed soon - XYPlotData* data = new XYPlotData(); + XYPlotDomain* data = new XYPlotDomain(); data->m_minX = 0.0; data->m_maxX = 100.0; data->m_minY = 0.0; @@ -51,7 +51,7 @@ void QChart::addSeries(QChartSeries* series) { case QChartSeries::LINE: XYLineChartItem* item = new XYLineChartItem(reinterpret_cast(series),this); - item->setXYPlotData(*m_plotDataList.at(0)); + item->updateXYPlotData(*m_plotDataList.at(0)); m_items<setPos(m_rect.topLeft()); m_grid->setSize(m_rect.size()); + m_plotDataList.at(0)->m_viewportRect = m_rect; foreach(QGraphicsItem* item , m_items) - reinterpret_cast(item)->setChartSize(m_rect); + reinterpret_cast(item)->updateXYPlotData(*m_plotDataList.at(0)); update(); } diff --git a/src/qchart.h b/src/qchart.h index 6e1ada4..76e5bce 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -9,7 +9,7 @@ QCHART_BEGIN_NAMESPACE class Axis; class XYGrid; class QChartSeries; -class XYPlotData; +class XYPlotDomain; class QCHART_EXPORT QChart : public QGraphicsItem { @@ -34,7 +34,7 @@ private: Axis* m_axisX; Axis* m_axisY; XYGrid* m_grid; - QList m_plotDataList; + QList m_plotDataList; QList m_items; int m_plotDataIndex; int m_marginSize; diff --git a/src/src.pro b/src/src.pro index b6cc1ae..5cf18bb 100644 --- a/src/src.pro +++ b/src/src.pro @@ -9,14 +9,14 @@ SOURCES += \ xylinechart/qxychartseries.cpp \ xylinechart/xylinechartitem.cpp \ xylinechart/xygrid.cpp \ - xylinechart/xyplotdata.cpp \ + xylinechart/xyplotdomain.cpp \ qchart.cpp \ axis.cpp \ qchartwidget.cpp PRIVATE_HEADERS += \ xylinechart/xylinechartitem_p.h \ - xylinechart/xyplotdata_p.h \# to be removed + xylinechart/xyplotdomain_p.h \ xylinechart/xygrid_p.h \ axis_p.h diff --git a/src/xylinechart/xygrid.cpp b/src/xylinechart/xygrid.cpp index 0875099..de71400 100644 --- a/src/xylinechart/xygrid.cpp +++ b/src/xylinechart/xygrid.cpp @@ -18,7 +18,7 @@ void XYGrid::setSize(const QSizeF& size) m_rect.setSize(size.toSize()); } -void XYGrid::setXYPlotData(const XYPlotData& xyPlotData) +void XYGrid::setXYPlotData(const XYPlotDomain& xyPlotData) { m_xyPlotData = xyPlotData; } diff --git a/src/xylinechart/xygrid_p.h b/src/xylinechart/xygrid_p.h index 81714da..b21be21 100644 --- a/src/xylinechart/xygrid_p.h +++ b/src/xylinechart/xygrid_p.h @@ -2,7 +2,7 @@ #define XYGRID_H_ #include -#include +#include #include QCHART_BEGIN_NAMESPACE @@ -18,12 +18,12 @@ public: virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); //TODO: this is just temporary interface - void setXYPlotData(const XYPlotData& xyPlotData); + void setXYPlotData(const XYPlotDomain& xyPlotData); void setSize(const QSizeF& rect); private: QRectF m_rect; - XYPlotData m_xyPlotData; + XYPlotDomain m_xyPlotData; }; QCHART_END_NAMESPACE diff --git a/src/xylinechart/xylinechartitem.cpp b/src/xylinechart/xylinechartitem.cpp index b39b2a6..76b0700 100644 --- a/src/xylinechart/xylinechartitem.cpp +++ b/src/xylinechart/xylinechartitem.cpp @@ -9,21 +9,34 @@ QCHART_BEGIN_NAMESPACE XYLineChartItem::XYLineChartItem(QXYChartSeries* series,QGraphicsItem *parent):QGraphicsItem(parent), -m_series(series), -m_dirtyGeometry(true) +m_series(series) { } -void XYLineChartItem::setChartSize(const QRectF& rect) +void XYLineChartItem::updateXYPlotData(const XYPlotDomain& data) { - m_rect = rect; - m_dirtyGeometry = true; -} - -void XYLineChartItem::setXYPlotData(const XYPlotData& data){ m_xyPlotData=data; - m_dirtyGeometry = true; + + if (!m_xyPlotData.m_viewportRect.isValid()) + return; + + const QRect& rect = m_xyPlotData.m_viewportRect; + + const qreal deltaX = (rect.width()-1)/m_xyPlotData.spanX(); + const qreal deltaY = (rect.height()-1)/m_xyPlotData.spanY(); + + m_polyline.clear(); + m_polyline.resize(m_series->count()); + + for (int j = 0; j < m_series->count(); ++j) { + qreal dx = m_series->x(j) - m_xyPlotData.m_minX; + qreal dy = m_series->y(j) - m_xyPlotData.m_minY; + qreal x = (dx * deltaX) + rect.left(); + qreal y = - (dy * deltaY) + rect.bottom(); + m_polyline[j] = QPointF(x, y); + } + } QRectF XYLineChartItem::boundingRect() const @@ -34,33 +47,7 @@ QRectF XYLineChartItem::boundingRect() const void XYLineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget) { - if(m_dirtyGeometry) { - - m_dirtyGeometry=false; - - if (!m_rect.isValid()) - return; - - painter->setClipRect(m_rect.adjusted(+1, +1, -1, -1)); - - const qreal deltaX = (m_rect.width()-1)/m_xyPlotData.spanX(); - const qreal deltaY = (m_rect.height()-1)/m_xyPlotData.spanY(); - - m_polyline.clear(); - m_polyline.resize(m_series->count()); - - for (int j = 0; j < m_series->count(); ++j) { - qreal dx = m_series->x(j) - m_xyPlotData.m_minX; - qreal dy = m_series->y(j) - m_xyPlotData.m_minY; - qreal x = (dx * deltaX) + m_rect.left(); - qreal y = - (dy * deltaY) + m_rect.bottom(); - m_polyline[j] = QPointF(x, y); - } - painter->setPen(m_series->color()); - painter->drawPolyline(m_polyline); - } - - painter->setClipRect(m_rect.adjusted(+1, +1, -1, -1)); + painter->setClipRect(m_xyPlotData.m_viewportRect.adjusted(+1, +1, -1, -1)); painter->setPen(m_series->color()); painter->drawPolyline(m_polyline); diff --git a/src/xylinechart/xylinechartitem_p.h b/src/xylinechart/xylinechartitem_p.h index 8826de9..0f35b10 100644 --- a/src/xylinechart/xylinechartitem_p.h +++ b/src/xylinechart/xylinechartitem_p.h @@ -3,7 +3,7 @@ #include "qchartconfig.h" #include "qchart.h" -#include "xyplotdata_p.h" +#include "xyplotdomain_p.h" QCHART_BEGIN_NAMESPACE @@ -20,16 +20,12 @@ public: virtual QRectF boundingRect() const; virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); - //TODO: this is just temporary interface - void setChartSize(const QRectF& size); - void setXYPlotData(const XYPlotData& data); + void updateXYPlotData(const XYPlotDomain& data); private: - QRectF m_rect; QPolygonF m_polyline; QXYChartSeries* m_series; - XYPlotData m_xyPlotData; - bool m_dirtyGeometry; + XYPlotDomain m_xyPlotData; }; QCHART_END_NAMESPACE diff --git a/src/xylinechart/xyplotdata.cpp b/src/xylinechart/xyplotdomain.cpp similarity index 65% rename from src/xylinechart/xyplotdata.cpp rename to src/xylinechart/xyplotdomain.cpp index 50fe34d..7e76564 100644 --- a/src/xylinechart/xyplotdata.cpp +++ b/src/xylinechart/xyplotdomain.cpp @@ -1,24 +1,30 @@ -#include "xyplotdata_p.h" +#include "xyplotdomain_p.h" QCHART_BEGIN_NAMESPACE -XYPlotData::XYPlotData() +XYPlotDomain::XYPlotDomain(): +m_ticksX(0), +m_ticksY(0), +m_minX(0), +m_maxX(0), +m_minY(0), +m_maxY(0) { } -XYPlotData::~XYPlotData() +XYPlotDomain::~XYPlotDomain() { // TODO Auto-generated destructor stub } -qreal XYPlotData::spanX() const +qreal XYPlotDomain::spanX() const { Q_ASSERT(m_maxX >= m_minX); return m_maxX - m_minX; } -qreal XYPlotData::spanY() const +qreal XYPlotDomain::spanY() const { Q_ASSERT(m_maxY >= m_minY); return m_maxY - m_minY; diff --git a/src/xylinechart/xyplotdata_p.h b/src/xylinechart/xyplotdomain_p.h similarity index 72% rename from src/xylinechart/xyplotdata_p.h rename to src/xylinechart/xyplotdomain_p.h index c8dc6a4..8ee0a1d 100644 --- a/src/xylinechart/xyplotdata_p.h +++ b/src/xylinechart/xyplotdomain_p.h @@ -1,14 +1,14 @@ -#ifndef PLOTTER_H_ -#define PLOTTER_H_ +#ifndef PLOTDOMAIN_H_ +#define PLOTDOMAIN_H_ #include "qchartconfig.h" -#include +#include QCHART_BEGIN_NAMESPACE -class XYPlotData { +class XYPlotDomain { public: - XYPlotData(); - virtual ~XYPlotData(); + XYPlotDomain(); + virtual ~XYPlotDomain(); qreal spanX() const; qreal spanY() const; @@ -22,8 +22,7 @@ public: qreal m_maxX; qreal m_minY; qreal m_maxY; - - + QRect m_viewportRect; }; QCHART_END_NAMESPACE