Skip to content

Commit e09e647

Browse files
author
David Buezas
committed
fixed multiple axes
1 parent e96ce70 commit e09e647

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

src/plotly-graph-card.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,19 @@ export class PlotlyGraph extends HTMLElement {
137137
const units = Array.from(
138138
new Set(Object.values(attributes).map((a) => a.unit_of_measurement))
139139
);
140+
140141
this.data = entities.map((trace) => {
141142
const entity_id = trace.entity;
142143
const history = histories[entity_id] || {};
143144
const attribute = attributes[entity_id] || {};
144-
const yaxis_idx = units.indexOf(attribute.unit_of_measurement) + 1;
145+
const yaxis_idx = units.indexOf(attribute.unit_of_measurement);
145146
return {
146147
name: trace.name || attribute.friendly_name || entity_id,
147148
hovertemplate: `%{y} ${attribute.unit_of_measurement || ""}`,
148149
...trace,
149150
x: history.map(({ last_changed }) => new Date(last_changed)),
150151
y: history.map(({ state }) => state),
151-
yaxis: "y" + yaxis_idx,
152+
yaxis: "y" + (yaxis_idx == 0 ? "" : yaxis_idx + 1),
152153
};
153154
});
154155

@@ -182,6 +183,7 @@ export class PlotlyGraph extends HTMLElement {
182183
const units = Array.from(
183184
new Set(Object.values(attributes).map((a) => a.unit_of_measurement))
184185
);
186+
185187
const yAxisTitles = Object.fromEntries(
186188
units.map((unit, i) => ["yaxis" + (i == 0 ? "" : i + 1), { title: unit }])
187189
);
@@ -195,6 +197,7 @@ export class PlotlyGraph extends HTMLElement {
195197
modeBarButtonsToRemove: ["resetScale2d", "toImage"],
196198
...config.config,
197199
};
200+
console.log(this.data, layout);
198201
this.isInternalRelayout = true;
199202
await Plotly.react(contentEl, data, layout, plotlyConfig);
200203
this.isInternalRelayout = false;

src/themed-layout.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,30 @@ import { HATheme } from "./types";
44
const defaultLayout: Partial<Plotly.Layout> = {
55
height: 260,
66
xaxis: {
7-
zeroline: false,
8-
showline: true,
97
automargin: true,
108
},
119
yaxis: {
12-
zeroline: false,
13-
showline: true,
14-
// fixedrange: true,
10+
automargin: true,
1511
},
1612
yaxis2: {
1713
side: "right",
1814
overlaying: "y",
15+
showgrid: false,
16+
},
17+
yaxis3: {
18+
side: "right",
19+
overlaying: "y",
20+
visible: false,
21+
},
22+
yaxis4: {
23+
side: "right",
24+
overlaying: "y",
25+
visible: false,
26+
},
27+
yaxis5: {
28+
side: "right",
29+
overlaying: "y",
30+
visible: false,
1931
},
2032
margin: {
2133
b: 0, // 50,
@@ -54,10 +66,6 @@ export default function getThemedLayout(
5466
paper_bgcolor: haTheme["--card-background-color"],
5567
plot_bgcolor: haTheme["--card-background-color"],
5668
xaxis: { ...axisStyle },
57-
xaxis2: { ...axisStyle },
58-
xaxis3: { ...axisStyle },
59-
xaxis4: { ...axisStyle },
60-
xaxis5: { ...axisStyle },
6169

6270
yaxis: { ...axisStyle },
6371
yaxis2: { ...axisStyle },

0 commit comments

Comments
 (0)