| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtCharts/QPercentBarSeries> |
| 5 | #include <private/qpercentbarseries_p.h> |
| 6 | #include <private/percentbarchartitem_p.h> |
| 7 | #include <private/chartdataset_p.h> |
| 8 | #include <private/charttheme_p.h> |
| 9 | #include <QtCharts/QValueAxis> |
| 10 | |
| 11 | QT_BEGIN_NAMESPACE |
| 12 | |
| 13 | /*! |
| 14 | \class QPercentBarSeries |
| 15 | \inmodule QtCharts |
| 16 | \brief The QPercentBarSeries class presents a series of categorized data as |
| 17 | a percentage of each category. |
| 18 | |
| 19 | This class draws data as a series of uniformly sized vertically stacked bars, with one |
| 20 | bar per category. Each bar set added to the series contributes a single segment to each |
| 21 | stacked bar. The segment size corresponds to the percentage of the segment value compared |
| 22 | with the total value of all segments in the stack. |
| 23 | Bars with zero value are not drawn. |
| 24 | |
| 25 | See the \l {Charts with Widgets Gallery} to learn how to create a |
| 26 | percent bar chart. |
| 27 | \image examples_percentbarchart.png |
| 28 | |
| 29 | \sa QBarSet, QStackedBarSeries, QAbstractBarSeries |
| 30 | */ |
| 31 | /*! |
| 32 | \qmltype PercentBarSeries |
| 33 | \nativetype QPercentBarSeries |
| 34 | \inqmlmodule QtCharts |
| 35 | |
| 36 | \inherits AbstractBarSeries |
| 37 | |
| 38 | \brief Presents a series of categorized data as a percentage of each category. |
| 39 | |
| 40 | The data is drawn as a series of uniformly sized vertically stacked bars, with one |
| 41 | bar per category. Each bar set added to the series contributes a single segment to each |
| 42 | stacked bar. The segment size corresponds to the percentage of the segment value compared |
| 43 | with the total value of all segments in the stack. |
| 44 | Bars with zero value are not drawn. |
| 45 | |
| 46 | The following QML code snippet shows how to create a simple percent bar chart: |
| 47 | \snippet qmlchartsgallery/qml/BarSeriesPercent.qml 1 |
| 48 | \beginfloatleft |
| 49 | \image examples_qmlchart8.png |
| 50 | \endfloat |
| 51 | \clearfloat |
| 52 | */ |
| 53 | |
| 54 | /*! |
| 55 | Constructs an empty vertical percent bar series that is a QObject and a child of \a parent. |
| 56 | */ |
| 57 | QPercentBarSeries::QPercentBarSeries(QObject *parent) |
| 58 | : QAbstractBarSeries(*new QPercentBarSeriesPrivate(this), parent) |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | /*! |
| 63 | Removes the bar series from the chart. |
| 64 | */ |
| 65 | QPercentBarSeries::~QPercentBarSeries() |
| 66 | { |
| 67 | Q_D(QPercentBarSeries); |
| 68 | if (d->m_chart) |
| 69 | d->m_chart->removeSeries(series: this); |
| 70 | } |
| 71 | |
| 72 | /*! |
| 73 | Returns the vertical percent bar series. |
| 74 | */ |
| 75 | QAbstractSeries::SeriesType QPercentBarSeries::type() const |
| 76 | { |
| 77 | return QAbstractSeries::SeriesTypePercentBar; |
| 78 | } |
| 79 | |
| 80 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 81 | |
| 82 | QPercentBarSeriesPrivate::QPercentBarSeriesPrivate(QPercentBarSeries *q) : QAbstractBarSeriesPrivate(q) |
| 83 | { |
| 84 | |
| 85 | } |
| 86 | |
| 87 | void QPercentBarSeriesPrivate::initializeDomain() |
| 88 | { |
| 89 | qreal minX(domain()->minX()); |
| 90 | qreal minY(domain()->minY()); |
| 91 | qreal maxX(domain()->maxX()); |
| 92 | qreal maxY(domain()->maxY()); |
| 93 | |
| 94 | qreal x = categoryCount(); |
| 95 | minX = qMin(a: minX, b: - (qreal)0.5); |
| 96 | maxX = qMax(a: maxX, b: x - (qreal)0.5); |
| 97 | minY = 0; |
| 98 | maxY = 100; |
| 99 | |
| 100 | domain()->setRange(minX, maxX, minY, maxY); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | void QPercentBarSeriesPrivate::initializeGraphics(QGraphicsItem* parent) |
| 105 | { |
| 106 | Q_Q(QPercentBarSeries); |
| 107 | PercentBarChartItem *bar = new PercentBarChartItem(q,parent); |
| 108 | m_item.reset(p: bar); |
| 109 | QAbstractSeriesPrivate::initializeGraphics(parent); |
| 110 | } |
| 111 | |
| 112 | QT_END_NAMESPACE |
| 113 | |
| 114 | #include "moc_qpercentbarseries.cpp" |
| 115 | |