##// END OF EJS Templates
Added bar chart example
Added bar chart example

File last commit:

r78:681c12da395a
r78:681c12da395a
Show More
qchart.cpp
285 lines | 8.1 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"
sauimone
Integrating bar chart. Cleaned up old implementation. TODO: show this in test application. how?
r56
#include "barchartseries.h"
#include "bargroup.h"
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
Add background to chart...
r69 m_background(new QGraphicsRectItem(this)),
m_title(new QGraphicsTextItem(this)),
Michal Klocek
Add zoom support...
r67 m_axisX(new AxisItem(AxisItem::X_AXIS,this)),
m_axisY(new AxisItem(AxisItem::Y_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?
setTheme(QChart::ChartThemeVanilla);
Michal Klocek
Adds pimpl to qchart class
r28 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
Michal Klocek
Add zoom support...
r67 PlotDomain domain;
m_plotDomainList<<domain;
m_chartItems<<m_axisX;
m_chartItems<<m_axisY;
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
Michal Klocek
Removes PIMPL for now...
r53 m_series<<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
if (!xyseries->color().isValid() && m_themeColors.count())
xyseries->setColor(m_themeColors.takeFirst());
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);
m_chartItems<<barGroup;
childItems().append(barGroup);
sauimone
Integrating bar chart. Cleaned up old implementation. TODO: show this in test application. how?
r56 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
Removed scale from chart's sizeChanged signals
r54 connect(this, SIGNAL(sizeChanged(QRectF)),
scatterSeries, SLOT(chartSizeChanged(QRectF)));
Tero Ahola
Resizing of QGraphicItems now possible by resize signal from QChart
r48 scatterSeries->d->setParentItem(this);
Tero Ahola
Draft implementation for setting color themes for a chart
r64 QColor nextColor = m_themeColors.takeFirst();
nextColor.setAlpha(150); // TODO: default opacity?
scatterSeries->setMarkerColor(nextColor);
Tero Ahola
Fixed line series usage in the test app
r65 }
Tero Ahola
Integrated draft version of pie series
r51 case QChartSeries::SeriesTypePie: {
// TODO: we now have also a list of y values as a parameter, it is ignored
// we should use a generic data class instead of list of x and y values
Tero Ahola
Fixed line series usage in the test app
r65 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
Tero Ahola
Removed scale from chart's sizeChanged signals
r54 connect(this, SIGNAL(sizeChanged(QRectF)),
pieSeries, SLOT(chartSizeChanged(QRectF)));
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
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;
}
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 background to chart...
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
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 background to chart...
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
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
Draft implementation for setting color themes for a chart
r64 void QChart::setTheme(QChart::ChartTheme theme)
{
// TODO: define color themes
switch (theme) {
case ChartThemeVanilla:
m_themeColors.append(QColor(255, 238, 174));
m_themeColors.append(QColor(228, 228, 160));
m_themeColors.append(QColor(228, 179, 160));
m_themeColors.append(QColor(180, 151, 18));
m_themeColors.append(QColor(252, 252, 37));
break;
case ChartThemeIcy:
m_themeColors.append(QColor(255, 238, 174));
m_themeColors.append(QColor(228, 228, 160));
m_themeColors.append(QColor(228, 179, 160));
m_themeColors.append(QColor(180, 151, 18));
m_themeColors.append(QColor(252, 252, 37));
break;
case ChartThemeGrayscale:
m_themeColors.append(QColor(255, 238, 174));
m_themeColors.append(QColor(228, 228, 160));
m_themeColors.append(QColor(228, 179, 160));
m_themeColors.append(QColor(180, 151, 18));
m_themeColors.append(QColor(252, 252, 37));
break;
default:
Q_ASSERT(false);
break;
}
Tero Ahola
Resizing of QGraphicItems now possible by resize signal from QChart
r48
Tero Ahola
Draft implementation for setting color themes for a chart
r64 // TODO: update coloring of different elements to match the selected theme
}
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];
PlotDomain newDomain;
qreal dx = oldDomain.spanX() / (m_rect.width() - 2 * margin);
qreal dy = oldDomain.spanY() / (m_rect.height() - 2 * margin);
newDomain.m_minX = oldDomain.m_minX + dx * rect.left();
newDomain.m_maxX = oldDomain.m_minX + dx * rect.right();
newDomain.m_minY = oldDomain.m_maxY - dy * rect.bottom();
newDomain.m_maxY = oldDomain.m_maxY - dy * rect.top();
m_plotDomainList.resize(m_plotDataIndex + 1);
m_plotDomainList<<newDomain;
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
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