barchartmodel.cpp
190 lines
| 4.1 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 | |||
|
r357 | QBarSet* BarChartModel::setAt(int index) | ||
|
r214 | { | ||
|
r357 | return mDataModel.at(index); | ||
|
r214 | } | ||
|
r357 | QList<QBarSet*> BarChartModel::barSets() | ||
|
r214 | { | ||
|
r357 | return mDataModel; | ||
|
r214 | } | ||
|
r280 | QList<QString> BarChartModel::legend() | ||
{ | ||||
QList<QString> legend; | ||||
for (int i=0; i<mDataModel.count(); i++) { | ||||
legend.append(mDataModel.at(i)->name()); | ||||
} | ||||
return legend; | ||||
} | ||||
|
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 | } | ||
|
r263 | qreal BarChartModel::percentageAt(int set, int category) | ||
{ | ||||
if ((set < 0) || (set >= mDataModel.count())) { | ||||
// No set, no value. | ||||
return 0; | ||||
} else if ((category < 0) || (category >= mDataModel.at(set)->count())) { | ||||
// No category, no value. | ||||
return 0; | ||||
} | ||||
qreal value = mDataModel.at(set)->valueAt(category); | ||||
qreal total = categorySum(category); | ||||
if (0 == total) { | ||||
return 100.0; | ||||
} | ||||
return value / total; | ||||
} | ||||
qreal BarChartModel::categorySum(int category) | ||||
|
r161 | { | ||
|
r171 | qreal sum(0); | ||
|
r263 | int count = mDataModel.count(); // Count sets | ||
|
r161 | |||
|
r263 | for (int set = 0; set < count; set++) { | ||
if (category < mDataModel.at(set)->count()) { | ||||
sum += mDataModel.at(set)->valueAt(category); | ||||
|
r161 | } | ||
} | ||||
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 | ||||