问题
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