I would like to create a function, which would automatically create data for my chart and I don't want to spam my source code with unused variables. My current max label count is 9 (so I don't need "count 10" and "count 11" condition yet), but it may be much more in the future, and I don't want to update my code everytime I create more labels...
Labels are stored in array $labels and every data from "count 2" should start with 0, and end with $downloads['total']. But from "count 3" I need to split $downloads['total'], because I need my $chart_data count to be equal to label count...
My current code may looks wierd, but it does exactly what I want:
if (count($labels) == 1) { $chart_type = "bar"; $chart_data = "0"; } else if (count($labels) == 2) { $chart_type = "area"; $chart_data = "0," . $downloads['total']; // "0,x" } // from now, it should be done automatically based on labels count else if (count($labels) == 3) { $chart_type = "area"; $chart_data = "0," . round($downloads['total']*0.5) . "," . $downloads['total']; // "0,1/2x,x" } else if (count($labels) == 4) { $chart_type = "area"; $chart_data = "0," . round($downloads['total']*0.33) . "," . round($downloads['total']*0.66) . "," . $downloads['total']; // "0,1/3x,2/3x,x" } else if (count($labels) == 5) { $chart_type = "area"; $chart_data = "0," . round($downloads['total']*0.25) . "," . round($downloads['total']*0.5) . "," . round($downloads['total']*0.75) . "," . $downloads['total']; // "0,1/4x,2/4x,3/4x,x" } else if (count($labels) == 6) { $chart_type = "area"; $chart_data = "0," . round($downloads['total']*0.2) . "," . round($downloads['total']*0.4) . "," . round($downloads['total']*0.6) . "," . round($downloads['total']*0.8) . "," . $downloads['total']; // "0,1/5x,2/5x,3/5x,4/5x,x" } else if (count($labels) == 7) { $chart_type = "area"; $chart_data = "0," . round($downloads['total']*0.166) . "," . round($downloads['total']*0.333) . "," . round($downloads['total']*0.5) . "," . round($downloads['total']*0.666) . "," . round($downloads['total']*0.833) . "," . $downloads['total']; // "0,1/6x,2/6x,3/6x,4/6x,5/6x,x" } else if (count($labels) == 8) { $chart_type = "area"; $chart_data = "0," . round($downloads['total']*0.143) . "," . round($downloads['total']*0.286) . "," . round($downloads['total']*0.429) . "," . round($downloads['total']*0.571) . "," . round($downloads['total']*0.714) . "," . round($downloads['total']*0.857) . "," . $downloads['total']; // "0,1/7x,2/7x,3/7x,4/7x,5/7x,6/7x,x" } else if (count($labels) == 9) { $chart_type = "area"; $chart_data = "0," . round($downloads['total']*0.125) . "," . round($downloads['total']*0.25) . "," . round($downloads['total']*0.375) . "," . round($downloads['total']*0.5) . "," . round($downloads['total']*0.625) . "," . round($downloads['total']*0.75) . "," . round($downloads['total']*0.875) . "," . $downloads['total']; // "0,1/8x,2/8x,3/8x,4/8x,5/8x,6/8x,7/8x,x" } else if (count($labels) == 10) { $chart_type = "area"; $chart_data = "0," . round($downloads['total']*0.111) . "," . round($downloads['total']*0.222) . "," . round($downloads['total']*0.333) . "," . round($downloads['total']*0.444) . "," . round($downloads['total']*0.555) . "," . round($downloads['total']*0.666) . "," . round($downloads['total']*0.777) . "," . round($downloads['total']*0.888) . "," . $downloads['total']; // "0,1/9x,2/9x,3/9x,4/9x,5/9x,6/9x,7/9x,8/9x,x" } else if (count($labels) == 11) { $chart_type = "area"; $chart_data = "0," . round($downloads['total']*0.1) . "," . round($downloads['total']*0.2) . "," . round($downloads['total']*0.3) . "," . round($downloads['total']*0.4) . "," . round($downloads['total']*0.5) . "," . round($downloads['total']*0.6) . "," . round($downloads['total']*0.7) . "," . round($downloads['total']*0.8) . "," . round($downloads['total']*0.9) . "," . $downloads['total']; // "0,1/10x,2/10x,3/10x,4/10x,5/10x,6/10x,7/10x,8/10x,9/10x,x" } Could anyone help me with that? Or there isn't any way, how to make it automatic/dynamic?
Thank you!
https://stackoverflow.com/questions/67309613/php-dynamically-split-number-into-fraction-parts-based-on-array-count April 29, 2021 at 08:40AM
没有评论:
发表评论