I create a line chart where it shows a total sales by a date but it return an error 'Data column(s) for axis #0 cannot be of type string'. I check my viewsource and it return a right data for my line chart. I don't really know whats the problem. Can anyone help me with this, on how to display the chart?
My viewsoure data
["18th April 2020","64.50"],["19th April 2020","91.00"],["20th April 2020","644.70"],["21st April 2020","120.50"]
My Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
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));
}
}
?>
My blade
<!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>