PHP and Highcharts, doubt list series

Asked

Viewed 208 times

1

I made a query with php and am iterating with my array using Foreach to set in Highcharts the 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 is like this:

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

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

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

            },
            ]

I need the result to be something like "Race" [60.50] that is, when the ConteudoNome is equal, it just adds the value and does not create another series.

1 answer

1


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

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

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

See functioned.

  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>
 <script src="https://code.highcharts.com/highcharts.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

  • I followed your advice, however when I will pass my array already in JSON format to JS, it is not right. I gave an Alert and it gets Object Object; you know what it can be?

  • @Luangabriel gives a console.log(seuarray) and sees how his array is using F12->console.

  • Ae, in the console.log the array appeared ok, however it is not mounting on the chart.

  • @Luangabriel imported highchats.js and Jquery.js? Note that Jquery comes before.

  • I did, I think the problem is in the "series: result". I already tried unsuccessfully to do this a few days ago to play a direct JSON array in the series

  • I don’t think that’s it, because the code above is working. Click Executar trecho de código and you’ll see. I think you’re missing some detail in adapting that code to your.

  • Join the chat so we can be careless about it. continue this discussion on chat](http://chat.stackexchange.com/rooms/32104/discussion-between-marconi-and-luan-gabriel).

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.