1

I am using chart.js to show some data, and everything was working fine when the code was this:

JavaScript/Blade

 var pieDataAssignments = [

                @for($i = 0;$i<count($name);$i++)

            {
                value: {!! count(array_filter($ass_c,create_function('$a','return $a=="'.$name[$i].'";')))!!},
                color: 'pink',
                highlight: "#eaa5a2",
                label: 'Subject'
            },

                @endfor

        ];

Then I thought it was working, which it was, and I thought to start customising the Pie Chart.

Firstly I tried inserting the value for each variable individually, e.g.

color: {!! $colour[$i] !!},
etc...

That just prevented the pie chart from appearing, which led me to ponder my failure..

I then tried to insert everything into the first Blade call, like so:

Blade

{!!'

{
    value:'.count(array_filter($ass_c,create_function('$a','return $a=="'.$name[$i].'";'))).',
    color: "'.$colour[$i].'",
    highlight: "#eaa5a2",
    label: "hi"
},

'!!}

But still, it did not show up.

How can I go about looping JavaScript code and passing php variables at the same time? I don't know what else to try and I can't find anything online apart from telling me to do exactly what I tried.

Thank you in advance.

2
  • can you provide (at least an excerpt) of the rendered javascript code? I think at the very least inspecting that could lead you to see where the issue is Commented Oct 21, 2015 at 16:18
  • I just solved it, by doing exactly what you said, thank you @watcher, I'll remember this next time something doesn't work :) Commented Oct 21, 2015 at 16:23

1 Answer 1

0

I fixed the issue, the code I had was recreating the array inside it's own instance. The code that now works is the following:

var pieDataAssignments = [

        @for($i = 0;$i<count($name);$i++)

        {!!'

        {
            value:'.count(array_filter($ass_c,create_function('$a','return $a=="'.$name[$i].'";'))).',
            color: "'.$colour[$i].'",
            highlight: "#eaa5a2",
            label: "'.$name[$i].'"
        },

        '!!}



        @endfor

];
Sign up to request clarification or add additional context in comments.

Comments

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.