mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Use CSS variables to theme echarts (#23963)
* Use CSS variables to theme echarts * fix styles
This commit is contained in:
parent
028472fc7b
commit
b2b71edd04
@ -1,3 +1,4 @@
|
|||||||
|
import memoizeOne from "memoize-one";
|
||||||
import { theme2hex } from "./convert-color";
|
import { theme2hex } from "./convert-color";
|
||||||
|
|
||||||
export const COLORS = [
|
export const COLORS = [
|
||||||
@ -74,3 +75,12 @@ export function getGraphColorByIndex(
|
|||||||
getColorByIndex(index);
|
getColorByIndex(index);
|
||||||
return theme2hex(themeColor);
|
return theme2hex(themeColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const getAllGraphColors = memoizeOne(
|
||||||
|
(style: CSSStyleDeclaration) =>
|
||||||
|
COLORS.map((_color, index) => getGraphColorByIndex(index, style)),
|
||||||
|
(newArgs: [CSSStyleDeclaration], lastArgs: [CSSStyleDeclaration]) =>
|
||||||
|
// this is not ideal, but we need to memoize the colors
|
||||||
|
newArgs[0].getPropertyValue("--graph-color-1") ===
|
||||||
|
lastArgs[0].getPropertyValue("--graph-color-1")
|
||||||
|
);
|
||||||
|
@ -20,6 +20,7 @@ import type { ECOption } from "../../resources/echarts";
|
|||||||
import { listenMediaQuery } from "../../common/dom/media_query";
|
import { listenMediaQuery } from "../../common/dom/media_query";
|
||||||
import type { Themes } from "../../data/ws-themes";
|
import type { Themes } from "../../data/ws-themes";
|
||||||
import { themesContext } from "../../data/context";
|
import { themesContext } from "../../data/context";
|
||||||
|
import { getAllGraphColors } from "../../common/color/colors";
|
||||||
|
|
||||||
export const MIN_TIME_BETWEEN_UPDATES = 60 * 5 * 1000;
|
export const MIN_TIME_BETWEEN_UPDATES = 60 * 5 * 1000;
|
||||||
|
|
||||||
@ -183,10 +184,9 @@ export class HaChartBase extends LitElement {
|
|||||||
}
|
}
|
||||||
const echarts = (await import("../../resources/echarts")).default;
|
const echarts = (await import("../../resources/echarts")).default;
|
||||||
|
|
||||||
this.chart = echarts.init(
|
echarts.registerTheme("custom", this._createTheme());
|
||||||
container,
|
|
||||||
this._themes.darkMode ? "dark" : "light"
|
this.chart = echarts.init(container, "custom");
|
||||||
);
|
|
||||||
this.chart.on("legendselectchanged", (params: any) => {
|
this.chart.on("legendselectchanged", (params: any) => {
|
||||||
if (this.externalHidden) {
|
if (this.externalHidden) {
|
||||||
const isSelected = params.selected[params.name];
|
const isSelected = params.selected[params.name];
|
||||||
@ -237,24 +237,14 @@ export class HaChartBase extends LitElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _createOptions(): ECOption {
|
private _createOptions(): ECOption {
|
||||||
const darkMode = this._themes.darkMode ?? false;
|
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
backgroundColor: "transparent",
|
|
||||||
animation: !this._reducedMotion,
|
animation: !this._reducedMotion,
|
||||||
darkMode,
|
darkMode: this._themes.darkMode ?? false,
|
||||||
aria: {
|
aria: {
|
||||||
show: true,
|
show: true,
|
||||||
},
|
},
|
||||||
dataZoom: this._getDataZoomConfig(),
|
dataZoom: this._getDataZoomConfig(),
|
||||||
...this.options,
|
...this.options,
|
||||||
legend: this.options?.legend
|
|
||||||
? {
|
|
||||||
// we should create our own theme but this is a quick fix for now
|
|
||||||
inactiveColor: darkMode ? "#444" : "#ccc",
|
|
||||||
...this.options.legend,
|
|
||||||
}
|
|
||||||
: undefined,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const isMobile = window.matchMedia(
|
const isMobile = window.matchMedia(
|
||||||
@ -274,6 +264,181 @@ export class HaChartBase extends LitElement {
|
|||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _createTheme() {
|
||||||
|
const style = getComputedStyle(this);
|
||||||
|
return {
|
||||||
|
color: getAllGraphColors(style),
|
||||||
|
backgroundColor: "transparent",
|
||||||
|
textStyle: {
|
||||||
|
color: style.getPropertyValue("--primary-text-color"),
|
||||||
|
},
|
||||||
|
title: {
|
||||||
|
textStyle: {
|
||||||
|
color: style.getPropertyValue("--primary-text-color"),
|
||||||
|
},
|
||||||
|
subtextStyle: {
|
||||||
|
color: style.getPropertyValue("--secondary-text-color"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
line: {
|
||||||
|
lineStyle: {
|
||||||
|
width: 1.5,
|
||||||
|
},
|
||||||
|
symbolSize: 1,
|
||||||
|
symbol: "circle",
|
||||||
|
smooth: false,
|
||||||
|
},
|
||||||
|
bar: {
|
||||||
|
itemStyle: {
|
||||||
|
barBorderWidth: 1.5,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
categoryAxis: {
|
||||||
|
axisLine: {
|
||||||
|
show: false,
|
||||||
|
},
|
||||||
|
axisTick: {
|
||||||
|
show: false,
|
||||||
|
},
|
||||||
|
axisLabel: {
|
||||||
|
show: true,
|
||||||
|
color: style.getPropertyValue("--primary-text-color"),
|
||||||
|
},
|
||||||
|
splitLine: {
|
||||||
|
show: false,
|
||||||
|
lineStyle: {
|
||||||
|
color: style.getPropertyValue("--divider-color"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
splitArea: {
|
||||||
|
show: false,
|
||||||
|
areaStyle: {
|
||||||
|
color: [
|
||||||
|
style.getPropertyValue("--divider-color") + "3F",
|
||||||
|
style.getPropertyValue("--divider-color") + "7F",
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
valueAxis: {
|
||||||
|
axisLine: {
|
||||||
|
show: true,
|
||||||
|
lineStyle: {
|
||||||
|
color: style.getPropertyValue("--divider-color"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
axisTick: {
|
||||||
|
show: true,
|
||||||
|
lineStyle: {
|
||||||
|
color: style.getPropertyValue("--divider-color"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
axisLabel: {
|
||||||
|
show: true,
|
||||||
|
color: style.getPropertyValue("--primary-text-color"),
|
||||||
|
},
|
||||||
|
splitLine: {
|
||||||
|
show: true,
|
||||||
|
lineStyle: {
|
||||||
|
color: style.getPropertyValue("--divider-color"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
splitArea: {
|
||||||
|
show: false,
|
||||||
|
areaStyle: {
|
||||||
|
color: [
|
||||||
|
style.getPropertyValue("--divider-color") + "3F",
|
||||||
|
style.getPropertyValue("--divider-color") + "7F",
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
logAxis: {
|
||||||
|
axisLine: {
|
||||||
|
show: true,
|
||||||
|
lineStyle: {
|
||||||
|
color: style.getPropertyValue("--divider-color"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
axisTick: {
|
||||||
|
show: true,
|
||||||
|
lineStyle: {
|
||||||
|
color: style.getPropertyValue("--divider-color"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
axisLabel: {
|
||||||
|
show: true,
|
||||||
|
color: style.getPropertyValue("--primary-text-color"),
|
||||||
|
},
|
||||||
|
splitLine: {
|
||||||
|
show: true,
|
||||||
|
lineStyle: {
|
||||||
|
color: style.getPropertyValue("--divider-color"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
splitArea: {
|
||||||
|
show: false,
|
||||||
|
areaStyle: {
|
||||||
|
color: [
|
||||||
|
style.getPropertyValue("--divider-color") + "3F",
|
||||||
|
style.getPropertyValue("--divider-color") + "7F",
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
timeAxis: {
|
||||||
|
axisLine: {
|
||||||
|
show: true,
|
||||||
|
lineStyle: {
|
||||||
|
color: style.getPropertyValue("--divider-color"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
axisTick: {
|
||||||
|
show: true,
|
||||||
|
lineStyle: {
|
||||||
|
color: style.getPropertyValue("--divider-color"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
axisLabel: {
|
||||||
|
show: true,
|
||||||
|
color: style.getPropertyValue("--primary-text-color"),
|
||||||
|
},
|
||||||
|
splitLine: {
|
||||||
|
show: true,
|
||||||
|
lineStyle: {
|
||||||
|
color: style.getPropertyValue("--divider-color"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
splitArea: {
|
||||||
|
show: false,
|
||||||
|
areaStyle: {
|
||||||
|
color: [
|
||||||
|
style.getPropertyValue("--divider-color") + "3F",
|
||||||
|
style.getPropertyValue("--divider-color") + "7F",
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
legend: {
|
||||||
|
textStyle: {
|
||||||
|
color: style.getPropertyValue("--primary-text-color"),
|
||||||
|
},
|
||||||
|
inactiveColor: style.getPropertyValue("--disabled-text-color"),
|
||||||
|
},
|
||||||
|
tooltip: {
|
||||||
|
axisPointer: {
|
||||||
|
lineStyle: {
|
||||||
|
color: style.getPropertyValue("--divider-color"),
|
||||||
|
},
|
||||||
|
crossStyle: {
|
||||||
|
color: style.getPropertyValue("--divider-color"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
timeline: {},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
private _getDefaultHeight() {
|
private _getDefaultHeight() {
|
||||||
return Math.max(this.clientWidth / 2, 400);
|
return Math.max(this.clientWidth / 2, 400);
|
||||||
}
|
}
|
||||||
|
@ -158,9 +158,6 @@ export class StateHistoryChartLine extends LitElement {
|
|||||||
) {
|
) {
|
||||||
const dayDifference = differenceInDays(this.endTime, this.startTime);
|
const dayDifference = differenceInDays(this.endTime, this.startTime);
|
||||||
const rtl = computeRTL(this.hass);
|
const rtl = computeRTL(this.hass);
|
||||||
const splitLineStyle = this.hass.themes?.darkMode
|
|
||||||
? { opacity: 0.15 }
|
|
||||||
: {};
|
|
||||||
this._chartOptions = {
|
this._chartOptions = {
|
||||||
xAxis: {
|
xAxis: {
|
||||||
type: "time",
|
type: "time",
|
||||||
@ -176,7 +173,6 @@ export class StateHistoryChartLine extends LitElement {
|
|||||||
},
|
},
|
||||||
splitLine: {
|
splitLine: {
|
||||||
show: true,
|
show: true,
|
||||||
lineStyle: splitLineStyle,
|
|
||||||
},
|
},
|
||||||
minInterval:
|
minInterval:
|
||||||
dayDifference >= 89 // quarter
|
dayDifference >= 89 // quarter
|
||||||
@ -196,9 +192,8 @@ export class StateHistoryChartLine extends LitElement {
|
|||||||
nameTextStyle: {
|
nameTextStyle: {
|
||||||
align: "left",
|
align: "left",
|
||||||
},
|
},
|
||||||
splitLine: {
|
axisLine: {
|
||||||
show: true,
|
show: false,
|
||||||
lineStyle: splitLineStyle,
|
|
||||||
},
|
},
|
||||||
axisLabel: {
|
axisLabel: {
|
||||||
margin: 5,
|
margin: 5,
|
||||||
|
@ -197,9 +197,12 @@ export class StateHistoryChartTimeline extends LitElement {
|
|||||||
max: this.endTime,
|
max: this.endTime,
|
||||||
axisTick: {
|
axisTick: {
|
||||||
show: true,
|
show: true,
|
||||||
lineStyle: {
|
},
|
||||||
opacity: 0.4,
|
splitLine: {
|
||||||
},
|
show: false,
|
||||||
|
},
|
||||||
|
axisLine: {
|
||||||
|
show: false,
|
||||||
},
|
},
|
||||||
axisLabel: getTimeAxisLabelConfig(
|
axisLabel: getTimeAxisLabelConfig(
|
||||||
this.hass.locale,
|
this.hass.locale,
|
||||||
|
@ -135,7 +135,7 @@ export class StateHistoryCharts extends LitElement {
|
|||||||
return html``;
|
return html``;
|
||||||
}
|
}
|
||||||
if (!Array.isArray(item)) {
|
if (!Array.isArray(item)) {
|
||||||
return html`<div class="entry-container">
|
return html`<div class="entry-container line">
|
||||||
<state-history-chart-line
|
<state-history-chart-line
|
||||||
.hass=${this.hass}
|
.hass=${this.hass}
|
||||||
.unit=${item.unit}
|
.unit=${item.unit}
|
||||||
@ -299,6 +299,9 @@ export class StateHistoryCharts extends LitElement {
|
|||||||
|
|
||||||
.entry-container {
|
.entry-container {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.entry-container.line {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user