Home > Software design >  How can I display the product sales per month using a Line Graph?
How can I display the product sales per month using a Line Graph?

Time:01-22

$monthlySales = OrderProduct::selectRaw('sum(amount) as total_sales, month(created_at) as month, year(created_at) as year')
    ->groupBy('month', 'year')
    ->get();

$labels = $monthlySales->pluck('month')->toArray();
$data = $monthlySales->pluck('total_sales')->toArray();

This is the line graph:

var xValues = ['January','Febuary','March','April','May','June','July','August','September','October','November','December'];
var yValues = [{{$data}}];

new Chart("myChart", {
    type: "line",
    data: {
        labels: xValues,
        datasets: [{
            fill: false,
            lineTension: 0,
            backgroundColor: "rgba(0,0,255,1.0)",
            borderColor: "rgba(0,0,255,0.1)",
            data: yValues
        }]
    },
    options: {
        legend: {display: false},
        scales: {
            yAxes: [{ticks: {min: 0, max:{{$sales}}}}],
        }
    }
});

I tried to use a for loop to make an array for the data but there's still no visible data, or maybe I don't have enough data in database?

CodePudding user response:

you can change your yValue in js like:

var yValues = @json($data);

Works like a charm

  • Related