forked from hunzy's block: Line Chart
forked from jhon0010's block: Line Chart
license: mit |
forked from hunzy's block: Line Chart
forked from jhon0010's block: Line Chart
[ | |
{ | |
"x":"2017-03-23", | |
"y":"2" | |
}, | |
{ | |
"x":"2017-03-26", | |
"y":"4" | |
}, | |
] |
<!doctype html> | |
<html lang="ja"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Line Chart</title> | |
<style> | |
.axis path, | |
.axis line{ | |
fill: none; | |
stroke: black; | |
} | |
.tick text{ | |
font-size: 12px; | |
} | |
.line{ | |
fill: none; | |
stroke: blue; | |
stroke-width: 2px; | |
} | |
</style> | |
</head> | |
<body> | |
<script src="http://d3js.org/d3.v3.js"></script> | |
<script> | |
var dataset = [ | |
{ | |
"x":"2017-03-23", | |
"y":"2" | |
}, | |
{ | |
"x":"2017-03-26", | |
"y":"4" | |
}, | |
] | |
var margin = {top: 20, right: 100, bottom: 30, left: 100}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
var xScale = d3.scale.linear() | |
.domain([0, d3.max(dataset, function(d){ return d.x; })]) | |
.range([0, width]); | |
var yScale = d3.scale.linear() | |
.domain([0, d3.max(dataset, function(d){ return d.y; })]) | |
.range([height, 0]); | |
var xAxis = d3.svg.axis() | |
.scale(xScale) | |
.orient("bottom"); | |
var yAxis = d3.svg.axis() | |
.scale(yScale) | |
.orient("left"); | |
var line = d3.svg.line() | |
.x(function(d) { return xScale(d.x); }) | |
.y(function(d) { return yScale(d.y); }); | |
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 + ")"); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis) | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis) | |
svg.append("path") | |
.datum(dataset) | |
.attr("class", "line") | |
.attr("d", line(dataset)); | |
</script> | |
</body> | |
</html> |