Published
Edited
Sep 14, 2020
3 forks
Insert cell
Insert cell
Insert cell
Insert cell
nycForecast = FileAttachment("canned-forecast.json").json()
Insert cell
Insert cell
Insert cell
fetch("https://api.weather.gov/gridpoints/OKX/33,37/forecast").then(response => response.json())
Insert cell
Insert cell
nycForecastPeriods = nycForecast.properties.periods
Insert cell
Insert cell
Insert cell
Insert cell
table(
nycForecastPeriods.slice(0, 7).map(
({ name, temperature, icon, detailedForecast, shortForecast }) => ({
Day: md`**${name}**`,
Temp: md`**${temperature} °F**`,
Icon: html`<img width=70 src="${icon}" />`,
Forecast: detailedForecast
})
)
)
Insert cell
Insert cell
Insert cell
vl
.markCircle() // Make a plot of circles
.data(nycForecastPeriods) // Using the NYC forecast data
.encode(
vl
.x() // For the X axis
.fieldN('name') // Use "name"
.sort(null), // But don’t sort alphabetically
vl
.y() // For the Y axis
.fieldQ('temperature'), // Use "temperature"
vl
.color() // For the color
.fieldN('isDaytime') // Use "isDaytime"
)
.render() // Draw the plot
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof coordinates = usaMapCoordinates({
value: [-122.27, 37.87], // The default location: Berkeley, CA.
width: 350,
title: "Pick a location, see the weather forecast"
})
Insert cell
Insert cell
coordinates
Insert cell
Insert cell
weatherStation = {
const [lng, lat] = coordinates;
return (await fetch(`https://api.weather.gov/points/${[lat, lng]}`)).json()
}
Insert cell
Insert cell
place = {
const {city, state} = weatherStation.properties.relativeLocation.properties;
return `${city}, ${state}`;
}
Insert cell
Insert cell
localForecast = (await fetch(weatherStation.properties.forecast)).json()
Insert cell
Insert cell
Insert cell
table(
localForecast.properties.periods.slice(0, 7).map(
({ name, temperature, icon, detailedForecast, shortForecast }) => ({
Day: md`**${name}**`,
Temp: md`**${temperature}&nbsp;°F**`,
Icon: html`<img width=70 src="${icon}" />`,
Forecast: detailedForecast
})
)
)
Insert cell
Insert cell
Insert cell
vl
.markCircle()
.data(localForecast.properties.periods)
.encode(
vl.x().fieldN('name').sort(null),
vl.y().scale(domain).fieldQ('temperature'),
vl.color().fieldN('isDaytime')
)
.render()
Insert cell
Insert cell
Insert cell
import { weatherChart } with { localForecast as forecast } from "@kerryrodden/simple-custom-weather-chart-in-d3"
Insert cell
Insert cell
weatherChart
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
domain = {
const temps = localForecast.properties.periods.map(d => d.temperature);
const high = Math.max.apply(Math, temps);
const low = Math.min.apply(Math, temps);
return {domain: [Math.min(low, 0), Math.max(high, 90)]};
}
Insert cell
function code(s) {
return html`<code style="white-space: nowrap;">${s}</code>`;
}
Insert cell
function caret() {
const c = caretImage.cloneNode(true);
c.style.verticalAlign = "middle";
return c;
}
Insert cell
caretImage = Object.assign(await FileAttachment("image@1.png").image(), {width: 18})
Insert cell
function run() {
return svg`<svg width="16" height="16" class="db bump" stroke-linejoin="round" fill="#3b5fc0"><path d="M11.7206 6.94335C12.2406 7.34365 12.2406 8.12786 11.7206 8.52816L5.60999 13.2321C4.95242 13.7383 4 13.2696 4 12.4397L4 3.03178C4 2.20194 4.95243 1.73318 5.60999 2.23937L11.7206 6.94335Z" stroke="#3b5fc0" stroke-width="1.6"></path></svg>`;
}
Insert cell
style = html`<style>

.table-2 table td,
.table-2 table th {
padding: 5px 10px;
}

</style>`
Insert cell
import {usaMapCoordinates} from "@jashkenas/inputs"
Insert cell
import {table} from "@tmcw/tables/2"
Insert cell
import {vl} from "@vega/vega-lite-api"
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