chart = {
const data = [3000, 2500, 2000, 3000, 2500, 1000];
const margin = {top: 50, right:50, bottom: 50, left: 50}
const width = 400;
const height = 350;
const minVal = d3.min(data, d => d);
const maxVal = d3.max(data, d => d);
const svg = d3.create("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height);
const x = d3.scaleLinear()
.domain([0, maxVal])
.range([margin.left, width - margin.left - margin.right]);
const y = d3.scaleBand()
.domain(d3.range(data.length))
.range([height - margin.top, margin.bottom])
.padding(0.1)
function stylizeAxis() {
svg.selectAll("text")
.style("fill", (d, i) => i % 2 === 0 ? "orange" : "blue" );
}
const xAxis = d3.axisBottom(x)
.ticks(4)
.tickValues([0, 1000, 3000]);
const yAxis = d3.axisLeft(y);
svg.append("g")
.attr('class', "x-axis")
.attr('transform', `translate(0, ${height - 50})`)
.call(xAxis)
.call(stylizeAxis);
svg.append("g")
.attr('class', "y-axis")
.attr('transform', `translate(${margin.left}, 0)`)
.call(yAxis)
svg
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", d => x(0))
.attr("y", (d, i) => y(i))
.attr("width", d => x(d)-x(0))
.attr("height", y.bandwidth())
.attr("fill", "red")
.attr("opacity", "0.8");
svg
.selectAll("text.label")
.data(data)
.join("text")
.attr("class", "label")
.attr("x", d => x(d) + 10)
.attr("y", (d,i) => {
return y(i) + y.bandwidth() / 2;
})
.attr("fill", "#001b42")
.text(d => d)
.attr('text-anchor', "start");
return svg.node();
}