d3.js 颜色比例尺 d3.scale.category10();

        var width = 600;
        var height = 600;
        var dataset = d3.range(10); //返回[0,1,2,3,4,5,6,7,8,9]
        console.log(dataset); 
        
        // 定义表示颜色的序数比例尺
        var color = d3.scale.category10();

        var svg = d3.select("body").append("svg")
                                .attr("width",width)
                                .attr("height",height);
        // 绘制园
        var circle = svg.selectAll("circle")
                        .data(dataset)
                        .enter()
                        .append("circle")
                        .attr("cx",function(d,i){
                            return 20 + i *30;
                        })
                        .attr("cy",100)
                        .attr("r",20)
                        .attr("fill",function(d,i){
                            return color(i);   //调用颜色比例尺
                        })

猜你喜欢

转载自www.cnblogs.com/webmc/p/11077272.html