chartdataset.cpp
307 lines
| 8.7 KiB
| text/x-c
|
CppLexer
/ src / chartdataset.cpp
|
r131 | #include "chartdataset_p.h" | ||
|
r223 | #include "qchartaxis.h" | ||
|
r139 | //series | ||
|
r349 | #include "qlineseries.h" | ||
|
r421 | #include "qareaseries.h" | ||
|
r338 | #include "qbarseries.h" | ||
#include "qstackedbarseries.h" | ||||
#include "qpercentbarseries.h" | ||||
|
r146 | #include "qpieseries.h" | ||
|
r158 | #include "qscatterseries.h" | ||
|
r401 | #include "qsplineseries.h" | ||
|
r131 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r223 | ChartDataSet::ChartDataSet(QObject *parent):QObject(parent), | ||
|
r401 | m_axisX(new QChartAxis(this)), | ||
m_axisY(new QChartAxis(this)), | ||||
m_domainIndex(0), | ||||
m_axisXInitialized(false) | ||||
|
r131 | { | ||
} | ||||
ChartDataSet::~ChartDataSet() | ||||
{ | ||||
} | ||||
|
r360 | void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY) | ||
|
r131 | { | ||
|
r439 | if(axisY==0) axisY = m_axisY; | ||
|
r139 | |||
|
r439 | QChartAxis* axis = m_seriesAxisMap.value(series); | ||
|
r223 | |||
|
r439 | if(axis) { | ||
qWarning() << "Can not add series. Series already on the chart"; | ||||
return; | ||||
} | ||||
|
r223 | |||
|
r439 | if(!series->parent()){ | ||
series->setParent(this); // take ownership | ||||
}; | ||||
|
r223 | |||
|
r439 | if(!axisY->parent()){ | ||
axisY->setParent(this); // take ownership | ||||
} | ||||
|
r223 | |||
|
r439 | Domain* domain = m_axisDomainMap.value(axisY); | ||
|
r223 | |||
|
r439 | if(!domain) { | ||
domain = new Domain(); | ||||
|
r223 | |||
|
r452 | QObject::connect(axisY,SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeYChanged(qreal,qreal))); | ||
QObject::connect(axisX(),SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeXChanged(qreal,qreal))); | ||||
|
r442 | //initialize | ||
|
r439 | m_axisDomainMap.insert(axisY,domain); | ||
emit axisAdded(axisY,domain); | ||||
|
r401 | } | ||
|
r421 | |||
|
r439 | if(!m_axisXInitialized){ | ||
emit axisAdded(axisX(),domain); | ||||
m_axisXInitialized=true; | ||||
} | ||||
|
r421 | |||
|
r439 | calculateDomain(series,domain); | ||
|
r421 | |||
|
r439 | m_seriesAxisMap.insert(series,axisY); | ||
emit seriesAdded(series,domain); | ||||
|
r139 | |||
|
r439 | } | ||
|
r142 | |||
|
r439 | void ChartDataSet::removeSeries(QSeries* series) | ||
{ | ||||
|
r158 | |||
|
r439 | QChartAxis* axis = m_seriesAxisMap.value(series); | ||
|
r401 | |||
|
r439 | if(!axis){ | ||
qWarning()<<"Can not remove series. Series not found on the chart."; | ||||
|
r401 | return; | ||
} | ||||
|
r439 | emit seriesRemoved(series); | ||
m_seriesAxisMap.remove(series); | ||||
|
r139 | |||
|
r439 | if(series->parent()==this){ | ||
delete series; | ||||
series=0; | ||||
|
r139 | } | ||
|
r439 | QList<QChartAxis*> axes = m_seriesAxisMap.values(); | ||
|
r223 | |||
|
r439 | int i = axes.indexOf(axis); | ||
|
r223 | |||
|
r439 | if(i==-1){ | ||
Domain* domain = m_axisDomainMap.take(axis); | ||||
emit axisRemoved(axis); | ||||
|
r444 | if(axis!=axisY()){ | ||
if(axis->parent()==this){ | ||||
delete axis; | ||||
axis=0; | ||||
} | ||||
} | ||||
|
r439 | delete domain; | ||
} | ||||
|
r223 | |||
|
r439 | if(m_seriesAxisMap.values().size()==0) | ||
{ | ||||
m_axisXInitialized=false; | ||||
emit axisRemoved(axisX()); | ||||
|
r223 | } | ||
|
r139 | } | ||
|
r258 | void ChartDataSet::removeAllSeries() | ||
{ | ||||
|
r439 | QList<QSeries*> series = m_seriesAxisMap.keys(); | ||
|
r223 | |||
|
r439 | foreach(QSeries* s , series) { | ||
removeSeries(s); | ||||
|
r139 | } | ||
|
r439 | Q_ASSERT(m_seriesAxisMap.count()==0); | ||
Q_ASSERT(m_axisDomainMap.count()==0); | ||||
|
r139 | } | ||
|
r439 | //to be removed with PIMPL | ||
void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) const | ||||
|
r139 | { | ||
|
r439 | switch(series->type()) | ||
{ | ||||
case QSeries::SeriesTypeLine: { | ||||
|
r373 | |||
|
r439 | QLineSeries* lineSeries = static_cast<QLineSeries*>(series); | ||
|
r265 | |||
|
r439 | for (int i = 0; i < lineSeries->count(); i++) | ||
{ | ||||
qreal x = lineSeries->x(i); | ||||
qreal y = lineSeries->y(i); | ||||
domain->setMinX(qMin(domain->minX(),x)); | ||||
domain->setMinY(qMin(domain->minY(),y)); | ||||
domain->setMaxX(qMax(domain->maxX(),x)); | ||||
domain->setMaxY(qMax(domain->maxY(),y)); | ||||
} | ||||
break; | ||||
|
r223 | } | ||
|
r439 | case QSeries::SeriesTypeArea: { | ||
|
r400 | |||
|
r439 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); | ||
|
r223 | |||
|
r439 | QLineSeries* upperSeries = areaSeries->upperSeries(); | ||
QLineSeries* lowerSeries = areaSeries->lowerSeries(); | ||||
|
r223 | |||
|
r439 | for (int i = 0; i < upperSeries->count(); i++) | ||
{ | ||||
qreal x = upperSeries->x(i); | ||||
qreal y = upperSeries->y(i); | ||||
domain->setMinX(qMin(domain->minX(),x)); | ||||
domain->setMinY(qMin(domain->minY(),y)); | ||||
domain->setMaxX(qMax(domain->maxX(),x)); | ||||
domain->setMaxY(qMax(domain->maxY(),y)); | ||||
} | ||||
if(lowerSeries) { | ||||
for (int i = 0; i < lowerSeries->count(); i++) | ||||
{ | ||||
qreal x = lowerSeries->x(i); | ||||
qreal y = lowerSeries->y(i); | ||||
domain->setMinX(qMin(domain->minX(),x)); | ||||
domain->setMinY(qMin(domain->minY(),y)); | ||||
domain->setMaxX(qMax(domain->maxX(),x)); | ||||
domain->setMaxY(qMax(domain->maxY(),y)); | ||||
}} | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypeBar: { | ||||
qDebug() << "QChartSeries::SeriesTypeBar"; | ||||
QBarSeries* barSeries = static_cast<QBarSeries*>(series); | ||||
qreal x = barSeries->categoryCount(); | ||||
qreal y = barSeries->max(); | ||||
domain->setMinX(qMin(domain->minX(),x)); | ||||
domain->setMinY(qMin(domain->minY(),y)); | ||||
domain->setMaxX(qMax(domain->maxX(),x)); | ||||
domain->setMaxY(qMax(domain->maxY(),y)); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypeStackedBar: { | ||||
qDebug() << "QChartSeries::SeriesTypeStackedBar"; | ||||
QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); | ||||
qreal x = stackedBarSeries->categoryCount(); | ||||
qreal y = stackedBarSeries->maxCategorySum(); | ||||
domain->setMinX(qMin(domain->minX(),x)); | ||||
domain->setMinY(qMin(domain->minY(),y)); | ||||
domain->setMaxX(qMax(domain->maxX(),x)); | ||||
domain->setMaxY(qMax(domain->maxY(),y)); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypePercentBar: { | ||||
qDebug() << "QChartSeries::SeriesTypePercentBar"; | ||||
QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); | ||||
qreal x = percentBarSeries->categoryCount(); | ||||
domain->setMinX(qMin(domain->minX(),x)); | ||||
domain->setMinY(0); | ||||
domain->setMaxX(qMax(domain->maxX(),x)); | ||||
domain->setMaxY(100); | ||||
break; | ||||
|
r418 | } | ||
|
r411 | |||
|
r439 | case QSeries::SeriesTypePie: { | ||
QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | ||||
// TODO: domain stuff | ||||
break; | ||||
} | ||||
|
r411 | |||
|
r439 | case QSeries::SeriesTypeScatter: { | ||
QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); | ||||
Q_ASSERT(scatterSeries); | ||||
|
r447 | qreal minX(domain->minX()); | ||
qreal minY(domain->minY()); | ||||
qreal maxX(domain->maxX()); | ||||
qreal maxY(domain->maxY()); | ||||
|
r439 | foreach (QPointF point, scatterSeries->data()) { | ||
|
r447 | minX = qMin(minX, point.x()); | ||
minY = qMin(minY, point.y()); | ||||
maxX = qMax(maxX, point.x()); | ||||
maxY = qMax(maxY, point.y()); | ||||
|
r439 | } | ||
|
r447 | domain->setMinX(minX); | ||
domain->setMinY(minY); | ||||
domain->setMaxX(maxX); | ||||
domain->setMaxY(maxY); | ||||
|
r439 | break; | ||
} | ||||
|
r223 | |||
|
r439 | case QSeries::SeriesTypeSpline: { | ||
QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series); | ||||
|
r223 | |||
|
r439 | for (int i = 0; i < splineSeries->count(); i++) | ||
{ | ||||
qreal x = splineSeries->x(i); | ||||
qreal y = splineSeries->y(i); | ||||
domain->setMinX(qMin(domain->minX(),x)); | ||||
domain->setMinY(qMin(domain->minY(),y)); | ||||
domain->setMaxX(qMax(domain->maxX(),x)); | ||||
domain->setMaxY(qMax(domain->maxY(),y)); | ||||
} | ||||
break; | ||||
} | ||||
|
r223 | |||
|
r439 | default: { | ||
qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported"; | ||||
return; | ||||
break; | ||||
|
r223 | } | ||
|
r439 | |||
|
r139 | } | ||
} | ||||
|
r439 | void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size) | ||
|
r139 | { | ||
|
r439 | QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap); | ||
while (i.hasNext()) { | ||||
i.next(); | ||||
i.value()->zoomIn(rect,size); | ||||
|
r223 | } | ||
|
r439 | } | ||
|
r223 | |||
|
r439 | void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size) | ||
{ | ||||
QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap); | ||||
while (i.hasNext()) { | ||||
i.next(); | ||||
i.value()->zoomOut(rect,size); | ||||
} | ||||
|
r223 | } | ||
|
r360 | QChartAxis* ChartDataSet::axisY(QSeries* series) const | ||
|
r223 | { | ||
if(series == 0) return m_axisY; | ||||
|
r439 | return m_seriesAxisMap.value(series); | ||
|
r223 | } | ||
|
r439 | Domain* ChartDataSet::domain(QSeries* series) const | ||
|
r223 | { | ||
|
r439 | QChartAxis* axis = m_seriesAxisMap.value(series); | ||
if(axis){ | ||||
return m_axisDomainMap.value(axis); | ||||
}else | ||||
return 0; | ||||
|
r223 | } | ||
|
r439 | Domain* ChartDataSet::domain(QChartAxis* axis) const | ||
|
r223 | { | ||
|
r444 | if(axis==axisX()) { | ||
return m_axisDomainMap.value(axisY()); | ||||
} | ||||
else { | ||||
return m_axisDomainMap.value(axis); | ||||
} | ||||
|
r223 | } | ||
|
r439 | QChartAxis* ChartDataSet::axis(QSeries* series) const | ||
|
r223 | { | ||
|
r439 | return m_seriesAxisMap.value(series); | ||
|
r131 | } | ||
#include "moc_chartdataset_p.cpp" | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||