React-vis: Crosshair does not work when xType="time"

Created on 2 Nov 2018  路  6Comments  路  Source: uber/react-vis

The xType="time" is working great on a LineSeries, but when I attempt to display a Crosshair that appears over the corresponding x-axis value, I get errors due to Date being an object.

The data used by the crosshair values prop is the same as what the LineSeries is receiving.

screen shot 2018-11-01 at 8 38 49 pm

The error message:

screen shot 2018-11-01 at 8 39 04 pm

Most helpful comment

@ejmudrak I was having the same issue and the way to get around it is by adding a titleFormat to the Crosshair which would convert from a date object to a string which which is a valid react child. This way you don't disrupt the auto positioning of the crosshair.

            <Crosshair values={crosshair}
              titleFormat={(d) => ({title: 'Date', value: d[0].x.toLocaleDateString()})}
          />

All 6 comments

May be related to #1031

I'm not sure that this is related, time is working fine on the x-axis, I believe there is just a format issue with the Crosshair component.

screen shot 2018-11-08 at 12 51 10 pm

For actual hints and crosshairs, you don't want to use an actual date object. I would recommend using d3's time format function to turn it into a string that you would like to see on hover. Or you could just say, for example, EriksCrossHairValue.x.toString()to really make it functional for now.

Sure! I've made them into strings already, but for the crosshair x to appear over the correct x-axis coordinate, it needs to be the same format as what is supplied to LineSeries. Otherwise the crosshair always appears off the left of the graph like so:

screen shot 2018-11-08 at 1 41 50 pm

@ejmudrak I was having the same issue and the way to get around it is by adding a titleFormat to the Crosshair which would convert from a date object to a string which which is a valid react child. This way you don't disrupt the auto positioning of the crosshair.

            <Crosshair values={crosshair}
              titleFormat={(d) => ({title: 'Date', value: d[0].x.toLocaleDateString()})}
          />

Thanks a ton @truncs

I had to convert it back to a date, but the Crosshair now positions correctly for dates! I also had to make sure that my function for onNearestX didn't touch any formatting.

```
titleFormat={ d => ({
title: 'Date',
value: new Date(d[0].x).toLocaleDateString(),
}) } />

Was this page helpful?
0 / 5 - 0 ratings

Related issues

basilaiman picture basilaiman  路  3Comments

cang-afero picture cang-afero  路  4Comments

wroughtec picture wroughtec  路  4Comments

mcnuttandrew picture mcnuttandrew  路  3Comments

mcnuttandrew picture mcnuttandrew  路  5Comments