diff options
Diffstat (limited to 'src/charts/barchart/horizontal/percent')
5 files changed, 412 insertions, 0 deletions
diff --git a/src/charts/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp b/src/charts/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp new file mode 100644 index 00000000..0bf9afbe --- /dev/null +++ b/src/charts/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp @@ -0,0 +1,140 @@ +/**************************************************************************** +** +** 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 "horizontalpercentbarchartitem_p.h" +#include "qabstractbarseries_p.h" +#include "qbarset_p.h" +#include "bar_p.h" + +QT_CHARTS_BEGIN_NAMESPACE + +HorizontalPercentBarChartItem::HorizontalPercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) + : AbstractBarChartItem(series, item) +{ +} + +void HorizontalPercentBarChartItem::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::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) { + topLeft = domain()->calculateGeometryPoint(QPointF(domain()->minX(), category - barWidth / 2), m_validData); + bottomRight = domain()->calculateGeometryPoint(QPointF(domain()->minX(), category + barWidth / 2), m_validData); + } else { + topLeft = domain()->calculateGeometryPoint(QPointF(0, category - barWidth / 2), m_validData); + bottomRight = domain()->calculateGeometryPoint(QPointF(0, category + barWidth / 2), m_validData); + } + + if (!m_validData) + return; + + rect.setTopLeft(topLeft); + rect.setBottomRight(bottomRight); + m_layout.append(rect.normalized()); + } + } +} + +QVector<QRectF> HorizontalPercentBarChartItem::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 sum = 0; + qreal categorySum = m_series->d_func()->categorySum(category); + for (int set = 0; set < setCount; set++) { + qreal value = m_series->barSets().at(set)->at(category); + QRectF rect; + qreal topX = 0; + if (sum > 0) + topX = 100 * sum / categorySum; + qreal bottomX = 0; + qreal newSum = value + sum; + if (newSum > 0) + bottomX = 100 * newSum / categorySum; + QPointF topLeft; + if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) + topLeft = domain()->calculateGeometryPoint(QPointF(set ? topX : domain()->minX(), category - barWidth/2), m_validData); + else + topLeft = domain()->calculateGeometryPoint(QPointF(set ? topX : 0, category - barWidth/2), m_validData); + QPointF bottomRight = domain()->calculateGeometryPoint(QPointF(bottomX, category + barWidth/2), m_validData); + + rect.setTopLeft(topLeft); + rect.setBottomRight(bottomRight); + layout.append(rect.normalized()); + sum = newSum; + } + } + return layout; +} + +void HorizontalPercentBarChartItem::handleUpdatedBars() +{ + // Handle changes in pen, brush, labels etc. + int categoryCount = m_series->d_func()->categoryCount(); + int setCount = m_series->count(); + int itemIndex(0); + static const QString valueTag(QLatin1String("@value")); + + for (int category = 0; category < categoryCount; category++) { + for (int set = 0; set < setCount; set++) { + QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); + Bar *bar = m_bars.at(itemIndex); + bar->setPen(barSet->m_pen); + bar->setBrush(barSet->m_brush); + bar->update(); + + QGraphicsTextItem *label = m_labels.at(itemIndex); + qreal p = m_series->d_func()->percentageAt(set, category) * 100.0; + QString vString(presenter()->numberToString(p, 'f', 0)); + QString valueLabel; + if (m_series->labelsFormat().isEmpty()) { + vString.append(QStringLiteral("%")); + valueLabel = vString; + } else { + valueLabel = m_series->labelsFormat(); + valueLabel.replace(valueTag, vString); + } + label->setHtml(valueLabel); + label->setFont(barSet->m_labelFont); + label->setDefaultTextColor(barSet->m_labelBrush.color()); + label->update(); + itemIndex++; + } + } +} + +#include "moc_horizontalpercentbarchartitem_p.cpp" + +QT_CHARTS_END_NAMESPACE + diff --git a/src/charts/barchart/horizontal/percent/horizontalpercentbarchartitem_p.h b/src/charts/barchart/horizontal/percent/horizontalpercentbarchartitem_p.h new file mode 100644 index 00000000..e24cdcf0 --- /dev/null +++ b/src/charts/barchart/horizontal/percent/horizontalpercentbarchartitem_p.h @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** 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 HORIZONTALPERCENTBARCHARTITEM_P_H +#define HORIZONTALPERCENTBARCHARTITEM_P_H + +#include "abstractbarchartitem_p.h" +#include <QGraphicsItem> + +QT_CHARTS_BEGIN_NAMESPACE + +class HorizontalPercentBarChartItem : public AbstractBarChartItem +{ + Q_OBJECT +public: + HorizontalPercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item = 0); + void handleUpdatedBars(); + +private: + virtual QVector<QRectF> calculateLayout(); + void initializeLayout(); +}; + +QT_CHARTS_END_NAMESPACE + +#endif // HORIZONTALPERCENTBARCHARTITEM_P_H diff --git a/src/charts/barchart/horizontal/percent/qhorizontalpercentbarseries.cpp b/src/charts/barchart/horizontal/percent/qhorizontalpercentbarseries.cpp new file mode 100644 index 00000000..bf915c85 --- /dev/null +++ b/src/charts/barchart/horizontal/percent/qhorizontalpercentbarseries.cpp @@ -0,0 +1,125 @@ +/**************************************************************************** +** +** 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 "qhorizontalpercentbarseries.h" +#include "qhorizontalpercentbarseries_p.h" +#include "horizontalpercentbarchartitem_p.h" + +#include "chartdataset_p.h" +#include "charttheme_p.h" + +QT_CHARTS_BEGIN_NAMESPACE + +/*! + \class QHorizontalPercentBarSeries + \inmodule Qt Charts + \brief Series for creating horizontal percent bar chart. + \mainclass + + QHorizontalPercentBarSeries represents a series of data shown as bars. The purpose of this + class is to draw bars as groups, where bars in same category are grouped next to each other. + QHorizontalPercentBarSeries groups the data from sets to categories, which are defined by a + QStringList. Bars with zero value are not drawn. + + See the \l {HorizontalPercentBarChart Example} {horizontal percent bar chart example} to learn + how to create a horizontal percent bar chart. + \image examples_horizontalpercentbarchart.png + + \sa QBarSet, QBarSeries, QPercentBarSeries, QAbstractBarSeries, QStackedBarSeries, + QHorizontalStackedBarSeries, QHorizontalBarSeries +*/ +/*! + \qmltype HorizontalPercentBarSeries + \instantiates QHorizontalPercentBarSeries + \inqmlmodule QtCharts + + \inherits AbstractBarSeries + + \brief Series type for creating horizontal precent bar chart. + + The following QML shows how to create a simple horizontal percent bar chart: + \snippet qmlchart/qml/qmlchart/View11.qml 1 + \beginfloatleft + \image examples_qmlchart11.png + \endfloat + \clearfloat +*/ + +/*! + Constructs empty QHorizontalPercentBarSeries. + QHorizontalPercentBarSeries is QObject which is a child of a \a parent. +*/ +QHorizontalPercentBarSeries::QHorizontalPercentBarSeries(QObject *parent) : + QAbstractBarSeries(*new QHorizontalPercentBarSeriesPrivate(this), parent) +{ +} + +/*! + Returns QAbstractSeries::SeriesTypeHorizontalPercentBar. +*/ +QAbstractSeries::SeriesType QHorizontalPercentBarSeries::type() const +{ + return QAbstractSeries::SeriesTypeHorizontalPercentBar; +} + +/*! + Destructor. + Removes series from chart. +*/ +QHorizontalPercentBarSeries::~QHorizontalPercentBarSeries() +{ + Q_D(QHorizontalPercentBarSeries); + if (d->m_chart) + d->m_chart->removeSeries(this); +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +QHorizontalPercentBarSeriesPrivate::QHorizontalPercentBarSeriesPrivate(QHorizontalPercentBarSeries *q) : QAbstractBarSeriesPrivate(q) +{ + +} + +void QHorizontalPercentBarSeriesPrivate::initializeDomain() +{ + qreal minX(domain()->minX()); + qreal minY(domain()->minY()); + qreal maxX(domain()->maxX()); + qreal maxY(domain()->maxY()); + + qreal y = categoryCount(); + minX = 0; + maxX = 100; + minY = qMin(minY, - (qreal)0.5); + maxY = qMax(maxY, y - (qreal)0.5); + + domain()->setRange(minX, maxX, minY, maxY); +} + +void QHorizontalPercentBarSeriesPrivate::initializeGraphics(QGraphicsItem* parent) +{ + Q_Q(QHorizontalPercentBarSeries); + HorizontalPercentBarChartItem *bar = new HorizontalPercentBarChartItem(q,parent); + m_item.reset(bar); + QAbstractSeriesPrivate::initializeGraphics(parent); +} + +#include "moc_qhorizontalpercentbarseries.cpp" + +QT_CHARTS_END_NAMESPACE diff --git a/src/charts/barchart/horizontal/percent/qhorizontalpercentbarseries.h b/src/charts/barchart/horizontal/percent/qhorizontalpercentbarseries.h new file mode 100644 index 00000000..0595532b --- /dev/null +++ b/src/charts/barchart/horizontal/percent/qhorizontalpercentbarseries.h @@ -0,0 +1,45 @@ +/**************************************************************************** +** +** 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 QHORIZONTALPERCENTBARSERIES_H +#define QHORIZONTALPERCENTBARSERIES_H + +#include <QtCharts/qabstractbarseries.h> + +QT_CHARTS_BEGIN_NAMESPACE + +class QHorizontalPercentBarSeriesPrivate; + +class QT_CHARTS_EXPORT QHorizontalPercentBarSeries : public QAbstractBarSeries +{ + Q_OBJECT +public: + explicit QHorizontalPercentBarSeries(QObject *parent = 0); + ~QHorizontalPercentBarSeries(); + QAbstractSeries::SeriesType type() const; + +private: + Q_DECLARE_PRIVATE(QHorizontalPercentBarSeries) + Q_DISABLE_COPY(QHorizontalPercentBarSeries) +}; + +QT_CHARTS_END_NAMESPACE + +#endif // QHORIZONTALPERCENTBARSERIES_H diff --git a/src/charts/barchart/horizontal/percent/qhorizontalpercentbarseries_p.h b/src/charts/barchart/horizontal/percent/qhorizontalpercentbarseries_p.h new file mode 100644 index 00000000..079f97bb --- /dev/null +++ b/src/charts/barchart/horizontal/percent/qhorizontalpercentbarseries_p.h @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** 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 QHORIZONTALPERCENTBARSERIES_P_H +#define QHORIZONTALPERCENTBARSERIES_P_H + +#include "qabstractbarseries_p.h" +#include "abstractdomain_p.h" + +QT_CHARTS_BEGIN_NAMESPACE + +class QHorizontalPercentBarSeriesPrivate: public QAbstractBarSeriesPrivate +{ +public: + QHorizontalPercentBarSeriesPrivate(QHorizontalPercentBarSeries *q); + void initializeGraphics(QGraphicsItem* parent); + void initializeDomain(); +private: + Q_DECLARE_PUBLIC(QHorizontalPercentBarSeries) +}; + +QT_CHARTS_END_NAMESPACE + +#endif // QHORIZONTALPERCENTBARSERIES_P_H |