3

Scenario description: According to the numerical value, the given long scale divides the dynamic unit scale.

1. CSS scale implementation

 刻度由很多个小块(li)、刻度线(a)组成。本次案例中以总长100个像素,五个单位块为例。

Step 1: First simulate the scale in equal-length units.
Set the total length of the scale to 100px, then the unit length is 20px, a total of 5 grids. The first step of the scale is completed by adding a tick mark to the scale by using a pseudo-class, and finally adding a value to the unit scale.
(Snippet code below)

 <div class="colorBarScale-container">
    <ul class="colorBarScale">
        <li class="colorBarScaleli">
            <a class="" href="javascript:void()"></a>
        </li>
        <li class="colorBarScaleli">
            <a class="" href="javascript:void()">20k</a>
        </li>
        <li class="colorBarScaleli">
            <a class="" href="javascript:void()">15k</a>
        </li>
        <li class="colorBarScaleli">
            <a class="" href="javascript:void()">10k</a>
        </li>
        <li class="colorBarScaleli">
            <a class="" href="javascript:void()">5k</a>
        </li>
        <a class="" href="javascript:void()">0</a>
    </ul>
</div>
 .colorBarScale-container{
        z-index: 10;
        right: 7%;
        bottom: 4%;
        position: absolute;
    }
    .colorBarScale {
        list-style: none;
        background-image: linear-gradient(to top, #0c3383,#005ea3, #0a88ba, #00c199, #f2d338, #F6b132, #f28f38, #e76124, #d91e1e);
        width: 25px;
        height: 100px;
        /* display: flex; */
        flex-direction: column;
        padding-left: 0;
        display: block;
    }

    .colorBarScale >.colorBarScaleli {
        width: 10px;
        height: 20px;
        position: relative;
        margin-left: 15px;
        display: block;
    }
    .colorBarScale li:first-child::before {
        height: 0px;
    }
    .colorBarScale li:first-child> a{
        margin-top: -8px;
    }
    .colorBarScale li:last-child::before{
        bottom: -2px;
        top: 20px;
    }

    .colorBarScale > a{
        margin-top: -10px;
        margin-left: 35px;
        display: block;
        color: #79d0f3;
        text-decoration: none;
    }

    .colorBarScale>.colorBarScaleli:before {
        content: "";
        position: absolute;
        left: 0;
        top: 0px;
        width: 100%;
        height: 1px;
        background-color: #000;
    }

    .colorBarScale>.colorBarScaleli+.colorBarScaleli {
        border-left-color: gray;
    }

    .colorBarScale > .colorBarScaleli > a {
        display: block;
        position: absolute;
        height: 20px;
        line-height: 20px;
        margin-left: 20px;
        margin-top: -10px;
        color: #79d0f3;
        text-decoration: none;
    }

image.png // equal length scale

2. Scale unitization

1. Get the nearest unit length

  • Suppose the existing unit selections are: 5, 10, 15, 20, 30, 50, 100, 200, 300, 500, 1000, 1500, 2000, 5000, 10000, 15000, 18000, 20000, 22000, 25000, 28000, 300 , 35000, 40000, 50000, 100000, 200000, 500000, 1000000, 2000000, 5000000, 10000000 (custom unit sequence)
  • Get the nearest unit based on the largest value

     最大值为5000:最邻近单位为1000;
      最大值为45000,以5个单位为例,一个单位为9000,但实际序列里没有9000,那么最邻近的单位则应该为10000;

    How to find:
    The first step: unit: Divide the maximum value by the preset scale unit (4 grids in this case) to obtain the average unit length.
    Step 2: nearCale: Compare the average unit length with the preset unit length to obtain the nearest equal length (ie: a grid unit).
    The third step: unitCalePX: According to the calculated unit length, calculate the pixel corresponding to the unit length (ie: the length of a grid).
    Step 4: Insert the dom

2. Get the nearest unit length

 数值1000格式化:1k
    数值1000000格式化:1M

js implementation

 // 将最近单位格式化
function formatterUnit(value:number, unitValue: number){
  let unitArr = ['', 'k', 'M', 'G', 'T'];
  let unit = Math.floor( (value.toString().length - 1) / 3);   // 获取单位进制
  let isFormatterValue = Math.floor( (unitValue.toString().length - 1) / 3);   // 用于判断是否格式化
  let formatterValue = isFormatterValue > 0 ? unit > 0 ? ( (value / Math.pow(1000,unit)) + unitArr[unit] ) : (value + ''):(value + '');
  return formatterValue;
}
// 获取最近单位
function getNearCale(value: number){
  const scalArr = [5, 10, 15, 20, 30, 50, 100, 200, 300, 500, 1000, 1500, 2000, 5000, 10000, 15000, 18000, 20000, 22000, 25000, 28000, 30000, 35000, 40000, 50000, 100000, 200000, 500000, 1000000, 2000000, 5000000, 10000000];
  let nearCale: number[] = [];
  let scalIndex:number = 0;
  scalArr.forEach((item, index)=>{ 
    if( (value / item) < 1 && nearCale.length < 1){
      nearCale.push( value);
      return;
    }else if((value / item) >= 1){
      nearCale.push(item);
      scalIndex = index;
    } });
    // 判断是否有更接近的单位刻度
  if( (1 - value / scalArr[scalIndex + 1]) < (value / scalArr[scalIndex + 1] - 1) ){
    nearCale.push(scalArr[scalIndex + 1]);
  }
  let len = nearCale.length;
  return nearCale[len - 1]
}

image.png // Unitize the scale (max is 23626)

code details


可可乐滋滋
358 声望7 粉丝

桃李春风一杯酒,江湖夜雨十年灯