D3 example from Observable on my wordpress site


Staff member
Thank you very much for the help in advance.

I am trying to add this Sankey Diagram on my wordpress site.

<a href="https://observablehq.com/@d3/sankey-diagram" rel="nofollow noreferrer">https://observablehq.com/@d3/sankey-diagram</a>

I am having a hard time. I downloaded the JS code, pasted it on my wordpress site, but it gives me a lot of errors

Uncaught SyntaxError: Unexpected token 'export'

Is the first error from the browser.

This is the code I downloaded below:

&lt;script src="https://d3js.org/d3.v5.js"&gt;&lt;/script&gt;
// https://observablehq.com/@d3/[email protected]
export default function define(runtime, observer) {
  const main = runtime.module();
  const fileAttachments = new Map([["energy.csv",new URL("./files/d6774e9422bd72369f195a30d3a6b33ff9d41676cff4d89c93511e1a458efb3cfd16cbb7ce3fecdd8dd2466121e10c9bfe57fd73c7520bf358d352a92b898614",import.meta.url)]]);
  main.builtin("FileAttachment", runtime.fileAttachments(name =&gt; fileAttachments.get(name)));
  main.variable(observer()).define(["md"], function(md){return(
md`# Sankey Diagram

This [Sankey diagram](https://github.com/d3/d3-sankey) visualizes the flow of energy: *supplies* are on the left, and *demands* are on the right. Links show how varying amounts of energy are converted or transmitted before being consumed or lost. Data: [Department of Energy &amp; Climate Change](http://www.decc.gov.uk/en/content/cms/tackling/2050/calculator_on/calculator_on.aspx) via [Tom Counsell](https://tamc.github.io/Sankey/)
  main.variable(observer("viewof edgeColor")).define("viewof edgeColor", ["html","URLSearchParams"], function(html,URLSearchParams){return(
  &lt;option value=input&gt;Color by input
  &lt;option value=output&gt;Color by output
  &lt;option value=path selected&gt;Color by input-output
  &lt;option value=none&gt;No color
&lt;/select&gt;`, {
  value: new URLSearchParams(html`&lt;a href&gt;`.search).get("color") || "path"
  main.variable(observer("edgeColor")).define("edgeColor", ["Generators", "viewof edgeColor"], (G, _) =&gt; G.input(_));
  main.variable(observer("viewof align")).define("viewof align", ["html","URLSearchParams"], function(html,URLSearchParams){return(
  &lt;option value=left&gt;Left-aligned
  &lt;option value=right&gt;Right-aligned
  &lt;option value=center&gt;Centered
  &lt;option value=justify selected&gt;Justified
&lt;/select&gt;`, {
  value: new URLSearchParams(html`&lt;a href&gt;`.search).get("align") || "justify"
  main.variable(observer("align")).define("align", ["Generators", "viewof align"], (G, _) =&gt; G.input(_));
  main.variable(observer("chart")).define("chart", ["d3","width","height","sankey","data","color","format","edgeColor","DOM"], function(d3,width,height,sankey,data,color,format,edgeColor,DOM)
  const svg = d3.create("svg")
      .attr("viewBox", [0, 0, width, height]);

  const {nodes, links} = sankey(data);

      .attr("stroke", "#000")
      .attr("x", d =&gt; d.x0)
      .attr("y", d =&gt; d.y0)
      .attr("height", d =&gt; d.y1 - d.y0)
      .attr("width", d =&gt; d.x1 - d.x0)
      .attr("fill", color)
      .text(d =&gt; `${d.name}\n${format(d.value)}`);

  const link = svg.append("g")
      .attr("fill", "none")
      .attr("stroke-opacity", 0.5)
      .style("mix-blend-mode", "multiply");

  if (edgeColor === "path") {
    const gradient = link.append("linearGradient")
        .attr("id", d =&gt; (d.uid = DOM.uid("link")).id)
        .attr("gradientUnits", "userSpaceOnUse")
        .attr("x1", d =&gt; d.source.x1)
        .attr("x2", d =&gt; d.target.x0);

        .attr("offset", "0%")
        .attr("stop-color", d =&gt; color(d.source));

        .attr("offset", "100%")
        .attr("stop-color", d =&gt; color(d.target));

      .attr("d", d3.sankeyLinkHorizontal())
      .attr("stroke", d =&gt; edgeColor === "none" ? "#aaa"
          : edgeColor === "path" ? d.uid 
          : edgeColor === "input" ? color(d.source) 
          : color(d.target))
      .attr("stroke-width", d =&gt; Math.max(1, d.width));

      .text(d =&gt; `${d.source.name} → ${d.target.name}\n${format(d.value)}`);

      .attr("font-family", "sans-serif")
      .attr("font-size", 10)
      .attr("x", d =&gt; d.x0 &lt; width / 2 ? d.x1 + 6 : d.x0 - 6)
      .attr("y", d =&gt; (d.y1 + d.y0) / 2)
      .attr("dy", "0.35em")
      .attr("text-anchor", d =&gt; d.x0 &lt; width / 2 ? "start" : "end")
      .text(d =&gt; d.name);

  return svg.node();
  main.variable(observer("sankey")).define("sankey", ["d3","align","width","height"], function(d3,align,width,height)
  const sankey = d3.sankey()
      .nodeId(d =&gt; d.name)
      .extent([[1, 5], [width - 1, height - 5]]);
  return ({nodes, links}) =&gt; sankey({
    nodes: nodes.map(d =&gt; Object.assign({}, d)),
    links: links.map(d =&gt; Object.assign({}, d))
  main.variable(observer("format")).define("format", ["d3","data"], function(d3,data)
  const format = d3.format(",.0f");
  return data.units ? d =&gt; `${format(d)} ${data.units}` : format;
  main.variable(observer("color")).define("color", ["d3"], function(d3)
  const color = d3.scaleOrdinal(d3.schemeCategory10);
  return d =&gt; color(d.category === undefined ? d.name : d.category);
  main.variable(observer("data")).define("data", ["d3","FileAttachment"], async function(d3,FileAttachment)
  const links = d3.csvParse(await FileAttachment("energy.csv").text(), d3.autoType);
  const nodes = Array.from(new Set(links.flatMap(l =&gt; [l.source, l.target])), name =&gt; ({name, category: name.replace(/ .*/, "")}));
  return {nodes, links, units: "TWh"};
  main.variable(observer("width")).define("width", function(){return(
  main.variable(observer("height")).define("height", function(){return(
  main.variable(observer("d3")).define("d3", ["require"], function(require){return(
require("[email protected]", "d3-[email protected]")
  return main;

The code downloaded is slightly different from the code on the webpage. I tried both versions of the code.

I believe there is something I am not understanding about D3, something about it being a module? I am not sure of the proper way to upload this on my wordpress site.

Here is the URL to the site to make things easier:
<a href="http://danielb66.sg-host.com/2001-2/" rel="nofollow noreferrer">http://danielb66.sg-host.com/2001-2/</a>