Pass Mysql result to Highcharts

Asked

Viewed 82 times

0

I have the following array, and I need to convert it for Highcharts to interpret and generate the chart, as I can iterate my array, since it returns the duplicate region but with different sale value?

Where the X axis would be [nomcin], the name would be [nomreg] and date would be an array with [valven], follows Highcharts format, and array returned by PHP:

xAxis: {
    categories: ['Outros', 'Peças', 'Tratores'],
    crosshair: true
}


series: [{
    name: 'PR',
    data: [
        49.9, 71.5, 500
    ]
  }, {
    name: 'SC',
    data: [
        83.6, 78.8, 63
    ]

}]



Array (
[0] => stdClass Object
    (
        [anomes] => 201601
        [codreg] => 41
        [nomreg] => PR
        [codcin] => 1
        [nomcin] => OUTROS
        [valven] => 6835.7000
        [qtdven] => 1078.8000
    )

[1] => stdClass Object
    (
        [anomes] => 201601
        [codreg] => 42
        [nomreg] => SC
        [codcin] => 1
        [nomcin] => OUTROS
        [valven] => 3129.0000
        [qtdven] => 366.6200
    )

[2] => stdClass Object
    (
        [anomes] => 201601
        [codreg] => 42
        [nomreg] => SC
        [codcin] => 2
        [nomcin] => PECAS
        [valven] => 346.9100
        [qtdven] => 73.6600
    )

)

SQL result:

anomes  codreg  nomreg  codcin  nomcin     valven     qtdven  
------  ------  ------  ------  ------  ---------  -----------
201601      41  PR           1  OUTROS  6835.7000    1078.8000
201601      42  SC           1  OUTROS  3129.0000     366.6200
201601      42  SC           2  PECAS    346.9100      73.6600

It currently stands that way: inserir a descrição da imagem aqui

And I’d like my chart to stay that way:

inserir a descrição da imagem aqui

The chart will show sales by Region, and rating a given year.

  • Hello @wribeiro , sorry let me see if I got it right, your difficulty is in relation to the duplicity of the region, as for example the SC that appears twice this? If yes, you can do a GROUP BY on sql to resolve...

  • @Neo I will edit the question, with some prints for better understanding

1 answer

1


Hi @wribeiro , From what I saw how would like in the print, let’s see if we set properly:

  • You need to get in while the data so:

    php:

    while ($row = mysqli_fetch_array($query)){
    
       $nomreg = $row['nomreg'];
       $valven = $row['valven'];
    
       $nomreg_chart []= $nomreg;
       $valven_chart_pecs []= $valven; 
       $valven_chart_outros []= $valven;
    
    }
    
    //$nomreg_chart_join = join($nomreg_chart,",");
      $nomreg_chart_join = "'".implode("','",$nomreg_chart)."'";
      $valven_chart_pecs_join = join($valven_chart,",");
      $valven_chart_outros_join = join($valven_chart,",");  
    
    
    aplicando no highchart:
    
     xAxis: {
            categories: [<?php echo $nomreg_chart_join; ?>],
            crosshair: true
        },
    

    series: [{ name: 'Pieces', date: [ ]] }, { name: 'Other', date: [ ]

    }]

  • Create two sqls, where one will be filtering with Where "other", and Where "pieces".

  • only that in this way it will repeat the right SC? with Join would return 'PR, SC, SC' ?

  • I’m using code Igniter, and I got the result like this, $sql = "SELECT * FROM dadf513 WHERE anomes ='$anomes';"; $query = $this->db->query($sql); Return $query->result();

  • I will make a correction to the answer so that the format is displayed in the Categories correctly... and so that SC(region) does not repeat you can use a GROUP BY in sql

  • I use another routine with Rigger, but I don’t think I need to quote here, but in it I do the grouping GROUP BY codcin, codreg, and insert in the table where the data is ready, so there I use select *, in that case it needs to group by the classification code and by the region

  • If you are doing a group by in codreg, then you should not repeat in the query. And for it to be inserted correctly in the highcharts format it would be interesting to assemble something like I showed in the answer: $nomreg_chart_join = "'". implode("','",$nomreg_chart)."'";

  • Certain axis X ok

  • right, I’ll edit the answer to put the date with 'valven'.

Show 3 more comments

Browser other questions tagged

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