• Home
  • Comparisons
16S_DAIRYdb_V1.1.2
Sunburst
Thematics overview
// @title: Tree layout interactif avec counts
// @load d3@7
//import {data} from "@d3/zoomable-sunburst"
viewof sunburst = {
  const data = await FileAttachment("reactable_data.json").json();

  const ranks = Object.keys(data[0]).filter(k => k !== "Count");

  function buildHierarchy(data) {
    const root = { name: "root", children: [] };
    for (const row of data) {
      let current = root;
      for (const rank of ranks) {
        const value = row[rank];
        if (!value) break;
        let child = current.children.find(c => c.name === value);
        if (!child) {
          child = { name: value, children: [] };
          current.children.push(child);
        }
        current = child;
      }
      current.value = (current.value || 0) + row.Count;
    }
    return root;
  }

  const hierarchyData = buildHierarchy(data);

  const container = document.createElement("div");
  container.id = "chart-container";
  container.style.width = "100%";
  container.style.maxWidth = "800px";
  container.style.margin = "auto";

  let width = container.offsetWidth;
  let svg = drawSunburst(width);
  container.appendChild(svg);

  function drawSunburst(width) {
    const height = width;
    const radius = width / 2 / (d3.hierarchy(hierarchyData).height + 1);

    const root = d3.hierarchy(hierarchyData)
      .sum(d => d.value)
      .sort((a, b) => b.value - a.value);

    const partition = d3.partition()
      .size([2 * Math.PI, root.height + 1]);
    partition(root);
    root.each(d => d.current = d);

    const color = d3.scaleOrdinal(d3.quantize(d3.interpolateRainbow, root.children.length + 1));

    const arc = d3.arc()
      .startAngle(d => d.x0)
      .endAngle(d => d.x1)
      .innerRadius(d => d.y0 * radius)
      .outerRadius(d => d.y1 * radius - 1);

    const svg = d3.create("svg")
      .attr("viewBox", [-width / 2, -height / 2, width, height])
      .attr("width", width)
      .attr("height", height)
      .attr("style", "max-width: 100%; height: auto; font: 10px sans-serif;");

    const g = svg.append("g");

    const path = g.append("g")
      .selectAll("path")
      .data(root.descendants().slice(1))
      .join("path")
        .attr("fill", d => {
          while (d.depth > 1) d = d.parent;
          return color(d.data.name);
        })
        .attr("fill-opacity", d => arcVisible(d.current) ? (d.children ? 0.6 : 0.4) : 0)
        .attr("d", d => arc(d.current))
        .style("cursor", d => d.children ? "pointer" : null)
        .on("click", clicked);

    path.append("title")
      .text(d => `${d.ancestors().map(d => d.data.name).reverse().join(" / ")}\n${d.value}`);

    const label = g.append("g")
      .attr("pointer-events", "none")
      .attr("text-anchor", "middle")
      .style("user-select", "none")
      .selectAll("text")
      .data(root.descendants().slice(1))
      .join("text")
        .attr("dy", "0.35em")
        .attr("fill-opacity", d => +labelVisible(d.current))
        .attr("transform", d => labelTransform(d.current))
        .text(d => d.data.name);

    const parent = g.append("circle")
      .datum(root)
      .attr("r", radius)
      .attr("fill", "none")
      .attr("pointer-events", "all")
      .on("click", clicked);

    function clicked(event, p) {
      parent.datum(p.parent || root);

      root.each(d => d.target = {
        x0: Math.max(0, Math.min(1, (d.x0 - p.x0) / (p.x1 - p.x0))) * 2 * Math.PI,
        x1: Math.max(0, Math.min(1, (d.x1 - p.x0) / (p.x1 - p.x0))) * 2 * Math.PI,
        y0: Math.max(0, d.y0 - p.depth),
        y1: Math.max(0, d.y1 - p.depth)
      });

      const t = svg.transition().duration(event.altKey ? 7500 : 750);

      path.transition(t)
        .tween("data", d => {
          const i = d3.interpolate(d.current, d.target);
          return t => d.current = i(t);
        })
        .filter(function(d) {
          return +this.getAttribute("fill-opacity") || arcVisible(d.target);
        })
        .attr("fill-opacity", d => arcVisible(d.target) ? (d.children ? 0.6 : 0.4) : 0)
        .attr("pointer-events", d => arcVisible(d.target) ? "auto" : "none")
        .attrTween("d", d => () => arc(d.current));

      label
        .filter(function(d) {
          return +this.getAttribute("fill-opacity") || labelVisible(d.target);
        }).transition(t)
        .attr("fill-opacity", d => +labelVisible(d.target))
        .attrTween("transform", d => () => labelTransform(d.current));
    }

    function arcVisible(d) {
      return d.y0 >= 1 && d.x1 > d.x0;
    }

    function labelVisible(d) {
      return d.y1 <= 3 && d.y0 >= 1 && (d.y1 - d.y0) * (d.x1 - d.x0) > 0.03;
    }

    function labelTransform(d) {
      const x = (d.x0 + d.x1) / 2 * 180 / Math.PI;
      const y = (d.y0 + d.y1) / 2 * radius;
      return `rotate(${x - 90}) translate(${y},0) rotate(${x < 180 ? 0 : 180})`;
    }

    return svg.node();
  }

  const resizeObserver = new ResizeObserver(entries => {
    for (const entry of entries) {
      const newWidth = entry.contentRect.width;
      if (Math.abs(newWidth - width) > 10) {
        width = newWidth;
        container.innerHTML = "";
        svg = drawSunburst(width);
        container.appendChild(svg);
      }
    }
  });

  resizeObserver.observe(container);

  return container;
}