##// END OF EJS Templates
Add build stamp and git head support for debug
Add build stamp and git head support for debug

File last commit:

r663:9af75e8e0281
r715:f8597835a464
Show More
barchartmodel.cpp
174 lines | 3.7 KiB | text/x-c | CppLexer
#include <limits.h>
#include <QVector>
#include <QDebug>
#include "barchartmodel_p.h"
#include "qbarset.h"
QTCOMMERCIALCHART_BEGIN_NAMESPACE
BarChartModel::BarChartModel(QStringList categories, QObject *parent) :
QObject(parent)
,mCategory(categories)
{
}
QStringList BarChartModel::category()
{
return mCategory;
}
void BarChartModel::addBarSet(QBarSet *set)
{
mDataModel.append(set);
}
void BarChartModel::removeBarSet(QBarSet *set)
{
if (mDataModel.contains(set)) {
mDataModel.removeOne(set);
}
}
void BarChartModel::insertBarSet(int i, QBarSet *set)
{
mDataModel.insert(i, set);
}
void BarChartModel::insertCategory(int i, QString category)
{
mCategory.insert(i, category);
}
void BarChartModel::removeCategory(int i)
{
mCategory.removeAt(i);
}
QBarSet* BarChartModel::setAt(int index)
{
return mDataModel.at(index);
}
QList<QBarSet*> BarChartModel::barSets()
{
return mDataModel;
}
int BarChartModel::barsetCount()
{
return mDataModel.count();
}
int BarChartModel::categoryCount()
{
return mCategory.count();
}
qreal 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 <mDataModel.count(); i++) {
int itemCount = mDataModel.at(i)->count();
for (int j=0; j<itemCount; j++) {
qreal temp = mDataModel.at(i)->valueAt(j);
if (temp < min) {
min = temp;
}
}
}
return min;
}
qreal 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 <mDataModel.count(); i++) {
int itemCount = mDataModel.at(i)->count();
for (int j=0; j<itemCount; j++) {
qreal temp = mDataModel.at(i)->valueAt(j);
if (temp > max) {
max = temp;
}
}
}
return max;
}
qreal BarChartModel::valueAt(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;
}
return mDataModel.at(set)->valueAt(category);
}
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)
{
qreal sum(0);
int count = mDataModel.count(); // Count sets
for (int set = 0; set < count; set++) {
if (category < mDataModel.at(set)->count()) {
sum += mDataModel.at(set)->valueAt(category);
}
}
return sum;
}
qreal BarChartModel::maxCategorySum()
{
qreal max = INT_MIN;
int count = categoryCount();
for (int col=0; col<count; col++) {
qreal sum = categorySum(col);
if (sum > max) {
max = sum;
}
}
return max;
}
QString BarChartModel::categoryName(int category)
{
return mCategory.at(category);
}
#include "moc_barchartmodel_p.cpp"
QTCOMMERCIALCHART_END_NAMESPACE