Published
Edited
Mar 25, 2021
15 stars
Insert cell
Insert cell
Insert cell
viewof view = vl.markBar()
.data(vl.data().name('data')) // empty data set, provide name for later reference
.encode(
vl.x().fieldO('x').axis({labelAngle: 0}),
vl.y().fieldQ('y').scale({domain: [0, 100]}) // stable y-axis across data draws
)
.render()
Insert cell
// create a button that changes the data when clicked
viewof button = {
const button = html`<button>Update Data</button>`;

button.addEventListener('click', () => {
const add = [];
for (let i = 1; i <= 10; ++i) { // generate random data
add.push({x: i, y: 5 + Math.round(90 * Math.random())});
}
update(view, 'data', add, () => true); // insert new data, remove old data
});

return button;
}
Insert cell
// insert and/or remove tuples using a Vega ChangeSet
// re-run and return (a promise for) the View
function update(view, name, insert, remove) {
const cs = vl.vega.changeset();
if (remove != null) cs.remove(remove);
if (insert != null) cs.insert(insert);
return view.change(name, cs).runAsync();
}
Insert cell
import {vl} from '@vega/vega-lite-api-v5'
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