JavaScript Javascript D3 Histogram: thresholds producing wrong number of bins

I'm in the process of creating a histogram JS script using D3, and it all seems to be working correctly... except for the number of bins.

Following is the relevant part of my code:

//Define the scales for the x and y attributes
var x = d3.scaleBand()
    .range([0, width])
    .padding(configProperties.barPadding);
var y = d3.scaleLinear()
    .range([height,0]);

//Create the bins
var bins = d3.histogram()
    .domain(d3.extent(data))
    .thresholds(configProperties.binsCount)
    (data);

console.log("number of bins: " + bins.length); //9
console.log("intended number of bins: " + configProperties.binsCount); //10

If I set configProperties.binsCount to 9, bins.length is still 9. If I set configProperties.binsCount to 14, bins.length is still 9.

If I set binsCount to 15 or higher, however... bins.length outputs 23.

My understanding of how histogram.thresholds works based on the documentation is that if I give it a value, it will divide the data into that many + 1 equal segments (i.e. that many bins). However, it doesn't seem to be doing that at all. All of the example code that I could find seemed to indicate that I am using it correctly, but I can't get the number of bins that I need.

I've also tried using d3.ticks as a thresholds argument, but I encounter the same issue.

Is there something I'm missing? Does it have to do with my domain? Thanks in advance.

Answer:1

You are passing a count (that is, a simple number) to the thresholds function, not an array.

What you're seeing is the expected behaviour when you pass a number. According to the same docs:

If a count is specified instead of an array of thresholds, then the domain will be uniformly divided into approximately count bins;

Let's see it in this demo:

var data = d3.range(100);

const histogram = d3.histogram()
  .value(d => d)
  .thresholds(5);

var bins = histogram(data);

console.log("The number of bins is " + bins.length)
<script src="https://d3js.org/d3.v4.js"></script>
Answer:2

I have this function where many parts of my code call it. function test() { $.ajax({ url : url, type : 'GET', success : { verifyID(); verifyName(); verifyBlah(); } ...

I have this function where many parts of my code call it. function test() { $.ajax({ url : url, type : 'GET', success : { verifyID(); verifyName(); verifyBlah(); } ...

  1. execute function after ajax call is complete
  2. execute function after setstate
  3. execute function after function javascript
  4. execute function after settimeout javascript
  5. execute function after async javascript
  6. execute function after some time javascript
  7. execute function after complete page load
  8. execute function after delay jquery
  9. execute function after for loop javascript
  10. execute function after foreach
  11. execute function after subscribe
  12. execute function after some time jquery
  13. execute function after datatable load
  14. execute function after another function jquery
  15. execute function after timeout javascript
  16. execute function after page load jquery
  17. execute function after map javascript
  18. execute function after render react
  19. execute function after window.location.reload
  20. execute function after 5 seconds jquery

I have a native javascript plugin that is wrapped in a directive and used in angular way. Few lines of directive, export default function () { 'use strict'; return { restrict: 'E', scope: { ...

I have a native javascript plugin that is wrapped in a directive and used in angular way. Few lines of directive, export default function () { 'use strict'; return { restrict: 'E', scope: { ...

  1. working with native american clients
  2. working with native american communities
  3. working with native american tribes
  4. working with native american students
  5. working with native american clients in social work
  6. working with native american patients
  7. working with native american youth
  8. working with native animals
  9. working with native students
  10. jobs working with native american tribes
  11. working with react native
  12. adults working with native youth training
  13. jobs working with native american youth
  14. working with indigenous/native american patients
  15. working with non native english speakers
  16. jobs working with native american
  17. working with json react native
  18. start working with react native
  19. working with images in react native
  20. working with api in react native

It's easy to get rid of unmatched object property by single value, like const person = [ {name:'james',gender:'male'}, {name:'john',gender:'male'}, {name:'chun li',gender:'female'} ] ...

It's easy to get rid of unmatched object property by single value, like const person = [ {name:'james',gender:'male'}, {name:'john',gender:'male'}, {name:'chun li',gender:'female'} ] ...

  1. filter multiple arrays javascript
  2. filter array multiple conditions
  3. filter array multiple values
  4. filter array multiple conditions flow
  5. filter array multiple conditions microsoft flow
  6. filter array multiple conditions javascript
  7. filter array multiple times
  8. filter array multiple properties
  9. filter array multiple js
  10. multiple filters array
  11. javascript filter array multiple values
  12. typescript filter array multiple values
  13. swift filter array multiple conditions
  14. jquery filter array multiple values
  15. angular filter array multiple values
  16. react filter array multiple values
  17. numpy filter array multiple conditions
  18. lodash filter array multiple conditions
  19. flow filter array multiple
  20. angularjs filter array multiple values

I am having a problem implementing the Query Selector All method with classList. Basically, what I want to do is add the class .purple to all instances of the fridge class. However, it keeps telling ...

I am having a problem implementing the Query Selector All method with classList. Basically, what I want to do is add the class .purple to all instances of the fridge class. However, it keeps telling ...

  1. javascript appending class
  2. javascript adding class
  3. javascript adding class to element
  4. javascript adding class attribute
  5. javascript adding class to div
  6. javascript adding class to html
  7. javascript adding class to li
  8. javascript adding class to button
  9. js adding class
  10. js adding class to element
  11. js adding class to div