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 <private/horizontalbarchartitem_p.h>
31#include <private/qabstractbarseries_p.h>
32#include <private/qbarset_p.h>
33#include <private/bar_p.h>
34
35QT_CHARTS_BEGIN_NAMESPACE
36
37HorizontalBarChartItem::HorizontalBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item)
38 : AbstractBarChartItem(series, item)
39{
40}
41
42void HorizontalBarChartItem::initializeLayout(int set, int category,
43 int layoutIndex, bool resetAnimation)
44{
45 QRectF rect;
46
47 if (set > 0) {
48 QBarSet *barSet = m_series->barSets().at(i: set - 1);
49 Bar *bar = m_indexForBarMap.value(akey: barSet).value(akey: category);
50 rect = m_layout.at(i: bar->layoutIndex());
51 qreal oldTop = rect.top();
52 if (resetAnimation)
53 rect.setTop(oldTop - rect.height());
54 rect.setBottom(oldTop);
55 rect.setRight(rect.left());
56 } else {
57 QPointF topLeft;
58 QPointF bottomRight;
59 const qreal barWidth = m_series->d_func()->barWidth() * m_seriesWidth;
60 const int setCount = m_series->count();
61 if (domain()->type() == AbstractDomain::LogXYDomain
62 || domain()->type() == AbstractDomain::LogXLogYDomain) {
63 topLeft = topLeftPoint(set, setCount, category, barWidth, value: domain()->minX());
64 bottomRight = bottomRightPoint(set, setCount, category, barWidth, value: domain()->minX());
65 } else {
66 topLeft = topLeftPoint(set, setCount, category, barWidth, value: 0.0);
67 bottomRight = bottomRightPoint(set, setCount, category, barWidth, value: 0.0);
68 }
69
70 if (m_validData) {
71 rect.setTopLeft(topLeft);
72 rect.setBottomRight(bottomRight);
73 }
74 }
75 m_layout[layoutIndex] = rect.normalized();
76}
77
78QPointF HorizontalBarChartItem::topLeftPoint(int set, int setCount, int category,
79 qreal barWidth, qreal value)
80{
81 return domain()->calculateGeometryPoint(
82 point: QPointF(value, m_seriesPosAdjustment + category - (barWidth / 2.0)
83 + (qreal(set)/setCount) * barWidth),
84 ok&: m_validData);
85}
86
87QPointF HorizontalBarChartItem::bottomRightPoint(int set, int setCount, int category,
88 qreal barWidth, qreal value)
89{
90 return domain()->calculateGeometryPoint(
91 point: QPointF(value, m_seriesPosAdjustment + category - (barWidth / 2.0)
92 + (qreal(set + 1)/setCount) * barWidth),
93 ok&: m_validData);
94}
95
96QVector<QRectF> HorizontalBarChartItem::calculateLayout()
97{
98 QVector<QRectF> layout;
99 layout.resize(asize: m_layout.size());
100
101 const int setCount = m_series->count();
102 const qreal barWidth = m_series->d_func()->barWidth() * m_seriesWidth;
103
104 for (int set = 0; set < setCount; set++) {
105 QBarSet *barSet = m_series->barSets().at(i: set);
106 const QList<Bar *> bars = m_barMap.value(akey: barSet);
107 for (int i = 0; i < m_categoryCount; i++) {
108 Bar *bar = bars.at(i);
109 const int category = bar->index();
110 qreal value = barSet->at(index: category);
111 QRectF rect;
112 QPointF topLeft;
113 if (domain()->type() == AbstractDomain::LogXYDomain
114 || domain()->type() == AbstractDomain::LogXLogYDomain) {
115 topLeft = topLeftPoint(set, setCount, category, barWidth, value: domain()->minX());
116 } else {
117 topLeft = topLeftPoint(set, setCount, category, barWidth, value: 0.0);
118 }
119
120 QPointF bottomRight = bottomRightPoint(set, setCount, category, barWidth, value);
121 rect.setTopLeft(topLeft);
122 rect.setBottomRight(bottomRight);
123 layout[bar->layoutIndex()] = rect.normalized();
124 }
125 }
126 return layout;
127}
128
129QT_CHARTS_END_NAMESPACE
130
131#include "moc_horizontalbarchartitem_p.cpp"
132

source code of qtcharts/src/charts/barchart/horizontal/bar/horizontalbarchartitem.cpp