Reasonable optimized chart scaling

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

    A slight refinement and tested... (works for fractions of units and not just integers)

    public void testNumbers() {
            double test = 0.20000;
    
            double multiple = 1;
            int scale = 0;
            String[] prefix = new String[]{"", "m", "u", "n"};
            while (Math.log10(test) < 0) {
                multiple = multiple * 1000;
                test = test * 1000;
                scale++;
            }
    
            double tick;
            double minimum = test / 10;
            double magnitude = 100000000;
            while (minimum <= magnitude){
                magnitude = magnitude / 10;
            }
    
            double residual = test / (magnitude * 10);
            if (residual > 5) {
                tick = 10 * magnitude;
            } else if (residual > 2) {
                tick = 5 * magnitude;
            } else if (residual > 1) {
                tick = 2 * magnitude;
            } else {
                tick = magnitude;
            }
    
            double curAmt = 0;
    
            int ticks = (int) Math.ceil(test / tick);
    
            for (int ix = 0; ix < ticks; ix++) {
                curAmt += tick;
                BigDecimal bigDecimal = new BigDecimal(curAmt);
                bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP);
                System.out.println(bigDecimal.stripTrailingZeros().toPlainString() + prefix[scale] + "s");
            }
    
            System.out.println("Value = " + test + prefix[scale] + "s");
            System.out.println("Tick = " + tick + prefix[scale] + "s");
            System.out.println("Ticks = " + ticks);
            System.out.println("Scale = " +  multiple + " : " + scale);
    
    
        }
    

提交回复
热议问题