laravel-chartjs icon indicating copy to clipboard operation
laravel-chartjs copied to clipboard

[ask] add label on pie chart

Open edwinnsan opened this issue 6 years ago • 20 comments

Hello guys, im already success to create pie chart, this is the result screenshot from 2019-01-28 10-54-50

but i want add label inside chart like this screenshot from 2019-01-28 10-55-03

can we do thing like this in chart js?thanks

edwinnsan avatar Jan 28 '19 03:01 edwinnsan

Use the data label plugin.

Mohammad-Yasin-Noori avatar Feb 04 '19 07:02 Mohammad-Yasin-Noori

First add the script file

then

add these lines of code to your optionsRaw in Controller: 'plugins' => [ 'datalabels' => [ 'borderColor' => 'white', 'borderRadius' => 25, 'borderWidth' => 2, 'color' => 'white', 'formatter' => 'Math.round' ] ],

Mohammad-Yasin-Noori avatar Feb 04 '19 08:02 Mohammad-Yasin-Noori

thanks for your answer, but when i put this script <script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>

i got an error like this screenshot from 2019-02-06 10-18-49

do you know why this happen?Thanks!

edwinnsan avatar Feb 06 '19 03:02 edwinnsan

This is because of the compatibility issue. Put this version of chart js

Mohammad-Yasin-Noori avatar Feb 06 '19 04:02 Mohammad-Yasin-Noori

already tried it, but get the another error like this.. screenshot from 2019-02-06 10-25-52

this is because the script or not? Thanks again btw

edwinnsan avatar Feb 06 '19 04:02 edwinnsan

Are you working with Ajax Call?

Mohammad-Yasin-Noori avatar Feb 06 '19 04:02 Mohammad-Yasin-Noori

Also, add your code snippet to better communicate

Mohammad-Yasin-Noori avatar Feb 06 '19 04:02 Mohammad-Yasin-Noori

No im not using ajax for this chart.. and this is my code

screenshot from 2019-02-06 11-56-46 screenshot from 2019-02-06 11-57-01

edwinnsan avatar Feb 06 '19 04:02 edwinnsan

then i get this error screenshot from 2019-02-06 12-00-40

edwinnsan avatar Feb 06 '19 05:02 edwinnsan

change the labels array inside plugins to datalabels AND also if you are trying to set font-size to datalabels use "font.size" instead of "FontSize".

Mohammad-Yasin-Noori avatar Feb 06 '19 05:02 Mohammad-Yasin-Noori

i already tried to change labels to datalabels, but i dont find the font-size, because im not setting it. why this happen

edwinnsan avatar Feb 06 '19 05:02 edwinnsan

The third error you faced is because of the Hover attribute try to remove it and test it again or try to work it in events.

Try to remove any other templates styles or scripts just keep chartjs and datalabels plugin in your blade files then test it.

There may be some compatibility issues.

Mohammad-Yasin-Noori avatar Feb 06 '19 05:02 Mohammad-Yasin-Noori

ah, now i got no error.. but still i cant see the label.. still like this screenshot from 2019-02-06 12-13-40

and this is my optionraw code screenshot from 2019-02-06 12-14-10

edwinnsan avatar Feb 06 '19 05:02 edwinnsan

a tiny issue when you open optionsRaw like a json keep all the json braces everywhere inside the optionsRaw function but if you treat to use it like array keep it all array. as this code

Mohammad-Yasin-Noori avatar Feb 06 '19 05:02 Mohammad-Yasin-Noori

oh because i ever try this code on my other chart and success..like this screenshot from 2019-02-06 12-24-55

but i try my code into code like yours, but still no result screenshot from 2019-02-06 12-25-26

edwinnsan avatar Feb 06 '19 05:02 edwinnsan

Check for the anchor (alignment) and the colors if there is not any error in console.

Mohammad-Yasin-Noori avatar Feb 06 '19 05:02 Mohammad-Yasin-Noori

screenshot from 2019-02-06 12-57-37

i set same option like yours, but still not get the datalabel in the chart

edwinnsan avatar Feb 06 '19 05:02 edwinnsan

Any error in your browser`s console?

Mohammad-Yasin-Noori avatar Feb 06 '19 06:02 Mohammad-Yasin-Noori

no, there is no error in my console..

edwinnsan avatar Feb 06 '19 06:02 edwinnsan

Any way to make it work on the latest version?

ChartJs 3.5 and plugin 2.0?

saulob avatar Sep 17 '21 15:09 saulob