qchart.cpp
140 lines
| 3.3 KiB
| text/x-c
|
CppLexer
/ src / qchart.cpp
Michal Klocek
|
r12 | #include "qchart.h" | ||
Michal Klocek
|
r21 | #include "qchartseries.h" | ||
Tero Ahola
|
r42 | #include "qscatterseries.h" | ||
#include "qscatterseries_p.h" | ||||
Michal Klocek
|
r21 | #include "xylinechartitem_p.h" | ||
Michal Klocek
|
r25 | #include "xyplotdomain_p.h" | ||
Michal Klocek
|
r21 | #include "axis_p.h" | ||
#include "xygrid_p.h" | ||||
Tero Ahola
|
r42 | #include <QGraphicsScene> | ||
Michal Klocek
|
r21 | #include <QDebug> | ||
Michal Klocek
|
r12 | |||
Tero Ahola
|
r30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
Michal Klocek
|
r12 | |||
Michal Klocek
|
r28 | class QChartPrivate | ||
{ | ||||
public: | ||||
QChartPrivate(QChart* parent): | ||||
m_axisX(new Axis(parent)), | ||||
m_axisY(new Axis(parent)), | ||||
m_grid(new XYGrid(parent)), | ||||
m_plotDataIndex(0), | ||||
m_marginSize(0){} | ||||
Axis* m_axisX; | ||||
Axis* m_axisY; | ||||
XYGrid* m_grid; | ||||
QRect m_rect; | ||||
QList<const QChartSeries*> m_series; | ||||
QList<XYPlotDomain> m_plotDataList; | ||||
QList<QGraphicsItem*> m_items; | ||||
int m_plotDataIndex; | ||||
int m_marginSize; | ||||
}; | ||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
Michal Klocek
|
r12 | QChart::QChart(QGraphicsItem* parent):QGraphicsItem(parent), | ||
Michal Klocek
|
r28 | d_ptr(new QChartPrivate(this)) | ||
Michal Klocek
|
r12 | { | ||
Michal Klocek
|
r28 | // setFlags(QGraphicsItem::ItemClipsChildrenToShape); | ||
Michal Klocek
|
r21 | // set axis | ||
Michal Klocek
|
r28 | Q_D(QChart); | ||
d->m_axisY->rotate(90); | ||||
Michal Klocek
|
r22 | |||
Michal Klocek
|
r28 | //TODO hardcoded values , to removed soon | ||
XYPlotDomain 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; | ||||
Michal Klocek
|
r21 | |||
Michal Klocek
|
r28 | d->m_plotDataList.clear(); | ||
d->m_plotDataList << data; | ||||
Michal Klocek
|
r21 | |||
Michal Klocek
|
r28 | d->m_grid->setZValue(10); | ||
d->m_grid->setXYPlotData(d->m_plotDataList.at(0)); | ||||
Michal Klocek
|
r12 | } | ||
QChart::~QChart(){} | ||||
Michal Klocek
|
r21 | QRectF QChart::boundingRect() const | ||
{ | ||||
Michal Klocek
|
r28 | Q_D(const QChart); | ||
return d->m_rect; | ||||
Michal Klocek
|
r21 | } | ||
Michal Klocek
|
r12 | |||
Michal Klocek
|
r21 | void QChart::addSeries(QChartSeries* series) | ||
Michal Klocek
|
r12 | { | ||
Michal Klocek
|
r28 | Q_D(QChart); | ||
d->m_series<<series; | ||||
Michal Klocek
|
r21 | |||
switch(series->type()) | ||||
{ | ||||
Tero Ahola
|
r30 | case QChartSeries::SeriesTypeLine: { | ||
Michal Klocek
|
r21 | XYLineChartItem* item = new XYLineChartItem(reinterpret_cast<QXYChartSeries*>(series),this); | ||
Michal Klocek
|
r28 | item->updateXYPlotData(d->m_plotDataList.at(0)); | ||
d->m_items<<item; | ||||
Michal Klocek
|
r21 | break; | ||
Michal Klocek
|
r12 | } | ||
Tero Ahola
|
r30 | case QChartSeries::SeriesTypeScatter: { | ||
break; | ||||
} | ||||
} | ||||
Michal Klocek
|
r21 | } | ||
Tero Ahola
|
r42 | QChartSeries* QChart::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type) | ||
{ | ||||
Q_D(QChart); | ||||
// TODO: support also other types | ||||
Q_ASSERT(type == QChartSeries::SeriesTypeScatter); | ||||
QChartSeries *series = 0; | ||||
switch (type) { | ||||
case QChartSeries::SeriesTypeScatter: { | ||||
QScatterSeries *scatterSeries = new QScatterSeries(x, y, this); | ||||
d->m_items.append(scatterSeries->d); | ||||
scene()->addItem(scatterSeries->d); | ||||
//series = qobject_cast<QChartSeries *>(scatterSeries); | ||||
break; | ||||
} | ||||
default: | ||||
break; | ||||
} | ||||
return series; | ||||
} | ||||
Michal Klocek
|
r28 | void QChart::setSize(const QSizeF& size) | ||
{ | ||||
Q_D(QChart); | ||||
Michal Klocek
|
r21 | //TODO refactor to setGeometry | ||
Michal Klocek
|
r28 | d->m_rect = QRect(QPoint(0,0),size.toSize()); | ||
d->m_rect.adjust(margin(),margin(),-margin(),-margin()); | ||||
d->m_grid->setPos(d->m_rect.topLeft()); | ||||
d->m_grid->setSize(d->m_rect.size()); | ||||
d->m_plotDataList[0].m_viewportRect = d->m_rect; | ||||
Tero Ahola
|
r42 | // TODO: line chart items would need to be updated separately as they don't support update | ||
// via paint method | ||||
Michal Klocek
|
r39 | foreach(QGraphicsItem* item , d->m_items) | ||
Tero Ahola
|
r42 | reinterpret_cast<XYLineChartItem*>(item)->updateXYPlotData(d->m_plotDataList.at(0)); | ||
Michal Klocek
|
r21 | update(); | ||
Michal Klocek
|
r12 | } | ||
Michal Klocek
|
r28 | int QChart::margin() const | ||
{ | ||||
Q_D(const QChart); | ||||
return d->m_marginSize; | ||||
} | ||||
Michal Klocek
|
r12 | void QChart::setMargin(int margin) | ||
{ | ||||
Michal Klocek
|
r28 | Q_D(QChart); | ||
d->m_marginSize = margin; | ||||
Michal Klocek
|
r12 | } | ||
Tero Ahola
|
r30 | QTCOMMERCIALCHART_END_NAMESPACE | ||