1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include <private/barchartitem_p.h> |
5 | #include <private/bar_p.h> |
6 | #include <private/qabstractbarseries_p.h> |
7 | #include <QtCharts/QBarSet> |
8 | #include <private/qbarset_p.h> |
9 | |
10 | QT_BEGIN_NAMESPACE |
11 | |
12 | BarChartItem::BarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) : |
13 | AbstractBarChartItem(series, item) |
14 | { |
15 | m_orientation = Qt::Vertical; |
16 | connect(sender: series, SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)), |
17 | receiver: this, SLOT(handleLabelsPositionChanged())); |
18 | connect(sender: series, SIGNAL(labelsFormatChanged(QString)), receiver: this, SLOT(positionLabels())); |
19 | } |
20 | |
21 | void BarChartItem::initializeLayout(int set, int category, int layoutIndex, bool resetAnimation) |
22 | { |
23 | QRectF rect; |
24 | |
25 | if (set > 0) { |
26 | QBarSet *barSet = m_series->barSets().at(i: set - 1); |
27 | Bar *bar = m_indexForBarMap.value(key: barSet).value(key: category); |
28 | rect = m_layout.at(i: bar->layoutIndex()); |
29 | qreal oldRight = rect.right(); |
30 | if (resetAnimation) |
31 | rect.setRight(oldRight + rect.width()); |
32 | rect.setLeft(oldRight); |
33 | rect.setTop(rect.bottom()); |
34 | } else { |
35 | QPointF topLeft; |
36 | QPointF bottomRight; |
37 | const int setCount = m_series->count(); |
38 | const qreal barWidth = m_series->d_func()->barWidth() * m_seriesWidth; |
39 | if (domain()->type() == AbstractDomain::XLogYDomain |
40 | || domain()->type() == AbstractDomain::LogXLogYDomain) { |
41 | topLeft = topLeftPoint(set, setCount, category, barWidth, value: domain()->minY()); |
42 | bottomRight = bottomRightPoint(set, setCount, category, barWidth, value: domain()->minY()); |
43 | } else { |
44 | topLeft = topLeftPoint(set, setCount, category, barWidth, value: 0.0); |
45 | bottomRight = bottomRightPoint(set, setCount, category, barWidth, value: 0.0); |
46 | } |
47 | |
48 | if (m_validData) { |
49 | rect.setTopLeft(topLeft); |
50 | rect.setBottomRight(bottomRight); |
51 | } |
52 | } |
53 | m_layout[layoutIndex] = rect.normalized(); |
54 | } |
55 | |
56 | QPointF BarChartItem::topLeftPoint(int set, int setCount, int category, |
57 | qreal barWidth, qreal value) |
58 | { |
59 | return domain()->calculateGeometryPoint( |
60 | point: QPointF(m_seriesPosAdjustment + category - (barWidth / 2.0) |
61 | + (qreal(set)/setCount) * barWidth, |
62 | value), ok&: m_validData); |
63 | } |
64 | |
65 | QPointF BarChartItem::bottomRightPoint(int set, int setCount, |
66 | int category, qreal barWidth, qreal value) |
67 | { |
68 | return domain()->calculateGeometryPoint( |
69 | point: QPointF(m_seriesPosAdjustment + category - (barWidth / 2.0) |
70 | + (qreal(set + 1)/setCount) * barWidth, |
71 | value), ok&: m_validData); |
72 | } |
73 | |
74 | QList<QRectF> BarChartItem::calculateLayout() |
75 | { |
76 | QList<QRectF> layout; |
77 | layout.resize(size: m_layout.size()); |
78 | |
79 | const int setCount = m_series->count(); |
80 | const qreal barWidth = m_series->d_func()->barWidth() * m_seriesWidth; |
81 | |
82 | for (int set = 0; set < setCount; set++) { |
83 | QBarSet *barSet = m_series->barSets().at(i: set); |
84 | const QList<Bar *> bars = m_barMap.value(key: barSet); |
85 | for (int i = 0; i < m_categoryCount; i++) { |
86 | Bar *bar = bars.at(i); |
87 | const int category = bar->index(); |
88 | qreal value = barSet->at(index: category); |
89 | QRectF rect; |
90 | QPointF topLeft = topLeftPoint(set, setCount, category, barWidth, value); |
91 | QPointF bottomRight; |
92 | if (domain()->type() == AbstractDomain::XLogYDomain |
93 | || domain()->type() == AbstractDomain::LogXLogYDomain) { |
94 | bottomRight = bottomRightPoint(set, setCount, category, barWidth, value: domain()->minY()); |
95 | } else { |
96 | bottomRight = bottomRightPoint(set, setCount, category, barWidth, value: 0.0); |
97 | } |
98 | |
99 | rect.setTopLeft(topLeft); |
100 | rect.setBottomRight(bottomRight); |
101 | layout[bar->layoutIndex()] = rect.normalized(); |
102 | } |
103 | } |
104 | |
105 | return layout; |
106 | } |
107 | |
108 | void BarChartItem::handleLabelsPositionChanged() |
109 | { |
110 | positionLabels(); |
111 | } |
112 | |
113 | void BarChartItem::positionLabels() |
114 | { |
115 | positionLabelsVertical(); |
116 | } |
117 | |
118 | QT_END_NAMESPACE |
119 | |
120 | #include "moc_barchartitem_p.cpp" |
121 | |