Created
December 11, 2017 18:39
-
-
Save tbnorth/e71469dd83635b8b3a2c7afa77ff6868 to your computer and use it in GitHub Desktop.
d3 layout experiment
This file contains hidden or 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
<html> | |
<head> | |
<style> | |
</style> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src="https://d3js.org/d3-dispatch.v1.min.js"></script> | |
<script src="https://d3js.org/d3-selection.v1.min.js"></script> | |
<script src="https://d3js.org/d3-drag.v1.min.js"></script> | |
</head><body> | |
<svg width='900' height='600'> | |
</svg> | |
</body> | |
<script> | |
var booths = [ | |
{name: 'Fred', rot: 0, x: 50, y: 50}, | |
{name: 'Joan', rot: 0, x: 50, y: 50}, | |
{name: 'Liam', rot: 90, x: 50, y: 50}, | |
{name: 'Mike', rot: 0, x: 50, y: 50}, | |
] | |
booth_trans = (d) => {return(`translate(${d.x},${d.y}) rotate(${d.rot})`)} | |
var g = d3.select("svg") | |
.selectAll("p").filter(".b") | |
.data(booths) | |
.enter() | |
.append('g').attr('transform', booth_trans) | |
.call(d3.drag() | |
.on("start", dragstarted) | |
.on("drag", dragged) | |
.on("end", dragended)) | |
// .text(function(d) { return "I’m number " + d.s + "!" }) | |
// .style('margin-left', function(d) { return d.s }) | |
// .on('click', (d) => { console.log('d.name') } ) | |
g.append("circle").attr('r', 10) | |
g.append("circle").attr('r', 10).attr('cx', 70) | |
g.append("text").text((d) => d.name).attr('transform', (d) => `rotate(${-d.rot})`) | |
function dragstarted(d) { | |
if (d3.event.sourceEvent.shiftKey) d.rot += 90 | |
d3.select(this).attr('transform', booth_trans) | |
d3.select(this).select('text').attr('transform', `rotate(${-d.rot})`) | |
} | |
function dragged(d) { | |
d3.select(this).attr('transform', (d) => {d.x = d3.event.x; d.y = d3.event.y; return booth_trans(d)}) | |
} | |
function dragended(d) { | |
} | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment