Public
Edited
Apr 24
Insert cell
Insert cell
Insert cell
data = [
{ name: "A", value: 40 },
{ name: "B", value: 55 },
{ name: "C", value: 35 },
{ name: "D", value: 20 },
{ name: "E", value: 70 },
];
Insert cell
Insert cell
{
const width = 300;
const height = 200;
const marginTop = 20;
const marginRight = 20;
const marginBottom = 40;
const marginLeft = 40;

// x scale: category names
// We use scaleBand to map categorical names to horizontal positions.
const x = d3.scaleBand()
.domain(data.map(d => d.name))
.range([marginLeft, width - marginRight])
.padding(0.1); // We add padding to make it a bar chart, removing this will result a histogram.

// y scale: linear value
// We use scaleLinear to map numeric values to vertical positions.
const y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.value)])
.range([height - marginBottom, marginTop]);

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

// Add bars
// We use rectangles to draw bars, positioning and sizing with the x, y scales we previously defined.
svg.append("g")
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", d => x(d.name))
.attr("y", d => y(d.value))
.attr("height", d => y(0) - y(d.value))
.attr("width", x.bandwidth())
.style("fill", "steelblue");

// X axis
svg.append("g")
.attr("transform", `translate(0,${height - marginBottom})`) // Shift up for margin
.call(d3.axisBottom(x))

// Y axis
svg.append("g")
.attr("transform", `translate(${marginLeft},0)`) // Shift right for margin
.call(d3.axisLeft(y).ticks(5)); // We want around 5 ticks on y axis

return svg.node();
}
Insert cell
Insert cell
{
const width = 300;
const height = 200;
const marginTop = 20;
const marginRight = 30;
const marginBottom = 30;
const marginLeft = 60;

// Color scale
const color = d3.scaleOrdinal()
.domain(data.map(d => d.name))
.range(d3.schemeObservable10);

// TODO: x scale
// x scale: linear value
// We use scaleLinear to map numeric values to horizontal positions.
const x = d3.scaleLinear()
.domain([0, d3.max(data, d => d.value)])
.range([marginLeft, width - marginRight]);

// TODO: y scale
// y scale: category names
// We use scaleBand to map categorical names to vertical positions.
const y = d3.scaleBand()
.domain(data.map(d => d.name))
.range([marginTop, height - marginBottom])
.padding(0.1); // We add padding to make it a bar chart, removing this will result a histogram


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

// TODO: add bars
// We use rectangles to draw bars, positioning and sizing with the x, y scales we previously defined.
svg.append("g")
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", marginLeft)
.attr("y", d => y(d.name))
.attr("height", y.bandwidth())
.attr("width", d => x(d.value) - x(0))
.style("fill", d => color(d.name));

// TODO: add x axis
svg.append("g")
.attr("transform", `translate(0,${height - marginBottom})`) // Shift up for margin
.call(d3.axisBottom(x).ticks(5))

// TODO: add y axis
svg.append("g")
.attr("transform", `translate(${marginLeft},0)`) // Shift right for margin
.call(d3.axisLeft(y)); // We want around 5 ticks on y axis

return svg.node();
}

Insert cell
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