Skip to content

Instantly share code, notes, and snippets.

@bollwyvl

bollwyvl/README.md

Last active Mar 30, 2021
Embed
What would you like to do?
A fishbone editor
  • Make a bulleted Markdown list in the top left to update the list
  • Press edit to hide the editor
  • Press save to use SVG Crowbar to save a copy
  • Click to drag nodes

This is an integration of this implementation of a Fishbone or Ishikawa diagram, which shows contributions of different levels of a hierarchy to a main concept, with a Markdown editor for making quick diagrams.

The diagram is implemented in d3.js, while rich-text editing is provided by CodeMirror, and marked handles Markdown processing.

If you have insights, or make modifications, share them on the thread on the d3-js discussion group that spurred this work!

/*
The MIT License (MIT)
Copyright (c) 2014 Nicholas Bollweg <nick.bollweg@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
;(function(d3){
"use strict";
d3.fishbone = function(){
/*
A Fishbone diagram implemented in d3.
*/
// private variables
var _margin = 50,
// the data...
_nodes,
_links;
// d3 selections and related things used in tick function
var _node,
_link,
_root,
_arrowId = function(d){ return "arrow"; },
// the children accessor
_children = function(d){ return d.children; },
// the label accessor
_label = function(d){ return d.name; },
// a custom tick accessor
_perNodeTick = function(d){},
// arbitrary "nice" values
_linkScale = d3.scale.log()
.domain([1, 5])
.range([60, 30]),
// the main workhorse of the layout engine
_force = d3.layout.force()
.gravity(0)
.size([
window.document.documentElement.clientWidth,
window.document.documentElement.clientHeight
])
.linkDistance(_linkDistance)
.chargeDistance([10])
.on("tick", _tick);
var fb1 = function($){
/*
the d3.fishbone modifier, expecting to be called against an `svg:svg`
or `svg:g` bound to the root node of a navigable tree, i.e.
d3.select("body").append("svg")
.datum({name: "foo", children: [{name: "bar"}]})
.call(d3.fishbone());
in addition to the properties created by `d3.layout.force`, this will
add some or all of the following properties, none of which are
guaranteed to be stable, useful, or sane:
- depth int
- horizontal bool
- vertical bool
- root bool
- childIdx int
- maxChildIdx bool
- totalLinks [int]
- linkCount int
*/
_links = [];
_nodes = [];
// populate the nodes and the links globals as a side effect
_build_nodes($.datum());
// set the nodes and the links of the force
_force
.nodes(_nodes)
.links(_links);
// create the links
_link = $.selectAll(".link")
.data(_links);
_link.enter().append("line");
_link
.attr({
"class": function(d){ return "link link-" + d.depth; },
"marker-end": function(d){
return d.arrow ? "url(#" + _arrowId(d) + ")" : null;
}
});
_link.exit().remove();
// establish the node selection
_node = $.selectAll(".node").data(_nodes);
// actually create nodes
_node.enter().append("g")
.attr({
"class": function(d){ return "node" + (d.root ? " root" : ""); }
})
.append("text");
_node.select("text")
.attr({
"class": function(d){ return "label-" + d.depth; },
"text-anchor": function(d){
return !d.depth ? "start" : d.horizontal ? "end" : "middle";
},
dy: function(d){
return d.horizontal ? ".35em" : d.region === 1 ? "1em" : "-.2em";
}
})
.text(_label);
_node.exit().remove();
// set up node events
_node
.call(_force.drag)
.on("mousedown", function(){ d3.event.stopPropagation(); });
// select this so we know its width in tick
_root = $.select(".root").node();
}; // fb1
function _arrow($){
// creates an svg:defs and marker with an arrow if needed...
// really just an example, as they aren't very flexible
var defs = $.selectAll("defs").data([1]);
defs.enter().append("defs");
// create the arrows
defs.selectAll("marker#" + _arrowId())
.data([1])
.enter().append("marker")
.attr({
id: _arrowId(),
viewBox: "0 -5 10 10",
refX: 10,
refY: 0,
markerWidth: 10,
markerHeight: 10,
orient: "auto"
})
.append("path")
.attr({d: "M0,-5L10,0L0,5"});
}
function _build_nodes(node){
/*
this builds up the real/fake nodes and links needed
- a node on the "spine" can be like:
- o--->
|
- o-+->
|
- o-+->
|
- a node off a "rib" or "subrib" can be like:
- o--->
\
- o--->
*/
_nodes.push(node);
var cx = 0;
var between = [node, node.connector],
nodeLinks = [{
source: node,
target: node.connector,
arrow: true,
depth: node.depth || 0
}],
prev,
childLinkCount;
if(!node.parent){
_nodes.push(prev = {tail: true});
between = [prev, node];
nodeLinks[0].source = prev;
nodeLinks[0].target = node;
node.horizontal = true;
node.vertical = false;
node.depth = 0;
node.root = true;
node.totalLinks = []
}else{
node.connector.maxChildIdx = 0;
node.connector.totalLinks = [];
}
node.linkCount = 1;
(_children(node) || []).forEach(function(child, idx){
child.parent = node;
child.depth = (node.depth || 0) + 1;
child.childIdx = idx;
child.region = node.region ? node.region : (idx & 1 ? 1 : -1);
child.horizontal = !node.horizontal;
child.vertical = !node.vertical;
if(node.root && prev && !prev.tail){
_nodes.push(child.connector = {
between: between,
childIdx: prev.childIdx
})
prev = null;
}else{
_nodes.push(prev = child.connector = {between: between, childIdx: cx++});
}
nodeLinks.push({
source: child,
target: child.connector,
depth: child.depth
});
// recurse capturing number of links created
childLinkCount = _build_nodes(child);
node.linkCount += childLinkCount;
between[1].totalLinks.push(childLinkCount);
});
between[1].maxChildIdx = cx;
Array.prototype.unshift.apply(_links, nodeLinks);
// the number of links created byt this node and its children...
// TODO: use `linkCount` and/instead of `childIdx` for spacing
return node.linkCount;
}
function _linePosition($){
$.attr({
x1: function(d){ return d.source.x; },
y1: function(d){ return d.source.y; },
x2: function(d){ return d.target.x; },
y2: function(d){ return d.target.y; }
})
}
function _nodePosition($){
// uses an SVG `transform` to position nodes
$.attr("transform", function(d){
return "translate(" + d.x + "," + d.y + ")";
})
}
function _linkDistance(d){
// make longer links for nodes with more children, or of lower depth
return (d.target.maxChildIdx + 1) * _linkScale(d.depth + 1);
}
function _tick(e){
/*
the primary layout mechanism: a fair amount of the work is done
by links, but override a lot of it here.
TODO: enable tweaks to these individual rules
*/
// this is a "little bit"
var k = 6 * e.alpha,
// cache some variables
size = _force.size(),
width = size[0],
height = size[1],
// scratch variables for lengthy expressions
a,
b;
_nodes.forEach(function(d){
// handle the middle... could probably store the root width...
if(d.root){ d.x = width - (_margin + _root.getBBox().width); }
if(d.tail){ d.x = _margin; d.y = height / 2; }
// put the first-generation items at the top and bottom
if(d.depth === 1){
d.y = d.region === -1 ? _margin : (height - _margin);
d.x -= 10 * k;
}
// vertically-oriented tend towards the top and bottom of the page
if(d.vertical){ d.y += k * d.region; }
// everything tends to the left
if(d.depth){ d.x -= k; }
// position synthetic nodes at evently-spaced intervals...
// TODO: do something based on the calculated size of each branch
// since we don't have individual links anymore
if(d.between){
a = d.between[0];
b = d.between[1];
d.x = b.x - (1 + d.childIdx) * (b.x - a.x) / (b.maxChildIdx + 1);
d.y = b.y - (1 + d.childIdx) * (b.y - a.y) / (b.maxChildIdx + 1);
}
_perNodeTick(d);
});
// actually apply all changes
_node.call(_nodePosition);
_link.call(_linePosition);
}
// the d3.fishbone() public API
// read-only
fb1.links = function(){ return _links; };
fb1.nodes = function(){ return _nodes; };
fb1.force = function(){ return _force; };
// callable
fb1.defaultArrow = _arrow;
// d3-style chainable
fb1.margin = function(_){
// how big is the whitespace around the diagram?
if(!arguments.length){ return _margin; }
_margin = _;
return my;
};
fb1.children = function(_){
// how will children be sought from each node?
if(!arguments.length){ return _children; }
_children = _;
return my;
};
fb1.label = function(_){
// how will a label be sought from each node?
if(!arguments.length){ return _label; }
_label = _;
return my;
};
fb1.perNodeTick = function(_){
// what custom rules should be done per node?
if(!arguments.length){ return _perNodeTick; }
_perNodeTick = _;
return my;
};
return fb1;
}; // d3.fishbone
}).call(this, d3);
{
"name": "Quality",
"children": [
{
"name": "Machine",
"children": [
{"name": "Mill"},
{"name": "Mixer"},
{"name": "Metal Lathe"}
]
},
{"name": "Method"},
{
"name": "Material",
"children": [
{"name": "Masonite"},
{
"name": "Marscapone",
"children": [
{"name": "Malty"},
{
"name": "Minty",
"children": [
{"name": "spearMint"},
{"name": "pepperMint"}
]
}
]
},
{"name": "Meat",
"children": [
{"name": "Mutton"}
]
}
]
},
{
"name": "Man Power",
"children": [
{"name": "Manager"},
{"name": "Master's Student"},
{"name": "Magician"},
{"name": "Miner"},
{"name": "Magister", "children": [
{"name": "Malpractice"}
]},
{
"name": "Massage Artist",
"children": [
{"name": "Masseur"},
{"name": "Masseuse"}
]
}
]
},
{
"name": "Measurement",
"children": [
{"name": "Malleability"}
]
},
{
"name": "Milieu",
"children": [
{"name": "Marine"}
]
}
]
}
;(function(d3, marked, CodeMirror){
"use strict";
d3.fishlist = function(){
var _editor,
_fb = d3.fishbone(),
_data = {},
_container,
_started,
_lexer = new marked.Lexer();
var api = function($){
_container = $;
_change();
};
api.editor = function(_){
if(!_){ return _editor; }
_editor = _;
_editor.on("change", _change);
return api;
};
function _change(){
_fb.force().stop();
var stack = [_data = {}],
tokens = _lexer.lex(_editor.getValue()),
child;
tokens.forEach(function(t){
switch(t.type){
case "list_start":
stack[0].children = [];
break;
case "list_item_start":
child = {};
stack[0].children.push(child);
stack.unshift(child);
break;
case "text":
stack[0].name = t.text;
break;
case "list_item_end":
stack.shift();
break;
}
});
_container
.datum(_data.children[0])
.call(_fb);
_fb.force().start();
};
api.force = _fb.force;
api.fishbone = function(){ return _fb; };
return api;
};
}).call(this, d3, marked, CodeMirror);
<!DOCTYPE html>
<head>
<title>d3 Fishbone diagram</title>
<meta charset="utf-8">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/codemirror/3.22.0/codemirror.min.css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/codemirror/3.22.0/theme/neat.css">
<style>
@import url("./style.css");
</style>
</head>
<body>
<div id="editor">
<a id="edit" href="#">edit</a>
<a href="javascript:javascript: (function () { var e = document.createElement('script'); if (window.location.protocol === 'https:') { e.setAttribute('src', 'https://raw.github.com/NYTimes/svg-crowbar/gh-pages/svg-crowbar.js'); } else { e.setAttribute('src', 'http://nytimes.github.com/svg-crowbar/svg-crowbar.js'); } e.setAttribute('class', 'svg-crowbar'); document.body.appendChild(e); })();">save</a>
<div id="code">
<textarea>- Quality
- Machine
- Material
- Milieu
- Manpower</textarea>
</div>
</div>
<div id="fishbone"></div>
<script src="http://cdnjs.cloudflare.com/ajax/libs/marked/0.3.1/marked.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/d3/3.4.1/d3.min.js" charset="utf-8"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/codemirror/3.22.0/codemirror.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/codemirror/3.22.0/addon/edit/continuelist.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/codemirror/3.22.0/mode/xml/xml.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/codemirror/3.22.0/mode/markdown/markdown.js"></script>
<script src="./d3.fishbone.js" charset="utf-8"></script>
<script src="./fishlist.js" charset="utf-8"></script>
<script>
var editing = 1;
var size = (function(){
return {width: this.clientWidth, height: this.clientHeight};
}).bind(window.document.documentElement);
var fishlist = d3.fishlist()
.editor(CodeMirror.fromTextArea(d3.select("#code textarea").node(), {
mode: "markdown",
lineNumbers: true,
theme: "neat",
extraKeys: {Enter: "newlineAndIndentContinueMarkdownList"}
}));
fishlist.force().size([size().width, size().height]);
var svg = d3.select("#fishbone")
.append("svg")
// firefox needs a real size
.attr(size())
// set up the default arrowhead
.call(fishlist.fishbone().defaultArrow)
.call(fishlist);
d3.select("#edit").on("click", function(){
d3.select("#code")
.transition()
.style("height", editing ? "0" : "100%");
editing = !editing;
});
// handle resizing the window
d3.select(window).on("resize", function(){
fishlist.force()
.size([size().width, size().height])
.resume();
svg.attr(size());
});
</script>
</body>
</html>
/*
Example fishbone styling... note that you can't actually change
line markers here, which is annoying
*/
html, body{ margin: 0; padding: 0; overflow: hidden;}
/* get it? gill? */
#fishbone *, a{ font-family: "Gill Sans", "Gill Sans MT"; }
.label-0{ font-size: 2em; }
.label-1{ font-size: 1.5em; fill: #111; }
.label-2{ font-size: 1em; fill: #444; }
.label-3{ font-size: .9em; fill: #888; }
.label-4{ font-size: .8em; fill: #aaa; }
.link-0{ stroke: #000; stroke-width: 2px}
.link-1{ stroke: #333; stroke-width: 1px}
.link-2, .link-3, .link-4{ stroke: #666; stroke-width: .5px; }
#editor{ position: absolute; opacity: .8; width: 300px; height: 100%; padding: 10px; overflow: hidden;}
#code{ position: absolute; height: 100%; overflow: hidden; }
.CodeMirror { border: 1px solid #eee; height: auto; border-radius: 5px; }
.CodeMirror-scroll { overflow-y: hidden; overflow-x: auto; }
@wowtqt

This comment has been minimized.

Copy link

@wowtqt wowtqt commented Aug 30, 2020

hi

Maybe is it better to make the lines thicker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment