Public
Edited
Apr 5, 2023
Insert cell
Insert cell
data = FileAttachment("gate-data.json").json({ typed: true })
Insert cell
residues = data.residues
Insert cell
scores = data.scores
Insert cell
dat = residues.map((d, i) => {
return { residue: d, score: scores[i] };
})
Insert cell
Table(dat, {
format: {
score: sparkbar(d3.max(dat, (d) => d.score))
}
})
Insert cell
Insert cell
Table = Inputs.table // deprecated alias
Insert cell
Plot.plot({
caption: "What are the common lengths of songs?",
x: {
label: "Residue"
},
marks: [Plot.ruleY([0]), Plot.barY(dat, { x: "residue", y: "score" })]
})
Insert cell
chart = {
const svg = d3.select(DOM.svg(width, height));

svg.append("g").call(xAxis);

svg.append("g").call(yAxis);

const serie = svg
.append("g")
.selectAll("g")
.data([{ key: "gate", dat }])
.enter()
.append("g")
.attr("transform", (d) => `translate(0,${y(d.key) + 1})`);

const tip = d3tip()
.attr("class", "d3-tip")
.html((d, x) => x.residue);

svg.call(tip);

serie
.append("g")
.selectAll("rect")
.data((d) => d.dat)
.enter()
.append("rect")
.attr("fill", (d) => z(d.score))
.attr("x", (d, i) => x(residues[i]))
.attr("y", 0)
.attr("height", y.bandwidth())
.attr("width", x.bandwidth())
.on("mouseover", tip.show)
.on("mouseout", tip.hide);

return svg.node();
}
Insert cell
margin = ({ top: 30, right: 20, bottom: 100, left: 20 })
Insert cell
x = d3
.scaleBand()
.domain(dat.map((d) => d.residue))
.range([margin.left, width - margin.right])
.padding(0.1)
Insert cell
y = d3
.scaleBand()
.domain(["gate"])
.range([margin.top, height - margin.bottom])
.padding(0.1)
Insert cell
z = d3.scaleSequential(d3.interpolateOrRd).domain([0, d3.max(scores)])
Insert cell
Insert cell
legendHeight = 20
Insert cell
legendElementWidth = Math.round(
(width - (margin.left + margin.right)) / 2 / residues.length
)
Insert cell
// xAxis = (g) =>
// g
// .attr(
// "transform",
// `translate(${x.bandwidth() / 2},${height - margin.bottom})`
// )
// .call(d3.axisBottom(x).tickSizeOuter(0))
// .call((g) => g.select(".domain").remove())
// .selectAll("text")
// .attr("y", 0)
// .attr("x", -9)
// .attr("dy", ".35em")
// .attr("transform", "rotate(270)")
// .style("text-anchor", "end")
// .style("fill", "#777")
xAxis = (g) =>
g
.attr("transform", `translate(0, ${height - margin.bottom})`)
.call(d3.axisBottom(x).tickSizeOuter(0))
.call((g) => g.select(".domain").remove())
.selectAll("text")
.attr("y", 0)
.attr("x", -9)
.attr("dy", ".35em")
.attr("transform", "rotate(270)")
.style("text-anchor", "end")
.style("fill", "#777")
Insert cell
yAxis = (g) =>
g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).tickSize(0).tickPadding(4))
.call((g) => g.select(".domain").remove())
.selectAll("text")
.style("fill", "#777")
Insert cell
d3tip = require("https://cdnjs.cloudflare.com/ajax/libs/d3-tip/0.9.1/d3-tip.js")
Insert cell
html`<link rel="stylesheet" href="//rawgithub.com/Caged/d3-tip/master/examples/example-styles.css">`
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