barPlot = (title, subtitle, sourceLink, sourceName, data) => {
const sortedData = data.orderby(['measure', 'caption']).objects()
const height = 230
const titleY = 0
const titleX = 10
const lineWidth = width * .5
const titleOptions = {
frameAnchor: 'top-left',
dx: titleX,
textAnchor: 'start',
stroke: colors.background,
lineHeight: 1.5
}
return Plot.plot({
width: width,
height: height,
marginRight: 45,
marginTop: 25,
marginLeft: 20,
style: {
background: colors.background,
color: colors.dark
},
y: {
axis: 'right',
grid: true,
tickSize: 0,
label: null,
tickPadding: 5,
tickFormat: d => d == 0 ? '' : d,
},
x: {
axis: null,
},
marks: [
Plot.barY(sortedData, {
x: 'caption',
y: 'measure',
title: 'caption',
fill: colors.light,
sort: {
x: 'y'
}
}),
Plot.line(sortedData, {
y: 'measure',
x: 'caption',
stroke: colors.darker,
curve: 'step',
}),
Plot.text([title], {
...titleOptions,
fill: colors.darker,
fontFamily: 'Oswald',
dy: titleY,
fontSize: 15,
lineAnchor: 'top',
lineWidth: lineWidth / 15
}),
Plot.text([subtitle], {
...titleOptions,
fill: colors.dark,
dy: titleY + (Math.ceil( title.length / (lineWidth / 10)) * 15) + 10,
fontSize: 11,
lineAnchor: 'top',
lineWidth: lineWidth / 10
}),
],
tooltip: {
fill: colors.highlight,
"stroke-width": 0
}
})
}