Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test 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 821 to 860

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 4th December 2024 08:35 Europe/London time based on the source file dated 7th November 2024 17:40 Europe/London time.