update-scale.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. "use strict";
  2. var Helper = require('../helper');
  3. var Util = require('../../util/common');
  4. module.exports = {
  5. updateLinearScale: function updateLinearScale(field, min, max) {
  6. var chart = this.chart;
  7. var scale = Helper.getScale(chart, field); // const colDef = Helper.getColDef(chart, field);
  8. // chart.scale(field, Util.mix({}, colDef, {
  9. // min,
  10. // max,
  11. // nice: false
  12. // }));
  13. scale.change({
  14. min: min,
  15. max: max,
  16. nice: false
  17. });
  18. },
  19. updateCatScale: function updateCatScale(field, newValues, ticks, values, minIndex, maxIndex) {
  20. var chart = this.chart; // const colDef = Helper.getColDef(chart, field);
  21. var scale = Helper.getScale(chart, field);
  22. scale.change({
  23. values: newValues,
  24. ticks: ticks,
  25. scale: function scale(value) {
  26. if (this.type === 'timeCat') {
  27. value = this._toTimeStamp(value);
  28. }
  29. var rangeMin = this.rangeMin();
  30. var rangeMax = this.rangeMax();
  31. var range = rangeMax - rangeMin;
  32. var min;
  33. var max;
  34. var percent;
  35. var currentIndex = values.indexOf(value); // 在完整数据集中的索引值
  36. if (currentIndex >= 0 && currentIndex < minIndex) {
  37. // 不在范围内,左侧数据
  38. max = rangeMin > 0 ? -0.1 : rangeMin - 0.1;
  39. min = max - range;
  40. percent = currentIndex / minIndex;
  41. } else if (currentIndex >= 0 && currentIndex > maxIndex) {
  42. // 不在范围内,右侧数据
  43. min = rangeMax < 1 ? 1.1 : rangeMax + 0.1;
  44. max = min + range;
  45. percent = (currentIndex - maxIndex - 1) / (values.length - 1 - maxIndex);
  46. } else {
  47. // 数值在当前 this.values 范围内
  48. var index = this.translate(value);
  49. if (this.values.length === 1) {
  50. percent = index;
  51. } else {
  52. percent = index / (this.values.length - 1);
  53. }
  54. min = rangeMin;
  55. max = rangeMax;
  56. }
  57. return min + percent * (max - min);
  58. },
  59. getTicks: function getTicks() {
  60. var self = this;
  61. var ticks = this.ticks;
  62. var rst = [];
  63. Util.each(ticks, function (tick) {
  64. var obj;
  65. if (Util.isObject(tick)) {
  66. obj = tick;
  67. } else {
  68. var value = self.scale(tick);
  69. value = value >= 0 && value <= 1 ? value : NaN;
  70. obj = {
  71. text: Util.isString(tick) ? tick : self.getText(tick),
  72. value: value,
  73. tickValue: tick // 用于坐标轴上文本动画时确定前后帧的对应关系
  74. };
  75. }
  76. rst.push(obj);
  77. });
  78. return rst;
  79. }
  80. });
  81. }
  82. };