CanvasJS in function only shows one chart (php, js)

Viewed 381
function timeDataToPointChart($dataPoints,$title,$xlabel,$ylabel,$chartID) { 
?>
    
    <script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
    <script>
    window.onload = function () {
    
    
    var NameOfChart = "<?php echo $chartID; ?>"
     
    window[NameOfChart] = new CanvasJS.Chart("chartContainer",
    {
        animationEnabled: true,
        title:{
        text: "<?php echo $title; ?>"
    },
    axisX:{
        title: "<?php echo $xlabel; ?>",
        valueFormatString: "MM/YYYY",
        crosshair: {
        enabled: true
        }
    },
    axisY: {
        title: "<?php echo $ylabel; ?>"
    },
    data: [
    {        
        type: "line",
        xValueType: "dateTime",
        dataPoints:     
        
        <?php echo json_encode($dataPoints, JSON_NUMERIC_CHECK); ?>
    }
    ]
    });

      window[NameOfChart].render();
    
    function toggleDataSeries(e){
    if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
        e.dataSeries.visible = false;
    }
    else{
        e.dataSeries.visible = true;
    }
      window[NameOfChart].render();
    }
    
    }
    </script>
    

    <div id="chartContainer" style="height: 370px; width: 650px;"></div>
          
<?php
}
?>

I use this PHP function to generate my canvas chart. This works well, but if I call the function twice on a page, only one chart is shown.

Console shows the following error, but I don't understand what's wrong.

CanvasJS namespace already exists. If you are loading both chart and stockchart scripts, just load stockchart alone as it includes all chart features.

Thank you for your help.

1 Answers

As described from canvasJs, i've implemented the code accordingly in my Laravel App like below

@foreach($records as $record)
<div id="chartContainer{{$record->employeeRequisitionId}}" style="height: 270px; width: 100%;"></div>
@endforeach

@push('after_scripts')

<script>
    window.onload = function () {
        @foreach($records as $record)
        const chart{{$record->employeeRequisitionId}} = new CanvasJS.Chart ( "chartContainer{{$record->employeeRequisitionId}}", {
            animationEnabled: true,
            theme: "light2",
            title: {
                text: "Requisition According Fiscal Year"
            },
            axisY: {
                suffix: "%",
                scaleBreaks: {
                    autoCalculate: false
                }
            },
            data: [{
                type: "column",
                yValueFormatString: "#,##0\"%\"",
                indexLabel: "{y}",
                indexLabelPlacement: "inside",
                indexLabelFontColor: "white",
                dataPoints: {!! formatRequisitionAccordingFiscalYearData($record) !!}
            }]
        } );
        @endforeach

        @foreach($records as $record)
            chart{{$record->employeeRequisitionId}}.render ();
        @endforeach
    }
</script>

@endpush

Related