0

I have the following code:

<html><head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);
  function drawChart() {

    var data = google.visualization.arrayToDataTable([
      ['Polarity', 'Percentage'],
      ['positive',     0],
      ['negative',      0],
      ['neutral',  0]

    ]);

    var options = {
      title: 'Sentiment Chart',
      is3D: true,
    };

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

    chart.draw(data, options);
  }
</script>
</head>

<body>

<?php

$positive = 20;
$negative = 30;
$neutral = 50;

echo "<div id=\"piechart\" style=\"width: 900px; height: 500px;\"></div>";
?>

</body>
</html>

It is a Google Pie Chart. How can I pass the PHP variables $positive, $negative and $neutral to the 'positive', 'negative' and 'neutral' labels in the chart so that it shows their values of 20%, 30% and 50% respectively on the chart?

I have searched SO, but have not found a simple approach.

1

3 Answers 3

3

Put your php top of your document before all other code and then echo variables onto javascript like this:

 var data = google.visualization.arrayToDataTable([
  ['Polarity', 'Percentage'],
  ['positive',     <?php echo $positive ?>],
  ['negative',      <?php echo $negative ?>],
  ['neutral',  <?php echo $neutral ?>]

]);
Sign up to request clarification or add additional context in comments.

Comments

1

Just output them on place they need to be:

<?php echo $negative; ?>

In your case probably something like this:

<script type="text/javascript">
  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);
  function drawChart() {

    var data = google.visualization.arrayToDataTable([
      ['Polarity', 'Percentage'],
      ['positive',     <?php echo $positive; ?>],
      ['negative',      <?php echo $negative; ?>],
      ['neutral',  <?php echo $neutral; ?>]

    ]);

    var options = {
      title: 'Sentiment Chart',
      is3D: true,
    };

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

    chart.draw(data, options);
  }
</script>

1 Comment

Thanks. I tried that though and when I did the chart would not render at all for some reason..
0

By loading the PHP before and open the tags inside the code:

    <?php

    $positive = 20;
    $negative = 30;
    $neutral = 50;

    ?>

<html><head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {

        var data = google.visualization.arrayToDataTable([
          ['Polarity', 'Percentage'],
          ['positive',     <?php echo $positive; ?>],
          ['negative',      <?php echo $negative; ?>],
          ['neutral',  <?php echo $neutral; ?>]

        ]);

        var options = {
          title: 'Sentiment Chart',
          is3D: true,
        };

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

        chart.draw(data, options);
      }
    </script>
    </head>

    <body>

    <div id=\"piechart\" style=\"width: 900px; height: 500px;\"></div>

    </body>
    </html>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.