【问题标题】:AngularJS + D3 Animations Bar Chart DirectiveAngularJS + D3 动画条形图指令
【发布时间】:2014-01-25 13:04:03
【问题描述】:

我有一个来自this example 的 AngularJS 指令。

我已经阅读了一些关于制作转换图表的教程like this one 和这个stackoverflow issue,但没有运气。

我的图表非常基本,看起来像:

下面是我的指令代码,关于为什么这些转换示例不起作用的任何想法?

       var width = 960 - opts.margin.left - opts.margin.right,
            height = 500 - opts.margin.top - opts.margin.bottom;

        var formatPercent = d3.format(opts.format);

        var x = d3.scale.ordinal()
            .rangeRoundBands([0, width], .1);

        var y = d3.scale.linear()
            .range([height, 0]);

        var xAxis = d3.svg.axis()
            .scale(x)
            .orient("bottom");

        var yAxis = d3.svg.axis()
            .scale(y)
            .orient("left")
            .tickFormat(formatPercent);

        var color = d3.scale.ordinal()
                .range(colorbrewer.Blues[4]);

        var svg = d3.select($elem[0]).append("svg")
            .attr("width", width + opts.margin.left + opts.margin.right)
            .attr("height", height + opts.margin.top + opts.margin.bottom)
            .append("g")
            .attr("transform", "translate(" + opts.margin.left + "," + opts.margin.top + ")")

        $scope.render = function(data) {
            // remove all previous items before render
            svg.selectAll('*').remove();

            x.domain(data.map(function(d) { return d[opts.xaxisProperty]; }));
            y.domain([0, d3.max(data, function(d) { return d[opts.yaxisProperty]; })]);

            svg.append("g")
                .attr("class", "x axis")
                .attr("transform", "translate(0," + height + ")")
                .call(xAxis)
                .append("text")
                .attr("x", opts.xaxisPos)
                .attr("dx", ".71em")
                .style("text-anchor", "end")
                .text(opts.xaxisName);

            svg.append("g")
                .attr("class", "y axis")
                .call(yAxis)
                .append("text")
                .attr("transform", "rotate(-90)")
                .attr("y", opts.yaxisPos)
                .attr("dy", ".71em")
                .style("text-anchor", "end")
                .text(opts.yaxisName);

            var i = 0;
            svg.selectAll(".bar")
                .data(data)
                .enter().append("rect")
                .style("fill", function(d) { return color(++i); })
                .attr("class", "bar")
                .attr("x", function(d) { return x(d[opts.xaxisProperty]); })
                .attr("width", x.rangeBand())
                 // tried to apply .transition() here
                .attr("y", function(d) { return y(d[opts.yaxisProperty]); })
                .attr("height", function(d) { return height - y(d[opts.yaxisProperty]); })      
        }

【问题讨论】:

  • 你的意思是什么过渡?您的代码中似乎没有任何转换。
  • 这应该发生在点击事件上吗?如果是这样,点击时会调用哪个函数,到底发生了什么?
  • 条形图在渲染时向上滑动。

标签: angularjs d3.js css-transitions


【解决方案1】:

要使条在渲染时向上滑动,请将当前的条添加代码替换为:

svg.selectAll(".bar")
   .data(data)
   .enter().append("rect")
   .style("fill", function(d) { return color(++i); })
   .attr("class", "bar")
   .attr("x", function(d) { return x(d[opts.xaxisProperty]); })
   .attr("width", x.rangeBand())
   .attr("y", height)
   .attr("height", 0)
   .transition().duration(500)
   .attr("y", function(d) { return y(d[opts.yaxisProperty]); })
   .attr("height", function(d) { return height - y(d[opts.yaxisProperty]); });

【讨论】:

    猜你喜欢
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    • 2021-08-19
    • 1970-01-01
    相关资源
    最近更新 更多