Using CSV for data
I wish to replace this portion of my script (from a dashingd3js tutorials)
with a reference to a CSV file with the same data.
var lineData = [ { "x": 1, "y": 5}, { "x": 20, "y": 20},
{ "x": 40, "y": 10}, { "x": 60, "y": 40},
{ "x": 80, "y": 5}, { "x": 100, "y": 60}];
The csv is located in the same directory and named 'dataFile.csv'
dataFile.csv:
x,y 1,5 20,20 40,10 60,40 80,5 100,60
Edit: Trying to incorporate feedback from Lars, this is what I tried:
//The data for our line
d3.csv("testData.csv", function(error, d){
//This is the accessor function we talked about above
var lineFunction = d3.svg.line()
.x(function(d) { return d.x; })
.y(function(d) { return d.y; })
.interpolate("linear");
//The SVG Container
var svgContainer = d3.select("body").append("svg")
.attr("width", 200)
.attr("height", 200);
//The line SVG Path we draw
var lineGraph = svgContainer.append("path")
.attr("d", lineFunction(lineData))
.attr("stroke", "blue")
.attr("stroke-width", 2)
.attr("fill", "none");
}
No comments:
Post a Comment