0

I am trying to use Chartjs with PHP and show a chart on my website. To do that, I get the "labels" from my database and add it to an array like this:

ChartController.php:

for($i = 0; $i < 5; $i++){
    $labels[] = Carbon::now()->startOfWeek()->addDay($i)->format("d M");
}

Above gives me below array, which I need to use as my labels:

array:5 [▼
  0 => "16 Jul"
  1 => "17 Jul"
  2 => "18 Jul"
  3 => "19 Jul"
  4 => "20 Jul"
]

Now, ChartJS labels needs to be formatted like this:

labels(['16 Jul', '17 Jul', '18 Jul', '19 Jul', '20 Jul'])

To do that, I use the implode() function like this:

labels("['".implode('\',\'', $labels). "']")

Which gives me this output:

['16 Jul','17 Jul','18 Jul','19 Jul','20 Jul']

However, this give me the following error:

Argument 1 passed to Fx3costa\LaravelChartJs\Builder::labels() must be of the type array, string given, called in /srv/users/serverpilot/apps/milestonechecker/app/Http/Controllers/ChartController.php on line 42

2
  • You tried to do like this: labels([Stging]), but you need labels(array). Please try this: labels($labels); Commented Jul 16, 2018 at 7:33
  • 1
    @VasylZhuryk Works like a charm. So simple, I didn't need to format it.. Please post this as an answer! :) Commented Jul 16, 2018 at 7:36

1 Answer 1

1

You tried to do like this: labels([String]), but you need to pass array as label. Please try this:

labels($labels);
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.