sparkline = (selection, date) => {
const width = 74;
const height = 28;
const r = 3;
const countKey = 'hospitalCount';
const duration = 14;
const startDate = new Date(Number(date));
startDate.setDate(date.getDate() - duration + 1);
const svg = selection
.append('svg')
.datum(d => data_municOfDateRange(d.municipality, startDate, date))
.attr('viewBox', [0, 0, width, height])
.attr('width', width)
.attr('height', height)
.style('margin-bottom', `-8px`)
.each(function(d, i) {
const svg = d3.select(this);
const x = d3
.scaleTime()
.domain(d3.extent(d, d => d.date))
.range([r, width - r]);
const y = d3
.scaleLinear()
.domain([0, d3.max(d, d => d[countKey])])
.range([height - r, r]);
if (y.domain()[0] === 0 && y.domain()[1] === 0) {
y.domain([0, 1]);
}
const line = d3
.line()
.defined(d => !isNaN(d[countKey]))
.x(d => x(d.date))
.y(d => y(d[countKey]));
const baseline = d3
.line()
.defined(d => !isNaN(d[countKey]))
.x(d => x(d.date))
.y(d => height - r);
const area = d3
.area()
.curve(d3.curveLinear)
.x(d => x(d.date))
.y0(y(0))
.y1(d => y(d[countKey]));
const diff = d[d.length - 1][countKey] - d[0][countKey];
const lineColor =
diff > 0
? `hsl(0, 20%, 50%)`
: diff === 0
? `hsl(0, 0%, 50%)`
: `hsl(220, 20%, 50%)`;
svg
.append('path')
.attr("fill", "gray")
.attr("opacity", 0.25)
.attr("d", area);
svg
.append('path')
.attr("fill", "none")
.attr("stroke", "hsl(0, 0%, 50%)")
.attr("stroke-width", 1)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", baseline);
svg
.append('path')
.attr("fill", "none")
.attr("stroke", lineColor)
.attr("stroke-width", 2)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
// .attr('stroke-opacity', .75)
.attr("d", line);
svg
.selectAll('circle.outer')
.data(d => [d[0], d[d.length - 1]])
.join('circle')
.classed('outer', true)
.attr('cx', d => x(d.date))
.attr('cy', d => y(d[countKey]))
.attr('r', r)
.attr("fill", lineColor);
svg
.selectAll('circle.inner')
.data(d => [d[0], d[d.length - 1]])
.join('circle')
.classed('inner', true)
.attr('cx', d => x(d.date))
.attr('cy', d => y(d[countKey]))
.attr('r', r - 1)
.attr("fill", "#fff");
const text = svg
.append('g')
.selectAll('text')
.data(d => [d[0], d[d.length - 1]])
.join('text')
.attr('x', d => x(d.date))
.attr('y', d =>
y(d[countKey]) + 12 <= height
? y(d[countKey]) + 12
: y(d[countKey]) - 5
)
.attr('font-family', 'Roboto Condensed')
.attr('font-size', '9px')
.text(d => d[countKey])
.attr('text-anchor', (_, i) => (i === 0 ? 'start' : 'end'))
.attr("fill", "hsl(0, 0%, 0%)");
text
.clone(true)
.lower()
.attr("stroke-linejoin", "round")
.attr("stroke-opacity", 1)
.attr("stroke-width", 3)
.attr("stroke", "white");
console.log(this, d);
});
// const municipality = selection.datum().municipality;
// const data = data_municOfDateRange(municipality, startDate, date);
// console.log(selection.data());
// console.log(selection.datum());
// console.log(municipality);
// console.log(startDate, date);
// console.log(data);
// svg
// .append('circle')
// .datum(d => data_munic(d.municipality).slice(-1))
// .attr('cx', d => x(d.date))
// // .attr('cy', d => y(d.rate))
// .attr('cy', d => {
// if (d.rate <= 0) {
// return -99;
// }
// const maxRate = d3.max(data_munic(d.municipality), d => d.rate);
// y.domain(yDomain(maxRate)).nice();
// return y(d.rate);
// })
// .attr('r', r)
// // .attr('r', d => (d.rate >= 1 ? r : 0))
// .attr("fill", "hsl(0, 20%, 50%)");
}