Skip to content

Commit e5e8904

Browse files
committed
feat(yaxis): id and yaxis_id no longer needed with only 1 yaxis defined
1 parent dad3469 commit e5e8904

File tree

5 files changed

+75
-44
lines changed

5 files changed

+75
-44
lines changed

.devcontainer/ui-lovelace.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1025,10 +1025,10 @@ views:
10251025
stroke_width: 2
10261026
series:
10271027
- entity: sensor.random0_100
1028-
yaxis_id: first
1028+
- entity: sensor.random0_100
1029+
transform: 'return Number(x) + 1230;'
10291030
yaxis:
1030-
- id: first
1031-
min: 0
1032-
max: ~80
1031+
- min: 0
1032+
max: ~1200
10331033
apex_config:
10341034
tickAmount: 4

README.md

+53-34
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ However, some things might be broken :grin:
4242
- [`data_generator` Option](#data_generator-option)
4343
- [`yaxis` Options. Multi-Y axis](#yaxis-options-multi-y-axis)
4444
- [Min/Max Format](#minmax-format)
45-
- [Example](#example)
45+
- [Examples](#examples)
4646
- [Apex Charts Options Example](#apex-charts-options-example)
4747
- [Layouts](#layouts)
4848
- [Configuration Templates](#configuration-templates)
@@ -55,7 +55,7 @@ However, some things might be broken :grin:
5555
- [`brush` experimental feature](#brush-experimental-feature)
5656
- [Known issues](#known-issues)
5757
- [Roadmap](#roadmap)
58-
- [Examples](#examples)
58+
- [Examples](#examples-1)
5959
- [Simple graph](#simple-graph)
6060
- [Multiple Types of Graphs](#multiple-types-of-graphs)
6161
- [Aggregating data](#aggregating-data)
@@ -433,7 +433,7 @@ You can have as many y-axis as there are series defined in your configuration or
433433

434434
| Name | Type | Default | Since | Description |
435435
| ---- | :--: | :-----: | :---: | ----------- |
436-
| :white_check_mark: `id` | string | | v1.9.0 | The identification name of the yaxis used to map it to a serie. Needs to be unique. |
436+
|`id` | string | | v1.9.0 | **Required** if you define multiple yaxis. The identification name of the yaxis used to map it to a serie. Needs to be unique. |
437437
| `show` | boolean | `true` | v1.9.0 | Whether to show or not the axis on the chart |
438438
| `opposite` | boolean | `false` | v1.9.0 | If `true`, the axis will be shown on the right side of the chart |
439439
| `min` | `auto`, number or string | `auto` | v1.9.0 | If undefined or `auto`, the `min` of the yaxis will be automatically calculated based on the min value of all the series associated to this axis. See [below](#minmax-format) for other formats. |
@@ -452,41 +452,60 @@ You can have as many y-axis as there are series defined in your configuration or
452452
* `min: '|-20|'`: The min of the data in the series is `32`, the y-axis min will be `12` (= `32 - 20`)
453453
* `max: '|+10|'`: The max of the data in the series is `32`, the y-axis max will be `42` (= `32 + 10`)
454454

455-
#### Example
455+
#### Examples
456456

457-
In this example, we have 2 sensors:
458-
* `sensor.random0_100`: goes from `0` to `100`
459-
* `sensor.random_0_1000`: goes from `0` to `1000`
457+
* Simple example with one y-axis:
460458

461-
The `min` and `max` of both y-axis are auto calculated based on the spread of the data associated with each axis.
459+
```yaml
460+
type: custom:apexcharts-card
461+
header:
462+
show: true
463+
title: Max Soft Bounds + Min Fixed Bound
464+
graph_span: 20min
465+
series:
466+
- entity: sensor.random0_100
467+
yaxis: # only 1 yaxis, no need for id or yaxis_id
468+
- min: 0
469+
# if the sensor doesn't go above 50, the max of the axis will be 50
470+
# else the max will be the maximum value of the sensor
471+
max: ~50
472+
apex_config:
473+
tickAmount: 4
474+
```
462475

463-
![multi_y_axis](docs/multi_y_axis.png)
476+
* In this example, we have 2 sensors:
477+
* `sensor.random0_100`: goes from `0` to `100`
478+
* `sensor.random_0_1000`: goes from `0` to `1000`
464479

465-
```yaml
466-
type: custom:apexcharts-card
467-
graph_span: 20min
468-
yaxis:
469-
- id: first # identification name of the first y-axis
470-
apex_config:
471-
tickAmount: 4
472-
- id: second # identification name of the second y-axis
473-
opposite: true # make it show on the right side
474-
apex_config:
475-
tickAmount: 4
476-
all_series_config:
477-
stroke_width: 2
478-
series:
479-
- entity: sensor.random0_100
480-
yaxis_id: first # this serie will be associated to the 'id: first' axis.
481-
- entity: sensor.random_0_1000
482-
yaxis_id: second # this serie will be associated to the 'id: second' axis.
483-
- entity: sensor.random0_100
484-
yaxis_id: first # this serie will be associated to the 'id: first' axis.
485-
transform: 'return Number(x) + 30;' # We make it go fom 30 to 130
486-
- entity: sensor.random0_100
487-
yaxis_id: first # this serie will be associated to the 'id: first' axis.
488-
transform: 'return Number(x) - 30;' # We make it go from -30 to 70
489-
```
480+
The `min` and `max` of both y-axis are auto calculated based on the spread of the data associated with each axis.
481+
482+
![multi_y_axis](docs/multi_y_axis.png)
483+
484+
```yaml
485+
type: custom:apexcharts-card
486+
graph_span: 20min
487+
yaxis:
488+
- id: first # identification name of the first y-axis
489+
apex_config:
490+
tickAmount: 4
491+
- id: second # identification name of the second y-axis
492+
opposite: true # make it show on the right side
493+
apex_config:
494+
tickAmount: 4
495+
all_series_config:
496+
stroke_width: 2
497+
series:
498+
- entity: sensor.random0_100
499+
yaxis_id: first # this serie will be associated to the 'id: first' axis.
500+
- entity: sensor.random_0_1000
501+
yaxis_id: second # this serie will be associated to the 'id: second' axis.
502+
- entity: sensor.random0_100
503+
yaxis_id: first # this serie will be associated to the 'id: first' axis.
504+
transform: 'return Number(x) + 30;' # We make it go fom 30 to 130
505+
- entity: sensor.random0_100
506+
yaxis_id: first # this serie will be associated to the 'id: first' axis.
507+
transform: 'return Number(x) - 30;' # We make it go from -30 to 70
508+
```
490509

491510
### Apex Charts Options Example
492511

src/apexcharts-card.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,13 @@ class ChartsCard extends LitElement {
349349
) {
350350
throw new Error(`Multiple yaxis detected: Some series are missing the 'yaxis_id' configuration.`);
351351
}
352+
if (
353+
this._config.yaxis.some((yaxis) => {
354+
return !yaxis.id;
355+
})
356+
) {
357+
throw new Error(`Multiple yaxis detected: Some yaxis are missing an 'id'.`);
358+
}
352359
}
353360
if (this._config.yaxis) {
354361
const yAxisConfig = this._generateYAxisConfig(this._config);
@@ -460,10 +467,15 @@ class ChartsCard extends LitElement {
460467
const burned: boolean[] = [];
461468
this._yAxisConfig = JSON.parse(JSON.stringify(config.yaxis));
462469
const yaxisConfig = config.series.map((serie, serieIndex) => {
463-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
464-
const idx = config.yaxis!.findIndex((yaxis) => {
465-
return yaxis.id === serie.yaxis_id;
466-
});
470+
let idx = -1;
471+
if (config.yaxis?.length !== 1) {
472+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
473+
idx = config.yaxis!.findIndex((yaxis) => {
474+
return yaxis.id === serie.yaxis_id;
475+
});
476+
} else {
477+
idx = 0;
478+
}
467479
if (idx < 0) {
468480
throw new Error(`yaxis_id: ${serie.yaxis_id} doesn't exist.`);
469481
}

src/types-config-ti.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export const ChartCardColorThreshold = t.iface([], {
131131
});
132132

133133
export const ChartCardYAxisExternal = t.iface([], {
134-
"id": "string",
134+
"id": t.opt("string"),
135135
"show": t.opt("boolean"),
136136
"opposite": t.opt("boolean"),
137137
"min": t.opt(t.union(t.lit('auto'), "number", "string")),

src/types-config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export interface ChartCardColorThreshold {
131131
}
132132

133133
export interface ChartCardYAxisExternal {
134-
id: string;
134+
id?: string;
135135
show?: boolean;
136136
opposite?: boolean;
137137
min?: 'auto' | number | string;

0 commit comments

Comments
 (0)