Public
Edited
Sep 5, 2023
Fork of Dot tile v2
2 stars
Insert cell
Insert cell
Insert cell
function dotTileV4 (data, {
title = null,
threshold = null, // pass in threshold to display
w = 500,
h = 90,
radius = 6,
margin = {left: 300, top: 30, right: 50, bottom: 10},
xDomain = [0, 100], // null to calculate on min/max
bgdCol = '#f1f2f0',
lineCol = '#bfbabe',
circleFill = '#37bbe4',
thresholdCol = '#08327d',
circleTextFill = bgdCol,
titleCol = '#555', // main title e.g. 'Engagement'
subTitleCol = '#bfbabe', // sub-titles e.g. 2022
} = {}) {
const min = (xDomain) ? xDomain[0] : d3.min(data, d => d.value);
const max = (xDomain) ? xDomain[1] : d3.max(data, d => d.value);
const yScale = d3.scaleLinear()
.domain([0, data.length])
.range([margin.top, h]);
const xScale = d3.scaleLinear()
.domain([min, max])
.range([0, w - (margin.left + margin.right)]);
const svg = DOM.svg(w, h);
const sel = d3.select(svg);
sel.append('rect')
.attr('width', w)
.attr('height', h)
.attr('rx', 10)
.attr('fill', bgdCol);
// append the title
sel.append('text')
.attr('class', 'title')
.attr('y', margin.top + 5)
.attr('x', margin.left -50)
.attr('text-anchor', 'end')
.style('font-size', '13px')
.style('fill', titleCol)
.style('line-height', '160%')
.text(title);
sel.select('text.title')
.each(function(d, i) { wrap_text(d3.select(this), (margin.left * 0.5), 1.4) });
// group for each segment
const join = sel.selectAll('g')
.data(data)
.join('g').append('g')
.attr('transform', (d, i) => {
return `translate(${margin.left}, ${yScale(i)})`
});
// lines for each row
join.append('line')
.attr('x1', 0)
.attr('y1', 0)
.attr('x2', w - (margin.left + margin.right))
.attr('y2', 0)
.attr('stroke', lineCol)
.attr('stroke-dasharray', 2);
if (threshold) {
// threshold line vertical
join.append('line')
.attr('x1', d => xScale(threshold))
.attr('x2', d => xScale(threshold))
.attr('y1', -10)
.attr('y2', 10)
.attr('stroke', thresholdCol)
.attr('stroke-width', 2)
.attr('stroke-dasharray', 3);
// show threshold value
sel.append('text')
.attr('text-anchor', 'middle')
.style('font-size', '11px')
.attr('y', 13)
.attr('x', d => xScale(threshold) + margin.left)
.style('fill', thresholdCol)
.text(d => `${threshold}%`);
}
join.append('circle')
.attr('fill', circleFill)
.attr('r', radius)
.attr('cx', d => xScale(d.value))
.style('opacity', 0.9);
// show pct
join.append('text')
.attr('text-anchor', 'right')
.style('font-size', '11px')
.attr('dy', 3)
.attr('x', w - (margin.left + margin.right))
.attr('dx', 8)
.text((d, i) => (i === 0) ? `${d.value}%` : d.value); // show % on first value only
// show date/cat
join.append('text')
.attr('text-anchor', 'right')
.style('font-size', '11px')
.attr('dy', 3)
.attr('x', -35)
.style('fill', subTitleCol)
.text(d => d.label);
return svg;
}
Insert cell
Insert cell
data = { return {
label: 'Q1: Sample survey question which needs to be compared to previous years and threshold',
threshold: 60,
data: [
{
label: '2022',
value: 62.6,
count: 4311
},
{
label: '2021',
value: 14.6,
},
{
label: '2020',
value: 66.4
}
]
}
};
Insert cell
Insert cell
measure_width = {
const context = document.createElement("canvas").getContext("2d");
return text => context.measureText(text).width;
}
Insert cell
<hr>
<link href="https://fonts.googleapis.com/css?family=Space+Mono" rel="stylesheet">
<style>
text {
font-family:'Space Mono',monospace;
fill: #130C0E;
}
</style>
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