we are going to cover
- data binding
- enter
- exit
- update funcs
- add input (adds empty string to input vals cos input boxes depend on length of array)
- remove input
- fill randomly
- change input (for if we want to change values manually)
- npm i d3
- import * as d3 from "d3";
useEffect(() => {
d3.select("#chart").attr("width", svgWidth).attr("height", svgHeight);
}, []);
- explain binding data
const svg = d3.select("svg");
const circleSelection = svg.selectAll("circle").data(inputValues);
console.log(circleSelection)
show the enter and exit function- explain that if there were 20 circles and ten data points there would be zero in enter and 10 in exit
- if 5 circles and ten data points then there would be zero exit and 5 enter
const enteringCircles = circleSelection
.enter()
.append("circle")
.attr("cx", svgWidth / 2)
.attr("cy", svgHeight / 2)
.attr("r", (d) => d)
.attr("stroke", "salmon")
.attr("stroke-width", 2)
.attr("fill", "none");
circleSelection
.enter()
.append("circle")
.attr("cx", svgWidth / 2)
.attr("cy", svgHeight / 2)
.attr("r", 0)
.attr("stroke", "salmon")
.attr("stroke-width", 2)
.attr("fill", "none")
.transition()
.duration(1500)
.attr("r", (d) => d);
circleSelection.exit().remove();
add transition
circleSelection.exit().transition().duration(1500).attr("r", 0).remove();
- the circle selection refers to the circles on the page
circleSelection
.transition()
.duration(1500)
.attr("r", (d) => d);
const enteringCircles = circleSelection
.enter()
.append("circle")
.attr("cx", svgWidth / 2)
.attr("cy", svgHeight / 2)
.attr("r", 0)
.attr("stroke", "salmon")
.attr("stroke-width", 2)
.attr("fill", "none");
const updateSelection = circleSelection.merge(enteringCircles);
updateSelection
.transition()
.duration(1500)
.attr("r", (d) => d);