Skip to content

Instantly share code, notes, and snippets.

@jwilber
Last active August 24, 2019 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwilber/f1141b8726cb5fc4e3f4cf804a6c4ef0 to your computer and use it in GitHub Desktop.
Save jwilber/f1141b8726cb5fc4e3f4cf804a6c4ef0 to your computer and use it in GitHub Desktop.
rough circles
license: mit
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.rawgit.com/pshihn/rough/master/dist/rough.js"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<!-- Create a div where the graph will take place -->
<div id="vis"></div>
<script>
const data = [1,5,3,4,7,1]
const margin = { top: 40, right: 20, bottom: 50, left: 100 }
const width = 920 - margin.left - margin.right;
const height = 420 - margin.top - margin.bottom;
let svg = d3.select('#vis')
.append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.append("g")
.attr('id', 'new')
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")")
const roughSvg = document.getElementById('new');
const rc = rough.svg(roughSvg, {
options: {
fill: 'chocolate',
stroke: 'chocolate'
}
});
var max = d3.max(data);
var pix = 10;
data.forEach((d,i)=>{
let node = rc.circle(
i*60+20,
40,
d*pix)
let bar = roughSvg.appendChild(node);
bar.setAttribute('class', 'bar');
})
d3.selectAll('g.bar')
.data(data)
.append('circle')
.attr('cx', (d,i) => i*60+20)
.attr('cy', 40)
.attr('r', d => d*pix/2)
.attr('fill', 'transparent');
d3.selectAll('g.bar')
.on('mouseover', function() {
d3.select(this).select('path').style('stroke', 'blue')
})
d3.selectAll('g.bar')
.on('mouseout', function() {
d3.select(this).selectAll('path').style('stroke', 'chocolate')
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment