@@ -9,6 +9,7 @@ import merge from "lodash-es/merge";
99import getThemedLayout from "./themed-layout" ;
1010import EventEmitter from "events" ;
1111import mapValues from "lodash/mapValues" ;
12+ import uniq from "lodash/mapValues" ;
1213
1314console . 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" ] ,
0 commit comments