Skip to content

Commit 33fc474

Browse files
committed
feat(yaxis): Set the number of decimals shown on the yaxis. Deprecate y_axis_precision.
Fix #164
1 parent 5a7d944 commit 33fc474

File tree

6 files changed

+25
-13
lines changed

6 files changed

+25
-13
lines changed

.devcontainer/ui-lovelace.yaml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,16 @@ views:
344344
graph_span: 24h
345345
span:
346346
start: day
347-
y_axis_precision: 5
347+
# y_axis_precision: 5
348348
now:
349349
show: true
350350
color: '#ff0000'
351351
label: Now
352352
header:
353353
show: true
354354
show_states: true
355+
yaxis:
356+
- decimals: 5
355357
series:
356358
- entity: sensor.pvpc
357359
float_precision: 5
@@ -1001,21 +1003,22 @@ views:
10011003
yaxis_id: second
10021004
- entity: sensor.random0_100
10031005
yaxis_id: first
1004-
transform: 'return Number(x) + 30;'
1006+
transform: 'return Number(x) + 30.12;'
10051007
- entity: sensor.random0_100
10061008
yaxis_id: first
10071009
transform: 'return Number(x) + 60;'
10081010
yaxis:
10091011
- id: first
10101012
apex_config:
1011-
tickAmount: 4
1013+
tickAmount: 2
10121014
- id: second
10131015
max: '|+100|'
10141016
min: ~100
10151017
opposite: true
1018+
decimals: 0
10161019
show: true
10171020
apex_config:
1018-
tickAmount: 4
1021+
tickAmount: 6
10191022
- type: custom:apexcharts-card
10201023
header:
10211024
show: true

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ The card stricly validates all the options available (but not for the `apex_conf
142142
| `layout` | string | | v1.0.0 | See [layouts](#layouts) |
143143
| `header` | object | | v1.0.0 | See [header](#header-options) |
144144
| `now` | object | | v1.5.0 | See [now](#now-options) |
145-
| `y_axis_precision` | number | `1` | v1.2.0 | The float precision used to display numbers on the Y axis. Only works if `yaxis` is undefined. |
145+
| ~~`y_axis_precision`~~ | ~~number~~ | ~~`1`~~ | ~~v1.2.0~~ | **DEPRECATED since NEXT_VERSION** ~~The float precision used to display numbers on the Y axis. Only works if `yaxis` is undefined.~~ |
146146
| `yaxis` | array | | v1.9.0 | See [yaxis](#yaxis-options-multi-y-axis) |
147147
| `apex_config`| object | | v1.0.0 | Apexcharts API 1:1 mapping. You call see all the options [here](https://apexcharts.com/docs/installation/) --> `Options (Reference)` in the Menu. See [Apex Charts](#apex-charts-options-example) |
148148
| `experimental` | object | | v1.6.0 | See [experimental](#experimental-features) |
@@ -438,6 +438,7 @@ You can have as many y-axis as there are series defined in your configuration or
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. |
440440
| `max` | `auto`, number or string | `auto` | v1.9.0 | If undefined or `auto`, the `min` of the yaxis will be automatically calculated based on the max value of all the series associated to this axis. See [below](#minmax-format) for other formats. |
441+
| `decimals` | number | `1` | NEXT_VERSION | Number of decimals to show on this y-axis |
441442
| `apex_config` | object | | v1.9.0 | Any configuration from https://apexcharts.com/docs/options/yaxis/, except `min`, `max`, `show` and `opposite` |
442443

443444
#### Min/Max Format
@@ -469,6 +470,7 @@ You can have as many y-axis as there are series defined in your configuration or
469470
# if the sensor doesn't go above 50, the max of the axis will be 50
470471
# else the max will be the maximum value of the sensor
471472
max: ~50
473+
decimals: 0
472474
apex_config:
473475
tickAmount: 4
474476
```
@@ -486,10 +488,12 @@ You can have as many y-axis as there are series defined in your configuration or
486488
graph_span: 20min
487489
yaxis:
488490
- id: first # identification name of the first y-axis
491+
decimals: 0
489492
apex_config:
490493
tickAmount: 4
491494
- id: second # identification name of the second y-axis
492495
opposite: true # make it show on the right side
496+
decimals: 0
493497
apex_config:
494498
tickAmount: 4
495499
all_series_config:

src/apex-layouts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ function getYAxis(config: ChartCardConfig) {
221221
return Array.isArray(config.apex_config?.yaxis) || config.yaxis
222222
? undefined
223223
: {
224-
decimalsInFloat: config.y_axis_precision === undefined ? DEFAULT_FLOAT_PRECISION : config.y_axis_precision,
224+
decimalsInFloat: DEFAULT_FLOAT_PRECISION,
225225
};
226226
}
227227

src/apexcharts-card.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import exportedTypeSuite from './types-config-ti';
4545
import {
4646
DEFAULT_AREA_OPACITY,
4747
DEFAULT_FILL_RAW,
48+
DEFAULT_FLOAT_PRECISION,
4849
DEFAULT_SHOW_IN_CHART,
4950
DEFAULT_SHOW_IN_HEADER,
5051
DEFAULT_SHOW_LEGEND_VALUE,
@@ -463,11 +464,11 @@ class ChartsCard extends LitElement {
463464
}
464465

465466
// eslint-disable-next-line @typescript-eslint/no-explicit-any
466-
private _generateYAxisConfig(config: ChartCardConfig): any {
467+
private _generateYAxisConfig(config: ChartCardConfig): ApexYAxis[] | undefined {
467468
if (!config.yaxis) return undefined;
468469
const burned: boolean[] = [];
469470
this._yAxisConfig = JSON.parse(JSON.stringify(config.yaxis));
470-
const yaxisConfig = config.series.map((serie, serieIndex) => {
471+
const yaxisConfig: ApexYAxis[] = config.series.map((serie, serieIndex) => {
471472
let idx = -1;
472473
if (config.yaxis?.length !== 1) {
473474
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@@ -480,9 +481,13 @@ class ChartsCard extends LitElement {
480481
if (idx < 0) {
481482
throw new Error(`yaxis_id: ${serie.yaxis_id} doesn't exist.`);
482483
}
483-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
484-
let yAxisDup: ChartCardYAxis = JSON.parse(JSON.stringify(config.yaxis![idx]));
484+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-explicit-any
485+
let yAxisDup: any = JSON.parse(JSON.stringify(config.yaxis![idx]));
485486
delete yAxisDup.apex_config;
487+
delete yAxisDup.decimals;
488+
yAxisDup.decimalsInFloat =
489+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
490+
config.yaxis![idx].decimals === undefined ? DEFAULT_FLOAT_PRECISION : config.yaxis![idx].decimals;
486491
if (this._yAxisConfig?.[idx].series_id) {
487492
this._yAxisConfig?.[idx].series_id?.push(serieIndex);
488493
} else {
@@ -492,7 +497,7 @@ class ChartsCard extends LitElement {
492497
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
493498
if (config.yaxis![idx].apex_config) {
494499
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
495-
yAxisDup = mergeDeep(JSON.parse(JSON.stringify(config.yaxis![idx])), config.yaxis![idx].apex_config);
500+
yAxisDup = mergeDeep(yAxisDup, config.yaxis![idx].apex_config);
496501
delete yAxisDup.apex_config;
497502
}
498503
if (typeof yAxisDup.min !== 'number') delete yAxisDup.min;

src/types-config-ti.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export const ChartCardExternalConfig = t.iface([], {
2323
"series": t.array("ChartCardSeriesExternalConfig"),
2424
"graph_span": t.opt("string"),
2525
"span": t.opt("ChartCardSpanExtConfig"),
26-
"y_axis_precision": t.opt("number"),
2726
"now": t.opt(t.iface([], {
2827
"show": t.opt("boolean"),
2928
"color": t.opt("string"),
@@ -136,6 +135,7 @@ export const ChartCardYAxisExternal = t.iface([], {
136135
"opposite": t.opt("boolean"),
137136
"min": t.opt(t.union(t.lit('auto'), "number", "string")),
138137
"max": t.opt(t.union(t.lit('auto'), "number", "string")),
138+
"decimals": t.opt("number"),
139139
"apex_config": t.opt("any"),
140140
});
141141

src/types-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export interface ChartCardExternalConfig {
1717
series: ChartCardSeriesExternalConfig[];
1818
graph_span?: string;
1919
span?: ChartCardSpanExtConfig;
20-
y_axis_precision?: number;
2120
now?: {
2221
show?: boolean;
2322
color?: string;
@@ -136,6 +135,7 @@ export interface ChartCardYAxisExternal {
136135
opposite?: boolean;
137136
min?: 'auto' | number | string;
138137
max?: 'auto' | number | string;
138+
decimals?: number;
139139
// eslint-disable-next-line @typescript-eslint/no-explicit-any
140140
apex_config?: any;
141141
}

0 commit comments

Comments
 (0)