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

source code of qtcharts/src/charts/barchart/horizontal/percent/qhorizontalpercentbarseries.cpp