JSP provides a lot of simple and practical tools, including reading data from the database, transmitting data, and can display results in a cake-like graphic. Let us now look at this simple and practical method. What you need is transferred from: Dynamic Network Production Guide www.knowsky.com In order to correctly run this article related example, you must need JDK 1.2 or higher, a relational database management system, a JSP network server. I am debugging these examples in Tomcat, and I also use the com.sun.image.codec.jpegClasses published by Sun Java 2 SDK. Database design assumes that you have worked in a company that engaged in fresh fruit, the fruits for sale include: apples, oranges, grapes. Now your boss wants to use a cake-like graphic to show the total sales of each fruit, and the piematic graphic can make the sales situation of each product at a glance, and the boss can quickly master the company's product transaction. Table A uses two database lists in this article. The first list (Products) contains the name of all sales products; Sales contains the sales volume corresponding to each product. Product (Products) list contains both domains of ProductID and ProductName. Sales (Sales) list contains SaleID, ProductID, and total. ProductID in the sales list provides the association between the two lists. The total amount in the sales list contains the amount of cash for every sale, which appears in floating-point data. Transfer from: Dynamic Network Production Guide WWW.KNOWSKY.COM Table B The getProducts () method is connected to the two databases, and saved all the product names in the array, I set the following database rules: ProductID is the most unique in the product list It is also the most critical; ProductID is 0 for the first record; all subsequent continuous records are accumulated, so the second recorded productId is 1, the third recorded ProductID is 2, and so on. These database rules allow data to be stored in the Product array, as shown below: Arr [rs.get ("ProductID")] = rs.getstring ("ProductName"); Some database management systems are allowed by default Accumulate or automatically sort. When you design a database, you must first identify what rules in your database management system, such as automatic accumulation, automatic sorting. Get total sales In most cases, there will be many records in the sales list, so it is very important to access the shortcut and efficiency of the database. Now we only need to access the total sales volume of each product in the database. The getsales () method in Table C is connected to the database and returns an array, which contains the total amount of sale of each product. When GetSales () traversed all records, it only stores new sales quantity: transfer from: Dynamic Network Production Guide www.knowsky.comint Product = rs.getInt ("ProductID"); Arr [Product] = Rs.getfloat ("Amount"); each product on the PieColor object cake pattern should be displayed in different colors. To achieve this, we create a PIECOLOR object (such as Table D), this object contains arrays related to colors: color poecolarray [] = {New Color (210, 60, 60), New Color (60, 210, 60 The PIECOLOR class defines a SetNewColor () method, which can make CurpieColor and index increment, and it can check that the index should not exceed the boundary range, that is, the method used is: if CurpieColor is 即 0 0 value.
More effective, setnewcolor () loop after each color and performs the following code under the first color: CurpieColor ; if (CurpieColor> = PIECOLORRAY.LENGTH) {curpiecolor = 0;} RenderingHints and Antialiasing class Java. The AWT.RenderingHints class defines many ways to display two-dimensional graphics, including alpha_interpolation, jitter, and antialiasing methods. RenderingHints helps to determine how the graphic is displayed and how the graphics reaches the optimal process. Transfer from: Dynamic Network Production Guide WWW.KNOWSKY.com In order to smoothing, you can use the Antialiasing method to handle pieces of graphics. AntialiaSing is a graphic smooth processing method. Its algorithm is to select a color value of a special pixel and replace the intersection of the intersection, so that the line cross is smoothed. Figure A illustrates the effect of the Antialiasing method. It can be seen that the cross-shaped lines of the cake graphics using the Antialiasing method becomes very smooth. Figure A
Can also establish a RenderingHints object, and transferred to Graphics2D setRenderingHints () method, as follows: RenderingHints renderHints = new RenderingHints (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHints (renderHints); making adjustable boundary The cake pattern in Figure A has a side, how can I change the size of the boundary? Int border = 10 can be defined first, then calculate the size of the area in the boundary: Ellipse2d.double ELB = New Ellipse2d.double (x_pie - border / 2, y_pie - border / 2, piewidth border, Pieheight border); x_pie The value of Y_PIE represents the upper left corner of the square surrounded by the square graphic. We take half of the border / 2 to get the center of the cake graphic through the boundary area. The Fillarc () method inherited from the Java.awt.Graphics class provides a simple method of various parts (or arcs) of the cake pattern: g2d.fillarc (x_position, y_position, width, height, Startangle, Sweepangle; X_Position, and y_position integer represents the X, Y coordinates of the upper left corners of the arc to be filled, and Width and Heigh integers represent their specific size. If Width and Height are equal, the cake graphic will be a circle. If width and height are not equal, the piematic graphic will be an ellipse. The Fillarc () method determines the size of the arc based on the Sweepangle integer value. If the SWEEPANGLE value is positive, the arc is drawn in the reverse clockwise direction, so that it is clockwise. Draw the arc first, use the PIECOLOR object's getPieColor () method to get the color of the nearest cake arc and give it the current arc :: g2d.setcolor (pc.getpiecolor ()); then, through constant loop Sales [] array and makes it accumulating to get a total of sales: salestotal = sales [i]; use a total sales volume, you can calculate the percentage of each product sales accounted for a total of total sales: float perc = (Sales [I] / Salestotal); We calculate Sweepangle to allocate each part of the arc: int SWeepangle = (int); After each part of the arc draws, Startangle can increment according to the current Sweepangle. This ensures that the current circular arc portion is the above arc as the beginning, thereby establishing a complete cake pattern. The display icon provides the simplest way of displaying the individual parts of the cake graphics. The size of an icon should correspond to the occupancy of the pie in the cake. Figure B shows a complete cake pattern and its icon corresponding to each part, including product name, sales of sales, and various parts. Figure B
Summary This article tells how to use JSP to draw pieces of cake graphics and algorithms, these methods and algorithms are simple and practical, and developers can fully utilize these methods.