Public
Edited
Sep 20, 2023
Insert cell
Insert cell
i = 7
Insert cell
j = 2 + k
Insert cell
viewof k = Inputs.range([100, 200])
Insert cell
Insert cell
viewof var_x = Inputs.select(cats)
Insert cell
viewof var_y = Inputs.select(cats)
Insert cell
scatterplot(var_x, var_y)
Insert cell
scatterplot = (var_x, var_y) => {

const margin = ({top: 20, right: 30, bottom: 30, left: 40})
const width = 400
const height = 300
// https://observablehq.com/@d3/margin-convention
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)

const x = d3.scaleLinear()
.domain([d3.min(raw_dataset, d => d[var_x]),
d3.max(raw_dataset, d => d[var_x])])
.range([margin.left, width -margin.left-margin.right])

const y = d3.scaleLinear()
.domain([d3.min(raw_dataset, d => d[var_y]),
d3.max(raw_dataset, d => d[var_y])])
.range([margin.top, height -margin.top-margin.bottom])

const c = d3.scaleOrdinal().domain(species).range(d3.schemeCategory10)

svg.selectAll("circle").data(raw_dataset).enter().append("circle")
.attr("cx", d => x(d[var_x]))
.attr("cy", d => y(d[var_y]))
.attr("r", d => d[var_y])
.attr("fill", d => c(d["species"]))

const xAxis = g => g.append('g')
.attr('transform', `translate(0, ${height-margin.bottom-margin.top})`)
.call(d3.axisBottom(x))

const yAxis = g => g.append('g')
.attr('transform', `translate(${margin.left}, 0)`)
.call(d3.axisLeft(y))
svg
.call(xAxis)
.call(yAxis)

var legend = svg.selectAll(".legend")
.data(c.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });

legend.append("rect")
.attr("width", 10)
.attr("height", 10)
.style("fill", function(d) { return c(d); })
.attr("transform", function(d, i) {
return "translate(" + (width -10) + "," + 10 + ")";
})
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.attr("font-size", 15)
.style("text-anchor", "end")
.text(function(d) { return d; });

return svg.node()
}
Insert cell
species = [...new Set(raw_dataset.map(d => d["species"]))]
Insert cell
raw_dataset = FileAttachment("iris.csv").csv()
Insert cell
cats = raw_dataset.columns.filter(d => d != "species")
Insert cell
dataset = raw_dataset.map(d => {

d.sepal_length = parseFloat(d.sepal_length)
d.petal_length = parseFloat(d.petal_length)
d.sepal_width = parseFloat(d.sepal_width)
d.petal_width = parseFloat(d.petal_width)
return d
})
Insert cell
Insert cell
Insert cell
dataset.map(d => parseFloat(d.sepal_length))
Insert cell
Math.max(...dataset.map(d => parseFloat(d.sepal_length)))
Insert cell
new Set(dataset.map(d => d.species))
Insert cell
Insert cell
Plot.dot(dataset,
{x: "petal_length",
y: "petal_width",
r: "petal_length",
fill: "species",
}).plot()
Insert cell
Plot.rectY(dataset, Plot.binX({y: "count"}, {x: "petal_length"})).plot()
Insert cell
edt = FileAttachment("edt-matiere.xlsx").xlsx()
Insert cell
e = edt.sheet("Sheet1", {headers: true})
Insert cell
csv_raw = FileAttachment("edt-matiere.csv").csv()
Insert cell
csv = csv_raw.map((e, i) => {
const d = Object.assign({}, e)
d.Date = parseTime(e.Date)
d.week = getISOWeekNumber(d.Date)

d.hour_start = parseHour(d.Heure.split("-")[0])
d.full_time_start = parseDateHour(d.Date + " " + d.Heure.split("-")[0])
d.index = i
return d
})
Insert cell
Plot.line(csv, {x: "Date", y: "index"}).plot()
Insert cell
groupes = [...new Set(csv.map(d => d["Groupe(s)"]))]
Insert cell
weeks = [...new Set(csv.map(d => d["week"]))]
Insert cell
heures = [...new Set(csv.map(d => d["Heure"]))]
Insert cell
hour_starts = [...new Set(csv.map(d => d["hour_start"]))]
Insert cell
parseTime = d3.timeParse("%m/%d/%Y")
Insert cell
viewof f = Inputs.checkbox(groupes, {value: groupes})
Insert cell
Plot.plot({
marks:[
Plot.dot(csv.filter(d => d["Groupe(s)"].length < 2 && f.indexOf(d["Groupe(s)"]) >= 0),
{x: "Date", y: "Groupe(s)", fill: "Groupe(s)"}
),
Plot.tickX(csv.filter(d => d["Groupe(s)"].length > 2 && f.indexOf(d["Groupe(s)"]) >= 0),
{x: "Date", y: "Groupe(s)", stroke: "Groupe(s)"})
]
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
getISOWeekNumber = (date) => {
const currentDate = new Date(date);
currentDate.setHours(0, 0, 0, 0); // Set the time to midnight to avoid time zone issues

// Find the Thursday in this week (ISO week starts on Monday)
currentDate.setDate(currentDate.getDate() + 3 - (currentDate.getDay() + 6) % 7);

// Calculate the year of the Thursday
const year = currentDate.getFullYear();

// Calculate the day number of the year for the Thursday
const dayNumber = Math.floor((currentDate - new Date(year, 0, 1)) / 86400000);

// Calculate the week number
const weekNumber = 1 + Math.floor(dayNumber / 7);

return weekNumber;
}
Insert cell
Insert cell
viewof w = Inputs.range(d3.extent(weeks), {step: 1})
Insert cell
Insert cell
Insert cell
Plot.plot({
padding: 0,
y: {
tickFormat: Plot.formatMonth()
},
color: {
scheme: "BuRd",
legend: true,
domain: [0, 10]
},
marks: [
Plot.cell(csv, Plot.group({fill: "count"}, {
x: d => d.Date.getUTCDate(),
y: d => d.Date.getUTCMonth(),
fill: "count",
inset: 0.5
})),
Plot.text(csv, Plot.group({fill: "count"}, {
x: d => d.Date.getUTCDate(),
y: d => d.Date.getUTCMonth(),
text: "count"
}))
]
})
Insert cell
parseHour = d3.timeParse("%H:%M ");
Insert cell
parseDateHour = d3.timeParse("%Y-%m-%d %H:%M ");
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