Prefacing this with the fact I'm new to D3 and it's probably a simple mistake I'm missing - I know it has to be. Something with the way I'm attempting to access the data.
Trying to figure this guy out for a while. (maybe I've looked at it too long)
I played around with it until my line finally showed up at all, but now it's not displaying the full line, just two points - that are numbers I'm not sure of.
Thanks in advance!
EDIT: The goal is to use lineData[0] to plot one line along the x-axis of lineData1 (months). Eventually, lineData2 will be used for a second line, but just trying to get the first line to work at the moment.
EDIT: Updated code (per Shashank's suggetions) can be found here: https://codepen.io/anon/pen/RevxYG?editors=0010
My updated graph is looking better, but still seeing NaN values:
Here's my code:
let lineData = [
[4, 2, 0, 3, 3, 4, 52, 3, 0, 3, 3, 1],
[0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0],
["Oct", "Nov", "Dec", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep"]
]
console.log("lineData", lineData)
// set dimensions of the line chart
let width = 393,
height = 285;
// set the scales
let yScale = d3.scaleLinear()
.domain([0, 52])
.range([height - 40 , 0]);
let xScale = d3.scaleOrdinal()
.range([0, width - 40]);
// create the line
let line = d3.line()
.x(function(d, i) { return xScale(i); })
.y(function(d) { return yScale(d[0]); });
// create svg container
let lineChart = d3.select(element)
.append("svg")
.attr("viewbox", "0 60 960 600")
.attr("preserveAspectRatio", "xMinYMin meet")
.attr("width", width)
.attr("height", height)
.attr("class", "line-chart")
lineChart.append("path")
.datum(lineData)
.attr("d", line)
.attr("fill", "none")
.attr("stroke", "rebeccapurple")
.attr("stroke-width", 2)
lineChart.selectAll("dot")
.data(lineData)
.attr("d", line)
.enter()
.append("circle")
.attr("r", 3.5)
.attr("cx", function(d, i) { return xScale(i)})
.attr("cy", function(d) { return yScale(d[0])})
.attr("fill", "rebeccapurple")
lineChart.append("g")
.attr("transform", "translate(20, 245)")
.call(d3.axisBottom(xScale))
lineChart.append("g")
.attr("transform", "translate(20, 0)")
.call(d3.axisLeft(yScale))
The data is being passed in via a function.