詳細情報を提供していただければ幸いです。JavaScript スクリプトで使用する PHP データを渡す方法を説明するために、私が作成した棒グラフのコードをいくつか示します。
<html>
<head>
<?php
$hits = array(
array("day"=>10,"count" =>53),
array("day"=>11,"count" =>67),
array("day"=>12,"count" =>85),
array("day"=>13,"count" =>57),
array("day"=>14,"count" =>65),
array("day"=>15,"count" =>71),
array("day"=>16,"count" =>85),
array("day"=>17,"count" =>106),
array("day"=>18,"count" =>55),
array("day"=>19,"count" =>96),
);
//this counter will be used later on the foreach
$counter = count($hits);?>
<meta charset="utf-8">
<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([
['Day',
'Number of hits'],
<?php foreach ($hits as $key =>$hit):?>
<?php /*if the key is equal to the counter-1 it means we've reached
the end of our array in that case the javascript array,
won't have a comma at the end, or else it'll give a
unexpected identifier error*/
if(($counter-1)==$key):?>
['<?=$hit["day"]?>', <?=$hit["count"]?>]
<?php else:?>
['<?=$hit["day"]?>', <?=$hit["count"]?>],
<?php endif;?>
<?php endforeach;?>
]);
var options = {
title: 'Number of hits per day',
hAxis: {title: 'Day', titleTextStyle: {color: 'blue'}}
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>