#include "qbarchart.h" #include "qbarchartbar.h" #include QBarChart::QBarChart(QGraphicsItem *parent) : QGraphicsItem(parent) ,mBarDefaultWidth( 1 ) { } void QBarChart::addSeries(QList data) { mData.clear(); mData.append(data); dataChanged(); } void QBarChart::setSize( int h, int w ) { mHeight = h; mWidth = w; dataChanged(); // TODO: separate data from grid } void QBarChart::setBarWidth( int w ) { mBarDefaultWidth = w; } void QBarChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { qDebug() << "QBarChart::paint"; } QRectF QBarChart::boundingRect() const { return QRectF(0,0,mWidth,mHeight); // TODO: size from layout } void QBarChart::dataChanged() { int count = mData.count(); if (count <= 0) { // No items return; } mMax = mData.at(0); mMin = mData.at(0); // Find out min and max values. for (int i=0; i mMax) { mMax = mData.at(i); } else if (mData.at(i) < mMin) { mMin = mData.at(i); } } // Create items for data, delete old ones foreach (QGraphicsItem* i, childItems()) { delete i; } gridChanged(); int posStep = (mWidth / count) / 2; // TODO: Why I need magical 2 here to get correct step? for (int i=0; isetSize(barHeight, mBarDefaultWidth * 3 * i); // TODO: widht settable by style or something. bar->setColor(QColor(255 - i*25, i*10, i*15)); bar->setPos(i*posStep, 0); childItems().append(bar); } } void QBarChart::gridChanged() { // Scale of grid has changed QBarChartGrid *grid = new QBarChartGrid(this); grid->setSize(mHeight, mWidth); grid->setLimits(mMin,mMax); grid->setHorizontalLineCount(5); childItems().append(grid); }