javascript - 无法在 d3 中添加过渡

标签 javascript css d3.js

我想为我在d3 中设计的垂直条添加过渡效果。我是 d3 的新手,我尝试添加 transition() 方法但没有安静的工作。我的代码如下-

svg.selectAll(".bar")
            .data(data)
            .enter().append("g")
            .attr("class", "bar")
            .append("rect")
            .attr("rx", barRadius)
            .attr("fill", function(d,i) {
                 var drilledvalue;
                    try {
                        drilledvalue = JSON.parse(parent.$("#drills").val())[colIds[0]];
                    } catch (e) {
                    }
                    if (typeof drilledvalue !== 'undefined' && drilledvalue.length > 0 && drilledvalue.indexOf(d[columns[0]]) !== -1) {
                        return drillShade;
                    }
                if(typeof chartData[div]["transpose"] === "undefined" || chartData[div]["transpose"]=='N')
                {
                return getDrawColor(div, parseInt(i));//"url(#gradientBar_" + (d[columns[0]]).replace(/[^a-zA-Z0-9]/g, '', 'gi') + ")";
                }
                else
                {
                    return color(0);
                }
            })
            // .attr("color_value", "steelblue")
            .attr("index_value", function(d, i) {
                return "index-" + d[columns[0]].replace(/[^a-zA-Z0-9]/g, '', 'gi');
            })
            .attr("class", function(d, i) {
                return "bars-Bubble-index-" + d[columns[0]].replace(/[^a-zA-Z0-9]/g, '', 'gi')+div;
            })
            .attr("id", function(d) {
                return d[columns[0]] + ":" + d[measure1];
            })
            .attr("onclick", fun)
            .attr("x", function(d) {
                return x(d[columns[0]]);
            })
            .attr("width", x.rangeBand())
            .attr("y", function(d) {
                return y(d[measure1]);
            })
            .attr("height", function(d) {
                return height - y(d[measure1]);
            });

我希望条形图一次出现在一张图的下方。请帮助。

最佳答案

没有实例,很难帮到你。但是,看看您的代码,您应该将初始高度设置为 0,然后在转换后设置最终高度:

svg.selectAll(".bar")
//all settings
.attr("height",0)
.transition()
.duration(1000)//1 second
.attr("height",function(d)( return height - y(d[measure1]);));

编辑:

抱歉,当然是从上面来的,你需要旋转条。此外,您可能需要在应用旋转后重新评估高度计算

svg.selectAll(".bar")
    //all settings
    .attr("height",0)
    .attr("transform", "rotate(180,x,y)"); //note y must be the bottom of the chart
    .transition()
    .duration(1000)//1 second
    .attr("height",function(d)( return height - y(d[measure1]);));

关于javascript - 无法在 d3 中添加过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30182167/

相关文章:

javascript - 如何用纯 javascript 和 html 制作 Accordion

javascript - 如何在 d3js 中制作基本柱形(垂直)图表?

javascript - jQuery动态调整div的大小

javascript - 多个 Angular 5 组件操纵一个中心组件

javascript - 如何创建一个先快后慢的间隔?

javascript - 在 NodeJs 端点中引用 JSON 对象时未定义“id”

html - 使用内联时所有浏览器中的楼梯影响问题

javascript - 使用以下布局设置跨浏览器兼容的 div 结构

javascript - 插值错误

javascript - 使用 D3 JS 在桑基图中显示百分比