Skip to content

Instantly share code, notes, and snippets.

@edharmowongso
Last active May 29, 2017 16:28
Show Gist options
  • Save edharmowongso/346022822d728d5a83a45605749e0176 to your computer and use it in GitHub Desktop.
Save edharmowongso/346022822d728d5a83a45605749e0176 to your computer and use it in GitHub Desktop.
Line Chart
license: mit
<!doctype html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>Line Chart</title>
<style>
.axis path,
.axis line{
fill: none;
stroke: black;
}
.axis line {
opacity: 0.8;
stroke-dasharray: 5 5;
}
.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 margin = {top: 20, right: 100, bottom: 30, left: 100},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var dataset = [
{x: 0, y: 5},
{x: 1, y: 8},
{x: 2, y: 13},
{x: 3, y: 12},
{x: 4, y: 16},
{x: 5, y: 21},
{x: 6, y: 18},
{x: 7, y: 23},
{x: 8, y: 24},
{x: 9, y: 28},
{x: 10, y: 35},
{x: 11, y: 30},
{x: 12, y: 32},
{x: 13, y: 36},
{x: 14, y: 40},
{x: 15, y: 38},
];
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));
svg.selectAll("g").data(dataset).enter()
.append("circle")
.attr("cx", function(d) { return x(d.x) + 50; })
.attr("cy", function(d) { return y(d.y); })
.attr("r", 5)
.style("fill", "black")
.on("mouseover", function(d) {
d3.select(this).style("fill", "red")
svg.selectAll("path").data([d]).enter()
.append("line")
.style("stroke", "black")
.style("stroke-dasharray", ("2"))
.attr("id","line")
.attr("x1",function(d){return x(d.x)+50;})
.attr("y1",function(d){return y(d.y);})
.attr("x2",function(d){return x(d.x)+50;})
.attr("y2",400)
svg.selectAll("#tooltip").data([d]).enter()
.append("text")
.text(function(d){return(d.value);})
.attr("id","tooltip")
.attr("x",function(d){return x(d.x)+50;})
.attr("y",function(d){return y(d.y)-25;})
.style("fill","green")
})
.on("mouseout", function(d){
d3.select(this).style("fill","black")
svg.selectAll("#line, #tooltip").data(data).remove()
});
svg.selectAll("g").data([dataset[dataset.length -1]]).enter()
.append("text")
.text(function(d, i) { return displayValue(d.y); })
.attr("class", "line")
.attr("y", function(d) { return y(d.y) })
.attr("x", width - 20)
.style("font-size", 20)
.style("font-family", "monospace");
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment