Skip to content

Commit

Permalink
feat(series): hide specific serie in legend with in_legend: false
Browse files Browse the repository at this point in the history
Closes #74
  • Loading branch information
RomRider committed Jul 9, 2024
1 parent 2935b4e commit 12cb893
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .devcontainer/ui-lovelace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,7 @@ views:
cards:
- type: custom:apexcharts-card
stacked: true
# cache: false
cache: false
graph_span: 5min
series:
- entity: sensor.random0_100
Expand All @@ -1364,6 +1364,8 @@ views:
name: tes2t
group_by:
duration: 1min
show:
in_legend: false
- entity: sensor.random0_100
stack_group: group1
type: column
Expand Down
9 changes: 9 additions & 0 deletions src/apex-layouts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import parse from 'parse-duration';
import {
DEFAULT_AREA_OPACITY,
DEFAULT_FLOAT_PRECISION,
DEFAULT_LEGEND_MARKER_WIDTH,
DEFAULT_SERIE_TYPE,
HOUR_24,
NO_VALUE,
Expand Down Expand Up @@ -72,6 +73,7 @@ export function getLayoutConfig(
position: 'bottom',
show: true,
formatter: getLegendFormatter(config, hass),
markers: getLegendMarkers(config),
},
stroke: {
curve: getStrokeCurve(config, false),
Expand Down Expand Up @@ -390,6 +392,9 @@ function getLegendFormatter(config: ChartCardConfig, hass: HomeAssistant | undef
undefined,
hass2?.states[conf.series_in_graph[opts.seriesIndex].entity],
);
if (!conf.series_in_graph[opts.seriesIndex].show.in_legend) {
return [];
}
if (!conf.series_in_graph[opts.seriesIndex].show.legend_value) {
return [name];
} else {
Expand Down Expand Up @@ -430,6 +435,10 @@ function getLegendFormatter(config: ChartCardConfig, hass: HomeAssistant | undef
};
}

function getLegendMarkers(config: ChartCardConfig) {
return { width: config.series_in_graph.map((serie) => (serie.show.in_legend ? DEFAULT_LEGEND_MARKER_WIDTH : 0)) };
}

function getStrokeCurve(config: ChartCardConfig, brush: boolean) {
const series = brush ? config.series_in_brush : config.series_in_graph;
return series.map((serie) => {
Expand Down
3 changes: 3 additions & 0 deletions src/apexcharts-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
DEFAULT_FLOAT_PRECISION,
DEFAULT_SHOW_IN_CHART,
DEFAULT_SHOW_IN_HEADER,
DEFAULT_SHOW_IN_LEGEND,
DEFAULT_SHOW_LEGEND_VALUE,
DEFAULT_SHOW_NAME_IN_HEADER,
DEFAULT_SHOW_OFFSET_IN_NAME,
Expand Down Expand Up @@ -429,13 +430,15 @@ class ChartsCard extends LitElement {
}
if (!serie.show) {
serie.show = {
in_legend: DEFAULT_SHOW_IN_LEGEND,
legend_value: DEFAULT_SHOW_LEGEND_VALUE,
in_header: DEFAULT_SHOW_IN_HEADER,
in_chart: DEFAULT_SHOW_IN_CHART,
name_in_header: DEFAULT_SHOW_NAME_IN_HEADER,
offset_in_name: DEFAULT_SHOW_OFFSET_IN_NAME,
};
} else {
serie.show.in_legend = serie.show.in_legend === undefined ? DEFAULT_SHOW_IN_LEGEND : serie.show.in_legend;
serie.show.legend_value =
serie.show.legend_value === undefined ? DEFAULT_SHOW_LEGEND_VALUE : serie.show.legend_value;
serie.show.in_chart = serie.show.in_chart === undefined ? DEFAULT_SHOW_IN_CHART : serie.show.in_chart;
Expand Down
2 changes: 2 additions & 0 deletions src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const DEFAULT_DURATION = '1h';
export const DEFAULT_FUNC = 'raw';
export const DEFAULT_GROUP_BY_FILL = 'last';
export const DEFAULT_FILL_RAW = 'null';
export const DEFAULT_SHOW_IN_LEGEND = true;
export const DEFAULT_SHOW_LEGEND_VALUE = true;
export const DEFAULT_SHOW_IN_HEADER = true;
export const DEFAULT_SHOW_IN_CHART = true;
Expand Down Expand Up @@ -48,3 +49,4 @@ export const DEFAULT_MAX = 100;

export const DEFAULT_UPDATE_DELAY = 1500;
export const DEFAULT_AREA_OPACITY = 0.7;
export const DEFAULT_LEGEND_MARKER_WIDTH = 12;
1 change: 1 addition & 0 deletions src/types-config-ti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export const ActionsConfig = t.iface([], {

export const ChartCardSeriesShowConfigExt = t.iface([], {
"as_duration": t.opt("ChartCardPrettyTime"),
"in_legend": t.opt("boolean"),
"legend_value": t.opt("boolean"),
"in_header": t.opt(t.union("boolean", t.lit('raw'), t.lit('before_now'), t.lit('after_now'))),
"name_in_header": t.opt("boolean"),
Expand Down
1 change: 1 addition & 0 deletions src/types-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export interface ActionsConfig {

export interface ChartCardSeriesShowConfigExt {
as_duration?: ChartCardPrettyTime;
in_legend?: boolean;
legend_value?: boolean;
in_header?: boolean | 'raw' | 'before_now' | 'after_now';
name_in_header?: boolean;
Expand Down

0 comments on commit 12cb893

Please sign in to comment.