KQuickCharts

BarChart.h
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
14struct Bar;
15
16/**
17 * An item to render a bar chart.
18 *
19 * This chart renders
20 *
21 * ## Usage example
22 *
23 * \snippet snippets/barchart.qml example
24 *
25 * \image html barchart.png "The resulting bar chart."
26 */
27class QUICKCHARTS_EXPORT BarChart : public XYChart
28{
29 Q_OBJECT
30 QML_ELEMENT
31
32public:
33 /**
34 * Helper enum to provide an easy way to set barWidth to auto.
35 */
36 enum WidthMode { AutoWidth = -2 };
37 Q_ENUM(WidthMode)
38
40 HorizontalOrientation = Qt::Horizontal, ///< Bars are oriented horizontally, with low values left and high values right.
41 VerticalOrientation = Qt::Vertical ///< Bars are oriented vertically, with low values at the bottom and high values at the top.
42 };
43 Q_ENUM(Orientation)
44
45 explicit BarChart(QQuickItem *parent = nullptr);
46
47 /**
48 * The spacing between bars for each value source.
49 *
50 * Note that spacing between each X axis value is determined automatically
51 * based on barWidth, spacing and total chart width. The default is 0.
52 */
53 Q_PROPERTY(qreal spacing READ spacing WRITE setSpacing NOTIFY spacingChanged)
54 qreal spacing() const;
55 void setSpacing(qreal newSpacing);
56 Q_SIGNAL void spacingChanged();
57
58 /**
59 * The width of individual bars in the chart.
60 *
61 * If set to WidthMode::AutoWidth (also the default), the width will be
62 * calculated automatically based on total chart width and item count.
63 */
64 Q_PROPERTY(qreal barWidth READ barWidth WRITE setBarWidth NOTIFY barWidthChanged)
65 qreal barWidth() const;
66 void setBarWidth(qreal newBarWidth);
67 Q_SIGNAL void barWidthChanged();
68
69 /**
70 * The radius of the ends of bars in the chart in pixels.
71 *
72 * By default this is 0, which means no rounding will be done.
73 */
74 Q_PROPERTY(qreal radius READ radius WRITE setRadius NOTIFY radiusChanged)
75 qreal radius() const;
76 void setRadius(qreal newRadius);
77 Q_SIGNAL void radiusChanged();
78
79 /**
80 * The orientation of bars in the chart.
81 *
82 * By default this is Vertical.
83 */
84 Q_PROPERTY(Orientation orientation READ orientation WRITE setOrientation NOTIFY orientationChanged)
85 Orientation orientation() const;
86 void setOrientation(Orientation newOrientation);
87 Q_SIGNAL void orientationChanged();
88
89 /**
90 * The background color of bars in the chart.
91 *
92 * By default this is Qt::transparent. If set to something non-transparent,
93 * the chart will render backgrounds for the bars. These backgrounds will
94 * have the same width as the bars but stretch the full height.
95 */
96 Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged)
97 QColor backgroundColor() const;
98 void setBackgroundColor(const QColor &newBackgroundColor);
99 Q_SIGNAL void backgroundColorChanged();
100
101protected:
102 /**
103 * Reimplemented from QQuickItem.
104 */
105 QSGNode *updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNodeData *) override;
106 /**
107 * Reimplemented from Chart.
108 */
109 void onDataChanged() override;
110
111private:
112 QList<Bar> calculateBars();
113
114 qreal m_spacing = 0.0;
115 qreal m_barWidth = AutoWidth;
116 qreal m_radius = 0.0;
117 Orientation m_orientation = VerticalOrientation;
118 bool m_orientationChanged = false;
119 struct BarData {
120 qreal value = 0;
121 QColor color;
122 };
123 QList<QList<BarData>> m_barDataItems;
124 QColor m_backgroundColor = Qt::transparent;
125};
126
127#endif // BARCHART_H
An item to render a bar chart.
Definition BarChart.h:28
WidthMode
Helper enum to provide an easy way to set barWidth to auto.
Definition BarChart.h:36
virtual void onDataChanged()=0
Called when the data of a value source changes.
A base class for Charts that are based on an X/Y grid.
Definition XYChart.h:33
Q_ENUM(...)
Q_PROPERTY(...)
virtual QSGNode * updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *updatePaintNodeData)
transparent
Horizontal
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:53 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.