Published
Edited
Apr 15, 2021
1 fork
Insert cell
Insert cell
DATA = [
{ nom: 'Lausanne', population: 138905 },
{ nom: 'Yverdon-les-Bains', population: 30143 },
{ nom: 'Montreux', population: 26574 },
{ nom: 'Renens', population: 21036 },
{ nom: 'Nyon', population: 20533 },
{ nom: 'Vevey', population: 19827 },
]
Insert cell
Insert cell
d3 = require('d3')
Insert cell
viewof d3view = {
const WIDTH = width
const HEIGHT = width / 3
const container = DOM.svg(WIDTH, HEIGHT)
const MARGIN = 5
const MARGIN_BOTTOM = HEIGHT / 10
const GRAPH_HEIGHT = HEIGHT - MARGIN_BOTTOM
const MARGIN_LEFT = WIDTH / 20
const GRAPH_WIDTH = WIDTH - MARGIN_LEFT
const svg = d3.select(container)
const BAR_WIDTH = GRAPH_WIDTH / DATA.length
const yScale = d3.scaleLinear()
.domain([0, d3.max(DATA, d => d.population)])
.range([GRAPH_HEIGHT, 0])
const bars = svg.append('g')
.attr('transform', `translate(${MARGIN_LEFT}, 0)`)
bars.selectAll('rect')
.data(DATA)
.enter()
.append('rect')
.attr('x', (d, i) => i * BAR_WIDTH)
.attr('width', BAR_WIDTH - MARGIN)
.attr('y', d => yScale(d.population))
.attr('height', d => GRAPH_HEIGHT - yScale(d.population))
.attr('fill', 'steelblue')
const cityNames = svg.append('g')
.attr('transform', `translate(${MARGIN_LEFT}, 0)`)
cityNames.selectAll('text')
.data(DATA)
.enter()
.append('text')
.attr('x', (d, i) => i * BAR_WIDTH + BAR_WIDTH / 2)
.attr('y', HEIGHT - MARGIN_BOTTOM + 20)
.attr('text-anchor', 'middle')
.attr('font-family', 'sans-serif')
.attr('font-size', 10)
.text(d => d.nom)
const axisY = d3.axisLeft().scale(yScale)
.tickFormat(d => `${d / 1000}k`)
.ticks(5)

svg.append('g')
.attr('transform', `translate(${MARGIN_LEFT - 3})`)
.call(axisY)
return container
}
Insert cell
Insert cell
viewof vegaView = vega({
$schema: 'https://vega.github.io/schema/vega/v5.json',
width: 400,
height: 200,
padding: 5,

data: [
{
name: 'villes',
values: DATA,
}
],

scales: [
{
name: 'xscale',
type: 'band',
domain: { data: 'villes', field: 'nom' },
range: 'width',
padding: 0.05
},
{
name: 'yscale',
domain: { data: 'villes', field: 'population'},
nice: true,
range: 'height'
}
],

axes: [
{ orient: 'bottom', scale: 'xscale', labelAngle: 30, labelAlign: 'left' },
{ orient: 'left', scale: 'yscale' }
],

marks: [
{
type: 'rect',
from: { data: 'villes' },
encode: {
enter: {
x: { scale: 'xscale', field: 'nom' },
width: { scale: 'xscale', band: 1 },
y: { scale: 'yscale', field: 'population' },
y2: { scale: 'yscale', value: 0}
}
}
}
]
})
Insert cell
vega = require("vega-embed@6")
Insert cell
Insert cell
viewof vegaLiteView = vega({
$schema: 'https://vega.github.io/schema/vega-lite/v4.json',
width: 360,
data: {
values: DATA
},
mark: 'bar',
encoding: {
x: { field: 'nom', type: 'ordinal' },
y: { field: 'population', type: 'quantitative' },
}
})
Insert cell
Insert cell
bbView = DOM.element('div', { style: `width:${width}px;height:${width/3}px` })
Insert cell
billboard = require('billboard.js')
Insert cell
billboardCss = html`<link href='${resolve('billboard.js/dist/billboard.css')}' rel='stylesheet' />`
Insert cell

billboard.bb.generate({
data: {
json: {
population: DATA.map(({ population }) => population),
},
type: 'bar',
},
axis: {
x: {
type: 'category',
categories: DATA.map(({ nom }) => nom),
}
},
bindto: bbView
})
Insert cell
Insert cell
chartjsCanvas = DOM.canvas(width, width / 2)
Insert cell
chartjs = require('https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.js')
Insert cell
new chartjs(chartjsCanvas, {
type: 'bar',
data: {
labels: DATA.map(d => d.nom),
datasets: [
{
label: 'Population',
data: DATA.map(d => d.population),
backgroundColor: 'steelblue',
}
]
}
})
Insert cell
Insert cell
Insert cell
chartist = require('chartist')
Insert cell
chartistCss = html`<link rel='stylesheet' href='https://unpkg.com/chartist@0.11.0/dist/chartist.css' />`
Insert cell
new chartist.Bar(chartistDiv, {
labels: DATA.map(d => d.nom),
series: [
DATA.map(d => d.population),
],
})
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