declarativeseries.cpp
100 lines
| 2.9 KiB
| text/x-c
|
CppLexer
/ qmlplugin / declarativeseries.cpp
Tero Ahola
|
r120 | #include "declarativeseries.h" | ||
#include "declarativechart.h" | ||||
Tero Ahola
|
r179 | #include <qscatterseries.h> | ||
Michal Klocek
|
r145 | #include <qlinechartseries.h> | ||
Tero Ahola
|
r120 | #include <cmath> | ||
#include <QDebug> | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
DeclarativeSeries::DeclarativeSeries(QDeclarativeItem *parent) : | ||||
QDeclarativeItem(parent), | ||||
m_seriesType(SeriesTypeInvalid), // TODO: default type? | ||||
m_chart(0), | ||||
m_series(0) | ||||
{ | ||||
setFlag(QGraphicsItem::ItemHasNoContents, false); | ||||
connect(this, SIGNAL(parentChanged()), | ||||
this, SLOT(setParentForSeries())); | ||||
} | ||||
void DeclarativeSeries::setSeriesType(SeriesType type) | ||||
{ | ||||
if (!m_series || type != m_seriesType) { | ||||
m_seriesType = type; | ||||
initSeries(); | ||||
Tero Ahola
|
r180 | } else { | ||
m_seriesType = type; | ||||
Tero Ahola
|
r120 | } | ||
} | ||||
void DeclarativeSeries::setParentForSeries() | ||||
{ | ||||
Tero Ahola
|
r180 | if (!m_series) | ||
initSeries(); | ||||
else if (m_series->type() != m_seriesType) | ||||
initSeries(); | ||||
Tero Ahola
|
r120 | } | ||
void DeclarativeSeries::initSeries() | ||||
{ | ||||
DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent()); | ||||
if (declarativeChart && m_seriesType != SeriesTypeInvalid) { | ||||
delete m_series; | ||||
m_series = 0; | ||||
QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart); | ||||
qDebug() << "creating series for chart: " << chart; | ||||
Q_ASSERT(chart); | ||||
switch (m_seriesType) { | ||||
case SeriesTypeLine: { | ||||
Tero Ahola
|
r158 | m_series = new QLineChartSeries(this); | ||
Tero Ahola
|
r120 | for (qreal i(0.0); i < 100.0; i += 1.0) | ||
Michal Klocek
|
r145 | ((QLineChartSeries *)m_series)->add(i, i); | ||
Tero Ahola
|
r120 | chart->addSeries(m_series); | ||
break; | ||||
} | ||||
case SeriesTypeBar: | ||||
// fallthrough; bar and scatter use the same test data | ||||
case SeriesTypeScatter: { | ||||
m_series = chart->createSeries((QChartSeries::QChartSeriesType) m_seriesType); | ||||
Tero Ahola
|
r179 | QScatterSeries *scatter = qobject_cast<QScatterSeries *>(m_series); | ||
Q_ASSERT(scatter); | ||||
for (qreal i = 0; i < 100; i += 0.1) | ||||
scatter->addData(QPointF(i + (rand() % 5), | ||||
abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5))); | ||||
Tero Ahola
|
r120 | break; | ||
} | ||||
case SeriesTypeStackedBar: | ||||
break; | ||||
case SeriesTypePercentBar: | ||||
break; | ||||
case SeriesTypePie: { | ||||
m_series = chart->createSeries((QChartSeries::QChartSeriesType) m_seriesType); | ||||
QList<qreal> data; | ||||
data << 1.0; | ||||
data << 12.0; | ||||
data << 4.0; | ||||
Q_ASSERT(m_series->setData(data)); | ||||
break; | ||||
} | ||||
default: | ||||
break; | ||||
} | ||||
} | ||||
} | ||||
QVariant DeclarativeSeries::itemChange(GraphicsItemChange change, | ||||
const QVariant &value) | ||||
{ | ||||
// For debugging purposes only: | ||||
// qDebug() << QString::number(change) << " : " << value.toString(); | ||||
return QGraphicsItem::itemChange(change, value); | ||||
} | ||||
#include "moc_declarativeseries.cpp" | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||