Published
Edited
Apr 30, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
matches = d3.csvParse(await FileAttachment("matches.csv").text(), d3.autoType)
Insert cell
data = {
var format = d3.timeFormat("%Y-%m-%d");
let m = d3.rollup(matches,
v => d3.sum(v, d => d.gameDuration/60),
d => {
return format(d.gameCreation)
})

// Filling days without game with 0
for(const d of xRange){
console.log(format(d))
if (!(m.has(format(d)))){
m.set(format(d), 0)
}
}
let d = Array.from(m, ([k,v])=>({
date: k,
totalDuration: v
})).sort((a,b) => a.date > b.date )
return d
}
Insert cell
Insert cell
import { movingAverage } from '@d3/moving-average'
Insert cell
averages = {
let values = movingAverage(data.map(d => d.totalDuration), N)
let format = d3.timeFormat("%Y-%m-%d");
let d = xRange.map((e,i)=>({
date: format(e),
mean: values[i]
}))

// We remove the N first elements since they have no value
d = d.slice(N)
return d
}
Insert cell
Insert cell
csum = {
let values = d3.cumsum(data.map(d => d.totalDuration))

let format = d3.timeFormat("%Y-%m-%d");
let d = xRange.map((e,i)=>({
date: format(e),
csum: values[i]
}))
return d
}
Insert cell
Insert cell
variations = {
let values = d3.pairs(data.map(d => d.totalDuration), (a,b) => (a !== 0) ? (b-a)/a : null)
let format = d3.timeFormat("%Y-%m-%d");
let d = xRange.map((e,i)=>({
date: new Date(format(e)),
difference: values[i]
// difference: i > N ? values[i] - averages[i-N].mean : 0
}))
return d
}
Insert cell
Insert cell
xDomain = [new Date(d3.min(matches, d => d.gameCreation)), new Date(d3.max(matches, d => d.gameCreation))]
Insert cell
xTime = d3
.scaleTime()
.domain(xDomain)
.rangeRound([margin.left, width - margin.right])
Insert cell
xRange = d3.timeDay.range(...xTime.domain())
Insert cell
xBand = d3.scaleBand()
.domain(xDomain)
Insert cell
barWidth = xBand.bandwidth()
Insert cell
barXPosition = d => xTime(new Date(d.date)) - barWidth/2
Insert cell
lineXPosition = d => xTime(new Date(d.date))
Insert cell
Insert cell
import { legend } from '@d3/color-legend'
Insert cell
width
Insert cell
height = 500
Insert cell
margin = ({top: 20, right: 20, bottom: 30, left: 40})
Insert cell
color = "steelblue"
Insert cell
d3 = require("d3@6")
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