简体   繁体   English

将图像放在折线图中的Y轴标签上

[英]Place image on Y-axis labels in Line chart

fiddler link is = http://jsfiddle.net/2hfx3/61/ 小提琴手链接是= http://jsfiddle.net/2hfx3/61/

I'm using D3.js line chart i just want to place image on y-axis here is my code snipped where image are placed on x-axis but when i try to change xAxis to yAxis happened nothing. 我正在使用D3.js折线图,我只想将图像放置在y轴上,这是我的代码片段,其中图像放置在x轴上,但是当我尝试将xAxis更改为yAxis时,什么也没发生。 Please help. 请帮忙。

svg.append("svg:g")
.attr("class", "x axis")
.attr("transform", "translate(0, " + height + ")")
.call(xAxis)
.selectAll(".tick").each(function(d,i){        
    d3.select(this)
      .append('image')
      .attr('xlink:href', data[i].img)
      .attr('x',0)
      .attr('width',128)
      .attr('height',128);
});

Here's a corrected fiddle : http://jsfiddle.net/2hfx3/104/ 这是一个更正的小提琴: http : //jsfiddle.net/2hfx3/104/

The problem is simple, their is 12 items in your data but their is 14 ticks on your yAxis. 问题很简单,它们在您的数据中是12个项目,但是在yAxis上是14个刻度。 The error happens because when it is loading the image on the 13th tick, it is trying to load the 13th item which does not exist so their is an error and your Line Chart stop drawing. 发生错误的原因是,当它在第13个刻度上加载图像时,它试图加载不存在的第13个项目,因此它们是错误的,并且折线图停止绘制。

Here's what you can do : 您可以执行以下操作:

1) Append as many ticks in your yAxis as you have items in your data 1)在yAxis中添加与数据中包含项一样多的刻度

2) Change your data to have as many items as there is ticks in your yAxis 2)更改数据以使其与yAxis中的滴答声一样多

3) Load directly the image without reading data, like this: (I also modified x and y coordinates of the image to match your example) 3)直接加载图像而不读取数据,如下所示:(我还修改了图像的x和y坐标以匹配您的示例)

svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.selectAll(".tick").each(function(d,i){        
    d3.select(this)
      .append('image')
      .attr('xlink:href', img)
      .attr('x',0 - 128)
      .attr('y',0 - 128)
      .attr('width',128)
      .attr('height',128);
});

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

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