Published
Edited
Jun 1, 2020
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// a_text = fetch(`https://en.wikipedia.org/w/api.php?format=json&origin=*&action=parse&prop=text&page=List_of_proverbial_phrases&section=1`).then(response => response.json())
Insert cell
Insert cell
text = FileAttachment("list-of-proverbial-phrases-a-1-Jun-2020.json").json();
Insert cell
Insert cell
text.parse.text["*"]
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
phrases = phrases_list.querySelectorAll("li")
Insert cell
Insert cell
output = Array.from(phrases, r => r.textContent.trim())
Insert cell
Insert cell
clean_phrases = Array.from(phrases, r => ( // arrow function expression with a concise body
r.textContent.trim()
.replace(/\..*$/g, "") // s/A new language is a new life. Persian proverb[2]/A new language is a new life/
.replace(/\[.\]/g, "") // s/A fool and his money are soon parted[1]/A fool and his money are soon parted/
.replace(/,$/g, "") // s/April showers bring forth May flowers,/April showers bring forth May flowers/
.replace(/[()]/g,"") // s/A stitch in time (saves nine)/A stitch in time saves nine/
.replace(/\/[^ ]*/,"") // s#A watched pot/kettle never boils#A watched pot never boils#
+ "." // add trailing full stop
))
Insert cell
data = Object.assign(clean_phrases.map(d => d.length), {x: "Characters"})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
clean_phrases.slice(0,3)
Insert cell
Insert cell
clean_phrases.length
Insert cell
Insert cell
clean_phrases[0]
Insert cell
Insert cell
Insert cell
// What is the total number of characters across the dataset?
// data.reduce((sum, d) => (sum += +d), 0)
d3.sum(data)
Insert cell
Insert cell
min = d3.min(data)
Insert cell
Insert cell
max = d3.max(data)
Insert cell
Insert cell
extent = d3.extent(data)
Insert cell
Insert cell
median = d3.median(data)
Insert cell
Insert cell
mean = d3.mean(data)
Insert cell
Insert cell
stddev = d3.deviation(data)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
x = d3.scaleLinear()
.domain([0, d3.max(data)]).nice()
.range([margin.left, width - margin.right])
Insert cell
bins = d3.histogram()
.domain(x.domain())
.thresholds(14)
(data)
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(bins, d => d.length)]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(10 ).tickSizeOuter(0))
.call(g => g.append("text")
.attr("x", width - margin.right + 12)
.attr("y", 8)
.attr("fill", "currentColor")
.attr("font-weight", "bold")
.attr("text-anchor", "start")
.text(data.x))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(height / 40))
.call(g => g.select(".domain").remove())
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", 4)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text(data.y))
Insert cell
height = 450
Insert cell
margin = ({top: 48, right: 72, bottom: 48, left: 24})
Insert cell
d3 = require("d3@5")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more