xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v3.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
.line {
fill: pink;
stroke: blue;
stroke-width: 1.5px;
}
</style>
</head>
<body>
<script>
// Feel free to change or delete any of the code you see in this editor!
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500)
var width = 800;
var height = 400;
// https://bl.ocks.org/zanarmstrong/raw/05c1e95bf7aa16c4768e/
var randomColor = (function(){
var golden_ratio_conjugate = 0.618033988749895;
var h = Math.random();
var hslToRgb = function (h, s, l){
var r, g, b;
if(s == 0){
r = g = b = l; // achromatic
}else{
function hue2rgb(p, q, t){
if(t < 0) t += 1;
if(t > 1) t -= 1;
if(t < 1/6) return p + (q - p) * 6 * t;
if(t < 1/2) return q;
if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
}
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q;
r = hue2rgb(p, q, h + 1/3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1/3);
}
return '#'+Math.round(r * 255).toString(16)+Math.round(g * 255).toString(16)+Math.round(b * 255).toString(16);
};
return function(){
h += golden_ratio_conjugate;
h %= 1;
return hslToRgb(h, 0.5, 0.60);
};
})();
var parseDate = d3.time.format("%Y-%m");
var displayDate = d3.time.format("%b %y");
var displayValue = d3.format(",.0f");
// Ordinal scale
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .5);
var y = d3.scale.linear()
.range([height, height - 200]);
var line = d3.svg.line()
.x(function(d) { return x(d.name); })
.y(function(d) { return y(d.value); });
var g = svg.append("g")
.attr("transform", "translate(50, 0)")
d3.json("dataset.json", function(data) {
// Pre-processing
data.forEach(function(d) {
d.value;// = +d.value;
d["date"] = parseDate.parse(d["date"]);
});
x.domain(data.map(function(d) { return d.name; }));
y.domain([0, d3.max(data, function(d) { return d.value; })]);
svg.selectAll("text").data(data).enter()
.append("text")
.text(function(d, i) { return displayDate(d.date); })
.attr("y", 420)
.attr("x", function(d) { return x(d.name); })
.style("font-size", 15)
.style("font-family", "Comic Sans MS");
g.selectAll(".value").data(data).enter()
.append("text")
.text(function(d, i) { return displayValue(d.value); })
.attr("class", "value")
.attr("y", function(d) { return y(d.value)-20; })
.attr("x", function(d) { return x(d.name);})
.style("font-size", 10)
.style("opacity",0)
.style("font-family", "Comic Sans MS");
g.selectAll("path").data([data]).enter().append("path")
.attr("class", "line")
.attr("d", line);
g.selectAll("line").data(data).enter().append("line")
.attr('x1',function(d) { return x(d.name); })
.attr('y1',function(d) { return y(0); })
.attr('x2',function(d) { return x(d.name); })
.attr('y2',function(d) { return y(d.value); })
.style("stroke-width", 5)
.style("stroke", randomColor)
// .style("stroke-dasharray", ("2, 2"))
.style("opacity",0);
g.selectAll("circle").data(data).enter()
.append("circle")
.attr("cx",function(d) { return x(d.name); })
.attr("cy",function(d) { return y(d.value); })
.attr("r",8)
.attr("opacity",0.15)
.on("mouseover", function(d) {
d3.select(this).transition().duration(500)
.attr("opacity",1).attr("r",15).style("fill", randomColor);
d3.selectAll(".value").filter(function(e) {
return d === e;
})
.style("opacity",1)
.style("font-size", 30);
d3.selectAll("line").filter(function(e) {
return d === e;
})
.style("opacity",1)
})
.on("mouseout", function(d) {
d3.select(this).transition().duration(1000).style("fill","black").attr("opacity",0.15).attr("r",8);
d3.selectAll(".value").filter(function(e) {
return d === e;})
.style("opacity",0)
.transition().duration(100);
d3.selectAll("line").filter(function(e) {
return d === e;
})
.style("opacity",0)
.transition().duration(100);
});
});
</script>
</body>
https://d3js.org/d3.v3.min.js