Last active
December 21, 2015 22:29
-
-
Save Sarah-W/6376046 to your computer and use it in GitHub Desktop.
D3 practice
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> | |
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
<meta charset="utf-8"> | |
<title></title> | |
<!--<script type="text/javascript" src="./D3 Page Template_files/d3.v3.min.js"></script>--> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<style type="text/css"> | |
div{ | |
background-color:darkgrey; | |
width:950px; | |
border:2px solid black; | |
text-align:center; | |
font-family:sans-serif; | |
} | |
#control{font-size:8pt; | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges;} | |
.tracer{stroke : black; | |
} | |
</style> | |
</head> | |
<body> | |
<div></div> | |
<div style= "display:inline-block;"> | |
<button id=start>Start</button><button id=stop>Stop</button> | |
</div> | |
<script type="text/javascript"> | |
var npoints = 50, | |
width = 500, | |
t =0, | |
lobes = 3, | |
started = false, | |
//r = function(th,t){return 1.2+ Math.cos(t+th/lobes)}, | |
x = function(th,t){return (1.2+ Math.cos(t+th*lobes))*Math.cos(th)}, | |
y = function(th,t){return (1.2+ Math.cos(t+th*lobes))*Math.sin(th)}; | |
var thetas = d3.range(npoints).map(function (d){return 2*Math.PI*d/npoints}); | |
//var myformat = function(d){if(d<1){return d3.format(",.1f")} else {return d3.format(",.0f")} } | |
var svg = d3.select("div").append("svg") | |
.attr("width",width+200) | |
.attr("height",width) | |
.append("g") | |
.attr("id","main"); | |
d3.select("svg").append("g").attr("id","control") | |
//lobes stuff | |
var lobeScale = d3.scale.log().domain([10,0.1]).range([0,400]); | |
var lobeax = d3.svg.axis().scale(lobeScale).orient("right").tickFormat(d3.format(",.1f")); | |
d3.select("#control").append("g").attr("transform","translate (650,40)") | |
.attr("id","lobeg") | |
.append("rect") | |
.attr("height",400) | |
.attr("width",10) | |
.attr("fill","lightgrey") | |
.attr("stroke","black") | |
.attr("id","loberect"); | |
d3.select("#lobeg").append("g") | |
.attr("transform", "translate(10,0)") | |
.call(lobeax); | |
//Put the pointer on the lobe bar | |
d3.select("#lobeg").append("circle") | |
.attr("cy",400) | |
.attr("cx",5) | |
.attr("r",5) | |
.attr("fill","darkgrey") | |
.attr("stroke","black"); | |
var pointer = function(lobes){d3.select("#lobeg").select("circle") | |
.attr("cy", lobeScale(lobes));} | |
pointer(lobes); | |
//put the lines on the display | |
d3.select("#main").selectAll("line").data(thetas).enter().append("line") | |
.attr("r",5) | |
.attr("x1",function(d){return 20*Math.cos(d)}) | |
.attr("y1",function(d){return 20*Math.sin(d)}) | |
.attr("x2",function(d){return 220*Math.cos(d)}) | |
.attr("y2",function(d){return 220*Math.sin(d)}) | |
.attr("stroke","lightgrey") | |
.attr("transform","translate(350,250)") | |
; | |
//put the tracing sircles on the display | |
d3.select("#main").selectAll("circle").data(thetas).enter().append("circle") | |
.attr("class","tracer") | |
.attr("r",5) | |
.attr("cx",function(d){return 100*x(d,t)}) | |
.attr("cy",function(d){return 100*y(d,t)}) | |
.attr("fill",function(d){return "hsl(" + d*360/(2*Math.PI) + ",100%,50%)"}) | |
.attr("transform","translate(350,250)") | |
; | |
d3.select("#start").on("click",function(){ | |
stop=false | |
if (started == false){ | |
started = true; | |
var myVar=setInterval(function(){ | |
t= t + Math.PI/100; | |
d3.selectAll(".tracer") | |
.attr("cx",function(d){return 100*x(d,t)}) | |
.attr("cy",function(d){return 100*y(d,t)}); | |
//STOP! | |
//if (time > dateend){stop = true;} | |
if (stop == true){clearTimeout(myVar); | |
stop = false; | |
started = false} | |
},20); | |
}; | |
}); | |
d3.select("#stop").on("click",function(){stop=true;}); | |
d3.select("#loberect").on("click",function(){var mouse_pos = d3.mouse(this); | |
lobes = lobeScale.invert(mouse_pos[1]); | |
pointer(lobes); | |
d3.selectAll(".tracer") | |
.attr("cx",function(d){return 100*x(d,t)}) | |
.attr("cy",function(d){return 100*y(d,t)}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment