Published
Edited
Aug 7, 2020
Insert cell
Insert cell
chart = html`
<div style="width=${markWidth}; height=${markHeight}">
<h3 style="text-align: center;">${title}</h3>
<p style="text-align: center;">${subtitle}</p>
${markRendered}
</div>
`
Insert cell
Insert cell
markWidth
Insert cell
Insert cell
Insert cell
Insert cell
embed = require("vega-embed")
Insert cell
Insert cell
vl = {
const [vega, vegalite, api, tooltip] = await Promise.all([
'vega@5.11.1',
'vega-lite@4.11.0',
'vega-lite-api@0.11.0',
'vega-tooltip@0.22.1'
].map(module => require(module)));

const options = {
config: {
// vega-lite default configuration
config: {
view: {continuousWidth: 400, continuousHeight: 300},
mark: {tooltip: null}
}
},
init: view => {
// initialize tooltip handler
view.tooltip(new tooltip.Handler().call);
// enable horizontal scrolling for large plots
if (view.container()) view.container().style['overflow-x'] = 'auto';
if (view.container()) view.container().style['height'] = markHeight;
if (view.container()) view.container().style['height'] = markWidth;
},
view: {
// view constructor options
loader: vega.loader({baseURL: 'https://cdn.jsdelivr.net/npm/vega-datasets@1/'}),
renderer: 'canvas'
}
};
return api.register(vega, vegalite, options);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data.length
Insert cell
table(data)
Insert cell
d3.max(data, d => d.pct_margin)
Insert cell
d3.min(data, d => d.pct_margin)
Insert cell
statewideYesVotes = d3.sum(data, d => d.yes_votes)
Insert cell
statewideNoVotes = d3.sum(data, d => d.no_votes)
Insert cell
statewideTotalVotes = statewideYesVotes + statewideNoVotes
Insert cell
statewidePctMargin = ((statewideYesVotes - statewideNoVotes) / statewideTotalVotes) * 100
Insert cell
Insert cell
geoURL = 'https://raw.githubusercontent.com/stlpublicradio/dailygraphics-archive/master/2018/2018-11-06-us-sen-mo-map/mocountieskc.json'
Insert cell
topoJSON = vl.topojson(geoURL)
.feature('mocountieskc')
Insert cell
Insert cell
markRendered = mark.render()
Insert cell
Insert cell
legendConfig = (
{
labelExpr: legendLabelExpression,
orient: "none",
direction: (markWidth >= 600 ? "vertical" : "horizontal"),
legendX: (markWidth >= 600 ? markWidth - 75 : markWidth - 115),
legendY: 10
}
)
Insert cell
colorEncoding = (
{
field: "pct_margin", type: "quantitative",
title: "Margin",
scale: {
domain: [-80, 0, 80],
scheme: "redblue",
},
legend: legendConfig
}
)
Insert cell
mark.render()
Insert cell
embed(mark.toSpec())
Insert cell
Insert cell
screen.height
Insert cell
markWidth
Insert cell
screen.height <= markWidth
Insert cell
markHeight = {
if (screen.height <= markWidth) {
return screen.height - 5;
} else if (markWidth >= 500) {
return 500;
} else {
return markWidth;
}
}
Insert cell
title = `Medicaid Expansion Passes`
Insert cell
subtitle = `On Tuesday, Missourians voted 'Yes' on Constitutional Amendment No. 2 by a ${d3.format(".1f")(statewidePctMargin)} percent margin.`
Insert cell
width
Insert cell
markWidth = {
if (width >= 1190) {
return Math.min(width - 518, 746)
} else if (width >= 990) {
return Math.min(width - 434, 746)
} else {
return width - 20
}
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more