0

I need show temperature monthly to charts in laravel my view page consist temp.blade

<script>
    console.log({!! $temp !!});
    console.log({!! $dateTemp !!});
    window.onload = function() {
    var ctx = document.getElementById("myChart");
    var myChart = new Chart(ctx, {
        type: 'line',
        data: {
            labels: {!! $temp !!},
            datasets: [{
                label: 'Temperature',
                data: {!! $dateTemp !!},
                borderWidth: 1
            }]
        }
    });
    }
</script>

and controller

public function tempChart()
{
    $temp = Temps::select(DB::raw('temp'))
        ->orderBy('date_temp','asc')
        ->get();
    $temp->implode(',',$temp);
    $dateTemp = Temps::select(DB::raw('temps'))
        ->select('date_temp')
        ->orderBy('date_temp','asc')
        ->get();
    $dateTemp->implode(',',$dateTemp);
    //dd($temp,$dateTemp);
    return view('report/temp')
        ->with('temp',$temp)
        ->with('dateTemp',$dateTemp);
}

it can not show data array but it show

[{...}],[{...}],[{...}]

2 Answers 2

3

I am not sure which Chart library you are using but most of them wants an array of strings or integer and you give them array of objects.

I think that you just need to convert your array in the php (or in the javescript)

php way:

$temp = Temps::select('temp'`)
    ->orderBy('date_temp','asc')
    ->get()
    ->pluck('temp');


$dateTemp = Temps::select(['temps', 'data-temp'])
    ->orderBy('date_temp','asc')
    ->get()
    ->pluck('data-temp'); // I am not whats the acual query you want but this is the idea

and then if you using laravel 5.5 just write on your blade

<script>
window.onload = function() {

    var ctx = document.getElementById("myChart");
    var myChart = new Chart(ctx, {
        type: 'line',
        data: {
            labels: @json($temp),
            datasets: [{
                label: 'Temperature',
                data: @json($dateTemp) ,
                borderWidth: 1
            }]
        }
    });

}
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

Try add high comma to Data?

        datasets: [{
            label: 'Temperature',
            data: '{!! $dateTemp !!}',
            borderWidth: 1
        }]

4 Comments

No , error Uncaught TypeError: Object.defineProperty called on non-object
array:3 [▼ 0 => array:1 [▶] 1 => array:1 [▶] 2 => array:1 [▶] ] array:3 [▼ 0 => array:1 [▶] 1 => array:1 [▶] 2 => array:1 [▶] ]
what is about json_encode on the array? not implode
I use both toarray and implode the same results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.