chartdataset.cpp
448 lines
| 12.4 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() | ||||
{ | ||||
// TODO Auto-generated destructor stub | ||||
} | ||||
|
r223 | const Domain ChartDataSet::domain(QChartAxis *axisY) const | ||
|
r131 | { | ||
|
r401 | int i = m_domainMap.count(axisY); | ||
if(i == 0){ | ||||
return Domain(); | ||||
} | ||||
i = i - m_domainIndex -1; | ||||
|
r223 | return m_domainMap.values(axisY).at(i); | ||
|
r131 | } | ||
|
r360 | void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY) | ||
|
r131 | { | ||
|
r139 | // TODO: we should check the series not already added | ||
|
r420 | series->setParent(this);// take ownership | ||
|
r223 | clearDomains(); | ||
if(axisY==0) axisY = m_axisY; | ||||
|
r420 | axisY->setParent(this);// take ownership | ||
|
r223 | |||
|
r420 | QList<QSeries*> seriesList = m_seriesMap.values(axisY); | ||
|
r223 | |||
QList<Domain> domainList = m_domainMap.values(axisY); | ||||
Q_ASSERT(domainList.size()<=1); | ||||
Domain domain; | ||||
if(domainList.size()>0) domain = domainList.at(0); | ||||
|
r139 | |||
switch(series->type()) | ||||
{ | ||||
|
r418 | case QSeries::SeriesTypeLine: { | ||
|
r401 | |||
|
r426 | QLineSeries* lineSeries = static_cast<QLineSeries*>(series); | ||
|
r401 | |||
|
r426 | for (int i = 0; i < lineSeries->count(); i++) | ||
|
r401 | { | ||
|
r426 | qreal x = lineSeries->x(i); | ||
qreal y = lineSeries->y(i); | ||||
|
r139 | 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); | ||||
} | ||||
|
r401 | break; | ||
} | ||||
|
r426 | case QSeries::SeriesTypeArea: { | ||
|
r421 | |||
|
r426 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); | ||
|
r421 | |||
|
r426 | QLineSeries* upperSeries = areaSeries->upperSeries(); | ||
QLineSeries* lowerSeries = areaSeries->lowerSeries(); | ||||
|
r421 | |||
|
r426 | for (int i = 0; i < upperSeries->count(); i++) | ||
{ | ||||
qreal x = upperSeries->x(i); | ||||
qreal y = upperSeries->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); | ||||
} | ||||
if(lowerSeries){ | ||||
|
r421 | for (int i = 0; i < lowerSeries->count(); i++) | ||
{ | ||||
qreal x = lowerSeries->x(i); | ||||
qreal y = lowerSeries->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); | ||||
}} | ||||
|
r426 | break; | ||
} | ||||
case QSeries::SeriesTypeBar: { | ||||
qDebug() << "QChartSeries::SeriesTypeBar"; | ||||
QBarSeries* barSeries = static_cast<QBarSeries*>(series); | ||||
qreal x = barSeries->categoryCount(); | ||||
qreal y = barSeries->max(); | ||||
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); | ||||
break; | ||||
} | ||||
|
r418 | case QSeries::SeriesTypeStackedBar: { | ||
|
r173 | qDebug() << "QChartSeries::SeriesTypeStackedBar"; | ||
|
r139 | |||
|
r418 | QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); | ||
qreal x = stackedBarSeries->categoryCount(); | ||||
|
r401 | qreal y = stackedBarSeries->maxCategorySum(); | ||
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); | ||||
break; | ||||
} | ||||
|
r418 | case QSeries::SeriesTypePercentBar: { | ||
|
r173 | qDebug() << "QChartSeries::SeriesTypePercentBar"; | ||
|
r139 | |||
|
r418 | QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); | ||
qreal x = percentBarSeries->categoryCount(); | ||||
|
r401 | domain.m_minX = qMin(domain.m_minX,x); | ||
domain.m_minY = 0; | ||||
domain.m_maxX = qMax(domain.m_maxX,x); | ||||
domain.m_maxY = 100; | ||||
break; | ||||
} | ||||
|
r139 | |||
|
r418 | case QSeries::SeriesTypePie: { | ||
|
r401 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | ||
// TODO: domain stuff | ||||
break; | ||||
} | ||||
|
r142 | |||
|
r418 | case QSeries::SeriesTypeScatter: { | ||
|
r401 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); | ||
Q_ASSERT(scatterSeries); | ||||
foreach (QPointF point, scatterSeries->data()) { | ||||
domain.m_minX = qMin(domain.m_minX, point.x()); | ||||
domain.m_maxX = qMax(domain.m_maxX, point.x()); | ||||
domain.m_minY = qMin(domain.m_minY, point.y()); | ||||
domain.m_maxY = qMax(domain.m_maxY, point.y()); | ||||
|
r158 | } | ||
|
r401 | break; | ||
} | ||||
|
r158 | |||
|
r419 | case QSeries::SeriesTypeSpline: { | ||
|
r401 | QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series); | ||
for (int i = 0; i < splineSeries->count(); i++) | ||||
{ | ||||
|
r419 | qreal x = splineSeries->x(i); | ||
qreal y = splineSeries->y(i); | ||||
|
r401 | 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); | ||||
|
r139 | } | ||
|
r401 | break; | ||
} | ||||
default: { | ||||
qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported"; | ||||
return; | ||||
break; | ||||
} | ||||
|
r139 | |||
} | ||||
|
r223 | if(!m_domainMap.contains(axisY)) | ||
{ | ||||
emit axisAdded(axisY); | ||||
|
r400 | QObject::connect(axisY,SIGNAL(rangeChanged(QChartAxis*)),this,SLOT(handleRangeChanged(QChartAxis*))); | ||
|
r223 | QObject::connect(axisY,SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*))); | ||
} | ||||
if(!m_axisXInitialized) | ||||
{ | ||||
emit axisAdded(axisX()); | ||||
|
r400 | QObject::connect(axisX(),SIGNAL(rangeChanged(QChartAxis*)),this,SLOT(handleRangeChanged(QChartAxis*))); | ||
|
r223 | QObject::connect(axisX(),SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*))); | ||
m_axisXInitialized=true; | ||||
} | ||||
|
r400 | m_domainMap.replace(axisY,domain); | ||
m_seriesMap.insert(axisY,series); | ||||
|
r389 | emit seriesAdded(series); | ||
|
r411 | setDomain(m_domainIndex,axisY); | ||
|
r223 | |||
} | ||||
|
r360 | void ChartDataSet::removeSeries(QSeries* series) | ||
|
r223 | { | ||
QList<QChartAxis*> keys = m_seriesMap.uniqueKeys(); | ||||
foreach(QChartAxis* axis , keys) { | ||||
|
r420 | if(m_seriesMap.contains(axis,series)) { | ||
|
r223 | emit seriesRemoved(series); | ||
m_seriesMap.remove(axis,series); | ||||
//remove axis if no longer there | ||||
|
r420 | if(!m_seriesMap.contains(axis)) { | ||
|
r223 | emit axisRemoved(axis); | ||
m_domainMap.remove(axis); | ||||
|
r258 | if(axis != m_axisY) | ||
|
r401 | delete axis; | ||
|
r223 | } | ||
|
r258 | series->setParent(0); | ||
|
r223 | break; | ||
} | ||||
} | ||||
|
r139 | } | ||
|
r258 | void ChartDataSet::removeAllSeries() | ||
{ | ||||
QList<QChartAxis*> keys = m_seriesMap.uniqueKeys(); | ||||
foreach(QChartAxis* axis , keys) { | ||||
|
r360 | QList<QSeries*> seriesList = m_seriesMap.values(axis); | ||
|
r420 | for(int i =0; i < seriesList.size();i++ ) | ||
|
r258 | { | ||
emit seriesRemoved(seriesList.at(i)); | ||||
delete(seriesList.at(i)); | ||||
} | ||||
m_seriesMap.remove(axis); | ||||
m_domainMap.remove(axis); | ||||
emit axisRemoved(axis); | ||||
if(axis != m_axisY) delete axis; | ||||
|
r401 | } | ||
|
r258 | m_domainIndex=0; | ||
} | ||||
|
r139 | bool ChartDataSet::nextDomain() | ||
{ | ||||
|
r223 | int limit = (m_domainMap.values().size()/m_domainMap.uniqueKeys().size())-1; | ||
if (m_domainIndex < limit) { | ||||
|
r139 | m_domainIndex++; | ||
|
r223 | setDomain(m_domainIndex); | ||
|
r139 | return true; | ||
} | ||||
else { | ||||
return false; | ||||
} | ||||
} | ||||
bool ChartDataSet::previousDomain() | ||||
{ | ||||
if (m_domainIndex > 0) { | ||||
m_domainIndex--; | ||||
|
r223 | setDomain(m_domainIndex); | ||
|
r139 | return true; | ||
} | ||||
else { | ||||
return false; | ||||
} | ||||
} | ||||
|
r223 | void ChartDataSet::setDomain(int index) | ||
|
r139 | { | ||
|
r223 | QList<QChartAxis*> domainList = m_domainMap.uniqueKeys(); | ||
|
r265 | |||
|
r373 | if(domainList.count()==0) return; | ||
|
r265 | Domain domain; | ||
|
r223 | foreach (QChartAxis* axis , domainList) { | ||
int i = m_domainMap.count(axis) - index -1; | ||||
Q_ASSERT(i>=0); | ||||
|
r265 | domain = m_domainMap.values(axis).at(i); | ||
|
r223 | QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY); | ||
|
r360 | QList<QSeries*> seriesList = m_seriesMap.values(axis); | ||
foreach(QSeries* series, seriesList) { | ||||
|
r223 | emit seriesDomainChanged(series,domain); | ||
} | ||||
|
r403 | axis->updateRange(domain.m_minY,domain.m_maxY); | ||
|
r399 | emit axisRangeChanged(axis,labels); | ||
|
r400 | |||
|
r223 | } | ||
QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX); | ||||
|
r403 | axisX()->updateRange(domain.m_minX,domain.m_maxY); | ||
|
r399 | emit axisRangeChanged(axisX(),labels); | ||
|
r223 | } | ||
|
r411 | void ChartDataSet::setDomain(int index,QChartAxis* axis) | ||
{ | ||||
int i = m_domainMap.count(axis) - index -1; | ||||
Q_ASSERT(i>=0); | ||||
Domain domain = m_domainMap.values(axis).at(i); | ||||
{ | ||||
|
r418 | QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY); | ||
QList<QSeries*> seriesList = m_seriesMap.values(axis); | ||||
foreach(QSeries* series, seriesList) { | ||||
emit seriesDomainChanged(series,domain); | ||||
} | ||||
axis->updateRange(domain.m_minY,domain.m_maxY); | ||||
emit axisRangeChanged(axis,labels); | ||||
|
r411 | } | ||
QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX); | ||||
axisX()->updateRange(domain.m_minX,domain.m_maxY); | ||||
emit axisRangeChanged(axisX(),labels); | ||||
} | ||||
|
r223 | void ChartDataSet::clearDomains(int toIndex) | ||
{ | ||||
Q_ASSERT(toIndex>=0); | ||||
m_domainIndex = toIndex; | ||||
QList<QChartAxis*> keys = m_domainMap.uniqueKeys(); | ||||
foreach (QChartAxis* key , keys) | ||||
{ | ||||
QList<Domain> domains = m_domainMap.values(key); | ||||
m_domainMap.remove(key); | ||||
int i = domains.size() - toIndex - 1; | ||||
|
r420 | while(i--) { | ||
|
r223 | domains.removeFirst(); | ||
} | ||||
|
r420 | for(int j=domains.size()-1; j>=0;j--) | ||
|
r223 | m_domainMap.insert(key,domains.at(j)); | ||
|
r139 | } | ||
} | ||||
|
r223 | void ChartDataSet::addDomain(const QRectF& rect, const QRectF& viewport) | ||
|
r139 | { | ||
|
r223 | Q_ASSERT(rect.isValid()); | ||
Q_ASSERT(viewport.isValid()); | ||||
clearDomains(m_domainIndex); | ||||
QList<QChartAxis*> domainList = m_domainMap.uniqueKeys(); | ||||
|
r266 | Domain domain; | ||
|
r420 | foreach (QChartAxis* axis , domainList) { | ||
|
r266 | domain = m_domainMap.value(axis).subDomain(rect,viewport.width(),viewport.height()); | ||
|
r223 | m_domainMap.insert(axis,domain); | ||
} | ||||
|
r400 | setDomain(++m_domainIndex); | ||
|
r223 | } | ||
|
r360 | QChartAxis* ChartDataSet::axisY(QSeries* series) const | ||
|
r223 | { | ||
if(series == 0) return m_axisY; | ||||
QList<QChartAxis*> keys = m_seriesMap.uniqueKeys(); | ||||
foreach(QChartAxis* axis , keys) { | ||||
|
r420 | if(m_seriesMap.contains(axis,series)) { | ||
|
r223 | return axis; | ||
} | ||||
} | ||||
return 0; | ||||
} | ||||
QStringList ChartDataSet::createLabels(QChartAxis* axis,qreal min, qreal max) | ||||
{ | ||||
Q_ASSERT(max>=min); | ||||
|
r139 | |||
|
r223 | QStringList labels; | ||
|
r241 | int ticks = axis->ticksCount()-1; | ||
|
r223 | |||
|
r420 | for(int i=0; i<= ticks; i++) { | ||
|
r223 | qreal value = min + (i * (max - min)/ ticks); | ||
QString label = axis->axisTickLabel(value); | ||||
|
r420 | if(label.isEmpty()) { | ||
|
r223 | labels << QString::number(value); | ||
|
r420 | } | ||
else { | ||||
|
r223 | labels << label; | ||
} | ||||
} | ||||
return labels; | ||||
} | ||||
|
r400 | void ChartDataSet::handleRangeChanged(QChartAxis* axis) | ||
|
r223 | { | ||
|
r400 | qreal min = axis->min(); | ||
qreal max = axis->max(); | ||||
|
r223 | |||
|
r400 | if(axis==axisX()) { | ||
|
r223 | |||
|
r400 | m_domainIndex=0; | ||
clearDomains(m_domainIndex); | ||||
QList<QChartAxis*> domainList = m_domainMap.uniqueKeys(); | ||||
foreach (QChartAxis* axis , domainList) { | ||||
Q_ASSERT(m_domainMap.values(axis).size()==1); | ||||
Domain domain = m_domainMap.value(axis); | ||||
domain.m_minX=min; | ||||
domain.m_maxX=max; | ||||
m_domainMap.replace(axis,domain); | ||||
} | ||||
|
r411 | setDomain(m_domainIndex); | ||
|
r400 | } | ||
else { | ||||
QList<Domain> domains = m_domainMap.values(axis); | ||||
m_domainMap.remove(axis); | ||||
for(int i=0;i<domains.size();i++) | ||||
{ | ||||
domains[i].m_minY=min; | ||||
domains[i].m_maxY=max; | ||||
} | ||||
for(int j=domains.size()-1; j>=0;j--) | ||||
|
r418 | m_domainMap.insert(axis,domains.at(j)); | ||
|
r411 | |||
setDomain(m_domainIndex,axis); | ||||
|
r400 | } | ||
|
r223 | |||
} | ||||
void ChartDataSet::handleTickChanged(QChartAxis* axis) | ||||
{ | ||||
|
r420 | if(axis==axisX()) { | ||
|
r400 | Domain domain = m_domainMap.value(axisY()); | ||
|
r223 | QStringList labels = createLabels(axis,domain.m_minX,domain.m_maxX); | ||
|
r399 | emit axisRangeChanged(axis,labels); | ||
|
r420 | } | ||
else { | ||||
|
r400 | Domain domain = m_domainMap.value(axis); | ||
|
r223 | QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY); | ||
|
r399 | emit axisRangeChanged(axis,labels); | ||
|
r223 | } | ||
|
r131 | } | ||
#include "moc_chartdataset_p.cpp" | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||