Skip to content

Commit

Permalink
fix(xasis): Better handling of graph end time if everything is `colum…
Browse files Browse the repository at this point in the history
…n` and `group_by` is enabled
  • Loading branch information
RomRider committed Jan 28, 2021
1 parent 9a3071a commit b5b85a6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
33 changes: 31 additions & 2 deletions .devcontainer/ui-lovelace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ views:
- title: Main
cards:
- type: custom:apexcharts-card
apex_config:
chart:
toolbar:
show: true
tools:
zoom: true
zoomin: true
zoomout: true
pan: true
reset: true
zoom:
enabled: true
update_interval: 5sec
stacked: true
series:
Expand Down Expand Up @@ -168,20 +180,37 @@ views:
enabled: true
dropShadow:
enabled: true
yaxis:
- title:
text: test1
show: true
opposite: true
decimalsInFloat: 1
- title:
text: Test2
show: true
# opposite: true
decimalsInFloat: 1
series:
- entity: sensor.random0_100
extend_to_end: false
type: column
invert: true
group_by:
func: avg
fill: 'null'
fill: 'last'
- entity: sensor.random0_100
extend_to_end: false
type: column
group_by:
func: avg
fill: 'null'
fill: 'last'
- entity: sensor.random0_100
extend_to_end: false
type: column
group_by:
func: avg
fill: 'last'

- type: custom:apexcharts-card
span:
Expand Down
26 changes: 25 additions & 1 deletion src/apexcharts-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
DEFAULT_SERIE_TYPE,
HOUR_24,
} from './const';
import parse from 'parse-duration';

/* eslint no-console: 0 */
console.info(
Expand Down Expand Up @@ -370,7 +371,7 @@ class ChartsCard extends LitElement {
}),
xaxis: {
min: start.getTime(),
max: end.getTime(),
max: this._findEndOfChart(end),
},
colors: computeColors(this._colors),
};
Expand All @@ -382,6 +383,29 @@ class ChartsCard extends LitElement {
this._updating = false;
}

/*
Makes the chart end at the last timestamp of the data when everything displayed is a
column and group_by is enabled for every serie
*/
private _findEndOfChart(end: Date): number {
const localEnd = new Date(end);
let offsetEnd: number | undefined = 0;
const onlyBars = this._config?.series.reduce((acc, serie) => {
return acc && serie.type === 'column' && serie.group_by.func !== 'raw';
}, this._config?.series.length > 0);
if (onlyBars) {
offsetEnd = this._config?.series.reduce((acc, serie) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const dur = parse(serie.group_by.duration)!;
if (acc === -1 || dur < acc) {
return dur;
}
return acc;
}, -1);
}
return new Date(localEnd.getTime() - (offsetEnd ? offsetEnd : 0)).getTime();
}

private _invertData(data: (number | null)[][]): (number | null)[][] {
return data.map((item) => {
if (item[1] === null) return item;
Expand Down
5 changes: 4 additions & 1 deletion src/graphEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ export default class GraphEntry {
}
}

if (!history || history.data.length === 0) return false;
if (!history || history.data.length === 0) {
this._updating = false;
return false;
}
this._history = history;
if (this._config.group_by.func !== 'raw') {
this._computedHistory = this._dataBucketer().map((bucket) => {
Expand Down

0 comments on commit b5b85a6

Please sign in to comment.