barchartmodel.cpp
175 lines
| 4.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
|
r169 | BarChartModel::BarChartModel(QBarCategory &category, QObject *parent) : | ||
sauimone
|
r159 | QObject(parent) | ||
sauimone
|
r161 | ,mRunningId(1) | ||
sauimone
|
r169 | ,mCategory(category) | ||
sauimone
|
r159 | { | ||
} | ||||
sauimone
|
r161 | BarChartModel::~BarChartModel() | ||
sauimone
|
r159 | { | ||
sauimone
|
r165 | // qDebug() << "BarChartModel::~BarChartModel"; | ||
sauimone
|
r161 | foreach (DataContainer* c, mDataModel) { | ||
delete c; | ||||
} | ||||
sauimone
|
r159 | } | ||
sauimone
|
r161 | int BarChartModel::addData(QList<qreal> data) | ||
sauimone
|
r159 | { | ||
sauimone
|
r165 | // qDebug() << "BarChartModel::addData" << data.count(); | ||
sauimone
|
r161 | DataContainer* c = new DataContainer(data,mRunningId); | ||
mDataModel.append(c); | ||||
mRunningId++; | ||||
sauimone
|
r167 | emit modelUpdated(); | ||
sauimone
|
r161 | return mRunningId-1; | ||
} | ||||
void BarChartModel::removeData(int id) | ||||
{ | ||||
sauimone
|
r165 | // qDebug() << "BarChartModel::removeData"; | ||
sauimone
|
r161 | foreach(DataContainer* c, mDataModel) { | ||
if (c->mId == id) { | ||||
mDataModel.removeOne(c); | ||||
delete c; | ||||
} | ||||
sauimone
|
r159 | } | ||
sauimone
|
r167 | emit modelUpdated(); | ||
sauimone
|
r159 | } | ||
sauimone
|
r169 | void BarChartModel::addBarSet(QBarSet &set) | ||
{ | ||||
DataContainer* c = new DataContainer(set.mValues,mRunningId); | ||||
mDataModel.append(c); | ||||
mRunningId++; | ||||
} | ||||
void BarChartModel::removeBarSet(QBarSet &set) | ||||
{ | ||||
// TODO: | ||||
} | ||||
sauimone
|
r161 | int BarChartModel::countRows() | ||
sauimone
|
r159 | { | ||
sauimone
|
r165 | // qDebug() << "BarChartModel::countRows"; | ||
sauimone
|
r161 | return mDataModel.count(); | ||
sauimone
|
r159 | } | ||
sauimone
|
r161 | int BarChartModel::countColumns() | ||
sauimone
|
r159 | { | ||
sauimone
|
r165 | // qDebug() << "BarChartModel::countColumns"; | ||
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
|
r161 | int temp = mDataModel.at(i)->countColumns(); | ||
sauimone
|
r159 | if (temp > count) { | ||
count = temp; | ||||
} | ||||
} | ||||
return count; | ||||
} | ||||
int BarChartModel::countTotalItems() | ||||
{ | ||||
sauimone
|
r165 | // qDebug() << "BarChartModel::countTotalItems"; | ||
sauimone
|
r161 | int total = mDataModel.count() * countColumns(); | ||
sauimone
|
r159 | return total; | ||
} | ||||
sauimone
|
r167 | qreal BarChartModel::min() | ||
sauimone
|
r159 | { | ||
sauimone
|
r165 | // qDebug() << "BarChartModel::min"; | ||
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++) { | ||
int itemCount = mDataModel.at(i)->countColumns(); | ||||
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
|
r165 | // qDebug() << "BarChartModel::max"; | ||
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++) { | ||
int itemCount = mDataModel.at(i)->countColumns(); | ||||
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; | ||
} | ||||
qreal BarChartModel::valueAt(int series, int item) | ||||
{ | ||||
sauimone
|
r165 | // qDebug() << "BarChartModel::valueAt" << series << item; | ||
sauimone
|
r161 | if ((series < 0) || (series >= mDataModel.count())) { | ||
sauimone
|
r159 | // No series, no value. | ||
return 0; | ||||
sauimone
|
r161 | } else if ((item < 0) || (item >= mDataModel.at(series)->countColumns())) { | ||
sauimone
|
r159 | // No item, no value. | ||
return 0; | ||||
} | ||||
sauimone
|
r165 | // qDebug() << "ValueAt" << series << item << "=" << mDataModel.at(series)->valueAt(item); | ||
sauimone
|
r161 | return mDataModel.at(series)->valueAt(item); | ||
} | ||||
qreal BarChartModel::columnSum(int column) | ||||
{ | ||||
sauimone
|
r165 | // qDebug() << "BarChartModel::columnSum"; | ||
sauimone
|
r161 | int sum(0); | ||
int count = mDataModel.count(); // Count rows | ||||
for (int row = 0; row < count; row++) { | ||||
if (column < mDataModel.at(row)->countColumns()) { | ||||
sum += mDataModel.at(row)->valueAt(column); | ||||
} | ||||
} | ||||
return sum; | ||||
} | ||||
qreal BarChartModel::maxColumnSum() | ||||
{ | ||||
sauimone
|
r165 | // qDebug() << "BarChartModel::maxColumnSum"; | ||
sauimone
|
r161 | int max = INT_MIN; | ||
int count = countColumns(); | ||||
for (int col=0; col<count; col++) { | ||||
int sum = columnSum(col); | ||||
if (sum > max) { | ||||
max = sum; | ||||
} | ||||
} | ||||
return max; | ||||
sauimone
|
r159 | } | ||
#include "moc_barchartmodel_p.cpp" | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||