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