[英]d3.js radar chart with images provided in data
i'm trying to use the D3.js library together with the block of nbremer. 我正在尝试将D3.js库与nbremer块一起使用。 My problem is that the image I provide in the index.html ( "Picture1.png" for example ) is not displayed on the axis of the radar chart. 我的问题是我在index.html中提供的图像(例如“Picture1.png”)没有显示在雷达图的轴上。 At the momement I'm using a dummy image as you can see in radarchart.js But I want to use the image that is provided in the data in the index.html The img property to be exact. 我正在使用虚拟图像,你可以在radarchart.js中看到,但我想使用index.html中数据中提供的图像。确切地说,img属性。
Can anyone give me an idea / push in the right direction? 谁能给我一个想法/推动正确的方向? Thank you in advanced! 先谢谢你!
Here is a jsfiddle: fiddle 这是一个小提琴: 小提琴
//Append the icon
axis.append("svg:image")
.attr("class", "legend")
.style("font-size", "11px")
.attr("text-anchor", "middle") //
.attr("xlink:href", "http://dummyimage.com/60x60/000/ffffff.png")
.attr("x", function(d, i) {
return rScale(maxValue * cfg.labelFactor) * Math.cos(angleSlice * i - Math.PI / 2);
})
.attr("y", function(d, i) {
return rScale(maxValue * cfg.labelFactor) * Math.sin(angleSlice * i - Math.PI / 2);
})
.attr("width", 60)
.attr("height", 60);
This is where I load the dummy image. 这是我加载虚拟图像的地方。
To provide an image based on the data
array we'll have to filter the data, once the axes are using a different data array (called allAxis
, and also based on data
). 为了提供基于data
阵列的图像,一旦轴使用不同的数据阵列(称为allAxis
,并且还基于data
),我们将必须过滤data
。 Of course, a cleaner solution would be simply pushing the images together into allAxis
, but let's see how to link them using data
. 当然,更简洁的解决方案就是将图像一起推送到allAxis
,但让我们看看如何使用data
链接它们。
This is the snippet with the relevant modification: 这是相关修改的片段:
axis.append("svg:image")
.attr("xlink:href", function(d){
var image = data[0].filter(function(e) {
return e.axis == d;
});
return image[0].img
})
Here, for every value in axis
, we filter the data
array (I'm using data[0]
since your data array is repetitive) accordingly. 在这里,对于axis
每个值,我们相应地过滤data
数组(我使用data[0]
因为您的数据数组是重复的)。
Notice that, once we are returning the value for img
key, the key has to contain the entire path for that image (if it is in a different directory). 请注意,一旦我们返回img
键的值,该键必须包含该映像的整个路径 (如果它位于不同的目录中)。 Alternatively, you can do: 或者,你可以这样做:
return "http://www.somewebsite.com/" + image[0].img
If the img
key contains only the name of the file (and, again, if the file is in another directory/domain). 如果img
密钥仅包含文件的名称(并且,如果文件位于另一个目录/域中,则再次)。
Here is your fiddle with different images for each axis: https://jsfiddle.net/gerardofurtado/33k6m7tj/ 这是每个轴的不同图像的小提琴: https : //jsfiddle.net/gerardofurtado/33k6m7tj/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.