Skip to content

Commit 29c84d7

Browse files
authored
Implement autorange_after_scroll (#227)
1 parent 6731fac commit 29c84d7

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
lines changed

readme.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,14 +928,26 @@ Exactly the same as the history card, but more powerful
928928
### Fixed Relative Time
929929

930930
- Decimal values (e.g `hours_to_show: 0.5`)
931-
- Duration strings (e.g `hours_to_show: 2h`, `3d`, `1w`, `1M`). See #Duration
931+
- Duration strings (e.g `hours_to_show: 2h`, `3d`, `1w`, `1M`). See [Durations](#Duration)
932932

933933
### Dynamic Relative Time
934934

935935
Shows the current day, hour, etc from beginning to end.
936936
The options are: `current_minute`, `current_hour`, `current_day`, `current_week`, `current_month`, `current_quarter`, `current_year`
937937
It can be combined with the global `time_offset`.
938938

939+
## autorange_after_scroll:
940+
941+
Removes all data out of the visible range, and autoscales after each replot.
942+
Particularly useful when combined with [Range Selector Buttons](#Range-Selector-buttons)
943+
944+
```yaml
945+
type: custom:plotly-graph
946+
entities:
947+
- entity: sensor.garden_temperature
948+
autorange_after_scroll: true
949+
```
950+
939951
## refresh_interval:
940952

941953
Update data every `refresh_interval` seconds.

src/parse-config/defaults.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const defaultYamlRequired = {
6363
},
6464
layout: {},
6565
on_dblclick: noop$fn,
66+
autorange_after_scroll: false,
6667
};
6768

6869
//

src/parse-config/parse-config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ class ConfigParser {
240240
}
241241
this.yaml.visible_range = visible_range;
242242
}
243+
if (this.fnParam.getFromConfig("autorange_after_scroll")) {
244+
this.observed_range = visible_range.slice();
245+
}
243246
this.observed_range[0] = Math.min(this.observed_range[0], visible_range[0]);
244247
this.observed_range[1] = Math.max(this.observed_range[1], visible_range[1]);
245248
const statisticsParams = parseStatistics(

src/plotly-graph-card.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,13 @@ export class PlotlyGraph extends HTMLElement {
391391
.join("\n<br />\n");
392392
this.parsed_config = parsed;
393393

394-
const { entities, layout, config, refresh_interval } = this.parsed_config;
394+
const {
395+
entities,
396+
layout,
397+
config,
398+
refresh_interval,
399+
autorange_after_scroll,
400+
} = this.parsed_config;
395401
clearTimeout(this.handles.refreshTimeout!);
396402
if (refresh_interval !== "auto" && refresh_interval > 0) {
397403
this.handles.refreshTimeout = window.setTimeout(
@@ -405,6 +411,11 @@ export class PlotlyGraph extends HTMLElement {
405411
}
406412
await this.withoutRelayout(async () => {
407413
await Plotly.react(this.contentEl, entities, layout, config);
414+
if (autorange_after_scroll) {
415+
await Plotly.relayout(this.contentEl, {
416+
"yaxis.autorange": true,
417+
});
418+
}
408419
this.contentEl.style.visibility = "";
409420
});
410421
// this.handles.dataClick?.off("plotly_click", this.onDataClick)!;

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export type InputConfig = {
5353
significant_changes_only?: boolean; // defaults to false
5454
minimal_response?: boolean; // defaults to true
5555
disable_pinch_to_zoom?: boolean; // defaults to false
56+
autorange_after_scroll?: boolean; // defaults to false
5657
};
5758

5859
export type EntityConfig = EntityIdConfig & {
@@ -86,6 +87,7 @@ export type Config = {
8687
disable_pinch_to_zoom: boolean;
8788
visible_range: [number, number];
8889
on_dblclick: Function;
90+
autorange_after_scroll: boolean;
8991
};
9092
export type EntityIdStateConfig = {
9193
entity: string;

0 commit comments

Comments
 (0)