Published
Edited
Dec 2, 2021
Insert cell
# Cartesian coordinates
Insert cell
A line and area on classic cartesian coordinates
Insert cell
{
const width = 900
const height = 400
const svg = d3.create('svg')
.attr("width",width)
.attr("height",height);
const group = svg.append("g")

//------------------------------------------ THELINE

const x = d3.scaleLinear()
.domain([0, 100])
.range([100, width - 120]);

const y = d3.scaleLinear()
.domain([25000, 0])
.range([50, 300]);

const xAxis = d3.axisBottom().scale(x)
.ticks(19, ",.0f")
.tickPadding(5)
.tickValues(d3.range(0, 101, 5));

const yAxis = d3.axisLeft().scale(y).ticks(6, ",.0f");

const theline = d3.line()
.curve(d3.curveBasisOpen)
.x(d => x(d.index))
.y(d => y(d.ac));

const thearea = d3.area()
// .defined(d => d.index > 50 && d.index < 70)
.curve(d3.curveBasisOpen)
.x(d => x(d.index))
.y0(d => y(d.ac/1.8*Math.random()))
.y1(d => y(d.ac+d.ac/2*Math.random()));

group
.append("g")
.attr('transform', 'translate(0,' + (300) + ')')
.call(xAxis)

group
.append("g")
.attr('transform', 'translate(' + (100) + ',0)')
.call(yAxis);

group
.append("path")
.attr("d", thearea(population))
.attr("stroke-width", "0px")
.attr("stroke", "none")
.attr("fill", "#45E4FE")
group
.append("path")
.attr("d", theline(population))
.attr("stroke-width", "4px")
.attr("stroke", "#16B6D6")
.attr("fill", "none")

group
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 20)
.attr("x", 0 - (170))
.attr("dy", "1em")
.style("text-anchor", "middle")
.text("Population")
.attr("font-family", "Gill Sans, sans-serif")
.attr("font-size", 16)
.style("fill", "black");

group.append("text")
.attr("y", 330)
.attr("x", 420)
.attr("dy", "1em")
.style("text-anchor", "middle")
.text("Age")
.attr("font-family", "Gill Sans, sans-serif")
.attr("font-size", 16)
.style("fill", "black");

return svg.node();
}
Insert cell
population = FileAttachment("poblacion.json").json()
Insert cell
d3 = require("d3@6")
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