Built with blockbuilder.org
forked from romsson's block: simple line chart from dataset
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: none;
stroke: black;
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 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 y1 = [{name:"A", value: 10, y2: 420},{name: "B", value:30, y2: 420},
{name: "C", value: 20, y2: 420},{name: "D", value: 40, y2: 420}, {name:"E", value: 50, y2: 420},{name: "F", value: 20, y2: 420},{name: "G", value: 10, y2: 420},{name: "H", value: 30, y2: 420},{name: "I", value: 40, y2: 420},{name: "J", value: 60, y2: 420},{name: "K", value: 30, y2: 420},{name: "L", value: 10, y2: 420}];
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", 10)
.style("font-family", "monospace");
g.selectAll("line").data(y1).enter().append("line")
.style("stroke", "black")
.style("stroke-dasharray", "3, 3")
.style("opacity", 0)
.attr("x1",function(d) { return x(d.name); })
.attr("y1",function(d) { return y(d.value); })
.attr("x2",function(d) { return x(d.name); })
.attr("y2",function(d) { return d.y2; })
g.selectAll("path").data([data]).enter().append("path")
.attr("class", "line")
.attr("d", line);
g.selectAll("circle").
data(data).enter().append("circle")
.attr("cy", function (d) { return y(d.value);})
.attr("cx", function (d) { return x(d.name);})
.attr("r", function (d) { return d.rad; })
.style("fill", "darkgray")
.on("mouseover",function(d,i) {
alert("choosen"+i)
d3.select("g").select("line:nth-child("+ (i + 1)+")").style("opacity",5)
d3.select(this).style("fill","red");
//d3.select(this).append("text").attr("class", "hover").text(data[i].value)
svg.append("text").attr({
id: "leg-" + i, // Create an id for text so we can select it later for removing on mouseout
x: function() { return x(d.name) +50; },
y: function() { return y(d.value) - 15; }
})
.text(function() {
return data[i].value; // Value of the text
});
})
.on("mouseout",function(d,i){
d3.select("g").select("line:nth-child("+ (i + 1)+")").style("opacity",0)
d3.select(this).style("fill","darkgray")
d3.select("#leg-" + i).remove();
})
});
</script>
</body>
https://d3js.org/d3.v3.min.js