Public
Edited
Apr 15, 2024
8 forks
4 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
customData = [
{ name: "John", age: 13 },
{ name: "Jane", age: 23 },
{ name: "Dave", age: 33 }
]
Insert cell
Insert cell
FileAttachment("athletes@1.csv").csv()
Insert cell
Insert cell
Insert cell
remoteCSV = d3.csv(
"https://media.githubusercontent.com/media/MuseumofModernArt/collection/main/Artists.csv"
)
Insert cell
Insert cell
remoteJSON = d3.json(
"https://media.githubusercontent.com/media/MuseumofModernArt/collection/main/Artists.json"
)
Insert cell
Insert cell
Insert cell
viewof file = Inputs.file()
Insert cell
Insert cell
Insert cell
viewof csvfile = Inputs.file({label: "CSV file", accept: ".csv", required: true})
Insert cell
csvData = csvfile.csv({ typed: true })
Insert cell
Insert cell
import { alphabet } from "@observablehq/vega-lite-chart-types"
Insert cell
alphabet
Insert cell
Insert cell
database = FileAttachment("chinook.db").sqlite()
Insert cell
Insert cell
database
SELECT * FROM tracks WHERE AlbumId = 1
Insert cell
query
Insert cell
Insert cell
Insert cell
Insert cell
chart = BarChart(data, {chartHeight: 350})
Insert cell
function BarChart(data, {
chartWidth = 800,
chartHeight = 600,
marginTop = 40,
marginBottom = 10,
marginLeft = 120,
marginRight = 20,
barHeight = 25
} = {}) {
const width = chartWidth - marginLeft - marginRight;
const height = chartHeight - marginTop - marginBottom;
// Creates sources <svg> element
const svg = d3.create("svg")
.attr('width', width + marginLeft + marginRight)
.attr('height', height + marginTop + marginBottom);

// Group used to enforce margin
const g = svg.append('g').attr('transform', `translate(${marginLeft},${marginTop})`);
// Render the chart with new data

// DATA JOIN use the key argument for ensurign that the same DOM element is bound to the same data-item
const rect = g.selectAll('rect').data(data).join(
// ENTER
// new elements
(enter) => {
const rect_enter = enter.append('rect').attr('x', 0);
rect_enter.append('title');
return rect_enter;
},
// UPDATE
// update existing elements
(update) => update,
// EXIT
// elements that aren't associated with data
(exit) => exit.remove()
);

// ENTER + UPDATE
// both old and new elements
rect
.attr('height', barHeight)
.attr('width', (d) => d * 7)
.attr('y', (d, i) => i * (barHeight + 5));

rect.select('title').text((d) => `value: ${d}`);
return svg.node();
}
Insert cell
<style>
rect {
fill: steelblue;
fill-opacity: 0.8;
}
rect:hover {
fill-opacity: 1;
}
.axis {
font-size: smaller;
}
</style>
Insert cell
data = [66.38, 21.51, 23.37, 34.17, 36.21]
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