简体   繁体   English

d3.js地图无法在Internet Explorer或Firefox上呈现,仅Chrome

[英]d3.js Map not rendering on Internet Explorer or Firefox, Only Chrome

The path elements d attribute is where the problem is at. 路径元素d属性是问题所在。 When looking at the console in Internet Explorer it explicitly mentions 在Internet Explorer中查看控制台时,它明确提到

SVG4601: SVG Path data has incorrect format and could not be completely parsed. SVG4601:SVG路径数据格式不正确,无法完全解析。

It doesn't seem to be the geojson I'm using as it works in the following fiddle: http://jsfiddle.net/t9sd08q7/1/ 它似乎不是我正在使用的geojson,因为它在以下小提琴中起作用: http : //jsfiddle.net/t9sd08q7/1/

Here are the outputs when inspecting into the dom elements on each browser. 这是检查每个浏览器上dom元素时的输出。

Chrome : 镀铬 chrome_paths

Internet Explorer : IE浏览器 :

internetexplorer_paths

Firefox : 火狐: 在此处输入图片说明

This is how I'm generating my path: 这就是我生成路径的方式:

 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;
 }

I pass the "groupOfSvgElements" to another method wh 我将“ groupOfSvgElements”传递给另一种方法

Extracted code that I'm using to generate the d3.js map (inside an Angularjs directive). 我用于生成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;
 }

My json file - don't really know how to add it to a fiddle or share it here. 我的json文件-真的不知道如何将其添加到小提琴或在此处共享。 http://jsonblob.com/5595338ce4b051e806c87b42 http://jsonblob.com/5595338ce4b051e806c87b42

SVG4601: SVG Path data has incorrect format and could not be completely parsed. SVG4601:SVG路径数据格式不正确,无法完全解析。

There can be many reasons the following bug occurs, in my case it was how the width was being passed to the path construction. 出现以下错误的原因可能有很多,在我的情况下,这是宽度如何传递到路径构造的原因。 It seems that Chrome is very lenient on a lot more than Internet Explorer. Chrome似乎比Internet Explorer宽容得多。

The best way to debug this problem (as suggested by Ethan Jewett in the above comments) was to use the same geojson in a jsfiddle which I uploaded to myjson.com. 调试此问题的最佳方法(如上述评论中的Ethan Jewett所建议)是在我上载到myjson.com的jsfiddle中使用相同的geojson。 Snippet of the code that uses it 使用它的代码片段

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

I think this problem only occurs because SVG isn't really regulated across browsers as well as it should be. 我认为只会出现此问题是因为SVG并没有真正受到跨浏览器的监管。 There's not really much error information either as to what is causing the problem. 关于导致问题的原因,实际上并没有太多错误信息。 There was no error that the width was invalid for the path generation. 不会出现宽度对于生成路径无效的错误。 Only way to debug this was to add bits of my own code to the jsfiddle little by little. 调试此问题的唯一方法是一点一点地将自己的代码添加到jsfiddle中。 Final Jsfiddle : http://jsfiddle.net/t9sd08q7/11/ 最终的Jsfiddle: http : //jsfiddle.net/t9sd08q7/11/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM