Package com;
Import Java.awt.BasicStroke;
Import Java.awt.Color;
Import Java.io.File;
Import java.io.IOException;
Import Org.jfree.chart.ChartFactory;
Import org.jfree.chart.ChartUtilities;
Import Org.jfree.chart.JFreeChart;
Import Org.jfree.chart.axis.NumberAxis;
Import Org.jfree.chart.plot.CategoryPlot;
Import org.jfree.chart.plot.PlotOrientation;
Import Org.jfree.chart.renderer.category.LineAndShapeRenderer;
Import Org.jfree.data.category.CategoryDataset;
Import Org.jfree.data.category.DefaultCategoryDataset;
public class Chartservlettest {
Graph
private static Categorydataset CreateDataSet () {
String series1 = "First";
String Series2 = "Second";
String Series3 = "Third";
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";
String Type8 = "Type 8";
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 (8D, series1, Type8);
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 (Series2, Type7);
defaultcategorydataset.addvalue (1.0D, Series2, type8);
Defaultcategorydataset.addvalue (4 D, Series3, type1);
Defaultcategorydataset.addvalue (Series3, type2);
Defaultcategorydataset.addvalue (Series3, type3);
Defaultcategorydataset.addvalue (Series3, type4);
Defaultcategorydataset.addvalue (6D, Series3, Type5);
Defaultcategorydataset.addvalue (Series3, Type6);
Defaultcategorydataset.addvalue (4 D, Series3, Type7);
Defaultcategorydataset.addvalue (Series3, Type8);
return defaultcategorydataset;
}
Graph
private static Jfreechart Createchart (Categorydataset categorydataset) {
Jfreechart Jfreechart = Chartfactory.createlinechart (
"Line Chart Demo 1", "Type", "Value", Categorydataset,
Plotorientation.vertical, True, true, false);
Jfreechart.setbackgroundpaint (Color.White);
Categoryplot Categoryplot = (categoryplot) jfreechart.getplot ();
Categoryplot.setbackgroundpaint (Color.lightgray);
Categoryplot.setrangegridlinepaint (Color.White);
Numberaxis Numberaxis = (numberaxis) categoryplot.getrangeaxis ();
Numberaxis.setstandardtickunits (Numberaxis.createintegertickunits ());
Numberaxis.setautorangeincludeszero (TRUE);
Get renderer note here is the next fast sulphon styling to Lineandshaperenderer.
Lineandshaperenderer lineandshaperenderer = (lineandshaperenderer) categoryplot
. Getrenderer ();
Lineandshaperenderer.setshapesvisible (TRUE); Series points (that is, data points) are visible
Lineandshaperenderer.setseriesstroke (0, New Basicstroke (2.0F, 1, 1,
1.0F, new float[] {10F, 6F}, 0.0F)); Define the line between the "first" (i.e. series1) points of the series
, here is the dashed line, the default is Straight
Lineandshaperenderer.setseriesstroke (1, New Basicstroke (2.0F, 1, 1,
1.0F, new float[] {6F, 6F}, 0.0F)); Defines the line between the "Second" (i.e., Series2) points of the series
Lineandshaperenderer.setseriesstroke (2, New Basicstroke (2.0F, 1, 1,
1.0F, new float[] {2.0F, 6F}, 0.0F)); Defines the line between the "third" (i.e., series3) points of the series
return jfreechart;
}
/**
* @param args
* @throws IOException
*/
public static void Main (string[] args) throws IOException {
Pie chart Data source
Defaultcategorydataset DFG = new Defaultcategorydataset ();
Dfg.setvalue (25, "Administrative staff", "administrative staff");
Dfg.setvalue (12, "market personnel", "market personnel");
Dfg.setvalue (20, "research and development staff", "research and Development staff");
Dfg.setvalue (5, "financial personnel", "financial personnel");
Dfg.setvalue (16, "Other personnel", "other personnel");
//
Jfreechart chart = Chartfactory.createbarchart ("Organization Chart of a Company", "Personnel distribution",
"Number of personnel", DFG, Plotorientation.vertical, True, false, false);
Chart.gettitle (). SetFont (New Font ("script", Font.Bold + font.italic,
15));
Chart.getlegend (). Setitemfont (New Font ("script", Font.Bold + font.italic,
15));
Chart.getcategoryplot (). Getdomainaxis (). Setlabelfont ("Script",
Font.Bold + font.italic, 15));
Chart.getcategoryplot (). Getdomainaxis (). Setticklabelfont (New
Font ("Script", Font.Bold + font.italic, 15));
Chart.getcategoryplot (). Getrangeaxis (). Setlabelfont ("Script",
Font.Bold + font.italic, 15));
Jfreechart chart = Createchart (CreateDataSet ());
Column Chart
Defaultpiedataset DataSet = new Defaultpiedataset ();
Dataset.setvalue ("China", 44.4);
Dataset.setvalue ("The USA", 25.5);
Dataset.setvalue ("Japan", 18.5);
Dataset.setvalue ("Congo", 4.6);
Dataset.setvalue ("Great Britain", 5.5);
Dataset.setvalue ("Other", 0.5);
Create a 3D pie chart
Pieplot3d plot = new Pieplot3d (dataset);
Plot.settooltipgenerator (New Standardpietooltipgenerator ());
Set up a picture link
Plot.seturlgenerator (New Standardpieurlgenerator (
"Http://www.blogjava.net/supercrsky"));
//
Jfreechart chart = new Jfreechart ("Demographic Tables",
Jfreechart.default_title_font, Plot, true);
Chart.setbackgroundpaint (Color.lightgray);
Chartutilities
. Savechartasjpeg (New File (
"D:/temp/jfreechart/webroot/images/chart.jpg"), chart,
800, 600);
}
}