Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript D3示例来自我的wordpress站点上的Observable_Javascript_Wordpress_D3.js_Observablehq - Fatal编程技术网

Javascript D3示例来自我的wordpress站点上的Observable

Javascript D3示例来自我的wordpress站点上的Observable,javascript,wordpress,d3.js,observablehq,Javascript,Wordpress,D3.js,Observablehq,非常感谢您事先的帮助 我正试图在我的wordpress网站上添加这个Sankey图表 我的日子不好过。我下载了JS代码,粘贴在我的wordpress网站上,但它给了我很多错误 Uncaught SyntaxError: Unexpected token 'export' 是来自浏览器的第一个错误 下面是我下载的代码: <script src="https://d3js.org/d3.v5.js"></script> <script> // https://

非常感谢您事先的帮助

我正试图在我的wordpress网站上添加这个Sankey图表

我的日子不好过。我下载了JS代码,粘贴在我的wordpress网站上,但它给了我很多错误

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", "d3-sankey@0.12")
)});
  return main;
}
</script>

// https://observablehq.com/@d3/桑基-diagram@273
导出默认函数定义(运行时、观察者){
const main=runtime.module();
const fileAttachments=newmap([“energy.csv”,新URL(“./files/D6774E9422BD72369F195A30D3A6B33FF9D41676CFF4D89C93511A458EFB3CFD16CBB7CE3FECD8DD2466121E10C9BFE57FD73C7520BF358D352A92B898614”,import.meta.URL)];
main.builtin(“FileAttachment”,runtime.fileAttachments(name=>fileAttachments.get(name));
变量(observer()).define([“md”],函数(md){return(
md`#桑基图
这是[桑基图](https://github.com/d3/d3-sankey)可视化能源流动:*供应*在左边,需求*在右边。链接显示不同数量的能源在消耗或损耗之前是如何转换或传输的。数据:[能源和气候变化部](http://www.decc.gov.uk/en/content/cms/tackling/2050/calculator_on/calculator_on.aspx)经由[汤姆·康塞尔](https://tamc.github.io/Sankey/)
`
)});
main.variable(observer(“viewof edgeColor”)).define(“viewof edgeColor”、[“html”、“URLSearchParams”]、function(html、URLSearchParams){return(
Object.assign(html)`
输入颜色
输出颜色
输入输出颜色
没有颜色
`, {

value:newURLSearchParams(html`

这里是一个示例,有几个不同之处,因为这里我们使用的是来自外部服务器的运行时和文件附件

在您的示例中,必须替换以下行

从导入{Runtime,Inspector}”https://cdn.jsdelivr.net/npm/@可观察EHQ/runtime@4/dist/runtime.js”;
替换
https://cdn.jsdelivr.net/npm/@可观察EHQ/runtime@4/dist/runtime.js
/runtime.js
下载的runtime.js

将文件附件URL替换为相对于下载文件的路径

constfileattachments=newmap([[“energy.csv”),newurl(“https://static.observableusercontent.com/files/d6774e9422bd72369f195a30d3a6b33ff9d41676cff4d89c93511e1a458efb3cfd16cbb7ce3fecdd8dd2466121e10c9bfe57fd73c7520bf358d352a92b898614“,import.meta.url)]);
最后,在加载运行时和笔记本之后,您必须初始化运行时并在HTML节点元素上装载项目。在本例中,
document.body
,但您可以传递任何div元素选择器,即
document.querySelector(.container”)

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

从导入{Runtime,Inspector}”https://cdn.jsdelivr.net/npm/@可观察EHQ/runtime@4/dist/runtime.js“导出默认函数define(runtime,observer){const main=runtime.module();const fileAttachments=newmap([[“energy.csv”,新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图此[Sankey图](https://github.com/d3/d3-sankey)可视化流程
能源:*供应*在左边,需求*在右边。链接显示不同数量的能源在消耗或损耗之前是如何转换或传输的。数据:[能源和气候变化部](http://www.decc.gov.uk/en/content/cms/tackling/2050/calculator_on/calculator_on.aspx)
经由[汤姆·康塞尔](https://tamc.github.io/Sankey/)`)};main.variable(observer(“viewof edgeColor”))。定义(“viewof edgeColor”、[“html”、“URLSearchParams”]、函数(html,URLSearchParams){return(Object.assign(html`
输入颜色
输出颜色
输入输出颜色
没有颜色
`,{value:newURLSearchParams(html``.search.get(“color”)| |“path”
})
)});
主变量(观察者(“edgeColor”))。定义(“edgeColor”、[“生成器”、“edgeColor视图”]、(G,)=>G.input());
main.variable(observer(“viewof align”)).define(“viewof align”、[“html”、“URLSearchParams”]、function(html、URLSearchParams){return(
Object.assign(html)`
左对齐
右对齐
居中的
正当的
`, {
值:新建URLSearchParams(html`.search).get(“align”)| |“justify”
})
)});
main.variable(observer(“align”)。定义(“align”、[“Generators”、“viewof align”]、(G,)=>G.input());
主变量(观察者(“图表”))。定义(“图表”、“d3”、“宽度”、“高度”、“sankey”、“数据”、“颜色”、“格式”、“edgeColor”、“DOM”)、函数(d3、宽度、高度、sankey、数据、颜色、格式、edgeColor、DOM)
{
const svg=d3.create(“svg”)
.attr(“视图框”[0,0,宽度,高度]);
const{nodes,links}=sankey(数据);
svg.append(“g”)
.attr(“笔划”,“千”)
.selectAll(“rect”)
.数据(节点)
.join(“rect”)
.attr(“x”,d=>d.x0)
.attr(“y”,d=>d.y0)
.attr(“高度”,d=>d.y1-d.y0)
.attr(“宽度”,d=>d.x1-d.x0)
.attr(“填充”,颜色)
.附加(“标题”)
.text(d=>`${d.name}\n${format(d.value)}`);
const link=svg.append(“g”)
.attr(“填充”、“无”)
.attr(“笔划不透明度”,0.5)
.全选(“g”)
.数据(链接)
.加入(“g”)
.style(“混合模式”、“倍增模式”);
如果(edgeColor==“路径”){
常量梯度=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);
渐变。附加(“停止”)
.attr(“偏移量”、“0%”)
.attr(“停止颜色”,d=>color(d.source));
坡度