vendredi 28 décembre 2018

How to produce charts from data in database using laravel package?

I want to create pie chart using data from database using chart package from laravel.

I queried the values using this sql statement:

 $gender = DB::table('results')
            ->SELECT (DB::raw("SUM(CASE WHEN results.gender_id = 1 THEN 1 ELSE 0 END) as Male_count,
   SUM(CASE WHEN results.gender_id = 2 THEN 1 ELSE 0 END) as Female_count"))
             ->get();

I had queried the values into an array:

 Collection {#233 ▼
 #items: array:1 [▼
 0 => {#234 ▼
  +"Male_count": "2"
  +"Female_count": "1"
   }
  ]
  }

The code to generate the pie chart is as below:

   $chart = Charts::database($gender, 'pie', 'highcharts')
            ->title('Respondents Distribution by Gender')
            ->elementLabel("Gender")
            ->dimensions(1000,500)
            ->responsive(false);
  return view('gender_chart',compact('chart'));

However, the chart blade displays with no error but the values is null. So, no pie chart appear..only labels showing 0% values appear.

How to retrieve the gender counts of male and female from database and can be read by the chart?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire