【问题标题】:Working Zoom/Pan example with D3.js?使用 D3.js 进行缩放/平移示例?
【发布时间】:2013-07-26 01:30:42
【问题描述】:

我正在尝试让这个示例正常运行 (http://bl.ocks.org/mbostock/2206340),但出现错误(如下)。我尝试运行的代码与链接中的代码完全相同,但有一个例外,由于我没有在本地提供 json 文件,因此必须修改 json 位置。

我也尝试了几个 topojson 示例,但也遇到了错误。我不确定这是 API 版本问题还是什么。任何想法如何让这个工作或有人可以启发我如何调试这个问题?我对 D3 很陌生。

更新:添加错误

   GET http://bl.ocks.org/mbostock/raw/4090846/us.json
    200 OK
            1.29s   
    d3.v3.min.js (line 1)
    TypeError: us is undefined
    [Break On This Error]   
    .data(topojson.feature(us, us.objects.states).features)

更新:添加代码

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.background {
  fill: none;
  pointer-events: all;
}

#states {
  fill: #aaa;
}

#state-borders {
  fill: none;
  stroke: #fff;
  stroke-width: 1.5px;
  stroke-linejoin: round;
  stroke-linecap: round;
  pointer-events: none;
}

</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script>

var width = 960,
    height = 500;

var projection = d3.geo.albersUsa()
    .scale(1070)
    .translate([width / 2, height / 2]);

var path = d3.geo.path()
    .projection(projection);

var zoom = d3.behavior.zoom()
    .translate(projection.translate())
    .scale(projection.scale())
    .scaleExtent([height, 8 * height])
    .on("zoom", zoomed);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

var g = svg.append("g")
    .call(zoom);

g.append("rect")
    .attr("class", "background")
    .attr("width", width)
    .attr("height", height);

d3.json("http://bl.ocks.org/mbostock/raw/4090846/us.json", function(error, us) {
  g.append("g")
      .attr("id", "states")
    .selectAll("path")
      .data(topojson.feature(us, us.objects.states).features)
    .enter().append("path")
      .attr("d", path)
      .on("click", clicked);

  g.append("path")
      .datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
      .attr("id", "state-borders")
      .attr("d", path);
});

function clicked(d) {
  var centroid = path.centroid(d),
      translate = projection.translate();

  projection.translate([
    translate[0] - centroid[0] + width / 2,
    translate[1] - centroid[1] + height / 2
  ]);

  zoom.translate(projection.translate());

  g.selectAll("path").transition()
      .duration(700)
      .attr("d", path);
}

function zoomed() {
  projection.translate(d3.event.translate).scale(d3.event.scale);
  g.selectAll("path").attr("d", path);
}

</script>

【问题讨论】:

  • 这个错误基本上是告诉你JSON文件不存在。
  • 您使用的 d3 版本是否正确?如果您不发布一些代码,将很难为您提供帮助。
  • “错误”包含什么?
  • 好的。原始问题中发布的代码 - 以及错误消息。不知道它是否是正确的 d3 版本,但我会假设我是从 bl.ocks 网站直接粘贴的。

标签: d3.js


【解决方案1】:

如果您在 localhost 上运行代码,并以您发布的方式引用它,那么您将收到“Access-Control-Allow-Origin”错误。

如果你把代码放在一些要点上,它会正常工作的。

【讨论】:

  • 谢谢 - 就是这样!我下载了 json 文件并将其放在与示例文件相同的目录中,它可以工作。那么所有这些json文件都需要在javascript本地吗?
  • d3.json() 正在发出 XMLHttpRequest(ajax 请求),并且由于浏览器的同源策略,您只能向同一个域发出请求——我在这里进行了简化。因此,您所有的 JSON 文件都需要在同一台服务器上;在这种情况下,都在您的本地主机上。
猜你喜欢
  • 2013-01-17
  • 1970-01-01
  • 2017-01-29
  • 2013-10-05
  • 1970-01-01
  • 2020-10-20
  • 1970-01-01
  • 1970-01-01
  • 2018-03-03
相关资源
最近更新 更多