@@ -57,7 +57,7 @@ class MyTooltipLegendLabel extends React.PureComponent<ChartLegendLabelProps> {
57
57
return (
58
58
< ChartLegendTooltipLabel
59
59
{ ...this . props }
60
- dx = { ( ( dx || 0 ) * BaseChart . legendLabelStyle . fontSize ) / 14 - 6 }
60
+ dx = { ( ( dx || 0 ) * BaseChart . legendLabelStyle . fontSize ) / 14 }
61
61
style = { BaseChart . legendLabelStyle }
62
62
legendLabelComponent = { < MyTooltipLabel /> }
63
63
/>
@@ -243,6 +243,7 @@ export default class BaseChart extends React.PureComponent<Props> {
243
243
)
244
244
}
245
245
246
+ /** @return UI for one y axis */
246
247
private yAxis ( axis : BaseChartProps [ "yAxes" ] [ number ] ) {
247
248
if ( axis ) {
248
249
// re: the + BaseChart.fontSize * N: shift the axis labels over a bit, to overlap with the ticks
@@ -282,6 +283,43 @@ export default class BaseChart extends React.PureComponent<Props> {
282
283
}
283
284
}
284
285
286
+ /** @return UI for all of the y axes */
287
+ private yAxes ( chart : BaseChartProps ) {
288
+ return chart . series . flatMap ( ( { impl, stroke, fill = stroke , data } , idx ) => {
289
+ const yAxis =
290
+ chart . yAxes [ idx ] ||
291
+ chart . yAxes
292
+ . slice ( 0 , idx )
293
+ . reverse ( )
294
+ . find ( ( _ ) => _ && _ . y )
295
+ const y = yAxis ? yAxis . y : undefined
296
+
297
+ const props = {
298
+ style :
299
+ impl === "ChartArea"
300
+ ? this . areaStyle ( stroke , fill )
301
+ : impl === "ChartLine"
302
+ ? this . lineStyle ( fill )
303
+ : this . lineDashStyle ( fill ) ,
304
+ data,
305
+ y,
306
+ }
307
+
308
+ const chartui =
309
+ impl === "ChartArea" ? (
310
+ < ChartArea key = { idx } interpolation = "monotoneX" name = { data [ idx ] . name } { ...props } />
311
+ ) : (
312
+ < ChartLine key = { idx } interpolation = "monotoneX" name = { data [ idx ] . name } { ...props } />
313
+ )
314
+
315
+ if ( chart . yAxes [ idx ] ) {
316
+ return [ ...this . yAxis ( chart . yAxes [ idx ] ) , chartui ]
317
+ } else {
318
+ return [ chartui ]
319
+ }
320
+ } )
321
+ }
322
+
285
323
private areaStyle ( stroke : string , fill : string , strokeWidth = 2.5 , fillOpacity = 0.1 ) : ChartAreaProps [ "style" ] {
286
324
return { data : { stroke, strokeWidth, fill, fillOpacity } }
287
325
}
@@ -305,11 +343,15 @@ export default class BaseChart extends React.PureComponent<Props> {
305
343
)
306
344
}
307
345
308
- private getTooltipLabels ( { datum } : { datum : { name : string ; y : number } } ) {
346
+ /** @return the formatted data value to display in the `<ChartLegendTooltip />` */
347
+ private getTooltipLabels ( formatMap : Record < string , Format > , { datum } : { datum : { name : string ; y : number } } ) {
309
348
// TODO: format these e.g. x% or xC or x Gi
310
- return `${ datum . y } `
349
+ const format = formatMap [ datum . name ]
350
+ const formatter = format ? BaseChart . formatters [ format ] : undefined
351
+ return formatter ? formatter ( datum . y ) : `${ datum . y } `
311
352
}
312
353
354
+ /** @return the `legendData` model for `<ChartLegendTooltip/>` */
313
355
private getLegendData ( chart : BaseChartProps ) : ChartLegendTooltipProps [ "legendData" ] {
314
356
return chart . series . map ( ( { data, stroke, fill } ) => ( {
315
357
childName : data [ 0 ] . name ,
@@ -318,9 +360,25 @@ export default class BaseChart extends React.PureComponent<Props> {
318
360
} ) )
319
361
}
320
362
363
+ /** @return the UI for the idx-th chart in the chart set of this.props.charts */
321
364
private chart ( chart : BaseChartProps , idx : number ) {
322
- // ariaTitle={chart.title}
323
365
const CursorVoronoiContainer = createContainer ( "voronoi" , "cursor" )
366
+
367
+ // map from data series name to format (used in getTooltipLabels())
368
+ const formatMap = chart . yAxes . reduce ( ( M , yAxis , idx , yAxes ) => {
369
+ if ( ! yAxis ) {
370
+ yAxis = yAxes
371
+ . slice ( 0 , idx )
372
+ . reverse ( )
373
+ . find ( ( _ ) => _ )
374
+ }
375
+ if ( yAxis ) {
376
+ const label = chart . series [ idx ] . data [ 0 ] . name || yAxis . label
377
+ M [ label ] = yAxis . format
378
+ }
379
+ return M
380
+ } , { } as Record < string , Format > )
381
+
324
382
return (
325
383
< div className = "codeflare-chart-container" key = { idx } >
326
384
< Chart
@@ -331,10 +389,11 @@ export default class BaseChart extends React.PureComponent<Props> {
331
389
domain = { chart . domain }
332
390
containerComponent = {
333
391
< CursorVoronoiContainer
334
- labels = { this . getTooltipLabels }
392
+ labels = { this . getTooltipLabels . bind ( this , formatMap ) }
335
393
labelComponent = {
336
394
< ChartLegendTooltip
337
395
isCursorTooltip
396
+ flyoutStyle = { { fillOpacity : 0.825 } }
338
397
labelComponent = { < MyTooltipContent /> }
339
398
legendData = { this . getLegendData ( chart ) }
340
399
title = { ( datum : any ) => `${ new Date ( datum . x + this . props . timeRange . min ) . toLocaleString ( ) } ` }
@@ -347,39 +406,7 @@ export default class BaseChart extends React.PureComponent<Props> {
347
406
>
348
407
{ this . title ( chart ) }
349
408
{ this . xAxis ( ) }
350
- { chart . series . flatMap ( ( { impl, stroke, fill = stroke , data } , idx ) => {
351
- const yAxis =
352
- chart . yAxes [ idx ] ||
353
- chart . yAxes
354
- . slice ( 0 , idx )
355
- . reverse ( )
356
- . find ( ( _ ) => _ && _ . y )
357
- const y = yAxis ? yAxis . y : undefined
358
-
359
- const props = {
360
- style :
361
- impl === "ChartArea"
362
- ? this . areaStyle ( stroke , fill )
363
- : impl === "ChartLine"
364
- ? this . lineStyle ( fill )
365
- : this . lineDashStyle ( fill ) ,
366
- data,
367
- y,
368
- }
369
-
370
- const chartui =
371
- impl === "ChartArea" ? (
372
- < ChartArea key = { idx } interpolation = "monotoneX" name = { data [ idx ] . name } { ...props } />
373
- ) : (
374
- < ChartLine key = { idx } interpolation = "monotoneX" name = { data [ idx ] . name } { ...props } />
375
- )
376
-
377
- if ( chart . yAxes [ idx ] ) {
378
- return [ ...this . yAxis ( chart . yAxes [ idx ] ) , chartui ]
379
- } else {
380
- return [ chartui ]
381
- }
382
- } ) }
409
+ { this . yAxes ( chart ) }
383
410
</ Chart >
384
411
</ div >
385
412
)
0 commit comments