当前位置:网站首页>Calculate pie chart percentage

Calculate pie chart percentage

2022-04-23 16:40:51 Relaxed Xiao Xi

Requirements describe :

Given an array of integers , for example :[2, 3, 4], Calculate the percentage of each element , The required percentage is accumulated as 100%.

input = [2, 3, 4]
output = [22.22, 33.33, 44.45]

Project dependence :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Code implementation :

public class PercentUtils {
    
    /** *  Calculate the percentage of pie chart  * * @param arr * @param idx * @param precision * @return */
    public static double getPercentValue(int[] arr, int idx, int precision) {
    
        // Judging index 
        if (idx < 0 || (arr.length - 1) < idx) {
    
            return 0;
        }
        // Array sum 
        double sum = 0;
        for (int i = 0; i < arr.length; i++) {
    
            sum += arr[i];
        }
        if (sum <= 0) {
    
            return 0;
        }
        // Calculation accuracy 
        double digits = Math.pow(10, precision);
        // Expansion ratio 
        double[] votesPerQuota = new double[arr.length];
        for (int i = 0; i < arr.length; i++) {
    
            double val = arr[i] / sum * digits * 100;
            votesPerQuota[i] = val;
        }
        // Expanded total 
        double targetSeats = digits * 100;
        // Value down 
        double[] seats = new double[arr.length];
        for (int i = 0; i < votesPerQuota.length; i++) {
    
            seats[i] = Math.floor(votesPerQuota[i]);
        }
        // Add up again 
        double currentSum = 0;
        for (int i = 0; i < seats.length; i++) {
    
            currentSum += seats[i];
        }
        // Remainder array 
        double[] remainder = new double[arr.length];
        for (int i = 0; i < seats.length; i++) {
    
            remainder[i] = votesPerQuota[i] - seats[i];
        }
        // Maximum remainder 
        while (currentSum < targetSeats) {
    
            double max = Double.MIN_VALUE;
            int maxId = 0;
            for (int i = 0; i < remainder.length; ++i) {
    
                if (remainder[i] > max) {
    
                    max = remainder[i];
                    maxId = i;
                }
            }
            // Add one to the maximum balance 
            ++seats[maxId];
            // This remainder has been used 
            remainder[maxId] = 0;
            // Add one to the total 
            ++currentSum;
        }
        // Return proportion 
        return seats[idx] / digits;
    }
}

Running effect :

@SpringBootTest
public class TestPercentUtils {
    
    @Test
    public void testGetPercentValue01() {
    
        int[] arr = new int[]{
    2, 3, 4};
        for (int i = 0; i < arr.length; i++) {
    
            System.out.println(arr[i] + " percent = " + PercentUtils.getPercentValue(arr, i, 2));
        }
    }

    @Test
    public void testGetPercentValue02() {
    
        int[] arr = new int[]{
    2, 2, 2};
        for (int i = 0; i < arr.length; i++) {
    
            System.out.println(arr[i] + " percent = " + PercentUtils.getPercentValue(arr, i, 2));
        }
    }
}
2 percent = 22.22
3 percent = 33.33
4 percent = 44.45
2 percent = 33.34
2 percent = 33.33
2 percent = 33.33

版权声明
本文为[Relaxed Xiao Xi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231640089593.html

随机推荐