Built with blockbuilder.org
Created
May 16, 2018 15:03
-
-
Save yanhann10/6732bcfdcf191aa63636258fdcf0d8db to your computer and use it in GitHub Desktop.
fresh block
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
license: mit |
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> | |
<meta charset="utf-8"> | |
<style> | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
.axis text { | |
font: 10px sans-serif; | |
} | |
.cells path { | |
fill: none; | |
pointer-events: all; | |
} | |
.cells :hover circle { | |
fill: red; | |
} | |
</style> | |
<label> | |
<input type="radio" name='market' value="a" checked/>a | |
<input type="radio" name='market' value="b"/>b | |
</label> | |
<svg width="400" height="200"></svg> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script> | |
var svg = d3.select("svg"), | |
margin = {top: 40, right: 40, bottom: 40, left: 40}, | |
width = svg.attr("width") - margin.left - margin.right, | |
height = svg.attr("height") - margin.top - margin.bottom; | |
var formatValue = d3.format(",d"); | |
var x = d3.scaleLog() | |
.rangeRound([0, width]); | |
var g = svg.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
data = [ | |
{"id":1, | |
"value":20, | |
"group":"a"}, | |
{"id":1, | |
"value":10, | |
"group":"a"}, | |
{"id":1, | |
"value":30, | |
"group":"a"}, | |
{"id":1, | |
"value":40, | |
"group":"a"}, | |
{"id":1, | |
"value":42, | |
"group":"a"}, | |
{"id":1, | |
"value":10, | |
"group":"b"}, | |
{"id":1, | |
"value":12, | |
"group":"b"}, | |
{"id":1, | |
"value":15, | |
"group":"b"}, | |
{"id":1, | |
"value":23, | |
"group":"b"}, | |
{"id":1, | |
"value":22, | |
"group":"b"}, | |
{"id":1, | |
"value":24, | |
"group":"b"} | |
] | |
//default | |
draw(data.filter(d=> d.group=="a")); | |
d3.selectAll("input") | |
.on("change", selectDataset); | |
function selectDataset() | |
{ | |
var value = this.value; | |
if (value == "a") | |
{ | |
draw(data.filter(d=> d.group=="a")); | |
} | |
else if (value == "b") | |
{ | |
draw(data.filter(d=> d.group=="b")); | |
} | |
} | |
///////////////// | |
//draw swarmplot | |
///////////////// | |
function draw(data) { | |
// transition | |
var t = d3.transition() | |
.duration(750); | |
x.domain(d3.extent(data, d=> d.value)); | |
var simulation = d3.forceSimulation(data) | |
.force("x", d3.forceX(function(d) { return x(d.value); }).strength(1)) | |
.force("y", d3.forceY(height / 3)) | |
.force("collide", d3.forceCollide(18)) | |
.stop(); | |
for (var i = 0; i < 120; ++i) simulation.tick(); | |
//axis | |
g.append("g") | |
.transition(t) | |
.attr("class", "axis axis--x") | |
.attr("transform", "translate(0," + height + ")") | |
.call(d3.axisBottom(x).ticks(20, ".0s")); | |
//for mouse-over | |
var cell = g.append("g") | |
.attr("class", "cells") | |
.selectAll("g").data(d3.voronoi() | |
.extent([[-margin.left, -margin.top], [width + margin.right, height + margin.top]]) | |
.x(function(d) { return d.x; }) | |
.y(function(d) { return d.y; }) | |
.polygons(data)).enter().append("g"); | |
//circle | |
cell.append("circle") | |
.transition(t) | |
.attr("r", 3) | |
.attr("cx", function(d) { return d.data.x; }) | |
.attr("cy", function(d) { return d.data.y; }) | |
.attr("fill", d => (d.data.Food_Sub_Group)) | |
.attr("opacity", 0.4); | |
} | |
// x.domain(d3.extent(data, function(d) { return d.value; })); | |
// var simulation = d3.forceSimulation(data) | |
// .force("x", d3.forceX(function(d) { return x(d.value); }).strength(1)) | |
// .force("y", d3.forceY(height / 2)) | |
// .force("collide", d3.forceCollide(4)) | |
// .stop(); | |
// for (var i = 0; i < 120; ++i) simulation.tick(); | |
// g.append("g") | |
// .attr("class", "axis axis--x") | |
// .attr("transform", "translate(0," + height + ")") | |
// .call(d3.axisBottom(x).ticks(20, ".0s")); | |
// var cell = g.append("g") | |
// .attr("class", "cells") | |
// .selectAll("g").data(d3.voronoi() | |
// .extent([[-margin.left, -margin.top], [width + margin.right, height + margin.top]]) | |
// .x(function(d) { return d.x; }) | |
// .y(function(d) { return d.y; }) | |
// .polygons(data)).enter().append("g"); | |
// cell.append("circle") | |
// .attr("r", 3) | |
// .attr("cx", function(d) { return d.data.x; }) | |
// .attr("cy", function(d) { return d.data.y; }); | |
// cell.append("path") | |
// .attr("d", function(d) { return "M" + d.join("L") + "Z"; }); | |
// cell.append("title") | |
// .text(function(d) { return d.data.id + "\n" + formatValue(d.data.value); }); | |
// function type(d) { | |
// if (!d.value) return; | |
// d.value = +d.value; | |
// return d; | |
// } | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment