Published
Edited
Sep 19, 2022
Fork of Blog Post
23 forks
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import {vl} from '@vega/vega-lite-api-v5'
Insert cell
Insert cell
data = require('vega-datasets')
Insert cell
Insert cell
Insert cell
cars = data['cars.json']()
Insert cell
Insert cell
Inputs.table(cars)
Insert cell
Insert cell
data['cars.json'].url
Insert cell
Insert cell
Insert cell
nycweather = d3.json("https://gist.githubusercontent.com/emanueles/c628da6486ccb5059c091d9a13285cff/raw/8136ef5bc833ce6266e22ab0a8487c71bdb67bc2/nyc_weather_data.json")
Insert cell
Inputs.table(nycweather)
Insert cell
Insert cell
Insert cell
df = [
{"city": "Seattle", "month": "Apr", "precip": 2.68},
{"city": "Seattle", "month": "Aug", "precip": 0.87},
{"city": "Seattle", "month": "Dec", "precip": 5.31},
{"city": "New York", "month": "Apr", "precip": 3.94},
{"city": "New York", "month": "Aug", "precip": 4.13},
{"city": "New York", "month": "Dec", "precip": 3.58},
{"city": "Chicago", "month": "Apr", "precip": 3.62},
{"city": "Chicago", "month": "Aug", "precip": 3.98},
{"city": "Chicago", "month": "Dec", "precip": 2.56},
];
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
vl.markPoint()
.data(df)
.render()
Insert cell
Insert cell
vl.markPoint()
.data(df)
.encode(vl.y().field('city').type('nominal'))
.render()
Insert cell
Insert cell
vl.markPoint()
.data(df)
.encode(vl.y().fieldN('city'))
.render()
Insert cell
Insert cell
vl.markPoint()
.data(df)
.encode(
vl.x().fieldQ('precip'),
vl.y().fieldN('city')
)
.render()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Experimente aqui
Insert cell
Insert cell
Insert cell
vl.markPoint()
.data(df)
.encode(
vl.x().average('precip'),
vl.y().fieldN('city')
)
.render()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
vl.markBar()
.data(df)
.encode(
vl.x().average('precip'),
vl.y().fieldN('city')
)
.render()
Insert cell
Insert cell
vl.markBar()
.data(df)
.encode(
vl.y().average('precip'),
vl.x().fieldN('city')
)
.render()
Insert cell
Insert cell
Insert cell
vl.markPoint({color: 'firebrick'})
.data(df)
.encode(
vl.x().fieldQ('precip').scale({type: 'log'}).title('Log-Scaled Precipitation'),
vl.y().fieldN('city').title('City')
)
.render()
Insert cell
Insert cell
Insert cell
vl.markLine()
.data(cars)
.encode(
vl.x().fieldT('Year'),
vl.y().average('Miles_per_Gallon')
)
.render()
Insert cell
Insert cell
{
const line = vl.markLine().data(cars).encode(
vl.x().fieldT('Year'),
vl.y().average('Miles_per_Gallon')
);

const point = vl.markCircle().data(cars).encode(
vl.x().fieldT('Year'),
vl.y().average('Miles_per_Gallon')
);
return vl.layer(line, point).render();
}
Insert cell
Insert cell
{
const mpg = vl.markLine().data(cars).encode(
vl.x().fieldT('Year'),
vl.y().average('Miles_per_Gallon')
);

return vl.layer(mpg, mpg.markCircle()).render();
}
Insert cell
Insert cell
// Experimente aqui
Insert cell
Insert cell
{
const mpg = vl.markLine().data(cars).encode(
vl.x().fieldT('Year'),
vl.y().average('Miles_per_Gallon')
);
const hp = mpg.encode(vl.y().average('Horsepower'));

return vl.hconcat(
vl.layer(mpg, mpg.markCircle()),
vl.layer(hp, hp.markCircle())
).render();
}
Insert cell
Insert cell
Insert cell
Insert cell
scatter = vl.markPoint().data(cars).encode(
vl.x().fieldQ('Horsepower'),
vl.y().fieldQ('Miles_per_Gallon'),
vl.color().fieldN('Origin'),
vl.tooltip(['Name', 'Origin']) // show the Name and Origin fields in a tooltip
).render()
Insert cell
Insert cell
Insert cell
{
const plot = vl.markCircle().encode(
vl.x().average('precip'),
vl.y().fieldN('city')
);
return html`<pre>${JSON.stringify(plot.toObject(), 0, 2)}</pre>`; // format JSON data
}
Insert cell
Insert cell
{
const plot = vl.markCircle().encode({
x: {field: 'precip', type: 'quantitative', aggregate: 'average'},
y: {field: 'city', type: 'nominal'}
});
return html`<pre>${JSON.stringify(plot.toObject(), 0, 2)}</pre>`; // format JSON data
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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