Skip to content

Commit 784eb35

Browse files
authored
[graph] Add missing documentation for legend feature (#5266)
1 parent c0c804a commit 784eb35

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed

content/components/graph.md

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ graph:
6565
- **max_value** (*Optional*): Specifies the maximum Y-axis value.
6666
- **min_value** (*Optional*): Specifies the minimum Y-axis value.
6767
- **traces** (*Optional*): Use this to specify more than a single trace.
68+
- **legend** (*Optional*): Configures a legend for the graph traces. See [Legend Options](#legend-options).
6869
6970
Trace specific fields:
7071
@@ -74,15 +75,60 @@ Trace specific fields:
7475
- **continuous** (*Optional*): connects the individual points to make a continuous line. Defaults to `false`.
7576
- **color** (*Optional*): Sets the color of the sensor trace.
7677

78+
## Legend Options
79+
80+
The legend displays trace names, current values, units, and line style samples. Only one legend per graph is supported.
81+
82+
- **name_font** (**Required**, [Font](#display-fonts)): Font used for trace names.
83+
- **value_font** (*Optional*, [Font](#display-fonts)): Font used for current values. If not specified, values are not displayed.
84+
- **width** (*Optional*, int): Legend width in pixels. If not specified, width is automatically calculated.
85+
- **height** (*Optional*, int): Legend height in pixels. If not specified, height is automatically calculated.
86+
- **border** (*Optional*, boolean): Draw a border around the legend. Defaults to `true`.
87+
- **show_lines** (*Optional*, boolean): Display line style samples. Defaults to `true`.
88+
- **show_values** (*Optional*): Position of current values. Can be `NONE`, `AUTO`, `BESIDE`, `BELOW`. Defaults to `AUTO`.
89+
- **show_units** (*Optional*, boolean): Include units with values. Defaults to `true`.
90+
- **direction** (*Optional*): Layout direction. Can be `AUTO`, `HORIZONTAL`, `VERTICAL`. Defaults to `AUTO`.
91+
92+
Example with legend:
93+
94+
```yaml
95+
graph:
96+
- id: temperature_graph_with_legend
97+
duration: 1h
98+
width: 151
99+
height: 51
100+
traces:
101+
- sensor: indoor_temp
102+
name: "Indoor"
103+
line_type: SOLID
104+
color: my_red
105+
- sensor: outdoor_temp
106+
name: "Outdoor"
107+
line_type: DASHED
108+
color: my_blue
109+
legend:
110+
name_font: legend_font
111+
value_font: value_font
112+
show_values: BELOW
113+
border: true
114+
show_lines: true
115+
show_units: true
116+
117+
font:
118+
- file: "arial.ttf"
119+
id: legend_font
120+
size: 10
121+
- file: "arial.ttf"
122+
id: value_font
123+
size: 8
124+
```
125+
77126
And then later in code:
78127

79128
```yaml
80129
display:
81130
- platform: ...
82131
# ...
83-
pages:
84-
- id: page1
85-
lambda: |-
86132
pages:
87133
- id: page1
88134
lambda: |-
@@ -92,6 +138,11 @@ display:
92138
lambda: |-
93139
// Draw the graph at position [x=10,y=20]
94140
it.graph(10, 20, id(multi_temperature_graph), my_yellow);
141+
- id: page3
142+
lambda: |-
143+
// Draw graph and legend
144+
it.graph(10, 20, id(temperature_graph_with_legend));
145+
it.graph_legend(170, 20, id(temperature_graph_with_legend), my_yellow);
95146
96147
color:
97148
- id: my_red
@@ -117,6 +168,8 @@ Here are some things to note:
117168

118169
- Setting `y_grid` will expand any specified range to the nearest multiple of grid spacings.
119170
- Axis labels are currently not possible without manually placing them.
120-
- The grid and border color is set with it.graph(), while the traces are defined separately.
171+
- The grid and border color is set with `it.graph()`, while the traces are defined separately.
172+
- Legends are drawn separately using `it.graph_legend()` and can be positioned independently of the graph.
173+
- Legend dimensions are automatically calculated if not specified, based on font sizes and trace count.
121174

122175
{{< /note >}}

0 commit comments

Comments
 (0)