Reasonable optimized chart scaling

前端 未结 6 1694
耶瑟儿~
耶瑟儿~ 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:26

    This is from a previous similar question:

    Algorithm for "nice" grid line intervals on a graph

    I've done this with kind of a brute force method. First, figure out the maximum number of tick marks you can fit into the space. Divide the total range of values by the number of ticks; this is the minimum spacing of the tick. Now calculate the floor of the logarithm base 10 to get the magnitude of the tick, and divide by this value. You should end up with something in the range of 1 to 10. Simply choose the round number greater than or equal to the value and multiply it by the logarithm calculated earlier. This is your final tick spacing.

    Example in Python:

    import math
    
    def BestTick(largest, mostticks):
        minimum = largest / mostticks
        magnitude = 10 ** math.floor(math.log(minimum) / math.log(10))
        residual = minimum / magnitude
        if residual > 5:
            tick = 10 * magnitude
        elif residual > 2:
            tick = 5 * magnitude
        elif residual > 1:
            tick = 2 * magnitude
        else:
            tick = magnitude
        return tick
    

提交回复
热议问题