I have the following data
JSON structure.
var data = {
"Accel": {
"10%": {
"x": [0,1,2,3,4,5,6,...],
"y": [0.3769,0.4052,0.4354,0.4675,...]
},
"20%": {
"x": [0,1,2,3,4,5,6,...],
"y": [0.7543,0.3756,0.7556,0.2344...]
},
...
"100%": {
"x": [0,1,2,3,4,5,6,...],
"y": [0.7543,0.3756,0.7556,0.2344...]
}
}
}
I want to draw that 10 curves / polylines in the same axes with D3.js using the alphanumeric intermediate key (XX%) to label each curve on the plot. My best try so far:
// Margin, scales and axes
var margin = {top: 20, right: 50, bottom: 50, left: 20}, width = 1200, height = 800;
var xScale = d3.scaleLinear().domain([0,60]).range([0, width - margin.left - margin.right]).nice();
var yScale = d3.scaleLinear().domain([0,30]).range([height - margin.top - margin.bottom, 0]).nice();
var xAxis = d3.axisBottom(xScale).ticks(10, "s");
var yAxis = d3.axisRight(yScale).ticks(10, ".3");
// SVG
var svg = d3.select(".graph").append("svg")
.attr("preserveAspectRatio", "xMinYMin meet")
.attr('viewBox','0 0 ' + width + ' ' + height);
// X axis
svg.append("g")
.attr("class", "x-axis")
.attr("transform", "translate(" + margin.left + "," + (height - margin.bottom) + ")")
.call(xAxis);
// Y axis
svg.append("g")
.attr("class", "y-axis")
.attr("transform", "translate(" + (width - margin.right) + "," + margin.top + ")")
.call(yAxis);
// Line definition
var TbhLine = d3.line()
.x(d => xScale(d.x))
.y(d => yScale(d.y));
// Lines plotting
var TbhPaths = svg.append("g")
.selectAll("path")
.data(data["Accel"])
.enter()
.append('path')
.attr("fill", "none")
.attr("stroke-width", 1.5)
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.attr("d", d => TbhLine(d));
When I run it, axes, scales, domains and ranges are all fine, but no line is plotted. Any ideas on how could this be done? I am using D3 v6.
Thanks in advance.