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

var white = d3.rgb("#ffffff"); // Pass in Hex
var steelblue = d3.lab(52, -4, -32); // Lab color space (l, a, b); steelblue
d3.select("body").style("background-color", steelblue) // set background color
const gradient = DOM.uid();

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

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

svg.append("linearGradient")
.attr("id", gradient.id)
.attr("gradientUnits", "userSpaceOnUse")
.attr("x1", 0)
.attr("y1", height - margin.bottom)
.attr("x2", 0)
.attr("y2", margin.top)
.selectAll("stop")
.data(d3.ticks(0, 1, 10))
.join("stop")
.attr("offset", d => d)
.attr("stop-color", color.interpolator());

svg.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", gradient)
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", line);

return svg.node();
}
Insert cell
height = 500
Insert cell
margin = ({top: 20, right: 30, bottom: 30, left: 70})
Insert cell
x = d3.scaleUtc()
.domain(d3.extent(data, d => d.date))
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain(d3.extent(data, d => d.value)).nice()
.range([height - margin.bottom, margin.top])
Insert cell
color = d3.scaleSequential(y.domain(), d3.interpolateRgb("RGB(115,96,17)","RGB(242,229,73)"))
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 40).tickSizeOuter(0))
.call(g => g.select(".domain").remove())
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
.call(g => g.select(".tick:last-of-type text").append("tspan").text(data.y))
Insert cell
line = d3.line()
.curve(d3.curveStep)
.defined(d => !isNaN(d.value))
.x(d => x(d.date))
.y(d => y(d.value))
Insert cell
parseISO= d3.utcParse("%Y-%m-%dT%H:%M:%S.%LZ");
Insert cell
parser=d3.utcParse("%m/%d/%Y, %H:%M:%S.%L");
Insert cell
data = Object.assign(d3.csvParse(await FileAttachment("lightmock.csv").text(), d3.autoType).map(({date, light}) => ({date: parser(date), value: light})), {title: "Light in lumens", y: " lumens"})
Insert cell
d3 = require("d3@5")
Insert cell
import {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