Search code examples
d3.jshistogramvisualizationordinal

d3JS Histogram with ordinal scale


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?


Solution

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

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