#include #include #include #include "barchartmodel_p.h" #include "qbarcategory.h" #include "qbarset.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE BarChartModel::BarChartModel(QBarCategory &category, QObject *parent) : QObject(parent) ,mRunningId(1) ,mCategory(category) { } BarChartModel::~BarChartModel() { // qDebug() << "BarChartModel::~BarChartModel"; foreach (DataContainer* c, mDataModel) { delete c; } } int BarChartModel::addData(QList data) { // qDebug() << "BarChartModel::addData" << data.count(); DataContainer* c = new DataContainer(data,mRunningId); mDataModel.append(c); mRunningId++; emit modelUpdated(); return mRunningId-1; } void BarChartModel::removeData(int id) { // qDebug() << "BarChartModel::removeData"; foreach(DataContainer* c, mDataModel) { if (c->mId == id) { mDataModel.removeOne(c); delete c; } } emit modelUpdated(); } void BarChartModel::addBarSet(QBarSet &set) { DataContainer* c = new DataContainer(set.mValues,mRunningId); mDataModel.append(c); mRunningId++; } void BarChartModel::removeBarSet(QBarSet &set) { // TODO: } int BarChartModel::countRows() { // qDebug() << "BarChartModel::countRows"; return mDataModel.count(); } int BarChartModel::countColumns() { // qDebug() << "BarChartModel::countColumns"; int count(0); for (int i=0; icountColumns(); if (temp > count) { count = temp; } } return count; } int BarChartModel::countTotalItems() { // qDebug() << "BarChartModel::countTotalItems"; int total = mDataModel.count() * countColumns(); return total; } qreal BarChartModel::min() { // qDebug() << "BarChartModel::min"; Q_ASSERT(mDataModel.count() > 0); // TODO: make min and max members and update them when data changes. // This is slower since they are checked every time, even if data is same since previous call. qreal min = INT_MAX; for (int i=0; i countColumns(); for (int j=0; jvalueAt(j); if (temp < min) { min = temp; } } } return min; } qreal BarChartModel::max() { // qDebug() << "BarChartModel::max"; Q_ASSERT(mDataModel.count() > 0); // TODO: make min and max members and update them when data changes. // This is slower since they are checked every time, even if data is same since previous call. qreal max = INT_MIN; for (int i=0; i countColumns(); for (int j=0; jvalueAt(j); if (temp > max) { max = temp; } } } return max; } qreal BarChartModel::valueAt(int series, int item) { // qDebug() << "BarChartModel::valueAt" << series << item; if ((series < 0) || (series >= mDataModel.count())) { // No series, no value. return 0; } else if ((item < 0) || (item >= mDataModel.at(series)->countColumns())) { // No item, no value. return 0; } // qDebug() << "ValueAt" << series << item << "=" << mDataModel.at(series)->valueAt(item); return mDataModel.at(series)->valueAt(item); } qreal BarChartModel::columnSum(int column) { // qDebug() << "BarChartModel::columnSum"; int sum(0); int count = mDataModel.count(); // Count rows for (int row = 0; row < count; row++) { if (column < mDataModel.at(row)->countColumns()) { sum += mDataModel.at(row)->valueAt(column); } } return sum; } qreal BarChartModel::maxColumnSum() { // qDebug() << "BarChartModel::maxColumnSum"; int max = INT_MIN; int count = countColumns(); for (int col=0; col max) { max = sum; } } return max; } #include "moc_barchartmodel_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE