Skip to content

Instantly share code, notes, and snippets.

@EfratVil
Last active September 3, 2016 09:50
Show Gist options
  • Save EfratVil/33de6b2fc516af2f60742055ecf1ca9a to your computer and use it in GitHub Desktop.
Save EfratVil/33de6b2fc516af2f60742055ecf1ca9a to your computer and use it in GitHub Desktop.
Line chart with information box

Line chart with points hoover triggering updates of information box on the right.

<!DOCTYPE html>
<meta charset="utf-8">
<style>
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
</style>
<head>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<script src="//d3js.org/d3.v4.min.js"></script>
<div class="row" style="width:860px;">
<div class="col-sm-9" id="g1"></div>
<div class="col-sm-3" id="g2" ><br/><br /><br />
<div id="point_info" class="alert" style="background-color:lightgray; visibility:hidden;">
<strong>Point Info!</strong><br/><br />
ID: <p id="point_id" style="display:inline;">...</p><br />
Value: <p id="point_value" style="display:inline;">...</p><br />
</div>
</div>
</div>
<script>
function randomData(samples) {
var data = [],
random = d3.randomNormal();
for (i = 0; i < samples; i++) {
data.push({
id: i + 1,
y: d3.format(".3")(random())
});
}
return data;
}
var data = randomData(200);
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 560 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
var x = d3.scaleLinear()
.range([0, width]);
var y = d3.scaleLinear()
.range([height, 0]);
var line = d3.line()
.x(function(d) { return x(d.id); })
.y(function (d) { return y(d.y); });
var svg = d3.select("#g1").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 + ")");
data.forEach(function (d) {
Object.keys(data[0]).forEach(function (k) {
d[k] = +d[k];
});
});
x.domain(d3.extent(data, function(d) { return d.id; }));
y.domain(d3.extent(data, function (d) { return d.y; }));
var gX = svg.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
var gY = svg.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y));
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 2.5)
.attr("cx", function (d) { return x(d.id); })
.attr("cy", function (d) { return y(d.y); })
.attr("opacity", 1)
.on("mouseover", function (d) {
d3.selectAll(".focus_circle").transition()
.duration(150).remove();
svg.append("circle")
.attr("class", "focus_circle")
.attr("cx", x(d.id))
.attr("cy", y(d.y))
.attr("r", 5)
.style("opacity", 1.0)
.style("fill", "#f46d43");
d3.select("#point_info").transition()
.duration(700).style("visibility", "visible");
d3.select("#point_id").html(d.id);
d3.select("#point_value").html(d.y);
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment