chartdataset.cpp
334 lines
| 9.5 KiB
| text/x-c
|
CppLexer
/ src / chartdataset.cpp
Michal Klocek
|
r771 | /**************************************************************************** | ||
** | ||||
** Copyright (C) 2012 Digia Plc | ||||
** All rights reserved. | ||||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
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 | |||
Tero Ahola
|
r825 | if (seriesIndex(series) > -1) { | ||
Michal Klocek
|
r439 | 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); | ||
Tero Ahola
|
r825 | if (!domain) { | ||
Michal Klocek
|
r787 | domain = new Domain(axisY); | ||
Michal Klocek
|
r688 | QObject::connect(axisY,SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool))); | ||
QObject::connect(axisX(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool))); | ||||
Michal Klocek
|
r678 | QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axisY,SLOT(handleAxisRangeChanged(qreal,qreal,int))); | ||
QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axisX(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); | ||||
Michal Klocek
|
r442 | //initialize | ||
Michal Klocek
|
r439 | m_axisDomainMap.insert(axisY,domain); | ||
emit axisAdded(axisY,domain); | ||||
Marek Rosa
|
r401 | } | ||
Michal Klocek
|
r421 | |||
Tero Ahola
|
r825 | if (!m_axisXInitialized) { | ||
emit axisAdded(axisX(), domain); | ||||
m_axisXInitialized = true; | ||||
Michal Klocek
|
r439 | } | ||
Michal Klocek
|
r421 | |||
Tero Ahola
|
r825 | calculateDomain(series, domain); | ||
Michal Klocek
|
r421 | |||
Tero Ahola
|
r825 | m_seriesAxisList.append(QPair<QSeries*, QChartAxis*>(series, axisY)); | ||
Michal Klocek
|
r139 | |||
Tero Ahola
|
r825 | emit seriesAdded(series, domain); | ||
Michal Klocek
|
r439 | } | ||
Jani Honkonen
|
r142 | |||
Michal Klocek
|
r439 | void ChartDataSet::removeSeries(QSeries* series) | ||
{ | ||||
Tero Ahola
|
r825 | int index = seriesIndex(series); | ||
Jani Honkonen
|
r828 | if (index < 0) { | ||
Tero Ahola
|
r825 | qWarning() << "Can not remove series. Series not found on the chart."; | ||
Marek Rosa
|
r401 | return; | ||
} | ||||
Michal Klocek
|
r139 | |||
Tero Ahola
|
r825 | // Remove the series and the axis from the container | ||
QChartAxis* axis = m_seriesAxisList.at(index).second; | ||||
m_seriesAxisList.removeAt(index); | ||||
// Delete the series | ||||
emit seriesRemoved(series); | ||||
if (series->parent() == this) { | ||||
Michal Klocek
|
r439 | delete series; | ||
Tero Ahola
|
r825 | series = 0; | ||
Michal Klocek
|
r139 | } | ||
Tero Ahola
|
r825 | // Check if the Y axis is still in use | ||
bool yAxisInUse(false); | ||||
for (int i(0); i < m_seriesAxisList.count(); i++) { | ||||
QPair<QSeries *, QChartAxis *> pair = m_seriesAxisList.at(i); | ||||
if (pair.second == axis) | ||||
yAxisInUse = true; | ||||
} | ||||
Michal Klocek
|
r223 | |||
Tero Ahola
|
r825 | // Remove the Y axis if not in use | ||
if (!yAxisInUse) { | ||||
Michal Klocek
|
r439 | Domain* domain = m_axisDomainMap.take(axis); | ||
emit axisRemoved(axis); | ||||
Tero Ahola
|
r825 | if (axis != axisY()) { | ||
// Delete the Y axis unless it is the default one | ||||
if (axis->parent() == this) { | ||||
Michal Klocek
|
r444 | delete axis; | ||
Tero Ahola
|
r825 | axis = 0; | ||
Michal Klocek
|
r444 | } | ||
} | ||||
Michal Klocek
|
r439 | delete domain; | ||
} | ||||
Michal Klocek
|
r223 | |||
Tero Ahola
|
r825 | // Remove the x axis in case there are no y-axes left | ||
if (m_seriesAxisList.count() == 0) { | ||||
m_axisXInitialized = false; | ||||
Michal Klocek
|
r439 | emit axisRemoved(axisX()); | ||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r139 | } | ||
Michal Klocek
|
r258 | void ChartDataSet::removeAllSeries() | ||
{ | ||||
Tero Ahola
|
r825 | while (m_seriesAxisList.count()) { | ||
QPair<QSeries *, QChartAxis *> pair = m_seriesAxisList.last(); | ||||
removeSeries(pair.first); | ||||
Michal Klocek
|
r139 | } | ||
Tero Ahola
|
r825 | Q_ASSERT(m_seriesAxisList.count() == 0); | ||
Q_ASSERT(m_axisDomainMap.count() == 0); | ||||
Michal Klocek
|
r139 | } | ||
Michal Klocek
|
r439 | //to be removed with PIMPL | ||
Michal Klocek
|
r701 | void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) | ||
Michal Klocek
|
r139 | { | ||
Michal Klocek
|
r701 | qreal minX(domain->minX()); | ||
qreal minY(domain->minY()); | ||||
qreal maxX(domain->maxX()); | ||||
qreal maxY(domain->maxY()); | ||||
int tickXCount(domain->tickXCount()); | ||||
int tickYCount(domain->tickYCount()); | ||||
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 | 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 | } | ||
break; | ||||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r439 | case QSeries::SeriesTypeArea: { | ||
Michal Klocek
|
r400 | |||
Michal Klocek
|
r439 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); | ||
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); | ||||
Michal Klocek
|
r701 | minX = qMin(minX, x); | ||
minY = qMin(minY, y); | ||||
maxX = qMax(maxX, x); | ||||
maxY = qMax(maxY, y); | ||||
Michal Klocek
|
r439 | } | ||
if(lowerSeries) { | ||||
for (int i = 0; i < lowerSeries->count(); i++) | ||||
{ | ||||
qreal x = lowerSeries->x(i); | ||||
qreal y = lowerSeries->y(i); | ||||
Michal Klocek
|
r701 | minX = qMin(minX, x); | ||
minY = qMin(minY, y); | ||||
maxX = qMax(maxX, x); | ||||
maxY = qMax(maxY, y); | ||||
Michal Klocek
|
r439 | }} | ||
break; | ||||
} | ||||
case QSeries::SeriesTypeBar: { | ||||
Michal Klocek
|
r701 | |||
Michal Klocek
|
r439 | QBarSeries* barSeries = static_cast<QBarSeries*>(series); | ||
qreal x = barSeries->categoryCount(); | ||||
qreal y = barSeries->max(); | ||||
Michal Klocek
|
r701 | minX = qMin(minX, x); | ||
minY = qMin(minY, y); | ||||
maxX = qMax(maxX, x); | ||||
maxY = qMax(maxY, y); | ||||
tickXCount = x+1; | ||||
setupCategories(barSeries); | ||||
Michal Klocek
|
r439 | break; | ||
} | ||||
case QSeries::SeriesTypeStackedBar: { | ||||
Michal Klocek
|
r701 | |||
Michal Klocek
|
r439 | QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); | ||
qreal x = stackedBarSeries->categoryCount(); | ||||
qreal y = stackedBarSeries->maxCategorySum(); | ||||
Michal Klocek
|
r701 | minX = qMin(minX, x); | ||
minY = qMin(minY, y); | ||||
maxX = qMax(maxX, x); | ||||
maxY = qMax(maxY, y); | ||||
tickXCount = x+1; | ||||
setupCategories(stackedBarSeries); | ||||
Michal Klocek
|
r439 | break; | ||
} | ||||
case QSeries::SeriesTypePercentBar: { | ||||
Michal Klocek
|
r701 | |||
Michal Klocek
|
r439 | QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); | ||
qreal x = percentBarSeries->categoryCount(); | ||||
Michal Klocek
|
r701 | minX = qMin(minX, x); | ||
maxX = qMax(maxX, x); | ||||
minY = 0; | ||||
maxY = 100; | ||||
setupCategories(percentBarSeries); | ||||
Michal Klocek
|
r439 | break; | ||
Marek Rosa
|
r418 | } | ||
Michal Klocek
|
r411 | |||
Michal Klocek
|
r439 | case QSeries::SeriesTypePie: { | ||
Jani Honkonen
|
r609 | //QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | ||
Michal Klocek
|
r439 | // TODO: domain stuff | ||
break; | ||||
} | ||||
Michal Klocek
|
r411 | |||
Michal Klocek
|
r223 | |||
Michal Klocek
|
r439 | default: { | ||
qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported"; | ||||
return; | ||||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r439 | |||
Michal Klocek
|
r139 | } | ||
Michal Klocek
|
r701 | |||
domain->setRangeX(minX,maxX,tickXCount); | ||||
domain->setRangeY(minY,maxY,tickYCount); | ||||
} | ||||
void ChartDataSet::setupCategories(QBarSeries* series) | ||||
{ | ||||
QChartAxisCategories* categories = axisX()->categories(); | ||||
categories->clear(); | ||||
Michal Klocek
|
r703 | categories->insert(series->categories()); | ||
Michal Klocek
|
r139 | } | ||
Michal Klocek
|
r439 | void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size) | ||
Michal Klocek
|
r139 | { | ||
Michal Klocek
|
r771 | QMapIterator<QChartAxis*, Domain*> i(m_axisDomainMap); | ||
Michal Klocek
|
r439 | 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) | ||
{ | ||||
Michal Klocek
|
r771 | QMapIterator<QChartAxis*, Domain*> i(m_axisDomainMap); | ||
Michal Klocek
|
r439 | while (i.hasNext()) { | ||
i.next(); | ||||
i.value()->zoomOut(rect,size); | ||||
} | ||||
Michal Klocek
|
r223 | } | ||
Tero Ahola
|
r825 | int ChartDataSet::seriesIndex(QSeries *series) const | ||
Tero Ahola
|
r538 | { | ||
Tero Ahola
|
r825 | for (int i(0); i < m_seriesAxisList.count(); i++) { | ||
QPair<QSeries *, QChartAxis *> pair = m_seriesAxisList.at(i); | ||||
if (pair.first == series) | ||||
return i; | ||||
Tero Ahola
|
r538 | } | ||
Tero Ahola
|
r825 | return -1; | ||
Tero Ahola
|
r538 | } | ||
Michal Klocek
|
r360 | QChartAxis* ChartDataSet::axisY(QSeries* series) const | ||
Michal Klocek
|
r223 | { | ||
Tero Ahola
|
r825 | if (series == 0) | ||
return m_axisY; | ||||
return m_seriesAxisList.at(seriesIndex(series)).second; | ||||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r439 | Domain* ChartDataSet::domain(QSeries* series) const | ||
Michal Klocek
|
r223 | { | ||
Tero Ahola
|
r825 | QChartAxis* axis = m_seriesAxisList.at(seriesIndex(series)).second; | ||
if (axis) | ||||
Michal Klocek
|
r439 | return m_axisDomainMap.value(axis); | ||
Tero Ahola
|
r825 | return 0; | ||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r439 | Domain* ChartDataSet::domain(QChartAxis* axis) const | ||
Michal Klocek
|
r223 | { | ||
Michal Klocek
|
r771 | if(!axis || axis==axisX()) { | ||
Michal Klocek
|
r444 | return m_axisDomainMap.value(axisY()); | ||
} | ||||
else { | ||||
return m_axisDomainMap.value(axis); | ||||
} | ||||
Michal Klocek
|
r223 | } | ||
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 | ||||