KQuickCharts

Legend.qml
1/*
2 * This file is part of KQuickCharts
3 * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8import QtQuick
9import QtQuick.Layouts
10import QtQuick.Controls
11
12import org.kde.quickcharts as Charts
13import org.kde.quickcharts.controls
14
15/**
16 * A pre-made legend control that displays a legend for charts.
17 */
18Control {
19 id: control
21 /**
22 * The chart to display the legend for.
23 */
24 property Charts.Chart chart
25 /**
26 * The delegate to use to display legend information.
27 *
28 * \sa Legend::delegate
29 */
30 property alias delegate: legendRepeater.delegate
31 /**
32 *
33 */
34 property alias model: legendRepeater.model
35
36 property alias horizontalSpacing: legend.horizontalSpacing
37 property alias verticalSpacing: legend.verticalSpacing
38
39 property real maximumDelegateWidth: Theme.gridUnit * 10
40
41 property var formatValue: function(input, index) { return input }
42 property var maximumValueWidth: function(input, index) { return -1 }
43
44 property alias preferredWidth: legend.preferredWidth
45
46 property string nameRole: "name"
47 property string shortNameRole: "shortName"
48 property string colorRole: "color"
49 property string valueRole: "value"
50
51 default property alias _children: legend.children
52
53 leftPadding: 0
54 rightPadding: 0
55 topPadding: 0
56 bottomPadding: 0
57
58 implicitWidth: Math.max(implicitContentWidth, implicitBackgroundWidth) + leftPadding + rightPadding
59 implicitHeight: Math.max(implicitContentHeight, implicitBackgroundHeight) + topPadding + bottomPadding
60
61 contentItem: Flickable {
62 anchors.fill: parent
63
64 contentHeight: legend.implicitHeight
65 clip: true
66 boundsBehavior: Flickable.StopAtBounds
67
68 implicitHeight: legend.implicitHeight
69 implicitWidth: legend.implicitWidth
70
71 // Limit maximum flick velocity to ensure we can scroll one line per
72 // mouse wheel "tick" when the legend's height is very constrained.
73 maximumFlickVelocity: Theme.gridUnit * 50
74 LegendLayout {
75 id: legend
76
77 width: parent.width
78
79 Repeater {
80 id: legendRepeater
81
82 model: LegendModel { chart: control.chart }
83
84 delegate: LegendDelegate {
85 property var itemData: typeof modelData !== "undefined" ? modelData : model
86
87 name: itemData[control.nameRole] ?? ""
88 shortName: itemData[control.shortNameRole] ?? ""
89 color: itemData[control.colorRole] ?? "white"
90 value: control.formatValue(itemData[control.valueRole] ?? "", index)
91
92 maximumValueWidth: {
93 var result = control.maximumValueWidth(model.value, index)
94 if (result > 0) {
95 return result
96 }
97
98 return -1
99 }
100
101 LegendLayout.minimumWidth: minimumWidth
102 LegendLayout.preferredWidth: preferredWidth
103 LegendLayout.maximumWidth: Math.max(control.maximumDelegateWidth, preferredWidth)
104 }
105 }
106
107 horizontalSpacing: Theme.largeSpacing
108 verticalSpacing: Theme.smallSpacing
109 }
110
111 children: [
112 Item {
113 width: parent.width;
114 height: 1;
115 visible: parent.contentY > 0
116
117 ToolButton {
118 anchors {
119 horizontalCenter: parent.horizontalCenter
120 top: parent.top
121 }
122
123 width: Theme.smallIconSize
124 height: Theme.smallIconSize
125
126 icon.name: "arrow-up-symbolic"
127 icon.width: Theme.smallIconSize
128 icon.height: Theme.smallIconSize
129 enabled: false
130 }
131 },
132 Item {
133 y: parent.height - height
134 width: parent.width;
135 height: 1;
136 visible: parent.contentY + parent.height < legend.height
137
138 ToolButton {
139 anchors {
140 horizontalCenter: parent.horizontalCenter
141 bottom: parent.bottom
142 }
143
144 width: Theme.smallIconSize
145 height: Theme.smallIconSize
146
147 icon.name: "arrow-down-symbolic"
148 icon.width: Theme.smallIconSize
149 icon.height: Theme.smallIconSize
150 enabled: false
151 }
152 }
153 ]
154 }
155}
A delegate that can be used as part of a Legend.
A model that extracts information from a chart that can be displayed as a legend.
Definition LegendModel.h:31
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:57 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.