Built with blockbuilder.org
Last active
November 22, 2018 10:09
-
-
Save romsson/c607174fc6fd5c7bfaa804d46ca70eab to your computer and use it in GitHub Desktop.
simple bar chart with interaction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{"name": "john", "value": 10}, | |
{"name": "bob", "value": 15}, | |
{"name": "bill", "value": 12}, | |
{"name": "terry", "value": 11} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<style> | |
rect { | |
fill: white; | |
stroke: black; | |
stroke-width: 1px; | |
} | |
path { | |
fill: none; | |
stroke: black; | |
} | |
</style> | |
</head> | |
<body> | |
<script> | |
var svg = d3.select("body").append("svg") | |
.attr("width", 960) | |
.attr("height", 500); | |
var data = [22, 32, 21, 23, 10, 22, 11, 19, 30, 50]; | |
var x = d3.scaleLinear().range([0, 500]).domain([0, data.length]); | |
var y = d3.scaleLinear().range([0, 100]).domain([0, d3.max(data)]); | |
var g = svg.append("g").attr("transform", "translate(100, 0)"); | |
d3.json("dataset.json", function(error, data) { | |
if (error) throw error; | |
console.log(data); | |
}) | |
g.selectAll("text").data(data).enter().append("text") | |
.text(function(d) { return d; }) | |
.attr("y", 200) | |
.attr("x", function(d, i) { return x(i) + 20; }) | |
.style("font-size", 12) | |
.style("text-anchor", "middle") | |
.style("font-family", "monospace"); | |
g.selectAll("rect").data(data).enter().append("rect") | |
.attr("x", function(d, i) { return x(i); }) | |
.attr("y", function(d) { return 170 - y(d); }) | |
.attr("height", function(d) { return y(d); }) | |
.attr("width", 500 / data.length - 10 ) | |
.on("mouseover", function(d, i) { | |
d3.select(this).style("fill", "red"); | |
console.log(d3.select(this)) | |
// Using Ids instead of values | |
d3.selectAll("text").filter(function(e, j) { | |
return i === j; | |
}) | |
.style("font-size", 24); | |
}) | |
.on("mouseout", function(d, i) { | |
d3.select(this).transition().duration(500).style("fill", "white"); | |
// Should be using Ids instead of values | |
d3.selectAll("text").filter(function(e, j) { | |
return i === j; | |
}) | |
.transition().duration(500) | |
.style("font-size", 12); | |
}); | |
var line = d3.line() | |
.curve(d3.curveCardinal) | |
.x(function(d, i) { return x(i) + 20; }) | |
.y(function(d, i) { return 170 - y(d); }) | |
g.selectAll("path").data([data]).enter() | |
.append("path") | |
.attr("d", line) | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment