mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-23 17:26:42 +00:00
Make graph colors themable (#10698)
This commit is contained in:
parent
cf062bf0f4
commit
0c75d5afc9
@ -61,3 +61,14 @@ export const COLORS = [
|
|||||||
export function getColorByIndex(index: number) {
|
export function getColorByIndex(index: number) {
|
||||||
return COLORS[index % COLORS.length];
|
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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import type { ChartData, ChartDataset, ChartOptions } from "chart.js";
|
import type { ChartData, ChartDataset, ChartOptions } from "chart.js";
|
||||||
import { html, LitElement, PropertyValues } from "lit";
|
import { html, LitElement, PropertyValues } from "lit";
|
||||||
import { property, state } from "lit/decorators";
|
import { property, state } from "lit/decorators";
|
||||||
import { getColorByIndex } from "../../common/color/colors";
|
import { getGraphColorByIndex } from "../../common/color/colors";
|
||||||
import {
|
import {
|
||||||
formatNumber,
|
formatNumber,
|
||||||
numberFormatToLocale,
|
numberFormatToLocale,
|
||||||
@ -164,7 +164,7 @@ class StateHistoryChartLine extends LitElement {
|
|||||||
const pushData = (timestamp: Date, datavalues: any[] | null) => {
|
const pushData = (timestamp: Date, datavalues: any[] | null) => {
|
||||||
if (!datavalues) return;
|
if (!datavalues) return;
|
||||||
if (timestamp > endTime) {
|
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.
|
// endTime is "now" and client time is not in sync with server time.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -190,7 +190,7 @@ class StateHistoryChartLine extends LitElement {
|
|||||||
color?: string
|
color?: string
|
||||||
) => {
|
) => {
|
||||||
if (!color) {
|
if (!color) {
|
||||||
color = getColorByIndex(colorIndex);
|
color = getGraphColorByIndex(colorIndex, computedStyles);
|
||||||
colorIndex++;
|
colorIndex++;
|
||||||
}
|
}
|
||||||
data.push({
|
data.push({
|
||||||
|
@ -2,7 +2,7 @@ import type { ChartData, ChartDataset, ChartOptions } from "chart.js";
|
|||||||
import { HassEntity } from "home-assistant-js-websocket";
|
import { HassEntity } from "home-assistant-js-websocket";
|
||||||
import { css, CSSResultGroup, html, LitElement, PropertyValues } from "lit";
|
import { css, CSSResultGroup, html, LitElement, PropertyValues } from "lit";
|
||||||
import { customElement, property, state } from "lit/decorators";
|
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 { formatDateTimeWithSeconds } from "../../common/datetime/format_date_time";
|
||||||
import { computeDomain } from "../../common/entity/compute_domain";
|
import { computeDomain } from "../../common/entity/compute_domain";
|
||||||
import { numberFormatToLocale } from "../../common/number/format_number";
|
import { numberFormatToLocale } from "../../common/number/format_number";
|
||||||
@ -71,7 +71,7 @@ const getColor = (
|
|||||||
stateColorMap.set(stateString, color);
|
stateColorMap.set(stateString, color);
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
const color = getColorByIndex(colorIndex);
|
const color = getGraphColorByIndex(colorIndex, computedStyles);
|
||||||
colorIndex++;
|
colorIndex++;
|
||||||
stateColorMap.set(stateString, color);
|
stateColorMap.set(stateString, color);
|
||||||
return color;
|
return color;
|
||||||
|
@ -13,7 +13,7 @@ import {
|
|||||||
TemplateResult,
|
TemplateResult,
|
||||||
} from "lit";
|
} from "lit";
|
||||||
import { customElement, property, state } from "lit/decorators";
|
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 { isComponentLoaded } from "../../common/config/is_component_loaded";
|
||||||
import { computeStateName } from "../../common/entity/compute_state_name";
|
import { computeStateName } from "../../common/entity/compute_state_name";
|
||||||
import {
|
import {
|
||||||
@ -59,6 +59,8 @@ class StatisticsChart extends LitElement {
|
|||||||
|
|
||||||
@state() private _chartOptions?: ChartOptions;
|
@state() private _chartOptions?: ChartOptions;
|
||||||
|
|
||||||
|
private _computedStyle?: CSSStyleDeclaration;
|
||||||
|
|
||||||
protected shouldUpdate(changedProps: PropertyValues): boolean {
|
protected shouldUpdate(changedProps: PropertyValues): boolean {
|
||||||
return changedProps.size > 1 || !changedProps.has("hass");
|
return changedProps.size > 1 || !changedProps.has("hass");
|
||||||
}
|
}
|
||||||
@ -72,6 +74,10 @@ class StatisticsChart extends LitElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public firstUpdated() {
|
||||||
|
this._computedStyle = getComputedStyle(this);
|
||||||
|
}
|
||||||
|
|
||||||
protected render(): TemplateResult {
|
protected render(): TemplateResult {
|
||||||
if (!isComponentLoaded(this.hass, "history")) {
|
if (!isComponentLoaded(this.hass, "history")) {
|
||||||
return html`<div class="info">
|
return html`<div class="info">
|
||||||
@ -261,7 +267,7 @@ class StatisticsChart extends LitElement {
|
|||||||
) => {
|
) => {
|
||||||
if (!dataValues) return;
|
if (!dataValues) return;
|
||||||
if (timestamp > endTime) {
|
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.
|
// endTime is "now" and client time is not in sync with server time.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -280,7 +286,7 @@ class StatisticsChart extends LitElement {
|
|||||||
prevValues = dataValues;
|
prevValues = dataValues;
|
||||||
};
|
};
|
||||||
|
|
||||||
const color = getColorByIndex(colorIndex);
|
const color = getGraphColorByIndex(colorIndex, this._computedStyle!);
|
||||||
colorIndex++;
|
colorIndex++;
|
||||||
|
|
||||||
const statTypes: this["statTypes"] = [];
|
const statTypes: this["statTypes"] = [];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user