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

The beginner's avatar

Chart.js with php

Hello,

I'm using Chart.js and i defined a max and min values of my Y axis, but i want to have '%' next to my values.

Is it possible to add '%' only with php ?

Because it didn't work when i did that :

'max' => 30+'%'

or 

'max' => 30%

Thanks.

0 likes
15 replies
Sinnbeck's avatar

Show the surrounding code so we can see the context. Just a little on each side of this code

Concat with php uses a dot

'max' => 30 . '%'
//but why concat if it isn't a variable?
'max' => '30%'
1 like
The beginner's avatar

@Sinnbeck

I've already tried that but i doesnt seems to work.

This is my code :

     'scales' => [
                'yAxes' => [
                    [
                        'ticks' => [
                            'max' => 30,
                            'min' => 10,
                        ]
                    ],
	]
furqanDev's avatar

@The beginner It won't work.

The graph is drawn according to the values you pass. If you pass 30 then they can parse it to integer and draw the map if you pass 30% it will not be an integer.

1 like
The beginner's avatar

@Sinnbeck

If i try to concat this is what i got :

A non-numeric value encountered

The code that i've shown you work because it's only integer but if i add that : .'%'

it'll not work anymore

The beginner's avatar

@furqanDev

Yes, it only accepts integer so i'm asking how could i put that character '%' next to my values

Sinnbeck's avatar

@The beginner well I assume you somehow convert that php to a config for chartjs?

furqanDev's avatar

@The beginner Did you read the documentation ?

Check it and see if by any chance they provide this kind of option or not. I think there will be some option of prefix or postfix for datasets.

1 like
The beginner's avatar

@furqanDev Hi, sorry for the delayed response...

The documentation is in js only i was looking for the "php way". But i'll give it a look

furqanDev's avatar
Level 18

@The beginner No worries.

There is not php way I'm afraid because the Charts are rendered through JS. You pass some values as a json response from php backend and set those values/object/array as a dataset. Those dataset should be number because the chart will be drawn according to that number. If they have option to add prefix that would be perfect.

1 like
The beginner's avatar

@furqanDev

Alright, thanks for your response.

I'm still going to try to find a solution with php because i might have an idea... even if i think you're right.

Thanks again :)

Please or to participate in this conversation.