gpt4 book ai didi

php - 显示包含来自数据源的信息的 Google 图表

转载 作者:行者123 更新时间:2023-11-29 22:00:02 26 4
gpt4 key购买 nike

我目前正在尝试显示一个谷歌图表,其中填充了我从数据源提取的信息。我已经建立了与数据库的连接,我可以查询我想要的数据并将结果存储在 json 对象中。我遇到的问题是显示图表。我已经很接近了,但还没有到那一步。这是我的 php 代码中的内容

`<?php


$mysqli =mysqli_connect('localhost', 'root', '', 'prueba');
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: ".mysqli_connect_error();
}

$sql = mysqli_query($mysqli, 'SELECT * FROM pizza');

if (!$sql) {
die("Error running $sql: " . mysql_error());
}

$results = array(
'cols' => array(
array('label' => 'Topping', 'type' => 'varchar'),
array('label' => 'cantidad', 'type' => 'number')
),
'rows' => array()
);

while($row = mysqli_fetch_assoc($sql))
{
$results = array(
'Topping' => $row['Topping'],
'cantidad' => $row['cantidad']
);
$results ['rows'][] = array('c' => array(
array('v' => $row['Topping']),
array('v' => $row['cantidad'])
));
}
$json = json_encode($results, JSON_NUMERIC_CHECK);
echo $json;

?>`

然后,我的图表 html 代码:

     <head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">

// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});

// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);

// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.

function drawChart() {

// Create the data table.
$.ajax({
url: 'http://localhost/cakesetup/pizza',
dataType: "json",
success: function (jsonData){
var data = new google.visualization.DataTable(jsonData);

// Set chart options
var options = {'title':'Distribucion de pizzas',
'width':800,
'height':600};
}
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
chart.draw(data, options);
});
}
</script>
</head>

<body>
<!--Div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
</html>
</div>
</div>

当我运行代码时,我得到的是这个 "{"Topping":"Peperonni","cantidad":6,"rows":[{"c":[{"v":"Peperonni"}, {“v”:6}]}]}“

提前致谢。

最佳答案

检查您的网址

// Create the data table.
$.ajax({
url: 'http://localhost/cakesetup/pizza', // i dont think a url localhost will work. try cakesetup or /pizza
dataType: "json",
success: function (jsonData){
var data = new google.visualization.DataTable(jsonData);

// Set chart options
var options = {'title':'Distribucion de pizzas',
'width':800,
'height':600};
}
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
chart.draw(data, options);
});

关于php - 显示包含来自数据源的信息的 Google 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32747042/

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