Skip to content

Instantly share code, notes, and snippets.

@Masoumeh
Created March 7, 2017 06:34
Show Gist options
  • Save Masoumeh/81efcad2961fc03da0e00fe8d4938dab to your computer and use it in GitHub Desktop.
Save Masoumeh/81efcad2961fc03da0e00fe8d4938dab to your computer and use it in GitHub Desktop.
Al-Muqaddasi Hierarchichal Geographical Data
<!DOCTYPE html>
<meta charset="utf-8">
<style>
circle,
path {
cursor: pointer;
}
circle {
fill: none;
pointer-events: all;
}
#tooltip { background-color: white;
padding: 3px 5px;
border: 1px solid black;
text-align: center;}
html {
font-family: Amiri;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 350, right: 480, bottom: 350, left: 480},
radius = Math.min(margin.top, margin.right, margin.bottom, margin.left) - 10;
function filter_min_arc_size_text(d, i) {return (d.dx*d.depth*radius/3)>14};
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)
.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 - .01 / (d.depth + .5); })
.innerRadius(function(d) { return radius / 3 * d.depth; })
.outerRadius(function(d) { return radius / 3 * (d.depth + 1) - 1; });
//Tooltip description
var tooltip = d3.select("body")
.append("div")
.attr("id", "tooltip")
.style("position", "absolute")
.style("z-index", "10")
.style("opacity", 0);
function format_number(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function format_description(d) {
var description = d.description;
return '<b>' + d.name + '</b></br>'+ d.description;
}
function computeTextRotation(d) {
var angle=(d.x +d.dx/2)*180/Math.PI - 90
return angle;
}
function mouseOverArc(d) {
d3.select(this).attr("stroke","black")
tooltip.html(format_description(d));
return tooltip.transition()
.duration(50)
.style("opacity", 0.9);
}
function mouseOutArc(){
d3.select(this).attr("stroke","")
return tooltip.style("opacity", 0);
}
function mouseMoveArc (d) {
return tooltip
.style("top", (d3.event.pageY-10)+"px")
.style("left", (d3.event.pageX+10)+"px");
}
var root_ = null;
d3.json("muq_subRegs_sunBurst.json", function(error, root) {
if (error) return console.warn(error);
// 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 partitioned_data=partition.nodes(root).slice(1)
var path = svg.selectAll("path")
.data(partitioned_data)
.enter().append("path")
.attr("d", arc)
.style("fill", function(d) { return d.fill; })
.each(function(d) { this._current = updateArc(d); })
.on("click", zoomIn)
.on("mouseover", mouseOverArc)
.on("mousemove", mouseMoveArc)
.on("mouseout", mouseOutArc);
var texts = svg.selectAll("text")
.data(partitioned_data)
.enter().append("text")
.filter(filter_min_arc_size_text)
.attr("transform", function(d) { return "rotate(" + computeTextRotation(d) + ")"; })
.attr("x", function(d) { return radius / 3 * d.depth; })
.attr("dx", "6") // margin
.attr("dy", ".35em") // vertical-align
.text(function(d,i) {return 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);
}
// Zoom to the specified new root.
function zoom(root, p) {
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]);
var new_data=partition.nodes(root).slice(1)
path = path.data(new_data, 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 d.fill; })
.on("click", zoomIn)
.on("mouseover", mouseOverArc)
.on("mousemove", mouseMoveArc)
.on("mouseout", mouseOutArc)
.each(function(d) { this._current = enterArc(d); });
path.transition()
.style("fill-opacity", 1)
.attrTween("d", function(d) { return arcTween.call(this, updateArc(d)); });
});
texts = texts.data(new_data, function(d) { return d.key; })
texts.exit()
.remove()
texts.enter()
.append("text")
texts.style("opacity", 0)
.attr("transform", function(d) { return "rotate(" + computeTextRotation(d) + ")"; })
.attr("x", function(d) { return radius / 3 * d.depth; })
.attr("dx", "6") // margin
.attr("dy", ".35em") // vertical-align
.filter(filter_min_arc_size_text)
.text(function(d,i) {return d.name})
.transition().delay(750).style("opacity", 1)
}
});
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>
{
"name": "Muqadasi",
"children": [
{
"name": "الجبال",
"children": [
{
"name": "الري",
"size": 1,
"description": "الري"
},
{
"name": "همذان",
"size": 1,
"description": "همذان"
},
{
"name": "أصفهان",
"size": 1,
"description": "أصفهان"
},
{
"name": "قم",
"size": 1,
"description": "قم"
},
{
"name": "قاشان",
"size": 1,
"description": "قاشان"
},
{
"name": "الصيمرة",
"size": 1,
"description": "الصيمرة"
},
{
"name": "كرج",
"size": 1,
"description": "كرج"
},
{
"name": "ماه الكوفة",
"size": 1,
"description": "ماه الكوفة"
},
{
"name": "ماه البصرة",
"size": 1,
"description": "ماه البصرة"
},
{
"name": "شهرزور",
"size": 1,
"description": "شهرزور"
}
],
"description": "الجبال"
},
{
"name": "المشرق",
"children": [
{
"name": "هيطل",
"size": 1,
"description": "هيطل"
},
{
"name": "خراسان",
"size": 1,
"description": "خراسان"
}
],
"description": "المشرق"
},
{
"name": "الشام",
"children": [
{
"name": "قنسرين",
"size": 1,
"description": "قنسرين"
},
{
"name": "حمص",
"size": 1,
"description": "حمص"
},
{
"name": "دمشق",
"size": 1,
"description": "دمشق"
},
{
"name": "الأردن",
"size": 1,
"description": "الأردن"
},
{
"name": "فلسطين",
"size": 1,
"description": "فلسطين"
},
{
"name": "الشراة",
"size": 1,
"description": "الشراة"
}
],
"description": "الشام"
},
{
"name": "العراق",
"children": [
{
"name": "الکوفة",
"size": 1,
"description": "الکوفة"
},
{
"name": "البصرة",
"size": 1,
"description": "البصرة"
},
{
"name": "واسط",
"size": 1,
"description": "واسط"
},
{
"name": "بغداد",
"size": 1,
"description": "بغداد"
},
{
"name": "حلوان",
"size": 1,
"description": "حلوان"
},
{
"name": "سامرا",
"size": 1,
"description": "سامرا"
}
],
"description": "العراق"
},
{
"name": "مصر",
"children": [
{
"name": "الجفار",
"size": 1,
"description": "الجفار"
},
{
"name": "الحوف",
"size": 1,
"description": "الحوف"
},
{
"name": "الريف",
"size": 1,
"description": "الريف"
},
{
"name": "إسكندرية",
"size": 1,
"description": "إسكندرية"
},
{
"name": "مقدونية",
"size": 1,
"description": "مقدونية"
},
{
"name": "الصعيد",
"size": 1,
"description": "الصعيد"
},
{
"name": "الواحات",
"size": 1,
"description": "الواحات"
}
],
"description": "مصر"
},
{
"name": "خوزستان",
"children": [
{
"name": "السوس",
"size": 1,
"description": "السوس"
},
{
"name": "جنديسابور",
"size": 1,
"description": "جنديسابور"
},
{
"name": "تستر",
"size": 1,
"description": "تستر"
},
{
"name": "عسكر مكرم",
"size": 1,
"description": "عسكر مكرم"
},
{
"name": "الأهواز",
"size": 1,
"description": "الأهواز"
},
{
"name": "رامهرمز",
"size": 1,
"description": "رامهرمز"
},
{
"name": "الدورق",
"size": 1,
"description": "الدورق"
}
],
"description": "خوزستان"
},
{
"name": "السند",
"children": [
{
"name": "مكران",
"size": 1,
"description": "مكران"
},
{
"name": "طوران",
"size": 1,
"description": "طوران"
},
{
"name": "السند",
"size": 1,
"description": "السند"
},
{
"name": "ويهند",
"size": 1,
"description": "ويهند"
},
{
"name": "قنوج",
"size": 1,
"description": "قنوج"
},
{
"name": "الملتان",
"size": 1,
"description": "الملتان"
}
],
"description": "السند"
},
{
"name": "فارس",
"children": [
{
"name": "أرجان",
"size": 1,
"description": "أرجان"
},
{
"name": "أردشيرخره",
"size": 1,
"description": "أردشيرخره"
},
{
"name": "درابجرد",
"size": 1,
"description": "درابجرد"
},
{
"name": "شيراز",
"size": 1,
"description": "شيراز"
},
{
"name": "سابور",
"size": 1,
"description": "سابور"
},
{
"name": "إصطخر",
"size": 1,
"description": "إصطخر"
},
{
"name": "الروذان",
"size": 1,
"description": "الروذان"
},
{
"name": "نيريز",
"size": 1,
"description": "نيريز"
},
{
"name": "خسو",
"size": 1,
"description": "خسو"
}
],
"description": "فارس"
},
{
"name": "جزيرة العرب",
"children": [
{
"name": "الحجاز",
"size": 1,
"description": "الحجاز"
},
{
"name": "اليمن",
"size": 1,
"description": "اليمن"
},
{
"name": "عمان",
"size": 1,
"description": "عمان"
},
{
"name": "هجر",
"size": 1,
"description": "هجر"
},
{
"name": "الأحقاف",
"size": 1,
"description": "الأحقاف"
},
{
"name": "الأشجار",
"size": 1,
"description": "الأشجار"
},
{
"name": "اليمامة",
"size": 1,
"description": "اليمامة"
},
{
"name": "قرح",
"size": 1,
"description": "قرح"
}
],
"description": "جزيرة العرب"
},
{
"name": "الديلم",
"children": [
{
"name": "قومس",
"size": 1,
"description": "قومس"
},
{
"name": "جرجان",
"size": 1,
"description": "جرجان"
},
{
"name": "طبرستان",
"size": 1,
"description": "طبرستان"
},
{
"name": "الديلمان",
"size": 1,
"description": "الديلمان"
},
{
"name": "الخزر",
"size": 1,
"description": "الخزر"
}
],
"description": "الديلم"
},
{
"name": "كرمان",
"children": [
{
"name": "بردسير",
"size": 1,
"description": "بردسير"
},
{
"name": "نرماسير",
"size": 1,
"description": "نرماسير"
},
{
"name": "السيرجان",
"size": 1,
"description": "السيرجان"
},
{
"name": "بم",
"size": 1,
"description": "بم"
},
{
"name": "جيرفت",
"size": 1,
"description": "جيرفت"
}
],
"description": "كرمان"
},
{
"name": "أقور",
"children": [
{
"name": "ديار ربيعة",
"size": 1,
"description": "ديار ربيعة"
},
{
"name": "ديار مضر",
"size": 1,
"description": "ديار مضر"
},
{
"name": "ديار بکر",
"size": 1,
"description": "ديار بکر"
},
{
"name": "الفراتية",
"size": 1,
"description": "الفراتية"
},
{
"name": "الخابور",
"size": 1,
"description": "الخابور"
}
],
"description": "أقور"
},
{
"name": "المغرب",
"children": [
{
"name": "برقة",
"size": 1,
"description": "برقة"
},
{
"name": "إفريقية",
"size": 1,
"description": "إفريقية"
},
{
"name": "تاهرت",
"size": 1,
"description": "تاهرت"
},
{
"name": "سجلماسة",
"size": 1,
"description": "سجلماسة"
},
{
"name": "فاس",
"size": 1,
"description": "فاس"
},
{
"name": "السوس الأقصي",
"size": 1,
"description": "السوس الأقصي"
},
{
"name": "جزيرة إصقلية",
"size": 1,
"description": "جزيرة إصقلية"
}
],
"description": "المغرب"
},
{
"name": "الرحاب",
"children": [
{
"name": "الران",
"size": 1,
"description": "الران"
},
{
"name": "أرمينية",
"size": 1,
"description": "أرمينية"
},
{
"name": "أذربيجان",
"size": 1,
"description": "أذربيجان"
}
],
"description": "الرحاب"
}
],
"description": "Muqaddasi Regions"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment