Java自动化邮件中发送图表(三)之Highchart

一、Highchart

(1)Highchart.js

Highcharts 是一个用纯JavaScript编写的一个图表库。能够很简单便捷的在web网站或是web应用程序添加有交互性的图表。

(2)highcharts-serverside-export

Highcharts Serverside Export框架(HSE)的主要目标是为Highcharts提供包含图像生成功能的Java API。

与Highcharts模型对应的Java API (与Highcharts模型层次结构和属性保持兼容),基于Rhino-Apache Batik的渲染器:java ChartOptions ==> Rhino ==> Highcharts ==> SVG ==> image(png,JPEG等等),

 

二、实例

(1)柱状图

public ChartOptions createColumnBasic () {// http://highcharts.com/demo/column-basicChartOptions chartOptions = factory.createChartOptions ();chartOptions.getChart ().setDefaultSeriesType (SeriesType.column).setWidth (800).setHeight (400).setMarginLeft (70).setMarginTop (80);// titleschartOptions.getTitle ().setText ("Monthly Average Rainfall");chartOptions.getSubtitle ().setText ("Source: WorldClimate.com");// xAxischartOptions.getXAxis ().getCategories ().pushString ("Jan").pushString ("Feb").pushString ("Mar").pushString ("Apr").pushString ("May").pushString ("Jun").pushString ("Jul").pushString ("Aug").pushString ("Sep").pushString ("Oct").pushString ("Nov").pushString ("Dec");// yAxischartOptions.getYAxis ().setMin (0).getTitle ().setText ("Rainfall (mm)");// LegendchartOptions.getLegend ().setLayout ("vertical").setAlign ("left").setVerticalAlign ("top").setX (100).setY (70);// PlotOptionschartOptions.getPlotOptions ().getColumn ().setBorderWidth (0);// Several seriesaddSeries (chartOptions, "Tokyo", new double[] { 49.9, 71.5, 106.4, 129.2,144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4 });addSeries (chartOptions, "New York", new double[] { 83.6, 78.8, 98.5, 93.4,106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3 });addSeries (chartOptions, "London", new double[] { 48.9, 38.8, 39.3, 41.4,47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2 });addSeries (chartOptions, "Berlin", new double[] { 42.4, 33.2, 34.5, 39.7,52.6, 75.5, 57.4, 60.4, 47.6, 39.1, 46.8, 51.1 });return chartOptions;}

(2)饼图

public ChartOptions createPieChart () {// http://highcharts.com/demo/pie-basicChartOptions chartOptions = factory.createChartOptions ();chartOptions.getChart ().setWidth (800).setHeight (600).setMarginLeft (70).setMarginTop (80);// titlechartOptions.getTitle ().setText ("Browser market shares at a specific website, 2010");// plotOptionschartOptions.getPlotOptions ().getPie ().setAllowPointSelect (true).getDataLabels ().setEnabled (true).setColor ("#000000").setFormatter ("function() {return '<b>'+ this.point.name +'</b>: '+ this.y +' %';}");Series newSeries = factory.createSeries ().setName ("Browser share").setType ("pie");chartOptions.getSeries ().pushElement (newSeries);newSeries.getData ().pushElement (factory.createPoint ().setName ("Firefox").setY (45)).pushElement (factory.createPoint ().setName ("IE").setY (26.8)).pushElement (factory.createPoint ().setName ("Chrome").setY (12.8).setSliced (true).setSelected (true)).pushElement (factory.createPoint ().setName ("Safari").setY (8.5)).pushElement (factory.createPoint ().setName ("Opera").setY (6.2)).pushElement (factory.createPoint ().setName ("Others").setY (0.7));return chartOptions;}

(3)折线图

public ChartOptions createTimeDataWithIrregularIntervals () {// http://highcharts.com/demo/spline-irregular-timeChartOptions chartOptions = factory.createChartOptions ();chartOptions.getChart ().setWidth (800).setHeight (600).setDefaultSeriesType (SeriesType.spline).setMarginLeft (70).setMarginTop (80);// titleschartOptions.getTitle ().setText ("Snow depth in the Vikjafjellet mountain, Norway");chartOptions.getSubtitle ().setText ("An example of irregular time data in Highcharts JS");// axischartOptions.getXAxis ().setType ("datetime").getDateTimeLabelFormats ().set (TimeUnit.month, "%e. %b").set (TimeUnit.year, "%b");chartOptions.getYAxis ().setMin (0).getTitle ().setText ("Snow depth (m)");// plotOptionschartOptions.getPlotOptions ().getPie ().setAllowPointSelect (true).getDataLabels ().setEnabled (true).setColor ("#000000").setFormatter ("function() {return '<b>'+ this.point.name +'</b>: '+ this.y +' %';}");Series newSeries = factory.createSeries ().setName ("Winter 2007-2008");chartOptions.getSeries ().pushElement (newSeries);newSeries.getData ().pushElement (factory.createPoint ().setX (getDateUTC (1970, 9, 27)).setY (0)).pushElement (factory.createPoint ().setX (getDateUTC (1970, 10, 10)).setY (0.6)).pushElement (factory.createPoint ().setX (getDateUTC (1970, 10, 18)).setY (0.7)).pushElement (factory.createPoint ().setX (getDateUTC (1970, 11, 2)).setY (0.8)).pushElement (factory.createPoint ().setX (getDateUTC (1970, 11, 9)).setY (0.6)).pushElement (factory.createPoint ().setX (getDateUTC (1970, 11, 16)).setY (0.6)).pushElement (factory.createPoint ().setX (getDateUTC (1970, 11, 28)).setY (0.67)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 0, 1)).setY (0.81)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 0, 8)).setY (0.78)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 0, 12)).setY (0.98)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 0, 27)).setY (1.84)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 1, 10)).setY (1.8)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 1, 18)).setY (1.8)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 1, 24)).setY (1.92)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 2, 4)).setY (2.49)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 2, 11)).setY (2.79)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 2, 15)).setY (2.73)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 2, 25)).setY (2.61)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 3, 2)).setY (2.76)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 3, 6)).setY (2.82)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 3, 13)).setY (2.8)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 4, 3)).setY (2.1)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 4, 26)).setY (1.1)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 5, 9)).setY (0.25)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 5, 12)).setY (0));newSeries = factory.createSeries ().setName ("Winter 2008-2009");chartOptions.getSeries ().pushElement (newSeries);newSeries.getData ().pushElement (factory.createPoint ().setX (getDateUTC (1970, 9, 18)).setY (0)).pushElement (factory.createPoint ().setX (getDateUTC (1970, 9, 26)).setY (0.2)).pushElement (factory.createPoint ().setX (getDateUTC (1970, 11, 1)).setY (0.47)).pushElement (factory.createPoint ().setX (getDateUTC (1970, 11, 11)).setY (0.55)).pushElement (factory.createPoint ().setX (getDateUTC (1970, 11, 25)).setY (1.38)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 0, 8)).setY (1.38)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 0, 15)).setY (1.38)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 1, 1)).setY (1.38)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 1, 8)).setY (1.48)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 1, 21)).setY (1.5)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 2, 12)).setY (1.89)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 2, 25)).setY (2.0)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 3, 4)).setY (1.94)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 3, 9)).setY (1.91)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 3, 13)).setY (1.75)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 3, 19)).setY (1.6)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 4, 25)).setY (0.6)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 4, 31)).setY (0.35)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 5, 7)).setY (0));newSeries = factory.createSeries ().setName ("Winter 2009-2010");chartOptions.getSeries ().pushElement (newSeries);newSeries.getData ().pushElement (factory.createPoint ().setX (getDateUTC (1970, 9, 9)).setY (0)).pushElement (factory.createPoint ().setX (getDateUTC (1970, 9, 14)).setY (0.15)).pushElement (factory.createPoint ().setX (getDateUTC (1970, 10, 28)).setY (0.35)).pushElement (factory.createPoint ().setX (getDateUTC (1970, 11, 12)).setY (0.46)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 0, 1)).setY (0.59)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 0, 24)).setY (0.58)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 1, 1)).setY (0.62)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 1, 7)).setY (0.65)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 1, 23)).setY (0.77)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 2, 8)).setY (0.77)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 2, 14)).setY (0.79)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 2, 24)).setY (0.86)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 3, 4)).setY (0.8)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 3, 18)).setY (0.94)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 3, 24)).setY (0.9)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 4, 16)).setY (0.39)).pushElement (factory.createPoint ().setX (getDateUTC (1971, 4, 21)).setY (0));return chartOptions;}

 

三、图片导出

(1)测试例子

package examples;import java.io.File;import org.one2team.highcharts.server.export.ExportType;
import org.one2team.highcharts.server.export.HighchartsExporter;
import org.one2team.highcharts.shared.ChartOptions;
import org.one2team.highcharts.shared.Jsonify;public class SimpleExport {public static void main (String[] args) {//图片导出目录File exportDirectory = new File ("./b");if (!exportDirectory.exists()) {exportDirectory.mkdirs();}final SamplesFactory highchartsSamples = SamplesFactory.getSingleton ();// 创建柱状图ChartOptions chartOptions1 = highchartsSamples.createColumnBasic ();//导出柱状图pngHighchartsExporter<ChartOptions> pngExporter = ExportType.png.createExporter ();pngExporter.export (chartOptions1, null, new File (exportDirectory, "column-basic.png"));//创建饼图ChartOptions chartOptions2 = highchartsSamples.createPieChart ();pngExporter.export (chartOptions2, null, new File (exportDirectory, "pie-chart.png"));//导出饼图pngChartOptions chartOptions3 = highchartsSamples.createTimeDataWithIrregularIntervals ();final HighchartsExporter<ChartOptions> jpegExporter = ExportType.jpeg.createExporter ();jpegExporter.export (chartOptions3, null, new File (exportDirectory, "time-data-with-irregular-intervals.jpeg"));//柱状图json字符串String chartOption = highchartsSamples.createJsonColumnBasic ();HighchartsExporter<String> pngFromJsonExporter = ExportType.png.createJsonExporter ();pngFromJsonExporter.export (chartOption, null, new File (exportDirectory, "column-basic-from-json.png"));//柱状图Jsonify对象,JSMHighchartsFactory的ChartOption实现Jsonify接口 Jsonify jsonify = (Jsonify) highchartsSamples.createColumnBasic ();String json = jsonify.toJson ();System.out.println("json "+json);pngFromJsonExporter.export (json, null, new File (exportDirectory, "column-basic-from-jsonified-java.png"));}}

 

(2)API缺点没有直接转成字节数组的方法

package org.one2team.highcharts.server.export;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;import org.apache.commons.io.IOUtils;
import org.one2team.highcharts.server.export.util.SVGRendererInternal;public class HighchartsExporter<T> {public HighchartsExporter(ExportType type, SVGRendererInternal<T> internalRenderer) {this.type = type;this.renderer = new SVGStreamRenderer<T> (new SVGRenderer<T> (internalRenderer),type.getTranscoder ());}//新增的导出字节数组方法public byte[] export (T chartOptions,T globalOptions) {ByteArrayOutputStream fos = null;try {fos=new ByteArrayOutputStream();renderer.setChartOptions (chartOptions).setGlobalOptions (globalOptions).setOutputStream (fos).render ();return fos.toByteArray();} catch (Exception e) {e.printStackTrace ();throw (new RuntimeException (e));} finally {if (fos != null)IOUtils.closeQuietly (fos);}}private final SVGStreamRenderer<T> renderer;private final ExportType type;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/324249.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【2018.5.19】模拟赛之一-ssl2432 面积最大【数学】

正题 大意 解题思路 沟谷定理可以用半径求出高度&#xff0c;然后暴力枚举就好了 公式&#xff1a; ahr2−(a/2)2−−−−−−−−−√∗2ahr2−(a/2)2∗2bhr2−(b/2)2−−−−−−−−−√∗2bhr2−(b/2)2∗2然后计算两个的面积去掉重复的 Sa∗ahb∗bh−a∗bSa∗ahb∗bh−a…

thymeleaf如何迭代遍历,如何判断

大家好&#xff0c;我是雄雄&#xff0c;欢迎关注公众号【雄雄的小课堂】。最近&#xff0c;换了个桌面手机的也同步换了下&#xff1a;哈哈哈&#xff0c;自己看着舒服就行~这几天在业余时间搞一个电商项目&#xff0c;可以说是边学边做&#xff0c;效率比较低&#xff0c;但是…

ASP.NET Core MVC Tag Helpers 介绍

简介 Tag Helpers 提供了在视图中更改和增强现有HTML元素的功能。将它们添加到视图中&#xff0c;会经过Razor模板引擎处理并创建一个HTML&#xff0c;之后再返回给浏览器。有一些Tag Helpers&#xff0c;其实作为元素或实际的标签&#xff08;如environment&#xff0c;cache…

layui结合springboot上传图片

大家好&#xff0c;我是雄雄&#xff0c;欢迎关注公众号**【雄雄的小课堂】**。 最近实在是忙的抽不开身&#xff0c;并且身上也出现了一堆的毛病&#xff0c;今天牙疼&#xff0c;明天脊椎疼&#xff0c;然后接着又头疼……年纪轻轻的&#xff0c;咋这么多毛病呢&#xff1f;…

C#使用Xamarin开发可移植移动应用(1.入门与Xamarin.Forms页面),附源码

前言 什么是Xamarin? Xamarin始创于2011年&#xff0c;旨在使移动开发变得难以置信地迅捷和简单。 Xamarin的产品简化了针对多种平台的应用开发&#xff0c;包括iOS、Android、Windows Phone和Mac App。 Xamarin由许多著名的开源社区开发者创立和参与&#xff0c;而且也是…

Java不需要使用Javafx的Application也能将Node导出图片

一、Java通用导出Javafx Chart图片 Javafx不需要使用Application也能导出图片&#xff0c;使用PlatformImpl.startup方法初始化Javafx的Toolkit。 Javafx API并不是所有机器都支持的&#xff0c;Node导出图片不一定成功。&#xff08;如&#xff1a;无法初始化Javafx的Toolki…

layui上传图片,前端直接拷代码,后端……

大家好&#xff0c;我是雄雄&#xff0c;欢迎关注公众号【雄雄的小课堂】。最近实在是忙的抽不开身&#xff0c;并且身上也出现了一堆的毛病&#xff0c;今天牙疼&#xff0c;明天脊椎疼&#xff0c;然后接着又头疼……年纪轻轻的&#xff0c;咋这么多毛病呢&#xff1f;我也在…

ASP.NET Core 源码学习之 Options[3]:IOptionsSnapshot

2017-06-28 更新&#xff1a;OptionsSnapshot 已改为 OptionsManager 变更详情IOptionsCache 已改为 IOptionsMonitorCache 变更详情 在 上一章 中&#xff0c;介绍了 IOptions 的使用&#xff0c; 而我们知道&#xff0c;在 ConfigurationBuilder 的 AddJsonFile 中&#xff0…

layui结合ajax实现下拉联动效果

大家好好&#xff0c;我是雄雄&#xff0c;欢迎关注公众号&#xff1a;雄雄的小课堂。 昨天分享的是使用layui集合springboot实现图片上传&#xff0c;文章地址在这里&#xff0c;需要的小伙伴们可以去看看&#xff1a; 今天给大家分享的内容也是那天在造项目的时候完成的&am…

ASP.NET Core MVC – Caching Tag Helpers

简介 缓存可以大大提高应用程序加载时间和响应速度。我们可以使用缓存Tag Helpers缓存不会频繁更改的HTML内容。 在上一篇文章ASP.NET Core MVC Tag Helpers 介绍中&#xff0c;我们谈到了Tag Helpers&#xff0c;演示Tag Helpers能做什么、如何使用它们以及一些最常用的Tag …

layui结合ajax实现下拉菜单联动效果

大家好好&#xff0c;我是雄雄&#xff0c;欢迎关注公众号&#xff1a;雄雄的小课堂。昨天分享的是使用layui集合springboot实现图片上传&#xff0c;文章地址在这里&#xff0c;需要的小伙伴们可以去看看&#xff1a;今天给大家分享的内容也是那天在造项目的时候完成的&#x…

ASP.NET Core 源码学习之 Options[4]:IOptionsMonitor

前面我们讲到 IOptions 和 IOptionsSnapshot&#xff0c;他们两个最大的区别便是前者注册的是单例模式&#xff0c;后者注册的是 Scope 模式。而 IOptionsMonitor 则要求配置源必须是可监听的&#xff0c;用来实现 Options 实例的自动更新&#xff0c;并对外提供了 OnChage 事件…

Layui hint: Table element property lay-data configuration item has a syntax error解决方式

今天在使用layui的数据表格时&#xff0c;报错Layui hint: Table element property lay-data configuration item has a syntax error&#xff0c;找了好几中解决方式&#xff0c;网上说的用{}括起来。 对于我来说都不适应哎&#xff0c;我发现我的问题比较低级。 源代码&…

ASP.NET Core MVC – Form Tag Helpers

简介 我们已经介绍过Tag Helpers以及一些最常用的Tag Helpers&#xff0c;也谈到了缓存Tag Helpers。在这篇文章中&#xff0c;我们将讨论表单Tag Helpers。 HTML或Web表单通过使用各种HTML元素用于收集来自用户的输入&#xff0c;如输入框、复选框、单选按钮、下拉列表等。使…

表单提交报错405的解决方式

大家好&#xff0c;我是雄雄&#xff0c;欢迎关注公众号&#xff1a;雄雄的小课堂。今天&#xff0c;分享一个bug的解决的方法&#xff0c;到最后解决了也不知道为啥这样是可以的&#xff0c;原来的方法是不可以的……01问题描述项目中有这样的一个需求&#xff0c;就是在原来的…

JavaFX图表(二)之饼图

翻译自 JavaFX - 饼图 饼图是将值表示为具有不同颜色的圆的切片。标记这些切片&#xff0c;并且在图表中表示与每个切片对应的值。 以下是一张饼图&#xff0c;描绘了一个实例中各公司的移动销售情况。 在JavaFX中&#xff0c;饼图由名为PieChart的类表示。该类属于包javaf…

RabbitMQ(mall项目学习 死信、延迟队列)

文章目录RabbitMQ的死信队列RabbitMQ的延迟队列mall项目 业务场景说明Rabbitmq 插件实现延迟队列启动&#xff08; UI 界面 &#xff09;RabbitMQ的死信队列 TTL&#xff1a;time to live 消息存活时间 如果消息在存活时间内未被消费&#xff0c;则会被清除 RabbitMQ⽀持两种…

asp.net中条件查询+分页

大家好&#xff0c;我是雄雄&#xff0c;欢迎关注公众号&#xff1a;雄雄的小课堂。 今天&#xff0c;分享的是asp.net中条件查询分页的小案例。 如下图所示&#xff1a; 各个功能模块均已标注&#xff0c;先大致介绍一下业务。 \1. 点击左侧图书类别&#xff0c;根绝类别编…