Blame view

node_modules/echarts/src/scale/helper.js 3.96 KB
aaac7fed   liuqimichale   add
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
  * regarding copyright ownership.  The ASF licenses this file
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
  *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
  
  /**
   * For testable.
   */
  
  import * as numberUtil from '../util/number';
  
  var roundNumber = numberUtil.round;
  
  /**
   * @param {Array.<number>} extent Both extent[0] and extent[1] should be valid number.
   *                                Should be extent[0] < extent[1].
   * @param {number} splitNumber splitNumber should be >= 1.
   * @param {number} [minInterval]
   * @param {number} [maxInterval]
   * @return {Object} {interval, intervalPrecision, niceTickExtent}
   */
  export function intervalScaleNiceTicks(extent, splitNumber, minInterval, maxInterval) {
      var result = {};
      var span = extent[1] - extent[0];
  
      var interval = result.interval = numberUtil.nice(span / splitNumber, true);
      if (minInterval != null && interval < minInterval) {
          interval = result.interval = minInterval;
      }
      if (maxInterval != null && interval > maxInterval) {
          interval = result.interval = maxInterval;
      }
      // Tow more digital for tick.
      var precision = result.intervalPrecision = getIntervalPrecision(interval);
      // Niced extent inside original extent
      var niceTickExtent = result.niceTickExtent = [
          roundNumber(Math.ceil(extent[0] / interval) * interval, precision),
          roundNumber(Math.floor(extent[1] / interval) * interval, precision)
      ];
  
      fixExtent(niceTickExtent, extent);
  
      return result;
  }
  
  /**
   * @param {number} interval
   * @return {number} interval precision
   */
  export function getIntervalPrecision(interval) {
      // Tow more digital for tick.
      return numberUtil.getPrecisionSafe(interval) + 2;
  }
  
  function clamp(niceTickExtent, idx, extent) {
      niceTickExtent[idx] = Math.max(Math.min(niceTickExtent[idx], extent[1]), extent[0]);
  }
  
  // In some cases (e.g., splitNumber is 1), niceTickExtent may be out of extent.
  export function fixExtent(niceTickExtent, extent) {
      !isFinite(niceTickExtent[0]) && (niceTickExtent[0] = extent[0]);
      !isFinite(niceTickExtent[1]) && (niceTickExtent[1] = extent[1]);
      clamp(niceTickExtent, 0, extent);
      clamp(niceTickExtent, 1, extent);
      if (niceTickExtent[0] > niceTickExtent[1]) {
          niceTickExtent[0] = niceTickExtent[1];
      }
  }
  
  export function intervalScaleGetTicks(interval, extent, niceTickExtent, intervalPrecision) {
      var ticks = [];
  
      // If interval is 0, return [];
      if (!interval) {
          return ticks;
      }
  
      // Consider this case: using dataZoom toolbox, zoom and zoom.
      var safeLimit = 10000;
  
      if (extent[0] < niceTickExtent[0]) {
          ticks.push(extent[0]);
      }
      var tick = niceTickExtent[0];
  
      while (tick <= niceTickExtent[1]) {
          ticks.push(tick);
          // Avoid rounding error
          tick = roundNumber(tick + interval, intervalPrecision);
          if (tick === ticks[ticks.length - 1]) {
              // Consider out of safe float point, e.g.,
              // -3711126.9907707 + 2e-10 === -3711126.9907707
              break;
          }
          if (ticks.length > safeLimit) {
              return [];
          }
      }
      // Consider this case: the last item of ticks is smaller
      // than niceTickExtent[1] and niceTickExtent[1] === extent[1].
      if (extent[1] > (ticks.length ? ticks[ticks.length - 1] : niceTickExtent[1])) {
          ticks.push(extent[1]);
      }
  
      return ticks;
  }