scale.linearbase.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 'use strict';
  2. module.exports = function(Chart) {
  3. var helpers = Chart.helpers,
  4. noop = helpers.noop;
  5. Chart.LinearScaleBase = Chart.Scale.extend({
  6. handleTickRangeOptions: function() {
  7. var me = this;
  8. var opts = me.options;
  9. var tickOpts = opts.ticks;
  10. // If we are forcing it to begin at 0, but 0 will already be rendered on the chart,
  11. // do nothing since that would make the chart weird. If the user really wants a weird chart
  12. // axis, they can manually override it
  13. if (tickOpts.beginAtZero) {
  14. var minSign = helpers.sign(me.min);
  15. var maxSign = helpers.sign(me.max);
  16. if (minSign < 0 && maxSign < 0) {
  17. // move the top up to 0
  18. me.max = 0;
  19. } else if (minSign > 0 && maxSign > 0) {
  20. // move the bottom down to 0
  21. me.min = 0;
  22. }
  23. }
  24. if (tickOpts.min !== undefined) {
  25. me.min = tickOpts.min;
  26. } else if (tickOpts.suggestedMin !== undefined) {
  27. me.min = Math.min(me.min, tickOpts.suggestedMin);
  28. }
  29. if (tickOpts.max !== undefined) {
  30. me.max = tickOpts.max;
  31. } else if (tickOpts.suggestedMax !== undefined) {
  32. me.max = Math.max(me.max, tickOpts.suggestedMax);
  33. }
  34. if (me.min === me.max) {
  35. me.max++;
  36. if (!tickOpts.beginAtZero) {
  37. me.min--;
  38. }
  39. }
  40. },
  41. getTickLimit: noop,
  42. handleDirectionalChanges: noop,
  43. buildTicks: function() {
  44. var me = this;
  45. var opts = me.options;
  46. var tickOpts = opts.ticks;
  47. // Figure out what the max number of ticks we can support it is based on the size of
  48. // the axis area. For now, we say that the minimum tick spacing in pixels must be 50
  49. // We also limit the maximum number of ticks to 11 which gives a nice 10 squares on
  50. // the graph. Make sure we always have at least 2 ticks
  51. var maxTicks = me.getTickLimit();
  52. maxTicks = Math.max(2, maxTicks);
  53. var numericGeneratorOptions = {
  54. maxTicks: maxTicks,
  55. min: tickOpts.min,
  56. max: tickOpts.max,
  57. stepSize: helpers.getValueOrDefault(tickOpts.fixedStepSize, tickOpts.stepSize)
  58. };
  59. var ticks = me.ticks = Chart.Ticks.generators.linear(numericGeneratorOptions, me);
  60. me.handleDirectionalChanges();
  61. // At this point, we need to update our max and min given the tick values since we have expanded the
  62. // range of the scale
  63. me.max = helpers.max(ticks);
  64. me.min = helpers.min(ticks);
  65. if (tickOpts.reverse) {
  66. ticks.reverse();
  67. me.start = me.max;
  68. me.end = me.min;
  69. } else {
  70. me.start = me.min;
  71. me.end = me.max;
  72. }
  73. },
  74. convertTicksToLabels: function() {
  75. var me = this;
  76. me.ticksAsNumbers = me.ticks.slice();
  77. me.zeroLineIndex = me.ticks.indexOf(0);
  78. Chart.Scale.prototype.convertTicksToLabels.call(me);
  79. }
  80. });
  81. };