Public
Edited
May 16, 2023
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
points = data.map(d => {
let scale = d3.scaleLinear([0, _.sum(d.weightedVals)], [0, 1]);
let ps = d.weightedVals.map(dd => scale(dd) * 100);
let zeros = new Array(ps.length).fill(0);
let zipped = _.zip(zeros, ps).flat();
zipped.push(0);
return {...d, points: zipped}
})
Insert cell
// Copyright 2021 Observable, Inc.
// Released under the ISC license.
// https://observablehq.com/@d3/multi-line-chart
function LineChart(data, {
title, // given d in data, returns the title text
defined, // for gaps in data
curve = d3.curveLinear, // method of interpolation between points
marginTop = 20, // top margin, in pixels
marginRight = 30, // right margin, in pixels
marginBottom = 30, // bottom margin, in pixels
marginLeft = 40, // left margin, in pixels
width = 640, // outer width, in pixels
height = 400, // outer height, in pixels
xType = d3.scaleUtc, // type of x-scale
xDomain, // [xmin, xmax]
xRange = [marginLeft, width - marginRight], // [left, right]
yType = d3.scaleLinear, // type of y-scale
yDomain, // [ymin, ymax]
yRange = [height - marginBottom, marginTop], // [bottom, top]
yFormat, // a format specifier string for the y-axis
yLabel, // a label for the y-axis
zDomain, // array of z-values
color = "currentColor", // stroke color of line, as a constant or a function of *z*
strokeLinecap, // stroke line cap of line
strokeLinejoin, // stroke line join of line
strokeWidth = 1.5, // stroke width of line
strokeOpacity, // stroke opacity of line
mixBlendMode = "multiply", // blend mode of lines
voronoi // show a Voronoi overlay? (for debugging)
} = {}) {


// Compute default domains, and unique the z-domain.
xDomain = [-1, 4]
yDomain = [0, 1];

// Construct scales and axes.
const xScale = xType(xDomain, xRange);
const yScale = yType(yDomain, yRange);
const xAxis = d3.axisBottom(xScale).ticks(width / 80).tickSizeOuter(0);
const yAxis = d3.axisLeft(yScale).ticks(height / 60, yFormat);

// Compute titles.
const T = title === undefined ? "untitled" : title === null ? null : d3.map(data, title);

// Construct a line generator.
const line = d3.line()
.curve(curve)
.x(d => xScale(d[0]))
.y(d => yScale(d[1]));

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

svg.append("g")
.attr("transform", `translate(0,${height - marginBottom})`)
.call(xAxis);

svg.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.call(yAxis)
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", -marginLeft)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text(yLabel));

const path = svg.append("g")
.attr("fill", "none")
.attr("stroke", typeof color === "string" ? color : null)
.attr("stroke-linecap", strokeLinecap)
.attr("stroke-linejoin", strokeLinejoin)
.attr("stroke-width", strokeWidth)
.attr("stroke-opacity", strokeOpacity)
.selectAll("path")
.data(data)
.join("path")
.style("mix-blend-mode", mixBlendMode)
.attr("d", d => line([[0, 0], [d.value - .2, 0], [d.value, d.weight], [d.value + .2, 0], [4, 0]]))
.attr("title", title)

return Object.assign(svg.node(), {value: null});
}
Insert cell
lines = LineChart(points, {
curve: curve,
title: d => d.parameter,
yLabel: "↑ Confidence",
width,
height: 500,
color: "steelblue"
})
Insert cell
viewof t = Inputs.range([0,1], {step: .2})
Insert cell
curve = {
let curve = curveSelect;
try {
curve = curveSelect.tension(t);
}
catch(err) {
curve = curveSelect;
}
return curve
}
Insert cell
curveTypes = new Map([
["basis", d3.curveBasis],
["basis-closed", d3.curveBasisClosed],
["basis-open", d3.curveBasisOpen],
["bundle", d3.curveBundle],
["cardinal", d3.curveCardinal],
["cardinal-closed", d3.curveCardinalClosed],
["cardinal-open", d3.curveCardinalOpen],
["catmullrom", d3.curveCatmullRom],
["catmullrom-closed", d3.curveCatmullRomClosed],
["catmullrom-open", d3.curveCatmullRomOpen],
["linear", d3.curveLinear],
["linear-closed", d3.curveLinearclosed],
["monotonex", d3.curveMonotoneX],
["monotoney", d3.curveMonotoneY],
["natural", d3.curveNatural],
["step", d3.curveStep],
["setp-after", d3.curveStepAfter],
["step-before", d3.curveStepBefore]
] )
Insert cell
// Copyright 2021 Observable, Inc.
// Released under the ISC license.
// https://observablehq.com/@d3/multi-line-chart
function LineChart2(data, {
title, // given d in data, returns the title text
defined, // for gaps in data
curve = d3.curveLinear, // method of interpolation between points
marginTop = 20, // top margin, in pixels
marginRight = 30, // right margin, in pixels
marginBottom = 30, // bottom margin, in pixels
marginLeft = 40, // left margin, in pixels
width = 640, // outer width, in pixels
height = 400, // outer height, in pixels
xType = d3.scaleUtc, // type of x-scale
xDomain, // [xmin, xmax]
xRange = [marginLeft, width - marginRight], // [left, right]
yType = d3.scaleLinear, // type of y-scale
yDomain, // [ymin, ymax]
yRange = [height - marginBottom, marginTop], // [bottom, top]
yFormat, // a format specifier string for the y-axis
yLabel, // a label for the y-axis
zDomain, // array of z-values
color = "currentColor", // stroke color of line, as a constant or a function of *z*
strokeLinecap, // stroke line cap of line
strokeLinejoin, // stroke line join of line
strokeWidth = 1.5, // stroke width of line
strokeOpacity, // stroke opacity of line
mixBlendMode = "multiply", // blend mode of lines
voronoi // show a Voronoi overlay? (for debugging)
} = {}) {


// Compute default domains, and unique the z-domain.
xDomain = [0, 8]
yDomain = [0, 100];

// Construct scales and axes.
const xScale = xType(xDomain, xRange);
const yScale = yType(yDomain, yRange);
const xAxis = d3.axisBottom(xScale).ticks(width / 80).tickSizeOuter(0);
const yAxis = d3.axisLeft(yScale).ticks(height / 60, yFormat);

// Compute titles.
const T = title === undefined ? "untitled" : title === null ? null : d3.map(data, title);

// Construct a line generator.
const line = d3.line()
.curve(curve)
.x((d, i) => xScale(i))
.y((d, i) => yScale(d));

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

svg.append("g")
.attr("transform", `translate(0,${height - marginBottom})`)
.call(xAxis);

svg.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.call(yAxis)
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", -marginLeft)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text(yLabel));

const path = svg.append("g")
.attr("fill", "none")
.attr("stroke", typeof color === "string" ? color : null)
.attr("stroke-linecap", strokeLinecap)
.attr("stroke-linejoin", strokeLinejoin)
.attr("stroke-width", strokeWidth)
.attr("stroke-opacity", strokeOpacity)
.selectAll("path")
.data(data)
.join("path")
.style("mix-blend-mode", mixBlendMode)
.attr("d", d => line(d.points))
.attr("title", title)

return Object.assign(svg.node(), {value: null});
}
Insert cell
p = [points[1]]
Insert cell
p[0].parameter
Insert cell
lines2 = LineChart2(p, {
curve: curve,
title: d => d.parameter,
yLabel: "↑ Confidence",
width,
height: 500,
color: "steelblue"
})
Insert cell
viewof curveSelect = Inputs.select(curveTypes, {label: "Curves"})
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