d3JS具有序数标度的直方图

时间:2015-03-28 14:23:01

标签: d3.js histogram visualization ordinal

我试图让D3JS直方图以序数尺度工作(在这种情况下,学生成绩符号(A +,A,B ... F)。

以下是最低工作示例:

<!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>

运行上述操作时,控制台会输出一个包含5个数组的数组,其中dx和x字段等于NaN。

我该如何更正此代码?

1 个答案:

答案 0 :(得分:3)

我想我可能已经弄明白了。我将myHistogram的声明更改为:

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