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 769 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";
        }
    
?>
    <script type="text/javascript">
    // Setup Styling


    new Chart(document.getElementById(' echo escape($id?>'), {
        type: 'pie',
        data: {
            labels: [' echo escape(implode("', '",$labels)) ?>'],
                datasets: [
                        {
                    data: [ echo escape(implode(", ",$values)) ?>]
                }
            ]
        },
        options: chartstyling echo escape($id)?>,


    });

    </script>
    
    
}

This article was last updated 27th April 2024 10:35 Europe/London time based on the source file dated 10th April 2024 18:30 Europe/London time.