Published
Edited
Apr 7, 2021
Importers
Insert cell
Insert cell
Insert cell
Schart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("id", "chart")
svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);

svg.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "red")
.attr("stroke-width", 2)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", line);

svg.append("circle")
.attr("r",20)
.attr("cx",width - 50)
.attr("cy",50)
.attr("fill","lightgreen")
.style("cursor", "pointer")
.attr("id", "playButton")
.on("click", makeNoise)
svg.append("text")
.attr("x",width - 50)
.attr("y",52)
.style("font-size","12px")
.style("font-family","sans-serif")
.style("pointer-events","none")
.attr("text-anchor","middle")
.text("play")
svg.append("circle")
.attr("r",5)
.attr("stroke", "red")
.attr("cx",x(data[0].notification_date))
.attr("cy",y(data[0].mean_7day))
.attr("fill","none")
.attr("id", "playHead")
return svg.node();
}
Insert cell
margin = ({top: 20, right: 30, bottom: 30, left: 40})
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
Insert cell
line = d3.line()
.x(d => x(d.notification_date))
.y(d => y(d.mean_7day))
Insert cell
y = d3.scaleLinear()
.domain(d3.extent(data, d => d.mean_7day))
.range([height - margin.bottom, margin.top])
Insert cell
x = d3.scaleUtc()
.domain(d3.extent(data, d => d.notification_date))
.range([margin.left, width - margin.right])
Insert cell
data = Object.assign((d3.csvParse(await FileAttachment("nsw-cases@2.csv").text(), d3.autoType)))
Insert cell
height = 500
Insert cell
d3 = require("d3@6")
Insert cell
function makeNoise() {
var synth = new tone.Synth().toDestination();
console.log("start")
data.forEach(function(d,i) {
console.log(i,scale(d.mean_7day))
if (i == 0) {
synth.triggerAttackRelease(scale(d.mean_7day), data.length * note)
d3.select("#playHead")
.attr("cx",x(data[i].notification_date))
.attr("cy",y(data[i].mean_7day))
}
else {
tone.Transport.schedule(function(){
d3.select("#playHead").transition().duration(500)
.ease(d3.easeLinear)
.attr("cx",x(data[i].notification_date))
.attr("cy",y(data[i].mean_7day))
synth.frequency.rampTo(scale(d.mean_7day), note);
}, i*note);
}
})
tone.Transport.position = "0:0:0"
tone.Transport.start()
}
Insert cell
Insert cell
bpm = 180
Insert cell
note = 60 / bpm
Insert cell
Insert cell
low = 65.41
Insert cell
high = 1046.50
Insert cell
scale = d3.scaleLinear()
.domain(d3.extent(data, d => d.mean_7day))
.range([low,high])
Insert cell
tone = require("tone")
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