Skip to content

Commit ab793c5

Browse files
authored
feat(invert): Negates/Inverts the data for a serie (#13)
1 parent 8ac8d48 commit ab793c5

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed

.devcontainer/ui-lovelace.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ views:
169169
dropShadow:
170170
enabled: true
171171
series:
172+
- entity: sensor.random0_100
173+
extend_to_end: false
174+
type: column
175+
invert: true
176+
group_by:
177+
func: avg
178+
fill: 'null'
172179
- entity: sensor.random0_100
173180
extend_to_end: false
174181
type: column

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ The card stricly validates all the options available (but not for the `apex_conf
116116
| `extend_to_end` | boolean | `true` | v1.0.0 | If the last data is older than the end time displayed on the graph, setting to true will extend the value until the end of the timeline. Only works for `line` and `area` types. |
117117
| `unit` | string | | v1.0.0 | Override the unit of the sensor |
118118
| `group_by` | object | | v1.0.0 | See [group_by](#group_by-options) |
119+
| `invert` | boolean | `false` | NEXT_VERSION | Negates the data (`1` -> `-1`). Usefull to display opposites values like network in (standard)/out (inverted) |
119120

120121

121122
### `show` Options

src/apexcharts-card.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,14 @@ class ChartsCard extends LitElement {
336336
this._lastState[index] = (this._lastState[index] as number).toFixed(1);
337337
}
338338
}
339-
return {
340-
data:
341-
this._config?.series[index].extend_to_end && this._config?.series[index].type !== 'column'
342-
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
343-
[...graph.history, ...[[end.getTime(), graph.history.slice(-1)[0]![1]]]]
344-
: graph.history,
345-
};
339+
let data: (number | null)[][] = [];
340+
if (this._config?.series[index].extend_to_end && this._config?.series[index].type !== 'column') {
341+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
342+
data = [...graph.history, ...[[end.getTime(), graph.history.slice(-1)[0]![1]]]];
343+
} else {
344+
data = graph.history;
345+
}
346+
return this._config?.series[index].invert ? { data: this._invertData(data) } : { data };
346347
}),
347348
xaxis: {
348349
min: start.getTime(),
@@ -358,6 +359,13 @@ class ChartsCard extends LitElement {
358359
this._updating = false;
359360
}
360361

362+
private _invertData(data: (number | null)[][]): (number | null)[][] {
363+
return data.map((item) => {
364+
if (item[1] === null) return item;
365+
return [item[0], -item[1]];
366+
});
367+
}
368+
361369
private _getSpanDates(): { start: Date; end: Date } {
362370
let end = new Date();
363371
let start = new Date(end.getTime() - this._graphSpan + 1);

src/types-config-ti.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const ChartCardSeriesExternalConfig = t.iface([], {
3434
"curve": t.opt(t.union(t.lit('smooth'), t.lit('straight'), t.lit('stepline'))),
3535
"extend_to_end": t.opt("boolean"),
3636
"unit": t.opt("string"),
37+
"invert": t.opt("boolean"),
3738
"group_by": t.opt(t.iface([], {
3839
"duration": t.opt("string"),
3940
"func": t.opt("GroupByFunc"),

src/types-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface ChartCardSeriesExternalConfig {
2828
curve?: 'smooth' | 'straight' | 'stepline';
2929
extend_to_end?: boolean;
3030
unit?: string;
31+
invert?: boolean;
3132
group_by?: {
3233
duration?: string;
3334
func?: GroupByFunc;

0 commit comments

Comments
 (0)