如何使用Chart.js在Laravel中传递数据

问题描述 投票:0回答:1

我想使用Laravel中的Chartjs在图表的学习表中显示男女总数。

我的控制器

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');
    $learning = $men_learning + $women_learning  ;
    return view('home', compact('learning'));
}

我的刀片视图脚本。

<script>
    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
        type: 'bar',
        data: {
            labels: ['total'],
            datasets: [{
                label: '# of Votes',
                data: [12],
                backgroundColor: [
                    'rgba(255, 99, 132, 0.2)',

                ],
                borderColor: [
                    'rgba(255, 99, 132, 1)',
                ],
                borderWidth: 1
            }]
        },
        options: {
            scales: {
                yAxes: [{
                    ticks: {
                        beginAtZero: true
                    }
                }]
            }
        }
    });
</script>

如何将加载的统计信息从脚本传播到图表?

laravel chart.js
1个回答
0
投票
    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>
© www.soinside.com 2019 - 2024. All rights reserved.