Pie Chart : Chart « Advanced Graphics « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Advanced Graphics » ChartScreenshots 
Pie Chart
Pie Chart

import java.awt.*;
import java.util.*;

import javax.swing.*;

import no.geosoft.cc.geometry.Geometry;
import no.geosoft.cc.graphics.*;



/**
 * G demo program. Demonstrates:
 *
 * <ul>
 * <li>A rudimentary pie chart class
 * <li>Annotation techniques
 * <li>Example geometry generation
 * </ul>
 
 @author <a href="mailto:jacob.dreyer@geosoft.no">Jacob Dreyer</a>
 */   
public class Demo16 extends JFrame
{
  public Demo16()
  {
    super ("G Graphics Library - Demo 16");    
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    
    // Create the graphic canvas
    GWindow window = new GWindow();
    getContentPane().add (window.getCanvas());
    
    // Create scane with default viewport and world extent settings
    GScene scene = new GScene (window);

    GStyle pieStyle = new GStyle();
    pieStyle.setFont (new Font ("Dialog", Font.BOLD, 18));
    pieStyle.setLineStyle (GStyle.LINESTYLE_INVISIBLE);
    pieStyle.setForegroundColor (new Color (255255255));
    
    PieChart pieChart = new PieChart (250250200);
    pieChart.setStyle (pieStyle);

    pieChart.addSector (0.20"Canada", getStyle());
    pieChart.addSector (0.10"USA",    getStyle());
    pieChart.addSector (0.25"Spain",  getStyle());
    pieChart.addSector (0.35"China",  getStyle());
    pieChart.addSector (0.10"Chile",  getStyle());

    scene.add (pieChart);
    
    pack();
    setSize (new Dimension (500500));
    setVisible (true);
  }


  
  private GStyle getStyle()
  {
    GStyle style = new GStyle();
    style.setBackgroundColor (new Color (Color.HSBtoRGB ((float)Math.random()0.4f0.8f)));
    return style;
  }
  
    

  private class PieChart extends GObject
  {
    private int         x0_, y0_;
    private int         radius_;
    private Collection  sectors_;
    
    
    PieChart (int x0, int y0, int radius)
    {
      x0_      = x0;
      y0_      = y0;
      radius_  = radius;
      sectors_ = new ArrayList();
    }


    void addSector (double fraction, String text, GStyle style)
    {
      sectors_.add (new Sector (fraction, text, style));
    }
    
    
    public void draw()
    {
      removeSegments();

      // Loop through the sectors and draw the graphics for each
      double angle0 = 0.0;
      for (Iterator i = sectors_.iterator(); i.hasNext()) {
        Sector sector = (Sectori.next();

        //
        // Geometry for the sector itself
        //
        GSegment segment = new GSegment();
        addSegment (segment);
        segment.setStyle (sector.style);

        double angle1 = angle0 + sector.fraction * Math.PI * 2.0;

        int[] sectorGeometry = Geometry.createSector (x0_, y0_, radius_,
                                                      angle0, angle1);

        segment.setGeometry (sectorGeometry);
        angle0 = angle1;

        //
        // Add annotation. Create an invisible line from the sector center
        // thorugh the center of the arc and out and associated annotation
        // with this line.
        //
        double[] p0 = new double[3];
        double[] p1 = new double[3];

        int nPoints = sectorGeometry.length / 2;
        int pointNo = (nPoints - 22;
        
        p1[0= sectorGeometry[pointNo * 0];
        p1[1= sectorGeometry[pointNo * 1];
        p1[20.0;

        double[] sectorCenter = Geometry.computePointOnLine (x0_, y0_,
                                                             p1[0], p1[1],
                                                             0.5);
        p0[0= sectorCenter[0];
        p0[1= sectorCenter[1];
        p0[20.0;

        // Ensure line extends far out of the sector
        Geometry.extendLine (p0, p1, 1000.0);
        
        GSegment annotationLine = new GSegment();
        addSegment (annotationLine);

        annotationLine.setGeometry ((intp0[0](intp0[1],
                                    (intp1[0](intp1[1]);

        // Add the percentage text
        int percent = (intMath.round (sector.fraction * 100.0);
        GText text = new GText (percent + "%", GPosition.FIRST);
        annotationLine.addText (text);

        // Add the label to the same geometry point as the percentage;
        // It will be adjusted so it doesn't overlap
        text = new GText (sector.label, GPosition.FIRST);
        annotationLine.addText (text);
      }
    }
  }

  

  private class Sector
  {
    public double  fraction;
    public String  label;
    public GStyle  style;

    public Sector (double fraction, String label, GStyle style)
    {
      this.fraction = fraction;
      this.label    = label;
      this.style    = style;
    }
  }
  


  public static void main (String[] args)
  {
    new Demo16();
  }
}

           
       
G-PieChart.zip( 214 k)
Related examples in the same category
1. Chart based on Graph libraryChart based on Graph library
2. Scroll ChartScroll Chart
3. Animation Line ChartAnimation Line Chart
4. A rudimentary chart libraryA rudimentary chart library
5. A rudimentory bar chart classA rudimentory bar chart class
w___w__w__.j_a__v__a___2___s__.c___o___m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.