simple D3 line chart
copied from:
https://leanpub.com/D3-Tips-and-Tricks/read#leanpub-auto-starting-with-a-basic-graph
CSS
body {
font: 12px Arial;
}
path {
stroke: steelblue;
stroke-width: 2;
fill: none;
}
.axis path, .axis line {
fill: none;
stroke: grey;
stroke-width: 1;
shape-rendering: crispEdges;
}
JavaScript
console.log("running")
var margin = {
top: 30,
right: 20,
bottom: 30,
left: 50
};
var width = 400 - margin.left - margin.right;
var height = 500 - margin.top - margin.bottom;
var parseDate = d3.time.format("%d-%b-%y").parse;
var y = d3.scale.linear().range([height, 0]);
var x = d3.scale.linear().range([0, width]);
var xAxis = d3.svg.axis().scale(x)
.orient("bottom");
var yAxis = d3.svg.axis().scale(y)
.orient("left");
var valueline = d3.svg.line()
.x(function (d) {
console.log("x: " + d.shf)
return x(d.shf);
})
.y(function (d) {
console.log("y: " + d.depth)
return y(d.depth);
});
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Get the data
var data = [{
depth: "10",
shf: "3"
}, {
depth: "20",
shf: "7"
}, {
depth: "30",
shf: "3"
}, {
depth: "40",
shf: "5"
}, {
depth: "50",
shf: "1"
}, {
depth: "60",
shf: "0"
}];
data.forEach(function (d) {
d.depth = +d.depth;
d.shf = +d.shf;
});
// Scale the range of the data
x.domain(d3.extent(data, function (d) {
console.log("x domain: " + d.shf)
return d.shf;
}));
y.domain([10, d3.max(data, function (d) {
console.log("y domain: " + d.depth)
return d.depth;
})]);
svg.append("path") // Add the valueline path.
.attr("d", valueline(data));
svg.append("g") // Add the X Axis
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g") // Add the Y Axis
.attr("class", "y axis")
.call(yAxis);