gpt4 book ai didi

javascript - 如何将整数变量从 PHP 传递到 Javascript Google Chart API?

转载 作者:行者123 更新时间:2023-11-28 15:31:49 27 4
gpt4 key购买 nike

我有以下代码:

<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>

这是一个 Google 饼图。如何将 PHP 变量 $positive、$neutral 和 $neutral 传递给图表中的“正”、“负”和“中性”标签,以便在图表上分别显示它们的值 20%、30% 和 50%图表?

我已经搜索过,但没有找到简单的方法。

最佳答案

将 php 放在文档顶部所有其他代码之前,然后将变量回显到 javascript 上,如下所示:

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

]);

关于javascript - 如何将整数变量从 PHP 传递到 Javascript Google Chart API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26992341/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com