qchart.cpp
345 lines
| 10.5 KiB
| text/x-c
|
CppLexer
/ src / qchart.cpp
Michal Klocek
|
r12 | #include "qchart.h" | ||
Michal Klocek
|
r21 | #include "qchartseries.h" | ||
Tero Ahola
|
r42 | #include "qscatterseries.h" | ||
#include "qscatterseries_p.h" | ||||
Tero Ahola
|
r51 | #include "qpieseries.h" | ||
Michal Klocek
|
r45 | #include "qxychartseries.h" | ||
sauimone
|
r56 | |||
#include "barchartseries.h" | ||||
#include "bargroup.h" | ||||
Michal Klocek
|
r21 | #include "xylinechartitem_p.h" | ||
Michal Klocek
|
r67 | #include "plotdomain_p.h" | ||
#include "axisitem_p.h" | ||||
Tero Ahola
|
r42 | #include <QGraphicsScene> | ||
Michal Klocek
|
r21 | #include <QDebug> | ||
Michal Klocek
|
r12 | |||
Tero Ahola
|
r30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
Michal Klocek
|
r12 | |||
Tero Ahola
|
r48 | QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent), | ||
Michal Klocek
|
r69 | m_background(new QGraphicsRectItem(this)), | ||
m_title(new QGraphicsTextItem(this)), | ||||
Michal Klocek
|
r67 | m_axisX(new AxisItem(AxisItem::X_AXIS,this)), | ||
m_axisY(new AxisItem(AxisItem::Y_AXIS,this)), | ||||
Tero Ahola
|
r64 | m_plotDataIndex(0), | ||
m_marginSize(0) | ||||
Michal Klocek
|
r12 | { | ||
Tero Ahola
|
r64 | // TODO: the default theme? | ||
setTheme(QChart::ChartThemeVanilla); | ||||
Michal Klocek
|
r28 | // setFlags(QGraphicsItem::ItemClipsChildrenToShape); | ||
Michal Klocek
|
r67 | PlotDomain domain; | ||
m_plotDomainList<<domain; | ||||
m_chartItems<<m_axisX; | ||||
m_chartItems<<m_axisY; | ||||
Michal Klocek
|
r12 | } | ||
QChart::~QChart(){} | ||||
Michal Klocek
|
r21 | QRectF QChart::boundingRect() const | ||
{ | ||||
Michal Klocek
|
r53 | return m_rect; | ||
Michal Klocek
|
r21 | } | ||
Michal Klocek
|
r12 | |||
Michal Klocek
|
r21 | void QChart::addSeries(QChartSeries* series) | ||
Michal Klocek
|
r12 | { | ||
Tero Ahola
|
r48 | // TODO: we should check the series not already added | ||
Michal Klocek
|
r45 | |||
Tero Ahola
|
r75 | m_chartSeries << series; | ||
Michal Klocek
|
r21 | |||
switch(series->type()) | ||||
{ | ||||
Tero Ahola
|
r30 | case QChartSeries::SeriesTypeLine: { | ||
Michal Klocek
|
r47 | |||
QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series); | ||||
Tero Ahola
|
r65 | // Use color defined by theme in case the series does not define a custom color | ||
if (!xyseries->color().isValid() && m_themeColors.count()) | ||||
Tero Ahola
|
r75 | xyseries->setColor(nextColor()); | ||
Michal Klocek
|
r47 | |||
Michal Klocek
|
r67 | m_plotDataIndex = 0 ; | ||
m_plotDomainList.resize(1); | ||||
PlotDomain& domain = m_plotDomainList[m_plotDataIndex]; | ||||
Michal Klocek
|
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); | ||||
Michal Klocek
|
r67 | m_chartItems<<item; | ||
foreach(ChartItem* i ,m_chartItems) | ||||
i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex)); | ||||
Michal Klocek
|
r21 | break; | ||
Michal Klocek
|
r12 | } | ||
sauimone
|
r56 | case QChartSeries::SeriesTypeBar: { | ||
qDebug() << "barSeries added"; | ||||
BarChartSeries* barSeries = static_cast<BarChartSeries*>(series); | ||||
sauimone
|
r78 | BarGroup* barGroup = new BarGroup(*barSeries,this); | ||
sauimone
|
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)); | ||||
sauimone
|
r78 | m_chartItems<<barGroup; | ||
childItems().append(barGroup); | ||||
sauimone
|
r56 | break; | ||
} | ||||
Tero Ahola
|
r42 | case QChartSeries::SeriesTypeScatter: { | ||
Tero Ahola
|
r65 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); | ||
Tero Ahola
|
r75 | scatterSeries->d->setParentItem(this); | ||
// Set pre-defined colors in case the series has no colors defined | ||||
if (!scatterSeries->markerColor().isValid()) | ||||
scatterSeries->setMarkerColor(nextColor()); | ||||
Tero Ahola
|
r54 | connect(this, SIGNAL(sizeChanged(QRectF)), | ||
scatterSeries, SLOT(chartSizeChanged(QRectF))); | ||||
Tero Ahola
|
r75 | // QColor nextColor = m_themeColors.takeFirst(); | ||
// nextColor.setAlpha(150); // TODO: default opacity? | ||||
// scatterSeries->setMarkerColor(nextColor); | ||||
break; | ||||
Tero Ahola
|
r65 | } | ||
Tero Ahola
|
r51 | case QChartSeries::SeriesTypePie: { | ||
Tero Ahola
|
r65 | QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series); | ||
Tero Ahola
|
r75 | for (int i(0); i < pieSeries->sliceCount(); i++) { | ||
if (!pieSeries->sliceColor(i).isValid()) | ||||
pieSeries->setSliceColor(i, nextColor()); | ||||
} | ||||
Tero Ahola
|
r54 | connect(this, SIGNAL(sizeChanged(QRectF)), | ||
pieSeries, SLOT(chartSizeChanged(QRectF))); | ||||
Tero Ahola
|
r75 | |||
// Set pre-defined colors in case the series has no colors defined | ||||
Tero Ahola
|
r64 | // TODO: how to define the color for all the slices of a pie? | ||
Tero Ahola
|
r75 | // for (int (i); i < pieSeries.sliceCount(); i++) | ||
break; | ||||
Tero Ahola
|
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; | ||||
Tero Ahola
|
r42 | } | ||
default: | ||||
Tero Ahola
|
r61 | Q_ASSERT(false); | ||
Tero Ahola
|
r42 | break; | ||
} | ||||
Tero Ahola
|
r65 | addSeries(series); | ||
return series; | ||||
Tero Ahola
|
r42 | } | ||
Tero Ahola
|
r48 | |||
Michal Klocek
|
r67 | void QChart::setSize(const QSize& size) | ||
Michal Klocek
|
r28 | { | ||
Michal Klocek
|
r67 | m_rect = QRect(QPoint(0,0),size); | ||
QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); | ||||
Michal Klocek
|
r69 | |||
//recalculate background gradient | ||||
m_background->setRect(rect); | ||||
m_backgroundGradient.setFinalStop(0,m_background->rect().height()); | ||||
m_background->setBrush(m_backgroundGradient); | ||||
//resize elements | ||||
Michal Klocek
|
r67 | foreach (ChartItem* item ,m_chartItems) { | ||
item->setPos(rect.topLeft()); | ||||
item->setSize(rect.size()); | ||||
Michal Klocek
|
r47 | |||
Michal Klocek
|
r67 | } | ||
Tero Ahola
|
r54 | // TODO: TTD for setting scale | ||
//emit scaleChanged(100, 100); | ||||
Tero Ahola
|
r48 | // TODO: calculate the origo | ||
// TODO: not sure if emitting a signal here is the best from performance point of view | ||||
Tero Ahola
|
r54 | emit sizeChanged(QRectF(0, 0, size.width(), size.height())); | ||
Tero Ahola
|
r48 | |||
Michal Klocek
|
r21 | update(); | ||
Michal Klocek
|
r12 | } | ||
Michal Klocek
|
r69 | void QChart::setBackgroundColor(const QColor& color) | ||
{ | ||||
m_backgroundGradient.setColorAt( 0.0, Qt::white); | ||||
m_backgroundGradient.setColorAt( 1.0, color); | ||||
m_background->setBrush(m_backgroundGradient); | ||||
m_background->setPen(Qt::NoPen); | ||||
m_background->update(); | ||||
} | ||||
void QChart::setTitle(const QString& title) | ||||
{ | ||||
m_title->setPlainText(title); | ||||
} | ||||
Michal Klocek
|
r28 | int QChart::margin() const | ||
{ | ||||
Michal Klocek
|
r53 | return m_marginSize; | ||
Michal Klocek
|
r28 | } | ||
Michal Klocek
|
r12 | void QChart::setMargin(int margin) | ||
{ | ||||
Michal Klocek
|
r53 | m_marginSize = margin; | ||
Michal Klocek
|
r12 | } | ||
Tero Ahola
|
r75 | void QChart::setTheme(QChart::ChartThemeId theme) | ||
Tero Ahola
|
r64 | { | ||
Tero Ahola
|
r75 | // if (theme != m_currentTheme) { | ||
m_themeColors.clear(); | ||||
Tero Ahola
|
r64 | // TODO: define color themes | ||
switch (theme) { | ||||
Tero Ahola
|
r81 | case QChart::ChartThemeDefault: | ||
// TODO: define the default theme based on the OS | ||||
// For now we just fallthrough to "vanilla" | ||||
Tero Ahola
|
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)); | ||||
Tero Ahola
|
r77 | |||
m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xff9d844d))); | ||||
m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf))); | ||||
Tero Ahola
|
r64 | break; | ||
Tero Ahola
|
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)); | ||||
Tero Ahola
|
r77 | |||
m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xffe4ffff))); | ||||
Tero Ahola
|
r81 | m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffe4ffff))); | ||
Tero Ahola
|
r64 | break; | ||
Tero Ahola
|
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)); | ||||
Tero Ahola
|
r77 | |||
m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xffffffff))); | ||||
m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf))); | ||||
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))); | ||||
m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xfff3dc9e))); | ||||
m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf))); | ||||
Tero Ahola
|
r64 | break; | ||
default: | ||||
Q_ASSERT(false); | ||||
break; | ||||
} | ||||
Tero Ahola
|
r48 | |||
Tero Ahola
|
r77 | m_background->setBrush(m_backgroundGradient); | ||
m_background->setPen(Qt::NoPen); | ||||
Tero Ahola
|
r75 | foreach(QChartSeries* series, m_chartSeries) { | ||
// TODO: other series interested on themes? | ||||
if (series->type() == QChartSeries::SeriesTypeLine) { | ||||
QXYChartSeries *lineseries = reinterpret_cast<QXYChartSeries *>(series); | ||||
lineseries->setColor(nextColor()); | ||||
} 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; | ||||
Tero Ahola
|
r64 | } | ||
Michal Klocek
|
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]; | ||||
Michal Klocek
|
r76 | PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin); | ||
Michal Klocek
|
r67 | |||
m_plotDomainList.resize(m_plotDataIndex + 1); | ||||
Michal Klocek
|
r76 | m_plotDomainList<<domain; | ||
Michal Klocek
|
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(); | ||||
} | ||||
} | ||||
Tero Ahola
|
r64 | #include "moc_qchart.cpp" | ||
Tero Ahola
|
r48 | |||
Tero Ahola
|
r30 | QTCOMMERCIALCHART_END_NAMESPACE | ||