I'm trying to create my first react d3 line chart but I'm running into a snag. There are no errors showing up when I inspect Chrome so I'm not sure why it's not showing up. I feel like I'm close though.
I've tried to look at other examples but I don't find many with csv examples for some reason.
Here is my code:
import React, { useRef, useEffect, useState } from "react";
import * as d3 from "d3";
import csvData from "../sandbox.csv";
import {
select,
line,
curveCardinal,
axisBottom,
axisRight,
scaleLinear,
} from "d3";
function ActionsLineGraph() {
const [data, setData] = useState(null);
const [loading, setLoading] = React.useState(true);
// const [data, setData] = useState([25, 30, 45, 60, 20, 65, 75]);
const svgRef = useRef();
// will be called initially and on every data change
useEffect(() => {
d3.csv(csvData).then((data) => {
// console.log("Fetching Data");
console.log(data);
setData(data);
setLoading(false);
const svg = select(svgRef.current);
const xScale = scaleLinear()
.domain([0, data.length - 1])
.range([0, 300]);
const yScale = scaleLinear().domain([0, 150]).range([150, 0]);
const xAxis = axisBottom(xScale)
.ticks(data.length)
.tickFormat((index) => index + 1);
svg.select(".x-axis").style("transform", "translateY(150px)").call(xAxis);
const yAxis = axisRight(yScale);
svg.select(".y-axis").style("transform", "translateX(300px)").call(yAxis);
// set the dimensions and margins of the graph
const margin = { top: 20, right: 20, bottom: 50, left: 70 },
width = 300 - margin.left - margin.right,
height = 150 - margin.top - margin.bottom;
// add X axis and Y axis
const x = d3.scaleTime().range([0, width]);
const y = d3.scaleLinear().range([height, 0]);
const parseTime = d3.timeParse("%Y-%m-%d");
const myLine = d3.line()
.x(d => x(xScale(parseTime(d.date))))
.y(d => y(yScale(Number(d.added))));
/* const myLine = d3.line()
.x((d) => { return x(parseTime(d.date)); })
.y((d) => { return y(Number(d.added)); }); */
svg
.append("path")
.data([data])
.attr("class", "line")
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 10)
.attr("d", myLine)
.style('overflow', 'visible')
// .style("transform", "translate(500px, 150px)");
});
}, []);
return (
<React.Fragment>
<svg ref={svgRef}>
<g className="x-axis" />
<g className="y-axis" />
</svg>
</React.Fragment>
);
}
export default ActionsLineGraph;
Here is my csv data I'm using:
date,added,updated,deleted
2021-09-15,10,9,8
2021-09-16,20,11,7
2021-09-17,15,12,9
2021-09-18,20,9,8
2021-09-19,20,9,8
Currently, it just shows the tips of the axes
Any and all help or direction is appreciated.