Skip to content

Commit 2c1931a

Browse files
Add Y-Axis limits functionality from history graphs card to statistics graph card (#22771)
1 parent c9cad25 commit 2c1931a

File tree

5 files changed

+59
-3
lines changed

5 files changed

+59
-3
lines changed

src/components/chart/statistics-chart.ts

+11
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ export class StatisticsChart extends LitElement {
7272

7373
@property() public chartType: ChartType = "line";
7474

75+
@property({ type: Number }) public minYAxis?: number;
76+
77+
@property({ type: Number }) public maxYAxis?: number;
78+
79+
@property({ type: Boolean }) public fitYData = false;
80+
7581
@property({ type: Boolean }) public hideLegend = false;
7682

7783
@property({ type: Boolean }) public logarithmicScale = false;
@@ -113,6 +119,9 @@ export class StatisticsChart extends LitElement {
113119
changedProps.has("unit") ||
114120
changedProps.has("period") ||
115121
changedProps.has("chartType") ||
122+
changedProps.has("minYAxis") ||
123+
changedProps.has("maxYAxis") ||
124+
changedProps.has("fitYData") ||
116125
changedProps.has("logarithmicScale") ||
117126
changedProps.has("hideLegend")
118127
) {
@@ -232,6 +241,8 @@ export class StatisticsChart extends LitElement {
232241
text: unit || this.unit,
233242
},
234243
type: this.logarithmicScale ? "logarithmic" : "linear",
244+
min: this.fitYData ? null : this.minYAxis,
245+
max: this.fitYData ? null : this.maxYAxis,
235246
},
236247
},
237248
plugins: {

src/panels/lovelace/cards/hui-statistics-graph-card.ts

+3
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ export class HuiStatisticsGraphCard extends LitElement implements LovelaceCard {
195195
.statTypes=${this._statTypes!}
196196
.names=${this._names}
197197
.unit=${this._unit}
198+
.minYAxis=${this._config.min_y_axis}
199+
.maxYAxis=${this._config.max_y_axis}
200+
.fitYData=${this._config.fit_y_data || false}
198201
.hideLegend=${this._config.hide_legend || false}
199202
.logarithmicScale=${this._config.logarithmic_scale || false}
200203
></statistics-chart>

src/panels/lovelace/cards/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,9 @@ export interface StatisticsGraphCardConfig extends LovelaceCardConfig {
356356
period?: "5minute" | "hour" | "day" | "month";
357357
stat_types?: StatisticType | StatisticType[];
358358
chart_type?: "line" | "bar";
359+
min_y_axis?: number;
360+
max_y_axis?: number;
361+
fit_y_data?: boolean;
359362
hide_legend?: boolean;
360363
logarithmic_scale?: boolean;
361364
}

src/panels/lovelace/editor/config-elements/hui-statistics-graph-card-editor.ts

+38-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ const cardConfigStruct = assign(
6969
unit: optional(string()),
7070
hide_legend: optional(boolean()),
7171
logarithmic_scale: optional(boolean()),
72+
min_y_axis: optional(number()),
73+
max_y_axis: optional(number()),
74+
fit_y_data: optional(boolean()),
7275
})
7376
);
7477

@@ -126,7 +129,8 @@ export class HuiStatisticsGraphCardEditor
126129
(
127130
localize: LocalizeFunc,
128131
statisticIds: string[] | undefined,
129-
metaDatas: StatisticsMetaData[] | undefined
132+
metaDatas: StatisticsMetaData[] | undefined,
133+
showFitOption: boolean
130134
) => {
131135
const units = new Set<string>();
132136
metaDatas?.forEach((metaData) => {
@@ -213,6 +217,33 @@ export class HuiStatisticsGraphCardEditor
213217
],
214218
],
215219
},
220+
{
221+
name: "",
222+
type: "grid",
223+
schema: [
224+
{
225+
name: "min_y_axis",
226+
required: false,
227+
selector: { number: { mode: "box", step: "any" } },
228+
},
229+
{
230+
name: "max_y_axis",
231+
required: false,
232+
selector: { number: { mode: "box", step: "any" } },
233+
},
234+
],
235+
},
236+
237+
...(showFitOption
238+
? [
239+
{
240+
name: "fit_y_data",
241+
required: false,
242+
selector: { boolean: {} },
243+
},
244+
]
245+
: []),
246+
216247
{
217248
name: "hide_legend",
218249
required: false,
@@ -254,7 +285,9 @@ export class HuiStatisticsGraphCardEditor
254285
const schema = this._schema(
255286
this.hass.localize,
256287
this._configEntities,
257-
this._metaDatas
288+
this._metaDatas,
289+
this._config!.min_y_axis !== undefined ||
290+
this._config!.max_y_axis !== undefined
258291
);
259292
const configured_stat_types = this._config!.stat_types
260293
? ensureArray(this._config.stat_types)
@@ -359,6 +392,9 @@ export class HuiStatisticsGraphCardEditor
359392
case "unit":
360393
case "hide_legend":
361394
case "logarithmic_scale":
395+
case "min_y_axis":
396+
case "max_y_axis":
397+
case "fit_y_data":
362398
return this.hass!.localize(
363399
`ui.panel.lovelace.editor.card.statistics-graph.${schema.name}`
364400
);

src/translations/en.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -6162,7 +6162,10 @@
61626162
"pick_statistic": "Add a statistic",
61636163
"picked_statistic": "Statistic",
61646164
"hide_legend": "Hide legend",
6165-
"logarithmic_scale": "Logarithmic scale"
6165+
"logarithmic_scale": "Logarithmic scale",
6166+
"min_y_axis": "Y axis minimum",
6167+
"max_y_axis": "Y axis maximum",
6168+
"fit_y_data": "Extend Y axis limits to fit data"
61666169
},
61676170
"statistic": {
61686171
"name": "Statistic",

0 commit comments

Comments
 (0)