diff options
author | Michal Klocek <michal.klocek@digia.com> | 2012-07-17 12:40:05 +0300 |
---|---|---|
committer | Michal Klocek <michal.klocek@digia.com> | 2012-07-17 12:40:05 +0300 |
commit | a9fe09868de948d3a028f8b050b80aa8fe581b84 (patch) | |
tree | 45ea6f68c3fec6f1bfcf5a987a1fb1884b22aec4 /src/barchart/vertical | |
parent | 368f6161c675c32d722f048a1ee12db1d4c3a085 (diff) |
Refactor barchart
* Move implmentation to specific directories
* Implement axis intialization and default only in base class since code is very trival
Diffstat (limited to 'src/barchart/vertical')
19 files changed, 1129 insertions, 0 deletions
diff --git a/src/barchart/vertical/bar/bar.pri b/src/barchart/vertical/bar/bar.pri new file mode 100644 index 00000000..9d63dfb4 --- /dev/null +++ b/src/barchart/vertical/bar/bar.pri @@ -0,0 +1,13 @@ +INCLUDEPATH += $$PWD +DEPENDPATH += $$PWD + +SOURCES += \ + $$PWD/barchartitem.cpp \ + $$PWD/qbarseries.cpp + +PRIVATE_HEADERS += \ + $$PWD/barchartitem_p.h \ + $$PWD/qbarseries_p.h + +PUBLIC_HEADERS += \ + $$PWD/qbarseries.h diff --git a/src/barchart/vertical/bar/barchartitem.cpp b/src/barchart/vertical/bar/barchartitem.cpp new file mode 100644 index 00000000..9625c032 --- /dev/null +++ b/src/barchart/vertical/bar/barchartitem.cpp @@ -0,0 +1,96 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "barchartitem_p.h" +#include "bar_p.h" +#include "qbarset_p.h" +#include "qabstractbarseries_p.h" +#include "qbarset.h" +#include "qbarset_p.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +BarChartItem::BarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter) : + AbstractBarChartItem(series, presenter) +{ +} + +QVector<QRectF> BarChartItem::calculateLayout() +{ + QVector<QRectF> layout; + + // Use temporary qreals for accuracy + qreal categoryCount = m_series->d_func()->categoryCount(); + qreal setCount = m_series->count(); + bool barsVisible = m_series->isVisible(); + + // Domain: + qreal width = geometry().width(); + qreal height = geometry().height(); + qreal rangeY = m_domainMaxY - m_domainMinY; + qreal rangeX = m_domainMaxX - m_domainMinX; + qreal scaleY = (height / rangeY); + qreal scaleX = (width / rangeX); + qreal barWidth = (scaleX / setCount) * m_series->d_func()->barWidth(); + + int itemIndex(0); + for (int category = 0; category < categoryCount; category++) { + qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y(); + for (int set = 0; set < setCount; set++) { + QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); + + qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + m_rect.left(); + xPos -= setCount*barWidth/2; + xPos += set*barWidth; + qreal barHeight = barSet->value(category) * scaleY; + Bar* bar = m_bars.at(itemIndex); + + QRectF rect(xPos, yPos - barHeight, barWidth, barHeight); + layout.append(rect); + bar->setPen(barSet->m_pen); + bar->setBrush(barSet->m_brush); + if (qFuzzyIsNull(barHeight)) { + bar->setVisible(false); + } else { + bar->setVisible(barsVisible); + } + + QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); + + if (!qFuzzyIsNull(barSet->value(category))) { + label->setText(QString::number(barSet->value(category))); + } else { + label->setText(QString("")); + } + + label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) + ,yPos - barHeight/2 - label->boundingRect().height()/2); + label->setFont(barSet->m_labelFont); + label->setBrush(barSet->m_labelBrush); + + itemIndex++; + } + } + return layout; +} + +#include "moc_barchartitem_p.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/vertical/bar/barchartitem_p.h b/src/barchart/vertical/bar/barchartitem_p.h new file mode 100644 index 00000000..b4e0142a --- /dev/null +++ b/src/barchart/vertical/bar/barchartitem_p.h @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +// W A R N I N G +// ------------- +// +// This file is not part of the QtCommercial 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 BARCHARTITEM_H +#define BARCHARTITEM_H + +#include "abstractbarchartitem_p.h" +#include "qstackedbarseries.h" +#include <QGraphicsItem> + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class BarChartItem : public AbstractBarChartItem +{ + Q_OBJECT +public: + BarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter); + +private: + virtual QVector<QRectF> calculateLayout(); +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // BARCHARTITEM_H diff --git a/src/barchart/vertical/bar/qbarseries.cpp b/src/barchart/vertical/bar/qbarseries.cpp new file mode 100644 index 00000000..dac5043b --- /dev/null +++ b/src/barchart/vertical/bar/qbarseries.cpp @@ -0,0 +1,117 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "qbarseries.h" +#include "qbarseries_p.h" +#include "barchartitem_p.h" +#include "chartdataset_p.h" +#include "charttheme_p.h" +#include "chartanimator_p.h" +#include "baranimation_p.h" +#include "qvaluesaxis.h" +#include "qbarcategoriesaxis.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +/*! + \class QBarSeries + \brief Series for creating bar chart + \mainclass + + QBarSeries 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. QBarSeries groups the data + from sets to categories, which are defined by a QStringList. + + See the \l {BarChart Example} {bar chart example} to learn how to create a grouped bar chart. + \image examples_barchart.png + + \sa QBarSet, QPercentBarSeries, QAbstractBarSeries, QStackedBarSeries +*/ +/*! + \qmlclass BarSeries QBarSeries + \inherits AbstractBarSeries + + The following QML shows how to create a simple grouped bar chart: + \snippet ../demos/qmlchart/qml/qmlchart/View6.qml 1 + \beginfloatleft + \image demos_qmlchart6.png + \endfloat + \clearfloat +*/ + +/*! + Constructs empty QBarSeries. + QBarSeries is QObject which is a child of a \a parent. +*/ +QBarSeries::QBarSeries(QObject *parent) + : QAbstractBarSeries(*new QBarSeriesPrivate(this), parent) +{ +} + +/*! + Returns QChartSeries::SeriesTypeBar. +*/ +QAbstractSeries::SeriesType QBarSeries::type() const +{ + return QAbstractSeries::SeriesTypeBar; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) : QAbstractBarSeriesPrivate(q) +{ + +} + +void QBarSeriesPrivate::scaleDomain(Domain& domain) +{ + qreal minX(domain.minX()); + qreal minY(domain.minY()); + qreal maxX(domain.maxX()); + qreal maxY(domain.maxY()); + + qreal x = categoryCount(); + qreal y = max(); + minX = qMin(minX, - (qreal)0.5); + minY = qMin(minY, y); + maxX = qMax(maxX, x - (qreal)0.5); + maxY = qMax(maxY, y); + + domain.setRange(minX,maxX,minY,maxY); +} + + +Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter) +{ + Q_Q(QBarSeries); + + BarChartItem* bar = new BarChartItem(q,presenter); + if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { + bar->setAnimator(presenter->animator()); + bar->setAnimation(new BarAnimation(bar)); + } + presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); + return bar; +} + +#include "moc_qbarseries.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE + diff --git a/src/barchart/vertical/bar/qbarseries.h b/src/barchart/vertical/bar/qbarseries.h new file mode 100644 index 00000000..8435f07d --- /dev/null +++ b/src/barchart/vertical/bar/qbarseries.h @@ -0,0 +1,44 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef QBARSERIES_H +#define QBARSERIES_H + +#include <qabstractbarseries.h> + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QBarSeriesPrivate; + +class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractBarSeries +{ + Q_OBJECT +public: + explicit QBarSeries(QObject *parent = 0); + QAbstractSeries::SeriesType type() const; + +private: + Q_DECLARE_PRIVATE(QBarSeries) + Q_DISABLE_COPY(QBarSeries) +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // QBARSERIES_H diff --git a/src/barchart/vertical/bar/qbarseries_p.h b/src/barchart/vertical/bar/qbarseries_p.h new file mode 100644 index 00000000..8c669102 --- /dev/null +++ b/src/barchart/vertical/bar/qbarseries_p.h @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +// W A R N I N G +// ------------- +// +// This file is not part of the QtCommercial 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 QBARSERIES_P_H +#define QBARSERIES_P_H + +#include "qabstractbarseries_p.h" +#include "domain_p.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + + +class QBarSeriesPrivate: public QAbstractBarSeriesPrivate +{ +public: + QBarSeriesPrivate(QBarSeries* q); + Chart* createGraphics(ChartPresenter* presenter); + void scaleDomain(Domain& domain); +private: + Q_DECLARE_PUBLIC(QBarSeries) +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // QBARSERIES_P_H diff --git a/src/barchart/vertical/percent/percent.pri b/src/barchart/vertical/percent/percent.pri new file mode 100644 index 00000000..8fac3c70 --- /dev/null +++ b/src/barchart/vertical/percent/percent.pri @@ -0,0 +1,14 @@ +INCLUDEPATH += $$PWD +DEPENDPATH += $$PWD + +SOURCES += \ + $$PWD/percentbarchartitem.cpp \ + $$PWD/qpercentbarseries.cpp + + +PRIVATE_HEADERS += \ + $$PWD/percentbarchartitem_p.h \ + $$PWD/qpercentbarseries_p.h + +PUBLIC_HEADERS += \ + $$PWD/qpercentbarseries.h diff --git a/src/barchart/vertical/percent/percentbarchartitem.cpp b/src/barchart/vertical/percent/percentbarchartitem.cpp new file mode 100644 index 00000000..ad26814e --- /dev/null +++ b/src/barchart/vertical/percent/percentbarchartitem.cpp @@ -0,0 +1,102 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "percentbarchartitem_p.h" +#include "bar_p.h" +#include "qabstractbarseries_p.h" +#include "qbarset.h" +#include "chartanimator_p.h" +#include "qbarset_p.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +PercentBarChartItem::PercentBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter) : + AbstractBarChartItem(series, presenter) +{ +} + +QVector<QRectF> PercentBarChartItem::calculateLayout() +{ + QVector<QRectF> layout; + + // Use temporary qreals for accuracy + qreal categoryCount = m_series->d_func()->categoryCount(); + qreal setCount = m_series->count(); + bool barsVisible = m_series->isVisible(); + + // Domain: + qreal width = geometry().width(); + qreal height = geometry().height(); + qreal rangeY = m_domainMaxY - m_domainMinY; + qreal rangeX = m_domainMaxX - m_domainMinX; + qreal scaleY = (height / rangeY); + qreal scaleX = (width / rangeX); + qreal barWidth = scaleX * m_series->d_func()->barWidth(); + + int itemIndex(0); + for (int category = 0; category < categoryCount; category++) { + qreal colSum = m_series->d_func()->categorySum(category); + qreal percentage = (100 / colSum); + qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y(); + for (int set=0; set < setCount; set++) { + QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); + + qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + m_rect.left() - barWidth/2; + + qreal barHeight = barSet->value(category) * percentage * scaleY; + Bar* bar = m_bars.at(itemIndex); + bar->setPen(barSet->m_pen); + bar->setBrush(barSet->m_brush); + if (qFuzzyIsNull(barHeight)) { + bar->setVisible(false); + } else { + bar->setVisible(barsVisible); + } + + QRectF rect(xPos, yPos-barHeight, barWidth, barHeight); + layout.append(rect); + + QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); + + if (!qFuzzyIsNull(m_series->d_func()->valueAt(set,category))) { + int p = m_series->d_func()->percentageAt(set,category) * 100; + QString vString(QString::number(p)); + vString.truncate(3); + vString.append("%"); + label->setText(vString); + } else { + label->setText(QString("")); + } + + label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) + ,yPos - barHeight/2 - label->boundingRect().height()/2); + label->setFont(barSet->m_labelFont); + label->setBrush(barSet->m_labelBrush); + + itemIndex++; + yPos -= barHeight; + } + } + return layout; +} + +#include "moc_percentbarchartitem_p.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/vertical/percent/percentbarchartitem_p.h b/src/barchart/vertical/percent/percentbarchartitem_p.h new file mode 100644 index 00000000..6cefd54a --- /dev/null +++ b/src/barchart/vertical/percent/percentbarchartitem_p.h @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +// W A R N I N G +// ------------- +// +// This file is not part of the QtCommercial 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 PERCENTBARCHARTITEM_H +#define PERCENTBARCHARTITEM_H + +#include "abstractbarchartitem_p.h" +#include <QGraphicsItem> + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QAbstractBarSeries; + +class PercentBarChartItem : public AbstractBarChartItem +{ + Q_OBJECT +public: + PercentBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter); + +private: + virtual QVector<QRectF> calculateLayout(); +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // PERCENTBARCHARTITEM_H diff --git a/src/barchart/vertical/percent/qpercentbarseries.cpp b/src/barchart/vertical/percent/qpercentbarseries.cpp new file mode 100644 index 00000000..398778bc --- /dev/null +++ b/src/barchart/vertical/percent/qpercentbarseries.cpp @@ -0,0 +1,115 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "qpercentbarseries.h" +#include "qpercentbarseries_p.h" +#include "percentbarchartitem_p.h" +#include "chartdataset_p.h" +#include "charttheme_p.h" +#include "chartanimator_p.h" +#include "qvaluesaxis.h" +#include "percentbaranimation_p.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +/*! + \class QPercentBarSeries + \brief Series for creating percent bar chart + \mainclass + + QPercentBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars + as stacks, where each bar is shown as percentage of all bars in that category. + QPercentBarSeries groups the data from sets to categories, which are defined by a QStringList. + + See the \l {PercentbarChart Example} {percent bar chart example} to learn how to create a percent bar chart. + \image examples_percentbarchart.png + + \sa QBarSet, QStackedBarSeries, QAbstractBarSeries +*/ +/*! + \qmlclass PercentBarSeries QPercentBarSeries + \inherits QAbstractBarSeries + + The following QML shows how to create a simple percent bar chart: + \snippet ../demos/qmlchart/qml/qmlchart/View8.qml 1 + \beginfloatleft + \image demos_qmlchart8.png + \endfloat + \clearfloat +*/ + +/*! + Constructs empty QPercentBarSeries. + QPercentBarSeries is QObject which is a child of a \a parent. +*/ +QPercentBarSeries::QPercentBarSeries(QObject *parent) + : QAbstractBarSeries(*new QPercentBarSeriesPrivate(this), parent) +{ +} + +/*! + Returns QChartSeries::SeriesTypePercentBar. +*/ +QAbstractSeries::SeriesType QPercentBarSeries::type() const +{ + return QAbstractSeries::SeriesTypePercentBar; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +QPercentBarSeriesPrivate::QPercentBarSeriesPrivate(QPercentBarSeries *q) : QAbstractBarSeriesPrivate(q) +{ + +} + +void QPercentBarSeriesPrivate::scaleDomain(Domain& domain) +{ + 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); + maxX = qMax(maxX, x - (qreal)0.5); + minY = 0; + maxY = 100; + + domain.setRange(minX,maxX,minY,maxY); +} + + +Chart* QPercentBarSeriesPrivate::createGraphics(ChartPresenter* presenter) +{ + Q_Q(QPercentBarSeries); + + PercentBarChartItem* bar = new PercentBarChartItem(q,presenter); + if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { + bar->setAnimator(presenter->animator()); + bar->setAnimation(new PercentBarAnimation(bar)); + } + presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); + return bar; +} + +#include "moc_qpercentbarseries.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE + diff --git a/src/barchart/vertical/percent/qpercentbarseries.h b/src/barchart/vertical/percent/qpercentbarseries.h new file mode 100644 index 00000000..1045c7a8 --- /dev/null +++ b/src/barchart/vertical/percent/qpercentbarseries.h @@ -0,0 +1,45 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef PERCENTBARSERIES_H +#define PERCENTBARSERIES_H + +#include <QStringList> +#include <qabstractbarseries.h> + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QPercentBarSeriesPrivate; + +class QTCOMMERCIALCHART_EXPORT QPercentBarSeries : public QAbstractBarSeries +{ + Q_OBJECT +public: + explicit QPercentBarSeries(QObject *parent = 0); + QAbstractSeries::SeriesType type() const; + +private: + Q_DECLARE_PRIVATE(QPercentBarSeries) + Q_DISABLE_COPY(QPercentBarSeries) +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // PERCENTBARSERIES_H diff --git a/src/barchart/vertical/percent/qpercentbarseries_p.h b/src/barchart/vertical/percent/qpercentbarseries_p.h new file mode 100644 index 00000000..88ba6938 --- /dev/null +++ b/src/barchart/vertical/percent/qpercentbarseries_p.h @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +// W A R N I N G +// ------------- +// +// This file is not part of the QtCommercial 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 QPERCENTBARSERIES_P_H +#define QPERCENTBARSERIES_P_H + +#include "qabstractbarseries_p.h" +#include "domain_p.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + + +class QPercentBarSeriesPrivate: public QAbstractBarSeriesPrivate +{ +public: + QPercentBarSeriesPrivate(QPercentBarSeries* q); + void scaleDomain(Domain& domain); + Chart* createGraphics(ChartPresenter* presenter); +private: + Q_DECLARE_PUBLIC(QPercentBarSeries) +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif diff --git a/src/barchart/vertical/stacked/qstackedbarseries.cpp b/src/barchart/vertical/stacked/qstackedbarseries.cpp new file mode 100644 index 00000000..28290a03 --- /dev/null +++ b/src/barchart/vertical/stacked/qstackedbarseries.cpp @@ -0,0 +1,117 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "qstackedbarseries.h" +#include "qstackedbarseries_p.h" +#include "stackedbarchartitem_p.h" +#include "chartdataset_p.h" +#include "charttheme_p.h" +#include "chartanimator_p.h" +#include "qvaluesaxis.h" +#include "stackedbaranimation_p.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +/*! + \class QStackedBarSeries + \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 +*/ + +/*! + \qmlclass StackedBarSeries QStackedBarSeries + \inherits AbstractBarSeries + + The following QML shows how to create a simple stacked bar chart: + \snippet ../demos/qmlchart/qml/qmlchart/View7.qml 1 + \beginfloatleft + \image demos_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) +{ +} + +/*! + Returns QChartSeries::SeriesTypeStackedBar. +*/ +QAbstractSeries::SeriesType QStackedBarSeries::type() const +{ + return QAbstractSeries::SeriesTypeStackedBar; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +QStackedBarSeriesPrivate::QStackedBarSeriesPrivate(QStackedBarSeries *q) : QAbstractBarSeriesPrivate(q) +{ + +} + +void QStackedBarSeriesPrivate::scaleDomain(Domain& domain) +{ + qreal minX(domain.minX()); + qreal minY(domain.minY()); + qreal maxX(domain.maxX()); + qreal maxY(domain.maxY()); + + qreal x = categoryCount(); + qreal y = maxCategorySum(); + minX = qMin(minX, - (qreal)0.5); + minY = qMin(minY, y); + maxX = qMax(maxX, x - (qreal)0.5); + maxY = qMax(maxY, y); + + domain.setRange(minX,maxX,minY,maxY); +} + + +Chart* QStackedBarSeriesPrivate::createGraphics(ChartPresenter* presenter) +{ + Q_Q(QStackedBarSeries); + + StackedBarChartItem* bar = new StackedBarChartItem(q,presenter); + if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { + bar->setAnimator(presenter->animator()); + bar->setAnimation(new StackedBarAnimation(bar)); + } + presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); + return bar; +} + +#include "moc_qstackedbarseries.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE + diff --git a/src/barchart/vertical/stacked/qstackedbarseries.h b/src/barchart/vertical/stacked/qstackedbarseries.h new file mode 100644 index 00000000..b5fae6c2 --- /dev/null +++ b/src/barchart/vertical/stacked/qstackedbarseries.h @@ -0,0 +1,45 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef STACKEDBARSERIES_H +#define STACKEDBARSERIES_H + +#include <QStringList> +#include <qabstractbarseries.h> + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QStackedBarSeriesPrivate; + +class QTCOMMERCIALCHART_EXPORT QStackedBarSeries : public QAbstractBarSeries +{ + Q_OBJECT +public: + explicit QStackedBarSeries(QObject *parent = 0); + QAbstractSeries::SeriesType type() const; + +private: + Q_DECLARE_PRIVATE(QStackedBarSeries) + Q_DISABLE_COPY(QStackedBarSeries) +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // STACKEDBARSERIES_H diff --git a/src/barchart/vertical/stacked/qstackedbarseries_p.h b/src/barchart/vertical/stacked/qstackedbarseries_p.h new file mode 100644 index 00000000..475c0bfa --- /dev/null +++ b/src/barchart/vertical/stacked/qstackedbarseries_p.h @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +// W A R N I N G +// ------------- +// +// This file is not part of the QtCommercial 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 "domain_p.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + + +class QStackedBarSeriesPrivate: public QAbstractBarSeriesPrivate +{ +public: + QStackedBarSeriesPrivate(QStackedBarSeries* q); + Chart* createGraphics(ChartPresenter* presenter); + void scaleDomain(Domain& domain); +private: + Q_DECLARE_PUBLIC(QStackedBarSeries) +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif diff --git a/src/barchart/vertical/stacked/stacked.pri b/src/barchart/vertical/stacked/stacked.pri new file mode 100644 index 00000000..8344ebec --- /dev/null +++ b/src/barchart/vertical/stacked/stacked.pri @@ -0,0 +1,13 @@ +INCLUDEPATH += $$PWD +DEPENDPATH += $$PWD + +SOURCES += \ + $$PWD/qstackedbarseries.cpp \ + $$PWD/stackedbarchartitem.cpp + +PRIVATE_HEADERS += \ + $$PWD/stackedbarchartitem_p.h \ + $$PWD/qstackedbarseries_p.h + +PUBLIC_HEADERS += \ + $$PWD/qstackedbarseries.h diff --git a/src/barchart/vertical/stacked/stackedbarchartitem.cpp b/src/barchart/vertical/stacked/stackedbarchartitem.cpp new file mode 100644 index 00000000..ab1095f1 --- /dev/null +++ b/src/barchart/vertical/stacked/stackedbarchartitem.cpp @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "stackedbarchartitem_p.h" +#include "bar_p.h" +#include "qbarset_p.h" +#include "qabstractbarseries_p.h" +#include "qbarset.h" +#include "chartanimator_p.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +StackedBarChartItem::StackedBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter) : + AbstractBarChartItem(series, presenter) +{ +} + +QVector<QRectF> StackedBarChartItem::calculateLayout() +{ + QVector<QRectF> layout; + // Use temporary qreals for accuracy + qreal categoryCount = m_series->d_func()->categoryCount(); + qreal setCount = m_series->count(); + bool barsVisible = m_series->isVisible(); + + // Domain: + qreal width = geometry().width(); + qreal height = geometry().height(); + qreal rangeY = m_domainMaxY - m_domainMinY; + qreal rangeX = m_domainMaxX - m_domainMinX; + qreal scaleY = (height / rangeY); + qreal scaleX = (width / rangeX); + qreal barWidth = scaleX * m_series->d_func()->barWidth(); + + int itemIndex(0); + for (int category = 0; category < categoryCount; category++) { + qreal yPos = height + rangeY * m_domainMinY + geometry().topLeft().y(); + for (int set=0; set < setCount; set++) { + QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); + + qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + m_rect.left() - barWidth/2; + + qreal barHeight = barSet->value(category) * scaleY; + Bar* bar = m_bars.at(itemIndex); + bar->setPen(barSet->m_pen); + bar->setBrush(barSet->m_brush); + if (qFuzzyIsNull(barHeight)) { + bar->setVisible(false); + } else { + bar->setVisible(barsVisible); + } + + QRectF rect(xPos, yPos-barHeight, barWidth, barHeight); + layout.append(rect); + + QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); + + if (!qFuzzyIsNull(barSet->value(category))) { + label->setText(QString::number(barSet->value(category))); + } else { + label->setText(QString("")); + } + + label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) + ,yPos - barHeight/2 - label->boundingRect().height()/2); + label->setFont(barSet->m_labelFont); + label->setBrush(barSet->m_labelBrush); + itemIndex++; + yPos -= barHeight; + } + } + + return layout; +} + +#include "moc_stackedbarchartitem_p.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/vertical/stacked/stackedbarchartitem_p.h b/src/barchart/vertical/stacked/stackedbarchartitem_p.h new file mode 100644 index 00000000..85cb99a6 --- /dev/null +++ b/src/barchart/vertical/stacked/stackedbarchartitem_p.h @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +// W A R N I N G +// ------------- +// +// This file is not part of the QtCommercial 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> + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class StackedBarChartItem : public AbstractBarChartItem +{ + Q_OBJECT +public: + StackedBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter); + +private: + virtual QVector<QRectF> calculateLayout(); +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // STACKEDBARCHARTITEM_H diff --git a/src/barchart/vertical/vertical.pri b/src/barchart/vertical/vertical.pri new file mode 100644 index 00000000..cf5c7160 --- /dev/null +++ b/src/barchart/vertical/vertical.pri @@ -0,0 +1,3 @@ +include(bar/bar.pri) +include(percent/percent.pri) +include(stacked/stacked.pri)
\ No newline at end of file |