diff options
Diffstat (limited to 'src/charts/barchart/vertical/stacked')
5 files changed, 415 insertions, 0 deletions
diff --git a/src/charts/barchart/vertical/stacked/qstackedbarseries.cpp b/src/charts/barchart/vertical/stacked/qstackedbarseries.cpp new file mode 100644 index 00000000..7a1946f7 --- /dev/null +++ b/src/charts/barchart/vertical/stacked/qstackedbarseries.cpp @@ -0,0 +1,123 @@ +/**************************************************************************** +** +** Copyright (C) 2014 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 Enterprise Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise 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$ +** +****************************************************************************/ + +#include "qstackedbarseries.h" +#include "qstackedbarseries_p.h" +#include "stackedbarchartitem_p.h" +#include "chartdataset_p.h" +#include "charttheme_p.h" +#include "qvalueaxis.h" + +QT_CHARTS_BEGIN_NAMESPACE + +/*! + \class QStackedBarSeries + \inmodule Qt Charts + \brief Series for creating stacked bar chart. + \mainclass + + QStackedBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars + as stacks, where bars in same category are stacked on top of each other. + QStackedBarSeries groups the data from sets to categories, which are defined by QStringList. + + See the \l {StackedbarChart Example} {stacked bar chart example} to learn how to create a stacked bar chart. + \image examples_stackedbarchart.png + + \sa QBarSet, QPercentBarSeries, QAbstractBarSeries +*/ + +/*! + \qmltype StackedBarSeries + \instantiates QStackedBarSeries + \inqmlmodule QtCharts + + \inherits AbstractBarSeries + + \brief Series for creating stacked bar chart. + + The following QML shows how to create a simple stacked bar chart: + \snippet qmlchart/qml/qmlchart/View7.qml 1 + \beginfloatleft + \image examples_qmlchart7.png + \endfloat + \clearfloat +*/ + +/*! + Constructs empty QStackedBarSeries. + QStackedBarSeries is QObject which is a child of a \a parent. +*/ +QStackedBarSeries::QStackedBarSeries(QObject *parent) + : QAbstractBarSeries(*new QStackedBarSeriesPrivate(this), parent) +{ +} + +/*! + Destructor. Removes series from chart. +*/ +QStackedBarSeries::~QStackedBarSeries() +{ + Q_D(QStackedBarSeries); + if (d->m_chart) + d->m_chart->removeSeries(this); +} +/*! + Returns QAbstractSeries::SeriesTypeStackedBar. +*/ +QAbstractSeries::SeriesType QStackedBarSeries::type() const +{ + return QAbstractSeries::SeriesTypeStackedBar; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +QStackedBarSeriesPrivate::QStackedBarSeriesPrivate(QStackedBarSeries *q) : QAbstractBarSeriesPrivate(q) +{ + +} + +void QStackedBarSeriesPrivate::initializeDomain() +{ + qreal minX(domain()->minX()); + qreal minY(domain()->minY()); + qreal maxX(domain()->maxX()); + qreal maxY(domain()->maxY()); + + qreal x = categoryCount(); + minX = qMin(minX, - (qreal)0.5); + minY = qMin(minY, bottom()); + maxX = qMax(maxX, x - (qreal)0.5); + maxY = qMax(maxY, top()); + + domain()->setRange(minX, maxX, minY, maxY); +} + +void QStackedBarSeriesPrivate::initializeGraphics(QGraphicsItem* parent) +{ + Q_Q(QStackedBarSeries); + StackedBarChartItem *bar = new StackedBarChartItem(q,parent); + m_item.reset(bar); + QAbstractSeriesPrivate::initializeGraphics(parent); +} + +#include "moc_qstackedbarseries.cpp" + +QT_CHARTS_END_NAMESPACE + diff --git a/src/charts/barchart/vertical/stacked/qstackedbarseries.h b/src/charts/barchart/vertical/stacked/qstackedbarseries.h new file mode 100644 index 00000000..bab6e174 --- /dev/null +++ b/src/charts/barchart/vertical/stacked/qstackedbarseries.h @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (C) 2014 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 Enterprise Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise 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$ +** +****************************************************************************/ + +#ifndef QSTACKEDBARSERIES_H +#define QSTACKEDBARSERIES_H + +#include <QStringList> +#include <QtCharts/qabstractbarseries.h> + +QT_CHARTS_BEGIN_NAMESPACE + +class QStackedBarSeriesPrivate; + +class QT_CHARTS_EXPORT QStackedBarSeries : public QAbstractBarSeries +{ + Q_OBJECT +public: + explicit QStackedBarSeries(QObject *parent = 0); + ~QStackedBarSeries(); + QAbstractSeries::SeriesType type() const; + +private: + Q_DECLARE_PRIVATE(QStackedBarSeries) + Q_DISABLE_COPY(QStackedBarSeries) +}; + +QT_CHARTS_END_NAMESPACE + +#endif // QSTACKEDBARSERIES_H diff --git a/src/charts/barchart/vertical/stacked/qstackedbarseries_p.h b/src/charts/barchart/vertical/stacked/qstackedbarseries_p.h new file mode 100644 index 00000000..d5cf3c2d --- /dev/null +++ b/src/charts/barchart/vertical/stacked/qstackedbarseries_p.h @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2014 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 Enterprise Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise 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$ +** +****************************************************************************/ + +// W A R N I N G +// ------------- +// +// This file is not part of the Qt Enterprise Chart API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef QSTACKEDBARSERIES_P_H +#define QSTACKEDBARSERIES_P_H + +#include "qabstractbarseries_p.h" +#include "abstractdomain_p.h" + +QT_CHARTS_BEGIN_NAMESPACE + + +class QStackedBarSeriesPrivate: public QAbstractBarSeriesPrivate +{ +public: + QStackedBarSeriesPrivate(QStackedBarSeries *q); + void initializeGraphics(QGraphicsItem* parent); + void initializeDomain(); +private: + Q_DECLARE_PUBLIC(QStackedBarSeries) +}; + +QT_CHARTS_END_NAMESPACE + +#endif diff --git a/src/charts/barchart/vertical/stacked/stackedbarchartitem.cpp b/src/charts/barchart/vertical/stacked/stackedbarchartitem.cpp new file mode 100644 index 00000000..953bb455 --- /dev/null +++ b/src/charts/barchart/vertical/stacked/stackedbarchartitem.cpp @@ -0,0 +1,137 @@ +/**************************************************************************** +** +** Copyright (C) 2014 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 Enterprise Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise 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$ +** +****************************************************************************/ + +#include "stackedbarchartitem_p.h" +#include "bar_p.h" +#include "qbarset_p.h" +#include "qabstractbarseries_p.h" +#include "qbarset.h" + +QT_CHARTS_BEGIN_NAMESPACE + +StackedBarChartItem::StackedBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) : + AbstractBarChartItem(series, item) +{ + connect(series, SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)), + this, SLOT(handleLabelsPositionChanged())); + connect(series, SIGNAL(labelsFormatChanged(QString)), this, SLOT(positionLabels())); +} + +void StackedBarChartItem::initializeLayout() +{ + qreal categoryCount = m_series->d_func()->categoryCount(); + qreal setCount = m_series->count(); + qreal barWidth = m_series->d_func()->barWidth(); + + m_layout.clear(); + for(int category = 0; category < categoryCount; category++) { + for (int set = 0; set < setCount; set++) { + QRectF rect; + QPointF topLeft; + QPointF bottomRight; + + if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) { + topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, domain()->minY()), m_validData); + bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, domain()->minY()), m_validData); + } else { + topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, 0), m_validData); + bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, 0), m_validData); + } + + if (!m_validData) + return; + + rect.setTopLeft(topLeft); + rect.setBottomRight(bottomRight); + m_layout.append(rect.normalized()); + } + } +} + +QVector<QRectF> StackedBarChartItem::calculateLayout() +{ + QVector<QRectF> layout; + // Use temporary qreals for accuracy + qreal categoryCount = m_series->d_func()->categoryCount(); + qreal setCount = m_series->count(); + qreal barWidth = m_series->d_func()->barWidth(); + + for(int category = 0; category < categoryCount; category++) { + qreal positiveSum = 0; + qreal negativeSum = 0; + for (int set = 0; set < setCount; set++) { + qreal value = m_series->barSets().at(set)->at(category); + QRectF rect; + QPointF topLeft; + QPointF bottomRight; + if (value < 0) { + bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, value + negativeSum), m_validData); + if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) + topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? negativeSum : domain()->minY()), m_validData); + else + topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? negativeSum : 0), m_validData); + negativeSum += value; + } else { + topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, value + positiveSum), m_validData); + if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) + bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? positiveSum : domain()->minY()), m_validData); + else + bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? positiveSum : 0), m_validData); + positiveSum += value; + } + + rect.setTopLeft(topLeft); + rect.setBottomRight(bottomRight); + layout.append(rect.normalized()); + } + } + return layout; +} + +void StackedBarChartItem::handleLabelsPositionChanged() +{ + positionLabels(); +} + +void StackedBarChartItem::positionLabels() +{ + for (int i = 0; i < m_layout.count(); i++) { + QGraphicsTextItem *label = m_labels.at(i); + qreal xPos = m_layout.at(i).center().x() - label->boundingRect().center().x(); + qreal yPos = 0; + + int offset = m_bars.at(i)->pen().width() / 2 + 2; + if (m_series->labelsPosition() == QAbstractBarSeries::LabelsCenter) + yPos = m_layout.at(i).center().y() - label->boundingRect().center().y(); + else if (m_series->labelsPosition() == QAbstractBarSeries::LabelsInsideEnd) + yPos = m_layout.at(i).top() - offset; + else if (m_series->labelsPosition() == QAbstractBarSeries::LabelsInsideBase) + yPos = m_layout.at(i).bottom() - label->boundingRect().height() + offset; + else if (m_series->labelsPosition() == QAbstractBarSeries::LabelsOutsideEnd) + yPos = m_layout.at(i).top() - label->boundingRect().height() + offset; + + label->setPos(xPos, yPos); + label->setZValue(zValue() + 1); + } +} + +#include "moc_stackedbarchartitem_p.cpp" + +QT_CHARTS_END_NAMESPACE diff --git a/src/charts/barchart/vertical/stacked/stackedbarchartitem_p.h b/src/charts/barchart/vertical/stacked/stackedbarchartitem_p.h new file mode 100644 index 00000000..f2fa6b73 --- /dev/null +++ b/src/charts/barchart/vertical/stacked/stackedbarchartitem_p.h @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 2014 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 Enterprise Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise 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$ +** +****************************************************************************/ + +// W A R N I N G +// ------------- +// +// This file is not part of the Qt Enterprise Chart API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + + +#ifndef STACKEDBARCHARTITEM_H +#define STACKEDBARCHARTITEM_H + +#include "abstractbarchartitem_p.h" +#include <qstackedbarseries.h> +#include <QGraphicsItem> + +QT_CHARTS_BEGIN_NAMESPACE + +class StackedBarChartItem : public AbstractBarChartItem +{ + Q_OBJECT +public: + StackedBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item =0); + +private slots: + void handleLabelsPositionChanged(); + void positionLabels(); + +private: + virtual QVector<QRectF> calculateLayout(); + void initializeLayout(); + +}; + +QT_CHARTS_END_NAMESPACE + +#endif // STACKEDBARCHARTITEM_H |