Published
Edited
Aug 4, 2020
2 forks
Insert cell
Insert cell
chart = {
const svg = d3.select(DOM.svg(width, height));
svg.append("g")
.attr("fill", "steelblue")
.selectAll("rect").data(data).enter().append("rect")
.attr("x", d => x(d.min))
.attr("y", d => y(d.country))
.attr("height", y.bandwidth())
.attr("width", d => x(d.max) - x(d.min));

svg.append("g")
.call(xAxis);
svg.append("g")
.call(yAxis);
return svg.node();
}
Insert cell
createTooltip = function(el) {
el
.style("position", "absolute")
.style("pointer-events", "none")
.style("top", 0)
.style("opacity", 0)
.style("background", "white")
.style("border-radius", "5px")
.style("box-shadow", "0 0 10px rgba(0,0,0,.25)")
.style("padding", "10px")
.style("line-height", "1.3")
.style("font", "11px sans-serif")
}
Insert cell
getTooltipContent = function(d) {
return `<b>${d.country}</b>
<br/>
<b style="color:${d.color.darker()}">${d.region}</b>
<br/>
${formatDate(d.min)} - ${formatDate(d.max)}
`
}
Insert cell
getRect = function(d){
const el = d3.select(this);
const sx = x(d.min);
const w = x(d.max) - x(d.min);
const isLabelRight =(sx > width/2 ? sx+w < width : sx-w>0);

el.style("cursor", "pointer")

el
.append("rect")
.attr("x", sx)
.attr("height", y.bandwidth())
.attr("width", w)
.attr("fill", d.color);

el
.append("text")
.text(d.country)
.attr("x",isLabelRight ? sx-5 : sx+w+5)
.attr("y", 2.5)
.attr("fill", "black")
.style("text-anchor", isLabelRight ? "end" : "start")
.style("dominant-baseline", "hanging");
}
Insert cell
data = d3.csv("https://raw.githubusercontent.com/belamadrid/balbudgregion/master/balbudget_spellsregion.csv")
Insert cell
d3 = require("d3@5")
Insert cell
Insert cell
margin = ({top: 20, right: 20, bottom: 30, left: 150})
Insert cell
x = d3.scaleLinear()
.domain([d3.min(data, d => d.min), d3.max(data, d => d.max)]).nice()
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleBand()
.domain(data.map(d => d.country))
.range([height - margin.bottom, margin.top])
.padding(0.1)
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x)
.tickSizeOuter(0)
.tickFormat(formatDate))

Insert cell
formatDate = d=> d
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
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