Highchart Java导出图像

时间:2014-11-12 18:16:17

标签: java javascript charts highcharts

我正在尝试使用高图导出服务器生成图表图像。此代码返回text / html而不是image。不知道这里出了什么问题。

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);
        }



    }
}

3 个答案:

答案 0 :(得分:1)

您没有发送正确的parameters。您需要构造一个表单数据为:

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

我在PostMan尝试了这个并且效果很好。

答案 1 :(得分:0)

更改String str =" {' xAxis':{' categories':[' Jan',' Feb' ,' Mar']},'系列......"代码返回text / html

答案 2 :(得分:0)

如果你想要导出图表,你可以使用这个例子fiddle,存在一个使用的部分

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

我希望有用