【发布时间】: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