Public
Edited
Oct 20, 2023
Fork of Untitled
Insert cell
Insert cell
Insert cell
data = {
let data = await FileAttachment("barley.csv").csv({typed: true});
const yeld = data.columns.slice(0);
const variety = data.columns.slice(1);
const year = data.columns.slice(2);
const site = data.columns.slice(3);
// data = d3.sort(data, d => -d3.sum(category, category => d[category])).slice(0, 6);
const unfilteredData = variety.flatMap((category) => data.map((d) => ({variety: d.variety, site: d.site, yeld: d.yield, year: d.year})));
return unfilteredData.filter(d => d.year == yearSelect)
}


Insert cell
Insert cell
Insert cell
chart = {
// Specify the chart’s dimensions.
const width = 1000;
const height = 600;
const marginTop = 10;
const marginRight = 10;
const marginBottom = 20;
const marginLeft = 40;

// Prepare the scales for positional and color encodings.
// Fx encodes the sites.
const fx = d3.scaleBand()
.domain(new Set(data.map(d => d.site)))
.rangeRound([marginLeft, width - marginRight])
.paddingInner(0.1);

// Both x and color encode the variety class.
const variety = new Set(data.map(d => d.variety));
// const site = new Set(data.map(d => d.site));

const x = d3.scaleBand()
.domain(variety)
.rangeRound([0, fx.bandwidth()])
.padding(0.05);

const color = d3.scaleOrdinal()
.domain(variety)
.range(d3.schemeSpectral[variety.size])
.unknown("#ccc");

// Y encodes the height of the bar.
const y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.yeld)]).nice()
.rangeRound([height - marginBottom, marginTop]);

// A function to format the value in the tooltip.
const formatValue = x => isNaN(x) ? "N/A" : x.toLocaleString("en")

// Create the SVG container.
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%; height: auto;");

// Append a group for each site, and a rect for each variety.
svg.append("g")
.selectAll()
.filter(d => d.year == yearSelect)
.data(d3.group(data, d => d.site))
.join("g")
.attr("transform", ([site]) => `translate(${fx(site)},0)`)
.selectAll()
.data(([, d]) => d)
.join("rect")
.attr("x", d => x(d.variety))
.attr("y", d => y(d.yeld))
.attr("width", x.bandwidth())
.attr("height", d => y(0) - y(d.yeld))
.attr("fill", d => color(d.variety));

// Append the horizontal axis.
svg.append("g")
.attr("transform", `translate(0,${height - marginBottom})`)
.call(d3.axisBottom(fx).tickSizeOuter(0))
.call(g => g.selectAll(".domain").remove());

// Append the vertical axis.
svg.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.call(d3.axisLeft(y).ticks(null, "s"))
.call(g => g.selectAll(".domain").remove());

// Return the chart with the color scale as a property (for the legend).
return Object.assign(svg.node(), {scales: {color}});
}
Insert cell
import {legend, Swatches} from "@d3/color-legend"

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