带有Google图表的Laravel 6.0无法显示数据

时间:2019-12-26 13:45:51

标签: laravel charts

Laravel 6.0和Google Charts出现问题,我无法表示数据库中具有该功能的数据

public function index()
{

$data = DB::table('incidencias')
   ->select(
    DB::raw('requiere_rep as requiere_rep'),
    DB::raw('count(*) as number'))
   ->groupBy('requiere_rep')
   ->get();
 $array[] = ['Requiere_rep', 'Number'];
 foreach($data as $key => $value)
 {
  $array[++$key] = [$value->requiere_rep, $value->number];
 }
    return view('Incidencias.index')->with('requiere_rep', json_encode($array));
 }

这是我的剧本

<script type="text/javascript">

var analytics = <?php echo $requiere_rep; ?>
google.charts.load("current", {packages:['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
  var data = google.visualization.arrayToDataTable(analytics);

  var view = new google.visualization.DataView(data);
  view.setColumns([0, 1,
                   { calc: "stringify",
                     sourceColumn: 1,
                     type: "string",
                     role: "annotation" },
                   2]);

  var options = {
    title: "TOP INCIDENCIAS POR CLIENTES",
    width: 380,
    height: 180,
    bar: {groupWidth: "95%"},
    legend: { position: "none" },
  };
  var chart = new google.visualization.ColumnChart(document.getElementById("columnchart_values"));
  chart.draw(view, options);

}   

这是我的HTML

<div id="columnchart_values" style="width: 900px; height: 300px; margin-top: 10%; margin-left: -431%;"></div>

有人可以帮我吗?谢谢!

2 个答案:

答案 0 :(得分:0)

您可以在Blade模板中使用{{ }}{!! !!}语法。最好只将没有json_encode的数据传递给视图,这样您仍然可以操纵数组,然后使用:

{!! json_encode($var) !!}或Blade助手@json($var)来将您的数据回传到javascript中,如下所示:

<script>
    var myvar = @json($array);
</script>

答案 1 :(得分:0)

我找到了解决方法,这里是

<script type="text/javascript">
var analytics = <?php echo $requiere_rep; ?>

google.charts.load('current', {'packages':['corechart']});

google.charts.setOnLoadCallback(drawChart);

function drawChart()
{
var data = google.visualization.arrayToDataTable(analytics);
var options = {
title : 'INCIDENCIAS POR REGIONES'
};
var chart = new google.visualization.ColumnChart(document.getElementById('pie_chart'));
chart.draw(data, options);
}