barchartmodel.cpp
171 lines
| 3.6 KiB
| text/x-c
|
CppLexer
|
r159 | #include <limits.h> | ||
|
r161 | #include <QVector> | ||
#include <QDebug> | ||||
|
r159 | #include "barchartmodel_p.h" | ||
|
r169 | #include "qbarcategory.h" | ||
#include "qbarset.h" | ||||
|
r159 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r173 | BarChartModel::BarChartModel(QBarCategory *category, QObject *parent) : | ||
|
r159 | QObject(parent) | ||
|
r169 | ,mCategory(category) | ||
|
r159 | { | ||
} | ||||
|
r173 | BarChartModel::~BarChartModel() | ||
|
r159 | { | ||
|
r173 | delete mCategory; | ||
|
r159 | } | ||
|
r173 | |||
QBarCategory& BarChartModel::category() | ||||
|
r169 | { | ||
|
r173 | return *mCategory; | ||
|
r169 | } | ||
|
r173 | void BarChartModel::addBarSet(QBarSet *set) | ||
|
r169 | { | ||
|
r173 | mDataModel.append(set); | ||
|
r169 | } | ||
|
r173 | void BarChartModel::removeBarSet(QBarSet *set) | ||
{ | ||||
if (mDataModel.contains(set)) { | ||||
mDataModel.removeOne(set); | ||||
} | ||||
} | ||||
|
r169 | |||
|
r214 | QBarSet* BarChartModel::nextSet(bool getFirst) | ||
{ | ||||
if (getFirst) { | ||||
mCurrentSet = 0; | ||||
} | ||||
if ((mDataModel.count() <= 0) || (mDataModel.count() <= mCurrentSet)) { | ||||
return 0; | ||||
} | ||||
QBarSet* set = mDataModel.at(mCurrentSet); | ||||
mCurrentSet++; | ||||
return set; | ||||
} | ||||
QBarSet& BarChartModel::setAt(int index) | ||||
{ | ||||
return *mDataModel.at(index); | ||||
} | ||||
|
r171 | int BarChartModel::countSets() | ||
|
r159 | { | ||
|
r161 | return mDataModel.count(); | ||
|
r159 | } | ||
|
r171 | int BarChartModel::countCategories() | ||
|
r159 | { | ||
int count(0); | ||||
|
r161 | for (int i=0; i<mDataModel.count(); i++){ | ||
|
r160 | // TODO: can we assume that all series have same number of values? If not. then which values are empty? | ||
|
r171 | int temp = mDataModel.at(i)->count(); | ||
|
r159 | if (temp > count) { | ||
count = temp; | ||||
} | ||||
} | ||||
return count; | ||||
} | ||||
int BarChartModel::countTotalItems() | ||||
{ | ||||
|
r171 | int total = mDataModel.count() * countCategories(); | ||
|
r159 | return total; | ||
} | ||||
|
r167 | qreal BarChartModel::min() | ||
|
r159 | { | ||
|
r161 | Q_ASSERT(mDataModel.count() > 0); | ||
|
r159 | // 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. | ||||
|
r167 | qreal min = INT_MAX; | ||
|
r159 | |||
|
r161 | for (int i=0; i <mDataModel.count(); i++) { | ||
|
r171 | int itemCount = mDataModel.at(i)->count(); | ||
|
r161 | for (int j=0; j<itemCount; j++) { | ||
|
r167 | qreal temp = mDataModel.at(i)->valueAt(j); | ||
|
r161 | if (temp < min) { | ||
min = temp; | ||||
} | ||||
|
r159 | } | ||
} | ||||
return min; | ||||
} | ||||
|
r167 | qreal BarChartModel::max() | ||
|
r159 | { | ||
|
r161 | Q_ASSERT(mDataModel.count() > 0); | ||
|
r159 | |||
// 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. | ||||
|
r167 | qreal max = INT_MIN; | ||
|
r159 | |||
|
r161 | for (int i=0; i <mDataModel.count(); i++) { | ||
|
r171 | int itemCount = mDataModel.at(i)->count(); | ||
|
r161 | for (int j=0; j<itemCount; j++) { | ||
|
r167 | qreal temp = mDataModel.at(i)->valueAt(j); | ||
|
r161 | if (temp > max) { | ||
max = temp; | ||||
} | ||||
|
r159 | } | ||
} | ||||
|
r161 | |||
|
r159 | return max; | ||
} | ||||
|
r171 | qreal BarChartModel::valueAt(int set, int category) | ||
|
r159 | { | ||
|
r171 | if ((set < 0) || (set >= mDataModel.count())) { | ||
// No set, no value. | ||||
|
r159 | return 0; | ||
|
r171 | } else if ((category < 0) || (category >= mDataModel.at(set)->count())) { | ||
// No category, no value. | ||||
|
r159 | return 0; | ||
} | ||||
|
r171 | return mDataModel.at(set)->valueAt(category); | ||
|
r161 | } | ||
|
r171 | qreal BarChartModel::categorySum(int column) | ||
|
r161 | { | ||
|
r171 | qreal sum(0); | ||
|
r161 | int count = mDataModel.count(); // Count rows | ||
for (int row = 0; row < count; row++) { | ||||
|
r171 | if (column < mDataModel.at(row)->count()) { | ||
|
r161 | sum += mDataModel.at(row)->valueAt(column); | ||
} | ||||
} | ||||
return sum; | ||||
} | ||||
|
r171 | qreal BarChartModel::maxCategorySum() | ||
|
r161 | { | ||
|
r171 | qreal max = INT_MIN; | ||
int count = countCategories(); | ||||
|
r161 | |||
for (int col=0; col<count; col++) { | ||||
|
r171 | qreal sum = categorySum(col); | ||
|
r161 | if (sum > max) { | ||
max = sum; | ||||
} | ||||
} | ||||
return max; | ||||
|
r159 | } | ||
|
r172 | QString BarChartModel::label(int category) | ||
{ | ||||
|
r173 | return mCategory->label(category); | ||
|
r172 | } | ||
|
r159 | #include "moc_barchartmodel_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||