Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TrinityMH's avatar

Vue - Chart JS | Laravel

Hi. I need help!

I found something like this: https://vue-chartjs.org/guide/#chart-with-props Everything works with the example of local data.

The problem is with the API. I don't know what data to throw by PHP to capture my data in ChartsJS.

My code:

PHP Controller:

public function charts($id)
{

    $post_data = array(
            'date' => '2020-09-19', // labels
            'in' => '1024', // datasets
            'out' => '1024', // datasets
    );

    return response()->json($post_data);
}

ChartsComponent.vue

<template>
        <line-chart
                v-if="loaded"
                :chartdata="chartdata"
                :options="options"/>
            </div>
</template>

<script>
    import LineChart from './Chart.vue'

    export default {
        name: 'LineChartContainer',
        components: { LineChart },
        data: () => ({
            loaded: false,
            chartdata: null
        }),
        async mounted () {
            this.loaded = false
            try {
                const { stats } = await fetch('/dedicated/server/142/charts')
                this.chartdata = stats
                this.loaded = true
            } catch (e) {
                console.error(e)
            }
        }
    }
</script>

Chart.vue

<script>
    import { Line } from 'vue-chartjs'

    export default {
        extends: Line,
        props: {
            chartdata: {
                type: Object,
                default: null
            },
            options: {
                type: Object,
                default: null
            }
        },
        mounted () {
            this.renderChart(this.chartdata, this.options)
        }
    }
</script>
0 likes
9 replies
Sinnbeck's avatar

Perhaps try recreating the local data in php and test with that? If that works, try getting your own data to fit into this structure

$data =[
   'labels'  => ['January', 'February'],
      'datasets' => [
        [
          'label' => 'Data One',
          'backgroundColor' => '#f87979',
          'data' => [40, 20]
       ], 
      ];

return response()->json($data);
TrinityMH's avatar

[Vue warn]: Property or method "options" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

I fixed this way:

        data: () => ({
            loaded: false,
            options: null,
            chartdata: null
        }),

The chart remains blank.

https://i.imgur.com/uVIXVKB.jpg

usaandi's avatar

console.log (stats)

So you can check if anything even console.logs

usaandi's avatar
usaandi
Best Answer
Level 2
  let response = await fetch('/dedicated/server/142/charts');
  let data = await response.json()

  console.log( response );

Do you get anything

TrinityMH's avatar
Response {type: "basic", url: "http://localhost:81/dedicated/server/142/charts", redirected: false, status: 200, ok: true, …}
body: (...)
bodyUsed: true
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "basic"
url: "http://localhost:81/dedicated/server/142/charts"
__proto__: Response
TrinityMH's avatar

Yes. Everything works as it should. Thank you.

Please or to participate in this conversation.