@@ -222,7 +222,7 @@ const getSecondaryYAxisValues = (
222
222
} ;
223
223
224
224
export const getSchemaColors = (
225
- colors : PieColors | Color | null | undefined ,
225
+ colors : PieColors | Color | Color [ ] | string | null | undefined ,
226
226
colorMap : React . MutableRefObject < Map < string , string > > ,
227
227
isDarkTheme ?: boolean ,
228
228
) : string [ ] | string | undefined => {
@@ -248,28 +248,52 @@ export const getSchemaColors = (
248
248
return hexColors ;
249
249
} ;
250
250
251
+ export const _extractColor = (
252
+ useFluentVizColorPalette : boolean ,
253
+ colors : PieColors | Color | Color [ ] | string | null | undefined ,
254
+ colorMap : React . MutableRefObject < Map < string , string > > ,
255
+ isDarkTheme ?: boolean ,
256
+ ) : string | string [ ] | undefined => {
257
+ return ! useFluentVizColorPalette && colors ? getSchemaColors ( colors , colorMap , isDarkTheme ) : undefined ;
258
+ } ;
259
+
260
+ export const _resolveColor = (
261
+ extractedColors : string [ ] | string | null | undefined ,
262
+ index : number ,
263
+ legend : string ,
264
+ colorMap : React . MutableRefObject < Map < string , string > > ,
265
+ isDarkTheme ?: boolean ,
266
+ ) : string => {
267
+ let color = '' ;
268
+ if ( extractedColors && isArrayOrTypedArray ( extractedColors ) && extractedColors [ index ] ) {
269
+ color = extractedColors [ index % extractedColors . length ] ;
270
+ } else if ( typeof extractedColors === 'string' ) {
271
+ color = extractedColors ;
272
+ } else {
273
+ color = getColor ( legend , colorMap , isDarkTheme ) ;
274
+ }
275
+ return color ;
276
+ } ;
277
+
251
278
export const transformPlotlyJsonToDonutProps = (
252
279
input : PlotlySchema ,
253
280
colorMap : React . MutableRefObject < Map < string , string > > ,
254
281
useFluentVizColorPalette : boolean ,
255
282
isDarkTheme ?: boolean ,
256
283
) : IDonutChartProps => {
257
284
const firstData = input . data [ 0 ] as PieData ;
258
- let colors : string [ ] | string | null | undefined = undefined ;
259
- if ( ! useFluentVizColorPalette ) {
260
- colors = firstData . marker ?. colors ? getSchemaColors ( firstData ?. marker ?. colors , colorMap , isDarkTheme ) : undefined ;
261
- }
285
+ // extract colors for each series only once
286
+ const colors : string [ ] | string | null | undefined = _extractColor (
287
+ useFluentVizColorPalette ,
288
+ firstData ?. marker ?. colors ,
289
+ colorMap ,
290
+ isDarkTheme ,
291
+ ) ;
262
292
263
293
const mapLegendToDataPoint : Record < string , IChartDataPoint > = { } ;
264
294
firstData . labels ?. forEach ( ( label : string , index : number ) => {
265
- let color : string = '' ;
266
- if ( colors && isStringArray ( colors ) ) {
267
- color = colors [ index % colors . length ] ;
268
- } else if ( typeof colors === 'string' ) {
269
- color = colors ;
270
- } else {
271
- color = getColor ( label , colorMap , isDarkTheme ) ;
272
- }
295
+ // resolve color for each legend from the extracted colors
296
+ const color : string = _resolveColor ( colors , index , label , colorMap , isDarkTheme ) ;
273
297
//ToDo how to handle string data?
274
298
const value = typeof firstData . values ?. [ index ] === 'number' ? ( firstData . values [ index ] as number ) : 1 ;
275
299
@@ -313,6 +337,7 @@ export const transformPlotlyJsonToDonutProps = (
313
337
export const transformPlotlyJsonToVSBCProps = (
314
338
input : PlotlySchema ,
315
339
colorMap : React . MutableRefObject < Map < string , string > > ,
340
+ useFluentVizColorPalette : boolean ,
316
341
isDarkTheme ?: boolean ,
317
342
fallbackVSBC ?: boolean ,
318
343
) : IVerticalStackedBarChartProps => {
@@ -322,22 +347,33 @@ export const transformPlotlyJsonToVSBCProps = (
322
347
const { legends, hideLegend } = getLegendProps ( input . data , input . layout ) ;
323
348
input . data . forEach ( ( series : PlotData , index1 : number ) => {
324
349
const isXYearCategory = isYearArray ( series . x ) ; // Consider year as categorical not numeric continuous axis
350
+ // extract bar colors for each series only once
351
+ const extractedBarColors = _extractColor ( useFluentVizColorPalette , series . marker ?. color , colorMap , isDarkTheme ) as
352
+ | string [ ]
353
+ | string
354
+ | undefined ;
355
+ // extract line colors for each series only once
356
+ const extractedLineColors = _extractColor ( useFluentVizColorPalette , series . line ?. color , colorMap , isDarkTheme ) as
357
+ | string [ ]
358
+ | string
359
+ | undefined ;
325
360
( series . x as Datum [ ] ) ?. forEach ( ( x : string | number , index2 : number ) => {
326
361
if ( ! mapXToDataPoints [ x ] ) {
327
362
mapXToDataPoints [ x ] = { xAxisPoint : isXYearCategory ? x . toString ( ) : x , chartData : [ ] , lineData : [ ] } ;
328
363
}
329
364
const legend : string = legends [ index1 ] ;
365
+ // resolve color for each legend's bars from the extracted colors
366
+ const color = _resolveColor ( extractedBarColors , index1 , legend , colorMap , isDarkTheme ) ;
330
367
const yVal : number = ( series . y ?. [ index2 ] as number ) ?? 0 ;
331
368
if ( series . type === 'bar' ) {
332
- const color = getColor ( legend , colorMap , isDarkTheme ) ;
333
369
mapXToDataPoints [ x ] . chartData . push ( {
334
370
legend,
335
371
data : yVal ,
336
372
color,
337
373
} ) ;
338
374
yMaxValue = Math . max ( yMaxValue , yVal ) ;
339
375
} else if ( series . type === 'scatter' || ! ! fallbackVSBC ) {
340
- const color = getColor ( legend , colorMap , isDarkTheme ) ;
376
+ const lineColor = _resolveColor ( extractedLineColors , index1 , legend , colorMap , isDarkTheme ) ;
341
377
const lineOptions = getLineOptions ( series . line ) ;
342
378
const dashType = series . line ?. dash || 'solid' ;
343
379
const legendShape =
@@ -346,7 +382,7 @@ export const transformPlotlyJsonToVSBCProps = (
346
382
legend,
347
383
legendShape,
348
384
y : yVal ,
349
- color,
385
+ color : lineColor ,
350
386
...( lineOptions ? { lineOptions } : { } ) ,
351
387
useSecondaryYScale : usesSecondaryYScale ( series ) ,
352
388
} ) ;
@@ -378,22 +414,28 @@ export const transformPlotlyJsonToVSBCProps = (
378
414
export const transformPlotlyJsonToGVBCProps = (
379
415
input : PlotlySchema ,
380
416
colorMap : React . MutableRefObject < Map < string , string > > ,
417
+ useFluentVizColorPalette : boolean ,
381
418
isDarkTheme ?: boolean ,
382
419
) : IGroupedVerticalBarChartProps => {
383
420
const mapXToDataPoints : Record < string , IGroupedVerticalBarChartData > = { } ;
384
421
const secondaryYAxisValues = getSecondaryYAxisValues ( input . data , input . layout , 0 , 0 ) ;
385
422
const { legends, hideLegend } = getLegendProps ( input . data , input . layout ) ;
386
423
387
424
input . data . forEach ( ( series : PlotData , index1 : number ) => {
425
+ // extract colors for each series only once
426
+ const extractedColors = _extractColor ( useFluentVizColorPalette , series . marker ?. color , colorMap , isDarkTheme ) as
427
+ | string [ ]
428
+ | string
429
+ | undefined ;
388
430
( series . x as Datum [ ] ) ?. forEach ( ( x : string | number , xIndex : number ) => {
389
431
if ( ! mapXToDataPoints [ x ] ) {
390
432
mapXToDataPoints [ x ] = { name : x . toString ( ) , series : [ ] } ;
391
433
}
392
434
393
435
if ( series . type === 'bar' ) {
394
436
const legend : string = legends [ index1 ] ;
395
- const color = getColor ( legend , colorMap , isDarkTheme ) ;
396
-
437
+ // resolve color for each legend's bars from the extracted colors
438
+ const color = _resolveColor ( extractedColors , index1 , legend , colorMap , isDarkTheme ) ;
397
439
mapXToDataPoints [ x ] . series . push ( {
398
440
key : legend ,
399
441
data : ( series . y ?. [ xIndex ] as number ) ?? 0 ,
@@ -426,6 +468,7 @@ export const transformPlotlyJsonToGVBCProps = (
426
468
export const transformPlotlyJsonToVBCProps = (
427
469
input : PlotlySchema ,
428
470
colorMap : React . MutableRefObject < Map < string , string > > ,
471
+ useFluentVizColorPalette : boolean ,
429
472
isDarkTheme ?: boolean ,
430
473
) : IVerticalBarChartProps => {
431
474
const vbcData : IVerticalBarChartDataPoint [ ] = [ ] ;
@@ -435,7 +478,11 @@ export const transformPlotlyJsonToVBCProps = (
435
478
if ( ! series . x ) {
436
479
return ;
437
480
}
438
-
481
+ // extract colors for each series only once
482
+ const extractedColors = _extractColor ( useFluentVizColorPalette , series . marker ?. color , colorMap , isDarkTheme ) as
483
+ | string [ ]
484
+ | string
485
+ | undefined ;
439
486
const isXString = isStringArray ( series . x ) ;
440
487
// TODO: In case of a single bin, add an empty bin of the same size to prevent the
441
488
// default bar width from being used and ensure the bar spans the full intended range.
@@ -458,7 +505,8 @@ export const transformPlotlyJsonToVBCProps = (
458
505
459
506
xBins . forEach ( ( bin , index ) => {
460
507
const legend : string = legends [ seriesIdx ] ;
461
- const color : string = getColor ( legend , colorMap , isDarkTheme ) ;
508
+ // resolve color for each legend's bars from the extracted colors
509
+ const color = _resolveColor ( extractedColors , seriesIdx , legend , colorMap , isDarkTheme ) ;
462
510
const yVal = calculateHistNorm (
463
511
series . histnorm ,
464
512
y [ index ] ,
@@ -511,15 +559,18 @@ export const transformPlotlyJsonToScatterChartProps = (
511
559
let mode : string = 'tonexty' ;
512
560
const { legends, hideLegend } = getLegendProps ( input . data , input . layout ) ;
513
561
const chartData : ILineChartPoints [ ] = input . data . map ( ( series : PlotData , index : number ) => {
562
+ // extract colors for each series only once
563
+ const extractedColors = _extractColor ( useFluentVizColorPalette , series . line ?. color , colorMap , isDarkTheme ) as
564
+ | string [ ]
565
+ | string
566
+ | undefined ;
514
567
const xValues = series . x as Datum [ ] ;
515
568
const isString = typeof xValues [ 0 ] === 'string' ;
516
569
const isXDate = isDateArray ( xValues ) ;
517
570
const isXNumber = isNumberArray ( xValues ) ;
518
571
const legend : string = legends [ index ] ;
519
- const lineColor =
520
- ! useFluentVizColorPalette && series . line ?. color
521
- ? getSchemaColors ( series . line ?. color , colorMap , isDarkTheme )
522
- : getColor ( legend , colorMap , isDarkTheme ) ;
572
+ // resolve color for each legend's lines from the extracted colors
573
+ const lineColor = _resolveColor ( extractedColors , index , legend , colorMap , isDarkTheme ) ;
523
574
mode = series . fill === 'tozeroy' ? 'tozeroy' : 'tonexty' ;
524
575
const lineOptions = getLineOptions ( series . line ) ;
525
576
const dashType = series . line ?. dash || 'solid' ;
@@ -588,13 +639,20 @@ export const transformPlotlyJsonToScatterChartProps = (
588
639
export const transformPlotlyJsonToHorizontalBarWithAxisProps = (
589
640
input : PlotlySchema ,
590
641
colorMap : React . MutableRefObject < Map < string , string > > ,
642
+ useFluentVizColorPalette : boolean ,
591
643
isDarkTheme ?: boolean ,
592
644
) : IHorizontalBarChartWithAxisProps => {
593
645
const { legends, hideLegend } = getLegendProps ( input . data , input . layout ) ;
594
646
const chartData : IHorizontalBarChartWithAxisDataPoint [ ] = input . data
595
647
. map ( ( series : PlotData , index : number ) => {
648
+ // extract colors for each series only once
649
+ const extractedColors = _extractColor ( useFluentVizColorPalette , series . marker ?. color , colorMap , isDarkTheme ) as
650
+ | string [ ]
651
+ | string
652
+ | undefined ;
596
653
const legend = legends [ index ] ;
597
- const color = getColor ( legend , colorMap , isDarkTheme ) ;
654
+ // resolve color for each legend's bars from the extracted colors
655
+ const color = _resolveColor ( extractedColors , index , legend , colorMap , isDarkTheme ) ;
598
656
return ( series . y as Datum [ ] ) . map ( ( yValue : string , i : number ) => {
599
657
return {
600
658
x : series . x [ i ] ,
@@ -768,7 +826,6 @@ export const transformPlotlyJsonToSankeyProps = (
768
826
source : link ?. source ! [ index ] ,
769
827
target : link ?. target ! [ index ] ,
770
828
} ) )
771
- // eslint-disable-next-line @typescript-eslint/no-shadow
772
829
// Filter out negative nodes, unequal nodes and self-references (circular links)
773
830
. filter ( x => x . source >= 0 && x . target >= 0 && x . source !== x . target ) ;
774
831
0 commit comments