내 워드 프레스 사이트에서 Observable의 D3 예제

다니엘 화이트

사전에 도움을 주셔서 대단히 감사합니다.

이 Sankey 다이어그램을 워드 프레스 사이트에 추가하려고합니다.

https://observablehq.com/@d3/sankey-diagram

나는 어려움을 겪고있다. JS 코드를 다운로드하여 워드 프레스 사이트에 붙여 넣었지만 오류가 많이 발생합니다.

Uncaught SyntaxError: Unexpected token 'export'

브라우저의 첫 번째 오류입니다.

아래에서 다운로드 한 코드입니다.

<script src="https://d3js.org/d3.v5.js"></script>
<script>
// https://observablehq.com/@d3/sankey-diagram@273
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 => 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 & 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(
Object.assign(html`<select>
  <option value=input>Color by input
  <option value=output>Color by output
  <option value=path selected>Color by input-output
  <option value=none>No color
</select>`, {
  value: new URLSearchParams(html`<a href>`.search).get("color") || "path"
})
)});
  main.variable(observer("edgeColor")).define("edgeColor", ["Generators", "viewof edgeColor"], (G, _) => G.input(_));
  main.variable(observer("viewof align")).define("viewof align", ["html","URLSearchParams"], function(html,URLSearchParams){return(
Object.assign(html`<select>
  <option value=left>Left-aligned
  <option value=right>Right-aligned
  <option value=center>Centered
  <option value=justify selected>Justified
</select>`, {
  value: new URLSearchParams(html`<a href>`.search).get("align") || "justify"
})
)});
  main.variable(observer("align")).define("align", ["Generators", "viewof align"], (G, _) => 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);

  svg.append("g")
      .attr("stroke", "#000")
    .selectAll("rect")
    .data(nodes)
    .join("rect")
      .attr("x", d => d.x0)
      .attr("y", d => d.y0)
      .attr("height", d => d.y1 - d.y0)
      .attr("width", d => d.x1 - d.x0)
      .attr("fill", color)
    .append("title")
      .text(d => `${d.name}\n${format(d.value)}`);

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

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

    gradient.append("stop")
        .attr("offset", "0%")
        .attr("stop-color", d => color(d.source));

    gradient.append("stop")
        .attr("offset", "100%")
        .attr("stop-color", d => color(d.target));
  }

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

  link.append("title")
      .text(d => `${d.source.name} → ${d.target.name}\n${format(d.value)}`);

  svg.append("g")
      .attr("font-family", "sans-serif")
      .attr("font-size", 10)
    .selectAll("text")
    .data(nodes)
    .join("text")
      .attr("x", d => d.x0 < width / 2 ? d.x1 + 6 : d.x0 - 6)
      .attr("y", d => (d.y1 + d.y0) / 2)
      .attr("dy", "0.35em")
      .attr("text-anchor", d => d.x0 < width / 2 ? "start" : "end")
      .text(d => 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 => d.name)
      .nodeAlign(d3[`sankey${align[0].toUpperCase()}${align.slice(1)}`])
      .nodeWidth(15)
      .nodePadding(10)
      .extent([[1, 5], [width - 1, height - 5]]);
  return ({nodes, links}) => sankey({
    nodes: nodes.map(d => Object.assign({}, d)),
    links: links.map(d => Object.assign({}, d))
  });
}
);
  main.variable(observer("format")).define("format", ["d3","data"], function(d3,data)
{
  const format = d3.format(",.0f");
  return data.units ? d => `${format(d)} ${data.units}` : format;
}
);
  main.variable(observer("color")).define("color", ["d3"], function(d3)
{
  const color = d3.scaleOrdinal(d3.schemeCategory10);
  return d => 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 => [l.source, l.target])), name => ({name, category: name.replace(/ .*/, "")}));
  return {nodes, links, units: "TWh"};
}
);
  main.variable(observer("width")).define("width", function(){return(
954
)});
  main.variable(observer("height")).define("height", function(){return(
600
)});
  main.variable(observer("d3")).define("d3", ["require"], function(require){return(
require("d3@5", "[email protected]")
)});
  return main;
}
</script>

다운로드 된 코드는 웹 페이지의 코드와 약간 다릅니다. 두 버전의 코드를 모두 시도했습니다.

나는 D3에 대해 이해하지 못하는 것이 있다고 생각합니다. 모듈이라는 것에 대해 뭔가? 내 워드 프레스 사이트에 이것을 업로드하는 적절한 방법을 모르겠습니다.

다음은 더 쉽게 할 수있는 사이트의 URL입니다. http://danielb66.sg-host.com/2001-2/

진정시키기 위해

다음은 외부 서버의 런타임 및 파일 첨부를 사용하기 때문에 몇 가지 차이점이있는 예입니다.

귀하의 예에서 다음 줄을 바꿔야합니다

import {Runtime, Inspector} from "https://cdn.jsdelivr.net/npm/@observablehq/runtime@4/dist/runtime.js";

교체 https://cdn.jsdelivr.net/npm/@observablehq/runtime@4/dist/runtime.js./runtime.js다운로드 runtime.js

파일 첨부 URL을 다운로드 한 파일에 상대적인 경로로 바꿉니다.

const fileAttachments = new Map([["energy.csv",new URL("https://static.observableusercontent.com/files/d6774e9422bd72369f195a30d3a6b33ff9d41676cff4d89c93511e1a458efb3cfd16cbb7ce3fecdd8dd2466121e10c9bfe57fd73c7520bf358d352a92b898614",import.meta.url)]]);

마지막으로 런타임과 노트북을로드 한 후 런타임을 초기화하고 HTML 노드 요소에 프로젝트를 마운트해야합니다. 이 예에서는 document.bodydiv 요소 선택기를 전달할 수 있습니다.document.querySelector(".container")

const runtime = new Runtime();
const main = runtime.module(define, Inspector.into(document.body));

<body>

  <script type="module">
    import {Runtime, Inspector} from "https://cdn.jsdelivr.net/npm/@observablehq/runtime@4/dist/runtime.js"; export default function define(runtime, observer) { const main = runtime.module(); const fileAttachments = new Map([["energy.csv",new URL("https://static.observableusercontent.com/files/d6774e9422bd72369f195a30d3a6b33ff9d41676cff4d89c93511e1a458efb3cfd16cbb7ce3fecdd8dd2466121e10c9bfe57fd73c7520bf358d352a92b898614",import.meta.url)]]);
    main.builtin("FileAttachment", runtime.fileAttachments(name => 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 & 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( Object.assign(html`
    <select>
      <option value=input>Color by input
        <option value=output>Color by output
          <option value=path selected>Color by input-output
            <option value=none>No color
    </select>`, { value: new URLSearchParams(html`<a href>`.search).get("color") || "path"
})
)});
  main.variable(observer("edgeColor")).define("edgeColor", ["Generators", "viewof edgeColor"], (G, _) => G.input(_));
  main.variable(observer("viewof align")).define("viewof align", ["html","URLSearchParams"], function(html,URLSearchParams){return(
Object.assign(html`<select>
  <option value=left>Left-aligned
  <option value=right>Right-aligned
  <option value=center>Centered
  <option value=justify selected>Justified
</select>`, {
  value: new URLSearchParams(html`<a href>`.search).get("align") || "justify"
})
)});
  main.variable(observer("align")).define("align", ["Generators", "viewof align"], (G, _) => 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);

  svg.append("g")
      .attr("stroke", "#000")
    .selectAll("rect")
    .data(nodes)
    .join("rect")
      .attr("x", d => d.x0)
      .attr("y", d => d.y0)
      .attr("height", d => d.y1 - d.y0)
      .attr("width", d => d.x1 - d.x0)
      .attr("fill", color)
    .append("title")
      .text(d => `${d.name}\n${format(d.value)}`);

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

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

    gradient.append("stop")
        .attr("offset", "0%")
        .attr("stop-color", d => color(d.source));

    gradient.append("stop")
        .attr("offset", "100%")
        .attr("stop-color", d => color(d.target));
  }

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

  link.append("title")
      .text(d => `${d.source.name} → ${d.target.name}\n${format(d.value)}`);

  svg.append("g")
      .attr("font-family", "sans-serif")
      .attr("font-size", 10)
    .selectAll("text")
    .data(nodes)
    .join("text")
      .attr("x", d => d.x0 < width / 2 ? d.x1 + 6 : d.x0 - 6)
      .attr("y", d => (d.y1 + d.y0) / 2)
      .attr("dy", "0.35em")
      .attr("text-anchor", d => d.x0 < width / 2 ? "start" : "end")
      .text(d => 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 => d.name)
      .nodeAlign(d3[`sankey${align[0].toUpperCase()}${align.slice(1)}`])
      .nodeWidth(15)
      .nodePadding(10)
      .extent([[1, 5], [width - 1, height - 5]]);
  return ({nodes, links}) => sankey({
    nodes: nodes.map(d => Object.assign({}, d)),
    links: links.map(d => Object.assign({}, d))
  });
}
);
  main.variable(observer("format")).define("format", ["d3","data"], function(d3,data)
{
  const format = d3.format(",.0f");
  return data.units ? d => `${format(d)} ${data.units}` : format;
}
);
  main.variable(observer("color")).define("color", ["d3"], function(d3)
{
  const color = d3.scaleOrdinal(d3.schemeCategory10);
  return d => 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 => [l.source, l.target])), name => ({name, category: name.replace(/ .*/, "")}));
  return {nodes, links, units: "TWh"};
}
);
  main.variable(observer("width")).define("width", function(){return(
954
)});
  main.variable(observer("height")).define("height", function(){return(
600
)});
  main.variable(observer("d3")).define("d3", ["require"], function(require){return(
require("d3@5", "[email protected]")
)});
  return main;
}

const runtime = new Runtime();
const main = runtime.module(define, Inspector.into(document.body));
  
  
  </script>
</body>

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

하위 컨텍스트 (예 : 타이머) 내에서 d3 노드 선택

분류에서Dev

내 워드 프레스 사이트 내 FTP 서버의 손상된 파일

분류에서Dev

내 워드 프레스 사이트의 특정 페이지에서 색상을 편집하는 데 문제가 있습니다.

분류에서Dev

워드 프레스 사이트에서 사이드 바 제거

분류에서Dev

내 워드 프레스 페이지의 자바 스크립트

분류에서Dev

내 워드 프레스 테마의 문자 문제

분류에서Dev

PHP 워드 프레스 내부의 자바 스크립트

분류에서Dev

워드 프레스 사이트 맵에서 페이지 제외

분류에서Dev

내 첫 아이 테마 워드 프레스-색상 문제

분류에서Dev

문의 양식 7이 내 워드 프레스 사이트에 없습니까? 하지만 빈 페이지에서 작동합니다. 내 코드에 문제가 있습니다.

분류에서Dev

URL 및 워크 시트 ID를 사용하여 스프레드 시트 내의 워크 시트에 액세스

분류에서Dev

라인 변환, 워드 프레스의 에코 내 에코

분류에서Dev

워드 프레스 내의 BXSlider

분류에서Dev

워드 프레스 내의 BXSlider

분류에서Dev

Hobbelt의 "그룹 / 번들 노드"D3 강제 레이아웃 예제에서 노드에 레이블을 추가 하시겠습니까?

분류에서Dev

워드 프레스 단축 코드 내에있는 HTML 내의 PHP

분류에서Dev

워드 프레스에서 내 웹 사이트를 시작하는 방법

분류에서Dev

워드 프레스에서 업로드 된 이미지의 크기 제한

분류에서Dev

하나의 호스트에서 여러 워드 프레스 사이트

분류에서Dev

워드 프레스에서 게시물의 내용을 분리하는 방법

분류에서Dev

네트워크 로그에 워드 프레스 사이트 내의 폴더로드 시작시 404가 표시됨

분류에서Dev

새 워드 프레스 사이트에서 이상한 URL 재 작성 문제

분류에서Dev

워드 프레스 제어판에서 텍스트를 사용자 정의하는 방법

분류에서Dev

워드 프레스 포스트에서 목록 꺼내기

분류에서Dev

D3 방향 그래프의 경계에서 노드 이동 제한

분류에서Dev

워드 프레스에서 사용자 메타 용으로 예약 된 키-내 정보

분류에서Dev

워드 프레스에서 내비게이션 바의 레이아웃 변경하기

분류에서Dev

내 워드 프레스 사이트에서 축소 버튼을 수동으로 제어하려면 어떻게해야합니까?

분류에서Dev

Force 레이아웃의 d3 노드에 텍스트 레이블 추가

Related 관련 기사

  1. 1

    하위 컨텍스트 (예 : 타이머) 내에서 d3 노드 선택

  2. 2

    내 워드 프레스 사이트 내 FTP 서버의 손상된 파일

  3. 3

    내 워드 프레스 사이트의 특정 페이지에서 색상을 편집하는 데 문제가 있습니다.

  4. 4

    워드 프레스 사이트에서 사이드 바 제거

  5. 5

    내 워드 프레스 페이지의 자바 스크립트

  6. 6

    내 워드 프레스 테마의 문자 문제

  7. 7

    PHP 워드 프레스 내부의 자바 스크립트

  8. 8

    워드 프레스 사이트 맵에서 페이지 제외

  9. 9

    내 첫 아이 테마 워드 프레스-색상 문제

  10. 10

    문의 양식 7이 내 워드 프레스 사이트에 없습니까? 하지만 빈 페이지에서 작동합니다. 내 코드에 문제가 있습니다.

  11. 11

    URL 및 워크 시트 ID를 사용하여 스프레드 시트 내의 워크 시트에 액세스

  12. 12

    라인 변환, 워드 프레스의 에코 내 에코

  13. 13

    워드 프레스 내의 BXSlider

  14. 14

    워드 프레스 내의 BXSlider

  15. 15

    Hobbelt의 "그룹 / 번들 노드"D3 강제 레이아웃 예제에서 노드에 레이블을 추가 하시겠습니까?

  16. 16

    워드 프레스 단축 코드 내에있는 HTML 내의 PHP

  17. 17

    워드 프레스에서 내 웹 사이트를 시작하는 방법

  18. 18

    워드 프레스에서 업로드 된 이미지의 크기 제한

  19. 19

    하나의 호스트에서 여러 워드 프레스 사이트

  20. 20

    워드 프레스에서 게시물의 내용을 분리하는 방법

  21. 21

    네트워크 로그에 워드 프레스 사이트 내의 폴더로드 시작시 404가 표시됨

  22. 22

    새 워드 프레스 사이트에서 이상한 URL 재 작성 문제

  23. 23

    워드 프레스 제어판에서 텍스트를 사용자 정의하는 방법

  24. 24

    워드 프레스 포스트에서 목록 꺼내기

  25. 25

    D3 방향 그래프의 경계에서 노드 이동 제한

  26. 26

    워드 프레스에서 사용자 메타 용으로 예약 된 키-내 정보

  27. 27

    워드 프레스에서 내비게이션 바의 레이아웃 변경하기

  28. 28

    내 워드 프레스 사이트에서 축소 버튼을 수동으로 제어하려면 어떻게해야합니까?

  29. 29

    Force 레이아웃의 d3 노드에 텍스트 레이블 추가

뜨겁다태그

보관