Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(experimental): hidden_by_default to toggle series on load #62

Merged
merged 1 commit into from
Feb 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .devcontainer/ui-lovelace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ views:
stacked: false
experimental:
color_threshold: true
hidden_by_default: true
# disable_config_validation: true
series:
- entity: sensor.random_0_1000
Expand All @@ -90,6 +91,8 @@ views:
- entity: sensor.random0_100
name: test2
type: column
show:
hidden_by_default: true
group_by:
duration: 1m
func: last
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ However, some things might be broken :grin:
- [Experimental features](#experimental-features)
- [Configuration options](#configuration-options)
- [`color_threshold` experimental feature](#color_threshold-experimental-feature)
- [`hidden_by_default` experimental feature](#hidden_by_default-experimental-feature)
- [Known issues](#known-issues)
- [Roadmap](#roadmap)
- [Examples](#examples)
Expand Down Expand Up @@ -173,6 +174,7 @@ The card stricly validates all the options available (but not for the `apex_conf
| `in_header` | boolean | `true` | v1.4.0 | If `show_states` is enabled, this would show/hide this specific serie in the header |
| `in_chart` | boolean | `true` | v1.4.0 | If `false`, hides the serie from the chart |
| `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 |
| `hidden_by_default` | boolean | `false` | NEXT_VERSION | See [experimental](#hidden_by_default-experimental-feature) |


### Main `show` Options
Expand Down Expand Up @@ -442,6 +444,7 @@ For code junkies, you'll find the default options I use in [`src/apex-layouts.ts
| ---- | :--: | :-----: | :---: | ----------- |
| `color_threshold` | boolean | `false` | NEXT_VERSION | Will enable the color threshold feature. See [color_threshold](#color_threshold-experimental-feature) |
| `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. |
| `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) |

### `color_threshold` experimental feature

Expand Down Expand Up @@ -490,6 +493,24 @@ series:

![color_threshold](docs/color_threshold.png)

### `hidden_by_default` experimental feature

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.

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.

This is how to use it:
```yaml
type: custom:apexcharts-card
experimental:
hidden_by_default: true
series:
- entity: sensor.temperature
show:
hidden_by_default: true
- entity: sensor.temperature_office
```

## Known issues

* 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)
Expand Down
11 changes: 10 additions & 1 deletion src/apexcharts-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,16 @@ class ChartsCard extends LitElement {
if (this._updating || this._dataLoaded || !this._apexChart || !this._config || !this._hass) return;
this._dataLoaded = true;
this._updating = true;
this._updateData();
this._updateData().then(() => {
if (this._config?.experimental?.hidden_by_default) {
this._config.series_in_graph.forEach((serie, index) => {
if (serie.show.hidden_by_default) {
const name = computeName(index, this._config?.series_in_graph, this._hass?.states);
this._apexChart?.hideSeries(name);
}
});
}
});
}

public set hass(hass: HomeAssistant) {
Expand Down
2 changes: 2 additions & 0 deletions src/types-config-ti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const ChartCardExternalConfig = t.iface([], {
"experimental": t.opt(t.iface([], {
"color_threshold": t.opt("boolean"),
"disable_config_validation": t.opt("boolean"),
"hidden_by_default": t.opt("boolean"),
})),
"chart_type": t.opt(t.union(t.lit('line'), t.lit('scatter'), t.lit('pie'), t.lit('donut'), t.lit('radialBar'))),
"update_interval": t.opt("string"),
Expand Down Expand Up @@ -64,6 +65,7 @@ export const ChartCardSeriesExternalConfig = t.iface([], {
"in_header": t.opt("boolean"),
"in_chart": t.opt("boolean"),
"datalabels": t.opt("boolean"),
"hidden_by_default": t.opt("boolean"),
})),
"group_by": t.opt(t.iface([], {
"duration": t.opt("string"),
Expand Down
2 changes: 2 additions & 0 deletions src/types-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface ChartCardExternalConfig {
experimental?: {
color_threshold?: boolean;
disable_config_validation?: boolean;
hidden_by_default?: boolean;
};
chart_type?: 'line' | 'scatter' | 'pie' | 'donut' | 'radialBar';
update_interval?: string;
Expand Down Expand Up @@ -60,6 +61,7 @@ export interface ChartCardSeriesExternalConfig {
in_header?: boolean;
in_chart?: boolean;
datalabels?: boolean;
hidden_by_default?: boolean;
};
group_by?: {
duration?: string;
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface ChartCardSeriesConfig extends ChartCardSeriesExternalConfig {
in_header: boolean;
in_chart: boolean;
datalabels?: boolean;
hidden_by_default?: boolean;
};
}

Expand Down