FormidableLabs / victory

A collection of composable React components for building interactive data visualizations
http://commerce.nearform.com/open-source/victory/
Other
10.93k stars 524 forks source link

VoronoiContainer does not work with Dates on a discontinuous scale #2588

Open sem4phor opened 1 year ago

sem4phor commented 1 year ago

Describe the bug I want to create a timeline charts with hoverable scatter points. Therefore I wrapped a scatter chart with a voronoicontainer and a discontinuous scale to fix weekend gaps (there is no trading happening on the weekends resulting in no data points). This breaks because it seems that the voronoicontainer does some internal transformations from date to timestamp and passing it to the scale.

Error: e.getDay is not a function (after debugging e is a timestamp instead of an expected Date)

Victory version Which version are you using? 36.6.8

Code Sandbox link Paste following code to https://formidable.com/open-source/victory/gallery/discontinuous-scale/

function App() {
  // no weekends included
  const data = [
    { x: new Date(2023, 2, 14), y: 8 },
    { x: new Date(2023, 2, 15), y: 8 },
    { x: new Date(2023, 2, 16), y: 8 },
    { x: new Date(2023, 2, 17), y: 8 },
    { x: new Date(2023, 2, 20), y: 8 },
    { x: new Date(2023, 2, 21), y: 8 },
    { x: new Date(2023, 2, 22), y: 8 },
    { x: new Date(2023, 2, 23), y: 8 },
    { x: new Date(2023, 2, 24), y: 8 },
    { x: new Date(2023, 2, 27), y: 8 },
    { x: new Date(2023, 2, 28), y: 8 },
    { x: new Date(2023, 2, 29), y: 8 },
    { x: new Date(2023, 2, 30), y: 8 },
    { x: new Date(2023, 2, 31), y: 8 },
  ];

  const discontinuousScale = scaleDiscontinuous(
    d3Scale.scaleTime()
  ).discontinuityProvider(discontinuitySkipWeekends());

  return (
    <VictoryChart
      scale={{ x: discontinuousScale
      }}
      containerComponent={
          <VictoryVoronoiContainer
          voronoiDimension="x"
          labels={({ datum }) => new Date(datum.x).toString()}
          voronoiBlacklist={['line']}
        />
      }
    >
    <VictoryLine data={data} name="line" />
    <VictoryScatter
         data={data}
     />
    </VictoryChart>
  );
}

ReactDOM.render(<App />, mountNode);

To Reproduce Steps to reproduce the behavior:

  1. Go to https://formidable.com/open-source/victory/gallery/discontinuous-scale/
  2. Paste the code
  3. Open the browser console
  4. Hover the chart
  5. See the error in the console

Expected behavior The tooltip of the voronoi container is displayed and no error in the console is there.

Screenshots Screenshot 2023-04-28 at 10 01 19

Desktop (please complete the following information):

Smartphone (please complete the following information):

sem4phor commented 1 year ago

A potential fix is to handle dates in the getDatasets method of the voronoi helpers:

const voronoiX = x instanceof Date ? new Date((Number(x) + Number(x0)) / 2) : (Number(x) + Number(x0)) / 2;
// same for Y
sem4phor commented 1 year ago

@boygirl If you agree that this is a bug & the fix above a valid approach to fix it i could open a PR?