Skip to content

Commit e96ce70

Browse files
author
David Buezas
committed
better styling and axes units
1 parent afdf642 commit e96ce70

File tree

4 files changed

+69
-56
lines changed

4 files changed

+69
-56
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"dependencies": {
2424
"@types/lodash": "^4.14.175",
2525
"custom-card-helpers": "^1.8.0",
26+
"deepmerge": "^4.2.2",
2627
"lodash": "^4.17.21",
2728
"lodash-es": "^4.17.21",
2829
"plotly.js": "^2.5.1"

src/plotly-graph-card.ts

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import merge from "lodash-es/merge";
99
import getThemedLayout from "./themed-layout";
1010
import EventEmitter from "events";
1111
import mapValues from "lodash/mapValues";
12+
import uniq from "lodash/mapValues";
1213

1314
console.info(
1415
`%c PLOTLY-GRAPH-CARD %c ${version} `,
@@ -41,24 +42,24 @@ export class PlotlyGraph extends HTMLElement {
4142
connectedCallback() {
4243
if (!this.contentEl) {
4344
this.innerHTML = `
44-
<style>
45-
ha-card{
46-
padding: 5px;
47-
}
48-
button#reset.hidden{
49-
display: none;
50-
}
51-
button#reset {
52-
position: absolute;
53-
top: 15px;
54-
left: 15px;
55-
display: block;
56-
}
57-
</style>
5845
<ha-card>
46+
<style>
47+
ha-card{
48+
padding: 15px;
49+
}
50+
button#reset.hidden{
51+
display: none;
52+
}
53+
button#reset {
54+
position: absolute;
55+
top: 15px;
56+
left: 15px;
57+
display: block;
58+
}
59+
</style>
5960
<div id="plotly"> </div>
60-
</ha-card>
61-
<button id="reset" class="hidden">reset</button>`;
61+
<button id="reset" class="hidden">reset</button>
62+
</ha-card>`;
6263

6364
this.contentEl = this.querySelector("div#plotly")!;
6465
this.buttonEl = this.querySelector("button#reset")!;
@@ -90,10 +91,6 @@ export class PlotlyGraph extends HTMLElement {
9091
enterBrowsingMode() {
9192
this.isBrowsing = true;
9293
this.buttonEl.classList.remove("hidden");
93-
// Plotly.relayout(this.contentEl, {
94-
// "xaxis.autorange": false,
95-
// "yaxis.autorange": false,
96-
// });
9794
}
9895
exitBrowsingMode = async () => {
9996
this.isBrowsing = false;
@@ -137,16 +134,21 @@ export class PlotlyGraph extends HTMLElement {
137134
const entities = this.config.entities;
138135
const { histories, attributes } = this.cache;
139136

137+
const units = Array.from(
138+
new Set(Object.values(attributes).map((a) => a.unit_of_measurement))
139+
);
140140
this.data = entities.map((trace) => {
141141
const entity_id = trace.entity;
142142
const history = histories[entity_id] || {};
143143
const attribute = attributes[entity_id] || {};
144+
const yaxis_idx = units.indexOf(attribute.unit_of_measurement) + 1;
144145
return {
145146
name: trace.name || attribute.friendly_name || entity_id,
146147
hovertemplate: `%{y} ${attribute.unit_of_measurement || ""}`,
147148
...trace,
148149
x: history.map(({ last_changed }) => new Date(last_changed)),
149150
y: history.map(({ state }) => state),
151+
yaxis: "y" + yaxis_idx,
150152
};
151153
});
152154

@@ -177,20 +179,17 @@ export class PlotlyGraph extends HTMLElement {
177179
);
178180
}
179181
const { attributes } = this.cache;
180-
const unit = Object.values(attributes).map(
181-
({ unit_of_measurement }) => unit_of_measurement
182-
)[0];
182+
const units = Array.from(
183+
new Set(Object.values(attributes).map((a) => a.unit_of_measurement))
184+
);
185+
const yAxisTitles = Object.fromEntries(
186+
units.map((unit, i) => ["yaxis" + (i == 0 ? "" : i + 1), { title: unit }])
187+
);
183188
const { layout, config, width, contentEl, data } = this;
184189

185-
merge(
186-
layout,
187-
{ yaxis: { title: unit } },
188-
this.getThemedLayout(),
189-
config.layout,
190-
{
191-
width,
192-
}
193-
);
190+
merge(layout, yAxisTitles, this.getThemedLayout(), config.layout, {
191+
width,
192+
});
194193
const plotlyConfig: Partial<Plotly.Config> = {
195194
displaylogo: false,
196195
modeBarButtonsToRemove: ["resetScale2d", "toImage"],

src/themed-layout.ts

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,41 @@
11
import merge from "lodash/merge";
22
import { HATheme } from "./types";
3+
34
const defaultLayout: Partial<Plotly.Layout> = {
4-
height: 250,
5-
yaxis: {
5+
height: 260,
6+
xaxis: {
67
zeroline: false,
78
showline: true,
8-
// fixedrange: true,
9+
automargin: true,
910
},
10-
xaxis: {
11+
yaxis: {
1112
zeroline: false,
1213
showline: true,
14+
// fixedrange: true,
15+
},
16+
yaxis2: {
17+
side: "right",
18+
overlaying: "y",
1319
},
1420
margin: {
15-
b: 50,
16-
t: 10,
17-
l: 60,
18-
r: 10,
21+
b: 0, // 50,
22+
t: 0, // 10,
23+
l: 0, // 60,
24+
r: 0, // 10,
1925
},
2026
legend: {
2127
orientation: "h",
28+
// xanchor: "left",
29+
bgcolor: "transparent",
2230
x: 0,
23-
y: 1.3,
31+
y: 1.2,
32+
},
33+
...{
34+
// modebar is missing from the Layout Typings
35+
// vertical so it doesn't occlude the legend
36+
modebar: {
37+
orientation: "v",
38+
},
2439
},
2540
};
2641

@@ -32,22 +47,23 @@ export default function getThemedLayout(
3247
gridcolor: "rgba(127,127,127,.3)",
3348
linecolor: "rgba(127,127,127,.3)",
3449
zerolinecolor: "rgba(127,127,127,.3)",
50+
automargin: true,
3551
};
3652
return merge(
3753
{
3854
paper_bgcolor: haTheme["--card-background-color"],
3955
plot_bgcolor: haTheme["--card-background-color"],
40-
xaxis: axisStyle,
41-
xaxis2: axisStyle,
42-
xaxis3: axisStyle,
43-
xaxis4: axisStyle,
44-
xaxis5: axisStyle,
56+
xaxis: { ...axisStyle },
57+
xaxis2: { ...axisStyle },
58+
xaxis3: { ...axisStyle },
59+
xaxis4: { ...axisStyle },
60+
xaxis5: { ...axisStyle },
4561

46-
yaxis: axisStyle,
47-
yaxis2: axisStyle,
48-
yaxis3: axisStyle,
49-
yaxis4: axisStyle,
50-
yaxis5: axisStyle,
62+
yaxis: { ...axisStyle },
63+
yaxis2: { ...axisStyle },
64+
yaxis3: { ...axisStyle },
65+
yaxis4: { ...axisStyle },
66+
yaxis5: { ...axisStyle },
5167
font: {
5268
color: haTheme["--secondary-text-color"],
5369
size: 11,

0 commit comments

Comments
 (0)