javascript - 在 d3js 中动画散点图的示例

标签 javascript d3.js

我有一组大约 1000 个点,我想用 d3js 对其进行散点图。我希望每 0.1 秒出现大约 10 个点。某处有一个简单的例子吗? d3js 教程很详细,但我似乎找不到适合这种情况的教程。

最佳答案

  • http://alignedleft.com/tutorials/d3/making-a-scatterplot/http://bl.ocks.org/bunkat/2595950 ->这个例子让你了解如何绘制散点图!!!

  • http://swizec.com/blog/quick-scatterplot-tutorial-for-d3-js/swizec/5337 -->教程

    你需要先了解这些!!!

  • 用于散点图中动画的 DEMO FIDDLE---> http://jsfiddle.net/eaGhB/3/

         var w = 960,h = 500,nodes = [];
    
        var svg = d3.select("body").append("svg:svg")
            .attr("width", w)
            .attr("height", h);
    
        var force = d3.layout.force()
            .charge(-300)
            .size([w, h])
            .nodes(nodes)
            .on("tick", tick)
            .start();
    
        function tick() {
            svg.selectAll("circle")
                .attr("cx", function (d) { return d.x; })
                .attr("cy", function (d) { return d.y; });
    
        }
    
        var interval = setInterval(function () {
            var d = {
                x: w / 2 + 2 * Math.random()-1 ,
                y: h / 2 + 2 * Math.random() - 1
            };
    
            svg.append("svg:circle")
                .data([d])
                .attr("r", 10)
              .transition()
                .ease(Math.sqrt)
                   .style("stroke", "gray")
         .style("fill", "red")
         .attr("r", 10);
    
            if (nodes.push(d) > 20) {
                clearInterval(interval);
                d3.selectAll('circle').on("mouseover", animate).on("mouseout", function () {
                    d3.select(this).transition()
                        .duration(100)
                .attr("r", 40);
                    d3.selectAll('circle').style("fill", "black");
                });
            }
            force.stop()
            force.start();
        }, 200);
    
    
        function animate() {
            d3.select(this).transition()
                .duration(300)
                .attr("r", 20);
    
    
            d3.select(this).style("fill", "green");
            var sel = d3.select(this);
            sel.moveToFront();
        };
        d3.selection.prototype.moveToFront = function () {
            return this.each(function () {
                this.parentNode.appendChild(this);
            });
        };
    

关于javascript - 在 d3js 中动画散点图的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17636268/

相关文章:

javascript - 需要 JQuery 建议

javascript - 如何使用 Promise 等待异步 API 调用

javascript - 循环内的 addEventListener

Angular - 缩放在代码中创建的内联 d3 SVG

javascript - 使用 d3 将 SVG 文本绕一个圆旋转,然后绕其自身旋转

javascript - 如何使用d3制作实时多折线图,其中数据来自json格式的api

javascript - jssor 如何增加图像数量

Javascript十进制转十六进制

javascript - 使用 D3 防止循环的最佳方法

javascript - 将 D3-WordCloud 中的主词水平居中