PHP and Highcharts, doubts list series

1

I made a query with php and I'm iterating with my array using Foreach to set it to Highcharts values as follows:

series: [<?php  $i =0;
            foreach($result as $rs) { ?>{
                name: '<?php echo $rs['ConteudoNome'];?>',
                data: [ <?php echo $rs['TreinoTempo']; ?>]

            },<?php } ?>

And the result looks like this:

series: [{
                name: 'Corrida',
                data: [ 60]

            },{
                name: 'Ataque',
                data: [ 51]

            },{
                name: 'Corrida',
                data: [ 50]

            },
            ]

I need the result to be something like "Run" [60,50] that is, when ConteudoNome is equal, it just adds value and does not create another string.

    
asked by anonymous 24.11.2015 / 19:20

1 answer

1

By deducing that your array looks like this: [{name:'Corrida', dados:"50"},{name:'Corrida', dados:"60"},{name:'Ataque',dados:"51"}] the idea would be to put it in the format:

[{name:'Corrida', dados: [50,60]},
{name:'Ataque', dados: [51]}]

So just insert the array into series . I made a code that removes the duplicates and puts them in the correct format.

See it working.

  var array1 = [
        { name: 'Corrida', dado: 50 },
        { name: 'Corrida', dado: 60 },
        { name: 'Ataque', dado: 51 }
            ];
            var result = new Array();

            for (var i = 0; i < array1.length; i++) {
                var item = new Object();
                item.name = array1[i].name;
                item.data = new Array();
                item.data.push(parseInt(array1[i].dado));

                for (var j = i + 1; j < array1.length; j++) {
                    if (array1[i].name === array1[j].name) {
                        item.data.push(parseInt(array1[j].dado));
                        array1.splice(j, 1);
                    }
                }
                result.push(item);
            }


            $('#container').highcharts({
                title: {
                    text: 'Monthly Average Temperature',
                    x: -20 //center
                },
                subtitle: {
                    text: 'Source: WorldClimate.com',
                    x: -20
                },
                xAxis: {
                    categories: ['Jan', 'Feb']
                },
                yAxis: {
                    title: {
                        text: 'Temperature (°C)'
                    },
                    plotLines: [{
                        value: 0,
                        width: 1,
                        color: '#808080'
                    }]
                },
                tooltip: {
                    valueSuffix: '°C'
                },
                legend: {
                    layout: 'vertical',
                    align: 'right',
                    verticalAlign: 'middle',
                    borderWidth: 0
                },
                series: result
            });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://code.highcharts.com/highcharts.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
    
24.11.2015 / 21:20