Public
Edited
Feb 20
1 fork
4 stars
Insert cell
Insert cell
Insert cell
Insert cell
colorHexbinTernaryPlot = {
const node = DOM.svg(width, height);
const svg = d3.select(node);
const chart = svg
.append("g")
.attr("transform", `translate(${width / 2} ${height / 2 + yOffset})`)
.attr("height", 300)
.attr("font-family", "sans-serif")
.attr("id", "chart");

const defs = chart.append("defs");

defs
.append("clipPath")
.attr("id", "trianglePath")
.append("path")
.attr("d", hexBinTernaryPlot.triangle());

const axisLabelsGroup = chart
.append("g")
.attr("class", "axis-labels")
.call(axisLabels, hexBinTernaryPlot.axisLabels());

const gridLinesPaths = hexBinTernaryPlot
.gridLines()
.map(axisGrid => axisGrid.map(d3.line()).join(" "));

const gridGroup = chart
.append("g")
.attr("class", "grid")
.call(grid, gridLinesPaths);

const axisTicksGroups = chart
.append("g")
.attr("class", "ternary-ticks")
.attr("font-size", 10)
.selectAll("g")
.data(hexBinTernaryPlot.ticks())
.join("g")
.attr("class", "axis-ticks");

axisTicksGroups.call(ticks);
// color-coded hexbins
chart
.append("g")
.attr("stroke", "#000")
.attr("stroke-opacity", 0.1)
.attr("clip-path", "url(#trianglePath)")
.selectAll("path")
.data(bins)
.join("path")
.attr("d", hexbin.hexagon())
.attr("transform", d => `translate(${d.x},${d.y})`)
.attr("fill", d => color(d.length))
.append("title")
.text(d =>
d
.map(
e => `${e.country}: ${e.agriculture}%, ${e.industry}%, ${e.service}%`
)
.join("\n")
);

return node;
}
Insert cell
color = d3.scaleSequential(d3.interpolateBuPu)
.domain([0, d3.max(bins, d => d.length) / 2])
Insert cell
Insert cell
areaHexbinTernaryPlot = {
const node = DOM.svg(width, height);
const svg = d3.select(node);
const chart = svg
.append("g")
.attr("transform", `translate(${width / 2} ${height / 2 + yOffset})`)
.attr("height", 300)
.attr("font-family", "sans-serif")
.attr("id", "chart");
const defs = chart.append("defs");

defs
.append("clipPath")
.attr("id", "trianglePath")
.append("path")
.attr("d", hexBinTernaryPlot.triangle());

const axisLabelsGroup = chart
.append("g")
.attr("class", "axis-labels")
.call(axisLabels, hexBinTernaryPlot.axisLabels());

const gridLinesPaths = hexBinTernaryPlot
.gridLines()
.map(axisGrid => axisGrid.map(d3.line()).join(" "));

const gridGroup = chart
.append("g")
.attr("class", "grid")
.call(grid, gridLinesPaths);

const axisTicksGroups = chart
.append("g")
.attr("class", "ternary-ticks")
.attr("font-size", 10)
.selectAll("g")
.data(hexBinTernaryPlot.ticks())
.join("g")
.attr("class", "axis-ticks");

axisTicksGroups.call(ticks);

// area-coded hexbins
chart
.append("g")
.attr("clip-path", "url(#trianglePath)")
.attr("fill", "#ddd")
.attr("stroke", "black")
.selectAll("path")
.data(bins)
.join("path")
.attr("transform", d => `translate(${d.x},${d.y})`)
.attr("d", d => hexbin.hexagon(r(d.length)))
.append("title")
.text(d =>
d
.map(
e => `${e.country}: ${e.agriculture}%, ${e.industry}%, ${e.service}%`
)
.join("\n")
);

return node;
}
Insert cell
r = d3.scaleSqrt()
.domain([0, d3.max(bins, d => d.length)])
.range([0, hexbin.radius() * Math.SQRT2])
Insert cell
Insert cell
hexBinTernaryPlot = ternaryPlot(ternary).labels([
"Agriculture",
"Industry",
"Service"
])
Insert cell
ternary = barycentric()
.a(d => d.agriculture)
.b(d => d.industry)
.c(d => d.service)
Insert cell
Insert cell
xydata = data.map(d => {
const [x, y] = hexBinTernaryPlot(d);

return Object.assign({ x, y }, d);
})
Insert cell
hexbin = d3.hexbin()
.x(d => d.x)
.y(d => d.y)
.radius(hexagonRadius * width / (height - 1));
Insert cell
bins = hexbin(xydata)
Insert cell
gridLines = hexBinTernaryPlot
.gridLines()
.map(axisGrid => axisGrid.map(d3.line()).join(" "))
Insert cell
height = width * 2/3;
Insert cell
Insert cell
Insert cell
d3Ternary = import("d3-ternary")
Insert cell
barycentric = d3Ternary.barycentric
Insert cell
ternaryPlot = d3Ternary.ternaryPlot
Insert cell
import {

ticks,
grid,
axisLabels,
yOffset
} from "@julesblm/d3-ternary"
Insert cell
d3 = require('d3@6', "d3-hexbin@0.2")
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