jcharts:java图表类库使用介绍-亚博电竞手机版
java学习
2020年03月25日 23:44
1
本文由码农网 – 小峰原创,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划!
jcharts是一款基于java的图表绘制类库,jcharts包含了多种图表格式,包括线型图、饼图、柱形图和点图等。
jcharts的特点
- 100%基于java,继承其跨平台的特性。
- 图表中的文字,包括x轴和y轴上的文字,均可以设置字体。
- 图表数据构造非常简单。
jcharts使用案例
柱形图绘制
完整的java代码:
package com.jiuxing.mobile; import java.awt.color; import java.awt.font; import java.awt.paint; import java.io.ioexception; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.krysalis.jcharts.axischart.axischart; import org.krysalis.jcharts.axischart.customrenderers.axisvalue.renderers.valuelabelposition; import org.krysalis.jcharts.axischart.customrenderers.axisvalue.renderers.valuelabelrenderer; import org.krysalis.jcharts.chartdata.axischartdataset; import org.krysalis.jcharts.chartdata.dataseries; import org.krysalis.jcharts.chartdata.interfaces.iaxisdataseries; import org.krysalis.jcharts.encoders.servletencoderhelper; import org.krysalis.jcharts.properties.axisproperties; import org.krysalis.jcharts.properties.barchartproperties; import org.krysalis.jcharts.properties.chartproperties; import org.krysalis.jcharts.properties.dataaxisproperties; import org.krysalis.jcharts.properties.legendproperties; import org.krysalis.jcharts.properties.propertyexception; import org.krysalis.jcharts.properties.util.chartfont; import org.krysalis.jcharts.types.charttype; public class barchartservlet extends httpservlet { // ---all of my charts serviced by this servlet will have the same properties. private barchartproperties barchartproperties; // ---all of my charts serviced by this servlet will have the same properties. protected legendproperties legendproperties; protected axisproperties axisproperties; protected chartproperties chartproperties; protected int width = 950; protected int height = 360; /********************************************************************************************** * **********************************************************************************************/ public void init() { this.legendproperties = new legendproperties(); this.chartproperties = new chartproperties(); // 图形的xy轴的属性对象 this.axisproperties = new axisproperties(false); // 图表横坐标和纵坐标范围的字体,大小,颜色设置对象 chartfont axisscalefont = new chartfont(new font( "georgia negreta cursiva", font.plain, 13), new color(18, 189, 255)); axisproperties.getxaxisproperties().setscalechartfont(axisscalefont); axisproperties.getyaxisproperties().setscalechartfont(axisscalefont); // bar图形的横坐标和纵坐标的标题字体和颜色 ,大小的设置对象 chartfont axistitlefont = new chartfont(new font("arial narrow", font.plain, 14), new color(18, 189, 255)); axisproperties.getxaxisproperties().settitlechartfont(axistitlefont); axisproperties.getxaxisproperties().setshowendborder(false); axisproperties.getyaxisproperties().settitlechartfont(axistitlefont); axisproperties.getyaxisproperties().setshowendborder(false); dataaxisproperties dataaxisproperties = (dataaxisproperties) axisproperties .getyaxisproperties(); try { // 设置用户定义的纵轴纵坐标的 间隔范围 dataaxisproperties.setuserdefinedscale(0, 3000); } catch (propertyexception propertyexception) { propertyexception.printstacktrace(); } dataaxisproperties.setroundtonearest(3); // 设置图形标题的字体,颜色,大小 chartfont titlefont = new chartfont(new font("georgia negreta cursiva", font.plain, 14), new color(18, 189, 255)); // 生成图像的属性对象 this.chartproperties.settitlefont(titlefont); // bar图形的属性类 this.barchartproperties = new barchartproperties(); valuelabelrenderer valuelabelrenderer = new valuelabelrenderer(false, false, true, -1); valuelabelrenderer.setvaluelabelposition(valuelabelposition.on_top); // 是否设置显示的纵坐标标签垂直,true为是,flase为水平 valuelabelrenderer.useverticallabels(true); barchartproperties.addpostrendereventlistener(valuelabelrenderer); } /********************************************************************************************** * **********************************************************************************************/ public void service(httpservletrequest req, httpservletresponse httpservletresponse) throws servletexception, ioexception { try { // 设置横坐标标签 string[] xaxislabels = { "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014" }; // 设置横坐标的单位 string xaxistitle = "years"; // 设置纵坐标的标题 string yaxistitle = "problems"; // 设置图形的标题 string title = "micro$oft at work"; // 图形所需要的数据对象 iaxisdataseries dataseries = new dataseries(xaxislabels, xaxistitle, yaxistitle, title); // 设置条形数据 double[][] data = new double[][] { { 1500, 6880, 4510, 2600, 0, 1580, 0, 9555, 11000, 0, 7500, 5880, 3510, 2600, 1200, 1580, 9000, 8555, 9000, 3120 } }; // 条形区域 形示的标签 string[] legendlabels = { "8" }; // string[] legendlabels = null; // 条形区域绘制的颜色设置对象 paint[] paints = new paint[] { new color(18, 189, 255) }; dataseries.addiaxisplotdataset(new axischartdataset(data, legendlabels, paints, charttype.bar, this.barchartproperties)); // 产生一个 chart对象 axischart axischart = new axischart(dataseries, this.chartproperties, this.axisproperties, this.legendproperties, this.width, this.height); // 输出设置好的chart图形 servletencoderhelper.encodejpeg13(axischart, 1.0f, httpservletresponse); } catch (throwable throwable) { // hack do your error handling here... throwable.printstacktrace(); } } }
效果图如下:
总体来说,jcharts可以完成基本的图表绘制,并可以自己扩展代码来完成更高级的图表和报表功能。
软件亚博vip888首页 软件文档 软件下载
本文链接:http://www.codeceo.com/article/jcharts-java.html
本文作者:码农网 – 小峰
[ 原创作品,转载必须在正文中标注并保留原文链接和作者等信息。]
展开全文