##// END OF EJS Templates
Some fixes to bar charts. Now bars draw almost correctly
Some fixes to bar charts. Now bars draw almost correctly

File last commit:

r95:9f20814aa978
r97:f3d03f051601
Show More
qchart.cpp
427 lines | 13.0 KiB | text/x-c | CppLexer
Michal Klocek
adds missing files form previous commit
r12 #include "qchart.h"
Michal Klocek
Refactor current draft to fit int current design specs...
r21 #include "qchartseries.h"
Tero Ahola
Integrated scatter type series...
r42 #include "qscatterseries.h"
#include "qscatterseries_p.h"
Tero Ahola
Integrated draft version of pie series
r51 #include "qpieseries.h"
Michal Klocek
Changes reinterpret_cast to static_cast
r45 #include "qxychartseries.h"
Michal Klocek
Refactora axis and line chart to use graphics items insted of painter.
r85 #include "qchartaxis.h"
sauimone
Integrating bar chart. Cleaned up old implementation. TODO: show this in test application. how?
r56 #include "barchartseries.h"
#include "bargroup.h"
sauimone
added example for stacked bar chart.
r95 #include "stackedbarchartseries.h"
#include "stackedbargroup.h"
sauimone
Integrating bar chart. Cleaned up old implementation. TODO: show this in test application. how?
r56
Michal Klocek
Refactor current draft to fit int current design specs...
r21 #include "xylinechartitem_p.h"
Michal Klocek
Add zoom support...
r67 #include "plotdomain_p.h"
#include "axisitem_p.h"
Tero Ahola
Integrated scatter type series...
r42 #include <QGraphicsScene>
Michal Klocek
Refactor current draft to fit int current design specs...
r21 #include <QDebug>
Michal Klocek
adds missing files form previous commit
r12
Tero Ahola
Renamed to QtCommercialChart
r30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
Michal Klocek
adds missing files form previous commit
r12
Tero Ahola
Resizing of QGraphicItems now possible by resize signal from QChart
r48 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
Michal Klocek
Adds title support
r87 m_backgroundItem(0),
m_titleItem(0),
m_axisXItem(new AxisItem(AxisItem::X_AXIS,this)),
Tero Ahola
Draft implementation for setting color themes for a chart
r64 m_plotDataIndex(0),
m_marginSize(0)
Michal Klocek
adds missing files form previous commit
r12 {
Tero Ahola
Draft implementation for setting color themes for a chart
r64 // TODO: the default theme?
Tero Ahola
Enabled color themes again
r91 setTheme(QChart::ChartThemeDefault);
Michal Klocek
Refactora axis and line chart to use graphics items insted of painter.
r85
Michal Klocek
Add zoom support...
r67 PlotDomain domain;
m_plotDomainList<<domain;
Michal Klocek
Adds title support
r87 m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this);
m_chartItems<<m_axisXItem;
m_chartItems<<m_axisYItem.at(0);
Michal Klocek
adds missing files form previous commit
r12 }
QChart::~QChart(){}
Michal Klocek
Refactor current draft to fit int current design specs...
r21 QRectF QChart::boundingRect() const
{
Michal Klocek
Removes PIMPL for now...
r53 return m_rect;
Michal Klocek
Refactor current draft to fit int current design specs...
r21 }
Michal Klocek
adds missing files form previous commit
r12
Michal Klocek
Refactor current draft to fit int current design specs...
r21 void QChart::addSeries(QChartSeries* series)
Michal Klocek
adds missing files form previous commit
r12 {
Tero Ahola
Resizing of QGraphicItems now possible by resize signal from QChart
r48 // TODO: we should check the series not already added
Michal Klocek
Changes reinterpret_cast to static_cast
r45
Tero Ahola
Color themes now enabled for scatter, pie and line series.
r75 m_chartSeries << series;
Michal Klocek
Refactor current draft to fit int current design specs...
r21
switch(series->type())
{
Tero Ahola
Renamed to QtCommercialChart
r30 case QChartSeries::SeriesTypeLine: {
Michal Klocek
Removes hardcoded values for xydomain
r47
QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
Tero Ahola
Fixed line series usage in the test app
r65 // Use color defined by theme in case the series does not define a custom color
Michal Klocek
Refactora axis and line chart to use graphics items insted of painter.
r85
if (!xyseries->pen().color().isValid() && m_themeColors.count()) //TODO: wtf
xyseries->setPen(nextColor());
Michal Klocek
Removes hardcoded values for xydomain
r47
Michal Klocek
Add zoom support...
r67 m_plotDataIndex = 0 ;
m_plotDomainList.resize(1);
PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
Michal Klocek
Removes hardcoded values for xydomain
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
Add zoom support...
r67 m_chartItems<<item;
foreach(ChartItem* i ,m_chartItems)
i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
Michal Klocek
Refactor current draft to fit int current design specs...
r21 break;
Michal Klocek
adds missing files form previous commit
r12 }
sauimone
Integrating bar chart. Cleaned up old implementation. TODO: show this in test application. how?
r56 case QChartSeries::SeriesTypeBar: {
qDebug() << "barSeries added";
BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
sauimone
Added bar chart example
r78 BarGroup* barGroup = new BarGroup(*barSeries,this);
sauimone
correct drawing for barchart
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
Added bar chart example
r78 m_chartItems<<barGroup;
childItems().append(barGroup);
sauimone
Integrating bar chart. Cleaned up old implementation. TODO: show this in test application. how?
r56 break;
}
sauimone
added example for stacked bar chart.
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));
m_chartItems<<stackedBarGroup;
childItems().append(stackedBarGroup);
break;
}
Tero Ahola
Integrated scatter type series...
r42 case QChartSeries::SeriesTypeScatter: {
Tero Ahola
Fixed line series usage in the test app
r65 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
Tero Ahola
Color themes now enabled for scatter, pie and line 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());
Tero Ahola
Removed scale from chart's sizeChanged signals
r54 connect(this, SIGNAL(sizeChanged(QRectF)),
scatterSeries, SLOT(chartSizeChanged(QRectF)));
Tero Ahola
Color themes now enabled for scatter, pie and line series.
r75 // QColor nextColor = m_themeColors.takeFirst();
// nextColor.setAlpha(150); // TODO: default opacity?
// scatterSeries->setMarkerColor(nextColor);
break;
Tero Ahola
Fixed line series usage in the test app
r65 }
Tero Ahola
Integrated draft version of pie series
r51 case QChartSeries::SeriesTypePie: {
Tero Ahola
Fixed line series usage in the test app
r65 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
Tero Ahola
Color themes now enabled for scatter, pie and line series.
r75 for (int i(0); i < pieSeries->sliceCount(); i++) {
if (!pieSeries->sliceColor(i).isValid())
pieSeries->setSliceColor(i, nextColor());
}
Tero Ahola
Removed scale from chart's sizeChanged signals
r54 connect(this, SIGNAL(sizeChanged(QRectF)),
pieSeries, SLOT(chartSizeChanged(QRectF)));
Tero Ahola
Color themes now enabled for scatter, pie and line series.
r75
// Set pre-defined colors in case the series has no colors defined
Tero Ahola
Draft implementation for setting color themes for a chart
r64 // TODO: how to define the color for all the slices of a pie?
Tero Ahola
Color themes now enabled for scatter, pie and line series.
r75 // for (int (i); i < pieSeries.sliceCount(); i++)
break;
Tero Ahola
Fixed line series usage in the test app
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;
}
sauimone
added example for stacked bar chart.
r95 case QChartSeries::SeriesTypeStackedBar: {
series = new StackedBarChartSeries(this);
break;
}
Tero Ahola
Fixed line series usage in the test app
r65 case QChartSeries::SeriesTypeScatter: {
series = new QScatterSeries(this);
break;
}
case QChartSeries::SeriesTypePie: {
series = new QPieSeries(this);
break;
Tero Ahola
Integrated scatter type series...
r42 }
default:
Tero Ahola
Refactored series creation with QChart
r61 Q_ASSERT(false);
Tero Ahola
Integrated scatter type series...
r42 break;
}
Tero Ahola
Fixed line series usage in the test app
r65 addSeries(series);
return series;
Tero Ahola
Integrated scatter type series...
r42 }
Tero Ahola
Resizing of QGraphicItems now possible by resize signal from QChart
r48
Michal Klocek
Add zoom support...
r67 void QChart::setSize(const QSize& size)
Michal Klocek
Adds pimpl to qchart class
r28 {
Michal Klocek
Add zoom support...
r67 m_rect = QRect(QPoint(0,0),size);
QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
Michal Klocek
Add gradient bacground support...
r86 //recaculate title
Michal Klocek
Adds title support
r87 if(m_titleItem){
QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
Michal Klocek
Add gradient bacground support...
r86
}
Michal Klocek
Add background to chart...
r69
//recalculate background gradient
Michal Klocek
Adds title support
r87 if(m_backgroundItem){
m_backgroundItem->setRect(rect);
Michal Klocek
Add gradient bacground support...
r86 if(m_bacgroundOrinetation==HorizonatlGradientOrientation)
Michal Klocek
Adds title support
r87 m_backgroundGradient.setFinalStop(m_backgroundItem->rect().width(),0);
Michal Klocek
Add gradient bacground support...
r86 else
Michal Klocek
Adds title support
r87 m_backgroundGradient.setFinalStop(0,m_backgroundItem->rect().height());
Michal Klocek
Add gradient bacground support...
r86
Michal Klocek
Adds title support
r87 m_backgroundItem->setBrush(m_backgroundGradient);
Michal Klocek
Add gradient bacground support...
r86 }
Michal Klocek
Add background to chart...
r69
//resize elements
Michal Klocek
Add zoom support...
r67 foreach (ChartItem* item ,m_chartItems) {
item->setPos(rect.topLeft());
item->setSize(rect.size());
Michal Klocek
Removes hardcoded values for xydomain
r47
Michal Klocek
Add zoom support...
r67 }
Tero Ahola
Removed scale from chart's sizeChanged signals
r54 // TODO: TTD for setting scale
//emit scaleChanged(100, 100);
Tero Ahola
Resizing of QGraphicItems now possible by resize signal from QChart
r48 // TODO: calculate the origo
// TODO: not sure if emitting a signal here is the best from performance point of view
Tero Ahola
Removed scale from chart's sizeChanged signals
r54 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
Tero Ahola
Resizing of QGraphicItems now possible by resize signal from QChart
r48
Michal Klocek
Refactor current draft to fit int current design specs...
r21 update();
Michal Klocek
adds missing files form previous commit
r12 }
Michal Klocek
Add gradient bacground support...
r86 void QChart::setBackground(const QColor& startColor, const QColor& endColor, GradientOrientation orientation)
Michal Klocek
Add background to chart...
r69 {
Michal Klocek
Add gradient bacground support...
r86
Michal Klocek
Adds title support
r87 if(!m_backgroundItem){
m_backgroundItem = new QGraphicsRectItem(this);
m_backgroundItem->setZValue(-1);
Michal Klocek
Add gradient bacground support...
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);
}
Michal Klocek
Adds title support
r87 m_backgroundItem->setBrush(m_backgroundGradient);
m_backgroundItem->setPen(Qt::NoPen);
m_backgroundItem->update();
Michal Klocek
Add background to chart...
r69 }
Michal Klocek
Adds title support
r87 void QChart::setTitle(const QString& title,const QFont& font)
Michal Klocek
Add background to chart...
r69 {
Michal Klocek
Adds title support
r87 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
m_titleItem->setPlainText(title);
m_titleItem->setFont(font);
Michal Klocek
Add background to chart...
r69 }
Michal Klocek
Adds pimpl to qchart class
r28 int QChart::margin() const
{
Michal Klocek
Removes PIMPL for now...
r53 return m_marginSize;
Michal Klocek
Adds pimpl to qchart class
r28 }
Michal Klocek
adds missing files form previous commit
r12 void QChart::setMargin(int margin)
{
Michal Klocek
Removes PIMPL for now...
r53 m_marginSize = margin;
Michal Klocek
adds missing files form previous commit
r12 }
Tero Ahola
Color themes now enabled for scatter, pie and line series.
r75 void QChart::setTheme(QChart::ChartThemeId theme)
Tero Ahola
Draft implementation for setting color themes for a chart
r64 {
Tero Ahola
Color themes now enabled for scatter, pie and line series.
r75 // if (theme != m_currentTheme) {
m_themeColors.clear();
Tero Ahola
Draft implementation for setting color themes for a chart
r64 // TODO: define color themes
switch (theme) {
Tero Ahola
Added theme named 'default'
r81 case QChart::ChartThemeDefault:
// TODO: define the default theme based on the OS
Tero Ahola
Enabled color themes again
r91 m_themeColors.append(QColor(QRgb(0xff000000)));
m_themeColors.append(QColor(QRgb(0xff707070)));
setBackground(QColor(QRgb(0xffffffff)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
break;
Tero Ahola
Color themes now enabled for scatter, pie and line series.
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
Theme now affects background, enabled zoom by default in QChartWidget
r77
Tero Ahola
Enabled color themes again
r91 setBackground(QColor(QRgb(0xff9d844d)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
Tero Ahola
Draft implementation for setting color themes for a chart
r64 break;
Tero Ahola
Color themes now enabled for scatter, pie and line series.
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
Theme now affects background, enabled zoom by default in QChartWidget
r77
Tero Ahola
Enabled color themes again
r91 setBackground(QColor(QRgb(0xffe4ffff)), QColor(QRgb(0xffe4ffff)), VerticalGradientOrientation);
Tero Ahola
Draft implementation for setting color themes for a chart
r64 break;
Tero Ahola
Color themes now enabled for scatter, pie and line series.
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
Theme now affects background, enabled zoom by default in QChartWidget
r77
Tero Ahola
Enabled color themes again
r91 setBackground(QColor(QRgb(0xffffffff)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
Tero Ahola
Theme now affects background, enabled zoom by default in QChartWidget
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)));
Tero Ahola
Enabled color themes again
r91 setBackground(QColor(QRgb(0xfff3dc9e)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
Tero Ahola
Draft implementation for setting color themes for a chart
r64 break;
default:
Q_ASSERT(false);
break;
}
Tero Ahola
Resizing of QGraphicItems now possible by resize signal from QChart
r48
Michal Klocek
Adds title support
r87 if(m_backgroundItem){
m_backgroundItem->setBrush(m_backgroundGradient);
m_backgroundItem->setPen(Qt::NoPen);
Michal Klocek
Add gradient bacground support...
r86 }
Tero Ahola
Theme now affects background, enabled zoom by default in QChartWidget
r77
Tero Ahola
Color themes now enabled for scatter, pie and line series.
r75 foreach(QChartSeries* series, m_chartSeries) {
// TODO: other series interested on themes?
if (series->type() == QChartSeries::SeriesTypeLine) {
QXYChartSeries *lineseries = reinterpret_cast<QXYChartSeries *>(series);
Michal Klocek
Refactora axis and line chart to use graphics items insted of painter.
r85 lineseries->setPen(nextColor());
Tero Ahola
Color themes now enabled for scatter, pie and line series.
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;
Tero Ahola
Draft implementation for setting color themes for a chart
r64 }
Michal Klocek
Add zoom support...
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
Small refactor, adds subDomain to plotDomain
r76 PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin);
Michal Klocek
Add zoom support...
r67
m_plotDomainList.resize(m_plotDataIndex + 1);
Michal Klocek
Small refactor, adds subDomain to plotDomain
r76 m_plotDomainList<<domain;
Michal Klocek
Add zoom support...
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();
Tero Ahola
QChartWidget now zooms only x axis and zoom is reset with right click
r93 } else {
Michal Klocek
Add zoom support...
r67 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
QChartWidget now zooms only x axis and zoom is reset with right click
r93 void QChart::zoomReset()
{
if (m_plotDataIndex > 0) {
m_plotDataIndex = 0;
foreach (ChartItem* item ,m_chartItems)
item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
update();
}
}
Michal Klocek
Refactora axis and line chart to use graphics items insted of painter.
r85 void QChart::setAxisX(const QChartAxis& axis)
{
Michal Klocek
Adds title support
r87 setAxis(m_axisXItem,axis);
Michal Klocek
Refactora axis and line chart to use graphics items insted of painter.
r85 }
void QChart::setAxisY(const QChartAxis& axis)
{
Michal Klocek
Adds title support
r87 setAxis(m_axisYItem.at(0),axis);
Michal Klocek
Refactora axis and line chart to use graphics items insted of painter.
r85 }
void QChart::setAxisY(const QList<QChartAxis>& axis)
{
//TODO not implemented
}
void QChart::setAxis(AxisItem *item, const QChartAxis& axis)
{
item->setVisible(axis.isAxisVisible());
}
Tero Ahola
Draft implementation for setting color themes for a chart
r64 #include "moc_qchart.cpp"
Tero Ahola
Resizing of QGraphicItems now possible by resize signal from QChart
r48
Tero Ahola
Renamed to QtCommercialChart
r30 QTCOMMERCIALCHART_END_NAMESPACE