简体   繁体   English

我如何在d3.js中缩放

[英]How do I scale in d3.js

I want to scale some input values to some output range. 我想将一些输入值缩放到某个输出范围。

Input or Domain 10,20,40,30 ---Total Sum 100 输入或域10、20、40、30-总和100

output range should not go below 16.. and the sum of outputs of all input should be 214 输出范围不应低于16。并且所有输入的输出总和应为214

myScale---d3 linear scale myScale --- d3线性刻度

var myScale = d3.scale.linear().domain([0, 100]).range([16,214]);

out1=myScale(10)
out2=myScale(20)
out3=myScale(40)
out4=myScale(30)

I want out1+out2+out3+out4 should always be equal to 214 and minimum of all the outs is 16 or greater than that. 我希望out1 + out2 + out3 + out4始终等于214,并且所有outs的最小值等于或大于16。

Please let me know how can I write the scale 请让我知道如何写天平

Since the minValue is 16, and as qiu-deqing mentioned it is getting added as many times as the number of inputs, we should make sure that it is getting added only once. 由于minValue是16,并且正如qiu-deqing提到的那样,它的添加次数是输入数量的多少倍,因此我们应确保仅将其添加一次。

var minValue = 16,
    totalSum = 214,
    numberOfInputs = 4;

var maxValue = totalSum - ((numberOfInputs - 1)*minValue);

var myScale = d3.scale.linear().domain([0, 100]).range([minValue,maxValue]);

fiddle 小提琴

in your code, domain begin with 0 and range begin with 16 在您的代码中,域以0开头,范围以16开头

this produce a offset for each input when using scale 使用刻度时,这会为每个输入产生一个偏移

when you add out1 out2 out3 out4. 当您添加out1 out2 out3 out4时。 the offset is add 4 times. 偏移量相加4倍。 so this is not what you want. 所以这不是你想要的。

for example myScale(0 + 0 + 0 + 0 ) != myScale(0) + myScale(0) + myScale(0) + myScale(0) 例如myScale(0 + 0 + 0 + 0 ) != myScale(0) + myScale(0) + myScale(0) + myScale(0)

we can normalize the range of scale and save the offset 我们可以标准化比例范围并保存偏移量

var begin = 16,
    end = 214,
    range = end - begin;

var myScale = d3.scale.linear().domain([0, 100]).range([0,range]);

out1=myScale(10)
out2=myScale(20)
out3=myScale(40)
out4=myScale(30)

// maybe it is 214.000000000000003, float is not 100% accurate
console.log(begin + out1 + out2 + out3 + out4); 

demo: http://jsfiddle.net/q7WaU/ 演示: http : //jsfiddle.net/q7WaU/

to make this solution work, you should remember to add begin for every scale to reach your 为了使该解决方案有效,您应该记住为每个刻度添加开始以达到您的目标

destination 目的地

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM