qabstractbarseries.cpp
901 lines
| 26.7 KiB
| text/x-c
|
CppLexer
|
r794 | /**************************************************************************** | ||
** | ||||
|
r2432 | ** Copyright (C) 2013 Digia Plc | ||
|
r794 | ** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r1586 | #include "qabstractbarseries.h" | ||
#include "qabstractbarseries_p.h" | ||||
|
r172 | #include "qbarset.h" | ||
|
r938 | #include "qbarset_p.h" | ||
|
r2275 | #include "abstractdomain_p.h" | ||
|
r943 | #include "chartdataset_p.h" | ||
#include "charttheme_p.h" | ||||
|
r1805 | #include "qvalueaxis.h" | ||
|
r1810 | #include "qbarcategoryaxis.h" | ||
|
r2174 | #include "qbarlegendmarker.h" | ||
|
r2316 | #include "baranimation_p.h" | ||
#include "abstractbarchartitem_p.h" | ||||
|
r2174 | |||
|
r56 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
|
r313 | /*! | ||
|
r1586 | \class QAbstractBarSeries | ||
|
r1502 | \brief Series for creating a bar chart | ||
|
r995 | \mainclass | ||
|
r313 | |||
|
r1586 | QAbstractBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to | ||
|
r1208 | 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 | */ | ||
|
r1489 | /*! | ||
|
r1590 | \qmlclass AbstractBarSeries QAbstractBarSeries | ||
|
r1588 | \inherits QAbstractSeries | ||
|
r1521 | |||
The following QML shows how to create a simple bar chart: | ||||
\snippet ../demos/qmlchart/qml/qmlchart/View6.qml 1 | ||||
|
r1489 | |||
\beginfloatleft | ||||
\image demos_qmlchart6.png | ||||
\endfloat | ||||
\clearfloat | ||||
*/ | ||||
|
r313 | |||
|
r2331 | /*! | ||
\qmlproperty AbstractAxis AbstractBarSeries::axisX | ||||
The x axis used for the series. If you leave both axisX and axisXTop undefined, a BarCategoriesAxis is created for | ||||
the series. | ||||
\sa axisXTop | ||||
*/ | ||||
/*! | ||||
\qmlproperty AbstractAxis AbstractBarSeries::axisY | ||||
The y axis used for the series. If you leave both axisY and axisYRight undefined, a ValueAxis is created for | ||||
the series. | ||||
\sa axisYRight | ||||
*/ | ||||
/*! | ||||
\qmlproperty AbstractAxis AbstractBarSeries::axisXTop | ||||
The x axis used for the series, drawn on top of the chart view. Note that you can only provide either axisX or | ||||
axisXTop, but not both. | ||||
\sa axisX | ||||
*/ | ||||
/*! | ||||
\qmlproperty AbstractAxis AbstractBarSeries::axisYRight | ||||
The y axis used for the series, drawn to the right on the chart view. Note that you can only provide either axisY | ||||
or axisYRight, but not both. | ||||
\sa axisY | ||||
*/ | ||||
|
r1361 | /*! | ||
|
r1586 | \property QAbstractBarSeries::barWidth | ||
|
r1489 | The width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars | ||
|
r1425 | is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen | ||
is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis. | ||||
|
r1594 | Note that with QBarSeries this value means the width of one group of bars instead of just one bar. | ||
\sa QBarSeries | ||||
|
r1361 | */ | ||
|
r1489 | /*! | ||
|
r1590 | \qmlproperty real AbstractBarSeries::barWidth | ||
|
r1489 | The width of the bars of the series. The unit of width is the unit of x-axis. The minimum width for bars | ||
is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen | ||||
is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis. | ||||
|
r1594 | Note that with QBarSeries this value means the width of one group of bars instead of just one bar. | ||
|
r1489 | */ | ||
|
r1361 | |||
/*! | ||||
|
r1586 | \property QAbstractBarSeries::count | ||
|
r1489 | Holds the number of sets in series. | ||
*/ | ||||
/*! | ||||
|
r1590 | \qmlproperty int AbstractBarSeries::count | ||
|
r1489 | Holds the number of sets in series. | ||
|
r1361 | */ | ||
/*! | ||||
|
r1586 | \property QAbstractBarSeries::labelsVisible | ||
|
r1489 | Defines the visibility of the labels in series | ||
*/ | ||||
/*! | ||||
|
r1590 | \qmlproperty bool AbstractBarSeries::labelsVisible | ||
|
r1489 | Defines the visibility of the labels in series | ||
|
r1361 | */ | ||
|
r313 | /*! | ||
|
r1586 | \fn void QAbstractBarSeries::clicked(int index, QBarSet *barset) | ||
|
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 | ||||
|
r973 | */ | ||
|
r1489 | /*! | ||
|
r1590 | \qmlsignal AbstractBarSeries::onClicked(int index, BarSet barset) | ||
|
r1489 | The signal is emitted if the user clicks with a mouse on top of BarSet. | ||
Clicked bar inside set is indexed by \a index | ||||
*/ | ||||
|
r973 | |||
|
r980 | /*! | ||
|
r1586 | \fn void QAbstractBarSeries::hovered(bool status, QBarSet* barset) | ||
|
r980 | |||
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. | ||||
*/ | ||||
|
r1489 | /*! | ||
|
r1590 | \qmlsignal AbstractBarSeries::onHovered(bool status, BarSet barset) | ||
|
r1489 | |||
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. | ||||
*/ | ||||
|
r1460 | |||
/*! | ||||
|
r1586 | \fn void QAbstractBarSeries::countChanged() | ||
|
r1489 | This signal is emitted when barset count has been changed, for example by append or remove. | ||
*/ | ||||
/*! | ||||
|
r1590 | \qmlsignal AbstractBarSeries::onCountChanged() | ||
|
r1460 | This signal is emitted when barset count has been changed, for example by append or remove. | ||
*/ | ||||
|
r980 | |||
|
r1361 | /*! | ||
|
r1586 | \fn void QAbstractBarSeries::labelsVisibleChanged() | ||
|
r1489 | This signal is emitted when labels visibility have changed. | ||
|
r1362 | \sa isLabelsVisible(), setLabelsVisible() | ||
|
r1361 | */ | ||
/*! | ||||
|
r1586 | \fn void QAbstractBarSeries::barsetsAdded(QList<QBarSet*> sets) | ||
|
r1361 | This signal is emitted when \a sets have been added to the series. | ||
\sa append(), insert() | ||||
*/ | ||||
|
r1506 | /*! | ||
|
r2115 | \qmlsignal AbstractBarSeries::onBarsetsAdded(BarSet barset) | ||
|
r1506 | Emitted when \a barset has been added to the series. | ||
*/ | ||||
|
r1361 | |||
/*! | ||||
|
r1586 | \fn void QAbstractBarSeries::barsetsRemoved(QList<QBarSet*> sets) | ||
|
r1361 | This signal is emitted when \a sets have been removed from the series. | ||
\sa remove() | ||||
*/ | ||||
|
r1506 | /*! | ||
|
r2115 | \qmlsignal AbstractBarSeries::onBarsetsRemoved(BarSet barset) | ||
|
r1506 | Emitted when \a barset has been removed from the series. | ||
*/ | ||||
|
r1361 | |||
|
r1521 | /*! | ||
|
r1590 | \qmlmethod BarSet AbstractBarSeries::at(int index) | ||
|
r1521 | Returns bar set at \a index. Returns null if the index is not valid. | ||
*/ | ||||
/*! | ||||
|
r1590 | \qmlmethod BarSet AbstractBarSeries::append(string label, VariantList values) | ||
|
r2115 | Adds a new bar set with \a label and \a values to \a index. Values is a list of reals. | ||
|
r1521 | For example: | ||
\code | ||||
myBarSeries.append("set 1", [0, 0.2, 0.2, 0.5, 0.4, 1.5, 0.9]); | ||||
\endcode | ||||
*/ | ||||
/*! | ||||
|
r1590 | \qmlmethod BarSet AbstractBarSeries::insert(int index, string label, VariantList values) | ||
|
r1532 | Inserts a new bar set with \a label and \a values to \a index. Values can be a list of reals or a list of XYPoints. | ||
|
r1521 | If index is zero or smaller, the new barset is prepended. If the index is count or bigger, the new barset is | ||
appended. | ||||
|
r1590 | \sa AbstractBarSeries::append() | ||
|
r1521 | */ | ||
/*! | ||||
|
r1590 | \qmlmethod bool AbstractBarSeries::remove(BarSet barset) | ||
|
r1932 | Removes the barset from the series. Returns true if successful, false otherwise. | ||
|
r1521 | */ | ||
/*! | ||||
|
r1590 | \qmlmethod AbstractBarSeries::clear() | ||
|
r1521 | Removes all barsets from the series. | ||
*/ | ||||
|
r980 | /*! | ||
|
r1590 | Destructs abstractbarseries and owned barsets. | ||
|
r980 | */ | ||
|
r1584 | QAbstractBarSeries::~QAbstractBarSeries() | ||
|
r980 | { | ||
|
r1725 | |||
|
r980 | } | ||
|
r973 | /*! | ||
\internal | ||||
*/ | ||||
|
r2097 | QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &o, QObject *parent) | ||
: QAbstractSeries(o, parent) | ||||
|
r943 | { | ||
|
r2085 | Q_D(QAbstractSeries); | ||
|
r2097 | QObject::connect(this, SIGNAL(countChanged()), d, SIGNAL(countChanged())); | ||
|
r943 | } | ||
|
r1493 | /*! | ||
Sets the width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars | ||||
is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen | ||||
is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis. | ||||
|
r1594 | Note that with \link QBarSeries \endlink this value means the width of one group of bars instead of just one bar. | ||
|
r1493 | */ | ||
|
r1584 | void QAbstractBarSeries::setBarWidth(qreal width) | ||
|
r1283 | { | ||
|
r1584 | Q_D(QAbstractBarSeries); | ||
|
r1425 | d->setBarWidth(width); | ||
|
r1283 | } | ||
|
r1493 | /*! | ||
Returns the width of the bars of the series. | ||||
\sa setBarWidth() | ||||
*/ | ||||
|
r1584 | qreal QAbstractBarSeries::barWidth() const | ||
|
r1284 | { | ||
|
r1584 | Q_D(const QAbstractBarSeries); | ||
|
r1425 | return d->barWidth(); | ||
|
r1284 | } | ||
|
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 | */ | ||
|
r1584 | bool QAbstractBarSeries::append(QBarSet *set) | ||
|
r171 | { | ||
|
r1584 | Q_D(QAbstractBarSeries); | ||
|
r1353 | bool success = d->append(set); | ||
if (success) { | ||||
|
r2104 | QList<QBarSet *> sets; | ||
|
r1353 | sets.append(set); | ||
|
r1787 | set->setParent(this); | ||
|
r1353 | emit barsetsAdded(sets); | ||
|
r1462 | emit countChanged(); | ||
|
r1353 | } | ||
return success; | ||||
|
r171 | } | ||
|
r313 | /*! | ||
|
r1802 | Removes barset from series. Releases ownership of \a set. Deletes the set, if remove | ||
was successful. | ||||
|
r1208 | Returns true, if set was removed. | ||
|
r313 | */ | ||
|
r1584 | bool QAbstractBarSeries::remove(QBarSet *set) | ||
|
r171 | { | ||
|
r1584 | Q_D(QAbstractBarSeries); | ||
|
r1353 | bool success = d->remove(set); | ||
if (success) { | ||||
|
r2104 | QList<QBarSet *> sets; | ||
|
r1353 | sets.append(set); | ||
|
r1787 | set->setParent(0); | ||
|
r1353 | emit barsetsRemoved(sets); | ||
|
r1462 | emit countChanged(); | ||
|
r1802 | delete set; | ||
set = 0; | ||||
} | ||||
return success; | ||||
} | ||||
/*! | ||||
Takes a single \a set from the series. Does not delete the barset object. | ||||
NOTE: The series remains as the barset's parent object. You must set the | ||||
parent object to take full ownership. | ||||
|
r1932 | Returns true if take was successful. | ||
|
r1802 | */ | ||
bool QAbstractBarSeries::take(QBarSet *set) | ||||
{ | ||||
Q_D(QAbstractBarSeries); | ||||
bool success = d->remove(set); | ||||
if (success) { | ||||
|
r2104 | QList<QBarSet *> sets; | ||
|
r1802 | sets.append(set); | ||
emit barsetsRemoved(sets); | ||||
emit countChanged(); | ||||
|
r1353 | } | ||
return success; | ||||
|
r850 | } | ||
/*! | ||||
Adds a list of barsets to series. Takes ownership of \a sets. | ||||
|
r1935 | Returns true, if all sets were appended successfully. If any of the sets is null or is already appended to series, | ||
|
r1208 | 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 | */ | ||
|
r2104 | bool QAbstractBarSeries::append(QList<QBarSet *> sets) | ||
|
r850 | { | ||
|
r1584 | Q_D(QAbstractBarSeries); | ||
|
r1353 | bool success = d->append(sets); | ||
if (success) { | ||||
emit barsetsAdded(sets); | ||||
|
r1462 | emit countChanged(); | ||
|
r1353 | } | ||
return success; | ||||
|
r850 | } | ||
|
r1362 | /*! | ||
Insert a set of bars to series at \a index postion. Takes ownership of \a set. If the set is null or is already in series, it won't be appended. | ||||
Returns true, if inserting succeeded. | ||||
*/ | ||||
|
r1584 | bool QAbstractBarSeries::insert(int index, QBarSet *set) | ||
|
r1362 | { | ||
|
r1584 | Q_D(QAbstractBarSeries); | ||
|
r1362 | bool success = d->insert(index, set); | ||
if (success) { | ||||
|
r2104 | QList<QBarSet *> sets; | ||
|
r1362 | sets.append(set); | ||
emit barsetsAdded(sets); | ||||
|
r1462 | emit countChanged(); | ||
|
r1362 | } | ||
return success; | ||||
} | ||||
|
r1356 | /*! | ||
|
r1802 | Removes all barsets from the series. Deletes removed sets. | ||
|
r1356 | */ | ||
|
r1584 | void QAbstractBarSeries::clear() | ||
|
r1295 | { | ||
|
r1584 | Q_D(QAbstractBarSeries); | ||
|
r1364 | QList<QBarSet *> sets = barSets(); | ||
bool success = d->remove(sets); | ||||
|
r1363 | if (success) { | ||
emit barsetsRemoved(sets); | ||||
|
r1462 | emit countChanged(); | ||
|
r2097 | foreach (QBarSet *set, sets) | ||
|
r1802 | delete set; | ||
|
r1363 | } | ||
|
r1295 | } | ||
|
r313 | /*! | ||
Returns number of sets in series. | ||||
*/ | ||||
|
r1584 | int QAbstractBarSeries::count() const | ||
|
r214 | { | ||
|
r1584 | Q_D(const QAbstractBarSeries); | ||
|
r1005 | return d->m_barSets.count(); | ||
|
r214 | } | ||
|
r313 | /*! | ||
|
r357 | Returns a list of sets in series. Keeps ownership of sets. | ||
*/ | ||||
|
r2104 | QList<QBarSet *> QAbstractBarSeries::barSets() const | ||
|
r214 | { | ||
|
r1584 | Q_D(const QAbstractBarSeries); | ||
|
r1005 | return d->m_barSets; | ||
|
r214 | } | ||
|
r839 | /*! | ||
Sets the visibility of labels in series to \a visible | ||||
*/ | ||||
|
r1584 | void QAbstractBarSeries::setLabelsVisible(bool visible) | ||
|
r813 | { | ||
|
r1584 | Q_D(QAbstractBarSeries); | ||
|
r1246 | if (d->m_labelsVisible != visible) { | ||
|
r1353 | d->setLabelsVisible(visible); | ||
emit labelsVisibleChanged(); | ||||
|
r813 | } | ||
} | ||||
|
r1299 | /*! | ||
Returns the visibility of labels | ||||
*/ | ||||
|
r1584 | bool QAbstractBarSeries::isLabelsVisible() const | ||
|
r1246 | { | ||
|
r1584 | Q_D(const QAbstractBarSeries); | ||
|
r1246 | return d->m_labelsVisible; | ||
} | ||||
|
r938 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
r1584 | QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) : | ||
|
r988 | QAbstractSeriesPrivate(q), | ||
|
r1425 | m_barWidth(0.5), // Default value is 50% of category width | ||
|
r1284 | m_labelsVisible(false), | ||
|
r1899 | m_visible(true) | ||
|
r938 | { | ||
} | ||||
|
r1584 | int QAbstractBarSeriesPrivate::categoryCount() const | ||
|
r1208 | { | ||
// No categories defined. return count of longest set. | ||||
int count = 0; | ||||
|
r2097 | for (int i = 0; i < m_barSets.count(); i++) { | ||
if (m_barSets.at(i)->count() > count) | ||||
|
r1208 | count = m_barSets.at(i)->count(); | ||
} | ||||
return count; | ||||
} | ||||
|
r1584 | void QAbstractBarSeriesPrivate::setBarWidth(qreal width) | ||
|
r1167 | { | ||
|
r2097 | if (width < 0.0) | ||
|
r1425 | width = 0.0; | ||
m_barWidth = width; | ||||
|
r1917 | emit updatedLayout(); | ||
|
r1167 | } | ||
|
r1584 | qreal QAbstractBarSeriesPrivate::barWidth() const | ||
|
r1167 | { | ||
|
r1425 | return m_barWidth; | ||
|
r1167 | } | ||
|
r1112 | |||
|
r2104 | QBarSet *QAbstractBarSeriesPrivate::barsetAt(int index) | ||
|
r938 | { | ||
|
r1005 | return m_barSets.at(index); | ||
|
r938 | } | ||
|
r1584 | void QAbstractBarSeriesPrivate::setVisible(bool visible) | ||
|
r1284 | { | ||
|
r1353 | m_visible = visible; | ||
|
r1917 | emit visibleChanged(); | ||
|
r1284 | } | ||
|
r1584 | void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible) | ||
|
r1284 | { | ||
|
r1353 | m_labelsVisible = visible; | ||
emit labelsVisibleChanged(visible); | ||||
|
r1284 | } | ||
|
r1584 | qreal QAbstractBarSeriesPrivate::min() | ||
|
r938 | { | ||
|
r2097 | if (m_barSets.count() <= 0) | ||
|
r1005 | return 0; | ||
|
r2097 | |||
|
r1005 | 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++) { | ||||
|
r1580 | qreal temp = m_barSets.at(i)->at(j); | ||
|
r1005 | if (temp < min) | ||
min = temp; | ||||
} | ||||
} | ||||
return min; | ||||
|
r938 | } | ||
|
r1584 | qreal QAbstractBarSeriesPrivate::max() | ||
|
r938 | { | ||
|
r2097 | if (m_barSets.count() <= 0) | ||
|
r1005 | return 0; | ||
|
r2097 | |||
|
r1005 | 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++) { | ||||
|
r1580 | qreal temp = m_barSets.at(i)->at(j); | ||
|
r1005 | if (temp > max) | ||
max = temp; | ||||
} | ||||
} | ||||
return max; | ||||
|
r938 | } | ||
|
r1584 | qreal QAbstractBarSeriesPrivate::valueAt(int set, int category) | ||
|
r938 | { | ||
|
r2097 | if ((set < 0) || (set >= m_barSets.count())) | ||
return 0; // No set, no value. | ||||
else if ((category < 0) || (category >= m_barSets.at(set)->count())) | ||||
return 0; // No category, no value. | ||||
|
r1005 | |||
|
r1580 | return m_barSets.at(set)->at(category); | ||
|
r938 | } | ||
|
r1584 | qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category) | ||
|
r938 | { | ||
|
r2097 | if ((set < 0) || (set >= m_barSets.count())) | ||
return 0; // No set, no value. | ||||
else if ((category < 0) || (category >= m_barSets.at(set)->count())) | ||||
return 0; // No category, no value. | ||||
|
r1005 | |||
|
r1580 | qreal value = m_barSets.at(set)->at(category); | ||
|
r1005 | qreal sum = categorySum(category); | ||
|
r2242 | if (qFuzzyCompare(sum, 0)) | ||
|
r1005 | return 0; | ||
return value / sum; | ||||
|
r938 | } | ||
|
r1584 | qreal QAbstractBarSeriesPrivate::categorySum(int category) | ||
|
r938 | { | ||
|
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()) | ||||
|
r1580 | sum += m_barSets.at(set)->at(category); | ||
|
r1005 | } | ||
return sum; | ||||
|
r938 | } | ||
|
r1584 | qreal QAbstractBarSeriesPrivate::absoluteCategorySum(int category) | ||
|
r938 | { | ||
|
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()) | ||||
|
r1580 | sum += qAbs(m_barSets.at(set)->at(category)); | ||
|
r1005 | } | ||
return sum; | ||||
|
r938 | } | ||
|
r1584 | qreal QAbstractBarSeriesPrivate::maxCategorySum() | ||
|
r938 | { | ||
|
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 | } | ||
|
r1584 | qreal QAbstractBarSeriesPrivate::minX() | ||
|
r1423 | { | ||
|
r2097 | if (m_barSets.count() <= 0) | ||
|
r1423 | return 0; | ||
|
r2097 | |||
|
r1423 | 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++) { | ||||
|
r1580 | qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x(); | ||
|
r1423 | if (temp < min) | ||
min = temp; | ||||
} | ||||
} | ||||
return min; | ||||
} | ||||
|
r1584 | qreal QAbstractBarSeriesPrivate::maxX() | ||
|
r1423 | { | ||
|
r2097 | if (m_barSets.count() <= 0) | ||
|
r1423 | return 0; | ||
|
r2097 | |||
|
r1423 | 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++) { | ||||
|
r1580 | qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x(); | ||
|
r1423 | if (temp > max) | ||
max = temp; | ||||
} | ||||
} | ||||
return max; | ||||
} | ||||
|
r1897 | qreal QAbstractBarSeriesPrivate::categoryTop(int category) | ||
{ | ||||
// Returns top (sum of all positive values) of category. | ||||
// Returns 0, if all values are negative | ||||
qreal top(0); | ||||
int count = m_barSets.count(); | ||||
for (int set = 0; set < count; set++) { | ||||
if (category < m_barSets.at(set)->count()) { | ||||
qreal temp = m_barSets.at(set)->at(category); | ||||
if (temp > 0) { | ||||
top += temp; | ||||
} | ||||
} | ||||
} | ||||
return top; | ||||
} | ||||
qreal QAbstractBarSeriesPrivate::categoryBottom(int category) | ||||
{ | ||||
// Returns bottom (sum of all negative values) of category | ||||
// Returns 0, if all values are positive | ||||
qreal bottom(0); | ||||
int count = m_barSets.count(); | ||||
for (int set = 0; set < count; set++) { | ||||
if (category < m_barSets.at(set)->count()) { | ||||
qreal temp = m_barSets.at(set)->at(category); | ||||
if (temp < 0) { | ||||
bottom += temp; | ||||
} | ||||
} | ||||
} | ||||
return bottom; | ||||
} | ||||
qreal QAbstractBarSeriesPrivate::top() | ||||
{ | ||||
// Returns top of all categories | ||||
qreal top(0); | ||||
int count = categoryCount(); | ||||
|
r2097 | for (int i = 0; i < count; i++) { | ||
|
r1897 | qreal temp = categoryTop(i); | ||
|
r2097 | if (temp > top) | ||
|
r1897 | top = temp; | ||
} | ||||
return top; | ||||
} | ||||
qreal QAbstractBarSeriesPrivate::bottom() | ||||
{ | ||||
// Returns bottom of all categories | ||||
qreal bottom(0); | ||||
int count = categoryCount(); | ||||
|
r2097 | for (int i = 0; i < count; i++) { | ||
|
r1897 | qreal temp = categoryBottom(i); | ||
|
r2097 | if (temp < bottom) | ||
|
r1897 | bottom = temp; | ||
} | ||||
return bottom; | ||||
} | ||||
|
r1423 | |||
|
r2273 | void QAbstractBarSeriesPrivate::initializeDomain() | ||
|
r943 | { | ||
|
r2273 | qreal minX(domain()->minX()); | ||
qreal minY(domain()->minY()); | ||||
qreal maxX(domain()->maxX()); | ||||
qreal maxY(domain()->maxY()); | ||||
|
r943 | |||
|
r1423 | qreal seriesMinX = this->minX(); | ||
qreal seriesMaxX = this->maxX(); | ||||
|
r962 | qreal y = max(); | ||
|
r1679 | minX = qMin(minX, seriesMinX - (qreal)0.5); | ||
|
r943 | minY = qMin(minY, y); | ||
|
r1679 | maxX = qMax(maxX, seriesMaxX + (qreal)0.5); | ||
|
r943 | maxY = qMax(maxY, y); | ||
|
r2273 | domain()->setRange(minX, maxX, minY, maxY); | ||
|
r943 | } | ||
|
r2163 | QList<QLegendMarker*> QAbstractBarSeriesPrivate::createLegendMarkers(QLegend* legend) | ||
{ | ||||
|
r2174 | Q_Q(QAbstractBarSeries); | ||
|
r2163 | QList<QLegendMarker*> markers; | ||
|
r2174 | |||
|
r2163 | foreach(QBarSet* set, q->barSets()) { | ||
|
r2174 | QBarLegendMarker* marker = new QBarLegendMarker(q,set,legend); | ||
|
r2163 | markers << marker; | ||
} | ||||
return markers; | ||||
} | ||||
|
r1584 | bool QAbstractBarSeriesPrivate::append(QBarSet *set) | ||
|
r1263 | { | ||
|
r2097 | if ((m_barSets.contains(set)) || (set == 0)) | ||
return false; // Fail if set is already in list or set is null. | ||||
|
r2081 | |||
|
r1263 | m_barSets.append(set); | ||
|
r1917 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); | ||
|
r1339 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); | ||
QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); | ||||
|
r2081 | |||
|
r2097 | emit restructuredBars(); // this notifies barchartitem | ||
|
r1263 | return true; | ||
} | ||||
|
r1584 | bool QAbstractBarSeriesPrivate::remove(QBarSet *set) | ||
|
r1263 | { | ||
|
r2097 | if (!m_barSets.contains(set)) | ||
return false; // Fail if set is not in list | ||||
|
r2081 | |||
|
r1263 | m_barSets.removeOne(set); | ||
|
r1917 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); | ||
|
r1339 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); | ||
QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); | ||||
|
r2081 | |||
|
r2097 | emit restructuredBars(); // this notifies barchartitem | ||
|
r1263 | return true; | ||
} | ||||
|
r2104 | bool QAbstractBarSeriesPrivate::append(QList<QBarSet * > sets) | ||
|
r1263 | { | ||
|
r2097 | foreach (QBarSet *set, sets) { | ||
if ((set == 0) || (m_barSets.contains(set))) | ||||
return false; // Fail if any of the sets is null or is already appended. | ||||
if (sets.count(set) != 1) | ||||
return false; // Also fail if same set is more than once in given list. | ||||
|
r1263 | } | ||
|
r2097 | foreach (QBarSet *set, sets) { | ||
|
r1263 | m_barSets.append(set); | ||
|
r1917 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); | ||
|
r1339 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); | ||
QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); | ||||
|
r1263 | } | ||
|
r2081 | |||
|
r2097 | emit restructuredBars(); // this notifies barchartitem | ||
|
r1263 | return true; | ||
} | ||||
|
r2104 | bool QAbstractBarSeriesPrivate::remove(QList<QBarSet * > sets) | ||
|
r1263 | { | ||
|
r2097 | if (sets.count() == 0) | ||
|
r1371 | return false; | ||
|
r2097 | |||
foreach (QBarSet *set, sets) { | ||||
if ((set == 0) || (!m_barSets.contains(set))) | ||||
return false; // Fail if any of the sets is null or is not in series | ||||
if (sets.count(set) != 1) | ||||
return false; // Also fail if same set is more than once in given list. | ||||
|
r1263 | } | ||
|
r2097 | foreach (QBarSet *set, sets) { | ||
|
r1353 | m_barSets.removeOne(set); | ||
|
r1917 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); | ||
|
r1353 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); | ||
QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); | ||||
|
r1263 | } | ||
|
r1353 | |||
|
r1437 | emit restructuredBars(); // this notifies barchartitem | ||
|
r2173 | |||
|
r1353 | return true; | ||
|
r1263 | } | ||
|
r1584 | bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set) | ||
|
r1362 | { | ||
|
r2097 | if ((m_barSets.contains(set)) || (set == 0)) | ||
return false; // Fail if set is already in list or set is null. | ||||
|
r2081 | |||
|
r1362 | m_barSets.insert(index, set); | ||
|
r1917 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); | ||
|
r1362 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); | ||
QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); | ||||
|
r2081 | |||
|
r1437 | emit restructuredBars(); // this notifies barchartitem | ||
|
r1362 | return true; | ||
} | ||||
|
r2273 | void QAbstractBarSeriesPrivate::initializeAxes() | ||
|
r1588 | { | ||
|
r1707 | Q_Q(QAbstractBarSeries); | ||
|
r2273 | foreach(QAbstractAxis* axis, m_axes) { | ||
if (axis->type() == QAbstractAxis::AxisTypeBarCategory) { | ||||
switch (q->type()) { | ||||
case QAbstractSeries::SeriesTypeHorizontalBar: | ||||
case QAbstractSeries::SeriesTypeHorizontalPercentBar: | ||||
case QAbstractSeries::SeriesTypeHorizontalStackedBar: | ||||
if (axis->orientation() == Qt::Vertical) | ||||
|
r2104 | populateCategories(qobject_cast<QBarCategoryAxis *>(axis)); | ||
|
r2273 | break; | ||
case QAbstractSeries::SeriesTypeBar: | ||||
case QAbstractSeries::SeriesTypePercentBar: | ||||
case QAbstractSeries::SeriesTypeStackedBar: | ||||
if (axis->orientation() == Qt::Horizontal) | ||||
|
r2104 | populateCategories(qobject_cast<QBarCategoryAxis *>(axis)); | ||
|
r2273 | break; | ||
default: | ||||
qWarning() << "Unexpected series type"; | ||||
break; | ||||
} | ||||
|
r1706 | } | ||
|
r1595 | } | ||
|
r1588 | } | ||
|
r1695 | QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const | ||
|
r1588 | { | ||
|
r1707 | Q_Q(const QAbstractBarSeries); | ||
|
r2097 | switch (q->type()) { | ||
case QAbstractSeries::SeriesTypeHorizontalBar: | ||||
case QAbstractSeries::SeriesTypeHorizontalPercentBar: | ||||
case QAbstractSeries::SeriesTypeHorizontalStackedBar: | ||||
if (orientation == Qt::Vertical) | ||||
return QAbstractAxis::AxisTypeBarCategory; | ||||
break; | ||||
case QAbstractSeries::SeriesTypeBar: | ||||
case QAbstractSeries::SeriesTypePercentBar: | ||||
case QAbstractSeries::SeriesTypeStackedBar: | ||||
if (orientation == Qt::Horizontal) | ||||
return QAbstractAxis::AxisTypeBarCategory; | ||||
break; | ||||
default: | ||||
qWarning() << "Unexpected series type"; | ||||
|
r1707 | break; | ||
} | ||||
|
r1818 | return QAbstractAxis::AxisTypeValue; | ||
|
r1707 | |||
|
r1588 | } | ||
|
r2104 | void QAbstractBarSeriesPrivate::populateCategories(QBarCategoryAxis *axis) | ||
|
r1707 | { | ||
QStringList categories; | ||||
|
r2097 | if (axis->categories().isEmpty()) { | ||
for (int i(1); i < categoryCount() + 1; i++) | ||||
categories << QString::number(i); | ||||
|
r1707 | axis->append(categories); | ||
} | ||||
} | ||||
|
r1588 | |||
|
r2273 | QAbstractAxis* QAbstractBarSeriesPrivate::createDefaultAxis(Qt::Orientation orientation) const | ||
{ | ||||
Q_UNUSED(orientation); | ||||
return 0; | ||||
} | ||||
void QAbstractBarSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced) | ||||
{ | ||||
const QList<QGradient> gradients = theme->seriesGradients(); | ||||
QBrush brush; | ||||
QPen pen; | ||||
qreal takeAtPos = 0.5; | ||||
qreal step = 0.2; | ||||
if (m_barSets.count() > 1) { | ||||
step = 1.0 / (qreal) m_barSets.count(); | ||||
if (m_barSets.count() % gradients.count()) | ||||
step *= gradients.count(); | ||||
else | ||||
step *= (gradients.count() - 1); | ||||
} | ||||
for (int i(0); i < m_barSets.count(); i++) { | ||||
int colorIndex = (index + i) % gradients.count(); | ||||
if (i > 0 && i %gradients.count() == 0) { | ||||
// There is no dedicated base color for each sets, generate more colors | ||||
takeAtPos += step; | ||||
if (takeAtPos == 1.0) | ||||
takeAtPos += step; | ||||
takeAtPos -= (int) takeAtPos; | ||||
} | ||||
if (forced || brush == m_barSets.at(i)->brush()) | ||||
m_barSets.at(i)->setBrush(ChartThemeManager::colorAt(gradients.at(colorIndex), takeAtPos)); | ||||
// Pick label color from the opposite end of the gradient. | ||||
// 0.3 as a boundary seems to work well. | ||||
if (forced || brush == m_barSets.at(i)->labelBrush()) { | ||||
if (takeAtPos < 0.3) | ||||
m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 1)); | ||||
else | ||||
m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0)); | ||||
} | ||||
if (forced || pen == m_barSets.at(i)->pen()) { | ||||
QColor c = ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0); | ||||
m_barSets.at(i)->setPen(c); | ||||
} | ||||
} | ||||
} | ||||
|
r2316 | void QAbstractBarSeriesPrivate::initializeAnimations(QChart::AnimationOptions options) | ||
{ | ||||
AbstractBarChartItem *bar = static_cast<AbstractBarChartItem *>(m_item.data()); | ||||
Q_ASSERT(bar); | ||||
if (options.testFlag(QChart::SeriesAnimations)) { | ||||
bar->setAnimation(new BarAnimation(bar)); | ||||
}else{ | ||||
bar->setAnimation(0); | ||||
} | ||||
QAbstractSeriesPrivate::initializeAnimations(options); | ||||
} | ||||
|
r1586 | #include "moc_qabstractbarseries.cpp" | ||
#include "moc_qabstractbarseries_p.cpp" | ||||
|
r71 | |||
|
r56 | QTCOMMERCIALCHART_END_NAMESPACE | ||