Published
Edited
Jun 29, 2020
1 fork
1 star
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("id", "chart");

svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);
const path = svg.append("g")
.attr("fill", "none")
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.selectAll("path")
.data(data.series)
.join("path")
.attr("d", d => line(d.y))
.attr("data-index", (d, i) => i)
.attr("data-name", d => d.name)
.attr("class", "line");
return svg.node();
}
Insert cell
height = 360
Insert cell
yTickSize = 40
Insert cell
margin = ({top: 20, right: 0, bottom: 60, left: yTickSize})
Insert cell
x = d3.scaleLinear()
.domain(d3.extent(data.x))
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain([0, 1]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x)
.ticks(width / 140)
// .tickFormat(d3.format(".0s"))
.tickFormat(d3.formatPrefix(".0", 1e6))
.tickSizeOuter(0))
.call(g => g.selectAll(".tick text") // tick labels
.attr("class", "xsmall-copy"))
.call(g => g.select(".tick:first-of-type text").clone() // unit label
.attr("y", 28)
.attr("class", "xsmall-copy font-bold font-sans-serif")
.text("Samples"))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left + 1},0)`)
.attr("stroke-opacity", 0.05)
.call(d3.axisLeft(y)
.ticks(5)
.tickSize(yTickSize)) // left axis
.call(g => g.selectAll(".tick line").clone() // grid lines
.attr("stroke-opacity", 0.05)
.attr("x2", width - margin.left - margin.right))
.call(g => g.select(".tick:first-of-type") // first tick opacity
.attr("stroke-opacity", 1))
.call(g => g.selectAll(".tick text") // tick labels
.style("text-anchor", "start")
.attr("x", -yTickSize)
.attr("y", -9)
.attr("class", "xsmall-copy"))
.call(g => g.select(".domain").remove()) // remove domain line
.call(g => g.select(".tick:last-of-type text").clone() // unit label
.attr("x", `${yLabelOffset}`)
.attr("text-anchor", "start")
.attr("class", "xsmall-copy font-bold font-sans-serif")
.text("Normalized Reward"))
Insert cell
yLabelOffset = -yTickSize + 30
Insert cell
line = d3.line()
.defined(d => !isNaN(d))
.x((d, i) => x(data.x[i]))
.y(d => y(d))
Insert cell
data = {
const data = await d3.json("https://gist.githubusercontent.com/justinjaywang/f550f718980f2057b6e939c7f008e025/raw/f1b5463667faddc1f17f329beb215b971b347037/object_counting.json");

data.series = Object.keys(data).map(k => ({
name: k,
y: data[k].y,
y_upper: data[k].y_upper,
y_lower: data[k].y_lower
}));
data.x = data[Object.keys(data)[0]].x;
return data;
}
Insert cell
data.x
Insert cell
data.y
Insert cell
data
Insert cell
d3 = require("d3@5")
Insert cell
Insert cell
openaicss = {
return html`<link rel="stylesheet" type="text/css" href="https://cdn.openai.com/miscellaneous/observable-1.5.0/styles/openai-css.css">`
}
Insert cell
html`<style>
svg, g {
color: var(--black);
font-size: 1rem;
}
#chart [data-index="0"] { /* hide-and-seek */
stroke: var(--medium-blue);
}
#chart [data-index="1"] {
stroke: var(--cool-gray-1);
}
#chart [data-index="2"] { /* count-based */
stroke: var(--golden);
}
</style>`
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