Created
December 14, 2020 01:28
-
-
Save Jennyandhuang/3008bdfc42954ce50c9e59b7a6c52bb7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<script src="https://d3js.org/d3.v4.min.js" type="text/JavaScript"></script> | |
<style> | |
.rect { | |
fill: rgb(131, 192, 224); | |
stroke: white; | |
stroke-width: 2px; | |
} | |
.text { | |
font-family:"Arial Black", Gadget, sans-serif; | |
fill: rgb(3, 3, 3); | |
font-weight: bold; | |
font-size: 12px | |
} | |
.link { | |
fill: none; | |
stroke: #ccc; | |
stroke-width: 1px; | |
} | |
.circle{ | |
fill:rgb(248, 228, 115) | |
} | |
</style> | |
</head> | |
<body onload="treeChart()"> | |
<svg width="1200" height="1200"> | |
<g transform="translate(40, 10)"> | |
<g class="links"></g> | |
<g class="nodes"></g> | |
</g> | |
</svg> | |
</body> | |
<script> | |
function treeChart(data){ | |
var data=[ | |
{site:"Good Marketing Club", category:"Visualization",essay:"HOW INCLUSIVE ARE MAKEUP BRANDS?",link:"https://www.goodmarketing.club/visualization/how-inclusive-are-makeup-brands"}, | |
{site:"Good Marketing Club",category:"Visualization",essay:"THE 50 MOST INNOVATIVE COMPANIES OF 2020 ",link:"https://www.goodmarketing.club/visualization/the-50-most-innovative-companies-of-2020"}, | |
{site:"Good Marketing Club",category:"Visualization",essay:"VISUALIZING UNCERTAINTY FOR DECISION-MAKING — WHY AND HOW?", link:"https://www.goodmarketing.club/essays/visualizing-uncertainty-for-decision-making-why-and-how"}, | |
{site:"Good Marketing Club",category:"Analytics",essay:"BUSINESS METRICS— WHICH ONES MATTER, TO WHOM, AND WHEN?",link:"https://www.goodmarketing.club/essays/business-metrics-which-ones-matter-to-whom-and-when"}, | |
{site:"Good Marketing Club",category:"Analytics",essay:"3 TIPS ON FOSTERING A CULTURE OF EXPERIMENTATION IN YOUR COMPANY",link:"https://www.goodmarketing.club/essays/3-tips-on-fostering-a-culture-of-experimentation-in-your-company"}, | |
{site:"Good Marketing Club",category:"Marketing Examples",essay:"KNOW YOUR AUDIENCE (AND THEIR STATE OF MIND)",link:"https://www.goodmarketing.club/marketing-examples/know-your-audience-and-their-state-of-mind"}, | |
{site:"Good Marketing Club",category:"Marketing Examples",essay:"SEGMENT’S RECIPES",link:"https://www.goodmarketing.club/marketing-examples/segments-recipes"}, | |
{site:"Good Marketing Club",category:"Marketing Examples",essay:"MASS INTIMACY - FT. NIKE & ADOBE",link:"https://www.goodmarketing.club/marketing-examples/mass-intimacy-ft-nike-adobe"}, | |
{site:"Good Marketing Club",category:"Marketing Examples",essay:"GOOD EMAIL #4 — THE EFFORTLESS EXPERIENCE (FT. H&M)",link:"https://www.goodmarketing.club/marketing-examples/good-email-4the-effortless-experience-fthm"}, | |
{site:"Good Marketing Club",category:"Strategy",essay:"MAKE YOUR CUSTOMERS BUY — THE ESSENTIAL LIST OF COGNITIVE BIASES FOR MARKETERS",link:"https://www.goodmarketing.club/essays/make-your-customers-buy-the-essential-list-of-cognitive-biases-for-marketers"}, | |
{site:"Good Marketing Club",category:"Strategy",essay:"TEXT-MESSAGE MARKETING — WHY IS IT STILL NICHE?", link:"https://www.goodmarketing.club/essays/text-message-marketing-why-is-it-still-niche"}, | |
{site:"Good Marketing Club",category:"Reading",essay:"HOW TO (HAPPILY) READ MORE BOOKS",link:"https://www.goodmarketing.club/essays/how-to-happily-read-more-books"}, | |
{site:"Good Marketing Club",category:"Reading",essay:"BOOK RECOMMENDATIONS", link:"https://www.notion.so/goodmarketingclub/Read-with-us-6bcf88f98faf477ea6b9528cb0859e8e"} | |
] | |
//use d3.nest to transform data to hierarchical form | |
var nestedData = d3.nest() | |
.key(d => d.site) | |
.key(d => d.category) | |
.entries(data); | |
console.log(nestedData) | |
//pass the formated data to d3.hierarchy to specify root and children of the hierarchy | |
var treeData = d3.hierarchy(nestedData[0], d => d.values) | |
console.log(treeData) | |
//draw the tree layout 600x500 | |
var treeLayout = d3.tree().size([600, 500]) | |
treeLayout(treeData) | |
var parentsNumber = 6 | |
//Parent nodes as circles | |
treeNodes=d3.select("svg g.nodes") | |
treeNodes.selectAll("circle") | |
.data(treeData.descendants().slice(0, parentsNumber)) | |
.enter() | |
.append("circle") | |
.attr("class", "circle") | |
.attr("transform", d => `translate(${d.y},${d.x})`) | |
.attr("r", 8) | |
//Children nodes as rectangles | |
treeNodes.selectAll("rect") | |
.data(treeData.descendants().slice(parentsNumber)) | |
.enter() | |
.append("rect") | |
.attr("class", "rect") | |
.attr("transform", d => `translate(${d.y},${d.x})`) | |
.attr("width", d => ((d.data.essay + " ").length+4)*9) | |
.attr("height", 25) | |
.attr("y", -25 / 2) | |
//Links in between nodes | |
d3.select('svg g.links') | |
.selectAll("line") | |
.data(treeData.links()) | |
.enter() | |
.append("path") | |
.classed("link", true) | |
.attr("d", function (d) { | |
return "M" + d.target.y + "," + d.target.x | |
+ "C" + (d.source.y + 100) + "," + d.target.x | |
+ " " + (d.source.y + 100) + "," + d.source.x | |
+ " " + d.source.y + "," + d.source.x; | |
}) | |
//text for parents | |
treeNodes.selectAll("text.nodes") | |
.data(treeData.descendants().slice(0, parentsNumber)) | |
.enter() | |
.append("text") | |
.attr("class", "text") | |
.attr("transform", d => `translate(${d.y+10},${d.x+5})`) | |
.text((d => d.data.key)) | |
//text for children | |
treeNodes.selectAll("text.nodes") | |
.data(treeData.descendants().slice(parentsNumber)) | |
.enter() | |
.append("a") | |
.attr("xlink:href", d => d.data.link) | |
.attr("target", "_blank") //open in a new tab | |
.append("text") | |
.attr("class", "text") | |
.attr("transform", d => `translate(${d.y+10},${d.x+5})`) | |
.text((d => d.data.essay)) | |
.on("mouseover", MouseOverText) | |
.on("mouseout", MouseOutText) | |
//Functions of animations | |
function MouseOverText(d) { | |
d3.select(this) | |
.transition() | |
.duration(200) | |
.style("fill", "rgb(248, 228, 115)") | |
.style("stroke-width", "1px") | |
.style("text-decoration", "underline") | |
} | |
function MouseOutText(d) { | |
d3.select(this) | |
.transition() | |
.duration(200) | |
.style("fill", "rgb(3, 3, 3)") | |
.style("stroke-width", "1px") | |
.style("text-decoration", "none") | |
} | |
} | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment