barchartmodel.cpp
168 lines
| 3.7 KiB
| text/x-c
|
CppLexer
|
r159 | #include <limits.h> | ||
|
r161 | #include <QVector> | ||
#include <QDebug> | ||||
|
r159 | #include "barchartmodel_p.h" | ||
|
r169 | #include "qbarset.h" | ||
|
r159 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r762 | BarChartModel::BarChartModel(QStringList categories, QObject *parent) : QObject(parent), | ||
|
r763 | m_category(categories) | ||
|
r159 | { | ||
} | ||||
|
r377 | QStringList BarChartModel::category() | ||
|
r159 | { | ||
|
r763 | return m_category; | ||
|
r169 | } | ||
|
r776 | void BarChartModel::appendBarSet(QBarSet *set) | ||
|
r169 | { | ||
|
r763 | m_dataModel.append(set); | ||
|
r169 | } | ||
|
r173 | void BarChartModel::removeBarSet(QBarSet *set) | ||
{ | ||||
|
r763 | if (m_dataModel.contains(set)) { | ||
m_dataModel.removeOne(set); | ||||
|
r173 | } | ||
} | ||||
|
r169 | |||
|
r662 | void BarChartModel::insertBarSet(int i, QBarSet *set) | ||
{ | ||||
|
r763 | m_dataModel.insert(i, set); | ||
|
r662 | } | ||
void BarChartModel::insertCategory(int i, QString category) | ||||
{ | ||||
|
r763 | m_category.insert(i, category); | ||
|
r662 | } | ||
|
r663 | void BarChartModel::removeCategory(int i) | ||
{ | ||||
|
r763 | m_category.removeAt(i); | ||
|
r663 | } | ||
|
r776 | QBarSet* BarChartModel::barsetAt(int index) const | ||
|
r214 | { | ||
|
r763 | return m_dataModel.at(index); | ||
|
r214 | } | ||
|
r776 | QList<QBarSet*> BarChartModel::barSets() const | ||
|
r214 | { | ||
|
r763 | return m_dataModel; | ||
|
r214 | } | ||
|
r776 | int BarChartModel::barsetCount() const | ||
|
r159 | { | ||
|
r763 | return m_dataModel.count(); | ||
|
r159 | } | ||
|
r776 | int BarChartModel::categoryCount() const | ||
|
r159 | { | ||
|
r763 | return m_category.count(); | ||
|
r159 | } | ||
|
r776 | qreal BarChartModel::min() const | ||
|
r159 | { | ||
|
r763 | Q_ASSERT(m_dataModel.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 | |||
|
r763 | for (int i = 0; i < m_dataModel.count(); i++) { | ||
int itemCount = m_dataModel.at(i)->count(); | ||||
|
r737 | for (int j = 0; j < itemCount; j++) { | ||
|
r763 | qreal temp = m_dataModel.at(i)->valueAt(j); | ||
|
r737 | if (temp < min) | ||
|
r161 | min = temp; | ||
|
r159 | } | ||
} | ||||
return min; | ||||
} | ||||
|
r776 | qreal BarChartModel::max() const | ||
|
r159 | { | ||
|
r763 | Q_ASSERT(m_dataModel.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 | |||
|
r763 | for (int i = 0; i < m_dataModel.count(); i++) { | ||
int itemCount = m_dataModel.at(i)->count(); | ||||
|
r737 | for (int j = 0; j < itemCount; j++) { | ||
|
r763 | qreal temp = m_dataModel.at(i)->valueAt(j); | ||
|
r737 | if (temp > max) | ||
|
r161 | max = temp; | ||
|
r159 | } | ||
} | ||||
|
r161 | |||
|
r159 | return max; | ||
} | ||||
|
r776 | qreal BarChartModel::valueAt(int set, int category) const | ||
|
r159 | { | ||
|
r763 | if ((set < 0) || (set >= m_dataModel.count())) { | ||
|
r171 | // No set, no value. | ||
|
r159 | return 0; | ||
|
r763 | } else if ((category < 0) || (category >= m_dataModel.at(set)->count())) { | ||
|
r171 | // No category, no value. | ||
|
r159 | return 0; | ||
} | ||||
|
r763 | return m_dataModel.at(set)->valueAt(category); | ||
|
r161 | } | ||
|
r776 | qreal BarChartModel::percentageAt(int set, int category) const | ||
|
r263 | { | ||
|
r763 | if ((set < 0) || (set >= m_dataModel.count())) { | ||
|
r263 | // No set, no value. | ||
return 0; | ||||
|
r763 | } else if ((category < 0) || (category >= m_dataModel.at(set)->count())) { | ||
|
r263 | // No category, no value. | ||
return 0; | ||||
} | ||||
|
r763 | qreal value = m_dataModel.at(set)->valueAt(category); | ||
|
r263 | qreal total = categorySum(category); | ||
|
r764 | if ( qFuzzyCompare(total, 0) ) | ||
return 0; | ||||
|
r263 | |||
return value / total; | ||||
} | ||||
|
r776 | qreal BarChartModel::categorySum(int category) const | ||
|
r161 | { | ||
|
r171 | qreal sum(0); | ||
|
r763 | int count = m_dataModel.count(); // Count sets | ||
|
r161 | |||
|
r263 | for (int set = 0; set < count; set++) { | ||
|
r763 | if (category < m_dataModel.at(set)->count()) | ||
sum += m_dataModel.at(set)->valueAt(category); | ||||
|
r161 | } | ||
return sum; | ||||
} | ||||
|
r776 | qreal BarChartModel::maxCategorySum() const | ||
|
r161 | { | ||
|
r171 | qreal max = INT_MIN; | ||
|
r387 | int count = categoryCount(); | ||
|
r161 | |||
|
r737 | for (int col = 0; col < count; col++) { | ||
|
r171 | qreal sum = categorySum(col); | ||
|
r737 | if (sum > max) | ||
|
r161 | max = sum; | ||
} | ||||
return max; | ||||
|
r159 | } | ||
|
r412 | QString BarChartModel::categoryName(int category) | ||
|
r172 | { | ||
|
r763 | return m_category.at(category); | ||
|
r172 | } | ||
|
r159 | #include "moc_barchartmodel_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||