@@ -17,16 +17,12 @@ import {
17
17
import Cache from "./cache/Cache" ;
18
18
import getThemedLayout from "./themed-layout" ;
19
19
import isProduction from "./is-production" ;
20
- import { getIsPureObject , sleep } from "./utils" ;
20
+ import { sleep } from "./utils" ;
21
21
import { Datum } from "plotly.js" ;
22
- import colorSchemes , { isColorSchemeArray } from "./color-schemes" ;
23
22
import { parseISO } from "date-fns" ;
24
- import {
25
- STATISTIC_PERIODS ,
26
- STATISTIC_TYPES ,
27
- StatisticPeriod ,
28
- } from "./recorder-types" ;
23
+ import { StatisticPeriod } from "./recorder-types" ;
29
24
import { parseTimeDuration } from "./duration/duration" ;
25
+ import parseConfig from "./parse-config" ;
30
26
31
27
const componentName = isProduction ? "plotly-graph" : "plotly-graph-dev" ;
32
28
@@ -361,152 +357,7 @@ export class PlotlyGraph extends HTMLElement {
361
357
async _setConfig ( config : InputConfig ) {
362
358
config = JSON . parse ( JSON . stringify ( config ) ) ;
363
359
this . config = config ;
364
- const schemeName = config . color_scheme ?? "category10" ;
365
- const colorScheme = isColorSchemeArray ( schemeName )
366
- ? schemeName
367
- : colorSchemes [ schemeName ] ||
368
- colorSchemes [ Object . keys ( colorSchemes ) [ schemeName ] ] ||
369
- null ;
370
- if ( colorScheme === null ) {
371
- throw new Error (
372
- `color_scheme: "${ config . color_scheme
373
- } " is not valid. Valid are an array of colors (see readme) or ${ Object . keys (
374
- colorSchemes
375
- ) } `
376
- ) ;
377
- }
378
- if (
379
- typeof config . refresh_interval !== "number" &&
380
- config . refresh_interval !== undefined &&
381
- config . refresh_interval !== "auto"
382
- ) {
383
- throw new Error (
384
- `refresh_interval: "${ config . refresh_interval } " is not valid. Must be either "auto" or a number (in seconds). `
385
- ) ;
386
- }
387
- const newConfig : Config = {
388
- title : config . title ,
389
- hours_to_show : config . hours_to_show ?? 1 ,
390
- refresh_interval : config . refresh_interval ?? "auto" ,
391
- offset : parseTimeDuration ( config . offset ?? "0s" ) ,
392
- entities : config . entities . map ( ( entityIn , entityIdx ) => {
393
- if ( typeof entityIn === "string" ) entityIn = { entity : entityIn } ;
394
-
395
- // being lazy on types here. The merged object is temporarily not a real Config
396
- const entity : any = merge (
397
- {
398
- hovertemplate : `<b>%{customdata.name}</b><br><i>%{x}</i><br>%{y}%{customdata.unit_of_measurement}<extra></extra>` ,
399
- mode : "lines" ,
400
- show_value : false ,
401
- line : {
402
- width : 1 ,
403
- shape : "hv" ,
404
- color : colorScheme [ entityIdx % colorScheme . length ] ,
405
- } ,
406
- } ,
407
- config . defaults ?. entity ,
408
- entityIn
409
- ) ;
410
- entity . offset = parseTimeDuration ( entity . offset ?? "0s" ) ;
411
- if ( entity . lambda ) {
412
- entity . lambda = window . eval ( entity . lambda ) ;
413
- }
414
- if ( "statistic" in entity || "period" in entity ) {
415
- const validStatistic = STATISTIC_TYPES . includes ( entity . statistic ! ) ;
416
- if ( entity . statistic === undefined ) entity . statistic = "mean" ;
417
- else if ( ! validStatistic )
418
- throw new Error (
419
- `statistic: "${ entity . statistic } " is not valid. Use ${ STATISTIC_TYPES } `
420
- ) ;
421
- // @TODO : cleanup how this is done
422
- if ( entity . period === "auto" ) {
423
- entity . period = {
424
- "0s" : "5minute" ,
425
- "1d" : "hour" ,
426
- "7d" : "day" ,
427
- "28d" : "week" ,
428
- "12M" : "month" ,
429
- } ;
430
- }
431
- if ( getIsPureObject ( entity . period ) ) {
432
- entity . autoPeriod = entity . period ;
433
- entity . period = undefined ;
434
- let lastDuration = - 1 ;
435
- for ( const durationStr in entity . autoPeriod ) {
436
- const period = entity . autoPeriod [ durationStr ] ;
437
- const duration = parseTimeDuration ( durationStr as any ) ; // will throw if not a valud duration
438
- if ( ! STATISTIC_PERIODS . includes ( period as any ) ) {
439
- throw new Error (
440
- `Error parsing automatic period config: "${ period } " not expected. Must be ${ STATISTIC_PERIODS } `
441
- ) ;
442
- }
443
- if ( duration <= lastDuration ) {
444
- throw new Error (
445
- `Error parsing automatic period config: ranges must be sorted in ascending order, "${ durationStr } " not expected`
446
- ) ;
447
- }
448
- lastDuration = duration ;
449
- }
450
- }
451
- const validPeriod = STATISTIC_PERIODS . includes ( entity . period ) ;
452
- if ( entity . period === undefined ) entity . period = "hour" ;
453
- else if ( ! validPeriod )
454
- throw new Error (
455
- `period: "${ entity . period } " is not valid. Use ${ STATISTIC_PERIODS } `
456
- ) ;
457
- }
458
- entity . extend_to_present ??= ! entity . statistic ;
459
- const [ oldAPI_entity , oldAPI_attribute ] = entity . entity . split ( "::" ) ;
460
- if ( oldAPI_attribute ) {
461
- entity . entity = oldAPI_entity ;
462
- entity . attribute = oldAPI_attribute ;
463
- }
464
- return entity as EntityConfig ;
465
- } ) ,
466
- layout : merge (
467
- {
468
- yaxis : merge ( { } , config . defaults ?. yaxes ) ,
469
- yaxis2 : merge ( { } , config . defaults ?. yaxes ) ,
470
- yaxis3 : merge ( { } , config . defaults ?. yaxes ) ,
471
- yaxis4 : merge ( { } , config . defaults ?. yaxes ) ,
472
- yaxis5 : merge ( { } , config . defaults ?. yaxes ) ,
473
- yaxis6 : merge ( { } , config . defaults ?. yaxes ) ,
474
- yaxis7 : merge ( { } , config . defaults ?. yaxes ) ,
475
- yaxis8 : merge ( { } , config . defaults ?. yaxes ) ,
476
- yaxis9 : merge ( { } , config . defaults ?. yaxes ) ,
477
- yaxis10 : merge ( { } , config . defaults ?. yaxes ) ,
478
- yaxis11 : merge ( { } , config . defaults ?. yaxes ) ,
479
- yaxis12 : merge ( { } , config . defaults ?. yaxes ) ,
480
- yaxis13 : merge ( { } , config . defaults ?. yaxes ) ,
481
- yaxis14 : merge ( { } , config . defaults ?. yaxes ) ,
482
- yaxis15 : merge ( { } , config . defaults ?. yaxes ) ,
483
- yaxis16 : merge ( { } , config . defaults ?. yaxes ) ,
484
- yaxis17 : merge ( { } , config . defaults ?. yaxes ) ,
485
- yaxis18 : merge ( { } , config . defaults ?. yaxes ) ,
486
- yaxis19 : merge ( { } , config . defaults ?. yaxes ) ,
487
- yaxis20 : merge ( { } , config . defaults ?. yaxes ) ,
488
- yaxis21 : merge ( { } , config . defaults ?. yaxes ) ,
489
- yaxis22 : merge ( { } , config . defaults ?. yaxes ) ,
490
- yaxis23 : merge ( { } , config . defaults ?. yaxes ) ,
491
- yaxis24 : merge ( { } , config . defaults ?. yaxes ) ,
492
- yaxis25 : merge ( { } , config . defaults ?. yaxes ) ,
493
- yaxis26 : merge ( { } , config . defaults ?. yaxes ) ,
494
- yaxis27 : merge ( { } , config . defaults ?. yaxes ) ,
495
- yaxis28 : merge ( { } , config . defaults ?. yaxes ) ,
496
- yaxis29 : merge ( { } , config . defaults ?. yaxes ) ,
497
- yaxis30 : merge ( { } , config . defaults ?. yaxes ) ,
498
- } ,
499
- config . layout
500
- ) ,
501
- config : {
502
- ...config . config ,
503
- } ,
504
- no_theme : config . no_theme ?? false ,
505
- no_default_layout : config . no_default_layout ?? false ,
506
- significant_changes_only : config . significant_changes_only ?? false ,
507
- minimal_response : config . minimal_response ?? true ,
508
- } ;
509
-
360
+ const newConfig = parseConfig ( config ) ;
510
361
const was = this . parsed_config ;
511
362
this . parsed_config = newConfig ;
512
363
const is = this . parsed_config ;
@@ -649,7 +500,7 @@ export class PlotlyGraph extends HTMLElement {
649
500
ys = [ null ] ;
650
501
}
651
502
const customdatum = { unit_of_measurement : unit , name, attributes } ;
652
- const customdata = xs . map ( ( ) => customdatum ) ;
503
+ const customdata = xs . map ( ( x , i ) => ( { ... customdatum , x , y : ys [ i ] } ) ) ;
653
504
const mergedTrace = merge (
654
505
{
655
506
name,
0 commit comments