4

我如何在 g.raphael barchart 中为 raphaeljs 条分配 ID,以及之后如何访问它们

var r = Raphael("holder", 600, 500);
var data=[1,3,4,5];
var chart = r.g.barchart(30, 30, 350, 250, [data], {stacked: true, type: "soft"});
for (var i = 0; i < chart.bars[0].length; i++) {
      var bar = chart.bars[0][i];
      if (bar.value >= 7) {
                bar.attr("fill", "#bf2f2f");
                bar.attr("stroke", "#bf2f2f");
                bar.attr("id","id-"+i);   //this doesn't work
                bar.id="id-"+i;           //this also doesn't work
                //applied as per raphaeljs documentatoion 
                //[http://raphaeljs.com/reference.html#Element.id][1]
                   }
        }
4

2 回答 2

5

尝试

bar[0].id = "id-"+i;
//or
bar.node.id = "id-"+i;

bar是拉斐尔元素。它的[0]node属性指向实际的 DOM 元素

于 2011-12-15T18:40:08.740 回答
1

你可以试试这个

bar.data("id" : +i)

更多信息:http ://raphaeljs.com/reference.html#Element.data

于 2011-12-15T18:45:04.883 回答