Easily implement data pie chart 4 in the JSP page

xiaoxiao2021-03-06  62

Attachment: All source code this article

Listing E

<% @ Page language = "java"%> <% @ page import = "java.io.outputstream"%> <% @ page import = "java.sql. *"%> <% @ Page import = "java. AWT. * "%> <% @ page import =" java.awt.geom. * "%> <% @ page import =" java.awt.image.bufferedImage "%> <% @ page import =" com.sun . * "%> <%! // PIECOLORS CLASS MANAGES The Colors Used in the Pie Chart Class Piecolors {Color PieColorarray [] = {New Color (210, 60, 60), New Color (60, 210, 60), New Color (60, 60, 1210), New Color (60, 120, 210), New Color (210, 120, 60)}; int curpiecolor = 0; public color color getpiecolor () {Return PieColorarray [CURPIECOLOR] ;} public void setNewColor () {curPieColor ; if (curPieColor> = pieColorArray.length) {curPieColor = 0;}}}%> <% String driver = "com.mysql.jdbc.Driver";! String dburl = "jdbc : mysql: // localhost / articles "; string dbuid =" myuid "; string dbpwd =" mypwd "; // get the produter from the database as a string array public string [] getProducts () {String [] arr = new String [0]; connection con; nt stmt; ResultSet rs; int count = 0; String sql = "select * from p_products order by productID"; try {// Load Driver: Class.forName (driver); // Connect to the database with the url con = DriverManager .getConnection (DBURL, DBUID, DBPWD); Stmt = con.createstatement (); // get resultset = stmt.executeQuery (SQL); // count the records while (rs.next ()) {count ;

} // crete an array of the correct size arr = new string [count]; // get resultset (the Most Portable Way of Using RS A Second Time) RS = Stmt.executeQuery (SQL); while (rs.next () ) {Arr [r.get ("productID")] = rs.getstring ("productname");} stmt.close (); con.close ();} catch (java.lang.exception ex) {Arr [0 ] = EX.TOSTRING ();} Return Arr;} // Get The sales totals from the database public float [] getsales (int products) {float [] arr = new float [product]; connection con; statement stmt; result rs; String sql = "select productID, amount from p_sales"; try {// Load Driver: Class.forName (driver); // Connect to the database with the url con = DriverManager.getConnection (dburl, dbuid, dbpwd); Stmt = con.createstatement (); // get resultset = stmt.executeQuery (SQL); while (rs.next ()) {int product = rs.getinT ("ProductID"); // Check That The ProductID Is Valid IF (Product> = 0 && Product <% // Get an Array that contains the product names String products [] = getProducts (); // read the data and store the totals in an array float sales [] = getSales (products.length); // Declare PieColors PieColors pc = new PieColors (); // Colors Color dropShadow = new Color (240,240,240); // inner padding to make sure bars never touch the outer border int innerOffset = 20; // Set the graph's outer width & height int wIDTH = 400; int hEIGHT = 200; int Pieheight =

HEIGHT - (innerOffset * 2); int pieWidth = pieHeight; // To make a square (circular) pie int halfWidth = WIDTH / 2; // Width of the inner graphable area int innerWIDTH = WIDTH - (innerOffset * 2); / / graph dimensions Dimension graphDim = new Dimension (WIDTH, HEIGHT); Rectangle graphRect = new Rectangle (graphDim); // border dimensions Dimension borderDim = new Dimension (halfWidth-2, HEIGHT-2); Rectangle borderRect = new Rectangle (borderDim) ; / // Set up the graph // Set content type response.setContentType ( "image / jpeg"); // Create BufferedImage & Graphics2D BufferedImage bi = new BufferedImage (WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = bi. createGraphics (); // Set Antialiasing RenderingHints renderHints = new RenderingHints (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHints (renderHints); // Set graph background color to white: g2d.setColor (Color.white); g2d. Fill (graphRect); // Draw Black Border G2d.SetColor (Color.Black); BOR DERRECT.SETLOCATION (1, 1); g2d.draw (borderRect); // Now draw border for legend borderRect.setLocation (width / 2) 1, 1); g2d.draw (borderRect); // Draw Data ONTO the graph: x_pie = innerOffset int; int y_pie = innerOffset; int border = 20; // Main chart Ellipse //Ellipse2D.Double el = new Ellipse2D.Double (x_pie, y_pie, pieWidth, pieHeight); Ellipse2D.Double elb = new Ellipse2d.double (x_pie - border / 2, y_pie - border / 2, piewidth border, pieheight border); // shadow g2d.setcolor (Dropshadow); g2d.fill (ELB); // border g2d.setcolor (Color .black); g2d.draw (elb); / // Calculate the total sales / float salestotal = 0.0f;

INT lastelement = 0; for (int i = 0; i 0.0f) {sales = Sales [i]; lastelement = i;}} //// // Draw the pie chart / // Chart variables int startAngle = 0; // Legend variables int legendWidth = 20; int x_legendText = halfWidth innerOffset / 2 legendWidth 5; int x_legendBar = halfWidth innerOffset / 2; int textHeight = 20; int curElement = 0; int y_legend = 0; // Dimensions of the legend bar Dimension legendDim = new Dimension (legendWidth, textHeight / 2); Rectangle legendRect = new Rectangle (legendDim); for (int i = 0; i 0.0F) {// calculate percentage sales float perc = (Sales [i] / salestotal); // Calculate new Angle int swofle = (int) (Perc * 360); // Check what the last element goes back to 0 position if (i == lastelement) {SWEEPANGLE = 360-startangle;} // DRAW ARC G2D.SETCOLOR (pc.getpiecolor ()); g2d.fillarc (x_pie, y_pie, Piewidth, Pieheight, Startangle, Sweep Angle); // Increment startAngle with the sweepAngle startAngle = sweepAngle; / // Draw Legend / // Set y position for bar y_legend = curElement * textHeight innerOffset; // Display the current product String display = products [i]; G2D.SetColor (color.black); g2d.drawstring (Display, x_lendtext, y_legend); // display the total sales display = " (int) sales [i]; g2d.setcolor (color.black); G2D. DrawString (Display, X_LgeEndtext 80, Y_Legend); // Display THEES Percentage Display = "("

转载请注明原文地址:https://www.9cbs.com/read-86177.html

New Post(0)