Jfreechart encapsulation Tool

Source: Internet
Author: User
Tags border color
Package COM. wasu. ITV. common. util; import Java. AWT. basicstroke; import Java. AWT. color; import Java. AWT. font; import Java. io. file; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. SQL. timestamp; import Java. text. decimalformat; import Java. text. numberformat; import Java. text. simpledateformat; import Java. util. iterator; import Java. util. list; import javax. servlet. HTTP. httpservletrequest; import Org. codehaus. xfire. addressing. randomguid; import Org. jfree. chart. chartfactory; import Org. jfree. chart. chartutilities; import Org. jfree. chart. jfreechart; import Org. jfree. chart. axis. categoryaxis; import Org. jfree. chart. labels. standardcategoryitemlabelgenerator; import Org. jfree. chart. labels. standardpiesectionlabelgenerator; import Org. jfree. chart. labels. standardpietooltipgenerator; import Org. jfree. chart. plot. categoryplot; import Org. jfree. chart. plot. pieplot; import Org. jfree. chart. plot. plotorientation; import Org. jfree. chart. renderer. category. barrenderer; import Org. jfree. chart. title. legendtitle; import Org. jfree. chart. title. texttitle; import Org. jfree. data. category. categorydataset; import Org. jfree. data. category. defaultcategorydataset; import Org. jfree. data. general. defaultpiedataset; import Org. jfree. util. rotation; public class jfreechartutils {/*** step2: Create a piechart chart ** @ Param dataset * @ return */Private Static jfreechart createpiechart (defaultpiedataset dataset, String title, Boolean is3d) {jfreechart chart = NULL; If (is3d) {chart = chartfactory. createpiechart3d (title, // chart title dataset, // dataset true, // whether to display the legend true, // whether to display the tool prompt true // whether to generate a URL );} else {chart = chartfactory. createpiechart (title, // chart title dataset, // dataset true, // whether to display the legend true, // whether to display the tool prompt true // whether to generate a URL );} // set the title Font = to prevent Chinese garbled characters: You must set the font chart. settitle (New texttitle (title, new font ("", Font. italic, 22); // set the font of the legend = to prevent Chinese garbled characters: You must set the font chart. getlegend (). setitemfont (new font ("", Font. bold, 12); // obtain the plot object of the pie chart (actual chart) pieplot plot = (pieplot) chart. getplot (); // graphic border color plot. setbasesectionoutlinepaint (color. gray); // graphic Border width (PLOT. setbasesectionoutlinestroke (New basicstroke (0.0f); // you can set the orientation of the pie chart, which can be drawn clockwise or counterclockwise. setdirection (rotation. anticlockwise); // set the drawing angle (image rotation angle) plot. setstartangle (70); // set the highlighted data block // plot. setexplodepercent ("one", 0.1d); // sets the background color transparency. setbackgroundalpha (0.7f); // sets the foreground transparency. setforegroundalpha (0.65f); // set the font of the block label ==to prevent Chinese garbled characters: The font plot must be set. setlabelfont (new font ("", Font. plain, 12); // slice separation display, not effective for 3D images if (is3d) plot. setexplodepercent (Dataset. getkey (3), 0.1d); // legend display percentage: Custom mode. {0} indicates the option, {1} indicates the value, and {2} indicates the proportion, two plot digits after the decimal point. setlabelgenerator (New standardpiesectionlabelgenerator ("{0 }:{ 1} \ r \ n ({2})", numberformat. getnumberinstance (), new decimalformat ("0.00%"); // The percentage displayed in the legend // plot. setlegendlabelgenerator (new // standardpiesectionlabelgenerator ("{0 }={ 1} ({2})"); // specify the pie chart to show as circle (true) or an elliptical (false) plot. setcircular (true); // The content displayed when no data exists. setnodatamessage ("available data not found... "); // set the cursor hover prompt to plot. settooltipgenerator (New standardpietooltipgenerator (); // set the hotspot link // plot. seturlgenerator (New standardpieurlgenerator ("detail. JSP "); Return chart;} Private Static jfreechart createbarchart (categorydataset dataset, String title, string X, string y, Boolean is3d) {jfreechart chart = NULL; If (is3d) {chart = chartfactory. createbarchart3d (// 3D bar chart // jfreechart chart = chartfactory. createlinechart3d (// 3D line chart title, // The Title X of the chart, // The label y for displaying the directory axis, // The label dataset for displaying the value axis, // The dataset plotorientation. vertical, // chart mode: V vertical; H horizontal true, // whether to display the legend false, // whether to display the tool prompt false // whether to generate a URL );} else {chart = chartfactory. createbarchart (// bar chart // jfreechart chart = chartfactory. createlinechart3d (// 3D line chart title, // The Title X of the chart, // The label y for displaying the directory axis, // The label dataset for displaying the value axis, // The dataset plotorientation. vertical, // chart mode: V vertical; H horizontal true, // whether to display the legend false, // whether to display the tool prompt false // whether to generate a URL );} // ====================== to prevent Chinese garbled characters: You must set the font chart. settitle (New texttitle (title, new font ("", Font. italic, 22); legendtitle legend = chart. getlegend (); // obtain the legend. setitemfont (new font ("", Font. bold, 12); // set the font of the legend to prevent Chinese garbled categoryplot plot = (categoryplot) chart. getplot (); // obtain the plot object of the bar chart (actual chart) // set the background color of the bar chart (note, the system needs to use the 16-Bit mode to view the color encoding when taking the color, which is more accurate) plot. setbackgroundpaint (new color (255,255,204); plot. setforegroundalpha (0.65f); // sets the foreground transparency. // sets the horizontal dotted line to be visible. setrangegridlinesvisible (true); // dotted line color plot. setrangegridlinepaint (color. gray); categoryaxis H = plot. getdomainaxis (); // obtain the X axis H. setmaximumcategorylabelwidthratio (1.0f); // whether the lable on the horizontal axis is completely displayed H. setlabelfont (new font ("", Font. bold, 12); // set the font to prevent Chinese garbled characters. setticklabelfont (new font ("", Font. bold, 12); // axis value // H. setcategorylabelpositions (categorylabelpositions. up_45); // 45 degree skewed plot. getrangeaxis (). setlabelfont (new font ("", Font. bold, 12); // set the font on the Y axis to prevent Chinese garbled characters. // barrenderer Renderer = new barrenderer (); // set the column width // Renderer. setmaximumbarwidth (0.05); // set the column height // Renderer. setminimumbarlength (0.2); // set the bar border color Renderer. setbaseoutlinepaint (color. black); // set the bar border to be visible Renderer. setdrawbaroutline (true); // you can specify the Renderer color for each bar. setseriespaint (0, color. blue); Renderer. setseriespaint (1, color. green); Renderer. setseriespaint (2, color. red); // set the distance between parallel columns in each region to Renderer. setitemmargin (0.05); // display the value of each column and modify the font attribute Renderer. setincludebaseinrange (true); Renderer. setbaseitemlabelgenerator (New standardcategoryitemlabelgenerator (); Renderer. setbaseitemlabelsvisible (true); // sets the bar transparency. setforegroundalpha (1.0f); // Add the Renderer to the bar chart. setrenderer (Renderer); // The content displayed when no data exists. setnodatamessage ("available data not found... "); Return chart ;} /***** @ Param data * @ Param title * @ Param x * @ Param y * @ Param width * @ Param heigth * @ Param Request * @ return */public static string generatebarchart (list <object []> data, string title, string X, string y, int width, int heigth, Boolean is3d, httpservletrequest request) {Boolean returnvalue = false; defaultcategorydataset dataset = new defacategcategorydataset (); for (iterator <object []> iterator = data. iterator (); iterator. hasnext ();) {object [] O = (object []) iterator. next (); dataset. setvalue (long) O [1]). intvalue (), O [0] + ":" + O [1], O [2]. tostring ();} jfreechart chart = createbarchart (dataset, title, X, Y, is3d); randomguid guid = new randomguid (); simpledateformat df = new simpledateformat ("/yyyy/mm/DD/"); string subpath = DF. format (New timestamp (system. currenttimemillis () + guid. valueaftermd5 + ". jpg "; string returnpath ="/images/ITV/chart/"+ subpath; file F = new file (request. getsession (). getservletcontext (). getrealpath ("/") + returnpath); F. getparentfile (). mkdirs (); returnvalue = drawtooutputstream (F. getabsolutepath (), chart, width, heigth); Return returnpath;} public static string generatepiechart (list <object []> data, String title, int width, int heigth, httpservletrequest request, boolean is3d) {Boolean returnvalue = false; defaultpiedataset = new defapipiedataset (); For (iterator <object []> iterator = data. iterator (); iterator. hasnext ();) {object [] O = (object []) iterator. next (); dataset. setvalue (O [0]. tostring (), (long) O [1]). doublevalue ();} jfreechart chart = createpiechart (dataset, title, is3d); randomguid guid = new randomguid (); simpledateformat df = new simpledateformat ("/yyyy/mm/DD/"); string subpath = DF. format (New timestamp (system. currenttimemillis () + guid. valueaftermd5 + ". jpg "; string returnpath ="/images/ITV/chart/"+ subpath; file F = new file (request. getsession (). getservletcontext (). getrealpath ("/") + returnpath); F. getparentfile (). mkdirs (); returnvalue = drawtooutputstream (F. getabsolutepath (), chart, width, heigth); Return returnpath;}/*** Step3: output the chart to the specified disk ** @ Param destpath * @ Param chart */Private Static Boolean drawtooutputstream (string destpath, jfreechart chart, int width, int heigth) {fileoutputstream Fos = NULL; boolean returnvalue = true; try {Fos = new fileoutputstream (destpath); chartutilities. writechartaspng (FOS, // specify the target output stream chart, // chart object width, // width heigth, // high null); // chartrenderinginfo} catch (ioexception E) {e. printstacktrace (); returnvalue = false;} finally {try {FOS. close ();} catch (ioexception e) {e. printstacktrace () ;}} return returnvalue;}/*** @ Param ARGs */public static void main (string [] ARGs) {categorydataset dataset = createbardataset2 (); jfreechart chart = createbarchart (dataset, "111111111111111", "2222222", "2222222", true); drawtooutputstream ("D: \ mybook-pie.png", chart, 640,480 );} private Static defapipiedataset createpiedataset1 () {defaultpiedataset dataset = new defaultpiedataset (); dataset. setvalue ("Java programming language", 10000); dataset. setvalue ("JSP basic and case development details", 20000); dataset. setvalue ("struts basics and case development details", 30000); dataset. setvalue ("proficient in JSF", 40000); Return dataset;} Private Static defaultpiedataset createpiedataset2 () {defapipiedataset dataset = new defaultpiedataset (); dataset. setvalue ("market early", new double (10); dataset. setvalue ("project initiation", new double (15); dataset. setvalue ("scheduler", new double (10); dataset. setvalue ("requirement and design", new double (10); dataset. setvalue ("execution control", new double (35); dataset. setvalue ("", new double (10); dataset. setvalue ("O & M", new double (10); Return dataset;}/*** Step1: create a simple DataSet object ** @ return */Private Static categorydataset createbardataset1 () {defacategcategorydataset dataset = new defaultcategorydataset (); dataset. setvalue (10000, "", "corejava"); dataset. setvalue (20000, "", "javaweb"); dataset. setvalue (30000, "", "Easy to use struts"); dataset. setvalue (40000, "", "proficient in JSF"); Return dataset;}/*** combined DataSet object ** @ return */Private Static categorydataset createbardataset2 () {defacategcategorydataset dataset = new defaultcategorydataset (); dataset. setvalue (5000, "Beijing", "corejava"); dataset. setvalue (3000, "Shanghai", "corejava"); dataset. setvalue (2000, "Guangzhou", "corejava"); dataset. setvalue (10000, "Beijing", "javaweb"); dataset. setvalue (6000, "Shanghai", "javaweb"); dataset. setvalue (4000, "Guangzhou", "javaweb"); dataset. setvalue (15000, "Beijing", "Easy to use struts"); dataset. setvalue (5000, "Shanghai", "Easy to use struts"); dataset. setvalue (10000, "Guangzhou", "Easy to use struts"); dataset. setvalue (20000, "Beijing", "JSF proficient"); dataset. setvalue (10000, "Shanghai", "JSF proficient"); dataset. setvalue (10000, "Guangzhou", "JSF proficient"); Return dataset ;}}
 
http://iehyou.iteye.com/blog/586982

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.