Laravel-谷歌折线图未显示



我创建了一个折线图,其中按日期显示总销售额,但返回错误"轴#0的数据列不能为字符串类型"。我检查了我的视图源,它为我的折线图返回了正确的数据。我真的不知道问题出在哪里。有人能帮我如何显示图表吗?

我的视图源数据

["18th April 2020","64.50"],["19th April 2020","91.00"],["20th April 2020","644.70"],["21st April 2020","120.50"]

我的控制器

<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use DB;
class LaravelGoogleGraph extends Controller
{
function index()
{
$data = DB::table('booking_detail')
->select(
DB::raw('sum(total_price) as sums'), 
DB::raw("DATE_FORMAT(date_sell,'%D %M %Y') as months"))
->groupBy('months')
->get();
$array[] = ['Date', 'Sales'];
foreach($data as $key => $value)
{
$array[++$key] = [$value->months, $value->sums];
}
return view('google_pie_chart')->with('months', json_encode($array));
}
}
?>

我的刀片

<!DOCTYPE html>
<html>
<head>
<title>Make Google Pie Chart in Laravel</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<style type="text/css">
.box{
width:600px; height:400px;
}
</style>
<script type="text/javascript">
var analytics = <?php echo $months; ?>
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart()
{
var data = google.visualization.arrayToDataTable(analytics);
var options = {
title : 'Booking Sales'
};
var chart = new google.visualization.LineChart(document.getElementById('line_chart'));
chart.draw(data, options);
}
</script>
</head>
<body>
<br />
<div class="container">
<h3 align="center">Booking Report</h3><br />

<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Booking Sales</h3>
</div>
<div class="panel-body" align="center">
<div id="line_chart" style="width:550px; height:350px;">
</div>
</div>
</div>

</div>
</body>
</html>

行的值应该是数字,而不是字符串。。。

删除每行第二个值周围的引号,
更改。。。

["18th April 2020","64.50"]

到。。。

["18th April 2020",64.50]

此外,arrayToDataTable还有第二个可选布尔参数。。。

opt_firstRowIsData-第一行是否定义标题行。如果为true,则假定所有行都是数据。如果为false,则假定第一行为标题行,并将值指定为列标签。默认值为false。

如果数组中的第一行不表示列标题,
则应包含true作为第二个参数,此处。。。

var data = google.visualization.arrayToDataTable(analytics, true);

最新更新