When we use D3.js to draw histograms (bar charts), can we use scaleOrdinal()?

Viewed 32

I used scaleBand() to draw histograms, but since scaleOrdinal() is also for discrete values, so I tried to change scaleBand() to scaleOrdinal() to draw the bar chart.

But do we normally use setOrdinal() to draw bar charts? It seems we could, but we have to manually calculate the bandwidth() and x of each item? If it is scaleBand(), it is all calculated for us:

const scaleX = d3.scaleBand().domain(["A", "B", "C"]).range([0, 1000]);
console.log(scaleX("A"), scaleX("B"), scaleX("C"), scaleX.bandwidth());
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/6.6.2/d3.js" integrity="sha512-6G9G63qZLxEcLqVyklArI/2ZgWK1HdKMU9yWbQUJC/eSsLsaY0NZKv4JDW4ltw1Z9sEvFB3DP54GzOIkIJjyLg==" crossorigin="anonymous"></script>

The documentation said it is the most often used scale function, but it seems it merely does some kind of 1 to 1 mapping, so can't we just use JavaScript's object or Map() object to do the same? I don't quite see why it is the most often used scale function if it is just one to one mapping: then what visualization do we use it for?

0 Answers
Related