Obtain the total sales volume
In most cases, there are many records in the sales list, so it is very important to access the database quickly and efficiently. Now we only need to access the total sales volume of each product in the database.
The getSales () method in Table C connects to the database and returns an array containing the total sales volume of each product.
Listing C
//////////////////////////////////////// //////////////////// // Get the sales totals from the database //////////////////////////////////////// //////////////////// Public float [] getSales (int products) { Float [] arr = new float [products]; Connection con; Statement stmt; ResultSet rs; Int count = 0; 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 Rs = stmt.exe cuteQuery (SQL ); While (rs. next ()) { Int product = rs. getInt ("productID "); // Check that the productID is valid If (product> = 0 & product <products) { // Add to product total Arr [product] + = rs. getFloat ("amount "); Count ++; } } Stmt. close (); Con. close (); } Catch (java. lang. Exception ex) { Arr [0] =-1.0f; } Return arr; } |
After getSales () traverses all records, it only stores the new sales volume of each product:
Int product = rs. getInt ("productID "); Arr [product] + = rs. getFloat ("amount "); |
PieColor object
Each product on the pie chart should be displayed in different colors. To achieve this goal, we create a pieColor object, as shown in table D. This object contains an array of colors:
Color pieColorArray [] = {new Color (60,210, 60, 60), new Color (, 60 )...} |
The pieColor class defines a setNewColor () method, which enables curPieColor and index to increase progressively. At the same time, it can check that the index does not exceed the boundary range, that is, the method used is: if the value of curPieColor is too large, 0 is assigned.
More effectively, after setNewColor () loops each color, execute the following code in the first color:
CurPieColor ++; If (curPieColor> = pieColorArray. length) {CurPieColor = 0 ;} |
RenderingHints and antialiasing classes
The java. awt. RenderingHints class defines many methods to display 2D graphs, including alpha_interpolation, jitter, and antialiasing methods. RenderingHints helps determine how a graph is displayed and how it is best handled.
To achieve smooth display, you can use the antialiasing method to process pie charts. Antialiasing is a smooth processing method for images. The algorithm selects the color value of a special pixel and replaces the pixel at the intersection to smooth the intersection of the line.
Figure A illustrates the effects of the antialiasing method. We can see that the line intersection of the pie chart using the antialiasing method becomes smooth.
Figure
You can also create a RenderingHints object and pass it to Graphics2D setRenderingHints () method, as shown below:
RenderingHints renderHints = new RenderingHints (RenderingHints. KEY_ANTIALIASING, RenderingHints. VALUE_ANTIALIAS_ON ); G2d. setRenderingHints (renderHints ); |