chartdataset.cpp
319 lines
| 8.8 KiB
| text/x-c
|
CppLexer
/ src / chartdataset.cpp
Michal Klocek
|
r131 | #include "chartdataset_p.h" | ||
Michal Klocek
|
r223 | #include "qchartaxis.h" | ||
Michal Klocek
|
r139 | //series | ||
Michal Klocek
|
r349 | #include "qlineseries.h" | ||
Michal Klocek
|
r421 | #include "qareaseries.h" | ||
sauimone
|
r338 | #include "qbarseries.h" | ||
#include "qstackedbarseries.h" | ||||
#include "qpercentbarseries.h" | ||||
Jani Honkonen
|
r146 | #include "qpieseries.h" | ||
Tero Ahola
|
r158 | #include "qscatterseries.h" | ||
Marek Rosa
|
r401 | #include "qsplineseries.h" | ||
Michal Klocek
|
r131 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Michal Klocek
|
r223 | ChartDataSet::ChartDataSet(QObject *parent):QObject(parent), | ||
Marek Rosa
|
r401 | m_axisX(new QChartAxis(this)), | ||
m_axisY(new QChartAxis(this)), | ||||
m_domainIndex(0), | ||||
m_axisXInitialized(false) | ||||
Michal Klocek
|
r131 | { | ||
} | ||||
ChartDataSet::~ChartDataSet() | ||||
{ | ||||
} | ||||
Michal Klocek
|
r360 | void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY) | ||
Michal Klocek
|
r131 | { | ||
Michal Klocek
|
r439 | if(axisY==0) axisY = m_axisY; | ||
Michal Klocek
|
r139 | |||
Michal Klocek
|
r439 | QChartAxis* axis = m_seriesAxisMap.value(series); | ||
Michal Klocek
|
r223 | |||
Michal Klocek
|
r439 | if(axis) { | ||
qWarning() << "Can not add series. Series already on the chart"; | ||||
return; | ||||
} | ||||
Michal Klocek
|
r223 | |||
Michal Klocek
|
r439 | if(!series->parent()){ | ||
series->setParent(this); // take ownership | ||||
}; | ||||
Michal Klocek
|
r223 | |||
Michal Klocek
|
r439 | if(!axisY->parent()){ | ||
axisY->setParent(this); // take ownership | ||||
} | ||||
Michal Klocek
|
r223 | |||
Michal Klocek
|
r439 | Domain* domain = m_axisDomainMap.value(axisY); | ||
Michal Klocek
|
r223 | |||
Michal Klocek
|
r439 | if(!domain) { | ||
domain = new Domain(); | ||||
Michal Klocek
|
r554 | QObject::connect(axisY,SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeYChanged(qreal,qreal))); | ||
QObject::connect(axisX(),SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeXChanged(qreal,qreal))); | ||||
QObject::connect(axisY,SIGNAL(ticksCountChanged(int)),domain,SLOT(handleAxisYTicksCountChanged(int))); | ||||
QObject::connect(axisX(),SIGNAL(ticksCountChanged(int)),domain,SLOT(handleAxisXTicksCountChanged(int))); | ||||
QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axisY,SLOT(handleAxisRangeChanged(qreal,qreal))); | ||||
QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axisX(),SLOT(handleAxisRangeChanged(qreal,qreal))); | ||||
Michal Klocek
|
r442 | //initialize | ||
Michal Klocek
|
r439 | m_axisDomainMap.insert(axisY,domain); | ||
emit axisAdded(axisY,domain); | ||||
Marek Rosa
|
r401 | } | ||
Michal Klocek
|
r421 | |||
Michal Klocek
|
r439 | if(!m_axisXInitialized){ | ||
emit axisAdded(axisX(),domain); | ||||
m_axisXInitialized=true; | ||||
} | ||||
Michal Klocek
|
r421 | |||
Michal Klocek
|
r439 | calculateDomain(series,domain); | ||
Michal Klocek
|
r421 | |||
Michal Klocek
|
r439 | m_seriesAxisMap.insert(series,axisY); | ||
emit seriesAdded(series,domain); | ||||
Michal Klocek
|
r139 | |||
Michal Klocek
|
r439 | } | ||
Jani Honkonen
|
r142 | |||
Michal Klocek
|
r439 | void ChartDataSet::removeSeries(QSeries* series) | ||
{ | ||||
Tero Ahola
|
r158 | |||
Michal Klocek
|
r439 | QChartAxis* axis = m_seriesAxisMap.value(series); | ||
Marek Rosa
|
r401 | |||
Michal Klocek
|
r439 | if(!axis){ | ||
qWarning()<<"Can not remove series. Series not found on the chart."; | ||||
Marek Rosa
|
r401 | return; | ||
} | ||||
Michal Klocek
|
r439 | emit seriesRemoved(series); | ||
m_seriesAxisMap.remove(series); | ||||
Michal Klocek
|
r139 | |||
Michal Klocek
|
r439 | if(series->parent()==this){ | ||
delete series; | ||||
series=0; | ||||
Michal Klocek
|
r139 | } | ||
Michal Klocek
|
r439 | QList<QChartAxis*> axes = m_seriesAxisMap.values(); | ||
Michal Klocek
|
r223 | |||
Michal Klocek
|
r439 | int i = axes.indexOf(axis); | ||
Michal Klocek
|
r223 | |||
Michal Klocek
|
r439 | if(i==-1){ | ||
Domain* domain = m_axisDomainMap.take(axis); | ||||
emit axisRemoved(axis); | ||||
Michal Klocek
|
r444 | if(axis!=axisY()){ | ||
if(axis->parent()==this){ | ||||
delete axis; | ||||
axis=0; | ||||
} | ||||
} | ||||
Michal Klocek
|
r439 | delete domain; | ||
} | ||||
Michal Klocek
|
r223 | |||
Michal Klocek
|
r439 | if(m_seriesAxisMap.values().size()==0) | ||
{ | ||||
m_axisXInitialized=false; | ||||
emit axisRemoved(axisX()); | ||||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r139 | } | ||
Michal Klocek
|
r258 | void ChartDataSet::removeAllSeries() | ||
{ | ||||
Michal Klocek
|
r439 | QList<QSeries*> series = m_seriesAxisMap.keys(); | ||
Michal Klocek
|
r223 | |||
Michal Klocek
|
r439 | foreach(QSeries* s , series) { | ||
removeSeries(s); | ||||
Michal Klocek
|
r139 | } | ||
Michal Klocek
|
r439 | Q_ASSERT(m_seriesAxisMap.count()==0); | ||
Q_ASSERT(m_axisDomainMap.count()==0); | ||||
Michal Klocek
|
r139 | } | ||
Michal Klocek
|
r439 | //to be removed with PIMPL | ||
void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) const | ||||
Michal Klocek
|
r139 | { | ||
Michal Klocek
|
r439 | switch(series->type()) | ||
{ | ||||
Michal Klocek
|
r470 | case QSeries::SeriesTypeLine: | ||
case QSeries::SeriesTypeSpline: | ||||
case QSeries::SeriesTypeScatter: | ||||
{ | ||||
Michal Klocek
|
r373 | |||
Michal Klocek
|
r470 | QXYSeries* xySeries = static_cast<QXYSeries*>(series); | ||
Michal Klocek
|
r265 | |||
Michal Klocek
|
r470 | qreal minX(domain->minX()); | ||
qreal minY(domain->minY()); | ||||
qreal maxX(domain->maxX()); | ||||
qreal maxY(domain->maxY()); | ||||
for (int i = 0; i < xySeries->count(); i++) | ||||
Michal Klocek
|
r439 | { | ||
Michal Klocek
|
r470 | qreal x = xySeries->x(i); | ||
qreal y = xySeries->y(i); | ||||
minX = qMin(minX, x); | ||||
minY = qMin(minY, y); | ||||
maxX = qMax(maxX, x); | ||||
maxY = qMax(maxY, y); | ||||
Michal Klocek
|
r439 | } | ||
Michal Klocek
|
r530 | |||
domain->setRange(minX, maxX, minY, maxY); | ||||
Michal Klocek
|
r439 | break; | ||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r439 | case QSeries::SeriesTypeArea: { | ||
Michal Klocek
|
r400 | |||
Michal Klocek
|
r439 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); | ||
Michal Klocek
|
r223 | |||
Michal Klocek
|
r439 | QLineSeries* upperSeries = areaSeries->upperSeries(); | ||
QLineSeries* lowerSeries = areaSeries->lowerSeries(); | ||||
Michal Klocek
|
r223 | |||
Michal Klocek
|
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; | ||||
Marek Rosa
|
r418 | } | ||
Michal Klocek
|
r411 | |||
Michal Klocek
|
r439 | case QSeries::SeriesTypePie: { | ||
QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | ||||
// TODO: domain stuff | ||||
break; | ||||
} | ||||
Michal Klocek
|
r411 | |||
Michal Klocek
|
r223 | |||
Michal Klocek
|
r439 | default: { | ||
qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported"; | ||||
return; | ||||
break; | ||||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r439 | |||
Michal Klocek
|
r139 | } | ||
} | ||||
Michal Klocek
|
r439 | void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size) | ||
Michal Klocek
|
r139 | { | ||
Michal Klocek
|
r439 | QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap); | ||
while (i.hasNext()) { | ||||
i.next(); | ||||
i.value()->zoomIn(rect,size); | ||||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r439 | } | ||
Michal Klocek
|
r223 | |||
Michal Klocek
|
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); | ||||
} | ||||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r533 | int ChartDataSet::seriesCount(QSeries::QSeriesType type) | ||
{ | ||||
int count=0; | ||||
QMapIterator<QSeries*, QChartAxis*> i( m_seriesAxisMap); | ||||
while (i.hasNext()) { | ||||
i.next(); | ||||
if(i.key()->type()==type) count++; | ||||
} | ||||
return count; | ||||
} | ||||
Tero Ahola
|
r538 | int ChartDataSet::seriesIndex(QSeries *series) | ||
{ | ||||
int count(-1); | ||||
QMapIterator<QSeries*, QChartAxis*> i(m_seriesAxisMap); | ||||
while (i.hasNext()) { | ||||
i.next(); | ||||
count++; | ||||
if (i.key() == series) | ||||
return count; | ||||
} | ||||
return count; | ||||
} | ||||
Michal Klocek
|
r360 | QChartAxis* ChartDataSet::axisY(QSeries* series) const | ||
Michal Klocek
|
r223 | { | ||
if(series == 0) return m_axisY; | ||||
Michal Klocek
|
r439 | return m_seriesAxisMap.value(series); | ||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r439 | Domain* ChartDataSet::domain(QSeries* series) const | ||
Michal Klocek
|
r223 | { | ||
Michal Klocek
|
r439 | QChartAxis* axis = m_seriesAxisMap.value(series); | ||
if(axis){ | ||||
return m_axisDomainMap.value(axis); | ||||
}else | ||||
return 0; | ||||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r439 | Domain* ChartDataSet::domain(QChartAxis* axis) const | ||
Michal Klocek
|
r223 | { | ||
Michal Klocek
|
r444 | if(axis==axisX()) { | ||
return m_axisDomainMap.value(axisY()); | ||||
} | ||||
else { | ||||
return m_axisDomainMap.value(axis); | ||||
} | ||||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r439 | QChartAxis* ChartDataSet::axis(QSeries* series) const | ||
Michal Klocek
|
r223 | { | ||
Michal Klocek
|
r439 | return m_seriesAxisMap.value(series); | ||
Michal Klocek
|
r131 | } | ||
Michal Klocek
|
r531 | void ChartDataSet::scrollDomain(int dx,int dy,const QSizeF& size) | ||
{ | ||||
QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap); | ||||
while (i.hasNext()) { | ||||
i.next(); | ||||
i.value()->move(dx,dy,size); | ||||
} | ||||
} | ||||
Michal Klocek
|
r131 | #include "moc_chartdataset_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||