Skip to content

Commit 17004a3

Browse files
authored
feat(config_tpl): Create a config template and use it everywhere with config_templates, apply the same config to every series with all_series_config and add color_list to define your color list in one shot (#64)
* Initial config templates support * Display version in error card * Update documentation
1 parent 63717b7 commit 17004a3

File tree

6 files changed

+405
-116
lines changed

6 files changed

+405
-116
lines changed

.devcontainer/ui-lovelace.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
apexcharts_card_templates:
2+
test2:
3+
color_list:
4+
- red
5+
- red
6+
test:
7+
config_templates: test2
8+
graph_span: 24h
9+
color_list:
10+
- black
11+
all_series_config:
12+
group_by:
13+
duration: 1h
14+
func: avg
15+
stroke_width: 2
16+
117
views:
218
- title: Main
319
panel: true
@@ -586,3 +602,10 @@ views:
586602
color: green
587603
- value: 25
588604
color: orange
605+
606+
- type: custom:apexcharts-card
607+
config_templates:
608+
- test
609+
series:
610+
- entity: sensor.temperature
611+
- entity: sensor.temperature

README.md

Lines changed: 140 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![License](https://img.shields.io/github/license/custom-cards/button-card.svg)](LICENSE)
1+
[![License](https://img.shields.io/github/license/RomRider/apexcharts-card.svg)](LICENSE)
22
[![HACS Supported](https://img.shields.io/badge/HACS-Supported-green.svg)](https://github.com/custom-components/hacs)
33
![Downloads](https://img.shields.io/github/downloads/RomRider/apexcharts-card/total)
44
![GitHub Activity](https://img.shields.io/github/commit-activity/y/RomRider/apexcharts-card.svg?label=commits)
@@ -42,6 +42,9 @@ However, some things might be broken :grin:
4242
- [`data_generator` Option](#data_generator-option)
4343
- [Apex Charts Options Example](#apex-charts-options-example)
4444
- [Layouts](#layouts)
45+
- [Configuration Templates](#configuration-templates)
46+
- [General](#general)
47+
- [`all_series_config` options](#all_series_config-options)
4548
- [Experimental features](#experimental-features)
4649
- [Configuration options](#configuration-options)
4750
- [`color_threshold` experimental feature](#color_threshold-experimental-feature)
@@ -122,6 +125,9 @@ The card stricly validates all the options available (but not for the `apex_conf
122125
| ---- | :--: | :-----: | :---: | ----------- |
123126
| :white_check_mark: `type` | string | | v1.0.0 | `custom:apexcharts-card` |
124127
| :white_check_mark: `series` | array | | v1.0.0 | See [series](#series-options) |
128+
| `config_templates` | array | | NEXT_VERSION | Define a configuration once and reuse it multiple times. See [config_templates](#configuration-templates) |
129+
| `color_list` | array | | NEXT_VERSION | Define the array of colors applied to the series. Will be overriden by each serie's color if defined. Usefull for `config_templates` mainly. |
130+
| `all_series_config` | object | | NEXT_VERSION | If something is defined here it will apply this config to all the series. It accepts the same options as a serie minus `entity`. It is useful to avoid repetition but the same thing can be achieve in each serie individually. See [series](#series-options) and [all_series_config](#all_series_config-options) for an example |
125131
| `chart_type` | string | `line` | v1.4.0 | See [chart_type](#chart_type-options) |
126132
| `update_interval` | string | | v1.1.0 | By default the card updates on every state change. Setting this overrides the behaviour. Valid values are any time string, eg: `1h`, `12min`, `1d`, `1h25`, `10sec`, ... |
127133
| `update_delay` | string | `1500ms` | v1.4.0 | If the chart doesn't display the last state but the one before, you'll want to increase this value, don't go over `10s`, it's not necessary. You'll also want to increase this value if you are using `attribute` in the `series`. Valid values are any time strings. This is because of how Home-Assistant works with history, see [here](https://www.home-assistant.io/integrations/recorder/#commit_interval) |
@@ -411,7 +417,8 @@ This is how you could change some options from ApexCharts as described on the [`
411417

412418
Hundreds of options are available and it is not possible to describe them all here so check over there and ask on the [forum](https://community.home-assistant.io/t/apexcharts-card-a-highly-customizable-graph-card/272877) if you need help with using them.
413419

414-
Some options might not work in the context of this card.
420+
* :warning: Some options might not work in the context of this card.
421+
* :warning: Everything which is available through the default config of this card shouldn't be defined in `apex_config`. If you do, it might break.
415422

416423
```yaml
417424
type: custom:apexcharts-card
@@ -434,6 +441,132 @@ For now, only `minimal` is supported: It will remove the grid, the axis and disp
434441

435442
For code junkies, you'll find the default options I use in [`src/apex-layouts.ts`](src/apex-layouts.ts)
436443

444+
### Configuration Templates
445+
446+
#### General
447+
448+
- Define your config template in the main lovelace configuration and then use it in your cards. This will avoid a lot of repetitions! It's basically YAML anchors, but without using YAML anchors and is very useful if you split your config in multiple files 😄
449+
- You can overload any parameter with a new one
450+
- Arrays will be merged by matching the index
451+
- You can also inherit another template from within a template.
452+
- You can inherit multiple templates at once by making it an array. In this case, the templates will be merged together with the current configuration in the order they are defined. This happens recursively.
453+
454+
```yaml
455+
type: custom:apexcharts-card
456+
config_templates:
457+
- template1
458+
- template2
459+
# or
460+
type: custom:apexcharts-card
461+
config_templates: template1
462+
```
463+
464+
The card templates will be applied in the order they are defined: `template2` will be merged with `template1` and then the local config will be merged with the result. You can still chain templates together (ie. define template in a apexcharts-card template. It will follow the path recursively).
465+
466+
Make sure which type of lovelace dashboard you are using before changing the main lovelace configuration:
467+
* **`managed`** changes are managed by lovelace UI - add the template configuration to configuration in raw editor
468+
* go to your dashboard
469+
* click three dots and `Edit dashboard` button
470+
* click three dots again and click `Raw configuration editor` button
471+
* **`yaml`** - add template configuration to your dashboard file (`ui-lovelace.yaml` for eg.)
472+
473+
**Note:** Templates have to be defined in all dashboards, they are not shared.
474+
475+
To give you an idea where to put those (in your dashboard file/RAW editor):
476+
```yaml
477+
apexcharts_card_templates:
478+
default:
479+
color_list: ['red', 'green', 'blue']
480+
481+
bandwidth_chart:
482+
graph_span: 24h
483+
config_templates: default
484+
header:
485+
show: true
486+
show_states: true
487+
colorize_states: true
488+
all_series_config:
489+
stroke_width: 2
490+
opacity: 0.3
491+
type: area
492+
493+
views:
494+
- title: Main
495+
panel: true
496+
cards:
497+
[...]
498+
```
499+
500+
And then where you define your card, you can consume those templates, and/or overload it:
501+
502+
```yaml
503+
- type: custom:apexcharts-card
504+
template: bandwidth_chart
505+
header:
506+
title: WAN Bandwidth
507+
series:
508+
- entity: sensor.wan_download
509+
- entity: sensor.wan_upload
510+
invert: true
511+
```
512+
513+
In the end, this would produce the same result as but it's shorter and you can reuse that template elsewhere:
514+
```yaml
515+
- type: custom:apexcharts-card
516+
graph_span: 24h
517+
header:
518+
title: WAN Bandwidth
519+
show: true
520+
show_states: true
521+
colorize_states: true
522+
all_series_config:
523+
stroke_width: 2
524+
opacity: 0.3
525+
type: area
526+
color_list: ['red', 'green', 'blue']
527+
series:
528+
- entity: sensor.wan_download
529+
- entity: sensor.wan_upload
530+
invert: true
531+
```
532+
533+
#### `all_series_config` options
534+
535+
This will allow you to apply some settings to all the series avoiding repetition. It's just syntaxic sugar and doesn't add more features.
536+
537+
Eg:
538+
```yaml
539+
- type: custom:apexcharts-card
540+
graph_span: 24h
541+
all_series_config:
542+
stroke_width: 2
543+
type: area
544+
transform: return x / 1024;
545+
unit: Mb/s
546+
series:
547+
- entity: sensor.wan_download
548+
- entity: sensor.wan_upload
549+
invert: true
550+
```
551+
552+
Generates the same result as repeating the configuration in each series:
553+
```yaml
554+
- type: custom:apexcharts-card
555+
graph_span: 24h
556+
series:
557+
- entity: sensor.wan_download
558+
stroke_width: 2
559+
type: area
560+
transform: return x / 1024;
561+
unit: Mb/s
562+
- entity: sensor.wan_upload
563+
invert: true
564+
stroke_width: 2
565+
type: area
566+
transform: return x / 1024;
567+
unit: Mb/s
568+
```
569+
437570
## Experimental features
438571

439572
:warning: You enter the danger zone :warning:
@@ -529,7 +662,7 @@ Not ordered by priority:
529662
* [ ] Support for logarithmic
530663
* [X] ~~Support for state mapping for non-numerical state sensors~~
531664
* [X] ~~Support for simple color threshold (easier to understand/write than the ones provided natively by ApexCharts)~~
532-
* [ ] Support for graph configuration templates à la [`button-card`](https://github.com/custom-cards/button-card/blob/master/README.md#configuration-templates)
665+
* [X] ~~Support for graph configuration templates à la [`button-card`](https://github.com/custom-cards/button-card/blob/master/README.md#configuration-templates)~~
533666

534667
## Examples
535668

@@ -636,10 +769,8 @@ series:
636769
```yaml
637770
type: custom:apexcharts-card
638771
graph_span: 1d
639-
apex_config:
640-
stroke:
641-
# Will affect all the series
642-
width: 2
772+
all_series_config:
773+
stroke_width: 2
643774
series:
644775
- entity: sensor.temperature
645776
- entity: sensor.humidity
@@ -650,15 +781,11 @@ series:
650781
```yaml
651782
type: custom:apexcharts-card
652783
graph_span: 1d
653-
apex_config:
654-
stroke:
655-
# 2 will affect sensor.temperature
656-
# 4 will affect sensor.humidity
657-
# You need as much values here as the number of series
658-
width: [2, 4]
659784
series:
660785
- entity: sensor.temperature
786+
stroke_width: 2
661787
- entity: sensor.humidity
788+
stroke_width: 6
662789
```
663790

664791
### Use apexcharts-card with auto-entities

0 commit comments

Comments
 (0)