Skip to content

Commit 63717b7

Browse files
authored
feat(experimental): hidden_by_default to toggle series on load (#62)
Fixes #60
1 parent 7f19578 commit 63717b7

File tree

6 files changed

+39
-1
lines changed

6 files changed

+39
-1
lines changed

.devcontainer/ui-lovelace.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ views:
7070
stacked: false
7171
experimental:
7272
color_threshold: true
73+
hidden_by_default: true
7374
# disable_config_validation: true
7475
series:
7576
- entity: sensor.random_0_1000
@@ -90,6 +91,8 @@ views:
9091
- entity: sensor.random0_100
9192
name: test2
9293
type: column
94+
show:
95+
hidden_by_default: true
9396
group_by:
9497
duration: 1m
9598
func: last

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ However, some things might be broken :grin:
4545
- [Experimental features](#experimental-features)
4646
- [Configuration options](#configuration-options)
4747
- [`color_threshold` experimental feature](#color_threshold-experimental-feature)
48+
- [`hidden_by_default` experimental feature](#hidden_by_default-experimental-feature)
4849
- [Known issues](#known-issues)
4950
- [Roadmap](#roadmap)
5051
- [Examples](#examples)
@@ -173,6 +174,7 @@ The card stricly validates all the options available (but not for the `apex_conf
173174
| `in_header` | boolean | `true` | v1.4.0 | If `show_states` is enabled, this would show/hide this specific serie in the header |
174175
| `in_chart` | boolean | `true` | v1.4.0 | If `false`, hides the serie from the chart |
175176
| `datalabels` | boolean | `false` | v1.5.0 | If `true` will show the value of each point for this serie directly in the chart. Don't use it if you have a lot of points displayed, it will be a mess |
177+
| `hidden_by_default` | boolean | `false` | NEXT_VERSION | See [experimental](#hidden_by_default-experimental-feature) |
176178

177179

178180
### Main `show` Options
@@ -442,6 +444,7 @@ For code junkies, you'll find the default options I use in [`src/apex-layouts.ts
442444
| ---- | :--: | :-----: | :---: | ----------- |
443445
| `color_threshold` | boolean | `false` | NEXT_VERSION | Will enable the color threshold feature. See [color_threshold](#color_threshold-experimental-feature) |
444446
| `disable_config_validation` | boolean | `false` | NEXT_VERSION | If `true`, will disable the config validation. Useful if you have cards adding parameters to this one. Use at your own risk. |
447+
| `hidden_by_default` | boolean | `false` | NEXT_VERSION | Will allow you to use the `hidden_by_default` option. See [hidden_by_default](#hidden_by_default-experimental-feature) |
445448

446449
### `color_threshold` experimental feature
447450

@@ -490,6 +493,24 @@ series:
490493
491494
![color_threshold](docs/color_threshold.png)
492495
496+
### `hidden_by_default` experimental feature
497+
498+
Enabling this experimental feature might/will break the auto-scaling and auto-column width feature. Don't open an issue for that. It only works if all the series have a unique name.
499+
500+
This option is useful if you want to hide a serie from the chart by default when it's loaded as if you had clicked on the legend to disable this serie.
501+
502+
This is how to use it:
503+
```yaml
504+
type: custom:apexcharts-card
505+
experimental:
506+
hidden_by_default: true
507+
series:
508+
- entity: sensor.temperature
509+
show:
510+
hidden_by_default: true
511+
- entity: sensor.temperature_office
512+
```
513+
493514
## Known issues
494515

495516
* Sometimes, if `smoothing` is used alongside `area` and there is missing data in the chart, the background will be glitchy. See [apexcharts.js/#2180](https://github.com/apexcharts/apexcharts.js/issues/2180)

src/apexcharts-card.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,16 @@ class ChartsCard extends LitElement {
168168
if (this._updating || this._dataLoaded || !this._apexChart || !this._config || !this._hass) return;
169169
this._dataLoaded = true;
170170
this._updating = true;
171-
this._updateData();
171+
this._updateData().then(() => {
172+
if (this._config?.experimental?.hidden_by_default) {
173+
this._config.series_in_graph.forEach((serie, index) => {
174+
if (serie.show.hidden_by_default) {
175+
const name = computeName(index, this._config?.series_in_graph, this._hass?.states);
176+
this._apexChart?.hideSeries(name);
177+
}
178+
});
179+
}
180+
});
172181
}
173182

174183
public set hass(hass: HomeAssistant) {

src/types-config-ti.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const ChartCardExternalConfig = t.iface([], {
99
"experimental": t.opt(t.iface([], {
1010
"color_threshold": t.opt("boolean"),
1111
"disable_config_validation": t.opt("boolean"),
12+
"hidden_by_default": t.opt("boolean"),
1213
})),
1314
"chart_type": t.opt(t.union(t.lit('line'), t.lit('scatter'), t.lit('pie'), t.lit('donut'), t.lit('radialBar'))),
1415
"update_interval": t.opt("string"),
@@ -64,6 +65,7 @@ export const ChartCardSeriesExternalConfig = t.iface([], {
6465
"in_header": t.opt("boolean"),
6566
"in_chart": t.opt("boolean"),
6667
"datalabels": t.opt("boolean"),
68+
"hidden_by_default": t.opt("boolean"),
6769
})),
6870
"group_by": t.opt(t.iface([], {
6971
"duration": t.opt("string"),

src/types-config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export interface ChartCardExternalConfig {
33
experimental?: {
44
color_threshold?: boolean;
55
disable_config_validation?: boolean;
6+
hidden_by_default?: boolean;
67
};
78
chart_type?: 'line' | 'scatter' | 'pie' | 'donut' | 'radialBar';
89
update_interval?: string;
@@ -60,6 +61,7 @@ export interface ChartCardSeriesExternalConfig {
6061
in_header?: boolean;
6162
in_chart?: boolean;
6263
datalabels?: boolean;
64+
hidden_by_default?: boolean;
6365
};
6466
group_by?: {
6567
duration?: string;

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface ChartCardSeriesConfig extends ChartCardSeriesExternalConfig {
2929
in_header: boolean;
3030
in_chart: boolean;
3131
datalabels?: boolean;
32+
hidden_by_default?: boolean;
3233
};
3334
}
3435

0 commit comments

Comments
 (0)