将.attr()添加到变量Jquery中

Adding the .attr() to a variable - Jquery

本文关键字:变量 Jquery 添加 attr      更新时间:2023-09-26

我使用的是Rickshaw框架,该框架使用D3进行可视化。。我正在创建一个变量节点,在该节点上附加矩形。只要满足特定条件,我就希望Padding为20。

 var nodes = graph.vis.selectAll("path").data(series.stack.filter(function(d) {
                    return d.y !== null
                })).enter().append("svg:rect").attr("x", function(d) {
   console.log(brandCount[v])
    if(d.x == brandCount[v]) {
        console.log("called2")
        v++;
        var z = 0;
        padding = 10;
    } else {
        padding = 0;
    }.attr("y", function(d) {
                    return (graph.y(d.y0 + Math.abs(d.y))) * (d.y < 0 ? -1 : 1 )
                }).attr("width", 30).attr("height", function(d) {
                    return graph.y.magnitude(Math.abs(d.y))
                }).attr("rx", 4).attr("transform", transform); 

我应该在这里做些什么来访问变量并向其添加属性?

品牌计数,数组有很多值([1, 4, 1, 4, 1, 6, 1, 5, 1, 1, 1, 1, 1, 6, 1, 1, 1, 6, 4, 1, 2, 2, 1, 1, 1, 5, 8, 4, 1, 6, 3, 1, 9, 1, 1, 4, 2, 1, 1, 5, 1, 1, 1, 7, 1, 1, 2, 3, 1, 2, 2, 1, 1, 2, 1, 3, 2, 1, 1, 1, 1, 5, 1, 1, 1, 1, 7, 1, 1, 2, 6, 1, 6, 1, 3, 2, 2, 1, 4, 2, 1])。在每一个值之后,我都需要一个空格。我只是试着循环它..作为brand_count[v],并将其增加v++。

您需要在设置x属性的函数内评估您的条件,即

.attr("x", function(d) {
    if(z == brand_Count[v]) {
        v++;
        z = 0;
        padding = 10;
    } else {
        padding = 0;
    }
    return graph.x(d.x) + padding;
}

对于在5个值之后添加填充,您不需要单独的计数器:

.attr("x", function(d, i) {
    return graph.x(d.x) + (Math.floor(i/5)*20);
}

对于在特定值后添加填充,可以执行以下操作:

var counter = 0;
// ...more code...
.attr("x", function(d) {
    if(d == 1 || d == 4) counter++;
    return graph.x(d.x) + (counter*20);
}

如果值存储在数组中:

var counter = 0, brandCount = [1,2,3];
// ...more code...
.attr("x", function(d) {
    if(brandCount.indexOf(d) != -1) counter++;
    return graph.x(d.x) + (counter*20);
}