D3

一、定量比例尺

2017-10-24  本文已影响70人  陆lmj

比例尺:就像函数一样,将一个量转换为另一个量,定义域到值域的转换。
每个比例尺都需要指定一个domain(定义域)range(值域)

定量比例尺:定义域是连续的。值域有连续的也有离散的。

1、线性比例尺(Linear Scale)

var linear = d3.scale.linear()
                .domain([0,20])
                .range([0,100]);        
console.log( linear(10) );              //输出50
console.log( linear(30) );              //输出150
console.log( linear.invert(80) );              //输出16

linear.clamp(true);
console.log( linear(30) );              //输出100

linear.rangeRound([0,100]);
console.log( linear(13.33) );              //输出67,对结果进行了四舍五入

//使用nice之后,定义域变成了比较工整的形式,但是并不是四舍五入
linear.domain([0.12300000,0.4888888888]).nice();
console.log( linear.domain() );              //输出[0.1,0.5]
linear.domain([33.611111,45.97745]).nice();
console.log( linear.domain() );              //输出[33,46]
var linear = d3.scale.linear()
                .domain([-20,20])
                .range([0,100]);
var ticks = linear.ticks(5);
console.log(ticks);            //输出数组[-20,-10,0,10,20]

var tickFormat = linear.tickFormat(5,"+");//设定的格式为"+":表示如果是正数,则在前面添加一个加号,负数则添加一个减号。
for(var i=0;i<ticks.length;i++){
    ticks[i] = tickFormat(ticks[i]);
}
console.log(ticks);            //输出["-20","-10","+0","+10","+20"]

2、指数(Power Scale)和对数比例尺(Log Scale)

指数比例尺和对数比例尺的方法和线性比例尺一样,但指数比例尺多一个exponent(),对数比例尺多一个base(),两者都是用于指定底数。

var pow = d3.scale.pow().exponent(3);     //设置指数比例尺的指数为3
console.log( pow(2) );        //输出为8
console.log( pow(3) );        //输出为27

pow.exponent(0.5);
console.log( pow(2) );        //输出为1.414
console.log( pow(3) );        //输出为1.732
//指定domain和range
//指数比例尺
var pow = d3.scale.pow()
                .exponent(3)
                .domain([0,3])          //指数比例尺内部调用了线性比例尺,而且把这个线性比例尺
                .range([0,90]);         //定义域的边界变为了其指定的次方。即定义域为[0,27]   
//线性比例尺
var linear = d3.scale.linear()
                .domain([0,Math.pow(3,3)])
                .range([0,90]);
console.log( pow(1.5) );        //输出为11.25
console.log( linear(Math.pow(1.5,3)) );    //输出为11.25

3、量子(Quantize Scale)和分位比例尺(Quantile Scale)

var quantize = d3.scale.quantize()
                .domain([50,0])
                .range(["#888","#666","#444","#222","#000"]);
var quantize = d3.scale.quantize().domain([0,2,8,10]).range([1,100]),   //[0,5)对应1,[5,10]对应100
    quantile = d3.scale.quantile().domain([0,2,4,10]).range([1,100]);//[0,3)对应1,[3,10]对应100

console.log( quantize(4.99) );//量子比例尺,输出1
console.log( quantize(5) );//量子比例尺,输出100
console.log( quantile(2.99) );//分位比例尺,输出1
console.log( quantile(3) );//分位比例尺,输出100

4、阈值比例尺

阈值比例尺:通过设定阈值,将连续的定义域映射到离散的值域里,

//该阈值比例尺定义了三个阈值:10、20、30,则空间被划分为四段:负无穷到10,10到20,20到30,30到正无穷
var threshold = d3.scale.threshold()
                .domain([10,20,30])
                .range(["red","green","blue","black"]);
                
console.log( threshold(5) );                   //输出red
console.log( threshold(15) );                 //输出green
console.log( threshold(25) );                 //输出blue
console.log( threshold(35) );                 //输出black

console.log( threshold.invertExtent("red") );                   //输出[undefined,10]
console.log( threshold.invertExtent("green") );                 //输出[10,20]
console.log( threshold.invertExtent("blue") );                  //输出[20,30]
console.log( threshold.invertExtent("black") );                 //输出[30,undefined]
上一篇 下一篇

猜你喜欢

热点阅读