d3JS Histogram with ordinal scale

徘徊边缘 提交于 2019-12-11 10:40:38

问题


I am trying to get a D3JS histogram working with an ordinal scale (in this case, student grade symbols (A+, A, B ... F).

Here is a minimum working example:

<!DOCTYPE html>
<meta charset="utf-8">

<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
    var studentSymbols = ["F", "E", "D", "C", "C", "C", "C", "B", "B", "A", "A+"];

    var x = d3.scale.ordinal()
        .domain(["F", "E", "D", "C", "B", "A", "A+"])
        .rangeRoundBands([0, width]);

    // Generate a histogram using twenty uniformly-spaced bins.
    var myHistogram = d3.layout.histogram()
        (studentSymbols);

    console.log(myHistogram)
</script>
</body>

When the above is run, the console outputs an Array of 5 Arrays, with dx and x fields equal to NaN.

How should I correct this code?


回答1:


I think I may have figured it out. I changed the declaration of myHistogram to:

    var myHistogram = d3.layout.histogram()
        (studentSymbols.map(x));


来源:https://stackoverflow.com/questions/29318223/d3js-histogram-with-ordinal-scale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!