|
<html> |
|
<head> |
|
<style> |
|
.chart text { |
|
font-size: 11px; |
|
font-family: sans-serif; |
|
} |
|
|
|
.axis path, |
|
.axis line { |
|
fill: none; |
|
stroke: #aaa; |
|
shape-rendering: crispEdges; |
|
} |
|
|
|
.hidden { |
|
display: none; |
|
} |
|
|
|
.legendSize circle { |
|
fill: #00796B; |
|
opacity: .8; |
|
} |
|
|
|
.legendTitle { |
|
font-weight: 600; |
|
} |
|
</style> |
|
|
|
<script src="https://d3js.org/d3.v4.min.js"></script> |
|
<script src="https://cdn.rawgit.com/susielu/d3-annotation/75ff6169/d3-annotation.js"></script> |
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-legend/2.24.0/d3-legend.js"></script> |
|
</head> |
|
<body> |
|
|
|
<div class="chart"></div> |
|
|
|
<script> |
|
var height = 400; |
|
var width = 600; |
|
var margin = { |
|
global: 40, |
|
top: 40, |
|
right: 200, |
|
bottom: 80, |
|
left: 40 |
|
}; |
|
|
|
d3.csv("data.csv", function(d) { |
|
|
|
return { |
|
rank : +d.Rank, |
|
CountryCode : d.CountryCode, |
|
secrecyJurisdiction : d.SecrecyJurisdiction, |
|
fsi: +d.FSI, |
|
secrecyScore: +d.SecrecyScore, |
|
globalScaleWeight: +d.GlobalScaleWeight, |
|
y: +d.FSI, |
|
x: +d.SecrecyScore, |
|
color: +d.Highlight, |
|
size:+ d.GlobalScaleWeight, |
|
}; |
|
}, function(data) { |
|
|
|
var labelY = 'FSI Value'; |
|
var labelX = 'Secrecy Score'; |
|
var svg = d3.select('.chart') |
|
.append('svg') |
|
.attr('class', 'chart') |
|
.attr("width", width + margin.left + margin.right) |
|
.attr("height", height + margin.top + margin.bottom) |
|
.append("g") |
|
.attr("transform", "translate(" + (margin.left) + "," + (margin.top) + ")"); |
|
|
|
var x = d3.scaleLinear() |
|
.domain([d3.min(data, function (d) { return d.x; }), d3.max(data, function (d) { return d.x; })]) |
|
.range([0, width]); |
|
|
|
var y = d3.scaleLinear() |
|
.domain([d3.min(data, function (d) { return d.y; }), d3.max(data, function (d) { return d.y; })]) |
|
.range([height, 0]) |
|
|
|
var scale = d3.scaleSqrt() |
|
.domain([d3.min(data, function (d) { return d.size; }), d3.max(data, function (d) { return d.size; })]) |
|
.range([3, 30]); |
|
|
|
var opacity = d3.scaleSqrt() |
|
.domain([d3.min(data, function (d) { return d.size; }), d3.max(data, function (d) { return d.size; })]) |
|
.range([.8, .6]); |
|
|
|
var color = d3.scaleOrdinal() |
|
.domain([0,1]) |
|
.range(['#00796B', '#FF6D00']); |
|
|
|
// y axis and label |
|
svg.append("g") |
|
.attr("class", "axis axis-y") |
|
.call(d3.axisLeft(y)) |
|
|
|
svg.append("text") |
|
.attr("transform", "rotate(-90)") |
|
.attr("x", 20) |
|
.attr("y", 10) |
|
.attr("dy", ".71em") |
|
.style("text-anchor", "end") |
|
.text(labelY); |
|
|
|
// x axis and label |
|
svg.append("g") |
|
.attr("class", "axis axis-x") |
|
.attr("transform", "translate(0," + height + ")") |
|
.call(d3.axisBottom(x)) |
|
|
|
svg.append("text") |
|
.attr("x", width + 20) |
|
.attr("y", margin.bottom - 10) |
|
.attr("dy", ".71em") |
|
.attr("transform", "translate(0," + (height + 40 - margin.bottom) + ")") |
|
.style("text-anchor", "end") |
|
.text(labelX); |
|
|
|
// bubbles |
|
var bubbles = svg.append("g") |
|
.attr("class", "bubbles"); |
|
|
|
bubbles.selectAll("circle") |
|
.data(data) |
|
.enter() |
|
.insert("circle") |
|
.attr("cx", width / 2) |
|
.attr("cy", height / 2) |
|
.attr("opacity", function (d) { return opacity(d.size); }) |
|
.attr("r", function (d) { return scale(d.size); }) |
|
.style("fill", function (d) { return color(d.color); }) |
|
.on('mouseover', function (d, i) {fade(d.rank, .25); }) //use rank as unique id |
|
.on('mouseout', function (d, i) {fadeOut(d.rank); }) |
|
.transition('load') |
|
.delay(function (d, i) { return x(d.x) - y(d.y); }) |
|
.duration(3000) |
|
.attr("cx", function (d) { return x(d.x); }) |
|
.attr("cy", function (d) {return y(d.y); }) |
|
.ease(d3.easeBack); |
|
|
|
// Annotations |
|
var annotations = []; |
|
data.forEach(function(d) { |
|
annotations.push({ |
|
id: d.rank, |
|
note: { |
|
title: '#'+ d.rank+ ': ' + d.secrecyJurisdiction, |
|
label: "FSI Value " + d.fsi, |
|
wrap: 100 |
|
}, |
|
x: x(d.x), |
|
y: y(d.y), |
|
// data: { |
|
// x: d.x, |
|
// y: d.y |
|
// }, |
|
dy: 30, |
|
dx: 35, |
|
color: color(d.color), |
|
className: 'r' + d.rank + ((d.rank < '11' || d.color > 0) ? ' top10' : ''), |
|
}); |
|
}); |
|
|
|
var makeAnnotations = d3.annotation() |
|
.type(d3.annotationCallout) |
|
//accessors & accessorsInverse not needed |
|
//if using x, y in annotations JSON |
|
// .accessors({ |
|
// x: d => x(d.x), |
|
// y: d => y(d.y) |
|
// }) |
|
.annotations(annotations) |
|
|
|
svg.append("g") |
|
.attr("class", "annotation-group") |
|
.call(makeAnnotations) |
|
|
|
svg.selectAll(".annotation:not(.top10)") |
|
.classed('hidden', true) |
|
|
|
svg.selectAll(".annotation.top10") |
|
.style('opacity', 0) |
|
.transition() |
|
.duration(3300) |
|
.style('opacity', 1); |
|
|
|
|
|
// legends |
|
var legendSize = d3.legendSize() |
|
.shape('circle') |
|
.shapePadding(10) |
|
.labelOffset(10) |
|
.title("Global Scale Weight") |
|
.orient('vertical') |
|
.scale(scale); |
|
|
|
svg.append("g") |
|
.attr("class", "legendSize") |
|
.attr("transform", "translate(" + (width+90) + "," + 0 + ")") |
|
.call(legendSize); |
|
|
|
var legendColor = d3.legendColor() |
|
.shape('circle') |
|
.shapeRadius(5) |
|
.labelOffset(10) |
|
.title("Highlights") |
|
.cellFilter(function(d){ return d.data > 0}) |
|
.labels(['Normal','Highlights']) |
|
.scale(color); |
|
|
|
svg.append("g") |
|
.attr("class", "legendOrdinal") |
|
.attr("transform", "translate(" + (width+90) + "," + 270 + ")") |
|
.call(legendColor); |
|
|
|
// helpers |
|
function fade(rank, opacity) { |
|
svg.selectAll(".bubbles circle") |
|
.filter(function (d) { |
|
return d.rank != rank; |
|
}) |
|
.transition() |
|
.style("opacity", opacity); |
|
|
|
d3.selectAll(".annotation") |
|
.classed('hidden', true) |
|
|
|
d3.select(".r"+rank) |
|
.classed('hidden', false) |
|
} |
|
|
|
function fadeOut() { |
|
svg.selectAll("circle") |
|
.transition() |
|
.style("opacity", function (d) { opacity(d.size); }); |
|
|
|
// and, ...clumsy! |
|
d3.selectAll(".annotation.top10") |
|
.classed('hidden', false) |
|
|
|
d3.selectAll(".annotation:not(.top10)") |
|
.classed('hidden', true) |
|
} |
|
}); |
|
</script> |
|
</body> |
|
</html> |