Render charts

Pass the variable to the view

To render charts, you'll need to pass the chart variable into a view.

return view('sample_view', compact('chart'));

Or more commonly known as

return view('sample_view', ['chart' => $chart]));

Load the JS library

You need to make sure you loaded the library of javascript before you render the chart. Check out the Supported Libraries

You can use the CDN provided by that page or use NPM / other method to load it.

Render the container

To render the container you'll just need to call the container() method on the chart in the desired HTMl place.

{!! $chart->container() !!}

Render the script

You'll need to call the script() method contained in the chart to place the script tag. Make sure it's out of scope for frameworks like Vue. A full example is provided below.


Remember to load the chart library before rendering the script.

{!! $chart->script() !!}

Sample HTML

This example uses vue to demostrate that it's posible to use a JS front-end framework with the library.

<!DOCTYPE html>
        <meta charset="utf-8">
        <title>Chart with VueJS</title>

        <div id="app">
            {!! $chart->container() !!}
        <script src=""></script>
            var app = new Vue({
                el: '#app',
        <script src= charset=utf-8></script>
        {!! $chart->script() !!}

The JS chart variable

Turns out you might want to make use of the chart while it's still there.

All charts are binded to a JS variable, by default, this variable is generated in the constructor, creating a random 25 characters long id with only lowercase alphabetic characters. This is the ID of the chart. If you ever need to modify the chart after it's rendered (eg: because you want to add, remove data) you may acces it in the following JS variable.

window.{{ $chart->id }}
Last Updated: 9/3/2018, 7:44:57 PM