Published unlisted
Edited
Nov 27, 2020
Insert cell
md`# Why the second grey bar cannot show?`
Insert cell
d3 = require('d3@6');
Insert cell
mat = FileAttachment("Step1D_Organization_Forging_Stamping_Gene_Bar_Ob_LimitP.csv")
Insert cell
data = d3.csvParse(await mat.text());
Insert cell
width = 600
Insert cell
height = 130
Insert cell
margin = ({top: 20, right: 20, bottom: 20, left: 20})
Insert cell
y = d3.scaleBand()
.domain(data.map(d => d.species))
.range([margin.top, height - margin.bottom])
.padding(0.5)
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left}, ${margin.top})`)
Insert cell
x = d3.scaleLinear()
.domain([0, d3.max(data, d => +d.Pmax)]) // choose the min and max from forging
.range([0, width - margin.left - margin.right])
Insert cell
xAxis = g => g
.attr("transform", `translate(${margin.left}, ${height - margin.bottom})`)
.call(d3.axisBottom(x).tickSizeOuter(0))
.attr("font-size", 7)
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

const barsP = svg.append('g')
.attr('class', 'bars')
.selectAll("rect")
.data(data)
.attr("transform", `translate(-${width/2} -${height/2 - margin.top})`) // move to the left-top position
.join("rect")
.attr('class', 'bar')
.attr("fill", "#F0F0F0") // color, grey
.attr("x", d => x(d.Pmax))
.attr("y", d => y(d.species))
.attr("x", margin.left) // adjust the location of bars
.attr("y", height/4)
.attr("width", d => x(d.Pmax))
.attr("height", y.bandwidth())
// add x axis
svg.append("g")
.call(xAxis)
// add y axis
svg.append('g')
.call(yAxis)

// finally draw, return
return svg.node()
}
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