zoukankan      html  css  js  c++  java
  • d3.js学习8

    颜色尺度d3.scale.category()


    d3.scale.category10() construct an ordinal scale with ten categorical colors.

    d3.scale.category20() construct an ordinal scale with twenty categorical colors.

    d3.scale.category20b() construct an ordinal scale with twenty categorical colors.

    d3.scale.category20c() construct an ordinal scale with twenty categorical colors.

    var data=[];
    for(var i=0;i<20;i++){
    	data.push(i);//生成0-19的数组
    }
    function render(data,scale,component){
    	var selector=d3.select(component).selectAll("div.cell").data(data);
            //enter
    	selector.enter().append("div").classed("cell",true);
    	//exit
    	selector.exit().remove();
    	//update
    	selector.style("display","inline-block")
    		.style("background-color",function(d){
    		return scale(d).indexOf("#")>=0?scale(d):"white";//三元表达式返回背景颜色 #.....否则设置背景色为white
    	})
    	.text(function(d){
    		return scale(d);//背景色输出为文字
    	})
    }
    
    render(data,d3.scale.category20(),"#category");
    使得0-19 20个数字与20个颜色一一对应
        
    

    如果生成数组为20个数字,使用category10,将生成两遍同一内容;如果生成10个数字,使用category20,将截断,效果:

    这是我的个人日记本
  • 相关阅读:
    Attribute+Reflection,提高代码重用
    类型安全的EventHandlerList
    简单一招,使解决方案下的项目版本号统一
    T-SQL 随机返回特定行数据和分页查询
    2013年中国系统架构师大会随想
    C#实现在注册表中保存信息
    滤镜
    蒙版
    图层样式和混合模式
    布尔运算
  • 原文地址:https://www.cnblogs.com/valentineisme/p/4227912.html
Copyright © 2011-2022 走看看