chartdataset.cpp
373 lines
| 10.6 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
|
r145 | #include "qlinechartseries.h" | ||
sauimone
|
r216 | #include "qbarchartseries.h" | ||
#include "qstackedbarchartseries.h" | ||||
#include "qpercentbarchartseries.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() | ||||
{ | ||||
// TODO Auto-generated destructor stub | ||||
} | ||||
Michal Klocek
|
r223 | const Domain ChartDataSet::domain(QChartAxis *axisY) const | ||
Michal Klocek
|
r131 | { | ||
Marek Rosa
|
r401 | int i = m_domainMap.count(axisY); | ||
if(i == 0){ | ||||
return Domain(); | ||||
} | ||||
i = i - m_domainIndex -1; | ||||
Michal Klocek
|
r223 | return m_domainMap.values(axisY).at(i); | ||
Michal Klocek
|
r131 | } | ||
Michal Klocek
|
r223 | void ChartDataSet::addSeries(QChartSeries* series, QChartAxis *axisY) | ||
Michal Klocek
|
r131 | { | ||
Michal Klocek
|
r139 | // TODO: we should check the series not already added | ||
Jani Honkonen
|
r229 | series->setParent(this); // take ownership | ||
Michal Klocek
|
r223 | clearDomains(); | ||
if(axisY==0) axisY = m_axisY; | ||||
Michal Klocek
|
r248 | axisY->setParent(this); // take ownership | ||
Michal Klocek
|
r223 | |||
QList<QChartSeries*> seriesList = m_seriesMap.values(axisY); | ||||
QList<Domain> domainList = m_domainMap.values(axisY); | ||||
Q_ASSERT(domainList.size()<=1); | ||||
Domain domain; | ||||
if(domainList.size()>0) domain = domainList.at(0); | ||||
Michal Klocek
|
r139 | |||
switch(series->type()) | ||||
{ | ||||
Marek Rosa
|
r401 | case QChartSeries::SeriesTypeLine: { | ||
QLineChartSeries* xyseries = static_cast<QLineChartSeries*>(series); | ||||
for (int i = 0; i < xyseries->count(); i++) | ||||
{ | ||||
qreal x = xyseries->x(i); | ||||
qreal y = xyseries->y(i); | ||||
Michal Klocek
|
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); | ||||
} | ||||
Marek Rosa
|
r401 | break; | ||
} | ||||
case QChartSeries::SeriesTypeBar: { | ||||
qDebug() << "QChartSeries::SeriesTypeBar"; | ||||
QBarChartSeries* barSeries = static_cast<QBarChartSeries*>(series); | ||||
qreal x = barSeries->countCategories(); | ||||
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; | ||||
} | ||||
case QChartSeries::SeriesTypeStackedBar: { | ||||
sauimone
|
r173 | qDebug() << "QChartSeries::SeriesTypeStackedBar"; | ||
Michal Klocek
|
r139 | |||
Marek Rosa
|
r401 | QStackedBarChartSeries* stackedBarSeries = static_cast<QStackedBarChartSeries*>(series); | ||
qreal x = stackedBarSeries->countCategories(); | ||||
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; | ||||
} | ||||
case QChartSeries::SeriesTypePercentBar: { | ||||
sauimone
|
r173 | qDebug() << "QChartSeries::SeriesTypePercentBar"; | ||
Michal Klocek
|
r139 | |||
Marek Rosa
|
r401 | QPercentBarChartSeries* percentBarSeries = static_cast<QPercentBarChartSeries*>(series); | ||
qreal x = percentBarSeries->countCategories(); | ||||
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; | ||||
} | ||||
Michal Klocek
|
r139 | |||
Marek Rosa
|
r401 | case QChartSeries::SeriesTypePie: { | ||
QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | ||||
// TODO: domain stuff | ||||
break; | ||||
} | ||||
Jani Honkonen
|
r142 | |||
Marek Rosa
|
r401 | case QChartSeries::SeriesTypeScatter: { | ||
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()); | ||||
Tero Ahola
|
r158 | } | ||
Marek Rosa
|
r401 | break; | ||
} | ||||
Tero Ahola
|
r158 | |||
Marek Rosa
|
r401 | case QChartSeries::SeriesTypeSpline: { | ||
QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series); | ||||
for (int i = 0; i < splineSeries->count(); i++) | ||||
{ | ||||
qreal x = splineSeries->at(i).x(); | ||||
qreal y = splineSeries->at(i).y(); | ||||
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); | ||||
Michal Klocek
|
r139 | } | ||
Marek Rosa
|
r401 | break; | ||
} | ||||
default: { | ||||
qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported"; | ||||
return; | ||||
break; | ||||
} | ||||
Michal Klocek
|
r139 | |||
} | ||||
Michal Klocek
|
r223 | if(!m_domainMap.contains(axisY)) | ||
{ | ||||
emit axisAdded(axisY); | ||||
QObject::connect(axisY,SIGNAL(minChanged(qreal)),this,SLOT(handleMinChanged(qreal))); | ||||
QObject::connect(axisY,SIGNAL(maxChanged(qreal)),this,SLOT(handleMaxChanged(qreal))); | ||||
QObject::connect(axisY,SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*))); | ||||
} | ||||
m_domainMap.replace(axisY,domain); | ||||
m_seriesMap.insert(axisY,series); | ||||
if(!m_axisXInitialized) | ||||
{ | ||||
emit axisAdded(axisX()); | ||||
QObject::connect(axisX(),SIGNAL(minChanged(qreal)),this,SLOT(handleMinChanged(qreal))); | ||||
QObject::connect(axisX(),SIGNAL(maxChanged(qreal)),this,SLOT(handleMaxChanged(qreal))); | ||||
QObject::connect(axisX(),SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*))); | ||||
m_axisXInitialized=true; | ||||
} | ||||
Michal Klocek
|
r139 | emit seriesAdded(series); | ||
Michal Klocek
|
r223 | QStringList ylabels = createLabels(axisY,domain.m_minY,domain.m_maxY); | ||
QStringList xlabels = createLabels(axisX(),domain.m_minX,domain.m_maxX); | ||||
emit axisLabelsChanged(axisY,ylabels); | ||||
emit axisLabelsChanged(axisX(),xlabels); | ||||
emit seriesDomainChanged(series,domain); | ||||
} | ||||
void ChartDataSet::removeSeries(QChartSeries* series) | ||||
{ | ||||
QList<QChartAxis*> keys = m_seriesMap.uniqueKeys(); | ||||
foreach(QChartAxis* axis , keys) { | ||||
if(m_seriesMap.contains(axis,series)){ | ||||
emit seriesRemoved(series); | ||||
m_seriesMap.remove(axis,series); | ||||
//remove axis if no longer there | ||||
Michal Klocek
|
r258 | if(!m_seriesMap.contains(axis)){ | ||
Michal Klocek
|
r223 | emit axisRemoved(axis); | ||
m_domainMap.remove(axis); | ||||
Michal Klocek
|
r258 | if(axis != m_axisY) | ||
Marek Rosa
|
r401 | delete axis; | ||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r258 | series->setParent(0); | ||
Michal Klocek
|
r223 | break; | ||
} | ||||
} | ||||
Michal Klocek
|
r139 | } | ||
Michal Klocek
|
r258 | void ChartDataSet::removeAllSeries() | ||
{ | ||||
QList<QChartAxis*> keys = m_seriesMap.uniqueKeys(); | ||||
foreach(QChartAxis* axis , keys) { | ||||
QList<QChartSeries*> seriesList = m_seriesMap.values(axis); | ||||
for(int i =0 ; i < seriesList.size();i++ ) | ||||
{ | ||||
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; | ||||
Marek Rosa
|
r401 | } | ||
Michal Klocek
|
r258 | m_domainIndex=0; | ||
} | ||||
Michal Klocek
|
r139 | bool ChartDataSet::nextDomain() | ||
{ | ||||
Michal Klocek
|
r223 | int limit = (m_domainMap.values().size()/m_domainMap.uniqueKeys().size())-1; | ||
if (m_domainIndex < limit) { | ||||
Michal Klocek
|
r139 | m_domainIndex++; | ||
Michal Klocek
|
r223 | setDomain(m_domainIndex); | ||
Michal Klocek
|
r139 | return true; | ||
} | ||||
else { | ||||
return false; | ||||
} | ||||
} | ||||
bool ChartDataSet::previousDomain() | ||||
{ | ||||
if (m_domainIndex > 0) { | ||||
m_domainIndex--; | ||||
Michal Klocek
|
r223 | setDomain(m_domainIndex); | ||
Michal Klocek
|
r139 | return true; | ||
} | ||||
else { | ||||
return false; | ||||
} | ||||
} | ||||
Michal Klocek
|
r223 | void ChartDataSet::setDomain(int index) | ||
Michal Klocek
|
r139 | { | ||
Michal Klocek
|
r223 | QList<QChartAxis*> domainList = m_domainMap.uniqueKeys(); | ||
Michal Klocek
|
r265 | |||
Domain domain; | ||||
Michal Klocek
|
r223 | foreach (QChartAxis* axis , domainList) { | ||
int i = m_domainMap.count(axis) - index -1; | ||||
Q_ASSERT(i>=0); | ||||
Michal Klocek
|
r265 | domain = m_domainMap.values(axis).at(i); | ||
Michal Klocek
|
r223 | QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY); | ||
QList<QChartSeries*> seriesList = m_seriesMap.values(axis); | ||||
foreach(QChartSeries* series, seriesList) { | ||||
emit seriesDomainChanged(series,domain); | ||||
} | ||||
emit axisLabelsChanged(axis,labels); | ||||
} | ||||
QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX); | ||||
emit axisLabelsChanged(axisX(),labels); | ||||
} | ||||
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; | ||||
while(i--){ | ||||
domains.removeFirst(); | ||||
} | ||||
for(int j=domains.size()-1; j>=0 ;j--) | ||||
m_domainMap.insert(key,domains.at(j)); | ||||
Michal Klocek
|
r139 | } | ||
} | ||||
Michal Klocek
|
r223 | void ChartDataSet::addDomain(const QRectF& rect, const QRectF& viewport) | ||
Michal Klocek
|
r139 | { | ||
Michal Klocek
|
r223 | Q_ASSERT(rect.isValid()); | ||
Q_ASSERT(viewport.isValid()); | ||||
clearDomains(m_domainIndex); | ||||
QList<QChartAxis*> domainList = m_domainMap.uniqueKeys(); | ||||
Michal Klocek
|
r266 | Domain domain; | ||
Michal Klocek
|
r223 | foreach (QChartAxis* axis , domainList){ | ||
Michal Klocek
|
r266 | domain = m_domainMap.value(axis).subDomain(rect,viewport.width(),viewport.height()); | ||
Michal Klocek
|
r223 | QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY); | ||
QList<QChartSeries*> seriesList = m_seriesMap.values(axis); | ||||
foreach(QChartSeries* series, seriesList){ | ||||
emit seriesDomainChanged(series,domain); | ||||
} | ||||
emit axisLabelsChanged(axis,labels); | ||||
m_domainMap.insert(axis,domain); | ||||
} | ||||
QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX); | ||||
emit axisLabelsChanged(axisX(),labels); | ||||
Michal Klocek
|
r139 | m_domainIndex++; | ||
Michal Klocek
|
r223 | } | ||
QChartAxis* ChartDataSet::axisY(QChartSeries* series) const | ||||
{ | ||||
if(series == 0) return m_axisY; | ||||
QList<QChartAxis*> keys = m_seriesMap.uniqueKeys(); | ||||
foreach(QChartAxis* axis , keys) { | ||||
if(m_seriesMap.contains(axis,series)){ | ||||
return axis; | ||||
} | ||||
} | ||||
return 0; | ||||
} | ||||
QStringList ChartDataSet::createLabels(QChartAxis* axis,qreal min, qreal max) | ||||
{ | ||||
Q_ASSERT(max>=min); | ||||
Michal Klocek
|
r139 | |||
Michal Klocek
|
r223 | QStringList labels; | ||
Michal Klocek
|
r241 | int ticks = axis->ticksCount()-1; | ||
Michal Klocek
|
r223 | |||
for(int i=0; i<= ticks; i++){ | ||||
qreal value = min + (i * (max - min)/ ticks); | ||||
QString label = axis->axisTickLabel(value); | ||||
if(label.isEmpty()){ | ||||
labels << QString::number(value); | ||||
}else{ | ||||
labels << label; | ||||
} | ||||
} | ||||
return labels; | ||||
} | ||||
void ChartDataSet::handleMinChanged(qreal min) | ||||
{ | ||||
} | ||||
void ChartDataSet::handleMaxChanged(qreal max) | ||||
{ | ||||
} | ||||
void ChartDataSet::handleTickChanged(QChartAxis* axis) | ||||
{ | ||||
Domain domain = m_domainMap.value(axisY()); | ||||
if(axis==axisX()){ | ||||
QStringList labels = createLabels(axis,domain.m_minX,domain.m_maxX); | ||||
emit axisLabelsChanged(axis,labels); | ||||
}else{ | ||||
QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY); | ||||
emit axisLabelsChanged(axis,labels); | ||||
} | ||||
Michal Klocek
|
r131 | } | ||
#include "moc_chartdataset_p.cpp" | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||