Skip to content

Commit afdf642

Browse files
author
David Buezas
committed
follows home assistant theme
1 parent 6aebaa7 commit afdf642

File tree

8 files changed

+120
-75
lines changed

8 files changed

+120
-75
lines changed

package-lock.json

Lines changed: 14 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
"ts-jest": "^27.0.6"
2222
},
2323
"dependencies": {
24+
"@types/lodash": "^4.14.175",
2425
"custom-card-helpers": "^1.8.0",
26+
"lodash": "^4.17.21",
2527
"lodash-es": "^4.17.21",
2628
"plotly.js": "^2.5.1"
2729
}

src/Cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { TimestampRange, History } from "./types";
44

55
type Histories = Record<string, History>;
66

7-
function mapValues<T, S>(
7+
export function mapValues<T, S>(
88
o: Record<string, T>,
99
fn: (value: T, key: string) => S
1010
) {

src/plotly-graph-card.ts

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import { version } from "../package.json";
22
import { HomeAssistant } from "custom-card-helpers";
3-
import insertStyleHack from "./styleHack";
3+
import insertStyleHack from "./style-hack";
44
import Plotly from "./plotly";
55
import { Config } from "./types";
66
import { TimestampRange } from "./types";
77
import Cache from "./Cache";
88
import merge from "lodash-es/merge";
9-
import * as themes from "./themes";
9+
import getThemedLayout from "./themed-layout";
1010
import EventEmitter from "events";
11-
12-
import autorange from "./autorange";
11+
import mapValues from "lodash/mapValues";
1312

1413
console.info(
1514
`%c PLOTLY-GRAPH-CARD %c ${version} `,
@@ -28,6 +27,7 @@ export class PlotlyGraph extends HTMLElement {
2827
hass!: HomeAssistant; // set externally
2928
isBrowsing = false;
3029
isInternalRelayout = false;
30+
3131
handles: {
3232
resizeObserver?: ResizeObserver;
3333
relayoutListener?: EventEmitter;
@@ -42,18 +42,25 @@ export class PlotlyGraph extends HTMLElement {
4242
if (!this.contentEl) {
4343
this.innerHTML = `
4444
<style>
45+
ha-card{
46+
padding: 5px;
47+
}
4548
button#reset.hidden{
4649
display: none;
4750
}
4851
button#reset {
4952
position: absolute;
50-
top: 10px;
53+
top: 15px;
54+
left: 15px;
5155
display: block;
5256
}
5357
</style>
54-
<div> </div>
58+
<ha-card>
59+
<div id="plotly"> </div>
60+
</ha-card>
5561
<button id="reset" class="hidden">reset</button>`;
56-
this.contentEl = this.querySelector("div")!;
62+
63+
this.contentEl = this.querySelector("div#plotly")!;
5764
this.buttonEl = this.querySelector("button#reset")!;
5865
this.buttonEl.addEventListener("click", this.exitBrowsingMode);
5966
insertStyleHack(this.querySelector("style")!);
@@ -145,6 +152,18 @@ export class PlotlyGraph extends HTMLElement {
145152

146153
await this.plot();
147154
};
155+
getThemedLayout() {
156+
const styles = window.getComputedStyle(this.contentEl);
157+
let haTheme = {
158+
"--card-background-color": "red",
159+
"--primary-background-color": "red",
160+
"--primary-color": "red",
161+
"--primary-text-color": "red",
162+
"--secondary-text-color": "red",
163+
};
164+
haTheme = mapValues(haTheme, (_, key) => styles.getPropertyValue(key));
165+
return getThemedLayout(haTheme);
166+
}
148167
async plot() {
149168
if (!this.config) return;
150169
if (!this.hass) return;
@@ -162,10 +181,16 @@ export class PlotlyGraph extends HTMLElement {
162181
({ unit_of_measurement }) => unit_of_measurement
163182
)[0];
164183
const { layout, config, width, contentEl, data } = this;
165-
const themeLayout = themes[config.theme!] || themes.dark;
166-
merge(layout, { yaxis: { title: unit } }, themeLayout, config.layout, {
167-
width,
168-
});
184+
185+
merge(
186+
layout,
187+
{ yaxis: { title: unit } },
188+
this.getThemedLayout(),
189+
config.layout,
190+
{
191+
width,
192+
}
193+
);
169194
const plotlyConfig: Partial<Plotly.Config> = {
170195
displaylogo: false,
171196
modeBarButtonsToRemove: ["resetScale2d", "toImage"],
@@ -178,7 +203,7 @@ export class PlotlyGraph extends HTMLElement {
178203
// The height of your card. Home Assistant uses this to automatically
179204
// distribute all cards over the available columns.
180205
getCardSize() {
181-
return 3;
206+
return 30;
182207
}
183208
}
184209

File renamed without changes.

src/themed-layout.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import merge from "lodash/merge";
2+
import { HATheme } from "./types";
3+
const defaultLayout: Partial<Plotly.Layout> = {
4+
height: 250,
5+
yaxis: {
6+
zeroline: false,
7+
showline: true,
8+
// fixedrange: true,
9+
},
10+
xaxis: {
11+
zeroline: false,
12+
showline: true,
13+
},
14+
margin: {
15+
b: 50,
16+
t: 10,
17+
l: 60,
18+
r: 10,
19+
},
20+
legend: {
21+
orientation: "h",
22+
x: 0,
23+
y: 1.3,
24+
},
25+
};
26+
27+
export default function getThemedLayout(
28+
haTheme: HATheme
29+
): Partial<Plotly.Layout> {
30+
const axisStyle = {
31+
tickcolor: "rgba(127,127,127,.3)",
32+
gridcolor: "rgba(127,127,127,.3)",
33+
linecolor: "rgba(127,127,127,.3)",
34+
zerolinecolor: "rgba(127,127,127,.3)",
35+
};
36+
return merge(
37+
{
38+
paper_bgcolor: haTheme["--card-background-color"],
39+
plot_bgcolor: haTheme["--card-background-color"],
40+
xaxis: axisStyle,
41+
xaxis2: axisStyle,
42+
xaxis3: axisStyle,
43+
xaxis4: axisStyle,
44+
xaxis5: axisStyle,
45+
46+
yaxis: axisStyle,
47+
yaxis2: axisStyle,
48+
yaxis3: axisStyle,
49+
yaxis4: axisStyle,
50+
yaxis5: axisStyle,
51+
font: {
52+
color: haTheme["--secondary-text-color"],
53+
size: 11,
54+
},
55+
},
56+
defaultLayout
57+
);
58+
}

src/themes.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export type Config = {
22
type: "custom:plotly-graph-card";
33
hours_to_show?: number;
4-
theme?: string;
54
refresh_interval?: number; // in seconds
65
entities: (Partial<Plotly.PlotData> & {
76
entity: string;
@@ -20,3 +19,11 @@ export type History = {
2019
};
2120
}[];
2221
export type TimestampRange = Timestamp[]; // [Timestamp, Timestamp];
22+
23+
export type HATheme = {
24+
"--card-background-color": string;
25+
"--primary-background-color": string;
26+
"--primary-color": string;
27+
"--primary-text-color": string;
28+
"--secondary-text-color": string;
29+
};

0 commit comments

Comments
 (0)