Public
Edited
Dec 2, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
class Node {
constructor(name) {
this.name = name;
this.inputs = new Set;
this.outputs = new Set;
this._visited = false;
this._indegree = 0;
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
// if node has no iinputs , it is root node.
let nodes=findAllRootNodes(edges)
return dot`digraph "d3-bar-chart" {
${edges.map(([i, o]) => `${i} -> ${o}`).join("\n")}
${nodes.map(node=>`${node.name}[color="red"]`).join("\n")}
}`
}
Insert cell
function findAllRootNodes(edges){
let nodes=getNodesFromEdges(edges)
return nodes.filter((node)=>node.inputs.size==0)
}
Insert cell
findAllRootNodes(edges)
Insert cell
Insert cell
function getAllDecendents(rootName,nodes){
const set = new Set(nodes.filter(n => n.name === rootName));
for (let o of set) o.outputs.forEach(set.add, set);
return [...set]
}
Insert cell
Insert cell
{
// example:get d3 and its decendents
let nodes=getNodesFromEdges(edges)
let decendents=getAllDecendents(selected_node,nodes)

return dot`digraph "d3-bar-chart" {
${edges.map(([i, o]) => `${i} -> ${o}`).join("\n")}
${decendents.map(node=>`${node.name}[color="red"]`).join("\n")}
}`
}
Insert cell
Insert cell
Insert cell
function getNodesList(edges){
const set = new Set;
for (const [i, o] of edges) {
set.add(i)
set.add(o)
}
return [...set]
}
Insert cell
Insert cell
Insert cell
function nodesToJson(nodes){
return [...new Set(nodes)].map(e=>{
return {name:e,value:''}
})
}
Insert cell
Insert cell
Insert cell
function edgesToJson(edges){
return [...new Set(edges)].map(([i,o])=>{
return {source:i,target:o}
})
}
Insert cell
Insert cell
Insert cell
function toJson(edges){
let nodeJson=nodesToJson(getNodesList(edges))
let edgesJson=edgesToJson(edges)
return JSON.stringify({nodes:nodeJson,links:edgesJson})
}
Insert cell
toJson(edges)
Insert cell
Insert cell
Insert cell
{
var text=toJson(edges)
var data=JSON.parse(text)
var nodes=data.nodes.map(d=>{
return {
name:d.name,
symbolSize:20,
value:'',
x:Math.random()*900,
y:Math.random()*500
}
})
var links=data.links

/* Or as follows:
var nodes=nodesToJson(getNodesList(edges)).map(d=>{
return {
name:d.name,
symbolSize:10,
value:5,
x:Math.random()*900,
y:Math.random()*500
}
})
var links=edgesToJson(edges)
*/
var option = {
tooltip: {},
series: [
{
name: 'Echarts Example',
type: 'graph',
layout: 'force',//or 'force'
animation: false,
data: nodes,
edges: links,
roam: false,
label: {
show:true,
position: 'right',//or inside,
formatter: '{b}'
},
lineStyle: {
color: 'red',
//curveness: 0.3
},
edgeSymbol: ['circle', 'arrow'],
force: {
//initLayout: 'circular',
gravity: 0,
repulsion: 100,
edgeLength: 200
},
draggable:true,
emphasis: {
focus: 'adjacency',
lineStyle: {
width: 3
}
}
}
]
};
const myChart=echarts.init(document.getElementById('main'));
myChart.setOption(option);
}
Insert cell
Insert cell
Insert cell
Insert cell
jsonstring=JSON.stringify(toTreeLikeJSON('d3'))
Insert cell
Insert cell
echarts = require("https://cdn.jsdelivr.net/npm/echarts@5.3.3/dist/echarts.min.js")
Insert cell
edges=edges1.concat(edges2).concat(edges3)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more