Java class to generate Jfreechart, return to the chart URL address code sharing _java

Source: Internet
Author: User

Set in Web.xml:

Copy Code code as follows:

<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>

Methods in Java classes:
Copy Code code as follows:

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 ("Countless evidence available!") ");
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 ("bold", Font.Bold, 14);
Jfreechart.gettitle (). SetFont (FONT1);

Font font3 = new Font ("script", Font.Bold, 12);
Jfreechart.getlegend (). Setitemfont (FONT3);

Categoryaxis Categoryaxis = Categoryplot.getdomainaxis ();
Categoryaxis.setcategorylabelpositions (CATEGORYLABELPOSITIONS.UP_45);
Categoryaxis.setmaximumcategorylabellines (10);//line number, set according to need
Categoryaxis.setmaximumcategorylabelwidthratio (0.5f);//width of each line, set a width of Chinese characters here

Numberaxis Numberaxis = (numberaxis) categoryplot.getrangeaxis ();
Numberaxis.setstandardtickunits (Numberaxis.createintegertickunits ());
Numberaxis.setautorangeincludeszero (TRUE);
Numberaxis.setrangewithmargins (0, 3);

Numberaxis.setuppermargin (0.8)////set the highest one Item to the top of the picture distance
Numberaxis.setupperbound (3.5);/Ordinate maximum value

Categoryaxis.setticklabelfont (New Font ("Song Body", Font.Bold, 12));
Numberaxis.setticklabelfont (New Font ("Official script", 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))//Set the distance between the graph and the XY axis
Lineandshaperenderer lineandshaperenderer = (lineandshaperenderer) categoryplot.getrenderer ();
Lineandshaperenderer.setshapesvisible (TRUE); Data points visible

Lineandshaperenderer.setseriesstroke (0, New Basicstroke (2.0F, 1, 1, 1.0F, new float[] {
10F, 6F
}, 0.0F)); Define the line between the series points, which is dotted, and the default is a straight line
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);//Generate Picture
}
catch (IOException E)
{
E.printstacktrace ();
}

String Graphurl = "/projectname/displaychart?filename=" + filename; ProjectName is the path to the corresponding project, which is usually the project name

This is used in JSP: String Graphurl = Request.getcontextpath () + "/servlet/displaychart?filename=" + filename;
Return graphurl;//returns the address of the generated picture
}


Invoke the above method to get the generated chart URL:
Copy Code code as follows:

Getchart (Stus,score_field,type, "total picture");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.