This article is about how to use versatile D3.js with Octopress.
Recently I was looking for flow chart diagram or tree structure diagram, but I did not want to produce static image diagrams using Microsoft Vizio or similar softwares.
My requirement was to come up with tree structure data representation, which I could change in future unlike static pictures and can not be changed in future.
I found D3.js to be perfect for this scenario. Here, I store data in JSON file and I use D3.js to display that data according to my needs.
Challenge was how to fuse D3.js with Octopress.
First approcah to understand the D3.js from scratch but since I was short in time so I choose hacker way.
D3.js have long list of examples/gallery which are extremly helpful and powerful.
In this blog I am trying to incorporate this example.
Step 1
New post in Octopress created as .markdown file using
rake new_post [“D3.js and Octopress”]
This markdown file will now need to have have
CSS - D3.js related stylesheet code for styling
Javascript code - D3.js code to interpret data and bind with style sheet code
JSON data file - This is going to be data file apart from .markdown file
Step 2
JSON data file need to be placed in a folder from where it can be extracted when final html rendering is done.
In below code snippet confiure following parameters to customize height and width of tree
h = 650 - m[0] - m[2] (replace 650 with 800 for expanded tree)
// Normalize for fixed-depth. nodes.forEach(function(d) { d.y = d.depth * 130; }); ===> (Use 180 instead of 130 for wider tree)
// Initialize the display to show a few nodes. root.children.forEach(toggleAll); toggle(root.children[1]); toggle(root.children[1].children[2]); toggle(root.children[5]); =====> Here is number of 1st level childs toggle(root.children[5].children[0]);
[Update] To make each node a hyperlink comment following code and instead insert below code.
Make sure JSON file have a field name url