【问题标题】:d3.js Map not rendering on Internet Explorer or Firefox, Only Chromed3.js 地图无法在 Internet Explorer 或 Firefox 上呈现,只有 Chrome
【发布时间】:2015-07-02 10:20:13
【问题描述】:

路径元素 d 属性是问题所在。在 Internet Explorer 中查看控制台时,它明确提到了

SVG4601:SVG 路径数据的格式不正确,不能 完全解析。

它似乎不是我正在使用的 geojson,因为它在以下小提琴中工作:http://jsfiddle.net/t9sd08q7/1/

这是检查每个浏览器上的 dom 元素时的输出。

铬:

Internet Explorer:

火狐:

这就是我生成路径的方式:

 function initialSetup() {
     internalScope.svg.selectAll("path").transition().duration(500).style("opacity", 0).remove();

     internalScope.width = width = internalScope.svg[0][0].offsetWidth;
     internalScope.height = height = 800;
     projection = d3.geo.albers()
                        .rotate(4.4, 0)
                        .parallels([50, 60])
                        .scale(1)
                        .translate([0, 0]);
     path = d3.geo.path()
                  .projection(projection); // default path based on our projection

     internalScope.svg.attr("height", "100%")
                      .attr("viewBox", "0 0 " + width + " " + height)
                      .attr("preserveAspectRatio", "xMidYMid meet"); // prevent default height messing up svg

     // Append an outer rectangle so when it's clicked it will also reset back to all features view
     internalScope.svg.append("rect")
         .attr("class", "background")
         .attr("width", "100%")
         .attr("height", "100%")
         .on("click", function () {
             //TODO clicking the empty space could potentially do some sort of reset on everything ?
         })
     ;

     var groupofSvgElements = internalScope.svg.append("g");

     return groupofSvgElements;
 }

我将“groupOfSvgElements”传递给另一个方法

我用来生成 d3.js 映射的提取代码(在 Angularjs 指令内)。

function renderCountries(matchCounts) {

     var promise = processJson(internalScope.json.uk_countries).then(function (json) {
         if (matchCounts) {
             json.features = matchFeaturesToLocationsWithCounts(json.features);
         }

         var projectionProperties = generateScaleTranslate(json, path, width, height);

         projection
             .scale(projectionProperties.scale)
             .translate(projectionProperties.translate);

         internalScope.groupofSvgElements.selectAll()
             .data(json.features)
             .enter()
             .append("path")
             .attr("id", function (d) {
                 if (d.properties.Name === 'Ireland') {
                     return "Ireland";
                 } else {
                     return "country";
                 }
             })
             .attr("class", function (d) {
                 if (isNaN(d.properties.Total) || d.properties.Total == 0) { // this covers instances where the capita average is NaN because of the maths or if the total job counts are just 0
                     return "map-feature map-shade-0";
                 } else {
                     return "map-feature " + internalScope.quantizeMethod(d.properties.Total);
                 }
             })
             .attr("d", path)
             .on("click", function (d) {
                 sendStats(d.properties);
                 updateLineChart(d);
                 internalScope.$apply();
                 console.log("hello, you clicked a Country with the count " + d.properties.Total);
             });
     }, function (error) {
         $log.error("Error when procession json file - Error : " + error);
     });

     return promise;
 }

我的 json 文件 - 真的不知道如何将它添加到小提琴或在这里分享。 http://jsonblob.com/5595338ce4b051e806c87b42

【问题讨论】:

  • 路径计算在 IE 和 FF 上出现问题。你能分享你的代码,尤其是填充“d”属性的代码吗?
  • 致对我的帖子进行-1 的人,您至少可以礼貌地解释一下原因吗?难道我没有付出足够的努力来问我的问题吗?我看不出这个问题有什么问题,看起来相当简洁明了,你能告诉我哪里出了问题吗?
  • 该代码没有帮助,因为它没有显示path 变量的计算方式。我们需要了解为什么在不同的浏览器中“d”属性的填充方式不同。最好是创建一个 JSFiddle。 (我怀疑缺少一个可行的例子是 -1 投票的原因。)
  • 好消息,它不是 geojson 文件 (yayy) - jsfiddle.net/t9sd08q7/1 ...坏消息,我仍然不知道它是什么,有点希望它是某种格式在geojson文件上。在 Internet Explorer 中测试,它可以工作..
  • @EthanJewett 我误读了你的评论。我没有意识到如何计算路径很重要,请稍等,让我从我的代码中获取它并尝试将它也添加到小提琴中。

标签: javascript html svg d3.js geojson


【解决方案1】:

SVG4601:SVG 路径数据格式不正确,无法完全解析。

发生以下错误的原因可能有很多,在我的情况下,这是宽度传递给路径构造的方式。似乎 Chrome 比 Internet Explorer 更宽松。

调试此问题的最佳方法(如上述 cmets 中的 Ethan Jewett 所建议)是在我上传到 myjson.com 的 jsfiddle 中使用相同的 geojson。使用它的代码片段

d3.json("https://api.myjson.com/bins/4wkia", function (error, json) {

我认为这个问题的出现只是因为 SVG 并没有像它应该的那样在浏览器之间真正受到监管。关于导致问题的原因,并没有太多的错误信息。宽度对于路径生成无效,没有错误。调试它的唯一方法是一点一点地将我自己的代码添加到 jsfiddle 中。最后的 Jsfiddle : http://jsfiddle.net/t9sd08q7/11/

【讨论】:

  • offsetWidth 和 clientWidth 来自 CSSOM,它们确实不是非常跨浏览器,但它们不是 SVG。
猜你喜欢
  • 2013-09-27
  • 1970-01-01
  • 1970-01-01
  • 2019-05-03
  • 1970-01-01
  • 1970-01-01
  • 2013-01-26
  • 2013-01-01
  • 1970-01-01
相关资源
最近更新 更多