1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Charts module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 or (at your option) any later version |
20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include <QtCharts/QPercentBarSeries> |
31 | #include <private/qpercentbarseries_p.h> |
32 | #include <private/percentbarchartitem_p.h> |
33 | #include <private/chartdataset_p.h> |
34 | #include <private/charttheme_p.h> |
35 | #include <QtCharts/QValueAxis> |
36 | |
37 | QT_CHARTS_BEGIN_NAMESPACE |
38 | |
39 | /*! |
40 | \class QPercentBarSeries |
41 | \inmodule QtCharts |
42 | \brief The QPercentBarSeries class presents a series of categorized data as |
43 | a percentage of each category. |
44 | |
45 | This class draws data as a series of uniformly sized vertically stacked bars, with one |
46 | bar per category. Each bar set added to the series contributes a single segment to each |
47 | stacked bar. The segment size corresponds to the percentage of the segment value compared |
48 | with the total value of all segments in the stack. |
49 | Bars with zero value are not drawn. |
50 | |
51 | See the \l {PercentbarChart Example} {percent bar chart example} to learn how to create a |
52 | percent bar chart. |
53 | \image examples_percentbarchart.png |
54 | |
55 | \sa QBarSet, QStackedBarSeries, QAbstractBarSeries |
56 | */ |
57 | /*! |
58 | \qmltype PercentBarSeries |
59 | \instantiates QPercentBarSeries |
60 | \inqmlmodule QtCharts |
61 | |
62 | \inherits AbstractBarSeries |
63 | |
64 | \brief Presents a series of categorized data as a percentage of each category. |
65 | |
66 | The data is drawn as a series of uniformly sized vertically stacked bars, with one |
67 | bar per category. Each bar set added to the series contributes a single segment to each |
68 | stacked bar. The segment size corresponds to the percentage of the segment value compared |
69 | with the total value of all segments in the stack. |
70 | Bars with zero value are not drawn. |
71 | |
72 | The following QML code snippet shows how to create a simple percent bar chart: |
73 | \snippet qmlchart/qml/qmlchart/View8.qml 1 |
74 | \beginfloatleft |
75 | \image examples_qmlchart8.png |
76 | \endfloat |
77 | \clearfloat |
78 | */ |
79 | |
80 | /*! |
81 | Constructs an empty vertical percent bar series that is a QObject and a child of \a parent. |
82 | */ |
83 | QPercentBarSeries::QPercentBarSeries(QObject *parent) |
84 | : QAbstractBarSeries(*new QPercentBarSeriesPrivate(this), parent) |
85 | { |
86 | } |
87 | |
88 | /*! |
89 | Removes the bar series from the chart. |
90 | */ |
91 | QPercentBarSeries::~QPercentBarSeries() |
92 | { |
93 | Q_D(QPercentBarSeries); |
94 | if (d->m_chart) |
95 | d->m_chart->removeSeries(series: this); |
96 | } |
97 | |
98 | /*! |
99 | Returns the vertical percent bar series. |
100 | */ |
101 | QAbstractSeries::SeriesType QPercentBarSeries::type() const |
102 | { |
103 | return QAbstractSeries::SeriesTypePercentBar; |
104 | } |
105 | |
106 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
107 | |
108 | QPercentBarSeriesPrivate::QPercentBarSeriesPrivate(QPercentBarSeries *q) : QAbstractBarSeriesPrivate(q) |
109 | { |
110 | |
111 | } |
112 | |
113 | void QPercentBarSeriesPrivate::initializeDomain() |
114 | { |
115 | qreal minX(domain()->minX()); |
116 | qreal minY(domain()->minY()); |
117 | qreal maxX(domain()->maxX()); |
118 | qreal maxY(domain()->maxY()); |
119 | |
120 | qreal x = categoryCount(); |
121 | minX = qMin(a: minX, b: - (qreal)0.5); |
122 | maxX = qMax(a: maxX, b: x - (qreal)0.5); |
123 | minY = 0; |
124 | maxY = 100; |
125 | |
126 | domain()->setRange(minX, maxX, minY, maxY); |
127 | } |
128 | |
129 | |
130 | void QPercentBarSeriesPrivate::initializeGraphics(QGraphicsItem* parent) |
131 | { |
132 | Q_Q(QPercentBarSeries); |
133 | PercentBarChartItem *bar = new PercentBarChartItem(q,parent); |
134 | m_item.reset(other: bar); |
135 | QAbstractSeriesPrivate::initializeGraphics(parent); |
136 | } |
137 | |
138 | QT_CHARTS_END_NAMESPACE |
139 | |
140 | #include "moc_qpercentbarseries.cpp" |
141 | |