简体   繁体   English

未捕获的类型错误:d3.schemeCategory20 不是函数

[英]Uncaught TypeError: d3.schemeCategory20 is not a function

I'm new in d3js and all the javascript-world too.我是 d3js 和所有 javascript 世界的新手。 In my html-file I simply import the script like that:在我的 html 文件中,我只是像这样导入脚本:

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

By trying to use following:通过尝试使用以下内容:

var ordinalColorScale = d3.schemeCategory20();

I get the exception我得到了例外

Uncaught TypeError: d3.schemeCategory20 is not a function at index.html:48未捕获的类型错误:d3.schemeCategory20 不是 index.html:48 处的函数

Do I need any other d3js module, which has to be imported?我是否需要任何其他必须导入的 d3js 模块? Or what could have caused the problem?或者是什么导致了这个问题?

(v5) D3 no longer provides the d3.schemeCategory20* categorical color schemes. (v5) D3 不再提供 d3.schemeCategory20* 分类配色方案。 These twenty-color schemes were flawed because their grouped design could falsely imply relationships in the data: a shared hue can imply that the encoded data are part of a group (a super-category), while relative lightness can imply order.这 20 种配色方案存在缺陷,因为它们的分组设计可能会错误地暗示数据中的关系:共享色调可能暗示编码数据是一组(超类别)的一部分,而相对亮度可能暗示有序。 Instead, D3 now includes d3-scale-chromatic, which implements excellent schemes from ColorBrewer, including categorical, diverging, sequential single-hue and sequential multi-hue schemes.相反,D3 现在包括 d3-scale-chrom ,它实现了 ColorBrewer 的优秀方案,包括分类、发散、顺序单色调和顺序多色调方案。 These schemes are available in both discrete and continuous variants.这些方案可用于离散和连续变体。

https://github.com/d3/d3/blob/master/CHANGES.md https://github.com/d3/d3/blob/master/CHANGES.md

d3.schemeCategory20 is neither a scale nor a function. d3.schemeCategory20既不是尺度也不是函数。 It is just an array of colours.它只是一系列颜色。 According to the API , it is...根据API ,它是...

An array of twenty categorical colors represented as RGB hexadecimal strings.表示为 RGB 十六进制字符串的二十种分类颜色的数组。

The same API says:相同的 API 说:

These color schemes are designed to work with d3.scaleOrdinal.这些配色方案旨在与 d3.scaleOrdinal 一起使用。

Therefore, you have to pass it to an ordinal scale as its range, like this:因此,您必须将其传递给一个序数比例作为其范围,如下所示:

var myScale = d3.scaleOrdinal()
    .range(d3.schemeCategory20)

Which is the same of:这与以下内容相同:

    var myScale = d3.scaleOrdinal(d3.schemeCategory20)

Here is a demo:这是一个演示:

 var scale = d3.scaleOrdinal(d3.schemeCategory20); d3.select("body").selectAll(null) .data(d3.range(20)) .enter() .append("div") .style("background-color", function(d){ return scale(d)})
 div { min-height: 10px; }
 <script src="https://d3js.org/d3.v4.min.js"></script>

For d3 v4.对于 d3 v4。 You can use like as你可以像这样使用

   var color = d3.scaleOrdinal(d3.schemeCategory20c);
                     or
    var color = d3.scaleOrdinal()
      .range(["red", "green", "blue", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);

For anyone who is looking for it in future d3 versions and also something to consider when using this color scheme.对于在未来的 d3 版本中寻找它的任何人,以及在使用此配色方案时需要考虑的事项。 Changes in D3 5.0: D3 5.0 中的变化:

D3 no longer provides the d3.schemeCategory20* categorical color schemes. D3 不再提供 d3.schemeCategory20* 分类配色方案。 These twenty-color schemes were flawed because their grouped design could falsely imply relationships in the data: a shared hue can imply that the encoded data are part of a group (a super-category), while relative lightness can imply order这 20 种配色方案存在缺陷,因为它们的分组设计可能会错误地暗示数据中的关系:共享色调可能暗示编码数据是一组(超类别)的一部分,而相对亮度可能暗示顺序

from d3/CHANGES.md来自d3/CHANGES.md

d3.schemeCategory20 it is exactly not a function. d3.schemeCategory20它完全不是一个函数。 It is an array of default colors.它是一组默认颜色。 You should pass this array to your scale function.您应该将此数组传递给您的缩放函数。

 console.log('d3.schemeCategory20 ==> ', d3.schemeCategory20);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.11.0/d3.min.js"></script>

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

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