My scatter plot breaks any time I mouse over any of the circles.
Edit: Updated link to newer version of codepen. Still same problem. Any help greatly appreciated!
https://codepen.io/dr_rompecabezas/pen/NWpbBBy
Console error:
Uncaught TypeError: d.Time.substring is not a function
This is the block where the substring method is used:
data.forEach((d) => {
let minutes = d.Time.substring(0, 2);
let seconds = d.Time.substring(3);
d.Time = new Date(1976, 6, 28, 0, minutes, seconds);
});
This code runs fine as long as I do not mouse over the rendered circles. Its purpose is to reformat a string (mm:ss) as a Date Object in order to use the D3 time scale.
The rendered visualization passes all tests, except, of course, the ones that depend on the mouse events working.
Wondering whether the problem has to do with React re-renders, I tried to move this logic into the useEffect Hook that loads the JSON data, so this block runs only once, but it doesnât work because the data state is still null. I am trying to figure out how to mutate the data inside the hook, but havenât been able to do so far. All web examples I have seen are of csv files, using the row argument.
This is how the data is fetched:
useEffect(() => {
d3.json(url).then((json) => {
setData(json);
});
}, []);
I have also rewritten the data mutating function copied at the top of this post in many different ways (e.g., map instead of forEach), to no avail. The scatter plot renders fine with all my approaches but breaks as soon as the mouse enters a circle.
This is how circles are rendered via the Marks component:
<circle
key={data[i].Place}
data-xvalue={xValue(d)}
data-yvalue={yValue(d)}
className="dot"
cx={xScale(xValue(d))}
cy={yScale(yValue(d))}
r="7"
fill={data[i].Doping ? "#FF4500" : "#228C22"}
opacity="0.7"
onMouseEnter={() => setHoveredValue([0, 0])} // [0, 0] will to be switched to [xValue(d), yValue(d)]
onMouseLeave={() => setHoveredValue(null)}
onMouseMove={handleMouseMove}
/>
Any help is greatly appreciated. Thank you.
Challenge: Visualize Data with a Scatterplot Graph
Link to the challenge: