How to make a pie chart (pie) with database?

Asked

Viewed 4,835 times

4

I have the table in my database called votes, I have id, name and votes (number of votes), I wanted to plot these values being, name and value in a pie chart or whatever the percentage of votes has each name

<?
      $SQL1 = "SELECT * FROM votes";

       $result1 = mysql_query($SQL1);
       $data1 = array();
       $data2 = array();
       while ($row = mysql_fetch_array($result1)) {
           $data1[] = $row['name'];
           $data2[] = hexdec($row['votes']);
       } 
?>

<script src="js/grafico/highcharts.js"></script>
<script src="js/grafico/exporting.js"></script>

<script type="text/javascript">
 $(function () {
    $('#graficoPizza').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: 'Grafico Plataforma SMS'
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                }
            }
        },
        series: [{
            type: 'pie',
            name: 'Quantidade enviados',
            data: [
                [<?php echo join("','", $data1) ?>,  <?php echo join(',',$data2) ?>]
            ]
        }]
    });
});
</script>

<div id="graficoPizza" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
  • 1

    could post the [tag:json] which is returned by data: [[<?php echo join("','", $data1) ?>, <?php echo join(',',$data2) ?>]] ? It is an essential factor to know what is happening with your code

  • http://crissite.com/GFILA/index.php?pag=grafico

  • I’m kind of layy in javascript I’m learning now more json is what returns it or is values?

  • Your date it must be so data:[{&#xA; name: 'Fulano',&#xA; data: [49.9]&#xA;}, {&#xA; name: 'Cicrano',&#xA; data: [83.6]&#xA;}, {&#xA; name: 'Beltrano',&#xA; data: [48.9]&#xA;}, {&#xA; name: 'Arlindo',&#xA; data: [42.4]&#xA;}]

  • database table: votes id=1 name= Cristiano vote=1,

1 answer

5


So that your pie chart of the hightcharts work well you need your is being generated correctly according to the model

series: [{
    type: 'pie',
    name: 'Browser share',
    data: [
        ['Fulano',   27],
        ['Cicrano',       32],
        ['Beltrano',    25],
        ['Toinho',     12]
    ]
}]

As you can see in graph of the cane feet this format defines how the information will be displayed on the chart

This way you would have to format your data so that they can fit the chart.
This must be yours

$SQL1 = "SELECT * FROM votes";

$result1 = mysql_query($SQL1);
$data = array();
while ($row = mysql_fetch_array($result1)) {
   $data[] = array($row['name'], hexdec($row['votes']));
}

$jSon = json_encode($data);

And his should look like this:

$(function () {
    $('#graficoPizza').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: 'Grafico Plataforma SMS'
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                }
            }
        },
        series: [{
            type: 'pie',
            name: 'Quantidade enviados',
            data: <?php echo $jSon;?>
        }]
    });
});

This solves your problem and you will have a chart thus

  • Dear you are 10.... if you have any way that I can reward you for this help contact me E-mail: [email protected] Thank you very much!!!

  • Mark the answer as correct that for me is more than enough, hehe. We are here to help, this is how the community is strengthened.

Browser other questions tagged

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