Published
Edited
Feb 13, 2021
3 forks
1 star
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

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

svg
.append("g")
.call(yAxis)
.attr("transform", `translate(${margin.left},0)`);

const yLabel = svg.append("g").call(yTitle);

const xLabel = svg.append("g").call(xTitle);

const xgridlines = svg.append("g").call(xGrid);

const ygridlines = svg.append("g").call(yGrid);

const symbols = svg
.append("g")
.attr("stroke-width", 1)
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.selectAll("path")
.data(penguins)
.join("path")
.attr(
"transform",
d => `translate(${xScale(xAccessor(d))}, ${yScale(yAccessor(d))})`
)
.attr("fill", d => color(d.species))
.attr("d", d => shape(d.species));

const legends = svg
.append("g")
.call(legendLabel)
.attr(
"transform",
`translate(${width - margin.right - margin.left - 50}, ${height - 200})`
)
.style("font-size", "13px")
.style("font-family", "sans-serif");

return svg.node();

}
Insert cell
Insert cell
import { penguins } from "@enjalot/palmer-penguins"
Insert cell
penguins
Insert cell
Insert cell
xAccessor = d => d.flipper_length_mm
Insert cell
yAccessor = d => d.body_mass_g
Insert cell
xAccessor(penguins[100])
Insert cell
yAccessor(penguins[100])
Insert cell
Insert cell
width
Insert cell
height = 500
Insert cell
margin = ({ top: 20, right: 20, bottom: 30, left: 40 })
Insert cell
Insert cell
xScale = d3
.scaleLinear()
.domain(d3.extent(penguins, xAccessor))
.range([margin.left, width - margin.right])
.nice()
Insert cell
yScale = d3
.scaleLinear()
.domain(d3.extent(penguins, yAccessor))
.rangeRound([height - margin.bottom, margin.top])
.nice()
Insert cell
shape = d3.scaleOrdinal(
penguins.map(d => d.species),
d3.symbols.map(s => d3.symbol().type(s)())
)
Insert cell
color = d3.scaleOrdinal(d3.schemeCategory10)
Insert cell
legend = d3
.scaleOrdinal()
.domain(penguins.map(d => d.species))
.range(d3.schemeCategory10)
Insert cell
Insert cell
xAxis = d3.axisBottom(xScale)
Insert cell
yAxis = d3.axisLeft(yScale)
Insert cell
Insert cell
xGrid = g =>
g
.selectAll('line')
.data(xScale.ticks())
.join('line')
.attr('x1', d => xScale(d))
.attr('x2', d => xScale(d))
.attr('y1', margin.top)
.attr('y2', height - margin.bottom)
.style("stroke-width", 0.2)
.style("stroke", "black")
Insert cell
yGrid = g =>
g
.attr('class', 'grid-lines')
.selectAll('line')
.data(yScale.ticks())
.join('line')
.attr('x1', margin.left)
.attr('x2', width - margin.right)
.attr('y1', d => yScale(d))
.attr('y2', d => yScale(d))
.style("stroke-width", 0.2)
.style("stroke", "black")
Insert cell
Insert cell
yTitle = g =>
g
.append("text")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("y", 10)
.html("Body Mass (g)")
Insert cell
xTitle = g =>
g
.append("text")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("x", width - margin.right - 80)
.attr("y", height - margin.bottom - 5)
.html("Flipper Length (mm)")
Insert cell
legendLabel = d3Legend
.legendColor()
.shape("path", d3.symbols.map(s => d3.symbol().type(s)()))
.scale(legend)
.labelOffset(20)
.shapePadding(40)
.labels(['Adelie', 'Gentoo', 'Chinstrap'])
Insert cell
Insert cell
Insert cell
Insert cell
d3Legend = require('d3-svg-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