File indexing completed on 2024-10-06 03:40:52
0001 /* 0002 * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #ifndef BARCHART_H 0008 #define BARCHART_H 0009 0010 #include <qqmlregistration.h> 0011 0012 #include "XYChart.h" 0013 0014 struct Bar; 0015 0016 /** 0017 * An item to render a bar chart. 0018 * 0019 * This chart renders 0020 * 0021 * ## Usage example 0022 * 0023 * \snippet snippets/barchart.qml example 0024 * 0025 * \image html barchart.png "The resulting bar chart." 0026 */ 0027 class QUICKCHARTS_EXPORT BarChart : public XYChart 0028 { 0029 Q_OBJECT 0030 QML_ELEMENT 0031 0032 public: 0033 /** 0034 * Helper enum to provide an easy way to set barWidth to auto. 0035 */ 0036 enum WidthMode { AutoWidth = -2 }; 0037 Q_ENUM(WidthMode) 0038 0039 enum Orientation { 0040 HorizontalOrientation = Qt::Horizontal, ///< Bars are oriented horizontally, with low values left and high values right. 0041 VerticalOrientation = Qt::Vertical ///< Bars are oriented vertically, with low values at the bottom and high values at the top. 0042 }; 0043 Q_ENUM(Orientation) 0044 0045 explicit BarChart(QQuickItem *parent = nullptr); 0046 0047 /** 0048 * The spacing between bars for each value source. 0049 * 0050 * Note that spacing between each X axis value is determined automatically 0051 * based on barWidth, spacing and total chart width. The default is 0. 0052 */ 0053 Q_PROPERTY(qreal spacing READ spacing WRITE setSpacing NOTIFY spacingChanged) 0054 qreal spacing() const; 0055 void setSpacing(qreal newSpacing); 0056 Q_SIGNAL void spacingChanged(); 0057 0058 /** 0059 * The width of individual bars in the chart. 0060 * 0061 * If set to WidthMode::AutoWidth (also the default), the width will be 0062 * calculated automatically based on total chart width and item count. 0063 */ 0064 Q_PROPERTY(qreal barWidth READ barWidth WRITE setBarWidth NOTIFY barWidthChanged) 0065 qreal barWidth() const; 0066 void setBarWidth(qreal newBarWidth); 0067 Q_SIGNAL void barWidthChanged(); 0068 0069 /** 0070 * The radius of the ends of bars in the chart in pixels. 0071 * 0072 * By default this is 0, which means no rounding will be done. 0073 */ 0074 Q_PROPERTY(qreal radius READ radius WRITE setRadius NOTIFY radiusChanged) 0075 qreal radius() const; 0076 void setRadius(qreal newRadius); 0077 Q_SIGNAL void radiusChanged(); 0078 0079 /** 0080 * The orientation of bars in the chart. 0081 * 0082 * By default this is Vertical. 0083 */ 0084 Q_PROPERTY(Orientation orientation READ orientation WRITE setOrientation NOTIFY orientationChanged) 0085 Orientation orientation() const; 0086 void setOrientation(Orientation newOrientation); 0087 Q_SIGNAL void orientationChanged(); 0088 0089 /** 0090 * The background color of bars in the chart. 0091 * 0092 * By default this is Qt::transparent. If set to something non-transparent, 0093 * the chart will render backgrounds for the bars. These backgrounds will 0094 * have the same width as the bars but stretch the full height. 0095 */ 0096 Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged) 0097 QColor backgroundColor() const; 0098 void setBackgroundColor(const QColor &newBackgroundColor); 0099 Q_SIGNAL void backgroundColorChanged(); 0100 0101 protected: 0102 /** 0103 * Reimplemented from QQuickItem. 0104 */ 0105 QSGNode *updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNodeData *) override; 0106 /** 0107 * Reimplemented from Chart. 0108 */ 0109 void onDataChanged() override; 0110 0111 private: 0112 QList<Bar> calculateBars(); 0113 0114 qreal m_spacing = 0.0; 0115 qreal m_barWidth = AutoWidth; 0116 qreal m_radius = 0.0; 0117 Orientation m_orientation = VerticalOrientation; 0118 bool m_orientationChanged = false; 0119 struct BarData { 0120 qreal value = 0; 0121 QColor color; 0122 }; 0123 QList<QList<BarData>> m_barDataItems; 0124 QColor m_backgroundColor = Qt::transparent; 0125 }; 0126 0127 #endif // BARCHART_H