欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-17 00:00:52
web.xml中设置:复制代码 代码如下: displaychart

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

上一篇:

下一篇: