scale.category.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict';
  2. module.exports = function(Chart) {
  3. var helpers = Chart.helpers;
  4. // Default config for a category scale
  5. var defaultConfig = {
  6. position: 'bottom'
  7. };
  8. var DatasetScale = Chart.Scale.extend({
  9. /**
  10. * Internal function to get the correct labels. If data.xLabels or data.yLabels are defined, use those
  11. * else fall back to data.labels
  12. * @private
  13. */
  14. getLabels: function() {
  15. var data = this.chart.data;
  16. return (this.isHorizontal() ? data.xLabels : data.yLabels) || data.labels;
  17. },
  18. // Implement this so that
  19. determineDataLimits: function() {
  20. var me = this;
  21. var labels = me.getLabels();
  22. me.minIndex = 0;
  23. me.maxIndex = labels.length - 1;
  24. var findIndex;
  25. if (me.options.ticks.min !== undefined) {
  26. // user specified min value
  27. findIndex = helpers.indexOf(labels, me.options.ticks.min);
  28. me.minIndex = findIndex !== -1 ? findIndex : me.minIndex;
  29. }
  30. if (me.options.ticks.max !== undefined) {
  31. // user specified max value
  32. findIndex = helpers.indexOf(labels, me.options.ticks.max);
  33. me.maxIndex = findIndex !== -1 ? findIndex : me.maxIndex;
  34. }
  35. me.min = labels[me.minIndex];
  36. me.max = labels[me.maxIndex];
  37. },
  38. buildTicks: function() {
  39. var me = this;
  40. var labels = me.getLabels();
  41. // If we are viewing some subset of labels, slice the original array
  42. me.ticks = (me.minIndex === 0 && me.maxIndex === labels.length - 1) ? labels : labels.slice(me.minIndex, me.maxIndex + 1);
  43. },
  44. getLabelForIndex: function(index, datasetIndex) {
  45. var me = this;
  46. var data = me.chart.data;
  47. var isHorizontal = me.isHorizontal();
  48. if ((data.xLabels && isHorizontal) || (data.yLabels && !isHorizontal)) {
  49. return me.getRightValue(data.datasets[datasetIndex].data[index]);
  50. }
  51. return me.ticks[index];
  52. },
  53. // Used to get data value locations. Value can either be an index or a numerical value
  54. getPixelForValue: function(value, index, datasetIndex, includeOffset) {
  55. var me = this;
  56. // 1 is added because we need the length but we have the indexes
  57. var offsetAmt = Math.max((me.maxIndex + 1 - me.minIndex - ((me.options.gridLines.offsetGridLines) ? 0 : 1)), 1);
  58. if (value !== undefined && isNaN(index)) {
  59. var labels = me.getLabels();
  60. var idx = labels.indexOf(value);
  61. index = idx !== -1 ? idx : index;
  62. }
  63. if (me.isHorizontal()) {
  64. var valueWidth = me.width / offsetAmt;
  65. var widthOffset = (valueWidth * (index - me.minIndex));
  66. if (me.options.gridLines.offsetGridLines && includeOffset || me.maxIndex === me.minIndex && includeOffset) {
  67. widthOffset += (valueWidth / 2);
  68. }
  69. return me.left + Math.round(widthOffset);
  70. }
  71. var valueHeight = me.height / offsetAmt;
  72. var heightOffset = (valueHeight * (index - me.minIndex));
  73. if (me.options.gridLines.offsetGridLines && includeOffset) {
  74. heightOffset += (valueHeight / 2);
  75. }
  76. return me.top + Math.round(heightOffset);
  77. },
  78. getPixelForTick: function(index, includeOffset) {
  79. return this.getPixelForValue(this.ticks[index], index + this.minIndex, null, includeOffset);
  80. },
  81. getValueForPixel: function(pixel) {
  82. var me = this;
  83. var value;
  84. var offsetAmt = Math.max((me.ticks.length - ((me.options.gridLines.offsetGridLines) ? 0 : 1)), 1);
  85. var horz = me.isHorizontal();
  86. var valueDimension = (horz ? me.width : me.height) / offsetAmt;
  87. pixel -= horz ? me.left : me.top;
  88. if (me.options.gridLines.offsetGridLines) {
  89. pixel -= (valueDimension / 2);
  90. }
  91. if (pixel <= 0) {
  92. value = 0;
  93. } else {
  94. value = Math.round(pixel / valueDimension);
  95. }
  96. return value;
  97. },
  98. getBasePixel: function() {
  99. return this.bottom;
  100. }
  101. });
  102. Chart.scaleService.registerScaleType('category', DatasetScale, defaultConfig);
  103. };