[ Launch: Clickable Line ] 1d64b6cd8a6799b53c1f by emunsing
-
-
Save emunsing/1d64b6cd8a6799b53c1f to your computer and use it in GitHub Desktop.
Clickable Line
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
{"description":"Clickable Line","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"pingpong","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"ajax-caching":true} |
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
var svg = d3.select("svg"); | |
var data = [ | |
{x: 0, y: 10}, | |
{x: 10, y: 0}, | |
{x: 40, y: 30}, | |
]; | |
var line = d3.svg.line() //function that makes a line | |
.x(function(d) {return d.x;}) | |
.y(function(d) {return d.y;}); | |
var tx = 63; | |
var ty = 150; | |
var myGroup = svg.append("g") | |
.attr("transform", "translate(" + [tx, ty] + ")") | |
var myLineCoords = myGroup.append("text") | |
.attr("transform","translate("+[0,-20]+")") | |
.text(line(data)); | |
var myClickCoords = myGroup.append("text") | |
//.attr("transform","translate("+[0,]+")") | |
.text("Clicked @:"); | |
var myPath = myGroup.selectAll("path") | |
.data([data]) | |
.enter() | |
.append("path") | |
.attr({ | |
d: function(d) { return line(d)}, | |
fill: "none", | |
stroke: "black" | |
}) | |
svg.on("click", function() { | |
var p = d3.mouse(this); | |
data.push({x: p[0] - tx, y: p[1] - ty}) | |
myLineCoords.text(line(data)) | |
myClickCoords.text(p[0]+", "+p[1]) | |
myPath.attr("d", line) | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment