qchart.cpp
446 lines
| 12.7 KiB
| text/x-c
|
CppLexer
/ src / qchart.cpp
|
r12 | #include "qchart.h" | ||
|
r21 | #include "qchartseries.h" | ||
|
r42 | #include "qscatterseries.h" | ||
#include "qscatterseries_p.h" | ||||
|
r51 | #include "qpieseries.h" | ||
|
r103 | #include "qpieseries_p.h" | ||
|
r45 | #include "qxychartseries.h" | ||
|
r85 | #include "qchartaxis.h" | ||
|
r56 | #include "barchartseries.h" | ||
#include "bargroup.h" | ||||
|
r95 | #include "stackedbarchartseries.h" | ||
#include "stackedbargroup.h" | ||||
|
r101 | #include "percentbarchartseries.h" | ||
#include "percentbargroup.h" | ||||
|
r103 | #include "charttheme_p.h" | ||
|
r108 | #include "chartitem_p.h" | ||
|
r56 | |||
|
r21 | #include "xylinechartitem_p.h" | ||
|
r67 | #include "plotdomain_p.h" | ||
#include "axisitem_p.h" | ||||
|
r131 | #include "chartpresenter_p.h" | ||
#include "chartdataset_p.h" | ||||
|
r42 | #include <QGraphicsScene> | ||
|
r115 | #include <QGraphicsSceneResizeEvent> | ||
|
r21 | #include <QDebug> | ||
|
r12 | |||
|
r30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
|
r12 | |||
|
r115 | QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), | ||
|
r87 | m_backgroundItem(0), | ||
m_titleItem(0), | ||||
|
r110 | m_axisXItem(new AxisItem(AxisItem::X_AXIS, this)), | ||
|
r64 | m_plotDataIndex(0), | ||
|
r103 | m_marginSize(0), | ||
|
r131 | m_chartTheme(new ChartTheme(this)), | ||
|
r135 | m_dataset(0), | ||
//m_dataset(new ChartDataSet(this)), | ||||
m_presenter(0) | ||||
//m_presenter(new ChartPresenter(this,m_dataset)) | ||||
|
r12 | { | ||
|
r64 | // TODO: the default theme? | ||
|
r91 | setTheme(QChart::ChartThemeDefault); | ||
|
r85 | |||
|
r67 | PlotDomain domain; | ||
|
r103 | m_plotDomainList << domain; | ||
|
r87 | m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this); | ||
|
r108 | m_chartItems << m_axisXItem; | ||
m_chartItems << m_axisYItem.at(0); | ||||
|
r12 | } | ||
QChart::~QChart(){} | ||||
|
r21 | void QChart::addSeries(QChartSeries* series) | ||
|
r12 | { | ||
|
r131 | if(m_dataset) { | ||
m_dataset->addSeries(series); | ||||
return; | ||||
} | ||||
|
r120 | Q_ASSERT(series); | ||
Q_ASSERT(series->type() != QChartSeries::SeriesTypeInvalid); | ||||
|
r45 | |||
|
r131 | // TODO: we should check the series not already added | ||
|
r75 | m_chartSeries << series; | ||
|
r21 | |||
|
r111 | m_plotDataIndex = 0 ; | ||
m_plotDomainList.resize(1); | ||||
PlotDomain& domain = m_plotDomainList[m_plotDataIndex]; | ||||
|
r21 | switch(series->type()) | ||
{ | ||||
|
r30 | case QChartSeries::SeriesTypeLine: { | ||
|
r47 | |||
QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series); | ||||
|
r108 | for (int i = 0 ; i < xyseries->count() ; i++) { | ||
|
r47 | qreal x = xyseries->x(i); | ||
qreal y = xyseries->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); | ||||
} | ||||
|
r131 | //XYLineChartItem* item = new XYLineChartItem(xyseries,0,this); | ||
|
r67 | |||
|
r131 | //m_chartItems << item; | ||
|
r108 | // TODO: | ||
//m_chartTheme->addObserver(xyseries); | ||||
|
r67 | |||
|
r21 | break; | ||
|
r12 | } | ||
|
r56 | case QChartSeries::SeriesTypeBar: { | ||
qDebug() << "barSeries added"; | ||||
BarChartSeries* barSeries = static_cast<BarChartSeries*>(series); | ||||
|
r78 | BarGroup* barGroup = new BarGroup(*barSeries,this); | ||
|
r82 | |||
// Add some fugly colors for 5 fist series... | ||||
barGroup->addColor(QColor(255,0,0,128)); | ||||
barGroup->addColor(QColor(255,255,0,128)); | ||||
barGroup->addColor(QColor(0,255,0,128)); | ||||
barGroup->addColor(QColor(0,0,255,128)); | ||||
barGroup->addColor(QColor(255,128,0,128)); | ||||
|
r108 | m_chartItems << barGroup; | ||
|
r78 | childItems().append(barGroup); | ||
|
r107 | |||
qreal x = barSeries->countColumns(); | ||||
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); | ||||
|
r119 | m_axisXItem->setVisible(false); | ||
|
r56 | break; | ||
} | ||||
|
r95 | case QChartSeries::SeriesTypeStackedBar: { | ||
qDebug() << "barSeries added"; | ||||
StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series); | ||||
StackedBarGroup* stackedBarGroup = new StackedBarGroup(*stackedBarSeries,this); | ||||
// Add some fugly colors for 5 fist series... | ||||
stackedBarGroup->addColor(QColor(255,0,0,128)); | ||||
stackedBarGroup->addColor(QColor(255,255,0,128)); | ||||
stackedBarGroup->addColor(QColor(0,255,0,128)); | ||||
stackedBarGroup->addColor(QColor(0,0,255,128)); | ||||
stackedBarGroup->addColor(QColor(255,128,0,128)); | ||||
|
r108 | m_chartItems << stackedBarGroup; | ||
|
r95 | childItems().append(stackedBarGroup); | ||
|
r107 | |||
qreal x = stackedBarSeries->countColumns(); | ||||
qreal y = stackedBarSeries->maxColumnSum(); | ||||
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); | ||||
|
r119 | m_axisXItem->setVisible(false); | ||
|
r95 | break; | ||
} | ||||
|
r101 | case QChartSeries::SeriesTypePercentBar: { | ||
qDebug() << "barSeries added"; | ||||
PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series); | ||||
PercentBarGroup* percentBarGroup = new PercentBarGroup(*percentBarSeries,this); | ||||
// Add some fugly colors for 5 fist series... | ||||
percentBarGroup->addColor(QColor(255,0,0,128)); | ||||
percentBarGroup->addColor(QColor(255,255,0,128)); | ||||
percentBarGroup->addColor(QColor(0,255,0,128)); | ||||
percentBarGroup->addColor(QColor(0,0,255,128)); | ||||
percentBarGroup->addColor(QColor(255,128,0,128)); | ||||
|
r108 | m_chartItems << percentBarGroup; | ||
|
r101 | childItems().append(percentBarGroup); | ||
|
r107 | |||
qreal x = percentBarSeries->countColumns(); | ||||
domain.m_minX = qMin(domain.m_minX,x); | ||||
domain.m_minY = 0; | ||||
domain.m_maxX = qMax(domain.m_maxX,x); | ||||
domain.m_maxY = 100; | ||||
|
r119 | m_axisXItem->setVisible(false); | ||
|
r101 | break; | ||
} | ||||
|
r42 | case QChartSeries::SeriesTypeScatter: { | ||
|
r65 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); | ||
|
r108 | scatterSeries->d->m_theme = m_chartTheme->themeForSeries(); | ||
|
r75 | scatterSeries->d->setParentItem(this); | ||
|
r111 | scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); | ||
|
r108 | m_chartItems << scatterSeries->d; | ||
m_chartTheme->addObserver(scatterSeries->d); | ||||
|
r111 | |||
foreach (qreal x, scatterSeries->d->m_x) { | ||||
domain.m_minX = qMin(domain.m_minX, x); | ||||
domain.m_maxX = qMax(domain.m_maxX, x); | ||||
} | ||||
foreach (qreal y, scatterSeries->d->m_y) { | ||||
domain.m_minY = qMin(domain.m_minY, y); | ||||
domain.m_maxY = qMax(domain.m_maxY, y); | ||||
} | ||||
|
r75 | break; | ||
|
r65 | } | ||
|
r51 | case QChartSeries::SeriesTypePie: { | ||
|
r65 | QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series); | ||
|
r108 | pieSeries->d->setParentItem(this); | ||
m_chartItems << pieSeries->d; | ||||
pieSeries->d->m_chartTheme = m_chartTheme; | ||||
m_chartTheme->addObserver(pieSeries->d); | ||||
|
r75 | break; | ||
|
r65 | } | ||
|
r120 | default: | ||
break; | ||||
|
r65 | } | ||
|
r108 | |||
// Update all the items to match the new visible area of the chart | ||||
foreach(ChartItem* i, m_chartItems) | ||||
i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex)); | ||||
|
r65 | } | ||
QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type) | ||||
{ | ||||
// TODO: support also other types; not only scatter and pie | ||||
QChartSeries *series(0); | ||||
switch (type) { | ||||
case QChartSeries::SeriesTypeLine: { | ||||
series = QXYChartSeries::create(); | ||||
break; | ||||
} | ||||
case QChartSeries::SeriesTypeBar: { | ||||
series = new BarChartSeries(this); | ||||
break; | ||||
} | ||||
|
r95 | case QChartSeries::SeriesTypeStackedBar: { | ||
series = new StackedBarChartSeries(this); | ||||
break; | ||||
} | ||||
|
r101 | case QChartSeries::SeriesTypePercentBar: { | ||
series = new PercentBarChartSeries(this); | ||||
break; | ||||
} | ||||
|
r65 | case QChartSeries::SeriesTypeScatter: { | ||
series = new QScatterSeries(this); | ||||
break; | ||||
} | ||||
case QChartSeries::SeriesTypePie: { | ||||
series = new QPieSeries(this); | ||||
break; | ||||
|
r42 | } | ||
default: | ||||
|
r61 | Q_ASSERT(false); | ||
|
r42 | break; | ||
} | ||||
|
r65 | addSeries(series); | ||
return series; | ||||
|
r42 | } | ||
|
r48 | |||
|
r86 | void QChart::setBackground(const QColor& startColor, const QColor& endColor, GradientOrientation orientation) | ||
|
r69 | { | ||
|
r86 | |||
|
r87 | if(!m_backgroundItem){ | ||
m_backgroundItem = new QGraphicsRectItem(this); | ||||
m_backgroundItem->setZValue(-1); | ||||
|
r86 | } | ||
m_bacgroundOrinetation = orientation; | ||||
|
r103 | m_backgroundGradient.setColorAt(0.0, startColor); | ||
m_backgroundGradient.setColorAt(1.0, endColor); | ||||
|
r122 | m_backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); | ||
|
r86 | |||
if(orientation == VerticalGradientOrientation){ | ||||
|
r122 | m_backgroundGradient.setFinalStop(0,1); | ||
|
r86 | }else{ | ||
|
r122 | m_backgroundGradient.setFinalStop(1,0); | ||
|
r86 | } | ||
|
r87 | m_backgroundItem->setBrush(m_backgroundGradient); | ||
m_backgroundItem->setPen(Qt::NoPen); | ||||
m_backgroundItem->update(); | ||||
|
r69 | } | ||
|
r122 | void QChart::setChartBackgroundBrush(const QBrush& brush) | ||
{ | ||||
if(!m_backgroundItem){ | ||||
m_backgroundItem = new QGraphicsRectItem(this); | ||||
m_backgroundItem->setZValue(-1); | ||||
} | ||||
m_backgroundItem->setBrush(brush); | ||||
m_backgroundItem->update(); | ||||
} | ||||
void QChart::setChartBackgroundPen(const QPen& pen) | ||||
{ | ||||
if(!m_backgroundItem){ | ||||
m_backgroundItem = new QGraphicsRectItem(this); | ||||
m_backgroundItem->setZValue(-1); | ||||
} | ||||
m_backgroundItem->setPen(pen); | ||||
m_backgroundItem->update(); | ||||
} | ||||
|
r87 | void QChart::setTitle(const QString& title,const QFont& font) | ||
|
r69 | { | ||
|
r87 | if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this); | ||
m_titleItem->setPlainText(title); | ||||
m_titleItem->setFont(font); | ||||
|
r69 | } | ||
|
r28 | int QChart::margin() const | ||
{ | ||||
|
r53 | return m_marginSize; | ||
|
r28 | } | ||
|
r12 | void QChart::setMargin(int margin) | ||
{ | ||||
|
r53 | m_marginSize = margin; | ||
|
r12 | } | ||
|
r75 | void QChart::setTheme(QChart::ChartThemeId theme) | ||
|
r64 | { | ||
|
r108 | m_chartTheme->setTheme(theme); | ||
setBackground(m_chartTheme->d->m_gradientStartColor, | ||||
m_chartTheme->d->m_gradientEndColor, | ||||
m_bacgroundOrinetation); | ||||
// TODO: Move the controlling of the series presentations into private implementation of the | ||||
// series instead of QChart controlling themes for each | ||||
// In other words, the following should be used when creating xy series: | ||||
// m_chartTheme->addObserver(xyseries) | ||||
foreach (QChartSeries *series, m_chartSeries) { | ||||
if (series->type() == QChartSeries::SeriesTypeLine) { | ||||
QXYChartSeries *xyseries = static_cast<QXYChartSeries *>(series); | ||||
SeriesTheme seriesTheme = m_chartTheme->themeForSeries(); | ||||
xyseries->setPen(seriesTheme.linePen); | ||||
} | ||||
|
r75 | } | ||
|
r108 | |||
update(); | ||||
|
r64 | } | ||
|
r120 | QChart::ChartThemeId QChart::theme() | ||
{ | ||||
return (QChart::ChartThemeId) m_chartTheme->d->m_currentTheme; | ||||
} | ||||
|
r115 | void QChart::zoomInToRect(const QRectF& rectangle) | ||
|
r67 | { | ||
if(!rectangle.isValid()) return; | ||||
qreal margin = this->margin(); | ||||
|
r115 | QRectF rect = rectangle.normalized(); | ||
|
r67 | rect.translate(-margin, -margin); | ||
PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex]; | ||||
|
r76 | PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin); | ||
|
r67 | |||
m_plotDomainList.resize(m_plotDataIndex + 1); | ||||
|
r76 | m_plotDomainList<<domain; | ||
|
r67 | m_plotDataIndex++; | ||
|
r108 | foreach (ChartItem* item, m_chartItems) | ||
item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); | ||||
|
r67 | update(); | ||
} | ||||
void QChart::zoomIn() | ||||
{ | ||||
if (m_plotDataIndex < m_plotDomainList.count() - 1) { | ||||
m_plotDataIndex++; | ||||
|
r108 | foreach (ChartItem* item, m_chartItems) | ||
|
r67 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); | ||
update(); | ||||
|
r93 | } else { | ||
|
r115 | QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); | ||
|
r67 | rect.setWidth(rect.width()/2); | ||
rect.setHeight(rect.height()/2); | ||||
rect.moveCenter(m_rect.center()); | ||||
zoomInToRect(rect); | ||||
} | ||||
} | ||||
void QChart::zoomOut() | ||||
{ | ||||
if (m_plotDataIndex > 0) { | ||||
m_plotDataIndex--; | ||||
|
r108 | foreach (ChartItem* item, m_chartItems) | ||
|
r67 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); | ||
update(); | ||||
} | ||||
} | ||||
|
r93 | void QChart::zoomReset() | ||
{ | ||||
if (m_plotDataIndex > 0) { | ||||
m_plotDataIndex = 0; | ||||
|
r108 | foreach (ChartItem* item, m_chartItems) | ||
|
r93 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); | ||
update(); | ||||
} | ||||
} | ||||
|
r85 | void QChart::setAxisX(const QChartAxis& axis) | ||
{ | ||||
|
r87 | setAxis(m_axisXItem,axis); | ||
|
r85 | } | ||
void QChart::setAxisY(const QChartAxis& axis) | ||||
{ | ||||
|
r87 | setAxis(m_axisYItem.at(0),axis); | ||
|
r85 | } | ||
void QChart::setAxisY(const QList<QChartAxis>& axis) | ||||
{ | ||||
//TODO not implemented | ||||
} | ||||
void QChart::setAxis(AxisItem *item, const QChartAxis& axis) | ||||
{ | ||||
item->setVisible(axis.isAxisVisible()); | ||||
} | ||||
|
r115 | void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) | ||
{ | ||||
m_rect = QRectF(QPoint(0,0),event->newSize()); | ||||
QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); | ||||
// recalculate title position | ||||
if (m_titleItem) { | ||||
QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); | ||||
m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2); | ||||
} | ||||
//recalculate background gradient | ||||
if (m_backgroundItem) { | ||||
m_backgroundItem->setRect(rect); | ||||
} | ||||
// resize and reposition childs | ||||
foreach (ChartItem *item, m_chartItems) { | ||||
item->setPos(rect.topLeft()); | ||||
item->setSize(rect.size()); | ||||
} | ||||
|
r117 | QGraphicsWidget::resizeEvent(event); | ||
|
r115 | update(); | ||
} | ||||
|
r64 | #include "moc_qchart.cpp" | ||
|
r48 | |||
|
r30 | QTCOMMERCIALCHART_END_NAMESPACE | ||