Skip to content

Commit

Permalink
proper positioning for 'remaining' box #2
Browse files Browse the repository at this point in the history
  • Loading branch information
MindFreeze committed May 21, 2022
1 parent 464d673 commit 1aa4e84
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/ha-sankey-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,19 @@ export class SankeyChart extends LitElement {
const extraEntities: EntityConfigInternal[][] = this.config.sections.map(() => []);
this.sections = this.config.sections.map((section, sectionIndex) => {
let total = 0;
let boxes: Box[] = [...section.entities, ...extraEntities[sectionIndex]]
const allSectionEntities = section.entities.reduce((acc, conf) => {
const entityConf: EntityConfigInternal = typeof conf === 'string' ? {entity_id: conf} : conf;
const other = extraEntities[sectionIndex]
.find(e => e.children![e.children!.length - 1] === entityConf.entity_id);
// position 'remaining' boxes right after all other children of the same parent
return other ? [...acc, entityConf, other] : [...acc, entityConf];
}, [] as EntityConfigInternal[]);
let boxes: Box[] = allSectionEntities
.filter(entity => {
const state = Number(this._getEntityState(entity).state);
return !isNaN(state) && state > 0;
})
.map(conf => {
const entityConf: EntityConfigInternal = typeof conf === 'string' ? {entity_id: conf} : conf;
.map(entityConf => {
const entity = this._getEntityState(entityConf);
const {state, unit_of_measurement} = this._normalizeStateValue(
entityConf.accountedState ? Number(entity.state) - entityConf.accountedState : Number(entity.state),
Expand All @@ -264,7 +270,6 @@ export class SankeyChart extends LitElement {
let children = entityConf.children || [];
if (entityConf.remaining && extraEntities[sectionIndex + 1]) {
children = [...children, entityConf.entity_id]
// @TODO proper positioning
extraEntities[sectionIndex + 1].push({
...entityConf,
color: undefined,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type EntityConfig = {
}

export type EntityConfigInternal = EntityConfig & {
// children: string[];
isRemaining?: boolean;
accountedState?: number;
}
Expand Down

0 comments on commit 1aa4e84

Please sign in to comment.