Skip to content

Instantly share code, notes, and snippets.

@lcyraphael
Last active August 14, 2017 20:45
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 lcyraphael/421f3f14854cb49a8fd199f9e326ecaa to your computer and use it in GitHub Desktop.
Save lcyraphael/421f3f14854cb49a8fd199f9e326ecaa to your computer and use it in GitHub Desktop.
Tag hierarchy with d3
scrolling: yes
{"name": "Taxonomy", "children": [{"name": "Agriculture, animals, food and rural affairs", "children": [{"name": "Agriculture", "children": [{"name": "Agriculture and environment"}, {"name": "Common Agricultural Policy"}, {"name": "Farmers"}, {"name": "Organic farming"}]}, {"name": "Animals", "children": [{"name": "Animal diseases"}, {"name": "Animal welfare"}]}, {"name": "Countryside"}, {"name": "Fisheries"}, {"name": "Food"}, {"name": "Forestry"}]}, {"name": "Asylum, immigration and nationality", "children": [{"name": "Asylum"}, {"name": "Immigration"}, {"name": "Nationality"}]}, {"name": "Business, industry and consumers", "children": [{"name": "Business support", "children": [{"name": "EU grants and loans"}, {"name": "Regional development agencies"}]}, {"name": "Companies"}, {"name": "Competition"}, {"name": "Consumers"}, {"name": "Industrial health and safety"}, {"name": "Industrial relations"}, {"name": "Industry", "children": [{"name": "Construction industry"}, {"name": "Manufacturing industries"}, {"name": "Service industries"}, {"name": "Tourism"}]}, {"name": "Insolvency"}, {"name": "Intellectual property"}, {"name": "International trade"}, {"name": "Postal services"}, {"name": "Regulation"}, {"name": "Small businesses"}, {"name": "Standards"}, {"name": "Utilities"}]}, {"name": "Communities and families", "children": [{"name": "Charities"}, {"name": "Children and families", "children": [{"name": "Child care"}, {"name": "Child support"}, {"name": "Children's social services"}, {"name": "Divorce"}, {"name": "Marriage"}, {"name": "Registration of births, deaths and marriages"}]}, {"name": "Ethnic groups"}, {"name": "Family law", "children": [{"name": "Civil partnerships"}, {"name": "Cohabitation"}, {"name": "Divorce"}, {"name": "Marriage"}]}, {"name": "Incomes and poverty", "children": [{"name": "Fuel poverty"}]}, {"name": "Older people"}, {"name": "Population", "children": [{"name": "Census"}]}, {"name": "Religion and faith communities"}, {"name": "Voluntary organisations"}]}, {"name": "Crime, civil law, justice and rights", "children": [{"name": "Administration of justice", "children": [{"name": "Courts"}, {"name": "Legal profession"}]}, {"name": "Alternatives to prison"}, {"name": "Anti-social behaviour"}, {"name": "Civil law", "children": [{"name": "Coroners"}, {"name": "Gender recognition"}, {"name": "Legal aid"}, {"name": "Limitation of actions"}, {"name": "Personal injury"}, {"name": "Powers of attorney"}, {"name": "Small claims"}]}, {"name": "Crime", "children": [{"name": "Crimes of violence"}, {"name": "Drugs crimes"}, {"name": "Internet and cybercrime"}, {"name": "Offensive weapons"}, {"name": "Public order offences"}, {"name": "Sexual offences"}]}, {"name": "Criminal law"}, {"name": "Death", "children": [{"name": "Burial and cremation"}, {"name": "Coroners"}, {"name": "Funerals"}, {"name": "Wills and intestacy"}]}, {"name": "Emergencies and disaster management"}, {"name": "Emergency services", "children": [{"name": "Ambulance services"}, {"name": "Fire services"}, {"name": "Police"}]}, {"name": "Equality", "children": [{"name": "Age discrimination"}, {"name": "Disability discrimination"}, {"name": "Gender recognition"}, {"name": "Racial discrimination"}, {"name": "Religious discrimination"}, {"name": "Sex discrimination"}]}, {"name": "Family law", "children": [{"name": "Civil partnerships"}, {"name": "Cohabitation"}, {"name": "Divorce"}, {"name": "Marriage"}]}, {"name": "Firearms"}, {"name": "Freedom of information"}, {"name": "Human rights"}, {"name": "Intelligence services"}, {"name": "International law", "children": [{"name": "EU law and treaties"}]}, {"name": "Land law"}, {"name": "Prisons"}, {"name": "Privacy", "children": [{"name": "Data protection"}]}, {"name": "Security industry"}, {"name": "Terrorism"}]}, {"name": "Culture, media and sport", "children": [{"name": "Arts", "children": [{"name": "Art works"}, {"name": "Museums and galleries"}, {"name": "Performing arts"}]}, {"name": "Cultural heritage"}, {"name": "Gambling"}, {"name": "Leisure"}, {"name": "Libraries"}, {"name": "Licensing"}, {"name": "Media", "children": [{"name": "Broadcasting"}, {"name": "Press"}]}, {"name": "National Lottery"}, {"name": "Sports and Olympic Games"}, {"name": "Tourism"}]}, {"name": "Defence", "children": [{"name": "Armed forces", "children": [{"name": "Armed forces welfare"}, {"name": "Veterans"}]}, {"name": "Defence equipment and procurement"}, {"name": "Defence policy", "children": [{"name": "Arms control"}, {"name": "Ballistic missile defence"}, {"name": "Defence expenditure"}, {"name": "EU defence policy"}]}, {"name": "Military operations", "children": [{"name": "Afghanistan"}, {"name": "Falkland Islands"}, {"name": "Iraq"}]}]}, {"name": "Economy and finance", "children": [{"name": "Regional planning and development"}, {"name": "Economic policy"}, {"name": "Economic situation", "children": [{"name": "World economy"}]}, {"name": "EU budget"}, {"name": "Financial institutions"}, {"name": "Financial services", "children": [{"name": "Insolvency"}, {"name": "Loans"}, {"name": "Mortgages"}, {"name": "Pensions"}]}, {"name": "Incomes and poverty", "children": [{"name": "Fuel poverty"}]}, {"name": "International development"}, {"name": "International economic relations", "children": [{"name": "Economic and monetary union"}]}, {"name": "International trade"}, {"name": "Public expenditure"}, {"name": "Taxation", "children": [{"name": "National insurance"}]}]}, {"name": "Education", "children": [{"name": "Adult education"}, {"name": "Further education"}, {"name": "Higher education"}, {"name": "Local authorities: education"}, {"name": "Ofsted"}, {"name": "Pre-school education"}, {"name": "Schools"}, {"name": "Special educational needs"}, {"name": "Students"}, {"name": "Teachers"}]}, {"name": "Employment and training", "children": [{"name": "Adult education"}, {"name": "Employment"}, {"name": "Employment schemes"}, {"name": "National insurance"}, {"name": "Pay"}, {"name": "Pensions"}, {"name": "Training"}, {"name": "Unemployment"}]}, {"name": "Energy and environment", "children": [{"name": "Climate change"}, {"name": "Energy", "children": [{"name": "Coal"}, {"name": "Electricity"}, {"name": "Energy conservation"}, {"name": "Fuel poverty"}, {"name": "Nuclear power"}, {"name": "Oil, petrol and natural gas"}, {"name": "Renewable energy"}]}, {"name": "Environmental protection", "children": [{"name": "Biodiversity"}, {"name": "Countryside"}, {"name": "Hedges and ditches"}, {"name": "Marine environment"}, {"name": "Nature conservation"}, {"name": "Wildlife"}]}, {"name": "Pollution"}, {"name": "Utilities"}, {"name": "Waste management"}, {"name": "Water", "children": [{"name": "Flooding"}, {"name": "Water industry"}]}]}, {"name": "European Union", "children": [{"name": "Common Agricultural Policy"}, {"name": "Economic and monetary union"}, {"name": "EU budget"}, {"name": "EU defence policy"}, {"name": "EU enlargement"}, {"name": "EU external relations"}, {"name": "EU grants and loans"}, {"name": "EU institutions", "children": [{"name": "EU Council of Ministers"}, {"name": "European Commission"}, {"name": "European Council"}, {"name": "European Parliament"}]}, {"name": "EU law and treaties"}, {"name": "EU political integration"}]}, {"name": "Health services and medicine", "children": [{"name": "Diseases"}, {"name": "Health education and preventive medicine", "children": [{"name": "Alcoholism"}, {"name": "Communicable diseases"}, {"name": "Drugs misuse"}, {"name": "Obesity"}, {"name": "Smoking"}, {"name": "Vaccination"}]}, {"name": "Health finance"}, {"name": "Health services", "children": [{"name": "Mental health"}]}, {"name": "Health staff and professions"}, {"name": "Medical ethics"}, {"name": "Medicine", "children": [{"name": "Animal experiments"}, {"name": "Genetics"}]}, {"name": "Patient rights and complaints"}]}, {"name": "Housing and planning", "children": [{"name": "Housing", "children": [{"name": "Empty property"}, {"name": "Homelessness"}, {"name": "Housing adaptations"}, {"name": "Housing standards"}, {"name": "Housing supply"}, {"name": "Overcrowding"}, {"name": "Owner occupation"}, {"name": "Private rented housing"}, {"name": "Social rented housing"}, {"name": "Supported housing"}]}, {"name": "Housing benefits"}, {"name": "Planning", "children": [{"name": "Regional planning and development"}, {"name": "Regeneration"}, {"name": "Sustainable development"}]}]}, {"name": "International affairs", "children": [{"name": "Africa", "children": [{"name": "Zimbabwe"}]}, {"name": "Arms control"}, {"name": "Asia", "children": [{"name": "Afghanistan"}, {"name": "Iran"}]}, {"name": "Australasia"}, {"name": "EU defence policy"}, {"name": "Europe", "children": [{"name": "Eastern Europe"}, {"name": "Russia"}, {"name": "Western Europe"}]}, {"name": "Foreign and Commonwealth Office"}, {"name": "Human rights"}, {"name": "International development"}, {"name": "International economic relations", "children": [{"name": "Economic and monetary union"}]}, {"name": "International law", "children": [{"name": "EU law and treaties"}]}, {"name": "International organisations", "children": [{"name": "NATO"}, {"name": "Organisation for Security and Cooperation in Europe"}, {"name": "United Nations"}, {"name": "Western European Union"}]}, {"name": "International politics and government", "children": [{"name": "Election results : international"}]}, {"name": "Latin America"}, {"name": "Middle East", "children": [{"name": "Iran"}, {"name": "Iraq"}]}, {"name": "North America"}, {"name": "Overseas territories", "children": [{"name": "Falkland Islands"}]}, {"name": "Terrorism"}]}, {"name": "Parliament, government and politics", "children": [{"name": "Church and state"}, {"name": "Central government", "children": [{"name": "Civil Service"}, {"name": "Ministers"}, {"name": "Non-departmental public bodies"}]}, {"name": "Constitution"}, {"name": "Crown"}, {"name": "Devolution", "children": [{"name": "National Assembly for Wales"}, {"name": "Northern Ireland Assembly"}, {"name": "Regional assemblies"}, {"name": "Scottish Parliament"}]}, {"name": "Elections", "children": [{"name": "By-elections"}, {"name": "Election expenditure"}, {"name": "Election results : international"}, {"name": "Election results : UK"}, {"name": "Electoral franchise"}, {"name": "Electoral register"}, {"name": "Electoral systems"}, {"name": "General elections"}]}, {"name": "Freedom of information"}, {"name": "Legislative process"}, {"name": "Local government"}, {"name": "Parliament", "children": [{"name": "House of Commons"}, {"name": "House of Lords"}, {"name": "Lord Speaker"}, {"name": "Members of Parliament"}, {"name": "Members of the Lords"}, {"name": "Parliamentary procedure"}, {"name": "Speaker"}]}, {"name": "Political parties"}, {"name": "Public administration"}, {"name": "Public inquiries"}, {"name": "Statistics policy"}]}, {"name": "Science and technology", "children": [{"name": "Information technology", "children": [{"name": "Broadcasting"}, {"name": "Internet and cybercrime"}, {"name": "Telecommunications"}]}, {"name": "Research and innovation"}, {"name": "Science", "children": [{"name": "Animal experiments"}, {"name": "Climate change"}, {"name": "Genetics"}]}]}, {"name": "Social Security and pensions", "children": [{"name": "Benefits administration"}, {"name": "Benefits policy"}, {"name": "Bereavement benefits"}, {"name": "Family benefits"}, {"name": "Housing benefits"}, {"name": "Pensions"}, {"name": "Sickness, disability and carers' benefits"}, {"name": "Working age benefits"}]}, {"name": "Social services", "children": [{"name": "Children's social services"}, {"name": "Community care"}]}, {"name": "Transport", "children": [{"name": "Aviation"}, {"name": "Railways"}, {"name": "Roads", "children": [{"name": "Buses"}, {"name": "Cycling"}, {"name": "Motorcycles"}]}, {"name": "Shipping"}]}]}
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="//d3js.org/d3.v4.min.js"></script>
</head>
<style> /* set the CSS */
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 2px;
}
.node text { font: 12px sans-serif; }
.node--internal text {
text-shadow: 0 1px 0 #fff, 0 -1px 0 #fff, 1px 0 0 #fff, -1px 0 0 #fff;
}
.link {
fill: none;
stroke: #ccc;
stroke-opacity: 0.4;
stroke-width: 2px;
}
</style>
<body>
<script>
var jqXHR = $.getJSON( "d3_input.json", function() {
console.log( "success" );
})
.done(function() {
var treeData = jqXHR.responseJSON;
console.log( "loaded" );
// set the dimensions and margins of the diagram
var margin = {top: 20, right: 250, bottom: 30, left: 90},
width = 1000 - margin.left - margin.right,
height = 3800 - margin.top - margin.bottom;
// declares a tree layout and assigns the size
var treemap = d3.tree()
.size([height, width]);
// assigns the data to a hierarchy using parent-child relationships
var nodes = d3.hierarchy(treeData, function(d) {
return d.children;
});
// maps the node data to the tree layout
nodes = treemap(nodes);
// append the svg object to the body of the page
// appends a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom),
g = svg.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// adds the links between the nodes
var link = g.selectAll(".link")
.data( nodes.descendants().slice(1))
.enter().append("path")
.attr("class", "link")
.attr("d", function(d) {
return "M" + d.y + "," + d.x
+ "C" + (d.y + d.parent.y) / 2 + "," + d.x
+ " " + (d.y + d.parent.y) / 2 + "," + d.parent.x
+ " " + d.parent.y + "," + d.parent.x;
});
// adds each node as a group
var node = g.selectAll(".node")
.data(nodes.descendants())
.enter().append("g")
.attr("class", function(d) {
return "node" +
(d.children ? " node--internal" : " node--leaf"); })
.attr("transform", function(d) {
return "translate(" + d.y + "," + d.x + ")"; });
// adds the circle to the node
node.append("circle")
.attr("r", 5);
// adds the text to the node
node.append("text")
.attr("dy", ".35em")
.attr("x", function(d) { return d.children ? -13 : 13; })
.style("text-anchor", function(d) {
return d.children ? "end" : "start"; })
.text(function(d) { return d.data.name; });
})
.fail(function() {
console.log( "error" );
})
.always(function() {
console.log( "complete" );
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment