Maxime74
Posts: 73
Joined: Wed Sep 17, 2014 8:34 am

CLI highcharts-export-server : custom function for label formatter

Hello.

I've implemented functions to have an adaptative size to my labels in my treemaps.
That works as expected on a webpage.

However, when I want to generate my treemap as a static image with a CLI, I don't manage to call my custom function to format the points' datalabel.

I'm running a php script that retrieve the chart's options from a .json file, then get the data, and inject it in the JSON.
The JSON is then stringified, written into a file, and used for the `-infile` parameter of the CLI.
This part works as expected.

My problem is when I try to set my custom function to the series.datalabels.formatter option.
As a JSON can't have function for value, I first `json_encode` my chart options, and then set my function with a `str_replace`.
When performing the CLI, I get an error :
"Chart input data error
ReferenceError: Can't find variable: textWithAdaptiveSize"

I guess that my custom functions are not injected correctly in my environnement.
The functions I need are defined in a .js file.
I specify `--allowCodeExecution 1` and `--callback /path/to/my/functions.js` in my CLI, but it might be where my error and missunderstanding is.

Thanks for your help.
jakub.s
Posts: 1229
Joined: Fri Dec 16, 2022 11:45 am

Re: CLI highcharts-export-server : custom function for label formatter

Hi,

Thanks for the question!

Could you please send me the content you have in your file that you use with --infile? And could you please send the code of the callback?

Only then will I be able to help.

Best regards!
Jakub
Highcharts Developer
Pascal76
Posts: 1
Joined: Mon May 29, 2023 9:22 pm

Re: CLI highcharts-export-server : custom function for label formatter

Hi
I have a similar issue when I want use luxon.js for the formatter
https://github.com/highcharts/node-expo ... issues/398
Ex:
if (datethe.match(/^\d+\-w\d+$/))
{
var datetheYW = datethe.split('-w');
datethe = DateTime.now().set({year: datetheYW[0]}).set({weekNumber: datetheYW[1]}).startOf('week').toFormat('yyyy-LL-dd') + '&nbsp;&nbsp;<span style="font-weight:bold;">w' + datetheYW[1] + '</span>';
}
jakub.s
Posts: 1229
Joined: Fri Dec 16, 2022 11:45 am

Re: CLI highcharts-export-server : custom function for label formatter

Hi @Pascal76,

Unfortunately, I'm not really able to help you with using luxon.js in any simple way with the current version of Export Server.

I will contact our Export Server developers with the attached link so that they can take a look at the issue you created & decide what to do with it.

Kind regards!
Jakub
Highcharts Developer
Maxime74
Posts: 73
Joined: Wed Sep 17, 2014 8:34 am

Re: CLI highcharts-export-server : custom function for label formatter

Hi jakub.s,

Here is in attachement a sample of my code.
You will find a .json of the chart's options, the .php retrieving the options, getting the chart data and performing the CLI, and a .js file where are the functions I want to use as a formatter for the dataLabels.

Feel free to ask for further information.

Regards.
Attachments
sample.zip
(2.05 KiB) Downloaded 9 times
jakub.s
Posts: 1229
Joined: Fri Dec 16, 2022 11:45 am

Re: CLI highcharts-export-server : custom function for label formatter

Hi @Maxime74,

Thanks for sending! That's really helpful.

The file you pass to the --callback can be a JavaScript file (.js) with one function - the chart callback function (which takes "chart" as the only argument). Here's an example with vanilla JS: https://jsfiddle.net/BlackLabel/gvkjp0mL/

This is the way to add custom logic (or formatters) to your chart.

To see an example take a look at the /tests/callback/callback.js function included in the repository. That should help you with creating your own callback function.

Please try this out and let me know if it works.

If not, do not worry, we'll get to the bottom of the problem and figure something out.

Best regards!
Jakub
Highcharts Developer
Maxime74
Posts: 73
Joined: Wed Sep 17, 2014 8:34 am

Re: CLI highcharts-export-server : custom function for label formatter

Hello jakub.s,

Sorry for the long time since my last answer, but I had to postpone this development due to others priorities.
Anyway, your tips were very helpful and I can now achieve what I intend to do.

Thanks a lot.

Return to “Highcharts Usage”