0

I need to put a json from eloquent in a google chart but I can't convert the result from eloquent to a correct dataTable

 //activos represents result from eloquent
var activos = [
    {"descripcion":"peri\u00f3dico","riesgo":"5"}, 
    {"descripcion":"autom\u00e1ticas ","riesgo":"2"},
    {"descripcion":" \tAusencia","riesgo":"2"},
    {"descripcion":" \tAusencia de alto riesgo","riesgo":"4"},
    {"descripcion":"negocio","riesgo":"3"}
];

google.charts.load('current', {
        'packages': ['corechart']
    });

   function drawChart() {

        // Create the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Descripcion');
        data.addColumn('number', 'Cantidad');
        data.addRows(activos);
        // Set chart options
        var options = {
            'title': 'Reporte de Activos',

            'width': 400,
            'height': 300,

        };

        // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, options);
    }   

the chart doen't apear

2 Answers 2

1

Instead of:

data.addRows(activos);

Use something like this:

for(let j in activos){
    data.addRows([activos[j].descripcion, activos[j].riesgo]);
}
Sign up to request clarification or add additional context in comments.

Comments

0
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title></title>
        <script src='//www.gstatic.com/charts/loader.js'></script>
        <script>
            var activos = [
                {"descripcion":"peri\u00f3dico","riesgo":"5"}, 
                {"descripcion":"autom\u00e1ticas ","riesgo":"2"},
                {"descripcion":" \tAusencia","riesgo":"2"},
                {"descripcion":" \tAusencia de alto riesgo","riesgo":"4"},
                {"descripcion":"negocio","riesgo":"3"}
            ];      

            const loadchart=function(){

                let dataTbl = new google.visualization.DataTable();
                    dataTbl.addColumn( 'string', 'Descripcion' );
                    dataTbl.addColumn( 'number', 'Cantidad' );

                activos.forEach( obj=>{
                    let data=[ obj['descripcion'], parseInt( obj['riesgo'] ) ];
                    dataTbl.addRows( [ data ] );
                });

                let chart = new google.visualization.PieChart( document.getElementById('chart') );
                    chart.draw( dataTbl,{ width:400, height:300, title:'Reporte de Activos' } );
            }
            google.charts.load( 'current', {'packages':['corechart'] } );
            google.charts.setOnLoadCallback( loadchart );
        </script>       
    </head>
    <body>
        <div id='chart'></div>
    </body>
</html>

The resultant chart

2 Comments

excellent it worked, but something important to have in mind: due it come from blade it's important to set: blade.php: <input type="hidden" name="activos" id="activos" value="{{$activos}}">. And in js file: var activos =JSON.parse(document.getElementById("activos").value);
That is for you to add-glad it helped

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.