qchart.cpp
394 lines
| 11.9 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" | ||
|
r45 | #include "qxychartseries.h" | ||
|
r85 | #include "qchartaxis.h" | ||
|
r56 | #include "barchartseries.h" | ||
#include "bargroup.h" | ||||
|
r21 | #include "xylinechartitem_p.h" | ||
|
r67 | #include "plotdomain_p.h" | ||
#include "axisitem_p.h" | ||||
|
r42 | #include <QGraphicsScene> | ||
|
r21 | #include <QDebug> | ||
|
r12 | |||
|
r30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
|
r12 | |||
|
r48 | QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent), | ||
|
r87 | m_backgroundItem(0), | ||
m_titleItem(0), | ||||
m_axisXItem(new AxisItem(AxisItem::X_AXIS,this)), | ||||
|
r64 | m_plotDataIndex(0), | ||
m_marginSize(0) | ||||
|
r12 | { | ||
|
r64 | // TODO: the default theme? | ||
|
r91 | setTheme(QChart::ChartThemeDefault); | ||
|
r85 | |||
|
r67 | PlotDomain domain; | ||
m_plotDomainList<<domain; | ||||
|
r87 | m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this); | ||
m_chartItems<<m_axisXItem; | ||||
m_chartItems<<m_axisYItem.at(0); | ||||
|
r12 | } | ||
QChart::~QChart(){} | ||||
|
r21 | QRectF QChart::boundingRect() const | ||
{ | ||||
|
r53 | return m_rect; | ||
|
r21 | } | ||
|
r12 | |||
|
r21 | void QChart::addSeries(QChartSeries* series) | ||
|
r12 | { | ||
|
r48 | // TODO: we should check the series not already added | ||
|
r45 | |||
|
r75 | m_chartSeries << series; | ||
|
r21 | |||
switch(series->type()) | ||||
{ | ||||
|
r30 | case QChartSeries::SeriesTypeLine: { | ||
|
r47 | |||
QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series); | ||||
|
r65 | // Use color defined by theme in case the series does not define a custom color | ||
|
r85 | |||
if (!xyseries->pen().color().isValid() && m_themeColors.count()) //TODO: wtf | ||||
xyseries->setPen(nextColor()); | ||||
|
r47 | |||
|
r67 | m_plotDataIndex = 0 ; | ||
m_plotDomainList.resize(1); | ||||
PlotDomain& domain = m_plotDomainList[m_plotDataIndex]; | ||||
|
r47 | |||
for (int i = 0 ; i < xyseries->count() ; i++) | ||||
{ | ||||
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); | ||||
} | ||||
XYLineChartItem* item = new XYLineChartItem(xyseries,this); | ||||
|
r67 | m_chartItems<<item; | ||
foreach(ChartItem* i ,m_chartItems) | ||||
i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex)); | ||||
|
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)); | ||||
|
r78 | m_chartItems<<barGroup; | ||
childItems().append(barGroup); | ||||
|
r56 | break; | ||
} | ||||
|
r42 | case QChartSeries::SeriesTypeScatter: { | ||
|
r65 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); | ||
|
r75 | scatterSeries->d->setParentItem(this); | ||
// Set pre-defined colors in case the series has no colors defined | ||||
if (!scatterSeries->markerColor().isValid()) | ||||
scatterSeries->setMarkerColor(nextColor()); | ||||
|
r54 | connect(this, SIGNAL(sizeChanged(QRectF)), | ||
scatterSeries, SLOT(chartSizeChanged(QRectF))); | ||||
|
r75 | // QColor nextColor = m_themeColors.takeFirst(); | ||
// nextColor.setAlpha(150); // TODO: default opacity? | ||||
// scatterSeries->setMarkerColor(nextColor); | ||||
break; | ||||
|
r65 | } | ||
|
r51 | case QChartSeries::SeriesTypePie: { | ||
|
r65 | QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series); | ||
|
r75 | for (int i(0); i < pieSeries->sliceCount(); i++) { | ||
if (!pieSeries->sliceColor(i).isValid()) | ||||
pieSeries->setSliceColor(i, nextColor()); | ||||
} | ||||
|
r54 | connect(this, SIGNAL(sizeChanged(QRectF)), | ||
pieSeries, SLOT(chartSizeChanged(QRectF))); | ||||
|
r75 | |||
// Set pre-defined colors in case the series has no colors defined | ||||
|
r64 | // TODO: how to define the color for all the slices of a pie? | ||
|
r75 | // for (int (i); i < pieSeries.sliceCount(); i++) | ||
break; | ||||
|
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; | ||||
} | ||||
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 | |||
|
r67 | void QChart::setSize(const QSize& size) | ||
|
r28 | { | ||
|
r67 | m_rect = QRect(QPoint(0,0),size); | ||
QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); | ||||
|
r86 | //recaculate title | ||
|
r87 | if(m_titleItem){ | ||
QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); | ||||
m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2); | ||||
|
r86 | |||
} | ||||
|
r69 | |||
//recalculate background gradient | ||||
|
r87 | if(m_backgroundItem){ | ||
m_backgroundItem->setRect(rect); | ||||
|
r86 | if(m_bacgroundOrinetation==HorizonatlGradientOrientation) | ||
|
r87 | m_backgroundGradient.setFinalStop(m_backgroundItem->rect().width(),0); | ||
|
r86 | else | ||
|
r87 | m_backgroundGradient.setFinalStop(0,m_backgroundItem->rect().height()); | ||
|
r86 | |||
|
r87 | m_backgroundItem->setBrush(m_backgroundGradient); | ||
|
r86 | } | ||
|
r69 | |||
//resize elements | ||||
|
r67 | foreach (ChartItem* item ,m_chartItems) { | ||
item->setPos(rect.topLeft()); | ||||
item->setSize(rect.size()); | ||||
|
r47 | |||
|
r67 | } | ||
|
r54 | // TODO: TTD for setting scale | ||
//emit scaleChanged(100, 100); | ||||
|
r48 | // TODO: calculate the origo | ||
// TODO: not sure if emitting a signal here is the best from performance point of view | ||||
|
r54 | emit sizeChanged(QRectF(0, 0, size.width(), size.height())); | ||
|
r48 | |||
|
r21 | update(); | ||
|
r12 | } | ||
|
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; | ||||
m_backgroundGradient.setColorAt( 0.0, startColor); | ||||
m_backgroundGradient.setColorAt( 1.0, endColor); | ||||
m_backgroundGradient.setStart(0,0); | ||||
if(orientation == VerticalGradientOrientation){ | ||||
m_backgroundGradient.setFinalStop(0,m_rect.height()); | ||||
}else{ | ||||
m_backgroundGradient.setFinalStop(m_rect.width(),0); | ||||
} | ||||
|
r87 | m_backgroundItem->setBrush(m_backgroundGradient); | ||
m_backgroundItem->setPen(Qt::NoPen); | ||||
m_backgroundItem->update(); | ||||
|
r69 | } | ||
|
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 | { | ||
|
r75 | // if (theme != m_currentTheme) { | ||
m_themeColors.clear(); | ||||
|
r64 | // TODO: define color themes | ||
switch (theme) { | ||||
|
r81 | case QChart::ChartThemeDefault: | ||
// TODO: define the default theme based on the OS | ||||
|
r91 | m_themeColors.append(QColor(QRgb(0xff000000))); | ||
m_themeColors.append(QColor(QRgb(0xff707070))); | ||||
setBackground(QColor(QRgb(0xffffffff)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation); | ||||
break; | ||||
|
r75 | case QChart::ChartThemeVanilla: | ||
m_themeColors.append(QColor(217, 197, 116)); | ||||
m_themeColors.append(QColor(214, 168, 150)); | ||||
m_themeColors.append(QColor(160, 160, 113)); | ||||
m_themeColors.append(QColor(210, 210, 52)); | ||||
m_themeColors.append(QColor(136, 114, 58)); | ||||
|
r77 | |||
|
r91 | setBackground(QColor(QRgb(0xff9d844d)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation); | ||
|
r64 | break; | ||
|
r75 | case QChart::ChartThemeIcy: | ||
m_themeColors.append(QColor(0, 3, 165)); | ||||
m_themeColors.append(QColor(49, 52, 123)); | ||||
m_themeColors.append(QColor(71, 114, 187)); | ||||
m_themeColors.append(QColor(48, 97, 87)); | ||||
m_themeColors.append(QColor(19, 71, 90)); | ||||
m_themeColors.append(QColor(110, 70, 228)); | ||||
|
r77 | |||
|
r91 | setBackground(QColor(QRgb(0xffe4ffff)), QColor(QRgb(0xffe4ffff)), VerticalGradientOrientation); | ||
|
r64 | break; | ||
|
r75 | case QChart::ChartThemeGrayscale: | ||
m_themeColors.append(QColor(0, 0, 0)); | ||||
m_themeColors.append(QColor(50, 50, 50)); | ||||
m_themeColors.append(QColor(100, 100, 100)); | ||||
m_themeColors.append(QColor(140, 140, 140)); | ||||
m_themeColors.append(QColor(180, 180, 180)); | ||||
|
r77 | |||
|
r91 | setBackground(QColor(QRgb(0xffffffff)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation); | ||
|
r77 | break; | ||
case QChart::ChartThemeUnnamed1: | ||||
m_themeColors.append(QColor(QRgb(0xff3fa9f5))); | ||||
m_themeColors.append(QColor(QRgb(0xff7AC943))); | ||||
m_themeColors.append(QColor(QRgb(0xffFF931E))); | ||||
m_themeColors.append(QColor(QRgb(0xffFF1D25))); | ||||
m_themeColors.append(QColor(QRgb(0xffFF7BAC))); | ||||
|
r91 | setBackground(QColor(QRgb(0xfff3dc9e)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation); | ||
|
r64 | break; | ||
default: | ||||
Q_ASSERT(false); | ||||
break; | ||||
} | ||||
|
r48 | |||
|
r87 | if(m_backgroundItem){ | ||
m_backgroundItem->setBrush(m_backgroundGradient); | ||||
m_backgroundItem->setPen(Qt::NoPen); | ||||
|
r86 | } | ||
|
r77 | |||
|
r75 | foreach(QChartSeries* series, m_chartSeries) { | ||
// TODO: other series interested on themes? | ||||
if (series->type() == QChartSeries::SeriesTypeLine) { | ||||
QXYChartSeries *lineseries = reinterpret_cast<QXYChartSeries *>(series); | ||||
|
r85 | lineseries->setPen(nextColor()); | ||
|
r75 | } else if (series->type() == QChartSeries::SeriesTypeScatter) { | ||
QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series); | ||||
scatter->setMarkerColor(nextColor()); | ||||
} else if (series->type() == QChartSeries::SeriesTypePie) { | ||||
QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series); | ||||
for (int i(0); i < pieSeries->sliceCount(); i++) | ||||
pieSeries->setSliceColor(i, nextColor()); | ||||
} | ||||
} | ||||
update(); | ||||
} | ||||
QColor QChart::nextColor() | ||||
{ | ||||
QColor nextColor = m_themeColors.first(); | ||||
m_themeColors.move(0, m_themeColors.size() - 1); | ||||
return nextColor; | ||||
|
r64 | } | ||
|
r67 | void QChart::zoomInToRect(const QRect& rectangle) | ||
{ | ||||
if(!rectangle.isValid()) return; | ||||
qreal margin = this->margin(); | ||||
QRect rect = rectangle.normalized(); | ||||
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++; | ||
foreach (ChartItem* item ,m_chartItems) | ||||
item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); | ||||
update(); | ||||
} | ||||
void QChart::zoomIn() | ||||
{ | ||||
if (m_plotDataIndex < m_plotDomainList.count() - 1) { | ||||
m_plotDataIndex++; | ||||
foreach (ChartItem* item ,m_chartItems) | ||||
item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); | ||||
update(); | ||||
}else{ | ||||
QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); | ||||
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--; | ||||
foreach (ChartItem* item ,m_chartItems) | ||||
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()); | ||||
} | ||||
|
r64 | #include "moc_qchart.cpp" | ||
|
r48 | |||
|
r30 | QTCOMMERCIALCHART_END_NAMESPACE | ||