Reasonable optimized chart scaling

前端 未结 6 1621
耶瑟儿~
耶瑟儿~ 2021-01-30 13:43

I need to make a chart with an optimized y axis maximum value.

The current method I have of making charts simply uses the maximum value of all the graphs, then

6条回答
  •  無奈伤痛
    2021-01-30 14:15

    You could round up to two significant figures. The following pseudocode should work:

    // maxValue is the largest value in your chart
    magnitude = floor(log10(maxValue))
    base = 10^(magnitude - 1)
    chartHeight = ceiling(maxValue / base) * base
    

    For example, if maxValue is 1357, then magnitude is 3 and base is 100. Dividing by 100, rounding up, and multiplying by 100 has the result of rounding up to the next multiple of 100, i.e. rounding up to two significant figures. In this case, the result if 1400 (1357 ⇒ 13.57 ⇒ 14 ⇒ 1400).

提交回复
热议问题