3

I am trying to render and export FusionCharts completely on the server. I am aware of solutions such as FCimg and FusionCharts .NET Solution. I have also implemented a Java solution that uses the Process class to run wkhtmltoimage.

However, I am trying to find a pure Java solution of doing this. I have an html file that includes FusionCharts JS Libraries and code to generate the fusion chart. I found JxBrowser that properly renders the chart but it requires X-Server for it to work on Linux. I also have tried Cobra/Lobo Browser but it does not fully support JavaScript. Are there any other ways to render and export fusion charts on the server or atleast render an html file that includes JavaScript completely in Java (and that does not require xserver)?

Thanks in advance for all the help!

KrispyDonuts
  • 1,262
  • 2
  • 18
  • 37

1 Answers1

1

Update: Solution that does not require xserver: WebRenderer. The Swing Edition is the only edition that supports HTML5 as of July 9th, 2012. You can use the swing edition to capture the image without a GUI.


I found a way that uses Eclipse's SWT Browser. However this cannot be run in an headless mode. You will have to use xserver to implement this. See this question.

Since this requires xserver and cannot be run in an headless mode, I would suggest using JxBrowser. It is a lot simpler and all you need is to generate an html file with all the fusion charts scripts. See #1, #2, #3

  1. You have to create a template.html file that contains the header (<html><head>), jquery.min.js, FusionCharts.js, FusionCharts.HC.js, FusionCharts.HC.Charts.js. Make sure each of these scripts are in their own script tags (<script type="text/javascript"> [js code] </script>)

  2. Now add another JavaScript function with its own script tags containing the steps to render the chart. For example:

function load() { FusionCharts.setCurrentRenderer('javascript'); var chart = new FusionCharts("swf", 'chart0', "width", "height", "0", "1"); chart.setXMLData("XML DATA HERE"); chart.render("divNAMEHere"); }

  1. Now you need to call the load() function onload, create a div to render the chart in, and end the html file. For example:

    `

test `
  1. Create a new class that imports the eclipse swt browser libraries. Instantiate Display, Shell, and Browser (use this as a guideline to help understand what is happening: http://www.roseindia.net/tutorials/swt/swt-browser.shtml).

  2. Set the text of the browser (browser.setText("htmlcode")) to the html code from template.html. The best way to do this would be to read the file using BufferedReader.

  3. Lastly, the image takes some time to render. Now there is probably a better way to do this but if you want to just get it working, I set up a count and it captures the image after a certain number. This is what you need to add to the end:

    int i = 0;
    while (!shell.isDisposed())
    {
      if (!display.readAndDispatch())
      {
         display.sleep();
         i++;
       //  System.out.println(i);
         if(i==100)
         {
             GC source = new GC (shell);  
             Image image = new Image(display, browser.getClientArea()); 
             source.copyArea(image, 0, 0);
             ImageLoader io = new ImageLoader ();
             io.data = new ImageData[] { image.getImageData() };
             File f = new File (currentDir+"/workpng.png");
             io.save (f.getAbsolutePath(), SWT.IMAGE_PNG); 
         }
      }
    }
    
HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
KrispyDonuts
  • 1,262
  • 2
  • 18
  • 37