qabstractbarseries.cpp
870 lines
| 24.7 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
|
r1586 | #include "qabstractbarseries.h" | ||
#include "qabstractbarseries_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" | ||||
Marek Rosa
|
r1805 | #include "qvalueaxis.h" | ||
Marek Rosa
|
r1810 | #include "qbarcategoryaxis.h" | ||
Michal Klocek
|
r938 | |||
sauimone
|
r56 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
sauimone
|
r313 | /*! | ||
sauimone
|
r1586 | \class QAbstractBarSeries | ||
sauimone
|
r1502 | \brief Series for creating a bar chart | ||
Tero Ahola
|
r995 | \mainclass | ||
sauimone
|
r313 | |||
sauimone
|
r1586 | QAbstractBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to | ||
sauimone
|
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. | ||||
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 | */ | ||
Tero Ahola
|
r1489 | /*! | ||
sauimone
|
r1590 | \qmlclass AbstractBarSeries QAbstractBarSeries | ||
Michal Klocek
|
r1588 | \inherits QAbstractSeries | ||
Tero Ahola
|
r1521 | |||
The following QML shows how to create a simple bar chart: | ||||
\snippet ../demos/qmlchart/qml/qmlchart/View6.qml 1 | ||||
Tero Ahola
|
r1489 | |||
\beginfloatleft | ||||
\image demos_qmlchart6.png | ||||
\endfloat | ||||
\clearfloat | ||||
*/ | ||||
sauimone
|
r313 | |||
Marek Rosa
|
r1361 | /*! | ||
sauimone
|
r1586 | \property QAbstractBarSeries::barWidth | ||
Tero Ahola
|
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 | ||
sauimone
|
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. | ||||
sauimone
|
r1594 | Note that with QBarSeries this value means the width of one group of bars instead of just one bar. | ||
\sa QBarSeries | ||||
Marek Rosa
|
r1361 | */ | ||
Tero Ahola
|
r1489 | /*! | ||
sauimone
|
r1590 | \qmlproperty real AbstractBarSeries::barWidth | ||
Tero Ahola
|
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. | ||||
sauimone
|
r1594 | Note that with QBarSeries this value means the width of one group of bars instead of just one bar. | ||
Tero Ahola
|
r1489 | */ | ||
Marek Rosa
|
r1361 | |||
/*! | ||||
sauimone
|
r1586 | \property QAbstractBarSeries::count | ||
Tero Ahola
|
r1489 | Holds the number of sets in series. | ||
*/ | ||||
/*! | ||||
sauimone
|
r1590 | \qmlproperty int AbstractBarSeries::count | ||
Tero Ahola
|
r1489 | Holds the number of sets in series. | ||
Marek Rosa
|
r1361 | */ | ||
/*! | ||||
sauimone
|
r1586 | \property QAbstractBarSeries::labelsVisible | ||
Tero Ahola
|
r1489 | Defines the visibility of the labels in series | ||
*/ | ||||
/*! | ||||
sauimone
|
r1590 | \qmlproperty bool AbstractBarSeries::labelsVisible | ||
Tero Ahola
|
r1489 | Defines the visibility of the labels in series | ||
Marek Rosa
|
r1361 | */ | ||
sauimone
|
r313 | /*! | ||
sauimone
|
r1586 | \fn void QAbstractBarSeries::clicked(int index, QBarSet *barset) | ||
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 | */ | ||
Tero Ahola
|
r1489 | /*! | ||
sauimone
|
r1590 | \qmlsignal AbstractBarSeries::onClicked(int index, BarSet barset) | ||
Tero Ahola
|
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 | ||||
*/ | ||||
Tero Ahola
|
r973 | |||
sauimone
|
r980 | /*! | ||
sauimone
|
r1586 | \fn void QAbstractBarSeries::hovered(bool status, QBarSet* barset) | ||
sauimone
|
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. | ||||
*/ | ||||
Tero Ahola
|
r1489 | /*! | ||
sauimone
|
r1590 | \qmlsignal AbstractBarSeries::onHovered(bool status, BarSet barset) | ||
Tero Ahola
|
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. | ||||
*/ | ||||
sauimone
|
r1460 | |||
/*! | ||||
sauimone
|
r1586 | \fn void QAbstractBarSeries::countChanged() | ||
Tero Ahola
|
r1489 | This signal is emitted when barset count has been changed, for example by append or remove. | ||
*/ | ||||
/*! | ||||
sauimone
|
r1590 | \qmlsignal AbstractBarSeries::onCountChanged() | ||
sauimone
|
r1460 | This signal is emitted when barset count has been changed, for example by append or remove. | ||
*/ | ||||
sauimone
|
r980 | |||
Marek Rosa
|
r1361 | /*! | ||
sauimone
|
r1586 | \fn void QAbstractBarSeries::labelsVisibleChanged() | ||
Tero Ahola
|
r1489 | This signal is emitted when labels visibility have changed. | ||
Marek Rosa
|
r1362 | \sa isLabelsVisible(), setLabelsVisible() | ||
Marek Rosa
|
r1361 | */ | ||
/*! | ||||
sauimone
|
r1586 | \fn void QAbstractBarSeries::barsetsAdded(QList<QBarSet*> sets) | ||
Marek Rosa
|
r1361 | This signal is emitted when \a sets have been added to the series. | ||
\sa append(), insert() | ||||
*/ | ||||
Tero Ahola
|
r1506 | /*! | ||
sauimone
|
r1590 | \qmlsignal AbstractBarSeries::onAdded(BarSet barset) | ||
Tero Ahola
|
r1506 | Emitted when \a barset has been added to the series. | ||
*/ | ||||
Marek Rosa
|
r1361 | |||
/*! | ||||
sauimone
|
r1586 | \fn void QAbstractBarSeries::barsetsRemoved(QList<QBarSet*> sets) | ||
Marek Rosa
|
r1361 | This signal is emitted when \a sets have been removed from the series. | ||
\sa remove() | ||||
*/ | ||||
Tero Ahola
|
r1506 | /*! | ||
sauimone
|
r1590 | \qmlsignal AbstractBarSeries::onRemoved(BarSet barset) | ||
Tero Ahola
|
r1506 | Emitted when \a barset has been removed from the series. | ||
*/ | ||||
Marek Rosa
|
r1361 | |||
Tero Ahola
|
r1521 | /*! | ||
sauimone
|
r1590 | \qmlmethod BarSet AbstractBarSeries::at(int index) | ||
Tero Ahola
|
r1521 | Returns bar set at \a index. Returns null if the index is not valid. | ||
*/ | ||||
/*! | ||||
sauimone
|
r1590 | \qmlmethod BarSet AbstractBarSeries::append(string label, VariantList values) | ||
Tero Ahola
|
r1532 | Adds 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. | ||
Tero Ahola
|
r1521 | For example: | ||
\code | ||||
myBarSeries.append("set 1", [0, 0.2, 0.2, 0.5, 0.4, 1.5, 0.9]); | ||||
myBarSeries.append("set 2", [Qt.point(0, 1), Qt.point(2, 2.5), Qt.point(3.5, 2.2)]); | ||||
\endcode | ||||
*/ | ||||
/*! | ||||
sauimone
|
r1590 | \qmlmethod BarSet AbstractBarSeries::insert(int index, string label, VariantList values) | ||
Tero Ahola
|
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. | ||
Tero Ahola
|
r1521 | If index is zero or smaller, the new barset is prepended. If the index is count or bigger, the new barset is | ||
appended. | ||||
sauimone
|
r1590 | \sa AbstractBarSeries::append() | ||
Tero Ahola
|
r1521 | */ | ||
/*! | ||||
sauimone
|
r1590 | \qmlmethod bool AbstractBarSeries::remove(BarSet barset) | ||
Jani Honkonen
|
r1932 | Removes the barset from the series. Returns true if successful, false otherwise. | ||
Tero Ahola
|
r1521 | */ | ||
/*! | ||||
sauimone
|
r1590 | \qmlmethod AbstractBarSeries::clear() | ||
Tero Ahola
|
r1521 | Removes all barsets from the series. | ||
*/ | ||||
sauimone
|
r980 | /*! | ||
sauimone
|
r1590 | Destructs abstractbarseries and owned barsets. | ||
sauimone
|
r980 | */ | ||
sauimone
|
r1584 | QAbstractBarSeries::~QAbstractBarSeries() | ||
sauimone
|
r980 | { | ||
Michal Klocek
|
r1725 | |||
sauimone
|
r980 | } | ||
Tero Ahola
|
r973 | /*! | ||
\internal | ||||
*/ | ||||
sauimone
|
r1584 | QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &d, QObject *parent) : | ||
Tero Ahola
|
r988 | QAbstractSeries(d,parent) | ||
Michal Klocek
|
r943 | { | ||
} | ||||
sauimone
|
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. | ||||
sauimone
|
r1594 | Note that with \link QBarSeries \endlink this value means the width of one group of bars instead of just one bar. | ||
sauimone
|
r1493 | */ | ||
sauimone
|
r1584 | void QAbstractBarSeries::setBarWidth(qreal width) | ||
sauimone
|
r1283 | { | ||
sauimone
|
r1584 | Q_D(QAbstractBarSeries); | ||
sauimone
|
r1425 | d->setBarWidth(width); | ||
sauimone
|
r1283 | } | ||
sauimone
|
r1493 | /*! | ||
Returns the width of the bars of the series. | ||||
\sa setBarWidth() | ||||
*/ | ||||
sauimone
|
r1584 | qreal QAbstractBarSeries::barWidth() const | ||
sauimone
|
r1284 | { | ||
sauimone
|
r1584 | Q_D(const QAbstractBarSeries); | ||
sauimone
|
r1425 | return d->barWidth(); | ||
sauimone
|
r1284 | } | ||
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
|
r1584 | bool QAbstractBarSeries::append(QBarSet *set) | ||
sauimone
|
r171 | { | ||
sauimone
|
r1584 | Q_D(QAbstractBarSeries); | ||
sauimone
|
r1353 | bool success = d->append(set); | ||
if (success) { | ||||
QList<QBarSet*> sets; | ||||
sets.append(set); | ||||
sauimone
|
r1787 | set->setParent(this); | ||
sauimone
|
r1353 | emit barsetsAdded(sets); | ||
Tero Ahola
|
r1462 | emit countChanged(); | ||
sauimone
|
r1353 | } | ||
return success; | ||||
sauimone
|
r171 | } | ||
sauimone
|
r313 | /*! | ||
sauimone
|
r1802 | Removes barset from series. Releases ownership of \a set. Deletes the set, if remove | ||
was successful. | ||||
sauimone
|
r1208 | Returns true, if set was removed. | ||
sauimone
|
r313 | */ | ||
sauimone
|
r1584 | bool QAbstractBarSeries::remove(QBarSet *set) | ||
sauimone
|
r171 | { | ||
sauimone
|
r1584 | Q_D(QAbstractBarSeries); | ||
sauimone
|
r1353 | bool success = d->remove(set); | ||
if (success) { | ||||
QList<QBarSet*> sets; | ||||
sets.append(set); | ||||
sauimone
|
r1787 | set->setParent(0); | ||
sauimone
|
r1353 | emit barsetsRemoved(sets); | ||
Tero Ahola
|
r1462 | emit countChanged(); | ||
sauimone
|
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. | ||||
Jani Honkonen
|
r1932 | Returns true if take was successful. | ||
sauimone
|
r1802 | */ | ||
bool QAbstractBarSeries::take(QBarSet *set) | ||||
{ | ||||
Q_D(QAbstractBarSeries); | ||||
bool success = d->remove(set); | ||||
if (success) { | ||||
QList<QBarSet*> sets; | ||||
sets.append(set); | ||||
emit barsetsRemoved(sets); | ||||
emit countChanged(); | ||||
sauimone
|
r1353 | } | ||
return success; | ||||
sauimone
|
r850 | } | ||
/*! | ||||
Adds a list of barsets to series. Takes ownership of \a sets. | ||||
Jani Honkonen
|
r1935 | Returns true, if all sets were appended successfully. If any of the sets is null or is already appended to series, | ||
sauimone
|
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. | ||||
sauimone
|
r850 | */ | ||
sauimone
|
r1584 | bool QAbstractBarSeries::append(QList<QBarSet* > sets) | ||
sauimone
|
r850 | { | ||
sauimone
|
r1584 | Q_D(QAbstractBarSeries); | ||
sauimone
|
r1353 | bool success = d->append(sets); | ||
if (success) { | ||||
emit barsetsAdded(sets); | ||||
Tero Ahola
|
r1462 | emit countChanged(); | ||
sauimone
|
r1353 | } | ||
return success; | ||||
sauimone
|
r850 | } | ||
Marek Rosa
|
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. | ||||
*/ | ||||
sauimone
|
r1584 | bool QAbstractBarSeries::insert(int index, QBarSet *set) | ||
Marek Rosa
|
r1362 | { | ||
sauimone
|
r1584 | Q_D(QAbstractBarSeries); | ||
Marek Rosa
|
r1362 | bool success = d->insert(index, set); | ||
if (success) { | ||||
QList<QBarSet*> sets; | ||||
sets.append(set); | ||||
emit barsetsAdded(sets); | ||||
Tero Ahola
|
r1462 | emit countChanged(); | ||
Marek Rosa
|
r1362 | } | ||
return success; | ||||
} | ||||
Marek Rosa
|
r1356 | /*! | ||
sauimone
|
r1802 | Removes all barsets from the series. Deletes removed sets. | ||
Marek Rosa
|
r1356 | */ | ||
sauimone
|
r1584 | void QAbstractBarSeries::clear() | ||
Marek Rosa
|
r1295 | { | ||
sauimone
|
r1584 | Q_D(QAbstractBarSeries); | ||
Marek Rosa
|
r1364 | QList<QBarSet *> sets = barSets(); | ||
bool success = d->remove(sets); | ||||
Marek Rosa
|
r1363 | if (success) { | ||
emit barsetsRemoved(sets); | ||||
Tero Ahola
|
r1462 | emit countChanged(); | ||
sauimone
|
r1802 | foreach (QBarSet* set, sets) { | ||
delete set; | ||||
} | ||||
Marek Rosa
|
r1363 | } | ||
Marek Rosa
|
r1295 | } | ||
sauimone
|
r313 | /*! | ||
Returns number of sets in series. | ||||
*/ | ||||
sauimone
|
r1584 | int QAbstractBarSeries::count() const | ||
sauimone
|
r214 | { | ||
sauimone
|
r1584 | Q_D(const QAbstractBarSeries); | ||
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
|
r1584 | QList<QBarSet*> QAbstractBarSeries::barSets() const | ||
sauimone
|
r214 | { | ||
sauimone
|
r1584 | Q_D(const QAbstractBarSeries); | ||
sauimone
|
r1005 | return d->m_barSets; | ||
sauimone
|
r214 | } | ||
sauimone
|
r839 | /*! | ||
Sets the visibility of labels in series to \a visible | ||||
*/ | ||||
sauimone
|
r1584 | void QAbstractBarSeries::setLabelsVisible(bool visible) | ||
sauimone
|
r813 | { | ||
sauimone
|
r1584 | Q_D(QAbstractBarSeries); | ||
sauimone
|
r1246 | if (d->m_labelsVisible != visible) { | ||
sauimone
|
r1353 | d->setLabelsVisible(visible); | ||
emit labelsVisibleChanged(); | ||||
sauimone
|
r813 | } | ||
} | ||||
sauimone
|
r1299 | /*! | ||
Returns the visibility of labels | ||||
*/ | ||||
sauimone
|
r1584 | bool QAbstractBarSeries::isLabelsVisible() const | ||
sauimone
|
r1246 | { | ||
sauimone
|
r1584 | Q_D(const QAbstractBarSeries); | ||
sauimone
|
r1246 | return d->m_labelsVisible; | ||
} | ||||
Michal Klocek
|
r938 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
sauimone
|
r1584 | QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) : | ||
Tero Ahola
|
r988 | QAbstractSeriesPrivate(q), | ||
sauimone
|
r1425 | m_barWidth(0.5), // Default value is 50% of category width | ||
sauimone
|
r1284 | m_labelsVisible(false), | ||
sauimone
|
r1899 | m_visible(true) | ||
Michal Klocek
|
r938 | { | ||
} | ||||
sauimone
|
r1584 | int QAbstractBarSeriesPrivate::categoryCount() const | ||
sauimone
|
r1208 | { | ||
// 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
|
r1584 | void QAbstractBarSeriesPrivate::setBarWidth(qreal width) | ||
sauimone
|
r1167 | { | ||
sauimone
|
r1425 | if (width < 0.0) { | ||
width = 0.0; | ||||
sauimone
|
r1167 | } | ||
sauimone
|
r1425 | m_barWidth = width; | ||
sauimone
|
r1917 | emit updatedLayout(); | ||
sauimone
|
r1167 | } | ||
sauimone
|
r1584 | qreal QAbstractBarSeriesPrivate::barWidth() const | ||
sauimone
|
r1167 | { | ||
sauimone
|
r1425 | return m_barWidth; | ||
sauimone
|
r1167 | } | ||
sauimone
|
r1112 | |||
sauimone
|
r1584 | QBarSet* QAbstractBarSeriesPrivate::barsetAt(int index) | ||
Michal Klocek
|
r938 | { | ||
sauimone
|
r1005 | return m_barSets.at(index); | ||
Michal Klocek
|
r938 | } | ||
sauimone
|
r1584 | void QAbstractBarSeriesPrivate::setVisible(bool visible) | ||
sauimone
|
r1284 | { | ||
sauimone
|
r1353 | m_visible = visible; | ||
sauimone
|
r1917 | emit visibleChanged(); | ||
sauimone
|
r1284 | } | ||
sauimone
|
r1584 | void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible) | ||
sauimone
|
r1284 | { | ||
sauimone
|
r1353 | m_labelsVisible = visible; | ||
emit labelsVisibleChanged(visible); | ||||
sauimone
|
r1284 | } | ||
sauimone
|
r1584 | qreal QAbstractBarSeriesPrivate::min() | ||
Michal Klocek
|
r938 | { | ||
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
|
r1580 | qreal temp = m_barSets.at(i)->at(j); | ||
sauimone
|
r1005 | if (temp < min) | ||
min = temp; | ||||
} | ||||
} | ||||
return min; | ||||
Michal Klocek
|
r938 | } | ||
sauimone
|
r1584 | qreal QAbstractBarSeriesPrivate::max() | ||
Michal Klocek
|
r938 | { | ||
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
|
r1580 | qreal temp = m_barSets.at(i)->at(j); | ||
sauimone
|
r1005 | if (temp > max) | ||
max = temp; | ||||
} | ||||
} | ||||
return max; | ||||
Michal Klocek
|
r938 | } | ||
sauimone
|
r1584 | qreal QAbstractBarSeriesPrivate::valueAt(int set, int category) | ||
Michal Klocek
|
r938 | { | ||
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
|
r1580 | return m_barSets.at(set)->at(category); | ||
Michal Klocek
|
r938 | } | ||
sauimone
|
r1584 | qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category) | ||
Michal Klocek
|
r938 | { | ||
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
|
r1580 | qreal value = m_barSets.at(set)->at(category); | ||
sauimone
|
r1005 | qreal sum = categorySum(category); | ||
if ( qFuzzyIsNull(sum) ) { | ||||
return 0; | ||||
} | ||||
return value / sum; | ||||
Michal Klocek
|
r938 | } | ||
sauimone
|
r1584 | qreal QAbstractBarSeriesPrivate::categorySum(int category) | ||
Michal Klocek
|
r938 | { | ||
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
|
r1580 | sum += m_barSets.at(set)->at(category); | ||
sauimone
|
r1005 | } | ||
return sum; | ||||
Michal Klocek
|
r938 | } | ||
sauimone
|
r1584 | qreal QAbstractBarSeriesPrivate::absoluteCategorySum(int category) | ||
Michal Klocek
|
r938 | { | ||
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
|
r1580 | sum += qAbs(m_barSets.at(set)->at(category)); | ||
sauimone
|
r1005 | } | ||
return sum; | ||||
Michal Klocek
|
r938 | } | ||
sauimone
|
r1584 | qreal QAbstractBarSeriesPrivate::maxCategorySum() | ||
Michal Klocek
|
r938 | { | ||
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 | } | ||
sauimone
|
r1584 | qreal QAbstractBarSeriesPrivate::minX() | ||
sauimone
|
r1423 | { | ||
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
|
r1580 | qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x(); | ||
sauimone
|
r1423 | if (temp < min) | ||
min = temp; | ||||
} | ||||
} | ||||
return min; | ||||
} | ||||
sauimone
|
r1584 | qreal QAbstractBarSeriesPrivate::maxX() | ||
sauimone
|
r1423 | { | ||
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
|
r1580 | qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x(); | ||
sauimone
|
r1423 | if (temp > max) | ||
max = temp; | ||||
} | ||||
} | ||||
return max; | ||||
} | ||||
sauimone
|
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(); | ||||
for (int i=0; i<count; i++) { | ||||
qreal temp = categoryTop(i); | ||||
if (temp > top) { | ||||
top = temp; | ||||
} | ||||
} | ||||
return top; | ||||
} | ||||
qreal QAbstractBarSeriesPrivate::bottom() | ||||
{ | ||||
// Returns bottom of all categories | ||||
qreal bottom(0); | ||||
int count = categoryCount(); | ||||
for (int i=0; i<count; i++) { | ||||
qreal temp = categoryBottom(i); | ||||
if (temp < bottom) { | ||||
bottom = temp; | ||||
} | ||||
} | ||||
return bottom; | ||||
} | ||||
sauimone
|
r1423 | |||
sauimone
|
r1584 | void QAbstractBarSeriesPrivate::scaleDomain(Domain& domain) | ||
Michal Klocek
|
r943 | { | ||
qreal minX(domain.minX()); | ||||
qreal minY(domain.minY()); | ||||
qreal maxX(domain.maxX()); | ||||
qreal maxY(domain.maxY()); | ||||
sauimone
|
r1423 | qreal seriesMinX = this->minX(); | ||
qreal seriesMaxX = this->maxX(); | ||||
sauimone
|
r962 | qreal y = max(); | ||
Marek Rosa
|
r1679 | minX = qMin(minX, seriesMinX - (qreal)0.5); | ||
Michal Klocek
|
r943 | minY = qMin(minY, y); | ||
Marek Rosa
|
r1679 | maxX = qMax(maxX, seriesMaxX + (qreal)0.5); | ||
Michal Klocek
|
r943 | maxY = qMax(maxY, y); | ||
Michal Klocek
|
r1698 | domain.setRange(minX,maxX,minY,maxY); | ||
Michal Klocek
|
r943 | } | ||
Michal Klocek
|
r1736 | ChartElement* QAbstractBarSeriesPrivate::createGraphics(ChartPresenter* presenter) | ||
Michal Klocek
|
r943 | { | ||
sauimone
|
r1673 | Q_UNUSED(presenter); | ||
qWarning() << "QAbstractBarSeriesPrivate::createGraphics called"; | ||||
return 0; | ||||
Michal Klocek
|
r943 | } | ||
sauimone
|
r1584 | QList<LegendMarker*> QAbstractBarSeriesPrivate::createLegendMarker(QLegend* legend) | ||
Michal Klocek
|
r950 | { | ||
sauimone
|
r1584 | Q_Q(QAbstractBarSeries); | ||
Michal Klocek
|
r950 | QList<LegendMarker*> markers; | ||
foreach(QBarSet* set, q->barSets()) { | ||||
BarLegendMarker* marker = new BarLegendMarker(q,set,legend); | ||||
markers << marker; | ||||
} | ||||
return markers; | ||||
} | ||||
sauimone
|
r1584 | bool QAbstractBarSeriesPrivate::append(QBarSet *set) | ||
sauimone
|
r1263 | { | ||
sauimone
|
r1584 | Q_Q(QAbstractBarSeries); | ||
sauimone
|
r1263 | if ((m_barSets.contains(set)) || (set == 0)) { | ||
// Fail if set is already in list or set is null. | ||||
return false; | ||||
} | ||||
m_barSets.append(set); | ||||
sauimone
|
r1917 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); | ||
sauimone
|
r1339 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); | ||
QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); | ||||
sauimone
|
r1437 | emit restructuredBars(); // this notifies barchartitem | ||
sauimone
|
r1263 | if (m_dataset) { | ||
m_dataset->updateSeries(q); // this notifies legend | ||||
} | ||||
return true; | ||||
} | ||||
sauimone
|
r1584 | bool QAbstractBarSeriesPrivate::remove(QBarSet *set) | ||
sauimone
|
r1263 | { | ||
sauimone
|
r1584 | Q_Q(QAbstractBarSeries); | ||
sauimone
|
r1263 | if (!m_barSets.contains(set)) { | ||
// Fail if set is not in list | ||||
return false; | ||||
} | ||||
m_barSets.removeOne(set); | ||||
sauimone
|
r1917 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); | ||
sauimone
|
r1339 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); | ||
QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); | ||||
sauimone
|
r1437 | emit restructuredBars(); // this notifies barchartitem | ||
sauimone
|
r1263 | if (m_dataset) { | ||
m_dataset->updateSeries(q); // this notifies legend | ||||
} | ||||
return true; | ||||
} | ||||
sauimone
|
r1584 | bool QAbstractBarSeriesPrivate::append(QList<QBarSet* > sets) | ||
sauimone
|
r1263 | { | ||
sauimone
|
r1584 | Q_Q(QAbstractBarSeries); | ||
sauimone
|
r1263 | 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); | ||||
sauimone
|
r1917 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); | ||
sauimone
|
r1339 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); | ||
QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); | ||||
sauimone
|
r1263 | } | ||
sauimone
|
r1437 | emit restructuredBars(); // this notifies barchartitem | ||
sauimone
|
r1263 | if (m_dataset) { | ||
m_dataset->updateSeries(q); // this notifies legend | ||||
} | ||||
return true; | ||||
} | ||||
sauimone
|
r1584 | bool QAbstractBarSeriesPrivate::remove(QList<QBarSet* > sets) | ||
sauimone
|
r1263 | { | ||
sauimone
|
r1584 | Q_Q(QAbstractBarSeries); | ||
sauimone
|
r1371 | if (sets.count() == 0) { | ||
return false; | ||||
} | ||||
sauimone
|
r1263 | foreach (QBarSet* set, sets) { | ||
sauimone
|
r1353 | if ((set == 0) || (!m_barSets.contains(set))) { | ||
// Fail if any of the sets is null or is not in series | ||||
return false; | ||||
} | ||||
if (sets.count(set) != 1) { | ||||
// Also fail if same set is more than once in given list. | ||||
return false; | ||||
sauimone
|
r1263 | } | ||
} | ||||
sauimone
|
r1353 | foreach (QBarSet* set, sets) { | ||
m_barSets.removeOne(set); | ||||
sauimone
|
r1917 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); | ||
sauimone
|
r1353 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); | ||
QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); | ||||
sauimone
|
r1263 | } | ||
sauimone
|
r1353 | |||
sauimone
|
r1437 | emit restructuredBars(); // this notifies barchartitem | ||
sauimone
|
r1353 | if (m_dataset) { | ||
m_dataset->updateSeries(q); // this notifies legend | ||||
} | ||||
return true; | ||||
sauimone
|
r1263 | } | ||
sauimone
|
r1584 | bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set) | ||
Marek Rosa
|
r1362 | { | ||
sauimone
|
r1584 | Q_Q(QAbstractBarSeries); | ||
Marek Rosa
|
r1362 | if ((m_barSets.contains(set)) || (set == 0)) { | ||
// Fail if set is already in list or set is null. | ||||
return false; | ||||
} | ||||
m_barSets.insert(index, set); | ||||
sauimone
|
r1917 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); | ||
Marek Rosa
|
r1362 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); | ||
QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); | ||||
sauimone
|
r1437 | emit restructuredBars(); // this notifies barchartitem | ||
Marek Rosa
|
r1362 | if (m_dataset) { | ||
m_dataset->updateSeries(q); // this notifies legend | ||||
} | ||||
return true; | ||||
} | ||||
Michal Klocek
|
r1695 | void QAbstractBarSeriesPrivate::initializeAxis(QAbstractAxis* axis) | ||
Michal Klocek
|
r1588 | { | ||
Michal Klocek
|
r1707 | Q_Q(QAbstractBarSeries); | ||
Marek Rosa
|
r1818 | if(axis->type()==QAbstractAxis::AxisTypeBarCategory) { | ||
Michal Klocek
|
r1707 | |||
switch(q->type()) { | ||||
case QAbstractSeries::SeriesTypeHorizontalBar: | ||||
case QAbstractSeries::SeriesTypeHorizontalPercentBar: | ||||
case QAbstractSeries::SeriesTypeHorizontalStackedBar: { | ||||
if(axis->orientation()==Qt::Vertical) | ||||
{ | ||||
Marek Rosa
|
r1808 | populateCategories(qobject_cast<QBarCategoryAxis*>(axis)); | ||
Michal Klocek
|
r1707 | } | ||
break; | ||||
} | ||||
case QAbstractSeries::SeriesTypeBar: | ||||
case QAbstractSeries::SeriesTypePercentBar: | ||||
case QAbstractSeries::SeriesTypeStackedBar: { | ||||
if(axis->orientation()==Qt::Horizontal) | ||||
{ | ||||
Marek Rosa
|
r1808 | populateCategories(qobject_cast<QBarCategoryAxis*>(axis)); | ||
Michal Klocek
|
r1707 | } | ||
break; | ||||
} | ||||
default: | ||||
qWarning()<<"Unexpected series type"; | ||||
break; | ||||
Michal Klocek
|
r1706 | } | ||
Michal Klocek
|
r1595 | } | ||
Michal Klocek
|
r1588 | } | ||
Michal Klocek
|
r1695 | QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const | ||
Michal Klocek
|
r1588 | { | ||
Michal Klocek
|
r1707 | Q_Q(const QAbstractBarSeries); | ||
switch(q->type()) { | ||||
case QAbstractSeries::SeriesTypeHorizontalBar: | ||||
case QAbstractSeries::SeriesTypeHorizontalPercentBar: | ||||
case QAbstractSeries::SeriesTypeHorizontalStackedBar: { | ||||
if(orientation==Qt::Vertical) | ||||
{ | ||||
Marek Rosa
|
r1818 | return QAbstractAxis::AxisTypeBarCategory; | ||
Michal Klocek
|
r1707 | } | ||
break; | ||||
} | ||||
case QAbstractSeries::SeriesTypeBar: | ||||
case QAbstractSeries::SeriesTypePercentBar: | ||||
case QAbstractSeries::SeriesTypeStackedBar: { | ||||
if(orientation==Qt::Horizontal) | ||||
{ | ||||
Marek Rosa
|
r1818 | return QAbstractAxis::AxisTypeBarCategory; | ||
Michal Klocek
|
r1707 | } | ||
break; | ||||
} | ||||
default: | ||||
qWarning()<<"Unexpected series type"; | ||||
break; | ||||
} | ||||
Marek Rosa
|
r1818 | return QAbstractAxis::AxisTypeValue; | ||
Michal Klocek
|
r1707 | |||
Michal Klocek
|
r1588 | } | ||
Marek Rosa
|
r1808 | void QAbstractBarSeriesPrivate::populateCategories(QBarCategoryAxis* axis) | ||
Michal Klocek
|
r1707 | { | ||
QStringList categories; | ||||
if(axis->categories().isEmpty()) { | ||||
for (int i(1); i < categoryCount()+1; i++) | ||||
categories << QString::number(i); | ||||
axis->append(categories); | ||||
} | ||||
} | ||||
Michal Klocek
|
r1588 | |||
sauimone
|
r1586 | #include "moc_qabstractbarseries.cpp" | ||
#include "moc_qabstractbarseries_p.cpp" | ||||
sauimone
|
r71 | |||
Michal Klocek
|
r1588 | |||
sauimone
|
r56 | QTCOMMERCIALCHART_END_NAMESPACE | ||