Code Monkey home page Code Monkey logo

Comments (7)

bronius avatar bronius commented on May 28, 2024 1

Hi @cvasseng -
I've got a basic "pass javascript source to http server" going. Next I'll continue to try to adapt our legacy code to it.

For anyone else's help, here's what I learned:
Original:
curl -H "Content-Type: application/json" -X POST -d '{"infile":{"title": {"text": "Steep Chart"}, "xAxis": {"categories": ["Jan", "Feb", "Mar"]}, "series": [{"data": [29.9, 71.5, 106.4]}]}}' 127.0.0.1:7801 -o mychart.png becomes:
curl -H "Content-Type: application/json" -X POST -d '{"infile":"{title: {text: \"Steep Chart\"}, xAxis: {categories: [\"Jan\", \"Feb\", \"Mar\"]}, series: [{data: [29.9, 71.5, 106.4]}]}"}' 127.0.0.1:7801 -o mychart.png

  • POST payload must be structured as {"infile": "<javascript>"} where the POJO source javascript has to be encapsulated in "s for string.
  • Quotes (") within must be escaped as \".
  • Newlines must be escaped as \n.
  • TABs as whitespace throw off the compiler and must also be sanitized out.

If you're getting a message like:
SyntaxError: Unexpected token i in JSON at position 4
then the JSON compiler doesn't recognize your payload as javascript. Check that the body of your payload is enclosed in "s.

Thanks
-Bronius

from node-export-server.

cvasseng avatar cvasseng commented on May 28, 2024 1

Glad you got it working.

For reference, the POST valid arguments/format is documented here.

I'm closing this since it seems like it's working for you now. Please feel free to re-open this if you have any further issues with it.

Thanks.

from node-export-server.

cvasseng avatar cvasseng commented on May 28, 2024

Hi,

Are you passing the options directly to the export-function as an instanced object, or loading it from file first?

Functions are not completely supported when running as a node module. That said, I've made some tweaks, and added a sample that contains functions, and use the server as a node module here. This seems to be working fine.

Please let me know if you're still having issues after updating your copy of the server to the latest one.

from node-export-server.

jb3-2 avatar jb3-2 commented on May 28, 2024

Hi, great, no exceptions anymore if there are functions in the chart options. My function, however, does not seem to work correctly in your server. If I use this options file in highcharts export server, not all dataLabels are drawn (correct, see https://github.com/statabs-test/indikatoren/blob/issue-127/images/portal/4132.svg). Now, if I use your server in node.js mode, all dataLabels are drawn (not correct, see this code and this image).

I also tried using the server in CLI mode using this file and the following command line:
highcharts-export-server --batch "4132.json=4132.svg;"
and could not get it to work, I only ever got the the following error:
uncaughtException: [ReferenceError: func is not defined]

Can you help me get it to work and get the same results as on export.highcharts.com? Thank you very much!

from node-export-server.

cvasseng avatar cvasseng commented on May 28, 2024

Getting functions to work properly when using as a node module and passing instanced objects to the export function is non-trivial, which, along with keeping the options as valid JSON, is why it's not supported (or rather, the support ends at "it won't produce syntax errors, but functions are effectively ignored").

As of aad1a5a you can add custom formatters by using the customCode option in module mode, which is a function executed before initializing the chart where the options can be mutated:

var exportSettings = {
        options: {
            //Options goes here
        },
        customCode: function (options) {
            // Functions can be added here too
            options.title = { 
                text: 'Changed through custom code!'
            };
        }
    };
   
   // init code here
    
   exporter.export(exportSettings, ..);

A full sample can be found here.

I've also added JavaScript support to the infile option, so functions should work as expected when using that now (infile attribute when using as a node module, --infile when using through CLI. Expects the argument to be a filename).

If you need to pass in formatter functions etc. in the options themselves the best way is to run the server in HTTP mode, which is what export.highcharts.com is doing. In HTTP mode, the input is treated the same as with --instr (and now --infile), which injects the options into phantom as JavaScript and not JSON (this is largely done for legacy reasons - ideally the export server would only support JSON as there's a lot of additional magic that needs to be done for the inject to work, and the dataOptions/globalOptions/customCode/resources arguments can handle the use cases where e.g. functions are necessary).

from node-export-server.

bronius avatar bronius commented on May 28, 2024

I appreciate the discussion and work here and am trying to follow along.. Awesome work!
I am rendering charts via curl/http on my locally hosted highcharts-export-server but am generating chart options as json from a PHP array (json_encode). I couldn't figure out how to eval the javascript functions (as OP above), but I found that customCode is working for me ok.
In the discussion above, it was advised to use customCode for node module but that original pojo options should work via curl/http. Is there a recommended way to load eval'able functions into the json options payload?

from node-export-server.

cvasseng avatar cvasseng commented on May 28, 2024

@bronius: If you pass in JavaScript instead of JSON, things should "just work" over HTTP.

e.g. this will be interpreted as straight up JavaScript:

{
    chart: {}
}

while this will be passed into JSON.parse:

{
    "chart": {}
}

You can see this by going to the official server, and entering pure JavaScript into the options text area.

from node-export-server.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.