Rakesh Goyal
Rakesh Goyal

Reputation: 3231

Highchart Java export image

I am trying to generate chart image using highchart export server. This code returns me text/html instead of image. Not sure what is going wrong here.

package com.vidyartha;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;

public class CreateChart {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        HttpPost post = new HttpPost("http://export.highcharts.com");


        HttpClient client = HttpClientBuilder.create().build();

        try{

            String str="{'xAxis': {'categories': ['Jan', 'Feb', 'Mar']},'series': [{'data': [29.9, 71.5, 106.4]}]};";
            String dataString = "{type:image/png,options:" + str+"}";
             post.setEntity(new StringEntity(dataString, 
                     ContentType.create("application/json")));

        HttpResponse response = client.execute(post);

        InputStream is = response.getEntity().getContent();


        String filePath = "E:\\pdf\\rakesh.png";
        FileOutputStream fos = new FileOutputStream(new File(filePath));

        int inByte;
        while((inByte = is.read()) != -1) fos.write(inByte);
        is.close();
        fos.close();

        System.out.println("Got Repsonse");
        System.out.println(response);


        }catch(Exception e){
            System.out.println(e);
        }



    }
}

Upvotes: 1

Views: 1954

Answers (3)

xola139
xola139

Reputation: 1

If you want export chart , you can to make use this example fiddle , exist a part where to did use

var imageData = $(this).highcharts().createCanvas();

I hope will be useful

Upvotes: 0

venus
venus

Reputation: 1

After changing String str="{'xAxis': {'categories': ['Jan', 'Feb', 'Mar']},'series......" code returns me text/html

Upvotes: 0

Mark
Mark

Reputation: 108507

You aren't sending the correct parameters. You need to construct a POST request with form data of:

options = {'xAxis': {'categories': ['Jan', 'Feb', 'Mar']},'series': [{'data': [29.9, 71.5, 106.4]}]}
type = image/png
constr = Chart

I tried this in PostMan and it worked well.

Upvotes: 1

Related Questions