Skip to content

Instantly share code, notes, and snippets.

Created October 1, 2016 16:40
Show Gist options
  • Save anonymous/f37e70ce8098b742b00b18219ac777e2 to your computer and use it in GitHub Desktop.
Save anonymous/f37e70ce8098b742b00b18219ac777e2 to your computer and use it in GitHub Desktop.
Random Bouncy Circles
license: mit

ランダムバブルチャート

更新する度に表示が変わるバブルチャート

forked from hunzy's block: ランダムバブルチャート

forked from anonymous's block: ランダムバブルチャート

forked from anonymous's block: ランダムバブルチャート

forked from anonymous's block: ランダムバブルチャート

forked from anonymous's block: ランダムバブルチャート

forked from anonymous's block: ランダムバブルチャート

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

forked from anonymous's block: Random Bouncy Circles

<!doctype html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>バブルチャート</title>
<style>
body {
background: #000;
}
</style>
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<script>
// バブルチャート用のデータをランダムに生成
var dataset = [];
var random5to20 = d3.randomNormal(0.1);
var random0to100 = d3.randomUniform(0, 100);
for(i=0; i<20; i++){
var _x = random0to100();
var _y = random0to100();
var _r = random5to20();
dataset.push({x:_x, y:_y, r:_r});
}
// 色の配列(20個)
var color = d3.scaleOrdinal(d3.schemeCategory20);
// svg領域の幅と高さとパディング
var w = 960;
var h = 500;
var p = 50;
// Create SVG elements
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h)
var xScale = d3.scaleLinear()
.domain([0, d3.max(dataset.map(d => d.x))])
.range([p, w-p])
var yScale = d3.scaleLinear()
.domain([0, d3.max(dataset.map(d => d.y))])
.range([p, h-p])
var rScale = d3.scaleLinear()
.domain([0, d3.max(dataset.map(function(d){ return d.r; }))])
.range([0, 100])
svg.selectAll("circle")
.data(dataset)
.enter().append("circle")
.attr('cx', function(d) { return xScale(d.x)})
.attr('cy', function(d) { return yScale(d.y)})
.attr('fill', function(d,i){return color(i)})
.attr('r', 0);
var t = d3.transition()
.duration(1000)
.ease(d3.easeLinear);
d3.selectAll("circle").transition(t)
.attr('r', function(d) { return rScale(d.r)});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment