Import Java.awt.BasicStroke;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.io.File;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import javax.servlet.http.HttpSession;
Import Oracle.jrockit.jfr.openmbean.JFRStatsType;
Import Org.jfree.chart.ChartFactory;
Import Org.jfree.chart.ChartRenderingInfo;
Import org.jfree.chart.ChartUtilities;
Import Org.jfree.chart.JFreeChart;
Import Org.jfree.chart.axis.CategoryAxis;
Import org.jfree.chart.axis.CategoryLabelPositions;
Import Org.jfree.chart.axis.NumberAxis;
Import org.jfree.chart.entity.StandardEntityCollection;
Import Org.jfree.chart.plot.CategoryPlot;
Import org.jfree.chart.plot.PlotOrientation;
Import Org.jfree.chart.renderer.category.LineAndShapeRenderer;
Import org.jfree.chart.servlet.ServletUtilities;
Import Org.jfree.chart.title.LegendTitle;
Import Org.jfree.data.category.DefaultCategoryDataset;
Import Org.jfree.ui.RectangleEdge;
public class Charttest
{
public static void Main (string[] args) {
Charttest test=new charttest ();
Test.generatefile (Test.getchart ());
}
Public Jfreechart Getchart () {
String series1 = "a";
String Series2 = "Second";
String type1 = "Type 1";
String type2 = "Type 2";
String type3 = "Type 3";
String type4 = "Type 4";
String Type5 = "Type 5";
String Type6 = "Type 6";
String Type7 = "Type 7";
Defaultcategorydataset defaultcategorydataset = new Defaultcategorydataset ();
Defaultcategorydataset.addvalue (1.0D, series1, type1);
Defaultcategorydataset.addvalue (4 D, series1, type2);
Defaultcategorydataset.addvalue (Series1, type3);
Defaultcategorydataset.addvalue (5D, series1, type4);
Defaultcategorydataset.addvalue (5D, series1, Type5);
Defaultcategorydataset.addvalue (7D, series1, Type6);
Defaultcategorydataset.addvalue (7D, series1, Type7);
Defaultcategorydataset.addvalue (5D, Series2, type1);
Defaultcategorydataset.addvalue (7D, Series2, type2);
Defaultcategorydataset.addvalue (6D, Series2, type3);
Defaultcategorydataset.addvalue (8D, Series2, type4);
Defaultcategorydataset.addvalue (4 D, Series2, Type5);
Defaultcategorydataset.addvalue (4 D, Series2, Type6);
Defaultcategorydataset.addvalue (2d, series2, Type7);
Jfreechart Jfreechart = Chartfactory.createlinechart ("line Chart Demo 1", "Type", "Value", Defaultcategorydataset, Plotorientation.vertical,true,true,false);
Chartfactory.createlinechart (title, Categoryaxislabel, Valueaxislabel, DataSet, orientation, Legend, ToolTips, URLs )
When the above statement legend is set to false, it means that the newly created Jfreechart has no legend, and when True, the legend is the default position.
Jfreechart.getlegend (). SetPosition (rectangleedge.right);//Get legend, set location
Legendtitle legendtitle = new Legendtitle (Jfreechart.getplot ());//Create Legend, if there is default, still create, there are two at this time.
Legendtitle.setposition (rectangleedge.right);//Set the location of the legend
Jfreechart.addlegend (legendtitle);//Chart Add legend
Jfreechart.setbackgroundpaint (Color.White);//Set the background color of the legend
Categoryplot Categoryplot = (categoryplot) jfreechart.getplot ();
Categoryplot.setbackgroundpaint (Color.lightgray);
Categoryplot.setdomaingridlinesvisible (TRUE);
Categoryplot.setrangegridlinesvisible (TRUE);
Categoryplot.setdomaingridlinepaint (Color.White);/Set horizontal axis grid line
Categoryplot.setrangegridlinepaint (Color.White);//Set Ordinate grid lines
1, the title
Font font1 = new Font ("bold", Font.Bold, 14); Set font, type, font size
Jfreechart.gettitle (). SetFont (FONT1); Title
3, the following box area is a Legendtitle object
Font font3 = new Font ("script", Font.Bold, 12); Set font, type, font size
Jfreechart.getlegend (). Setitemfont (FONT3)/Bottom
This is the horizontal axis that handles the plot, and the same can be nyaya the shaft.
Categoryaxis Categoryaxis = Categoryplot.getdomainaxis (); On the horizontal axis
Ordinate line wrap Display
Categoryaxis.setmaximumcategorylabellines (10);//line number, set according to need
Categoryaxis.setmaximumcategorylabelwidthratio (0.5f);//width of each line, set a width of Chinese characters here
Categoryaxis.setcategorylabelpositions (categorylabelpositions.up_45)//45 degree tilt, can be changed to other, the default is horizontal
Numberaxis Numberaxis = (numberaxis) categoryplot.getrangeaxis ();
Numberaxis.setstandardtickunits (Numberaxis.createintegertickunits ())//Data axis data labels (you can display only integer labels, Need to set Autotickunitselection to false)
Numberaxis.setautorangeincludeszero (TRUE);//whether to force inclusion of 0 in the automatically selected range of data
Categoryaxis.setticklabelfont (New Font ("Sansserif", Font.Bold, 12);/axis Ruler value font
Numberaxis.setticklabelfont (New Font ("SimSun", Font.Bold, 12));
2, the figure inside the set of Chinese characters, that is, plot settings
Font font2 = new Font ("SimSun", Font.Bold, 16); Set font, type, font size
Categoryaxis.setlabelfont (Font2);//Axis Title font
Numberaxis.setlabelfont (Font2);
Lineandshaperenderer lineandshaperenderer = (lineandshaperenderer) categoryplot.getrenderer ();
Lineandshaperenderer.setshapesvisible (TRUE); Series point (i.e. data point) visible
Lineandshaperenderer.setseriesstroke (0, New Basicstroke (2.0F, 1, 1, 1.0F, new float[] {
10F, 6F
}, 0.0F)); Defines the line between the series (that is, the series1) point, which is dashed, and the default is a straight line.
Lineandshaperenderer.setseriesstroke (1, New Basicstroke (2.0F, 1, 1, 1.0F, new float[] {
6F, 6F
}, 0.0F)); Defines the connection between the series (that is, the Series2) of the Second.
return jfreechart; Back to Jfreechart
}
public void Generatefile (Jfreechart chart) {
String fileName = null;
try {
Chartutilities.savechartasjpeg (new file ("C:\\piechart.jpg"), chart,600,600);//Generate a chart file to the specified location
} catch (IOException e)
{
E.printstacktrace ();
}
}
Public String getfilename (jfreechart chart,httpsession session,printwriter pw) {
String fileName = nu ll
Chartrenderinginfo info=new chartrenderinginfo (New Standardentitycollection ());
try {
FileName = servletutilities.savechartaspng (chart, 450, info, session); //Generate temporary file
Char Tutilities.writeimagemap (PW, FileName, info, false);
Pw.flush ();
}
catch (IOException e)
{
E.printstacktrace ();
}
String Graphurl = Request.getcontextpath () + "/servlet/displaychart?filename=" + fileName;
Request.setattribute ("filename", filename);
Request.setattribute ("Graphurl", Graphurl);
Return filename;//returns the file name of the generated picture, passing in the JSP
}
}