Advertisement
Guest User

3 verschillende pie chart dingen

a guest
Apr 1st, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.49 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.GradientPaint;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.RenderingHints;
  6. import java.awt.geom.Arc2D;
  7. import java.awt.geom.Ellipse2D;
  8. import java.awt.geom.Point2D;
  9. import java.util.ArrayList;
  10.  
  11. import javax.swing.JPanel;
  12.  
  13. public class PieChart extends JPanel {
  14.  
  15.     enum Type {
  16.         STANDARD, SIMPLE_INDICATOR, GRADED_INDICATOR
  17.     }
  18.  
  19.     private Type type = null; //the type of pie chart
  20.  
  21.         private ArrayList<Double> values;
  22.         private ArrayList<Color> colors;
  23.  
  24.         private ArrayList<Double> gradingValues;
  25.         private ArrayList<Color> gradingColors;
  26.  
  27.     double percent = 0; //percent is used for simple indicator and graded indicator
  28.  
  29.     public PieChart(int percent) {
  30.  
  31.         type = Type.SIMPLE_INDICATOR;
  32.         this.percent = percent;
  33.     }
  34.  
  35.     public PieChart(ArrayList values, ArrayList colors) {
  36.  
  37.         type = Type.STANDARD;
  38.  
  39.         this.values = values;
  40.         this.colors = colors;
  41.     }
  42.  
  43.     public PieChart(int percent, ArrayList gradingValues, ArrayList gradingColors) {
  44.         type = Type.GRADED_INDICATOR;
  45.  
  46.         this.gradingValues = gradingValues;
  47.         this.gradingColors = gradingColors;
  48.         this.percent = percent;
  49.  
  50.     }
  51.  
  52.     @Override
  53.     protected void paintComponent(Graphics g) {
  54.  
  55.         int width = getSize().width;
  56.  
  57.         Graphics2D g2d = (Graphics2D) g;
  58.         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Anti-alias!
  59.                 RenderingHints.VALUE_ANTIALIAS_ON);
  60.  
  61.         if (type == Type.SIMPLE_INDICATOR) {
  62.  
  63.             //colours used for simple indicator
  64.             Color backgroundColor = Color.WHITE;
  65.             Color mainColor = Color.BLUE;
  66.  
  67.             g2d.setColor(backgroundColor);
  68.             g2d.fillOval(0, 0, width, width);
  69.             g2d.setColor(mainColor);
  70.             Double angle = (percent / 100) * 360;
  71.             g2d.fillArc(0, 0, width, width, -270, -angle.intValue());
  72.  
  73.         } else if (type == Type.STANDARD) {
  74.  
  75.             int lastPoint = -270;
  76.  
  77.             for (int i = 0; i < values.size(); i++) {
  78.                 g2d.setColor(colors.get(i));
  79.  
  80.                 Double val = values.get(i);
  81.                 Double angle = (val / 100) * 360;
  82.  
  83.                 g2d.fillArc(0, 0, width, width, lastPoint, -angle.intValue());
  84.                 System.out.println("fill arc " + lastPoint + " "
  85.                         + -angle.intValue());
  86.  
  87.                 lastPoint = lastPoint + -angle.intValue();
  88.             }
  89.         } else if (type == Type.GRADED_INDICATOR) {
  90.  
  91.             int lastPoint = -270;
  92.  
  93.             double gradingAccum = 0;
  94.  
  95.             for (int i = 0; i < gradingValues.size(); i++) {                g2d.setColor(gradingColors.get(i));                                 Double val = gradingValues.get(i);              gradingAccum = gradingAccum + val;                              Double angle = null;                                /**                  * If the sum of the gradings is greater than the percent, then we want to recalculate               * the last wedge, and break out of drawing.                 */                                 if (gradingAccum > percent) {
  96.  
  97.                     System.out.println("gradingAccum > percent");
  98.  
  99.                     //get the previous accumulated segments. Segments minus last one
  100.                     double gradingAccumMinusOneSegment = gradingAccum - val;
  101.  
  102.                     //make an adjusted calculation of the last wedge
  103.                     angle = ((percent - gradingAccumMinusOneSegment) / 100) * 360;
  104.  
  105.                     g2d.fillArc(0, 0, width, width, lastPoint, -angle.intValue());
  106.  
  107.                     lastPoint = lastPoint + -angle.intValue();
  108.  
  109.                     break;
  110.  
  111.                 }else {
  112.  
  113.                     System.out.println("normal");
  114.                     angle = (val / 100) * 360;
  115.  
  116.                     g2d.fillArc(0, 0, width, width, lastPoint, -angle.intValue());
  117.  
  118.                     System.out.println("fill arc " + lastPoint + " "
  119.                             + -angle.intValue());
  120.  
  121.                     lastPoint = lastPoint + -angle.intValue();
  122.                 }
  123.             }
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement