admin管理员组文章数量:1186133
I'm new in d3js and all the javascript-world too. In my html-file I simply import the script like that:
<script src=".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
Do I need any other d3js module, which has to be imported? Or what could have caused the problem?
I'm new in d3js and all the javascript-world too. In my html-file I simply import the script like that:
<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
Do I need any other d3js module, which has to be imported? Or what could have caused the problem?
Share Improve this question edited Nov 1, 2017 at 10:39 Luis Eduardo 472 silver badges12 bronze badges asked Nov 1, 2017 at 10:37 Ira ReIra Re 7903 gold badges9 silver badges28 bronze badges5 Answers
Reset to default 9(v5) D3 no longer provides the d3.schemeCategory20* categorical color schemes. 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. Instead, D3 now includes d3-scale-chromatic, which implements excellent schemes from ColorBrewer, including categorical, diverging, sequential single-hue and sequential multi-hue schemes. These schemes are available in both discrete and continuous variants.
https://github.com/d3/d3/blob/master/CHANGES.md
d3.schemeCategory20
is neither a scale nor a function. It is just an array of colours. According to the API, it is...
An array of twenty categorical colors represented as RGB hexadecimal strings.
The same API says:
These color schemes are designed to work with 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. 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. Changes in D3 5.0:
D3 no longer provides the d3.schemeCategory20* categorical color schemes. 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
from d3/CHANGES.md
d3.schemeCategory20
it is exactly not a function. 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>
本文标签: javascriptUncaught TypeError d3schemeCategory20 is not a functionStack Overflow
版权声明:本文标题:javascript - Uncaught TypeError: d3.schemeCategory20 is not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738337552a2077201.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论