java使用echart报表_简表-Java-Echart报表介绍

Java后台报表尝试了很多,最终发现了一款,而且是开源的,简表地址:http://www.jatools.com/jor/。问题的引入:该报表支持嵌套,钻去,应对excel类似的报表,足够了。但是,报表的图表特别难看,所以想改一下,结合现在流行的图表Echart, 访问地址:http://echarts.baidu.com/, 开源了这个解决方案,和大家一起完善这个报表。

Jor 报表的设计:使用awt 展示报表,最终使用的是graphics 对象,然后使用jreport 等开源组件,实现导出pdf,导出word ,导出excel 等。

Echrt 的引入,Echart 是javascript 组件,然后执行javascript,使用echart 的getDataURL可以得到Echart 展示的图片的base64 编码,这样,结合读取图片,展示grahics ,就可以展示出来,然后顺便导出等等。Js这部分,可以根据绑定的数据源,动态写js 方法,修改html 文件,然后展示即可。

方案1:

使用HtmlUnit组件,即http://htmlunit.sourceforge.net/。 展示html ,加载js,然后导出base64编码。大致源码如下:

final WebClient webClient = newWebClient(BrowserVersion.CHROME);final HtmlPage page = webClient.getPage("http://localhost:8000/line2.html");

System.out.println(" // 1 启动JS ");

webClient.getOptions().setJavaScriptEnabled(true);

System.out.println("// 2 禁用Css,可避免自动二次请求CSS进行渲染 ");

webClient.getOptions().setCssEnabled(false);

System.out.println("// 3 启动客户端重定向 ");

webClient.getOptions().setRedirectEnabled(true);

System.out.println("// 4 js运行错误时,是否抛出异常");

webClient.getOptions().setThrowExceptionOnScriptError(false);

System.out.println("// 5 设置超时 ");

webClient.getOptions().setTimeout(50000);

System.out.println(" 允许绕过SSL认证 ");

webClient.getOptions().setUseInsecureSSL(true);

System.out.println(" 允许启动注册组件 ");

webClient.getOptions().setActiveXNative(true);

System.out.println(" //等待JS驱动dom完成获得还原后的网页 ");

webClient.waitForBackgroundJavaScript(5000);

webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);

webClient.getCookieManager().setCookiesEnabled(true);

webClient.setAjaxController(newNicelyResynchronizingAjaxController());

webClient.addWebWindowListener(newWebWindowListener() {

@Overridepublic voidwebWindowOpened(WebWindowEvent webWindowEvent) {

System.out.println("windows opened");

}

@Overridepublic voidwebWindowContentChanged(WebWindowEvent webWindowEvent) {

System.out.println("windows changed");

}

@Overridepublic voidwebWindowClosed(WebWindowEvent webWindowEvent) {

System.out.println("windows closed");

}

});try{

Thread.sleep(10000);

}catch(Exception exp) {

}final HtmlDivision div = page.getHtmlElementById("text2");//执行按钮出发的js事件

ScriptResult sr = page.executeJavaScript("javascript:getData();");try{

String fileStr= "";

String test=sr.getJavaScriptResult().toString();byte[] b = newBASE64Decoder().decodeBuffer(test);//生成图片

OutputStream out = new FileOutputStream(new File(fileStr + "\\test.png"));

out.write(b);

out.flush();

out.close();

}catch(Exception exp) {

exp.printStackTrace();

}

}catch(Exception exp) {

exp.printStackTrace();

}

结果: 不行,执行不了js,原因没有深入研究。

方案2:

代码如下:

//BrowserEngineManager bem = BrowserEngineManager.instance();

// bem.setActiveEngine(BrowserEngineManager.IE);

//IBrowserEngine be = bem.getActiveEngine();

//URL url = new URL("http://www.hao123.com");

URL url = new File("http://localhost.:8000/line2.html").toURI().toURL();

final WebBrowser browser = new WebBrowser();

//browser = be.getWebBrowser();//new WebBrowser();

browser.addWebBrowserListener(new WebBrowserListener() {

public void downloadStarted(WebBrowserEvent event) {

System.out.println("27");

}

public void downloadCompleted(WebBrowserEvent event) {

System.out.println("30");

}

public void downloadProgress(WebBrowserEvent event) {

System.out.println("33");

}

public void downloadError(WebBrowserEvent event) {

System.out.println("36");

}

public void documentCompleted(WebBrowserEvent event) {

System.out.println("39");

browser.executeScript("alert('文档下载完毕!')");

String res = browser.executeScript("getData");

System.out.println(res);

}

public void titleChange(WebBrowserEvent event) {

System.out.println("43");

}

public void statusTextChange(WebBrowserEvent event) {

System.out.println("46");

}

public void windowClose(WebBrowserEvent webBrowserEvent) {

System.out.println("49");

}

public void initializationCompleted(WebBrowserEvent arg0) {

System.out.println("52");

} });

browser.setURL(url);

JFrame f = new JFrame();

f.setTitle("浏览器");

f.setSize(800,600);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//f.getContentPane().add(browser1);

f.getContentPane().add(browser);

f.setVisible(true);

}

结果: 代码跑不起来,dll 都是32 位,没有办法使用,所以放弃。

方案3:

使用c# winform 的webbrowser 控件,加载html, 然后执行js脚本。

代码:

private voidinit()

{

InitializeComponent();

CleanTempFiles();

webBrowser1.AllowWebBrowserDrop= false;

webBrowser1.WebBrowserShortcutsEnabled= false;

webBrowser1.IsWebBrowserContextMenuEnabled= false;

webBrowser1.Navigate(htmlurl+ "?random=" + DateTime.Now.ToString("yyyyMMddHHmmss"), null, null, null);

}private void webBrowser1_DocumentCompleted(objectsender, WebBrowserDocumentCompletedEventArgs e)

{try{var doc = this.webBrowser1.Document;var ele = doc.GetElementById("text2");

Console.WriteLine(ele.InnerText);//读取base64 , 然后转换为图片,保存

string base64 = ele.InnerText.Split(',')[1];byte[] arr =Convert.FromBase64String(base64);

MemoryStream ms= newMemoryStream(arr);

Bitmap bmp= newBitmap(ms);//bmp.Save(txtFileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);//bmp.Save(txtFileName + ".bmp", ImageFormat.Bmp);//bmp.Save(txtFileName + ".gif", ImageFormat.Gif);//bmp.Save(txtFileName + ".png", ImageFormat.Png);//bmp.Save(imageGuid + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

bmp.Save(imageGuid + ".png", System.Drawing.Imaging.ImageFormat.Png);

ms.Close();

}catch(Exception exp)

{

Console.WriteLine(exp.ToString());

}finally{

Application.Exit();

}

}

结果: 可行, 导出图片成功, 所以采用。

接下来要做:

1,    实现所有图表,代码中只实现了linechart,其他,饼图,什么的都需要更改。

2,    增加脚步选项,直接在界面中设置js代码,高度定制。

开源地址:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值