@@ -18,7 +18,6 @@ import React from "react"
1818
1919import {
2020 Chart ,
21- ChartProps ,
2221 ChartAxis ,
2322 ChartArea ,
2423 ChartAreaProps ,
@@ -86,12 +85,20 @@ export type Series = {
8685 data : { name ?: string ; x : number ; y : number } [ ]
8786}
8887
89- export type BaseChartProps = Pick < ChartProps , "domain" | "padding" > & {
88+ export type BaseChartProps = {
89+ /** Unique identifier for this chart */
90+ key : string
91+
92+ /** Displayed name/title for this chart */
9093 title : string
94+
95+ /** Aria description for this chart */
9196 desc : string
97+
98+ /** The time series for this chart */
9299 series : Series [ ]
93100
94- // y axis...
101+ /** The y axes for this chart */
95102 yAxes : (
96103 | undefined
97104 | ( Pick < ChartAreaProps , "y" > &
@@ -104,10 +111,8 @@ export type BaseChartProps = Pick<ChartProps, "domain" | "padding"> & {
104111}
105112
106113export type TimeRange = {
107- timeRange : {
108- min : number
109- max : number
110- }
114+ minTime : number
115+ maxTime : number
111116}
112117
113118type Props = TimeRange & {
@@ -197,6 +202,8 @@ export default class BaseChart extends React.PureComponent<Props> {
197202 return { fontSize, fontStyle, fontWeight, fill }
198203 }
199204
205+ private readonly flyoutStyle = { fillOpacity : 0.825 , fill : "var(--color-base06)" }
206+
200207 private axisStyleWithGrid : ChartAxisProps [ "style" ] = Object . assign ( { } , BaseChart . axisStyle , {
201208 grid : { strokeWidth : 1 , stroke : "var(--color-text-02)" , strokeOpacity : 0.15 } ,
202209 } )
@@ -233,7 +240,7 @@ export default class BaseChart extends React.PureComponent<Props> {
233240 private xAxis ( ) {
234241 // timestamps in the Series (i.e. datum.x values) are assumed to
235242 // be relativized to the given minTimestamp
236- const range = this . props . timeRange . max - this . props . timeRange . min
243+ const range = this . props . maxTime - this . props . minTime
237244
238245 return (
239246 < ChartAxis
@@ -285,8 +292,18 @@ export default class BaseChart extends React.PureComponent<Props> {
285292 }
286293 }
287294
288- /** @return UI for all of the y axes */
289- private yAxes ( chart : BaseChartProps ) {
295+ /**
296+ * Note: We need to interleave things so that the y axis for a
297+ * series occurs just before that series. This seems to be how
298+ * Victory wants the data to be arranged.
299+ *
300+ * For example: `(yAxis1, series1, series2, yAxis2, series3)` would
301+ * have `series1` and `series2` sharing `yAxis1`, and `series3`
302+ * using `yAxis2`.
303+ *
304+ * @return UI for all of the data sets and y axes.
305+ */
306+ private dataSetsAndYAxes ( chart : BaseChartProps ) {
290307 return chart . series . flatMap ( ( { impl, stroke, fill = stroke , data } , idx ) => {
291308 const yAxis =
292309 chart . yAxes [ idx ] ||
@@ -309,9 +326,9 @@ export default class BaseChart extends React.PureComponent<Props> {
309326
310327 const chartui =
311328 impl === "ChartArea" ? (
312- < ChartArea key = { idx } interpolation = "monotoneX" name = { data [ idx ] . name } { ...props } />
329+ < ChartArea key = { idx } interpolation = "monotoneX" name = { data [ 0 ] . name } { ...props } />
313330 ) : (
314- < ChartLine key = { idx } interpolation = "monotoneX" name = { data [ idx ] . name } { ...props } />
331+ < ChartLine key = { idx } interpolation = "monotoneX" name = { data [ 0 ] . name } { ...props } />
315332 )
316333
317334 if ( chart . yAxes [ idx ] ) {
@@ -362,10 +379,14 @@ export default class BaseChart extends React.PureComponent<Props> {
362379 } ) )
363380 }
364381
365- /** @return the UI for the idx-th chart in the chart set of this.props.charts */
366- private chart ( chart : BaseChartProps , idx : number ) {
367- const CursorVoronoiContainer = createContainer ( "voronoi" , "cursor" )
382+ private readonly getTooltipTitle = ( datum : { x : number } ) =>
383+ `${ new Date ( datum . x + this . props . minTime ) . toLocaleString ( ) } `
384+
385+ /** Our impl of a Victory container component; used in `containerComponent()`*/
386+ private readonly CursorVoronoiContainer = createContainer ( "voronoi" , "cursor" )
368387
388+ /** @return a Victory "container" that will handle the mouse motion (for tooltips) for us */
389+ private containerComponent ( chart : BaseChartProps ) {
369390 // map from data series name to format (used in getTooltipLabels())
370391 const formatMap = chart . yAxes . reduce ( ( M , yAxis , idx , yAxes ) => {
371392 if ( ! yAxis ) {
@@ -381,34 +402,38 @@ export default class BaseChart extends React.PureComponent<Props> {
381402 return M
382403 } , { } as Record < string , Format > )
383404
405+ return (
406+ < this . CursorVoronoiContainer
407+ mouseFollowTooltips
408+ voronoiDimension = "x"
409+ labels = { this . getTooltipLabels . bind ( this , formatMap ) }
410+ labelComponent = {
411+ < ChartLegendTooltip
412+ isCursorTooltip
413+ flyoutStyle = { this . flyoutStyle }
414+ labelComponent = { < MyTooltipContent /> }
415+ legendData = { this . getLegendData ( chart ) }
416+ title = { this . getTooltipTitle }
417+ />
418+ }
419+ />
420+ )
421+ }
422+
423+ /** @return the UI for the idx-th chart in the chart set of this.props.charts */
424+ private chart ( chart : BaseChartProps , idx : number ) {
384425 return (
385426 < div className = "codeflare-chart-container" key = { idx } >
386427 < Chart
387428 ariaDesc = { chart . desc }
388- padding = { chart . padding || BaseChart . padding }
429+ padding = { BaseChart . padding }
389430 width = { BaseChart . dimensions . width }
390431 height = { BaseChart . dimensions . height }
391- domain = { chart . domain }
392- containerComponent = {
393- < CursorVoronoiContainer
394- mouseFollowTooltips
395- voronoiDimension = "x"
396- labels = { this . getTooltipLabels . bind ( this , formatMap ) }
397- labelComponent = {
398- < ChartLegendTooltip
399- isCursorTooltip
400- flyoutStyle = { { fillOpacity : 0.825 , fill : "var(--color-base06)" } }
401- labelComponent = { < MyTooltipContent /> }
402- legendData = { this . getLegendData ( chart ) }
403- title = { ( datum : any ) => `${ new Date ( datum . x + this . props . timeRange . min ) . toLocaleString ( ) } ` }
404- />
405- }
406- />
407- }
432+ containerComponent = { this . containerComponent ( chart ) }
408433 >
409434 { this . title ( chart ) }
410435 { this . xAxis ( ) }
411- { this . yAxes ( chart ) }
436+ { this . dataSetsAndYAxes ( chart ) }
412437 </ Chart >
413438 </ div >
414439 )
0 commit comments