Published
Edited
Jun 25, 2020
1 star
Insert cell
Insert cell
Insert cell
checkerboardChart = {
const svg = d3.create("svg")
.attr("title", "Unlabelled chart")
.attr("class", "chart")
.attr("viewBox", [0, 0, width, height]);
const defs = svg
.append("defs");
const checkboardSize = x.bandwidth() / width * 100;
// const checkboardSize = x.bandwidth() / 10;
// const checkboardSize = 5;
const checkboardPattern = defs.append("pattern")
.attr("id", "checkboard")
.attr("width", checkboardSize * 2)
.attr("height", checkboardSize * 2)
.attr("patternUnits","userSpaceOnUse")
.html(`<rect width="${checkboardSize}" height="${checkboardSize}" fill="${activeColor}" />
<rect x="${checkboardSize}" y="${checkboardSize}" width="${checkboardSize}" height="${checkboardSize}" fill="${activeColor}" />`)
svg.append("g")
.attr("class", "bars")
.selectAll("rect")
.data(data)
.join("rect")
.attr("role", "presentation")
.attr("class", "bar")
.attr("x", d => x(d.x))
.attr("y", d => y(d.y))
.attr("width", x.bandwidth())
.attr("height", d => y(0) - y(d.y))
.style("fill", "url(#checkboard)");

svg.append("g")
.attr("class", "chart-by")
.call(chartBy)
return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
x = d3.scaleBand()
.domain(data.map(d => d.x))
.range([margin.left, width - margin.right])
.padding(0.1);
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.y)]).nice()
.range([height - margin.bottom, margin.top]);
Insert cell
chartBy = g => g
.attr("transform", `translate(${width}, ${height})`)
.append("text")
.attr("id", "source")
.attr("x", 0)
.attr("y", 0)
.attr("dy", "-0.5em")
.attr("text-anchor", "end")
.text('@DiDoesDigital');
Insert cell
Insert cell
margin = ({top: 24, right: 0, bottom: 48, left: 0})
Insert cell
activeColor = "#534e7a"
Insert cell
Insert cell
Insert cell
d3 = require("d3@5", "d3-array@2.4")
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