Skip to content

Commit

Permalink
feat(invert): Negates/Inverts the data for a serie (RomRider#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
RomRider authored Jan 27, 2021
1 parent 8ac8d48 commit ab793c5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .devcontainer/ui-lovelace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ views:
dropShadow:
enabled: true
series:
- entity: sensor.random0_100
extend_to_end: false
type: column
invert: true
group_by:
func: avg
fill: 'null'
- entity: sensor.random0_100
extend_to_end: false
type: column
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ The card stricly validates all the options available (but not for the `apex_conf
| `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. |
| `unit` | string | | v1.0.0 | Override the unit of the sensor |
| `group_by` | object | | v1.0.0 | See [group_by](#group_by-options) |
| `invert` | boolean | `false` | NEXT_VERSION | Negates the data (`1` -> `-1`). Usefull to display opposites values like network in (standard)/out (inverted) |


### `show` Options
Expand Down
22 changes: 15 additions & 7 deletions src/apexcharts-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,14 @@ class ChartsCard extends LitElement {
this._lastState[index] = (this._lastState[index] as number).toFixed(1);
}
}
return {
data:
this._config?.series[index].extend_to_end && this._config?.series[index].type !== 'column'
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
[...graph.history, ...[[end.getTime(), graph.history.slice(-1)[0]![1]]]]
: graph.history,
};
let data: (number | null)[][] = [];
if (this._config?.series[index].extend_to_end && this._config?.series[index].type !== 'column') {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
data = [...graph.history, ...[[end.getTime(), graph.history.slice(-1)[0]![1]]]];
} else {
data = graph.history;
}
return this._config?.series[index].invert ? { data: this._invertData(data) } : { data };
}),
xaxis: {
min: start.getTime(),
Expand All @@ -358,6 +359,13 @@ class ChartsCard extends LitElement {
this._updating = false;
}

private _invertData(data: (number | null)[][]): (number | null)[][] {
return data.map((item) => {
if (item[1] === null) return item;
return [item[0], -item[1]];
});
}

private _getSpanDates(): { start: Date; end: Date } {
let end = new Date();
let start = new Date(end.getTime() - this._graphSpan + 1);
Expand Down
1 change: 1 addition & 0 deletions src/types-config-ti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const ChartCardSeriesExternalConfig = t.iface([], {
"curve": t.opt(t.union(t.lit('smooth'), t.lit('straight'), t.lit('stepline'))),
"extend_to_end": t.opt("boolean"),
"unit": t.opt("string"),
"invert": t.opt("boolean"),
"group_by": t.opt(t.iface([], {
"duration": t.opt("string"),
"func": t.opt("GroupByFunc"),
Expand Down
1 change: 1 addition & 0 deletions src/types-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface ChartCardSeriesExternalConfig {
curve?: 'smooth' | 'straight' | 'stepline';
extend_to_end?: boolean;
unit?: string;
invert?: boolean;
group_by?: {
duration?: string;
func?: GroupByFunc;
Expand Down

0 comments on commit ab793c5

Please sign in to comment.