Public
Edited
Mar 29
Insert cell
AIDevops Coordinates
Insert cell
Insert cell
fields = [... new Set(Object.entries(selection[0]).map(e => e[0]))].map((e,i) => ({[e]:`${i}`}))
Insert cell
selection
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
function map$with$ (inPool,func,outPool,index){
const size = (pool) => pool.length
if(size(inPool) == 0){
return outPool
}else{
// console.log("inPool: ", inPool)
const inPoolNext = inPool.slice(1)
const funcNext = func
const outPoolNext = [...outPool,func(inPool[0],index)]
const indexNext = index + 1
return map$with$(inPoolNext,funcNext,outPoolNext,indexNext)
}
}
Insert cell
map$with$([1,2,3],(x) => x + 1, [],0)
Insert cell
flatAndflip = (values,i) => {
const input = Object.entries(values)
const f = (
(y) =>
([value,key],index) => {
return {[value]:key,"x":index,"y":y}
}
)(i)
const out = map$with$(input,f,[],0)
console.log("flatAndflip out: ", out)
return out
}
Insert cell
flatAndflip({"1":"one","2":"two"})
Insert cell
function flatAndFlipMap (selection){
console.log("Selection: ", selection)
const out = map$with$(selection,flatAndflip,[],0).flatMap(e => e)
console.log("flatAndFlipMap out: ", out)
return out
}
Insert cell
// map$with$(
raw = flatAndFlipMap(selectionTable
.slice(0,1000)
)
// (x) => ({...x,index:index}),
// []
// )
Insert cell
data10 = map$with$(
raw,
(e) => ({value:Object.entries(e)[0][1],date:e.x,name:e.y}),
[]
)
Insert cell
data11 = map$with$(
raw,
(e) => ({unemployment:Object.entries(e)[0][1],date:e.x,division:e.y}),
[]
)
Insert cell
// function assert (ideal,actual){
// return JSON.stringify(ideal) == JSON.stringify(actual) ? '✅' : '🤡'
// }
Insert cell
viewof h = Inputs.range([0, 10000], {label: "h", step: 1})
Insert cell
Plot.plot({
height: h,
width,
marginBottom: 1,
marginLeft: 120,
x: {axis: "top"},
y: {axis: null, range: [y1, y2]},
fy: {label: null, domain: data10.map(d => d.name)}, // preserve input order
marks: [
Plot.areaY(data10, {x: "date", y: "value", fy: "name", curve: "linear", sort: "date", fill: "#ccc",opacity:0.5,tip:true}),
Plot.lineY(data10, {x: "date", y: "value", fy: "name", curve: "linear", sort: "date", strokeWidth: 1,opacity:0.5,tip:true})
]
})
Insert cell
viewof y1 = Inputs.range([0, 100], {label: "y1", step: 1})
Insert cell
viewof y2 = Inputs.range([-100, 0], {label: "y2", step: 1})
Insert cell
{
const dict = {}
// const fields_ = fields

//
// flatAllRecordFieldsFrom = (record) => map$with$(pool,function)
// pool' = pool 'minus' currentRecord
// add$to$ = (currentRecord,dict) => flatAllRecordFieldsFrom(record) : dict
// currentRecordIndex' = currentRecordIndex + 1
// currentRecord' = pool[currentRecordIndex]
// dict' = add$to$(currentRecord,dict)



// for(record of records){
// }
// selection.map((e,i) => {
// // const getFiledNameFromRecord = (record,fieldName) => record[fieldName]
// const fieldEnum = (record,fieldName) => record[fieldName]
// return {
// name: i,
// date: fieldEnum(e),
// // value: fieldValue
// }
// })
}
Insert cell
selection // brush an axis below to see this update!
Insert cell
Insert cell
Insert cell
viewof selection = {

// 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(data, 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));

const path = svg.append("g")
.attr("fill", "none")
.attr("stroke-width", 1.5)
.attr("stroke-opacity", 0.4)
.selectAll("path")
.data(data.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]])))
.call(path => path.append("title")
.text(d => d.name));

// Append the axis for each key.
const axes = 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"));


// Create the brush behavior.
const deselectedColor = "#ddd";
const brushHeight = 50;
const brush = d3.brushX()
.extent([
[marginLeft, -(brushHeight / 2)],
[width - marginRight, brushHeight / 2]
])
.on("start brush end", brushed);

axes.call(brush);

const selections = new Map();

function brushed({selection}, key) {
if (selection === null) selections.delete(key);
else selections.set(key, selection.map(x.get(key).invert));
const selected = [];
path.each(function(d) {
const active = Array.from(selections).every(([key, [min, max]]) => d[key] >= min && d[key] <= max);
d3.select(this).style("stroke", active ? color(d[keyz]) : deselectedColor);
if (active) {
d3.select(this).raise();
selected.push(d);
}
});
svg.property("value", selected).dispatch("input");
}

return Object.assign(svg.property("value", data).node(), {scales: {color}});
}
Insert cell
untitled@1.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Select a data source…
Type Table, then Shift-Enter. Ctrl-space for more options.

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

Insert cell
keys = data.columns.slice(1)
Insert cell
import {legend as Legend} from "@d3/color-legend"
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