##// END OF EJS Templates
Added bar chart to test widget
Added bar chart to test widget

File last commit:

r61:45d3de5e814a
r63:5031f95e0f47
Show More
qpieseries.cpp
97 lines | 2.4 KiB | text/x-c | CppLexer
Tero Ahola
Integrated draft version of pie series
r51 #include "qpieseries.h"
#include "pieslice.h"
#include <QGraphicsObject>
#include <QDebug>
QTCOMMERCIALCHART_BEGIN_NAMESPACE
Tero Ahola
Refactored series creation with QChart
r61 QPieSeries::QPieSeries(QGraphicsObject *parent) :
Tero Ahola
Integrated draft version of pie series
r51 QChartSeries(parent),
Tero Ahola
Added size factor property to pie
r60 m_sizeFactor(1.0)
Tero Ahola
Integrated draft version of pie series
r51 {
Tero Ahola
Refactored series creation with QChart
r61 }
QPieSeries::~QPieSeries()
{
while (m_slices.count())
delete m_slices.takeLast();
}
bool QPieSeries::setData(QList<qreal> data)
{
m_data = data;
Tero Ahola
Removed scale from chart's sizeChanged signals
r54 // Create slices
Tero Ahola
Integrated draft version of pie series
r51 qreal fullPie = 360;
qreal total = 0;
Tero Ahola
Refactored series creation with QChart
r61 foreach (qreal value, m_data)
Tero Ahola
Integrated draft version of pie series
r51 total += value;
Tero Ahola
Refactored series creation with QChart
r61 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent());
Tero Ahola
Integrated draft version of pie series
r51 Q_ASSERT(parentItem);
Tero Ahola
Added size factor property to pie
r60 m_chartSize = parentItem->boundingRect();
Tero Ahola
Integrated draft version of pie series
r51 qreal angle = 0;
Tero Ahola
Removed scale from chart's sizeChanged signals
r54 // TODO: no need to create new slices in case size changed; we should re-use the existing ones
Tero Ahola
Refactored series creation with QChart
r61 foreach (qreal value, m_data) {
Tero Ahola
Integrated draft version of pie series
r51 qreal span = value / total * fullPie;
Tero Ahola
Added size factor property to pie
r60 PieSlice *slice = new PieSlice(randomColor(), angle, span, parentItem->boundingRect());
Tero Ahola
Integrated draft version of pie series
r51 slice->setParentItem(parentItem);
m_slices.append(slice);
angle += span;
}
Tero Ahola
Added size factor property to pie
r60
resizeSlices(m_chartSize);
Tero Ahola
Refactored series creation with QChart
r61 return true;
Tero Ahola
Removed scale from chart's sizeChanged signals
r54 }
Tero Ahola
Added size factor property to pie
r60 void QPieSeries::chartSizeChanged(QRectF chartRect)
Tero Ahola
Removed scale from chart's sizeChanged signals
r54 {
Tero Ahola
Added size factor property to pie
r60 // TODO: allow user setting the size?
// TODO: allow user defining the margins?
m_chartSize = chartRect;
resizeSlices(m_chartSize);
}
void QPieSeries::resizeSlices(QRectF rect)
{
QRectF tempRect = rect;
if (tempRect.width() < tempRect.height()) {
tempRect.setWidth(tempRect.width() * m_sizeFactor);
tempRect.setHeight(tempRect.width());
tempRect.moveCenter(rect.center());
} else {
tempRect.setHeight(tempRect.height() * m_sizeFactor);
tempRect.setWidth(tempRect.height());
tempRect.moveCenter(rect.center());
}
foreach (PieSlice *slice, m_slices)
slice->m_rect = tempRect;
}
void QPieSeries::setSizeFactor(qreal factor)
{
if (factor > 0.0)
m_sizeFactor = factor;
resizeSlices(m_chartSize);
// Initiate update via the parent graphics item
// TODO: potential issue: what if this function is called from the parent context?
QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent());
Q_ASSERT(parentItem);
parentItem->update();
Tero Ahola
Removed scale from chart's sizeChanged signals
r54 }
Tero Ahola
Integrated draft version of pie series
r51 QColor QPieSeries::randomColor()
{
QColor c;
c.setRed(qrand() % 255);
c.setGreen(qrand() % 255);
c.setBlue(qrand() % 255);
return c;
}
#include "moc_qpieseries.cpp"
QTCOMMERCIALCHART_END_NAMESPACE