How to land % to integer on google Harts

Asked

Viewed 667 times

1

I got this Chart: inserir a descrição da imagem aqui

In the visible part this percentage, as I do to change the percentage by the whole quantity:

Example:

As shown in the attached image instead of showing 14.3% show 1.

Follows my code:

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">

      google.charts.setOnLoadCallback(drawSetores);


      function drawSetores() {


        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Topping');
        data.addColumn('number', 'Slices');
        data.addRows([
        <?php
        $setores = mysql_query("SELECT  
        distinct sol_setor AS ID,
        st.setor_nome AS SETOR,
        CONCAT(count(s.sol_setor)) AS QUANT
        FROM
         solicitacao AS s 
         INNER JOIN  setor  AS st ON s.sol_setor = st.setor_id
         GROUP BY sol_setor");
         while ($r = mysql_fetch_array($setores)) {
            echo "['$r[1]', $r[2]],\n";
        }
        ?>
        ]);

        // Set options for Sarah's pie chart.
        var options = {title:'SETORES',
                       width:550,
                       height:300,
                       is3D:true};

        var chart = new google.visualization.PieChart(document.getElementById('Setores'));
        chart.draw(data, options);
      }

  • Hi Otácio, cool the question, thanks!

1 answer

2


Add to option the property pieSliceText : 'value'

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

      function drawChart() {

        var data = google.visualization.arrayToDataTable([
          ['Task', 'Hours per Day'],
          ['Work', 11],
          ['Eat', 2],
          ['Commute', 2],
          ['Watch TV', 2],
          ['Sleep', 7]
        ]);

        var options = {
          title: 'My Daily Activities',
          pieSliceText: 'value'
        };

        var chart = new google.visualization.PieChart(document.getElementById('piechart'));

        chart.draw(data, options);
      }
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="piechart" style="width: 900px; height: 500px;"></div>

In options from Piechart, you can configure the pieSliceText as follows:

  • 'percentage' - The percentage of the slice in relation to the total.
  • 'value' - The quantitative value of the slice.
  • 'label' - The name of the slice.
  • 'None' - No text.

also need to put $ and turn to decimal is possible ?

Yes, just add before the method draw the formatter:

var formatter = new google.visualization.NumberFormat({decimalSymbol: ',',groupingSymbol: '.', negativeColor: 'red', negativeParens: true, prefix: 'R$ '});
formatter.format(data, 1);

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

function drawChart() {

  var data = google.visualization.arrayToDataTable([
    ['Task', 'Hours per Day'],
    ['Work', 1123121],
    ['Eat', 3123212],
    ['Commute', 213212],
    ['Watch TV', 123122],
    ['Sleep', 1231237]
  ]);

  var options = {
    title: 'My Daily Activities',
    pieSliceText: 'value'
  };

  var chart = new google.visualization.PieChart(document.getElementById('piechart'));

  var formatter = new google.visualization.NumberFormat({
    decimalSymbol: ',',
    groupingSymbol: '.',
    negativeColor: 'red',
    negativeParens: true,
    prefix: 'R$ '
  });
  formatter.format(data, 1);

  chart.draw(data, options);
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="piechart" style="width: 900px; height: 500px;"></div>

  • Perfect, Thank you.

  • I added the values to the property, @Otáciobarbosa.

  • It is also part of what I need, also need to put R $ and turn to decimal is possible ?

  • You want to show monetary values in place of percentage and/or integer?

  • yes, and if possible include the acronym R$

  • Yes, it’s possible. I edited the answer and includes, @Otáciobarbosa.

  • Great. Thank you again.

Show 2 more comments

Browser other questions tagged

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