Published
Edited
Jan 27, 2022
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
scale = d3.scaleLinear()
.domain([0,1])
.range([0, 200]);
Insert cell
Insert cell
colorScale = d3.scaleSequential(d3.interpolateYlGn)
.domain([0,10]);
Insert cell
mock = Array.from(Array(10).keys())
Insert cell
{
const width = 500;
const height = 10;
const padding = 2;

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.style("display", "block");

svg.selectAll('rect')
.data(mock)
.join("rect")
.attr("x", (d, i) => i * (10 + padding))
.attr("y", 0)
.attr("width", 10)
.attr("height", 10)
.attr("fill", d => colorScale(d));
return svg.node();
}
Insert cell
Insert cell
mockdata = ['Alice', 'Bob', 'Jane', 'Doe'];
Insert cell
categorical = {
const categoricalScale = d3.scaleBand()
.domain(mockdata)
.range([200, 300])

return categoricalScale;
}
Insert cell
{
const width = 500;
const height = 10;
const padding = 2;

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.style("display", "block");

svg.selectAll('rect')
.data(mockdata)
.join("rect")
.attr("x", (d, i) => categorical(d))
.attr("y", 0)
.attr("width", 10)
.attr("height", 10)
.attr("fill", d => 'darkblue');
return svg.node();
}
Insert cell
Insert cell
Insert cell
xAxis = g => g
.call(d3.axisBottom(scale))
.selectAll("text")
.attr("y", 10)
.style("text-anchor", "center")
.style("font-size", "5px")
.style("fill", "#777")
Insert cell
{
const width = 500;
const height = 20;

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

svg.append("g").attr("transform", `translate(${10},${0})`).call(xAxis);

return svg.node();
}
Insert cell
Insert cell
Insert cell
matrixdata
Insert cell
Insert cell
Before we begin, we will define the margins for our chart. This is important to make sure that every element in our visualization will be rendered correctly.
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
teams_data = Array.from(new Set(matrixdata.map( entry => entry.team))).sort();
Insert cell
teamsBand = d3.scaleBand()
.paddingInner(0.1)
.paddingOuter(0.0)
.domain(teams_data)
.range([0, size.width]);
Insert cell
Insert cell
teamsAxis = g => g
.call(d3.axisBottom(teamsBand).tickSizeOuter(0))
.call(g => g.select(".domain").remove())
.call(g => g.selectAll(".tick")
.attr("stroke-width", 0.5)
.attr("stroke-color", "light")
)
.selectAll("text")
.attr("y", 0)
.attr("x", -9)
.attr("dy", ".35em")
.attr("transform", "rotate(270)")
.style("text-anchor", "end")
.style("font-size", "10px")
.style("fill", "#777")
Insert cell
Insert cell
labels_data = Array.from(new Set(matrixdata.map( entry => entry.key))).sort();
Insert cell
labelsBand = d3.scaleBand()
.paddingInner(0.1)
.paddingOuter(0.0)
.domain(labels_data)
.range([0, size.height - margins.top - margins.bottom])
Insert cell
yLabels = g => g
.call(d3.axisLeft(labelsBand).tickSizeOuter(0))
.call(g => g.select(".domain").remove())
.call(g => g.selectAll(".tick")
.attr("stroke-width", 0.5)
.attr("stroke-color", "light")
)
.selectAll("text")
.attr("y", 0)
.attr("x", -9)
.style("font-size", "10px")
.style("fill", "#777")
Insert cell
Insert cell
minMax ={
const values = matrixdata.map( entry => entry.value)
return d3.extent(values);
}
Insert cell
cScale = d3.scaleSequential(d3.interpolateYlGnBu)
.domain(minMax);
Insert cell
Insert cell
render_data = (group, data) => {

group
.selectAll('rect')
.data(data)
.join(
enter => enter
.append('rect')
.attr("x", d => teamsBand(d.team))
.attr("y", d => labelsBand(d.key))
.attr("width", teamsBand.bandwidth())
.attr("height", labelsBand.bandwidth())
.attr("fill", d => cScale(d.value)),
update => update.attr("fill", d => 'gray'),
exit => exit.attr("fill", d => 'green')
)
}
Insert cell
Insert cell
{
const svg = d3.select(DOM.svg(size.width, size.height));

svg.append("g").attr("transform", `translate(${margins.left}, ${size.height - 80})`).call(teamsAxis);
svg.append("g").attr("transform", `translate(${margins.left},${margins.top})`).call(yLabels);

const chartGroup = svg.append("g").attr("transform", `translate(${margins.left},${margins.top})`);
render_data(chartGroup, matrixdata);
// render_data(chartGroup, matrixdata.slice(0, 10));
return svg.node();
}
Insert cell
Insert cell
d3 = require("d3")
Insert cell
matches = {
const scsv = d3.dsvFormat(",") // Important to define the separator of your CSV file
return scsv.parse(await FileAttachment('premierleague1819.csv').text())
}
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