qbarseries.cpp
483 lines
| 12.0 KiB
| text/x-c
|
CppLexer
|
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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r338 | #include "qbarseries.h" | ||
|
r938 | #include "qbarseries_p.h" | ||
|
r172 | #include "qbarset.h" | ||
|
r938 | #include "qbarset_p.h" | ||
|
r943 | #include "domain_p.h" | ||
|
r950 | #include "legendmarker_p.h" | ||
|
r943 | #include "chartdataset_p.h" | ||
#include "charttheme_p.h" | ||||
#include "chartanimator_p.h" | ||||
|
r938 | |||
|
r56 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
|
r313 | /*! | ||
|
r338 | \class QBarSeries | ||
|
r313 | \brief part of QtCommercial chart API. | ||
|
r995 | \mainclass | ||
|
r313 | |||
|
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. | ||||
|
r313 | |||
|
r995 | See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart. | ||
\image examples_barchart.png | ||||
|
r319 | |||
|
r377 | \sa QBarSet, QStackedBarSeries, QPercentBarSeries | ||
|
r313 | */ | ||
/*! | ||||
|
r1008 | \fn void QBarSeries::clicked(QBarSet *barset, QString category) | ||
|
r973 | |||
|
r1008 | The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset of category \a category | ||
|
r973 | contained by the series. | ||
*/ | ||||
|
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. | ||||
*/ | ||||
|
r313 | /*! | ||
|
r1208 | Constructs empty QBarSeries. | ||
|
r338 | QBarSeries is QObject which is a child of a \a parent. | ||
|
r313 | */ | ||
|
r1208 | QBarSeries::QBarSeries(QObject *parent) : | ||
QAbstractSeries(*new QBarSeriesPrivate(this),parent) | ||||
|
r938 | { | ||
} | ||||
|
r980 | /*! | ||
Destructs barseries and owned barsets. | ||||
*/ | ||||
QBarSeries::~QBarSeries() | ||||
{ | ||||
// NOTE: d_ptr destroyed by QObject | ||||
} | ||||
|
r973 | /*! | ||
\internal | ||||
*/ | ||||
QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) : | ||||
|
r988 | QAbstractSeries(d,parent) | ||
|
r943 | { | ||
} | ||||
|
r980 | /*! | ||
Returns the type of series. Derived classes override this. | ||||
*/ | ||||
|
r1107 | QAbstractSeries::SeriesType QBarSeries::type() const | ||
|
r71 | { | ||
|
r988 | return QAbstractSeries::SeriesTypeBar; | ||
|
r71 | } | ||
|
r1208 | /*! | ||
Sets the \a categories, which are used to to group the data. | ||||
*/ | ||||
|
r1112 | void QBarSeries::setCategories(QBarCategories categories) | ||
{ | ||||
Q_D(QBarSeries); | ||||
d->setCategories(categories); | ||||
emit d->categoriesUpdated(); | ||||
} | ||||
|
r313 | /*! | ||
|
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. | ||||
|
r313 | */ | ||
|
r1194 | bool QBarSeries::append(QBarSet *set) | ||
|
r171 | { | ||
|
r934 | Q_D(QBarSeries); | ||
|
r1121 | if ((d->m_barSets.contains(set)) || (set == 0)) { | ||
// Fail if set is already in list or set is null. | ||||
return false; | ||||
} | ||||
|
r1005 | d->m_barSets.append(set); | ||
|
r1008 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged())); | ||
|
r938 | emit d->restructuredBars(); | ||
|
r1121 | return true; | ||
|
r171 | } | ||
|
r313 | /*! | ||
|
r974 | Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set. | ||
|
r1208 | Returns true, if set was removed. | ||
|
r313 | */ | ||
|
r1194 | bool QBarSeries::remove(QBarSet *set) | ||
|
r171 | { | ||
|
r934 | Q_D(QBarSeries); | ||
|
r1121 | if (!d->m_barSets.contains(set)) { | ||
// Fail if set is not in list | ||||
return false; | ||||
|
r1005 | } | ||
|
r1121 | d->m_barSets.removeOne(set); | ||
QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged())); | ||||
emit d->restructuredBars(); | ||||
return true; | ||||
|
r850 | } | ||
/*! | ||||
Adds a list of barsets to series. Takes ownership of \a sets. | ||||
|
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. | ||||
|
r850 | */ | ||
|
r1194 | bool QBarSeries::append(QList<QBarSet* > sets) | ||
|
r850 | { | ||
|
r934 | Q_D(QBarSeries); | ||
|
r1121 | foreach (QBarSet* set, sets) { | ||
if ((set == 0) || (d->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; | ||||
} | ||||
} | ||||
|
r1008 | foreach (QBarSet* set, sets) { | ||
d->m_barSets.append(set); | ||||
|
r1101 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged())); | ||
|
r850 | } | ||
|
r938 | emit d->restructuredBars(); | ||
|
r1121 | return true; | ||
|
r850 | } | ||
/*! | ||||
|
r974 | Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets. | ||
|
r850 | */ | ||
|
r1194 | bool QBarSeries::remove(QList<QBarSet* > sets) | ||
|
r850 | { | ||
|
r934 | Q_D(QBarSeries); | ||
|
r938 | |||
|
r1122 | bool setsRemoved = false; | ||
|
r1008 | foreach (QBarSet* set, sets) { | ||
if (d->m_barSets.contains(set)) { | ||||
d->m_barSets.removeOne(set); | ||||
|
r1101 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged())); | ||
|
r1122 | setsRemoved = true; | ||
|
r1005 | } | ||
|
r850 | } | ||
|
r1122 | |||
if (setsRemoved) { | ||||
emit d->restructuredBars(); | ||||
} | ||||
return setsRemoved; | ||||
|
r172 | } | ||
|
r313 | /*! | ||
Returns number of sets in series. | ||||
*/ | ||||
|
r776 | int QBarSeries::barsetCount() const | ||
|
r214 | { | ||
|
r934 | Q_D(const QBarSeries); | ||
|
r1005 | return d->m_barSets.count(); | ||
|
r214 | } | ||
|
r323 | /*! | ||
Returns number of categories in series | ||||
*/ | ||||
|
r776 | int QBarSeries::categoryCount() const | ||
|
r323 | { | ||
|
r934 | Q_D(const QBarSeries); | ||
|
r1208 | return d->categoryCount(); | ||
|
r323 | } | ||
|
r313 | /*! | ||
|
r357 | Returns a list of sets in series. Keeps ownership of sets. | ||
*/ | ||||
|
r776 | QList<QBarSet*> QBarSeries::barSets() const | ||
|
r214 | { | ||
|
r934 | Q_D(const QBarSeries); | ||
|
r1005 | return d->m_barSets; | ||
|
r214 | } | ||
|
r973 | /*! | ||
Returns the bar categories of the series. | ||||
*/ | ||||
|
r703 | QBarCategories QBarSeries::categories() const | ||
{ | ||||
|
r934 | Q_D(const QBarSeries); | ||
|
r1208 | return d->categories(); | ||
|
r703 | } | ||
|
r839 | /*! | ||
Sets the visibility of labels in series to \a visible | ||||
*/ | ||||
|
r820 | void QBarSeries::setLabelsVisible(bool visible) | ||
|
r813 | { | ||
foreach (QBarSet* s, barSets()) { | ||||
|
r820 | s->setLabelsVisible(visible); | ||
|
r813 | } | ||
} | ||||
|
r938 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
r1167 | QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) : | ||
|
r988 | QAbstractSeriesPrivate(q), | ||
|
r1230 | m_barMargin(0.05) // Default value is 5% of category width | ||
|
r938 | { | ||
} | ||||
|
r1112 | void QBarSeriesPrivate::setCategories(QBarCategories categories) | ||
{ | ||||
m_categories = categories; | ||||
} | ||||
|
r1208 | 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(); | ||||
} | ||||
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; | ||||
} | ||||
QBarCategories QBarSeriesPrivate::categories() const | ||||
{ | ||||
if (m_categories.count() > 0) { | ||||
return m_categories; | ||||
} | ||||
// No categories defined. retun list of indices. | ||||
QBarCategories categories; | ||||
int count = categoryCount(); | ||||
for (int i = 0; i < count; i++) { | ||||
categories.append(QString::number(i)); | ||||
} | ||||
return categories; | ||||
} | ||||
|
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(); | ||||
} | ||||
qreal QBarSeriesPrivate::barMargin() | ||||
{ | ||||
return m_barMargin; | ||||
} | ||||
|
r1112 | |||
|
r938 | QBarSet* QBarSeriesPrivate::barsetAt(int index) | ||
{ | ||||
|
r1005 | return m_barSets.at(index); | ||
|
r938 | } | ||
QString QBarSeriesPrivate::categoryName(int category) | ||||
{ | ||||
|
r1219 | if ((category >= 0) && (category < m_categories.count())) { | ||
|
r1208 | return m_categories.at(category); | ||
} | ||||
return QString::number(category); | ||||
|
r938 | } | ||
qreal QBarSeriesPrivate::min() | ||||
{ | ||||
|
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++) { | ||||
|
r1167 | qreal temp = m_barSets.at(i)->at(j).y(); | ||
|
r1005 | if (temp < min) | ||
min = temp; | ||||
} | ||||
} | ||||
return min; | ||||
|
r938 | } | ||
qreal QBarSeriesPrivate::max() | ||||
{ | ||||
|
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++) { | ||||
|
r1167 | qreal temp = m_barSets.at(i)->at(j).y(); | ||
|
r1005 | if (temp > max) | ||
max = temp; | ||||
} | ||||
} | ||||
return max; | ||||
|
r938 | } | ||
qreal QBarSeriesPrivate::valueAt(int set, int category) | ||||
{ | ||||
|
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; | ||||
} | ||||
|
r1167 | return m_barSets.at(set)->at(category).y(); | ||
|
r938 | } | ||
qreal QBarSeriesPrivate::percentageAt(int set, int category) | ||||
{ | ||||
|
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; | ||||
} | ||||
|
r1167 | qreal value = m_barSets.at(set)->at(category).y(); | ||
|
r1005 | qreal sum = categorySum(category); | ||
if ( qFuzzyIsNull(sum) ) { | ||||
return 0; | ||||
} | ||||
return value / sum; | ||||
|
r938 | } | ||
qreal QBarSeriesPrivate::categorySum(int category) | ||||
{ | ||||
|
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()) | ||||
|
r1167 | sum += m_barSets.at(set)->at(category).y(); | ||
|
r1005 | } | ||
return sum; | ||||
|
r938 | } | ||
qreal QBarSeriesPrivate::absoluteCategorySum(int category) | ||||
{ | ||||
|
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()) | ||||
|
r1167 | sum += qAbs(m_barSets.at(set)->at(category).y()); | ||
|
r1005 | } | ||
return sum; | ||||
|
r938 | } | ||
qreal QBarSeriesPrivate::maxCategorySum() | ||||
{ | ||||
|
r1005 | qreal max = INT_MIN; | ||
|
r1208 | int count = categoryCount(); | ||
|
r1005 | for (int i = 0; i < count; i++) { | ||
qreal sum = categorySum(i); | ||||
if (sum > max) | ||||
max = sum; | ||||
} | ||||
return max; | ||||
|
r938 | } | ||
void QBarSeriesPrivate::barsetChanged() | ||||
{ | ||||
emit updatedBars(); | ||||
} | ||||
|
r813 | |||
|
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()); | ||||
|
r1208 | qreal x = categoryCount(); | ||
|
r962 | qreal y = max(); | ||
|
r1228 | minX = qMin(minX, x) - 0.5; | ||
|
r943 | minY = qMin(minY, y); | ||
|
r1228 | maxX = qMax(maxX, x) - 0.5; | ||
|
r943 | maxY = qMax(maxY, y); | ||
tickXCount = x+1; | ||||
|
r1078 | domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount); | ||
|
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; | ||||
} | ||||
|
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; | ||||
} | ||||
|
r338 | #include "moc_qbarseries.cpp" | ||
|
r938 | #include "moc_qbarseries_p.cpp" | ||
|
r71 | |||
|
r56 | QTCOMMERCIALCHART_END_NAMESPACE | ||