Calculate ticks for gChart Y axis - accommodates large ranges


/ Published in: JavaScript
Save to your folder(s)

This will calculate the ticks for google charts so that you can have a chart with a defined height, and still represent large data ranges with out all the vertical information getting bunched up together.


Copy this code and paste it in your HTML
  1. function compute_rank_ticks(min, max){
  2. var start = min%2==1 ? min : min-1;
  3. var end = max%2==1 ? max : max+1;
  4. var span = end-start;
  5. var step = 1;
  6. var notches = Math.ceil(span/step);
  7.  
  8. if(span == 0){
  9. if(min == 1){
  10. return [1, 2, 3];
  11. }
  12. else{
  13. return [min - 1, min, min + 1];
  14. }
  15. }
  16.  
  17. while(notches > 10){
  18. step = step + 1;
  19. notches = Math.ceil(span/step);
  20. }
  21.  
  22. var ticks = [];
  23. var tick = start;
  24. var i;
  25. for(i=0; i<=notches; i++){
  26. ticks.push(tick);
  27. tick = tick + step;
  28. }
  29.  
  30. return ticks;
  31. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.