Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

render_pie_graph()

Description

Output the Javascript to build a pie chart in the canvas denoted by $id
$data must be in the following format
$data = array(
"slice_a label" => "slice_a value",
"slice_b label" => "slice_b value",
);

a string can be used to denote the total value to pad the data to

Parameters

ColumnTypeDefaultDescription
$id string identifier for the canvas to render the chart in
$data array data to be rendered in the chart
$total string|null null null will mean that the data is complete and an extra field is not required

Return

void

Location

include/reporting_functions.php lines 772 to 811

Definition

 
function render_pie_graph($id,$data,$total=null)
    {
    global 
$home_colour_style_override,$header_link_style_override;

    
$rt=0;
    
$labels = [];
    
$values = [];
    foreach (
$data as $row)
        {
        
$rt+=$row["c"];
        
$values[ ]= $row["c"];
        
$labels[] = $row["name"];
        }
    
    if (!
is_null($total) && $total>$rt)
        {
        
# The total doesn't match, some rows were truncated, add an "Other".
        
$values[] = $total-$rt;
        
$labels[] = "Other";
        }

    
$labels array_map(fn($v) => substr(json_encode($v),1,-1), $labels);
    
?>
    <script type="text/javascript">
    // Setup Styling
    new Chart(document.getElementById(' echo escape($id?>'), {
        type: 'pie',
        data: {
            labels: [' echo implode("', '",$labels?>'],
                datasets: [
                        {
                    data: [ echo escape(implode(", ",$values)) ?>]
                }
            ]
        },
        options: chartstyling echo escape($id)?>,
    });
    </script>
    
    
}

This article was last updated 23rd July 2024 21:35 Europe/London time based on the source file dated 1st July 2024 14:45 Europe/London time.