From 869a7f83dc3740ffe7b5d2f3827070d9c391101e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Wiedemann?= Date: Fri, 22 Jan 2021 20:55:20 +0000 Subject: [PATCH] feat: Show value in legend --- src/apex-layouts.ts | 16 +++++++++++++++- src/apexcharts-card.ts | 2 +- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/apex-layouts.ts b/src/apex-layouts.ts index 3aebd1b..a40abe2 100644 --- a/src/apex-layouts.ts +++ b/src/apex-layouts.ts @@ -1,8 +1,9 @@ +import { HomeAssistant } from 'custom-card-helpers'; import moment from 'moment'; import { ChartCardConfig } from './types'; import { getMilli, mergeDeep } from './utils'; -export function getLayoutConfig(config: ChartCardConfig): unknown { +export function getLayoutConfig(config: ChartCardConfig, hass: HomeAssistant | undefined = undefined): unknown { const def = { chart: { stacked: config?.stacked, @@ -49,6 +50,19 @@ export function getLayoutConfig(config: ChartCardConfig): unknown { postion: 'topRight', }, }, + legend: { + formatter: function (seriesName, opts, conf = config, hass2 = hass) { + return [ + seriesName, + ' - ', + `${opts.w.globals.series[opts.seriesIndex].slice(-1)}${ + conf.series[opts.seriesIndex].unit || + hass2?.states[conf.series[opts.seriesIndex].entity].attributes.unit_of_measurement || + '' + }`, + ]; + }, + }, stroke: { curve: config.series.map((serie) => { return serie.curve || 'smooth'; diff --git a/src/apexcharts-card.ts b/src/apexcharts-card.ts index b90eb0a..0be5de1 100644 --- a/src/apexcharts-card.ts +++ b/src/apexcharts-card.ts @@ -180,7 +180,7 @@ class ChartsCard extends LitElement { if (!this._apexChart && this.shadowRoot && this._config && this.shadowRoot.querySelector('#graph')) { this._loaded = true; const graph = this.shadowRoot.querySelector('#graph'); - this._apexChart = new ApexCharts(graph, getLayoutConfig(this._config)); + this._apexChart = new ApexCharts(graph, getLayoutConfig(this._config, this._hass)); this._apexChart.render(); } }