I am looking to create a dynamic line chart using d3.js based on data from an infotable. To achieve this, I have converted the information in the infotable into two arrays: a single-dimensional array xValueArray: [0, 10, 20, 30, 40]
and a two-dimensional array
yValueArray: [[0, 20, 30, 40, 50], [0, 200, 250, 400, 450]]
. My approach involves mapping the xValueArray to both arrays within yValueArray to represent x and y-coordinates, which I accomplish by using the map function.
var result = xValueArray.map(function (x, i) {
return [x, yValueArray[0][i]];
});
console.log(result); //returns [[0,0], [10,20], [20,30], [30,40], [40, 50]]
var data = result.map(function(d) {
return {
x: d[0],
y: d[1]
};
});
console.log(data); //returns [[x:0, y:0], [x:10, y:20], [x:20, y:30], [x:30, y:40], [x:40, y:50]]
//************* Plotting of graph ***************
var lineFunction = d3.line()
.x(function(d) {return x(d.x); })
.y(function(d) {return y(d.y); })
.curve(d3.curveLinear);
//plot line
var path1 = g.append("path")
.attr("class", "path1")
.attr("id", "blueLine")
.attr("d", lineFunction(data))
.attr("stroke", "blue")
.attr("stroke-width", 2)
.attr("fill", "none")
.attr("clip-path", "url(#clip)");
The line chart is generated successfully, but with only one line as I mapped the first array in yValueArray to xValueArray. To make the chart dynamic, I aim to plot lines corresponding to the number of data fields inputted by the user in the infotable. For example, if there are 2 arrays in yValueArray, I want 2 lines plotted on the chart. How can I accomplish this task? Any guidance or suggestions would be highly appreciated!