shone83
3 weeks ago
263
17
General

Google chart not working with array, just with number

Posted 3 weeks ago by shone83

This is my google chart code:

   <script type="text/javascript">
    google.charts.load('current', {'packages':['corechart']});
    google.charts.setOnLoadCallback(drawVisualization);

    function drawVisualization() {
      // Some raw data (not necessarily accurate)
      var data = google.visualization.arrayToDataTable([
       [
         'Месни одбори', 
         'Капиларни',
         'Последњи резултат',
         'Очекивани резултат'
       ],

       @if ($settlements)

         @foreach ($settlements as $settlement)

           @if (($settlement->members->count()) > '0')

             [

               '{{$settlement->name}}',
                {{$settlement->members->count()}},
                {{$last_results}},
                {{$expected_results}}

             ],

           @endif

         @endforeach

       @endif

    ]);

    var options = {
      title : 'Реон1',
      seriesType: 'bars',
      series: {
        1: {type: 'line'},
        2: {type: 'line'}
      },
      colors: ['blue', 'red', 'purple'],
    };

    var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }
  </script>

  <div id="chart_div" style="width: 'auto'; height: 500px;"></div>

and this is my variables in laravel controller:

     $settlements = Settlement::where('town_id', Auth::user()->town_id)
      ->where('coordinator_id', '1')
      ->with('members')
      ->get();

    $last_results = Settlement::with('members')->whereHas('members', function($query) {
      $query->where('coordinator_id', '1');
      })->where('town_id', Auth::user()->town_id)
      ->pluck('last_result');

    $expected_results = Settlement::with('members')->whereHas('members', function($query) {
      $query->where('coordinator_id', '1');
      })->where('town_id', Auth::user()->town_id)
      ->pluck('expected_result');

Without variables $last_results and $expected_results working fine but when I add them google chart disappear. I have one more chart where is working fine but there are lines where is only one number, here is an array of numbers.

Please sign in or create an account to participate in this conversation.