gpt4 book ai didi

javascript - d3.js 在 d3.js 特定函数中使用函数

转载 作者:行者123 更新时间:2023-11-30 12:41:29 24 4
gpt4 key购买 nike

我想知道是否可以在 d3.js 函数中使用函数。为了澄清我的问题,我们假设您有色标

  var colorScale = d3.scale.quantile()
.domain([0,8])
.range(["#ffffd9","#edf8b1","#c7e9b4","#7fcdbb","#41b6c4","#1d91c0","#225ea8","#253494","#081d58"]);

有没有办法让下面的工作正常进行?

      var colorScale = d3.scale.quantile()
.domain([0,8])
.range(function(){return ["#ffffd9","#edf8b1","#c7e9b4","#7fcdbb","#41b6c4","#1d91c0","#225ea8","#253494","#081d58"]});

所以从函数返回整个参数?

非常感谢任何知道的人!

最佳答案

谢谢 Explunit、Lars 和 Thomas

不过,我已经找到了我的问题的充分答案。

答案是:是的,这是可能的,如果你知道你的范围的参数将是一个函数,这是如何做到的:

var myfunction = function()
{return ["#ffffd9","#edf8b1","#c7e9b4","#7fcdbb","#41b6c4","#1d91c0","#225ea8","#253494","#081d58"]}

var colorScale = d3.scale.quantile()
.domain([0,8])
.range(myfunction());

如果您不确定。你可以使用 d3.functor() 即使你传递的是一个值而不是一个函数,它也能工作。所以像这样(如果它是一个函数):

 var myfunction = function()
{return ["#ffffd9","#edf8b1","#c7e9b4","#7fcdbb","#41b6c4","#1d91c0","#225ea8","#253494","#081d58"]}

var myfunctionWrapped = d3.functor(myfunction);

var colorScale = d3.scale.quantile()
.domain([0,8])
.range(myfunctionWrapped());

如果它是一个值(或数组),就像这样:

var myfunction = ["#ffffd9","#edf8b1","#c7e9b4","#7fcdbb","#41b6c4","#1d91c0","#225ea8","#253494","#081d58"]

var myfunctionWrapped = d3.functor(myfunction);

var colorScale = d3.scale.quantile()
.domain([0,8])
.range(myfunctionWrapped());

所以我建议使用 d3.functor(),因为它使您的代码更加通用。

关于javascript - d3.js 在 d3.js 特定函数中使用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24287484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com