Published
Edited
May 9, 2022
Insert cell
import {vl} from '@vega/vega-lite-api-v5'
Insert cell
import {printTable} from '@jonfroehlich/data-utilities'
Insert cell
import {printTableTypes} from '@jonfroehlich/data-utilities'
Insert cell
hurricaneFile = FileAttachment("Hurricane_1@1.csv")
Insert cell
hurricane = hurricaneFile.csv()
Insert cell
printTable(hurricane.slice(0,5))
Insert cell
hurricaneTyped = hurricaneFile.csv({typed: true})
Insert cell
printTableTypes(hurricaneTyped)
Insert cell
{
const histogram = vl.markBar({tooltip: true, color: '#3C7A89'})
.data(hurricaneTyped)
.title('Значение пиковой скорости ветра для тропических циклонов')
.encode(
vl.x().fieldQ('wind').title('Cкорость ветра').bin(true),
vl.y().aggregate('count').title('Число наблюдений')
)

const meanRule = vl.markRule({color: '#E45756CC', size: 2})
.data(hurricaneTyped)
.encode(
vl.x().fieldQ('wind').aggregate('mean'),
vl.tooltip([{
field : "wind",
aggregate: 'mean',
title : "Среднее значение",
format: '0.1f'
}]
)
);

const meanLabel = vl.markText({ color: '#E45756CC', align: 'left', dx:3, y: 10 })
.data(hurricaneTyped)
.encode(
vl.x().mean('wind'),
vl.text().mean('wind').format('0.1f'),
);

return vl.layer(histogram, meanRule, meanLabel).render();

}
Insert cell
vl.markBar({tooltip: true, color: '#9FA2B2'})
.data(hurricaneTyped)
.title("Количество ураганов по годам")
.encode(
vl.x().fieldT("date").timeUnit('year').title('Год'),
vl.y().aggregate('count').title('Число наблюдений'),
)
.render();
Insert cell
{
const selectInterval = vl.selectInterval().encodings("x");

const BarYear = vl.markBar({color: '#DBC2CF'})
.data(hurricaneTyped)
.params(selectInterval)
.title({text: "Количество ураганов по годам",
subtitle: "Выберите интервал"})
.encode(
vl.x().fieldT("date").timeUnit('year').title('Год'),
vl.y().aggregate('count').title('Число наблюдений'),
vl.color().value("lightgray").if(
selectInterval,
vl.color().fieldT("date"))
);

const hurricaneNames = vl.markBar()
.transform(vl.filter(selectInterval))
//.data(name)
.encode(
vl.y().fieldN("name").sort("-x"),
vl.x().count("name")
)

return vl.vconcat(BarYear, hurricaneNames)
.data(hurricaneTyped)
.render();
}
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