Public
Edited
Aug 12, 2020
1 fork
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
margin = ({ top: 30, right: 20, bottom: 50, left: 300 })
Insert cell
chart = {
const svg = d3.select(DOM.svg(width, height)).attr("class", "barchart");

const gx = svg.append("g").call(xAxis);

const gy = svg.append("g").call(yAxis);

const yLabels = [];
gy.selectAll(".tick")
.selectAll("text")
.each(d => yLabels.push(d));
const maxAvgLen = Math.floor(
(Math.min(...yLabels.map(d => d.length)) +
Math.max(...yLabels.map(d => d.length))) /
2
);
let avgLabel = maxAvgLen < 40 ? maxAvgLen : 40;

const bar = svg
.append("g")
.attr("fill", "#ff6464")
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", x(0))
.attr("y", d => y(d.name))
.attr("width", d => x(d.value) - x(0))
.attr("height", y.bandwidth())
.on("mouseover", function() {
return tooltip.style("opacity", 1);
})
.on("mousemove", function() {
let [x, y] = d3.mouse(this);
return tooltip.style("top", y - 10 + "px").style("left", x + "px");
})
.on("mouseout", function() {
return tooltip.style("opacity", 0);
});

const label = svg
.append("g")
.attr("fill", "black")
.attr("opacity", 0.8)
.attr("text-anchor", "start")
.style("font-size", "12px")
.style("font-family", "'Lucida Console', Monaco, monospace")
.selectAll("text")
.data(data)
.join("text")
.attr("x", x(0) + 10)
.attr("y", d => y(d.name) + y.bandwidth() / 2)
.attr("dy", "0.35em")
.text(d =>
d.mode === "pvalue" ? d3.format(".3")(d.pvalue) : d3.format(".3")(d.value)
);

const title = svg
.append("text")
.attr("class", "title")
.attr("fill", "black")
.attr("opacity", 0.8)
.style("font", "12px sans-serif")
.attr("x", margin.left + (width - margin.left) / 2)
.attr("y", height - margin.bottom + 40)
.text("−log₁₀(p‐value)");

svg.node().update = () => {
d3.selectAll(".domain").attr("opacity", 0);

const t = svg.transition().duration(750);

bar
.data(data, d => d.name)
.order()
.transition(t)
.delay((d, i) => i * 20)
.attr("y", d => y(d.name))
.attr("width", d => x(d.value) - x(0));

label
.data(data, d => d.name)
.order()
.transition(t)
.delay((d, i) => i * 20)
.attr("x", x(0) + 10)
.attr("y", d => y(d.name) + y.bandwidth() / 2)
.text(d =>
d.mode === "pvalue"
? d3.format(".3")(d.pvalue)
: d3.format(".3")(d.value)
);

gx.transition(t)
.call(xAxis)
.selectAll(".tick")
.delay((d, i) => i * 20)
.selectAll("line")
.attr("opacity", 0.1);

gy.transition(t)
.call(yAxis)
.selectAll(".tick")
.delay((d, i) => i * 20)
.selectAll("text")
.attr("fill", "black")
.style("font-size", "12px")
.style("font-family", "'Lucida Console', Monaco, monospace")
.text(d => trimLabel(d, avgLabel));
};

return svg.node();
}
Insert cell
Insert cell
function trimLabel(label, avgLen) {
if (label.length > avgLen) {return `${label.slice(0, avgLen-1)}…`}
else if (label.length < avgLen) return label.padEnd(avgLen, '\xa0')
else return label
}
Insert cell
Insert cell
Insert cell
dataEnrichr = d3
.json(
"https://gist.githubusercontent.com/maxim-k/9c96d5b7fb2664180844fa2febedd9e4/raw/f9a9cf6517d8c5da3587bebada901ce86ff0d8cf/ChEA_2016.json"
)
.then(function(data) {
return data["ChEA_2016"].map(data => ({
name: data[1],
value: -Math.log10(data[2]),
pvalue: data[2],
odds: data[3],
combined: data[4],
genes: data[5],
adjusted: data[6],
mode: "pvalue",
caption: "-log₁₀(p-value)"
}));
})
Insert cell
data = dataEnrichr.slice(0, numBar).sort((a, b) => b.value - a.value)
Insert cell
x = d3.scaleLinear()
.domain([0, d3.max(data, d => d.value)])
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleBand()
.domain(data.map(d => d.name))
.range([margin.top, height - margin.bottom])
.padding(0.1)
Insert cell
xAxis = g => g
.attr("class", "axis-x")
.attr("transform", `translate(0,${margin.top})`)
.call(d3.axisBottom(x).ticks(width / 100).tickSize(height - 80))
Insert cell
yAxis = g => g
.attr("class", "axis-y")
.attr("transform", `translate(${margin.left}, 0)`)
.style("text-anchor", "end")
.call(d3.axisLeft(y).tickSize(0))
Insert cell
height = data.length * 25 + margin.top + margin.bottom
Insert cell
d3 = require("d3@5")
Insert cell
saveSvgAsPng = require('save-svg-as-png')
Insert cell
d3.version
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