qbarseries.cpp
562 lines
| 13.6 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r794 | /**************************************************************************** | ||
** | ||||
** Copyright (C) 2012 Digia Plc | ||||
** All rights reserved. | ||||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
sauimone
|
r338 | #include "qbarseries.h" | ||
Michal Klocek
|
r938 | #include "qbarseries_p.h" | ||
sauimone
|
r172 | #include "qbarset.h" | ||
Michal Klocek
|
r938 | #include "qbarset_p.h" | ||
Michal Klocek
|
r943 | #include "domain_p.h" | ||
Michal Klocek
|
r950 | #include "legendmarker_p.h" | ||
Michal Klocek
|
r943 | #include "chartdataset_p.h" | ||
#include "charttheme_p.h" | ||||
#include "chartanimator_p.h" | ||||
Michal Klocek
|
r938 | |||
sauimone
|
r56 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
sauimone
|
r313 | /*! | ||
sauimone
|
r338 | \class QBarSeries | ||
sauimone
|
r313 | \brief part of QtCommercial chart API. | ||
Tero Ahola
|
r995 | \mainclass | ||
sauimone
|
r313 | |||
sauimone
|
r1208 | QBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to | ||
the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar | ||||
and y-value is the height of the bar. The category names are ignored with this series and x-axis | ||||
shows the x-values. | ||||
sauimone
|
r313 | |||
Tero Ahola
|
r995 | See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart. | ||
\image examples_barchart.png | ||||
sauimone
|
r319 | |||
sauimone
|
r377 | \sa QBarSet, QStackedBarSeries, QPercentBarSeries | ||
sauimone
|
r313 | */ | ||
/*! | ||||
sauimone
|
r1321 | \fn void QBarSeries::clicked(QBarSet *barset, int index) | ||
Tero Ahola
|
r973 | |||
sauimone
|
r1321 | The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset. | ||
Clicked bar inside set is indexed by \a index | ||||
Tero Ahola
|
r973 | */ | ||
sauimone
|
r980 | /*! | ||
\fn void QBarSeries::hovered(QBarSet* barset, bool status) | ||||
The signal is emitted if mouse is hovered on top of series. | ||||
Parameter \a barset is the pointer of barset, where hover happened. | ||||
Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series. | ||||
*/ | ||||
sauimone
|
r313 | /*! | ||
sauimone
|
r1208 | Constructs empty QBarSeries. | ||
sauimone
|
r338 | QBarSeries is QObject which is a child of a \a parent. | ||
sauimone
|
r313 | */ | ||
sauimone
|
r1208 | QBarSeries::QBarSeries(QObject *parent) : | ||
QAbstractSeries(*new QBarSeriesPrivate(this),parent) | ||||
Michal Klocek
|
r938 | { | ||
} | ||||
sauimone
|
r980 | /*! | ||
Destructs barseries and owned barsets. | ||||
*/ | ||||
QBarSeries::~QBarSeries() | ||||
{ | ||||
sauimone
|
r1263 | Q_D(QBarSeries); | ||
if(d->m_dataset){ | ||||
d->m_dataset->removeSeries(this); | ||||
} | ||||
sauimone
|
r980 | } | ||
Tero Ahola
|
r973 | /*! | ||
\internal | ||||
*/ | ||||
QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) : | ||||
Tero Ahola
|
r988 | QAbstractSeries(d,parent) | ||
Michal Klocek
|
r943 | { | ||
} | ||||
sauimone
|
r980 | /*! | ||
Returns the type of series. Derived classes override this. | ||||
*/ | ||||
Michal Klocek
|
r1107 | QAbstractSeries::SeriesType QBarSeries::type() const | ||
sauimone
|
r71 | { | ||
Tero Ahola
|
r988 | return QAbstractSeries::SeriesTypeBar; | ||
sauimone
|
r71 | } | ||
sauimone
|
r1299 | /*! | ||
Sets the margin around bars. Parameter \a margin is from 0 to 1 and represents | ||||
percentage of margin compared to bars | ||||
*/ | ||||
sauimone
|
r1283 | void QBarSeries::setBarMargin(qreal margin) | ||
{ | ||||
Q_D(QBarSeries); | ||||
d->setBarMargin(margin); | ||||
} | ||||
sauimone
|
r1299 | /*! | ||
Returns the margin around bars | ||||
*/ | ||||
sauimone
|
r1284 | qreal QBarSeries::barMargin() const | ||
{ | ||||
Q_D(const QBarSeries); | ||||
return d->barMargin(); | ||||
} | ||||
sauimone
|
r313 | /*! | ||
sauimone
|
r1208 | Adds a set of bars to series. Takes ownership of \a set. If the set is null or is already in series, it won't be appended. | ||
Returns true, if appending succeeded. | ||||
sauimone
|
r313 | */ | ||
sauimone
|
r1194 | bool QBarSeries::append(QBarSet *set) | ||
sauimone
|
r171 | { | ||
sauimone
|
r934 | Q_D(QBarSeries); | ||
sauimone
|
r1263 | return d->append(set); | ||
sauimone
|
r171 | } | ||
sauimone
|
r313 | /*! | ||
Michal Klocek
|
r974 | Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set. | ||
sauimone
|
r1208 | Returns true, if set was removed. | ||
sauimone
|
r313 | */ | ||
sauimone
|
r1194 | bool QBarSeries::remove(QBarSet *set) | ||
sauimone
|
r171 | { | ||
sauimone
|
r934 | Q_D(QBarSeries); | ||
sauimone
|
r1263 | return d->remove(set); | ||
sauimone
|
r850 | } | ||
/*! | ||||
Adds a list of barsets to series. Takes ownership of \a sets. | ||||
sauimone
|
r1208 | Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series, | ||
nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended | ||||
and function returns false. | ||||
sauimone
|
r850 | */ | ||
sauimone
|
r1194 | bool QBarSeries::append(QList<QBarSet* > sets) | ||
sauimone
|
r850 | { | ||
sauimone
|
r934 | Q_D(QBarSeries); | ||
sauimone
|
r1263 | return d->append(sets); | ||
sauimone
|
r850 | } | ||
/*! | ||||
Michal Klocek
|
r974 | Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets. | ||
sauimone
|
r850 | */ | ||
sauimone
|
r1194 | bool QBarSeries::remove(QList<QBarSet* > sets) | ||
sauimone
|
r850 | { | ||
sauimone
|
r934 | Q_D(QBarSeries); | ||
sauimone
|
r1263 | return d->remove(sets); | ||
sauimone
|
r172 | } | ||
Marek Rosa
|
r1295 | void QBarSeries::clear() | ||
{ | ||||
Q_D(QBarSeries); | ||||
d->m_barSets.clear(); | ||||
} | ||||
sauimone
|
r313 | /*! | ||
Returns number of sets in series. | ||||
*/ | ||||
sauimone
|
r776 | int QBarSeries::barsetCount() const | ||
sauimone
|
r214 | { | ||
sauimone
|
r934 | Q_D(const QBarSeries); | ||
sauimone
|
r1005 | return d->m_barSets.count(); | ||
sauimone
|
r214 | } | ||
sauimone
|
r313 | /*! | ||
sauimone
|
r357 | Returns a list of sets in series. Keeps ownership of sets. | ||
*/ | ||||
sauimone
|
r776 | QList<QBarSet*> QBarSeries::barSets() const | ||
sauimone
|
r214 | { | ||
sauimone
|
r934 | Q_D(const QBarSeries); | ||
sauimone
|
r1005 | return d->m_barSets; | ||
sauimone
|
r214 | } | ||
sauimone
|
r1299 | /*! | ||
Sets the visibility of series to \a visible | ||||
*/ | ||||
sauimone
|
r1284 | void QBarSeries::setVisible(bool visible) | ||
{ | ||||
Q_D(QBarSeries); | ||||
d->setVisible(visible); | ||||
} | ||||
sauimone
|
r1299 | /*! | ||
Returns the visibility of series | ||||
*/ | ||||
sauimone
|
r1284 | bool QBarSeries::isVisible() const | ||
{ | ||||
Q_D(const QBarSeries); | ||||
return d->isVisible(); | ||||
} | ||||
sauimone
|
r839 | /*! | ||
Sets the visibility of labels in series to \a visible | ||||
*/ | ||||
sauimone
|
r820 | void QBarSeries::setLabelsVisible(bool visible) | ||
sauimone
|
r813 | { | ||
sauimone
|
r1246 | Q_D(QBarSeries); | ||
if (d->m_labelsVisible != visible) { | ||||
d->m_labelsVisible = visible; | ||||
Tero Ahola
|
r1302 | emit d->labelsVisibleChanged(visible); | ||
sauimone
|
r813 | } | ||
} | ||||
sauimone
|
r1299 | /*! | ||
Returns the visibility of labels | ||||
*/ | ||||
sauimone
|
r1246 | bool QBarSeries::isLabelsVisible() const | ||
{ | ||||
Q_D(const QBarSeries); | ||||
return d->m_labelsVisible; | ||||
} | ||||
Michal Klocek
|
r938 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
sauimone
|
r1167 | QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) : | ||
Tero Ahola
|
r988 | QAbstractSeriesPrivate(q), | ||
sauimone
|
r1285 | m_barMargin(0.5), // Default value is 50% of category width | ||
sauimone
|
r1284 | m_labelsVisible(false), | ||
m_visible(true) | ||||
Michal Klocek
|
r938 | { | ||
} | ||||
Marek Rosa
|
r1333 | void QBarSeriesPrivate::setCategories(QStringList categories) | ||
{ | ||||
m_categories = categories; | ||||
} | ||||
void QBarSeriesPrivate::insertCategory(int index, const QString category) | ||||
{ | ||||
m_categories.insert(index, category); | ||||
emit categoriesUpdated(); | ||||
} | ||||
void QBarSeriesPrivate::removeCategory(int index) | ||||
{ | ||||
m_categories.removeAt(index); | ||||
emit categoriesUpdated(); | ||||
} | ||||
sauimone
|
r1208 | int QBarSeriesPrivate::categoryCount() const | ||
{ | ||||
if (m_categories.count() > 0) { | ||||
return m_categories.count(); | ||||
} | ||||
// No categories defined. return count of longest set. | ||||
int count = 0; | ||||
for (int i=0; i<m_barSets.count(); i++) { | ||||
if (m_barSets.at(i)->count() > count) { | ||||
count = m_barSets.at(i)->count(); | ||||
} | ||||
} | ||||
return count; | ||||
} | ||||
sauimone
|
r1321 | QStringList QBarSeriesPrivate::categories() const | ||
sauimone
|
r1208 | { | ||
if (m_categories.count() > 0) { | ||||
return m_categories; | ||||
} | ||||
// No categories defined. retun list of indices. | ||||
sauimone
|
r1321 | QStringList categories; | ||
sauimone
|
r1208 | |||
int count = categoryCount(); | ||||
for (int i = 0; i < count; i++) { | ||||
categories.append(QString::number(i)); | ||||
} | ||||
return categories; | ||||
} | ||||
sauimone
|
r1167 | void QBarSeriesPrivate::setBarMargin(qreal margin) | ||
{ | ||||
if (margin > 1.0) { | ||||
margin = 1.0; | ||||
} else if (margin < 0.0) { | ||||
margin = 0.0; | ||||
} | ||||
m_barMargin = margin; | ||||
emit updatedBars(); | ||||
} | ||||
sauimone
|
r1284 | qreal QBarSeriesPrivate::barMargin() const | ||
sauimone
|
r1167 | { | ||
return m_barMargin; | ||||
} | ||||
sauimone
|
r1112 | |||
Michal Klocek
|
r938 | QBarSet* QBarSeriesPrivate::barsetAt(int index) | ||
{ | ||||
sauimone
|
r1005 | return m_barSets.at(index); | ||
Michal Klocek
|
r938 | } | ||
sauimone
|
r1284 | void QBarSeriesPrivate::setVisible(bool visible) | ||
{ | ||||
if (m_visible != visible) { | ||||
m_visible = visible; | ||||
emit updatedBars(); | ||||
} | ||||
} | ||||
bool QBarSeriesPrivate::isVisible() const | ||||
{ | ||||
return m_visible; | ||||
} | ||||
Michal Klocek
|
r938 | QString QBarSeriesPrivate::categoryName(int category) | ||
{ | ||||
sauimone
|
r1219 | if ((category >= 0) && (category < m_categories.count())) { | ||
sauimone
|
r1208 | return m_categories.at(category); | ||
} | ||||
return QString::number(category); | ||||
Michal Klocek
|
r938 | } | ||
qreal QBarSeriesPrivate::min() | ||||
{ | ||||
sauimone
|
r1005 | if (m_barSets.count() <= 0) { | ||
return 0; | ||||
} | ||||
qreal min = INT_MAX; | ||||
for (int i = 0; i < m_barSets.count(); i++) { | ||||
int categoryCount = m_barSets.at(i)->count(); | ||||
for (int j = 0; j < categoryCount; j++) { | ||||
sauimone
|
r1167 | qreal temp = m_barSets.at(i)->at(j).y(); | ||
sauimone
|
r1005 | if (temp < min) | ||
min = temp; | ||||
} | ||||
} | ||||
return min; | ||||
Michal Klocek
|
r938 | } | ||
qreal QBarSeriesPrivate::max() | ||||
{ | ||||
sauimone
|
r1005 | if (m_barSets.count() <= 0) { | ||
return 0; | ||||
} | ||||
qreal max = INT_MIN; | ||||
for (int i = 0; i < m_barSets.count(); i++) { | ||||
int categoryCount = m_barSets.at(i)->count(); | ||||
for (int j = 0; j < categoryCount; j++) { | ||||
sauimone
|
r1167 | qreal temp = m_barSets.at(i)->at(j).y(); | ||
sauimone
|
r1005 | if (temp > max) | ||
max = temp; | ||||
} | ||||
} | ||||
return max; | ||||
Michal Klocek
|
r938 | } | ||
qreal QBarSeriesPrivate::valueAt(int set, int category) | ||||
{ | ||||
sauimone
|
r1005 | if ((set < 0) || (set >= m_barSets.count())) { | ||
// No set, no value. | ||||
return 0; | ||||
} else if ((category < 0) || (category >= m_barSets.at(set)->count())) { | ||||
// No category, no value. | ||||
return 0; | ||||
} | ||||
sauimone
|
r1167 | return m_barSets.at(set)->at(category).y(); | ||
Michal Klocek
|
r938 | } | ||
qreal QBarSeriesPrivate::percentageAt(int set, int category) | ||||
{ | ||||
sauimone
|
r1005 | if ((set < 0) || (set >= m_barSets.count())) { | ||
// No set, no value. | ||||
return 0; | ||||
} else if ((category < 0) || (category >= m_barSets.at(set)->count())) { | ||||
// No category, no value. | ||||
return 0; | ||||
} | ||||
sauimone
|
r1167 | qreal value = m_barSets.at(set)->at(category).y(); | ||
sauimone
|
r1005 | qreal sum = categorySum(category); | ||
if ( qFuzzyIsNull(sum) ) { | ||||
return 0; | ||||
} | ||||
return value / sum; | ||||
Michal Klocek
|
r938 | } | ||
qreal QBarSeriesPrivate::categorySum(int category) | ||||
{ | ||||
sauimone
|
r1005 | qreal sum(0); | ||
int count = m_barSets.count(); // Count sets | ||||
for (int set = 0; set < count; set++) { | ||||
if (category < m_barSets.at(set)->count()) | ||||
sauimone
|
r1167 | sum += m_barSets.at(set)->at(category).y(); | ||
sauimone
|
r1005 | } | ||
return sum; | ||||
Michal Klocek
|
r938 | } | ||
qreal QBarSeriesPrivate::absoluteCategorySum(int category) | ||||
{ | ||||
sauimone
|
r1005 | qreal sum(0); | ||
int count = m_barSets.count(); // Count sets | ||||
for (int set = 0; set < count; set++) { | ||||
if (category < m_barSets.at(set)->count()) | ||||
sauimone
|
r1167 | sum += qAbs(m_barSets.at(set)->at(category).y()); | ||
sauimone
|
r1005 | } | ||
return sum; | ||||
Michal Klocek
|
r938 | } | ||
qreal QBarSeriesPrivate::maxCategorySum() | ||||
{ | ||||
sauimone
|
r1005 | qreal max = INT_MIN; | ||
sauimone
|
r1208 | int count = categoryCount(); | ||
sauimone
|
r1005 | for (int i = 0; i < count; i++) { | ||
qreal sum = categorySum(i); | ||||
if (sum > max) | ||||
max = sum; | ||||
} | ||||
return max; | ||||
Michal Klocek
|
r938 | } | ||
void QBarSeriesPrivate::barsetChanged() | ||||
{ | ||||
emit updatedBars(); | ||||
} | ||||
sauimone
|
r813 | |||
Michal Klocek
|
r943 | void QBarSeriesPrivate::scaleDomain(Domain& domain) | ||
{ | ||||
qreal minX(domain.minX()); | ||||
qreal minY(domain.minY()); | ||||
qreal maxX(domain.maxX()); | ||||
qreal maxY(domain.maxY()); | ||||
int tickXCount(domain.tickXCount()); | ||||
int tickYCount(domain.tickYCount()); | ||||
sauimone
|
r1208 | qreal x = categoryCount(); | ||
sauimone
|
r962 | qreal y = max(); | ||
sauimone
|
r1228 | minX = qMin(minX, x) - 0.5; | ||
Michal Klocek
|
r943 | minY = qMin(minY, y); | ||
sauimone
|
r1264 | maxX = qMax(maxX, x) - 0.5; | ||
Michal Klocek
|
r943 | maxY = qMax(maxY, y); | ||
tickXCount = x+1; | ||||
Michal Klocek
|
r1078 | domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount); | ||
Michal Klocek
|
r943 | } | ||
Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter) | ||||
{ | ||||
Q_Q(QBarSeries); | ||||
BarChartItem* bar = new BarChartItem(q,presenter); | ||||
if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { | ||||
presenter->animator()->addAnimation(bar); | ||||
} | ||||
presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); | ||||
return bar; | ||||
} | ||||
Michal Klocek
|
r950 | QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend) | ||
{ | ||||
Q_Q(QBarSeries); | ||||
QList<LegendMarker*> markers; | ||||
foreach(QBarSet* set, q->barSets()) { | ||||
BarLegendMarker* marker = new BarLegendMarker(q,set,legend); | ||||
markers << marker; | ||||
} | ||||
return markers; | ||||
} | ||||
sauimone
|
r1263 | bool QBarSeriesPrivate::append(QBarSet *set) | ||
{ | ||||
Q_Q(QBarSeries); | ||||
if ((m_barSets.contains(set)) || (set == 0)) { | ||||
// Fail if set is already in list or set is null. | ||||
return false; | ||||
} | ||||
m_barSets.append(set); | ||||
QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged())); | ||||
if (m_dataset) { | ||||
m_dataset->updateSeries(q); // this notifies legend | ||||
} | ||||
Marek Rosa
|
r1333 | emit restructuredBars(); // this notifies barchartitem | ||
sauimone
|
r1263 | return true; | ||
} | ||||
bool QBarSeriesPrivate::remove(QBarSet *set) | ||||
{ | ||||
Q_Q(QBarSeries); | ||||
if (!m_barSets.contains(set)) { | ||||
// Fail if set is not in list | ||||
return false; | ||||
} | ||||
m_barSets.removeOne(set); | ||||
QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged())); | ||||
if (m_dataset) { | ||||
m_dataset->updateSeries(q); // this notifies legend | ||||
} | ||||
Marek Rosa
|
r1333 | emit restructuredBars(); // this notifies barchartitem | ||
sauimone
|
r1263 | return true; | ||
} | ||||
bool QBarSeriesPrivate::append(QList<QBarSet* > sets) | ||||
{ | ||||
Q_Q(QBarSeries); | ||||
foreach (QBarSet* set, sets) { | ||||
if ((set == 0) || (m_barSets.contains(set))) { | ||||
// Fail if any of the sets is null or is already appended. | ||||
return false; | ||||
} | ||||
if (sets.count(set) != 1) { | ||||
// Also fail if same set is more than once in given list. | ||||
return false; | ||||
} | ||||
} | ||||
foreach (QBarSet* set, sets) { | ||||
m_barSets.append(set); | ||||
QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged())); | ||||
} | ||||
if (m_dataset) { | ||||
m_dataset->updateSeries(q); // this notifies legend | ||||
} | ||||
Marek Rosa
|
r1333 | emit restructuredBars(); // this notifies barchartitem | ||
sauimone
|
r1263 | return true; | ||
} | ||||
bool QBarSeriesPrivate::remove(QList<QBarSet* > sets) | ||||
{ | ||||
Q_Q(QBarSeries); | ||||
bool setsRemoved = false; | ||||
foreach (QBarSet* set, sets) { | ||||
if (m_barSets.contains(set)) { | ||||
m_barSets.removeOne(set); | ||||
QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged())); | ||||
setsRemoved = true; | ||||
} | ||||
} | ||||
if (setsRemoved) { | ||||
if (m_dataset) { | ||||
m_dataset->updateSeries(q); // this notifies legend | ||||
} | ||||
Marek Rosa
|
r1333 | emit restructuredBars(); // this notifies barchartitem | ||
sauimone
|
r1263 | } | ||
return setsRemoved; | ||||
} | ||||
sauimone
|
r338 | #include "moc_qbarseries.cpp" | ||
Michal Klocek
|
r938 | #include "moc_qbarseries_p.cpp" | ||
sauimone
|
r71 | |||
sauimone
|
r56 | QTCOMMERCIALCHART_END_NAMESPACE | ||