Published
Edited
Jan 19, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
ifsc_results_text = FileAttachment("combined_results.csv").text()
Insert cell
ifsc_results = d3.csvParse(await ifsc_results_text)
Insert cell
table(ifsc_results)
Insert cell
md`On a ${ifsc_results.length} lignes et ${ifsc_results.columns.length} colonnes.`
Insert cell
{
var parseTime = d3.timeParse("%d %B %Y")
ifsc_results.forEach((d, index) => {
// Turn any "" to null
Object.keys(d).forEach(k => {
d[k] = d[k] === "" ? null : d[k]
})
// Date splitting and d3 formatting
if (/^[0-9]{1,2} [a-zA-Z]+ [0-9]{4}$/g.test(d["Competition Date"])) {
//e.g. 24 February 2001
d["Competition Date Stop"] = d["Competition Date Start"] = parseTime(d["Competition Date"])
}
else if (/^[0-9]{1,2} +- +[0-9]{1,2} +[a-zA-Z]+ +[0-9]{4}$/g.test(d["Competition Date"])) {
//e.g. 24 - 26 February 2001
let d_d_MY = /([0-9]{1,2}) +- +([0-9]{1,2}) +([a-zA-Z]+) +([0-9]{4})/g.exec(d["Competition Date"])
d["Competition Date Start"] = parseTime([d_d_MY[1], d_d_MY[3], d_d_MY[4]].join(' '))
d["Competition Date Stop"] = parseTime([d_d_MY[2], d_d_MY[3], d_d_MY[4]].join(' '))
}
else {
throw(Error(`Unhadled date at row ${index}`))
}
// Rank : String to int
d.Rank = d.Rank === null ? null : parseInt(d.Rank, 10)
// Points : String to Float
d.Points = d.Points === null ? null : parseFloat(d.Points)
// Final Points : String to Float
d["Final Points"] = d["Final Points"] === null ? null : parseFloat(d["Final Points"])
})
// Delete original date column
delete ifsc_results["Competition Date"] // NOTE : Not working but ... normal ?
}
Insert cell
ifsc_results_autotyped = d3.csvParse(ifsc_results_text, d3.autoType)
Insert cell
table(ifsc_results_autotyped)
Insert cell
Insert cell
ifsc_results.columns
Insert cell
competitions_titles = new Set(ifsc_results.map(d => d["Competition Title"]))
Insert cell
competitors_names = new Set(ifsc_results.map(d => d["FIRST"] + " " + d["LAST"]))
Insert cell
competitor_by_country = d3.group(ifsc_results, d => d.Nation)
Insert cell
finalists_average_quali_points = d3.mean(ifsc_results.filter(d => d["Final Points"] != null), d => (d.Points))
Insert cell
nb_of_competitors_by_country = d3.rollups(ifsc_results, v => v.length, d => d.Nation).map(d => {
return {Nation: d[0], "Nb of competitors": d[1]}
})
Insert cell
non_finalists_average_quali_points = d3.mean(ifsc_results.filter(d => d["Final Points"] === null), d => (d.Points))
Insert cell
Insert cell
vl.markPoint()
.data(d3.csvParse(await ifsc_results_text))
.encode(
vl.y().fieldN("Competition Title"),
vl.x().fieldQ("Final Points")
)
.render()
Insert cell
vl.markBar()
.data(nb_of_competitors_by_country)
.encode(
vl.x().fieldN("Nation"),
vl.y().fieldQ("Nb of competitors")
)
.render()
Insert cell
Insert cell
// Code marginalement adapté de https://observablehq.com/d/4b6b426ae7d964e8
scatterplot = (dataset, var_x, var_y, var_color, w, h) => {
const width = w
const height = h
const svg = d3.create("svg").attr("width", width + 100).attr("height", height + 100)
const g = svg.append("g").attr("transform", "translate(10, 20)")
const x = d3.scaleLinear().domain([0, d3.max(dataset, d => d[var_x])]).range([0, width])
const y = d3.scaleLinear().domain([d3.max(dataset, d => d[var_y]), 0]).range([0, height])
const c = d3.scaleOrdinal(d3.schemeCategory10).domain(d3.map(dataset, d => d[var_color]))
const yAxis = g => g
.attr("transform", `translate(35, 10)`)
.call(d3.axisLeft(y))
const xAxis = g => g
.attr("transform", `translate(35, ${height + 10})`)
.call(d3.axisBottom(x))

const rect = g.selectAll("circle")
.data(dataset)
.enter()
.append("circle")
.attr("cx", d => x(d[var_x]))
.attr("cy", d => y(d[var_y]))
.attr("r", 3)
.attr("transform", `translate(35,10)`)
.style("fill", d => c(d[var_color]))
g.append("g")
.call(yAxis);
g.append("g")
.call(xAxis);
return svg.node()
}
Insert cell
scatterplot(ifsc_results, x, y, "Competition Title", 600, 600)
Insert cell
viewof x = html`
<select>
${
ifsc_results.columns.map(d => {
return "<option" + (d === "Rank"?" selected=\"selected\"":"") + ">" + d + "</option>"
})
}
</select>
`
Insert cell
viewof y = html`
<select>
${
ifsc_results.columns.map(d => {
return "<option" + (d === "Points"?" selected=\"selected\"":"") + ">" + d + "</option>"
})
}
</select>
`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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