qchart.cpp
166 lines
| 4.5 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" | ||||
Tero Ahola
|
r51 | #include "qpieseries.h" | ||
Michal Klocek
|
r45 | #include "qxychartseries.h" | ||
sauimone
|
r56 | |||
#include "barchartseries.h" | ||||
#include "bargroup.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 | |||
Tero Ahola
|
r48 | QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent), | ||
Michal Klocek
|
r53 | m_axisX(new Axis(this)), | ||
m_axisY(new Axis(this)), | ||||
m_grid(new XYGrid(this)), | ||||
m_plotDataIndex(0), | ||||
m_marginSize(0) | ||||
Michal Klocek
|
r12 | { | ||
Michal Klocek
|
r28 | // setFlags(QGraphicsItem::ItemClipsChildrenToShape); | ||
Michal Klocek
|
r21 | // set axis | ||
Michal Klocek
|
r53 | m_axisY->rotate(90); | ||
Michal Klocek
|
r12 | } | ||
QChart::~QChart(){} | ||||
Michal Klocek
|
r21 | QRectF QChart::boundingRect() const | ||
{ | ||||
Michal Klocek
|
r53 | return m_rect; | ||
Michal Klocek
|
r21 | } | ||
Michal Klocek
|
r12 | |||
Michal Klocek
|
r21 | void QChart::addSeries(QChartSeries* series) | ||
Michal Klocek
|
r12 | { | ||
Tero Ahola
|
r48 | // TODO: we should check the series not already added | ||
Michal Klocek
|
r45 | |||
Michal Klocek
|
r53 | m_series<<series; | ||
Michal Klocek
|
r21 | |||
switch(series->type()) | ||||
{ | ||||
Tero Ahola
|
r30 | case QChartSeries::SeriesTypeLine: { | ||
Michal Klocek
|
r47 | |||
QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series); | ||||
XYPlotDomain domain; | ||||
//TODO "nice numbers algorithm" | ||||
domain.m_ticksX=4; | ||||
domain.m_ticksY=4; | ||||
for (int i = 0 ; i < xyseries->count() ; i++) | ||||
{ | ||||
qreal x = xyseries->x(i); | ||||
qreal y = xyseries->y(i); | ||||
domain.m_minX = qMin(domain.m_minX,x); | ||||
domain.m_minY = qMin(domain.m_minY,y); | ||||
domain.m_maxX = qMax(domain.m_maxX,x); | ||||
domain.m_maxY = qMax(domain.m_maxY,y); | ||||
} | ||||
XYLineChartItem* item = new XYLineChartItem(xyseries,this); | ||||
item->updateXYPlotDomain(domain); | ||||
Michal Klocek
|
r53 | m_plotDomainList<<domain; | ||
m_xyLineChartItems<<item; | ||||
Michal Klocek
|
r21 | break; | ||
Michal Klocek
|
r12 | } | ||
Tero Ahola
|
r51 | // TODO: Not tested: | ||
// case QChartSeries::SeriesTypeScatter: { | ||||
// QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series); | ||||
// if (scatter) { | ||||
// scatter->d->setParentItem(this); | ||||
// scene()->addItem(scatter->d); | ||||
// } | ||||
// break; | ||||
// } | ||||
sauimone
|
r56 | |||
case QChartSeries::SeriesTypeBar: { | ||||
qDebug() << "barSeries added"; | ||||
BarChartSeries* barSeries = static_cast<BarChartSeries*>(series); | ||||
// Who owns the series? | ||||
BarGroup* group = new BarGroup(*barSeries, this); | ||||
scene()->addItem(group); | ||||
m_BarGroupItems.append(group); // If we need to access group later | ||||
break; | ||||
} | ||||
Tero Ahola
|
r30 | } | ||
Michal Klocek
|
r21 | } | ||
Tero Ahola
|
r42 | QChartSeries* QChart::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type) | ||
{ | ||||
Tero Ahola
|
r51 | // TODO: support also other types; not only scatter and pie | ||
Q_ASSERT(type == QChartSeries::SeriesTypeScatter | ||||
|| type == QChartSeries::SeriesTypePie); | ||||
Tero Ahola
|
r42 | |||
switch (type) { | ||||
case QChartSeries::SeriesTypeScatter: { | ||||
QScatterSeries *scatterSeries = new QScatterSeries(x, y, this); | ||||
Tero Ahola
|
r54 | connect(this, SIGNAL(sizeChanged(QRectF)), | ||
scatterSeries, SLOT(chartSizeChanged(QRectF))); | ||||
Tero Ahola
|
r48 | scatterSeries->d->setParentItem(this); | ||
Tero Ahola
|
r51 | return scatterSeries; | ||
} | ||||
case QChartSeries::SeriesTypePie: { | ||||
// TODO: we now have also a list of y values as a parameter, it is ignored | ||||
// we should use a generic data class instead of list of x and y values | ||||
QPieSeries *pieSeries = new QPieSeries(x, this); | ||||
Tero Ahola
|
r54 | connect(this, SIGNAL(sizeChanged(QRectF)), | ||
pieSeries, SLOT(chartSizeChanged(QRectF))); | ||||
Tero Ahola
|
r51 | return pieSeries; | ||
Tero Ahola
|
r42 | } | ||
default: | ||||
break; | ||||
} | ||||
Tero Ahola
|
r51 | return 0; | ||
Tero Ahola
|
r42 | } | ||
Tero Ahola
|
r48 | |||
Michal Klocek
|
r28 | void QChart::setSize(const QSizeF& size) | ||
{ | ||||
Michal Klocek
|
r53 | m_rect = QRect(QPoint(0,0),size.toSize()); | ||
m_rect.adjust(margin(),margin(), -margin(), -margin()); | ||||
m_grid->setPos(m_rect.topLeft()); | ||||
m_grid->setSize(m_rect.size()); | ||||
Michal Klocek
|
r47 | |||
Tero Ahola
|
r54 | // TODO: TTD for setting scale | ||
//emit scaleChanged(100, 100); | ||||
Tero Ahola
|
r48 | // TODO: calculate the origo | ||
// TODO: not sure if emitting a signal here is the best from performance point of view | ||||
Tero Ahola
|
r54 | emit sizeChanged(QRectF(0, 0, size.width(), size.height())); | ||
Tero Ahola
|
r48 | |||
Michal Klocek
|
r53 | for (int i(0); i < m_plotDomainList.size(); i++) | ||
m_plotDomainList[i].m_viewportRect = m_rect; | ||||
Michal Klocek
|
r47 | |||
Tero Ahola
|
r48 | // TODO: line chart items are updated separately as they don't support update | ||
// via sizeChanged signal | ||||
Michal Klocek
|
r53 | foreach(XYLineChartItem* item ,m_xyLineChartItems) | ||
item->updateXYPlotDomain(m_plotDomainList.at(m_plotDataIndex)); | ||||
if (m_plotDomainList.count()) | ||||
m_grid->setXYPlotData(m_plotDomainList.at(m_plotDataIndex)); | ||||
Michal Klocek
|
r47 | |||
Michal Klocek
|
r21 | update(); | ||
Michal Klocek
|
r12 | } | ||
Michal Klocek
|
r28 | int QChart::margin() const | ||
{ | ||||
Michal Klocek
|
r53 | return m_marginSize; | ||
Michal Klocek
|
r28 | } | ||
Michal Klocek
|
r12 | void QChart::setMargin(int margin) | ||
{ | ||||
Michal Klocek
|
r53 | m_marginSize = margin; | ||
Michal Klocek
|
r12 | } | ||
Tero Ahola
|
r48 | #include "moc_qchart.cpp" | ||
Tero Ahola
|
r30 | QTCOMMERCIALCHART_END_NAMESPACE | ||