Skip to content

Instantly share code, notes, and snippets.

@farazshuja
Last active August 25, 2017 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farazshuja/557525aa93b0d1a064189dcafabf8b1a to your computer and use it in GitHub Desktop.
Save farazshuja/557525aa93b0d1a064189dcafabf8b1a to your computer and use it in GitHub Desktop.
fresh block
license: mit
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://d3js.org/d3.v3.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<style type="text/css">
body, html {
width: 100%;
height: 100%;
margin: 0;
}
#main {
border: 1px solid #ccc;
width: 400px;
height: 400px;
overflow: hidden;
}
#list a {
display: inline-block;
color: #073b98;
font-size: 16px;
padding: 5px;
cursor: pointer;
}
#list a.active {
background: #eee;
font-weight: bold;
}
#list > div {display: inline-block;}
</style>
</head>
<body>
<div id="main"></div>
<div id="list"></div>
</body>
<script type="text/javascript">
var data = [
{id: 1, radius: 10, title: 'Item 1'},
{id: 2, radius: 15, title: 'Item 2'},
{id: 3, radius: 20, title: 'Item 3'},
{id: 4, radius: 25, title: 'Item 4'},
{id: 5, radius: 30, title: 'Item 5'}
];
var width = 400;
var height = 400;
var scale = d3.scale.linear()
.domain([0,1])
.range([-height/2,height/2]);
var svg = d3.select('#main')
.append('svg')
.attr('width', '400px')
.attr('height', '400px')
.call(d3.behavior.zoom().on("zoom", function () {
svg.attr("transform", "translate(" + d3.event.translate + ")" + " scale(" + d3.event.scale + ")")
}));
svg.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("cx", function(d,i){return (width/2 + scale(Math.random())*(i/5))})
.attr("cy", function(d,i){return (.5+i)*height/5})
.attr("r", function(d,i){return d.radius})
.attr("fill", function(){return randomcolor()})
.on("mouseover", handleMouseOver)
.on("mouseout", handleMouseOut);
function randomcolor(){
return "#" + Math.random().toString(16).slice(2, 8) + "";
}
function handleMouseOver(d, i) {
d3.select(this).attr("r", d.radius*1.2);
$('#list a').eq(i).addClass('active');
}
function handleMouseOut(d, i) {
d3.select(this).attr({
r: d.radius
});
$('#list a').eq(i).removeClass('active');
}
function itemOn(i) {
var $s = $('svg circle').eq(i)[0];
var d = d3.select($s).data();
handleMouseOver.call($s, d[0], i)
}
function itemOut(i) {
var $s = $('svg circle').eq(i)[0];
var d = d3.select($s).data();
handleMouseOut.call($s, d[0], i)
}
//draw list
for(var i=0;i<data.length;i++) {
var $div = $('<div><a onmouseover="itemOn(' + i +')" onmouseout="itemOut(' + i +')">' + data[i].title + '</a></div>');
$('#list').append($div);
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment