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,"总分图");

标签:jfreechart,图表,url
0
投稿

猜你喜欢

  • Java实现五子棋的基础方法

    2021-07-11 12:32:08
  • 在Java的Struts中判断是否调用AJAX及用拦截 器对其优化

    2023-11-25 00:40:40
  • Android7.0中关于ContentProvider组件详解

    2023-10-30 19:48:29
  • Java String类的性质与比较

    2021-07-29 16:48:37
  • 详解Java Proxy动态 代理机制

    2023-07-24 21:01:58
  • spring 中事务注解@Transactional与trycatch的使用

    2022-08-28 18:26:07
  • Android Studio使用小技巧:布局预览时填充数据

    2021-06-04 09:00:03
  • java 生成有序账号的实现方法

    2023-08-12 03:28:01
  • C#编程调用Cards.dll实现图形化发牌功能示例

    2022-10-24 12:02:54
  • Java数组的定义与使用

    2023-08-12 11:44:38
  • Jenkins自动化打包为war包

    2021-08-08 20:25:49
  • Lombok为啥这么牛逼?SpringBoot和IDEA官方都要支持它

    2021-10-18 23:04:50
  • C# 实现TXT文档转Table的示例代码

    2022-04-23 07:47:52
  • C++实现扫雷游戏示例讲解

    2022-05-03 18:49:05
  • 关于Struts2文件上传与自定义拦截器

    2021-10-31 12:52:44
  • jQuery 动画效果代码分享

    2023-11-24 00:10:12
  • C#实现FTP客户端的案例

    2023-06-15 19:46:47
  • Android使用AutoCompleteTextView实现自动填充功能的案例

    2023-03-26 06:56:47
  • C#中的委托、事件学习笔记

    2023-01-21 18:03:49
  • Spring和SpringBoot之间的区别

    2022-09-28 11:47:38
  • asp之家 软件编程 m.aspxhome.com