Skip to content

Commit

Permalink
feat: Show value in legend
Browse files Browse the repository at this point in the history
  • Loading branch information
RomRider committed Jan 22, 2021
1 parent c5a8ee2 commit 869a7f8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/apex-layouts.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -49,6 +50,19 @@ export function getLayoutConfig(config: ChartCardConfig): unknown {
postion: 'topRight',
},
},
legend: {
formatter: function (seriesName, opts, conf = config, hass2 = hass) {
return [
seriesName,
' - ',
`<strong>${opts.w.globals.series[opts.seriesIndex].slice(-1)}${
conf.series[opts.seriesIndex].unit ||
hass2?.states[conf.series[opts.seriesIndex].entity].attributes.unit_of_measurement ||
''
}</strong>`,
];
},
},
stroke: {
curve: config.series.map((serie) => {
return serie.curve || 'smooth';
Expand Down
2 changes: 1 addition & 1 deletion src/apexcharts-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down

0 comments on commit 869a7f8

Please sign in to comment.