[Chartjs]-How to pass data in Laravel with Chart.js

3๐Ÿ‘

โœ…

    public function index()
    {
        $men_learning = DB::table('learnings')->where('active', 1)->whereYear('created_at', $year)->sum('men');
        $women_learning = DB::table('learnings')->where('active', 1)->whereYear('created_at', $year)->sum('women');
        return view('home', compact('men_learning', 'women_learning'));
    }


   <script type="text/javascript">
  $(function(){
    var ctx = document.getElementById("myChart").getContext('2d');
    var myChart = new Chart(ctx, {
      type: 'bar',
      data: {
        labels: ['Men', 'Women'],
        datasets: [{
          label: 'Men',
          data: [{!!$men_learning!!}],
          borderWidth: 2,
          backgroundColor: 'rgba(40,167,69,1)',
          borderWidth: 0,
          borderColor: 'transparent',
          pointBorderWidth: 0 ,
          pointRadius: 3.5,
          pointBackgroundColor: 'transparent',
          pointHoverBackgroundColor: 'rgba(254,86,83,.8)',
        },
        {
          label: 'Women',
          data: [{!!$women_learning!!}],
          borderWidth: 2,
          backgroundColor: 'rgba(220,53,69,.8)',
          borderWidth: 0,
          borderColor: 'transparent',
          pointBorderWidth: 0,
          pointRadius: 3.5,
          pointBackgroundColor: 'transparent',
          pointHoverBackgroundColor: 'rgba(63,82,227,.8)',
        }]
      },
      options: {
        legend: {
          display: false
        },
        scales: {
          yAxes: [{
            gridLines: {
              display: true,
              drawBorder: false,
              color: '#f2f2f2',
            },
            ticks: {
              beginAtZero: true,
              stepSize: 100,
              callback: function(value, index, values) {
                return value;
              }
            }
          }],
          xAxes: [{
            gridLines: {
              display: false,
              tickMarkLength: 15,
            }
          }]
        },
      }
    });
});
</script>

Leave a comment