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