java类中生成jfreechart,返回图表的url地址 代码分享
发布时间:2023-09-08 00:54:07
web.xml中设置:
<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet >
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/DisplayChart</url-pattern>
</servlet-mapping>
java类中方法:
public String getChart(String series[],double score[][],String type[],String name){
final int num=8;
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
for(int i=0;i<type.length;i++){
type[i]=type[i].substring(0, (type[i].length()<num)?type[i].length():num);
}
for(int j=0;j<series.length;j++){
int i=0;
for( i=0;i<type.length;i++){
defaultcategorydataset.addValue(score[j][i], series[j], type[i]);
}
}
JFreeChart jfreechart = ChartFactory.createLineChart(name,null,null,defaultcategorydataset,PlotOrientation.VERTICAL,true,true,false);
jfreechart.getLegend().setPosition(RectangleEdge.RIGHT);
jfreechart.setBackgroundPaint(Color.white);
CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
categoryplot.setNoDataMessage("无数据可供显示!");
categoryplot.setBackgroundPaint(Color.white);
categoryplot.setDomainGridlinesVisible(true);
categoryplot.setRangeGridlinesVisible(true);
categoryplot.setRangeGridlinePaint(Color.gray);
categoryplot.setDomainGridlinePaint(Color.gray);
categoryplot.setBackgroundAlpha(0.8f);
Font font1 = new Font("黑体",Font.BOLD, 14);
jfreechart.getTitle().setFont(font1);
Font font3 = new Font("隶书",Font.BOLD, 12);
jfreechart.getLegend().setItemFont(font3);
CategoryAxis categoryaxis = categoryplot.getDomainAxis();
// categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
categoryaxis.setMaximumCategoryLabelLines(10);//行数,根据需要自己设
categoryaxis.setMaximumCategoryLabelWidthRatio(0.5f);//每行宽度,这里设一个汉字宽
NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setAutoRangeIncludesZero(true);
numberaxis.setRangeWithMargins(0, 3);
numberaxis.setUpperMargin(0.8);////设置最高的一个 Item 与图片顶端的距离
numberaxis.setUpperBound(3.5);//纵坐标最大值
categoryaxis.setTickLabelFont(new Font("宋体", Font.BOLD, 12));
numberaxis.setTickLabelFont(new Font("隶书", Font.BOLD, 12));
Font font2 = new Font("SimSun", Font.BOLD, 16);
categoryaxis.setLabelFont(font2);
numberaxis.setLabelFont(font2);
categoryplot.setAxisOffset(new RectangleInsets(0D, 0D,0D, 10D));//设置曲线图与xy轴的距离
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer)categoryplot.getRenderer();
lineandshaperenderer.setShapesVisible(true); //数据点可见
lineandshaperenderer.setSeriesStroke(0, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] {
10F, 6F
}, 0.0F)); //定义series点之间的连线 ,这里是虚线,默认是直线
lineandshaperenderer.setSeriesStroke(1, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] {
6F, 6F
}, 0.0F));
lineandshaperenderer.setBaseItemLabelsVisible(true);
lineandshaperenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
ChartRenderingInfo info=new ChartRenderingInfo(new StandardEntityCollection());
String fileName = null;
try
{
fileName = ServletUtilities.saveChartAsPNG(jfreechart, 700,300, info, null);//生成图片
}
catch (IOException e)
{
e.printStackTrace();
}
String graphURL = "/projectname/DisplayChart?filename=" + fileName; //projectname为对应项目的路径path,一般就是项目名称
//jsp中这样使用: String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
return graphURL;//返回生成图片的地址
}
调用上述方法得到生成的chart的url:
getChart(stus,score_field,type,"总分图");


猜你喜欢
- Spring是什么?Spring是一个轻量级Java开发框架,最早有Rod Johnson创建,目的是为了解决企业级应用开发的业务逻辑层和其
- using System; using System.Collections.Generic; using
- 介绍今天我们将研究java中的Builder模式。Builder 设计模式是一种创造性的设计模式,如工厂模式和抽象工厂模式。当Object包
- 首先引入pom <!--SpringBoot 2.1.0--> <parent>  
- 本文实例讲述了Android中显示GIF动画的实现代码。分享给大家供大家参考,具体如下:gif图动画在android中还是比较常用的,比如像
- Shiro是什么Shiro是一个Java平台的开源权限框架,用于认证和访问授权。具体来说,满足对如下元素的支持:用户,角色,权限(仅仅是操作
- 项目地址:https://github.com/JeasonWong/SlackLoadingView老规矩,先上效果。图好大。。说下第一眼
- Mybatis typeAlias配置1.定义别名<typeAliases> <ty
- 一、引入:Android提供了View来进行绘图处理,在大部分情况下,View都能满足绘图需求。大家都知道View是通过刷新来重绘视图,An
- 一、Druid简介Druid是阿里开源的数据库连接池,作为后起之秀,性能比dbcp、c3p0更高,使用也越来越广泛。当然Druid不仅仅是一
- 一般有点开发经验的朋友都能实现这样的功能,只不过是效率上的问题。我们一般在面对这样的问题时,总会平铺直序的联想到,先生成一个数组,然后在一个
- 自从SEOTcs系统11月份24日更新了一下SEO得分算法以来,一直困扰我的一个问题出现了,java的数据job任务,在执行过程中会经常报以
- 本文实例讲述了Android控件之ListView用法。分享给大家供大家参考。具体如下:示例一:在android开发中ListView是比较
- TimeLineStepView支持时间轴和StepView,三种布局,支持水平布局,垂直布局和自定义布局,截图如下
- Springboot内部提供的事务管理器是根据autoconfigure来进行决定的。比如当使用jpa的时候,也就是pom中加入了sprin
- Java非法字符: ‘\ufeff‘Java中项目启动出现 非法字符: '\ufeff
- public void refresh() throws BeansException, IllegalStateException { &
- 本文实例讲述了Android编程实现图片放大缩小功能ZoomControls控件用法。分享给大家供大家参考,具体如下:MainActivit
- 什么是接口?说到接口,USB大家肯定不陌生~接口是一种标准、规范.注意:接口一旦制定好,使用者和实现者都必须遵循的标准.接口的语法: (JD
- 程序的最主要的功能在于对数据进行操作,通过对数据进行操作来实现某个功能。而数据库就是很重要的一个方面的,Android中内置了小巧轻便,功能