Skip to content

Instantly share code, notes, and snippets.

@SJXdct
Last active October 22, 2017 02:53
Show Gist options
  • Save SJXdct/13f581fc7b78ed26c4b6e439f228b31c to your computer and use it in GitHub Desktop.
Save SJXdct/13f581fc7b78ed26c4b6e439f228b31c to your computer and use it in GitHub Desktop.
Bilevel graph of 28 sites and their trackers
{
"name": "flare",
"children": [
{"name":"apple.com","size":273686,
"children": [
{"name":"RU","size":2},
{"name":"Others","size":4},
{"name":"CN","size":70},
{"name":"Others","size":1},
{"name":"CA","size":4},
{"name":"DE","size":2},
{"name":"JP","size":2},
{"name":"FR","size":16},
{"name":"US","size":322},
{"name":"HK","size":3},
{"name":"Others","size":2},
{"name":"Others","size":2},
{"name":"Others","size":1},
{"name":"Others","size":1},
{"name":"GB","size":1}
]
},
{"name":"baidu.com","size":106203,
"children": [
{"name":"RU","size":2},
{"name":"Others","size":4},
{"name":"CN","size":94},
{"name":"Others","size":2},
{"name":"CA","size":10},
{"name":"DE","size":7},
{"name":"FR","size":21},
{"name":"US","size":505},
{"name":"HK","size":5},
{"name":"Others","size":1},
{"name":"Others","size":3},
{"name":"Others","size":1},
{"name":"Others","size":2},
{"name":"Others","size":1},
{"name":"Others","size":1},
{"name":"Others","size":5},
{"name":"GB","size":2}
]
},
{"name":"weibo.com","size":96152,
"children": [
{"name":"CN","size":51},
{"name":"Others","size":1},
{"name":"CA","size":3},
{"name":"DE","size":3},
{"name":"FR","size":7},
{"name":"US","size":107}
]
},
{"name":"sina.com.cn","size":80500,
"children": [
{"name":"CN","size":57},
{"name":"DE","size":1},
{"name":"FR","size":6},
{"name":"Others","size":3},
{"name":"HK","size":1},
{"name":"US","size":148},
{"name":"GB","size":1}
]
},
{"name":"qq.com","size":76711,
"children": [
{"name":"FR","size":37},
{"name":"DE","size":10},
{"name":"JP","size":7},
{"name":"HK","size":10},
{"name":"RU","size":3},
{"name":"Others","size":4},
{"name":"Others","size":1},
{"name":"Others","size":4},
{"name":"CN","size":110},
{"name":"Others","size":3},
{"name":"CA","size":14},
{"name":"Others","size":3},
{"name":"Others","size":1},
{"name":"Others","size":3},
{"name":"Others","size":5},
{"name":"Others","size":1},
{"name":"Others","size":1},
{"name":"US","size":778},
{"name":"Others","size":5},
{"name":"Others","size":1},
{"name":"Others","size":3},
{"name":"GB","size":9}
]
},
{"name":"163.com","size":76464,
"children": [
{"name":"Others","size":1},
{"name":"CN","size":68},
{"name":"CA","size":1},
{"name":"DE","size":6},
{"name":"JP","size":2},
{"name":"FR","size":8},
{"name":"US","size":197},
{"name":"HK","size":1},
{"name":"GB","size":3},
{"name":"Others","size":1}
]
},
{"name":"hao123.com","size":74462,
"children": [
{"name":"FR","size":5},
{"name":"CN","size":41},
{"name":"DE","size":2},
{"name":"US","size":101},
{"name":"GB","size":3}
]
},
{"name":"xunlei.com","size":57513,
"children": [
{"name":"RU","size":3},
{"name":"CN","size":61},
{"name":"DE","size":3},
{"name":"FR","size":7},
{"name":"US","size":166},
{"name":"HK","size":2},
{"name":"GB","size":4}
]
},
{"name":"sohu.com","size":52253,
"children": [
{"name":"RU","size":1},
{"name":"FR","size":9},
{"name":"CN","size":71},
{"name":"CA","size":2},
{"name":"DE","size":3},
{"name":"JP","size":1},
{"name":"US","size":208},
{"name":"HK","size":1},
{"name":"GB","size":3}
]
},
{"name":"taobao.com","size":51284,
"children": [
{"name":"RU","size":1},
{"name":"FR","size":11},
{"name":"CN","size":75},
{"name":"HK","size":3},
{"name":"CA","size":2},
{"name":"DE","size":4},
{"name":"JP","size":2},
{"name":"US","size":282},
{"name":"Others","size":3},
{"name":"Others","size":4},
{"name":"GB","size":3},
{"name":"Others","size":1}
]
},
{"name":"google.com","size":17001,
"children": [
{"name":"RU","size":3},
{"name":"Others","size":4},
{"name":"CN","size":75},
{"name":"Others","size":1},
{"name":"CA","size":4},
{"name":"DE","size":7},
{"name":"JP","size":1},
{"name":"FR","size":18},
{"name":"US","size":456},
{"name":"HK","size":4},
{"name":"Others","size":1},
{"name":"Others","size":1},
{"name":"Others","size":2},
{"name":"GB","size":1}
]
},
{"name":"youku.com","size":16142,
"children": [
{"name":"FR","size":1},
{"name":"CN","size":16},
{"name":"US","size":30}
]
},
{"name":"haosou.com","size":9335,
"children": [
{"name":"FR","size":1},
{"name":"CN","size":28},
{"name":"CA","size":1},
{"name":"US","size":86},
{"name":"Others","size":1}
]
},
{"name":"xiaomi.com","size":3658,
"children": [
{"name":"FR","size":2},
{"name":"DE","size":1},
{"name":"CN","size":23},
{"name":"US","size":45}
]
},
{"name":"twitter.com","size":820,
"children": [
{"name":"RU","size":1},
{"name":"DE","size":1},
{"name":"CN","size":1},
{"name":"US","size":22}
]
},
{"name":"uber.com","size":743,
"children": [
{"name":"DE","size":1},
{"name":"US","size":20}
]
},
{"name":"toutiao.com","size":627,
"children": [
{"name":"FR","size":2},
{"name":"CN","size":15},
{"name":"US","size":40}
]
},
{"name":"instagram.com","size":525,
"children": [
{"name":"FR","size":1},
{"name":"CN","size":1},
{"name":"Others","size":1},
{"name":"US","size":21},
{"name":"Others","size":1}
]
},
{"name":"bing.com","size":512,
"children": [
{"name":"CN","size":12},
{"name":"DE","size":2},
{"name":"JP","size":2},
{"name":"FR","size":2},
{"name":"US","size":63}
]
},
{"name":"meituan.com","size":505,
"children": [
{"name":"FR","size":1},
{"name":"DE","size":1},
{"name":"CN","size":16},
{"name":"US","size":53}
]
},
{"name":"wandoujia.com","size":389,
"children": [
{"name":"FR","size":4},
{"name":"CN","size":36},
{"name":"DE","size":1},
{"name":"US","size":68},
{"name":"HK","size":1},
{"name":"Others","size":1}
]
},
{"name":"amazon.com","size":377,
"children": [
{"name":"DE","size":1},
{"name":"CN","size":4},
{"name":"US","size":25}
]
},
{"name":"renren.com","size":313,
"children": [
{"name":"FR","size":4},
{"name":"US","size":22},
{"name":"CN","size":2}
]
},
{"name":"alipay.com","size":313,
"children": [
{"name":"FR","size":4},
{"name":"CA","size":1},
{"name":"CN","size":25},
{"name":"US","size":68}
]
},
{"name":"10086.cn","size":301,
"children": [
{"name":"FR","size":2},
{"name":"DE","size":1},
{"name":"CN","size":32},
{"name":"US","size":57}
]
},
{"name":"tmall.com","size":184,
"children": [
{"name":"FR","size":2},
{"name":"CN","size":26},
{"name":"DE","size":1},
{"name":"US","size":69},
{"name":"Others","size":1},
{"name":"Others","size":1}
]
},
{"name":"yahoo.com","size":137,
"children": [
{"name":"Others","size":1},
{"name":"CN","size":39},
{"name":"Others","size":2},
{"name":"CA","size":5},
{"name":"DE","size":1},
{"name":"FR","size":9},
{"name":"US","size":132},
{"name":"HK","size":1},
{"name":"Others","size":1},
{"name":"Others","size":1},
{"name":"GB","size":1}
]
},
{"name":"facebook.com","size":104,
"children": [
{"name":"RU","size":1},
{"name":"FR","size":8},
{"name":"CN","size":23},
{"name":"CA","size":2},
{"name":"DE","size":2},
{"name":"JP","size":1},
{"name":"US","size":138}
]
}
]
}
<!DOCTYPE html>
<meta charset="utf-8">
<style>
circle,
path {
cursor: pointer;
}
circle {
fill: none;
pointer-events: all;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var colors = {
"haosou.com":"#CD2626",
"xiaomi.com":"#CD2626",
"bing.com":"#5687d1",
"weibo.com":"#CD2626",
"uber.com":"#5687d1",
"xunlei.com":"#CD2626",
"instagram.com":"#5687d1",
"baidu.com":"#CD2626",
"sohu.com":"#CD2626",
"youku.com":"#CD2626",
"taobao.com":"#CD2626",
"meituan.com":"#CD2626",
"qq.com":"#CD2626",
"163.com":"#CD2626",
"twitter.com":"#5687d1",
"apple.com":"#5687d1",
"sina.com.cn":"#CD2626",
"toutiao.com":"#CD2626",
"google.com":"#5687d1",
"hao123.com":"#CD2626",
"wandoujia.com":"#CD2626",
"amazon.com":"#5687d1",
"renren.com":"#CD2626",
"alipay.com":"#CD2626",
"10086.cn":"#CD2626",
"tmall.com":"#CD2626",
"yahoo.com":"#5687d1",
"facebook.com":"#5687d1",
"RU":"#6E8B3D",
"FR":"#bbbbbb",
"CN":"#CD2626",
"CA":"#FFFF00",
"DE":"#de783b",
"JP":"#7b615c",
"US":"#5687d1",
"HK":"#B23AEE",
"GB":"#E0FFFF",
"Others":"#030303"
};
var margin = {top: 350, right: 480, bottom: 350, left: 480},
radius = Math.min(margin.top, margin.right, margin.bottom, margin.left) - 10;
var hue = d3.scale.category10();
var luminance = d3.scale.sqrt()
.domain([0, 1e6])
.clamp(true)
.range([90, 20]);
var svg_ = d3.select("body").append("svg")
.attr("width", margin.left + margin.right)
.attr("height", margin.top + margin.bottom);
var svg =svg_
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var partition = d3.layout.partition()
.sort(function(a, b) { return d3.ascending(a.name, b.name); })
.size([2 * Math.PI, radius]);
var arc = d3.svg.arc()
.startAngle(function(d) { return d.x; })
.endAngle(function(d) { return d.x + d.dx ; })
.padAngle(.01)
.padRadius(radius / 3)
.innerRadius(function(d) { return radius / 3 * d.depth; })
.outerRadius(function(d) { return radius / 3 * (d.depth + 1) - 1; });
d3.json("flare-top28.json", function(error, root) {
if (error) throw error;
//drawLegend();
//addLegend();
// Compute the initial layout on the entire tree to sum sizes.
// Also compute the full name and fill color for each node,
// and stash the children so they can be restored as we descend.
partition
.value(function(d) { return d.size; })
.nodes(root)
.forEach(function(d) {
d._children = d.children;
d.sum = d.value;
d.key = key(d);
d.fill = fill(d);
});
// Now redefine the value function to use the previously-computed sum.
partition
.children(function(d, depth) { return depth < 2 ? d._children : null; })
.value(function(d) { return d.sum; });
var center = svg.append("circle")
.attr("r", radius / 3)
.on("click", zoomOut);
center.append("title")
.text("zoom out");
var path = svg.selectAll("path")
.data(partition.nodes(root).slice(1))
//.text("test!!!!!!")
//.append("test!!!!!!")
.enter().append("path")
.attr("d", arc)
//.style("fill", function(d) { return d.fill; })
.style("fill", function(d) { return colors[d.name]; })
.each(function(d) { this._current = updateArc(d); })
.on("click", zoomIn);
var text = svg.selectAll("text")
.data(partition.nodes(root).slice(1))
.enter()
.append("text")
/*.attr({
"x": function (d) {
return arc.centroid(d)[0]
},
"y": function (d) {
return arc.centroid(d)[1]
}
})*/
//.attr("x", function(d) { return arc.centroid(d)[0] })
//.attr("y", function(d) { return arc.centroid(d)[1] })
//.attr("transform", "rotate(30)")
.attr("transform", function(d) { return "rotate(" + (d.x + d.dx / 2 - Math.PI / 2) / Math.PI * 180 + ")"; })
.attr("x", function(d) { return Math.sqrt(d.y); })
.attr("dx", "120") // margin
.attr("dy", ".35em") // vertical-align
//.text(function(d) { return d.key; });
.text(function (d) {
console.log(d);
return d.depth == 1 ? d.name : "";
})
function zoomIn(p) {
if (p.depth > 1) p = p.parent;
if (!p.children) return;
zoom(p, p);
}
function zoomOut(p) {
if (!p.parent) return;
zoom(p.parent, p);
var text = svg.selectAll("text")
.data(partition.nodes(root).slice(1))
.enter()
.append("text")
/*.attr({
"transform":function(d){
return "translate("+(arc.centroid(d)[0] +margin.left +","+(arc.centroid(d)[1]+margin.right))
}
})*/
.attr("transform", function(d) { return "rotate(" + (d.x + d.dx / 2 - Math.PI / 2) / Math.PI * 180 + ")"; })
.attr("x", function(d) { return Math.sqrt(d.y); })
.attr("dx", "120") // margin
.attr("dy", ".35em") // vertical-align
.text(function(d){
return d.depth == 1 ? d.name : "";
})
}
// Zoom to the specified new root.
function zoom(root, p) {
svg.selectAll("text").data([]).exit().remove()
if (document.documentElement.__transition__) return;
// Rescale outside angles to match the new layout.
var enterArc,
exitArc,
outsideAngle = d3.scale.linear().domain([0, 2 * Math.PI]);
function insideArc(d) {
return p.key > d.key
? {depth: d.depth - 1, x: 0, dx: 0} : p.key < d.key
? {depth: d.depth - 1, x: 2 * Math.PI, dx: 0}
: {depth: 0, x: 0, dx: 2 * Math.PI};
}
function outsideArc(d) {
return {depth: d.depth + 1, x: outsideAngle(d.x), dx: outsideAngle(d.x + d.dx) - outsideAngle(d.x)};
}
center.datum(root);
// When zooming in, arcs enter from the outside and exit to the inside.
// Entering outside arcs start from the old layout.
if (root === p) enterArc = outsideArc, exitArc = insideArc, outsideAngle.range([p.x, p.x + p.dx]);
path = path.data(partition.nodes(root).slice(1), function(d) { return d.key; });
// When zooming out, arcs enter from the inside and exit to the outside.
// Exiting outside arcs transition to the new layout.
if (root !== p) enterArc = insideArc, exitArc = outsideArc, outsideAngle.range([p.x, p.x + p.dx]);
d3.transition().duration(d3.event.altKey ? 7500 : 750).each(function() {
path.exit().transition()
.style("fill-opacity", function(d) { return d.depth === 1 + (root === p) ? 1 : 0; })
.attrTween("d", function(d) { return arcTween.call(this, exitArc(d)); })
.remove();
path.enter().append("path")
.style("fill-opacity", function(d) { return d.depth === 2 - (root === p) ? 1 : 0; })
.style("fill", function(d) { return colors[d.name]; })
.on("click", zoomIn)
//.text(function(d) { return d.data.key; });
.each(function(d) { this._current = enterArc(d); });
path.transition()
.style("fill-opacity", 1)
.attrTween("d", function(d) { return arcTween.call(this, updateArc(d)); });
});
}
// ==========
// = legend_lh =
// ==========
var legend_data = [
/*"RU",
"FR",
"NL",
"CN",
"VG",
"CA",
"DE",
"JP",
"US",
"HK",
"BE",
"GB",
"MY",
"PL",
"KR"*/
"US",
"CN",
"FR",
"HK",
"DE",
"GB",
"JP",
"RU",
"CA",
"Others"
];
var legend_g = svg_.append("g").attr("transform","translate("+(margin.left + margin.right -100)+",20)");
var legend_rect = legend_g.selectAll("rect").data(legend_data);
var legend_text = legend_g.selectAll("text").data(legend_data);
legend_rect.enter().append("rect")
.attr({
"x":0,
"y":function (d,i) {
return 10 * (i + 1) + (i * 20)
},
"width":20,
"height":20,
"fill":function (d) {
return colors[d];
}
});
legend_text.enter().append("text")
.attr({
"x":30,
"y":function (d,i) {
return 10 * (i + 1) + (i * 20) + 15
}
})
.text(function (d) {
return d;
});
});
function key(d) {
var k = [], p = d;
while (p.depth) k.push(p.name), p = p.parent;
return k.reverse().join(".");
}
function fill(d) {
var p = d;
while (p.depth > 1) p = p.parent;
var c = d3.lab(hue(p.name));
c.l = luminance(d.sum);
return c;
}
function arcTween(b) {
var i = d3.interpolate(this._current, b);
this._current = i(0);
return function(t) {
return arc(i(t));
};
}
function updateArc(d) {
return {depth: d.depth, x: d.x, dx: d.dx};
}
d3.select(self.frameElement).style("height", margin.top + margin.bottom + "px");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment