Public
Edited
Nov 14, 2023
2 forks
Insert cell
Insert cell
Insert cell
Insert cell
chart = {

// Specify the chart’s dimensions.
const width = 928;
const height = keys.length * 120;
const marginTop = 20;
const marginRight = 10;
const marginBottom = 20;
const marginLeft = 10;

// Create an horizontal (*x*) scale for each key.
const x = new Map(Array.from(keys, key => [key, d3.scaleLinear(d3.extent(data6, d => d[key]), [marginLeft, width - marginRight])]));

// Create the vertical (*y*) scale.
const y = d3.scalePoint(keys, [marginTop, height - marginBottom]);

// Create the color scale.
const color = d3.scaleSequential(x.get(keyz).domain(), t => d3.interpolateBrBG(1 - t));

// Create the SVG container.
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("width", width)
.attr("height", height)
.attr("style", "max-width: 100%; height: auto;");

// Append the lines.
const line = d3.line()
.defined(([, value]) => value != null)
.x(([key, value]) => x.get(key)(value))
.y(([key]) => y(key));

svg.append("g")
.attr("fill", "none")
.attr("stroke-width", 1.5)
.attr("stroke-opacity", 0.4)
.selectAll("path")
.data(data6.slice().sort((a, b) => d3.ascending(a[keyz], b[keyz])))
.join("path")
.attr("stroke", d => color(d[keyz]))
.attr("d", d => line(d3.cross(keys, [d], (key, d) => [key, d[key]])))
.append("title")
.text(d => d.name);

// Append the axis for each key.
svg.append("g")
.selectAll("g")
.data(keys)
.join("g")
.attr("transform", d => `translate(0,${y(d)})`)
.each(function(d) { d3.select(this).call(d3.axisBottom(x.get(d))); })
.call(g => g.append("text")
.attr("x", marginLeft)
.attr("y", -6)
.attr("text-anchor", "start")
.attr("fill", "currentColor")
.text(d => d))
.call(g => g.selectAll("text")
.clone(true).lower()
.attr("fill", "none")
.attr("stroke-width", 5)
.attr("stroke-linejoin", "round")
.attr("stroke", "white"));

return Object.assign(svg.node(), {scales: {color}});
}
Insert cell
data10 = FileAttachment("cars.csv").csv({typed: true})
Insert cell
data2 = [
{name: "AMC Ambassador Brougham", economy: 13, cylinders: 8, displacement: 360, power: 175, weight: 3821, "0-60 mph": 11, year: 73}
]
Insert cell
alerts (2).json
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
keys = data.columns//.slice(1)
// keys = Object.keys(data[0])
Insert cell
data4 = [
{fruit:"banana",country:"Morocco",weight:100},
{fruit:"apples",country:"Tunisia",weight:100},
{fruit:"banana",country:"Egypt",weight:200},
{fruit:"pineapple",country:"Morocco",weight:130},
{fruit:"pineapple",country:"Tunisia",weight:140},
{fruit:"apples",country:"Tunisia",weight:120},
{fruit:"banana",country:"Egypt",weight:110},
{fruit:"pineapple",country:"Morocco",weight:400},
{fruit:"pineapple",country:"Tunisia",weight:200},
{fruit:"apples",country:"Egypt",weight:250},
{fruit:"banana",country:"Morocco",weight:220}
]
Insert cell
keys4= Object.keys(data4[0])
Insert cell
x = {
const out = {}
keys4.map(key => out[key] = new Set())
data4.map(record => {
keys4.map(key => out[key].add(record[key]))
})
keys4.map(key => out[key] = [...out[key]])
return out
}
Insert cell
Insert cell
data5 = {
return data4.map(record => {
const obj = {}
keys4.map(key => obj[key] = x[key].indexOf(record[key]))
return obj
})
}
Insert cell
data.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
data = FileAttachment("data.csv").csv({typed: true})
Insert cell
Select a data source…
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
import {legend as Legend} from "@d3/color-legend"
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