Published
Edited
Feb 11, 2021
1 fork
4 stars
Insert cell
Insert cell
dot = require("@observablehq/graphviz@0.2")
Insert cell
dot`digraph {
node [shape="square", color="red"];
edge [label="edgeName", color="blue"];
a -> b;
b -> c;
d -> h [label="from d to h"];
}`
Insert cell
themes = ({
orangeSquares: 'node [color=orange; shape=square]; edge [color=red]',
orangeCircles: 'node [color=orange; shape=circle]; edge [color=red]',
graySquares: 'node [color=gray; shape=square; fontname="monospace"]; edge [color=black]',
grayCircles: 'node [color=gray; shape=circle; fontname="monospace"]; edge [color=black]',
})
Insert cell
prepareGraph = (g, s = '->') => g.map(edge => `${edge[0]} ${s} ${edge[1]}`).join(';');
Insert cell
showGraph = (g, size=3, options="") => dot`graph {size=${size}; ${options} ${prepareGraph(g, '--')}}`
Insert cell
showDigraph = (g, size=3, options="") => dot`digraph {size=${size}; ${options} ${prepareGraph(g, '->')}}`
Insert cell
graph = [['a','b'], ['a', 100], ["hello","test"]]
Insert cell
d3 = require('d3@6');
Insert cell
Insert cell
showMethods(d3, "random")
Insert cell
rndInt = (a=0, b=100) => ~~(Math.random() * (b - a) + a)
Insert cell
randomList = (n, a=0, b=100) => {
const numbers = [];
for(let i = 0; i < n; i++) { numbers.push(rndInt(a, b)) }
return numbers;
}
Insert cell
r = d3.range(10, 600)// randomList(300, 10, 600)
Insert cell
makePairs = (arr, pred) => {
const pairs = [];
for(let i = 0; i < arr.length; i++) {
for(let j = i + 1; j < arr.length; j++) {
if (pred(arr[i], arr[j])) {
pairs.push([arr[i], arr[j]]);
}
}
}
return pairs;
}
Insert cell
g2 = makePairs(r, (i, j) => j % i === 0).filter(edge => edge[0] !== edge[1] && edge[1] % (edge[0] + 1) === 2);
Insert cell
showDigraph(g2, 10, themes.orangeSquares)
Insert cell
pairsFromWords = words => {
const pairsSet = new Set();
const pairs = [];
const s = words;
for(let i = 1; i < s.length; i++) {
const twoChars = s.charAt(i-1) + s.charAt(i);
if(twoChars.includes(' ') || twoChars.includes('.') || twoChars.includes('+')) continue;
if(pairsSet.has(twoChars)) continue;
pairsSet.add(twoChars);
pairs.push([s.charAt(i-1), s.charAt(i)]);
}
return pairs;
}
Insert cell
g3 = pairsFromWords("randomBates randomBernoulli randomBeta randomBinomial randomCauchy randomExponential randomGamma randomGeometric randomInt randomIrwinHall randomLcg randomLogNormal randomLogistic randomNormal randomPareto randomPoisson randomUniform randomWeibull".toUpperCase())
Insert cell
showDigraph(g3, 10, themes.orangeSquares)
Insert cell
g4 = pairsFromWords("This leads to algorithms for generating polyominoes inductively".toUpperCase())
Insert cell
showDigraph(g4, 10, themes.orangeSquares)
Insert cell
printList1 = d3.range(2, 100).filter(isPrime).join(" ")
Insert cell
g5 = pairsFromWords(printList1)
Insert cell
Insert cell
showDigraph(g5, 10, themes.grayCircles)
Insert cell
primeList2 = d3.range(10000, 10200).filter(isPrime).join(" ")
Insert cell
g6 = pairsFromWords(primeList2)
Insert cell
showDigraph(g6, 10, themes.grayCircles)
Insert cell
showGraph(graph,2,'node [color=gray; shape=square]')
Insert cell
showDigraph(graph,2,'node [color=gray; shape=square]')
Insert cell
dot`graph {
size = 2;
a -- b -- c;
d -- a -- c;
}`
Insert cell
dot`digraph G {
node [shape=plaintext]
a -> b; b-> c; c -> a;
}`
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