|
<html> |
|
<head> |
|
<style> |
|
.node circle { |
|
stroke: #3a403d; |
|
stroke-width: .5px; |
|
} |
|
body { |
|
background-color:#111111; |
|
} |
|
</style> |
|
</head> |
|
<!-- Creating the buttons --> |
|
<button id="start_pp" style="position: absolute; margin-left: 100px; margin-top: 20px;">Original</button> |
|
<button id="reset_pp" style="position: absolute; margin-left: 200px; margin-top: 20px;">Wine type</button> |
|
<button id="reset_pp" style="position: absolute; margin-left: 300px; margin-top: 20px;">Country</button> |
|
|
|
<body> |
|
|
|
<script src="https://d3js.org/d3.v4.min.js"></script> |
|
<script> |
|
var width = window.innerWidth, height = window.innerHeight, sizeDivisor = 100, nodePadding = .5; |
|
|
|
var svg = d3.select("body") |
|
.append("svg") |
|
.attr("width", width) |
|
.attr("height", height); |
|
|
|
var color = d3.scaleOrdinal(["#f20675", "#f20675", "#f20675", "#f20675", "#f20675", "#f20675", "#f20675", "#f20675"]); |
|
|
|
var simulation = d3.forceSimulation() |
|
.force("forceX", d3.forceX().strength(.3).x(width * .9)) |
|
.force("forceY", d3.forceY().strength(.3).y(height * .9)) |
|
.force("center", d3.forceCenter().x(width * .4).y(height * .4)) |
|
.force("charge", d3.forceManyBody().strength(-25)); |
|
var simulation1 = d3.forceSimulation() |
|
.force("forceX", d3.forceX().strength(.3).x(width * .9)) |
|
.force("forceY", d3.forceY().strength(.3).y(height * .9)) |
|
.force("center", d3.forceCenter().x(width * .4).y(height * .4)) |
|
.force("charge", d3.forceManyBody().strength(-15)); |
|
|
|
|
|
d3.csv("https://gist.githubusercontent.com/linacarrillo23/d6caae34c6d5f3a941caebbb651e4671/raw/de5544f9399f50db921d2b5fd47a40db9731df2a/wine_97_above.csv", types, function(error,graph, graph_top_10,graph_new){ |
|
if (error) throw error; |
|
|
|
// sort the nodes so that the bigger ones are at the back |
|
graph = graph.sort(function(a,b){ return b.size - a.size; }); |
|
graph_top_10 = graph.filter(function(d,i){return i > 0 && i < 11;} ) |
|
//graph_new = graph.filter(function(d,i)return i == 0 || i == 1; ); |
|
|
|
//update the simulation based on the data |
|
simulation |
|
.nodes(graph) |
|
.force("collide", d3.forceCollide().strength(.5).radius(function(d){ return d.radius + nodePadding; }).iterations(1)) |
|
.on("tick", function(d){ |
|
node |
|
.attr("cx", function(d){ return d.x; }) |
|
.attr("cy", function(d){ return d.y; }) |
|
}); |
|
|
|
var node = svg.append("g") |
|
.attr("class", "node") |
|
.selectAll("circle") |
|
.data(graph) |
|
.enter().append("circle") |
|
.attr("r", function(d) { return d.radius; }) |
|
.attr("fill", function(d) { return color(d.country); }) |
|
.attr("cx", function(d){ return d.x; }) |
|
.attr("cy", function(d){ return d.y; }) |
|
.call(d3.drag() |
|
.on("start", dragstarted) |
|
.on("drag", dragged) |
|
.on("end", dragended)); |
|
|
|
|
|
simulation1 |
|
.nodes(graph_top_10) |
|
.force("collide", d3.forceCollide().strength(.5).radius(function(d){ return d.radius + nodePadding; }).iterations(1)) |
|
.on("tick", function(d){ |
|
node_top_10 |
|
.attr("cx", function(d){ return d.x+150; }) |
|
.attr("cy", function(d){ return d.y+200; }) |
|
}); |
|
var node_top_10 = svg.append("g") |
|
.attr("class", "node") |
|
.selectAll("circle") |
|
.data(graph_top_10) |
|
.enter() |
|
.append("circle") |
|
.attr("r", function(d) { return d.radius; }) |
|
.attr("fill", function(d) { return color(d.country); }) |
|
.attr("cx", function(d){ return d.x+100; }) |
|
.attr("cy", function(d){ return d.y+500; }) |
|
.call(d3.drag() |
|
.on("start", dragstarted) |
|
.on("drag", dragged) |
|
.on("end", dragended)); |
|
|
|
|
|
}); |
|
|
|
function dragstarted(d) { |
|
if (!d3.event.active) simulation.alphaTarget(.03).restart(); |
|
d.fx = d.x; |
|
d.fy = d.y; |
|
} |
|
|
|
function dragged(d) { |
|
d.fx = d3.event.x; |
|
d.fy = d3.event.y; |
|
} |
|
|
|
function dragended(d) { |
|
if (!d3.event.active) simulation.alphaTarget(.03); |
|
d.fx = null; |
|
d.fy = null; |
|
} |
|
|
|
function types(d){ |
|
d.variety = +d.variety; |
|
d.size = +d.price / sizeDivisor; |
|
d.size < 3 ? d.radius = 3 : d.radius = d.size; |
|
return d; |
|
} |
|
</script> |
|
</body> |
|
</html> |