diff --git a/src/common/color/colors.ts b/src/common/color/colors.ts index fd9de4d3df..3f276c8ac9 100644 --- a/src/common/color/colors.ts +++ b/src/common/color/colors.ts @@ -61,3 +61,14 @@ export const COLORS = [ export function getColorByIndex(index: number) { return COLORS[index % COLORS.length]; } + +export function getGraphColorByIndex( + index: number, + style: CSSStyleDeclaration +) { + // The CSS vars for the colors use range 1..n, so we need to adjust the index from the internal 0..n color index range. + return ( + style.getPropertyValue(`--graph-color-${index + 1}`) || + getColorByIndex(index) + ); +} diff --git a/src/components/chart/state-history-chart-line.ts b/src/components/chart/state-history-chart-line.ts index e19b6586a5..100f9dd7af 100644 --- a/src/components/chart/state-history-chart-line.ts +++ b/src/components/chart/state-history-chart-line.ts @@ -1,7 +1,7 @@ import type { ChartData, ChartDataset, ChartOptions } from "chart.js"; import { html, LitElement, PropertyValues } from "lit"; import { property, state } from "lit/decorators"; -import { getColorByIndex } from "../../common/color/colors"; +import { getGraphColorByIndex } from "../../common/color/colors"; import { formatNumber, numberFormatToLocale, @@ -164,7 +164,7 @@ class StateHistoryChartLine extends LitElement { const pushData = (timestamp: Date, datavalues: any[] | null) => { if (!datavalues) return; if (timestamp > endTime) { - // Drop datapoints that are after the requested endTime. This could happen if + // Drop data points that are after the requested endTime. This could happen if // endTime is "now" and client time is not in sync with server time. return; } @@ -190,7 +190,7 @@ class StateHistoryChartLine extends LitElement { color?: string ) => { if (!color) { - color = getColorByIndex(colorIndex); + color = getGraphColorByIndex(colorIndex, computedStyles); colorIndex++; } data.push({ diff --git a/src/components/chart/state-history-chart-timeline.ts b/src/components/chart/state-history-chart-timeline.ts index 802baf85f4..413ee0a831 100644 --- a/src/components/chart/state-history-chart-timeline.ts +++ b/src/components/chart/state-history-chart-timeline.ts @@ -2,7 +2,7 @@ import type { ChartData, ChartDataset, ChartOptions } from "chart.js"; import { HassEntity } from "home-assistant-js-websocket"; import { css, CSSResultGroup, html, LitElement, PropertyValues } from "lit"; import { customElement, property, state } from "lit/decorators"; -import { getColorByIndex } from "../../common/color/colors"; +import { getGraphColorByIndex } from "../../common/color/colors"; import { formatDateTimeWithSeconds } from "../../common/datetime/format_date_time"; import { computeDomain } from "../../common/entity/compute_domain"; import { numberFormatToLocale } from "../../common/number/format_number"; @@ -71,7 +71,7 @@ const getColor = ( stateColorMap.set(stateString, color); return color; } - const color = getColorByIndex(colorIndex); + const color = getGraphColorByIndex(colorIndex, computedStyles); colorIndex++; stateColorMap.set(stateString, color); return color; diff --git a/src/components/chart/statistics-chart.ts b/src/components/chart/statistics-chart.ts index 1c2615ea07..944b467a41 100644 --- a/src/components/chart/statistics-chart.ts +++ b/src/components/chart/statistics-chart.ts @@ -13,7 +13,7 @@ import { TemplateResult, } from "lit"; import { customElement, property, state } from "lit/decorators"; -import { getColorByIndex } from "../../common/color/colors"; +import { getGraphColorByIndex } from "../../common/color/colors"; import { isComponentLoaded } from "../../common/config/is_component_loaded"; import { computeStateName } from "../../common/entity/compute_state_name"; import { @@ -59,6 +59,8 @@ class StatisticsChart extends LitElement { @state() private _chartOptions?: ChartOptions; + private _computedStyle?: CSSStyleDeclaration; + protected shouldUpdate(changedProps: PropertyValues): boolean { return changedProps.size > 1 || !changedProps.has("hass"); } @@ -72,6 +74,10 @@ class StatisticsChart extends LitElement { } } + public firstUpdated() { + this._computedStyle = getComputedStyle(this); + } + protected render(): TemplateResult { if (!isComponentLoaded(this.hass, "history")) { return html`
@@ -261,7 +267,7 @@ class StatisticsChart extends LitElement { ) => { if (!dataValues) return; if (timestamp > endTime) { - // Drop datapoints that are after the requested endTime. This could happen if + // Drop data points that are after the requested endTime. This could happen if // endTime is "now" and client time is not in sync with server time. return; } @@ -280,7 +286,7 @@ class StatisticsChart extends LitElement { prevValues = dataValues; }; - const color = getColorByIndex(colorIndex); + const color = getGraphColorByIndex(colorIndex, this._computedStyle!); colorIndex++; const statTypes: this["statTypes"] = [];