I can not make the phantomJs Highcharts export server work

Thank you very much in advance.

I want to configure phantomjs Highcharts export server. It should accept json options as input and output files of jpeg images.

That's what I'm doing:

Then I tried to use the client code on this site: http://export.highcharts.com/demo to send the request. I changed the form url from this:

  <form id="exportForm" action="./" method="POST"> 

:

  <form id="exportForm" action="http://0.0.0.0:3001" method="POST"> 

and clicked "Highcharts Configuration Object (JSON)". All I get is this message:

Rendering failed: SyntaxError: unable to parse JSON string

Since the same request can be correctly processed on the Highcharts server, the error should be in the js code of the Highcharts server that I use. I also tried the following command:

 phantomjs highcharts-convert.js -infile options.js \ -outfile chart.png -scale 2.5 -width 300 

Using this code in options.js :

 { infile: { xAxis: { categories:['Jan','Feb','Mar','Apr', 'May','Jun','Jul','Aug', 'Sep','Oct','Nov','Dec'] }, series:[ { data:[29.9,71.5,106.4,129.2, 144.0,176.0,135.6,148.5, 216.4,194.1,95.6,54.4] }] }, callback: function(chart){ chart.renderer .arc(200,150,100,50,-Math.PI,0) .attr({fill:'#FCFFC5',stroke:'black','stroke-width':1}) .add(); }, constr: "Chart", outfile: "//tmp//chart.png" } 

And it successfully generates png.

I think Highchart did not work much in exporting functions, and I found some typo in the highcharts-convert.js file. Can someone help me with this? Many thanks.

+7
export phantomjs highcharts
source share
4 answers

I finally solved the problem. I think there is a misunderstanding in the so-called "JSON" string. The Javascript export server does not accept the actual "JSON" string . In the real line "JSON" all the lines will be indicated, something like

  { "value": [1,2,3], "name": "jack" } 

What the export server accepts is actually part of the Javascript code for creating a Javascript object , for example:

  { value: [1,2,3], name: "jack" } 

This is because the server will use this line as part of the Javascript code on the generated web page. I wrote a small function to convert a JSON string to this format and pass it to the server, and it finally works.

 var getUnQuotedJsonString = function (str) { return str.replace(/"\w+":/g, function(s, key) { return s.replace(/"/g, ""); }); } 
+6
source share

I got the same error when I tried to send a JSON string that was longer than the one on which my server would host the WAR file of the highcharts exporters file. Check your message length setting on your server. Make sure it is long enough to send the request. Now, since you are not specifying which export server you are using (java or PHP), I would suggest that you did not actually create a web interface for the export server and you just configured the export without a headset (phantomJS + some highcharts js files) . To use the export server for use in the interface (for example, when the user clicks the export buttons on a web page), you also need to configure Java or PHP .

0
source share

This is because the Phantoms HTTP server launched

 phantomjs highcharts-convert.js -host ... -port ... 

expects parameters to be sent in JSON format. Please read the documentation , paragraph "start as a web server"

Out of curiosity ... what typo did you find?

-one
source share

I wrote a similar function to solve this problem for PHP, if someone wants to remove quotes from the json_encode result -

 function unQuote($str){ return preg_replace_callback('/"\w+":/', function ($match){ return str_replace('"', '', $match[0]); }, $str ); } 
-one
source share

All Articles