| 1 | /* |
| 2 | * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 5 | */ |
| 6 | |
| 7 | #ifndef BARCHART_H |
| 8 | #define BARCHART_H |
| 9 | |
| 10 | #include <qqmlregistration.h> |
| 11 | |
| 12 | #include "XYChart.h" |
| 13 | |
| 14 | struct Bar; |
| 15 | |
| 16 | /*! |
| 17 | * \qmltype BarChart |
| 18 | * \inherits XYChart |
| 19 | * \inqmlmodule org.kde.quickcharts |
| 20 | * |
| 21 | * \brief An item to render a bar chart. |
| 22 | * |
| 23 | * This chart renders |
| 24 | * |
| 25 | * \section1 Usage example |
| 26 | * |
| 27 | * \snippet barchart.qml example |
| 28 | * |
| 29 | * \image barchart.png The resulting bar chart. |
| 30 | */ |
| 31 | class QUICKCHARTS_EXPORT BarChart : public XYChart |
| 32 | { |
| 33 | Q_OBJECT |
| 34 | QML_ELEMENT |
| 35 | |
| 36 | public: |
| 37 | /*! |
| 38 | * \enum BarChart::WidthMode |
| 39 | * |
| 40 | * Helper enum to provide an easy way to set barWidth to auto. |
| 41 | * |
| 42 | * \value AutoWidth |
| 43 | * Automatically calculate the width of bars. |
| 44 | */ |
| 45 | enum WidthMode { AutoWidth = -2 }; |
| 46 | Q_ENUM(WidthMode) |
| 47 | |
| 48 | /*! |
| 49 | * \enum BarChart::Orientation |
| 50 | * |
| 51 | * Bar direction. |
| 52 | * |
| 53 | * \value HorizontalOrientation |
| 54 | * Bars are oriented horizontally, with low values left and high values right. |
| 55 | * \value VerticalOrientation |
| 56 | * Bars are oriented vertically, with low values at the bottom and high values at the top. |
| 57 | */ |
| 58 | enum Orientation { |
| 59 | HorizontalOrientation = Qt::Horizontal, |
| 60 | VerticalOrientation = Qt::Vertical |
| 61 | }; |
| 62 | Q_ENUM(Orientation) |
| 63 | |
| 64 | explicit BarChart(QQuickItem *parent = nullptr); |
| 65 | |
| 66 | /*! |
| 67 | * \qmlproperty real BarChart::spacing |
| 68 | * \brief The spacing between bars for each value source. |
| 69 | * |
| 70 | * Note that spacing between each X axis value is determined automatically |
| 71 | * based on barWidth, spacing and total chart width. The default is 0. |
| 72 | */ |
| 73 | Q_PROPERTY(qreal spacing READ spacing WRITE setSpacing NOTIFY spacingChanged) |
| 74 | qreal spacing() const; |
| 75 | void setSpacing(qreal newSpacing); |
| 76 | Q_SIGNAL void spacingChanged(); |
| 77 | |
| 78 | /*! |
| 79 | * \qmlproperty real BarChart::barWidth |
| 80 | * \brief The width of individual bars in the chart. |
| 81 | * |
| 82 | * If set to BarChart.AutoWidth (also the default), the width will be |
| 83 | * calculated automatically based on total chart width and item count. |
| 84 | */ |
| 85 | Q_PROPERTY(qreal barWidth READ barWidth WRITE setBarWidth NOTIFY barWidthChanged) |
| 86 | qreal barWidth() const; |
| 87 | void setBarWidth(qreal newBarWidth); |
| 88 | Q_SIGNAL void barWidthChanged(); |
| 89 | |
| 90 | /*! |
| 91 | * \qmlproperty real BarChart::radius |
| 92 | * \brief The radius of the ends of bars in the chart in pixels. |
| 93 | * |
| 94 | * By default this is 0, which means no rounding will be done. |
| 95 | */ |
| 96 | Q_PROPERTY(qreal radius READ radius WRITE setRadius NOTIFY radiusChanged) |
| 97 | qreal radius() const; |
| 98 | void setRadius(qreal newRadius); |
| 99 | Q_SIGNAL void radiusChanged(); |
| 100 | |
| 101 | /*! |
| 102 | * \qmlproperty enumeration BarChart::orientation |
| 103 | * \qmlenumeratorsfrom BarChart::Orientation |
| 104 | * \brief The orientation of bars in the chart. |
| 105 | * |
| 106 | * By default this is BarChart.VerticalOrientation. |
| 107 | */ |
| 108 | Q_PROPERTY(Orientation orientation READ orientation WRITE setOrientation NOTIFY orientationChanged) |
| 109 | Orientation orientation() const; |
| 110 | void setOrientation(Orientation newOrientation); |
| 111 | Q_SIGNAL void orientationChanged(); |
| 112 | |
| 113 | /*! |
| 114 | * \qmlproperty color BarChart::backgroundColor |
| 115 | * \brief The background color of bars in the chart. |
| 116 | * |
| 117 | * By default this is Qt::transparent. If set to something non-transparent, |
| 118 | * the chart will render backgrounds for the bars. These backgrounds will |
| 119 | * have the same width as the bars but stretch the full height. |
| 120 | */ |
| 121 | Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged) |
| 122 | QColor backgroundColor() const; |
| 123 | void setBackgroundColor(const QColor &newBackgroundColor); |
| 124 | Q_SIGNAL void backgroundColorChanged(); |
| 125 | |
| 126 | protected: |
| 127 | QSGNode *updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNodeData *) override; |
| 128 | void onDataChanged() override; |
| 129 | |
| 130 | private: |
| 131 | QList<Bar> calculateBars(); |
| 132 | |
| 133 | qreal m_spacing = 0.0; |
| 134 | qreal m_barWidth = AutoWidth; |
| 135 | qreal m_radius = 0.0; |
| 136 | Orientation m_orientation = VerticalOrientation; |
| 137 | bool m_orientationChanged = false; |
| 138 | struct BarData { |
| 139 | qreal value = 0; |
| 140 | QColor color; |
| 141 | }; |
| 142 | QList<QList<BarData>> m_barDataItems; |
| 143 | QColor m_backgroundColor = Qt::transparent; |
| 144 | }; |
| 145 | |
| 146 | #endif // BARCHART_H |
| 147 | |