Skip to content

Commit 8ff1cf7

Browse files
authored
Fix show_value padding. Solves #118 (#119)
1 parent 122d1fa commit 8ff1cf7

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

src/plotly-graph-card.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,19 @@ export class PlotlyGraph extends HTMLElement {
186186
const ms = this.parsed_config.hours_to_show * 60 * 60 * 1000;
187187
return [+new Date() - ms, +new Date()] as [number, number];
188188
}
189+
getAutoFetchRangeWithValueMargins() {
190+
const [start, end] = this.getAutoFetchRange();
191+
const padPercent = Math.max(
192+
...this.parsed_config.entities.map(({ show_value }) => {
193+
if (show_value === false) return 0 / 100;
194+
if (show_value === true) return 10 / 100;
195+
return show_value.right_margin / 100;
196+
})
197+
);
198+
const msToShow = this.parsed_config.hours_to_show * 1000 * 60 * 60;
199+
const msPad = (msToShow / (1 - padPercent)) * padPercent;
200+
return [start, end + msPad];
201+
}
189202
getVisibleRange() {
190203
return this.contentEl.layout.xaxis!.range!.map((date) =>
191204
// if autoscale is used after scrolling, plotly returns the dates as numbers instead of strings
@@ -202,7 +215,7 @@ export class PlotlyGraph extends HTMLElement {
202215
this.withoutRelayout(async () => {
203216
await Plotly.relayout(this.contentEl, {
204217
uirevision: Math.random(), // to trigger the autoranges in all y-yaxes
205-
xaxis: { range: this.getAutoFetchRange() }, // to reset xaxis to hours_to_show quickly, before refetching
218+
xaxis: { range: this.getAutoFetchRangeWithValueMargins() }, // to reset xaxis to hours_to_show quickly, before refetching
206219
});
207220
});
208221
await this.fetch(this.getAutoFetchRange());
@@ -531,24 +544,6 @@ export class PlotlyGraph extends HTMLElement {
531544
x: mergedTrace.x.slice(-1),
532545
y: mergedTrace.y.slice(-1),
533546
});
534-
if (
535-
typeof mergedTrace.show_value === "object" &&
536-
"right_margin" in mergedTrace.show_value
537-
) {
538-
const timeMargin =
539-
mergedTrace.show_value.right_margin *
540-
((this.parsed_config.hours_to_show * 1000 * 60 * 60) / 100);
541-
show_value_traces.push({
542-
...mergedTrace,
543-
marker: {
544-
color: "transparent",
545-
},
546-
hoverinfo: "skip",
547-
showlegend: false,
548-
x: [new Date(Date.now() + timeMargin)],
549-
y: mergedTrace.y.slice(-1),
550-
});
551-
}
552547
}
553548
});
554549
// Preserving the original sequence of real_traces is important for `fill: tonexty`
@@ -568,7 +563,7 @@ export class PlotlyGraph extends HTMLElement {
568563
xaxis: {
569564
range: this.isBrowsing
570565
? this.getVisibleRange()
571-
: this.getAutoFetchRange(),
566+
: this.getAutoFetchRangeWithValueMargins(),
572567
},
573568
},
574569
this.parsed_config.no_default_layout ? {} : yAxisTitles,

0 commit comments

Comments
 (0)