zl程序教程

您现在的位置是:首页 >  后端

当前栏目

java类中生成jfreechart,返回图表的url地址代码分享

JAVA地址代码 分享 生成 返回 url 图表
2023-06-13 09:15:04 时间

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类中方法:
复制代码代码如下:

publicStringgetChart(Stringseries[],doublescore[][],Stringtype[],Stringname){
finalintnum=8;
DefaultCategoryDatasetdefaultcategorydataset=newDefaultCategoryDataset();
for(inti=0;i<type.length;i++){ 
type[i]=type[i].substring(0,(type[i].length()<num)?type[i].length():num);
}

for(intj=0;j<series.length;j++){
inti=0;
for(i=0;i<type.length;i++){
defaultcategorydataset.addValue(score[j][i],series[j],type[i]); 
}
}

JFreeChartjfreechart=ChartFactory.createLineChart(name,null,null,defaultcategorydataset,PlotOrientation.VERTICAL,true,true,false);

jfreechart.getLegend().setPosition(RectangleEdge.RIGHT);

jfreechart.setBackgroundPaint(Color.white);

CategoryPlotcategoryplot=(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);
Fontfont1=newFont("黑体",Font.BOLD,14);
jfreechart.getTitle().setFont(font1);

Fontfont3=newFont("隶书",Font.BOLD,12);
jfreechart.getLegend().setItemFont(font3);

CategoryAxiscategoryaxis=categoryplot.getDomainAxis();
// categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
categoryaxis.setMaximumCategoryLabelLines(10);//行数,根据需要自己设
categoryaxis.setMaximumCategoryLabelWidthRatio(0.5f);//每行宽度,这里设一个汉字宽

NumberAxisnumberaxis=(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(newFont("宋体",Font.BOLD,12));
numberaxis.setTickLabelFont(newFont("隶书",Font.BOLD,12));
Fontfont2=newFont("SimSun",Font.BOLD,16);
categoryaxis.setLabelFont(font2);
numberaxis.setLabelFont(font2);
categoryplot.setAxisOffset(newRectangleInsets(0D,0D,0D,10D));//设置曲线图与xy轴的距离
LineAndShapeRendererlineandshaperenderer=(LineAndShapeRenderer)categoryplot.getRenderer();
lineandshaperenderer.setShapesVisible(true);//数据点可见

lineandshaperenderer.setSeriesStroke(0,newBasicStroke(2.0F,1,1,1.0F,newfloat[]{
10F,6F
},0.0F));//定义series点之间的连线,这里是虚线,默认是直线
lineandshaperenderer.setSeriesStroke(1,newBasicStroke(2.0F,1,1,1.0F,newfloat[]{
6F,6F
},0.0F));

lineandshaperenderer.setBaseItemLabelsVisible(true);
lineandshaperenderer.setBaseItemLabelGenerator(newStandardCategoryItemLabelGenerator());

ChartRenderingInfoinfo=newChartRenderingInfo(newStandardEntityCollection());
StringfileName=null;
try
{
fileName=ServletUtilities.saveChartAsPNG(jfreechart,700,300,info,null);//生成图片
}
catch(IOExceptione)
{
e.printStackTrace();
}

StringgraphURL="/projectname/DisplayChart?filename="+fileName;//projectname为对应项目的路径path,一般就是项目名称

//jsp中这样使用:StringgraphURL=request.getContextPath()+"/servlet/DisplayChart?filename="+filename;
returngraphURL;//返回生成图片的地址
}


调用上述方法得到生成的chart的url:
复制代码代码如下:
  getChart(stus,score_field,type,"总分图");