Each sparkline is a standard Chart.js line chart with everything turned off except the line and a subtle fill. The key settings:
new Chart(ctx, {
type: 'line',
data: {
labels: data.map((_, i) => i), // dummy labels
datasets: [{
data: data,
borderColor: color,
borderWidth: 2,
fill: true,
backgroundColor: color + '20', // 12% opacity fill
pointRadius: 0, // no dots
tension: 0.4 // smooth curve
}]
},
options: {
responsive: false, // fixed size
plugins: {
legend: { display: false },
tooltip: { enabled: false }
},
scales: {
x: { display: false },
y: { display: false }
}
}
});
The color of the trend arrow depends on the polarity of the metric. For pageviews and sessions, going up is good (green). For bounce rate, going down is good (also green). For average duration, going down is bad (red).
// polarity: 'positive' means "up is good", 'negative' means "down is good"
function trendClass(changePercent, polarity) {
if (polarity === 'negative') {
// Lower is better (bounce rate)
return changePercent < 0 ? 'good' : 'bad';
}
// Higher is better (pageviews, sessions, duration)
return changePercent > 0 ? 'good' : 'bad';
}
function trendArrow(changePercent) {
return changePercent >= 0 ? '\u25B2' : '\u25BC'; // ▲ or ▼
}