WebChart. java Package net. sentom. chart; Import java. awt. Insets; Import java. awt. Font; Import java. io. PrintWriter; Import javax. servlet. http. HttpSession; Import org. jfree. data .*; Import org. jfree. chart .*; Import org. jfree. chart. plot .*; Import org. jfree. chart. entity .*; Import org. jfree. chart. urls .*; Import org. jfree. chart. servlet .*; Import org. jfree. chart. labels. StandardPieToolTipGenerator; /** * @ Author sentom * * To change the template for this generated type comment go * Window> Preferences> Java> Code Generation> Code and Comments */ Public class WebChart { Private defapipiedataset data = new DefaultPieDataset (); Public void setValue (String key, double value ){ Data. setValue (key, value ); } Public String generatePieChart (String title, HttpSession session, PrintWriter pw ){ String filename = null; Try { // Create a chart object PiePlot plot = new PiePlot (data ); Plot. setInsets (new Insets (0, 5, 5, 5 )); // Create a link on the statistical Image Plot. setURLGenerator (new StandardPieURLGenerator ("link. jsp", "section ")); Plot. setToolTipGenerator (new StandardPieToolTipGenerator ()); JFreeChart chart = new JFreeChart ("", JFreeChart. DEFAULT_TITLE_FONT, Plot, true ); Chart. setBackgroundPaint (java. awt. Color. white); // you can specify the background Color of an image. Font font = new Font ("", Font. CENTER_BASELINE, 20); // set the Font of the image title And size TextTitle _ title = new TextTitle (title ); _ Title. setFont (font ); Chart. setTitle (_ title ); // Put the generated image in a temporary directory ChartRenderingInfo info = new ChartRenderingInfo (new StandardEntityCollection ()); // 500 indicates the image length and 300 indicates the image height. Filename = ServletUtilities. saveChartAsPNG (chart, 500,300, info, Session ); ChartUtilities. writeImageMap (pw, filename, info ); Pw. flush (); } Catch (Exception e ){ System. out. println ("Exception-" + e. toString ()); E. printStackTrace (System. out ); Filename = "public_error_500x300.png "; } Return filename; } } JSP file <% @ Page contentType = "text/html; charset = GBK" %> <% @ Page import = "java. io. PrintWriter" %> <% @ Page import = "net. sentom. chart. WebChart" %> <% WebChart chart = new WebChart (); Chart. setValue ("June", 500 ); Chart. setValue ("July", 580 ); Chart. setValue ("August", 828 ); String filename = chart. generatePieChart ("www.111cn.net website access statistics table", session, new PrintWriter (out )); String graphURL = request. getContextPath () + "/servlet/DisplayChart? Filename = "+ filename; %> <HTML> <HEAD> <TITLE> www.sentom.net </TITLE> </HEAD> <BODY> <P align = "CENTER"> " width = 500 height = 300 border = 0 usemap = "# <% = Filename %> "> </P> </BODY> </HTML> |