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