序数规模行为 [英] Ordinal scale behaviour

查看:103
本文介绍了序数规模行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个范围和域之间一一对应的序数标度。我希望不属于域的值/字符串不会返回任何东西。相反,我得到以下行为:

  var x = d3.scaleOrdinal()。domain(['a','b' ,'c'])。range([10,20,30])
x('d')
10
x('e')
20
x 'f')
30

这种行为背后有逻辑吗?

解决方案

这是顺序量表的预期行为。实际上,D3量表(定量或定性)是以这样的方式,使您可以传递值以外的域(请参阅我的这里回答< a>)。



如果要定义未知输入的输出,请使用 scale.unknown()



 

 < script src =https://d3js.org/d3.v4.min .js>< / script>  



EDIT :有关OP在评论中的问题


域外的值的范围值是随机选择还是有逻辑?


可以看到逻辑检查源代码

 函数scale(d){
var key = d +,i = index.get(key);
if(!i){
if(unknown!== implicit)return unknown;
index.set(key,i = domain.push(d));
}
返回范围[(i - 1)%range.length];
}

它告诉我们,如果值不存在,域。因此,未在域中指定的值将具有将循环该范围的输出。检查此演示,输出为 10,20,30,10,20,30,10,20 ...


$ b b

  var x = d3.scaleOrdinal().domain(['a','b','c'])。范围([10,20,30]); console.log(d is+ x(d)); console.log(e is+ x(e)); console.log f是+ x(f)); console.log(g is+ x(g)); console.log(h is+ x(h)); console.log i is+ x(i)); console.log(j is+ x(j));   < script src =https://d3js.org/d3.v4.min.js>< / script>  



这样写的结果相同:

  var x = d3.scaleOrdinal()
.domain(['a','b','c','d' e','f','g','h','i'])
.range([10,20,30]);

在这种情况下, d e 对应于20, f 对应于30等...


I would like to create an ordinal scale with one to one correspondence between range and domain. I would expect that values/strings that do not belong to the domain would return nothing. Instead I get the following behaviour:

var x = d3.scaleOrdinal().domain(['a', 'b', 'c']).range([10, 20, 30])
x('d')
10
x('e')
20
x('f')
30

Is there any logic behind this behaviour? Maybe I am missing something, but I couldn't find something out there.

解决方案

That's the expected behaviour of an ordinal scale. Actually, D3 scales (quantitative or qualitative) are made in such a way that you can pass values outside the domain (see my answer here).

If you want to define outputs for unknown inputs, use scale.unknown():

var x = d3.scaleOrdinal()
    .domain(['a', 'b', 'c'])
    .range([10, 20, 30])
    .unknown("not in the scale");

console.log("a is " + x("a"));
console.log("b is " + x("b"));
console.log("c is " + x("c"));
console.log("f is " + x("f"));
console.log("xyz is " + x("xyz"));

<script src="https://d3js.org/d3.v4.min.js"></script>

EDIT: Regarding OP's question in the comments:

Are the range values for values outside the domain chosen randomly or is there a logic?

The logic can be seen inspecting the source code:

function scale(d) {
    var key = d + "", i = index.get(key);
    if (!i) {
        if (unknown !== implicit) return unknown;
        index.set(key, i = domain.push(d));
    }
    return range[(i - 1) % range.length];
}

It shows us that, if the value doesn't exist, it's pushed into the domain. So, values not specified in the domain will have an output that will "cycle" through the range. Check this demo, the output is 10, 20, 30, 10, 20, 30, 10, 20...:

var x = d3.scaleOrdinal()
    .domain(['a', 'b', 'c'])
    .range([10, 20, 30]);

console.log("d is " + x("d"));
console.log("e is " + x("e"));
console.log("f is " + x("f"));
console.log("g is " + x("g"));
console.log("h is " + x("h"));
console.log("i is " + x("i"));
console.log("j is " + x("j"));

<script src="https://d3js.org/d3.v4.min.js"></script>

Which has the same outcome of writing this:

var x = d3.scaleOrdinal()
.domain(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
.range([10, 20, 30]);

In this case d corresponds to 10, e corresponds to 20, f corresponds to 30 etc...

这篇关于序数规模行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆