java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow javax.swing arrow How to implement charts such as pie chart
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4091
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to implement charts such as pie chart E-mail
User Rating: / 57
PoorBest 

This Java tip illustrates a method of implementing a pie chart. The chart is implemented with the help of ChartPainter class. User may use this in their applications to display graphical view of statistical data.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.*;

public class PieChartPainter extends ChartPainter {
    
    protected static PieChartPainter chartUI = new PieChartPainter();
    protected int originX, originY;
    protected int radius;
    
    private static double piby2 = Math.PI / 2.0;
    private static double twopi = Math.PI * 2.0;
    private static double d2r   = Math.PI / 180.0// Degrees to radians.
    private static int xGap = 5;
    private static int inset = 40;
    
    public int indexOfEntryAt(MouseEvent me) {
        
        int x = me.getX() - originX;
        int y = originY - me.getY();  // Upside-down coordinate system.
        
        // Is (x,y) in the circle?
        if (Math.sqrt(x*x + y*y> radius) { return -1}
        
        double percent = Math.atan2(Math.abs(y), Math.abs(x));
        
        if (x >= 0) {
            if (y <= 0) { // (IV)
                percent = (piby2 - percent* piby2; // (IV)
            }
        else {
            if (y >= 0) { // (II)
                percent = Math.PI - percent;
            else // (III)
                percent = Math.PI + percent;
            }
        }
        
        percent /= twopi;
        double t = 0.0;
        
        if (values != null) {
        
            for (int i = 0; i < values.length; i++) {
                
                if (t + values[i> percent) {
                    return i;
                }
            
                t += values[i];
            }
        }
        
        return -1;
    }
    
    public void paint(Graphics g, JComponent c) {
        
        Dimension size = c.getSize();
        originX = size.width / 2;
        originY = size.height / 2;
        
        int diameter = (originX < originY ? size.width - inset
                : size.height - inset);
        
        radius = (diameter / 21;
        
        int cornerX = (originX - (diameter / 2));
        int cornerY = (originY - (diameter / 2));
        
        int startAngle = 0;
        int arcAngle = 0;
        
        for (int i = 0; i < values.length; i++) {
            arcAngle = (int)(i < values.length - ?
                Math.round(values[i360:
                360 - startAngle);
        
            g.setColor(colors[i % colors.length]);
            g.fillArc(cornerX, cornerY, diameter, diameter,
                    startAngle, arcAngle);
            
            drawLabel(g, labels[i], startAngle + (arcAngle / 2));
            startAngle += arcAngle;
        }
        
        g.setColor(Color.black);
        g.drawOval(cornerX, cornerY, diameter, diameter);  // Cap the circle.
    }
    
    public void drawLabel(Graphics g, String text, double angle) {
        
        g.setFont(textFont);
        g.setColor(textColor);
        double radians = angle * d2r;
        int x = (int) ((radius + xGap* Math.cos(radians));
        int y = (int) ((radius + xGap* Math.sin(radians));
        
        if (x < 0) {
            x -= SwingUtilities.computeStringWidth(g.getFontMetrics(), text);
        }
        
        if (y < 0) {
            y -= g.getFontMetrics().getHeight();
        }
        
        g.drawString(text, x + originX, originY - y);
    }
    
    public static ComponentUI createUI(JComponent c) {
        return chartUI;
    }
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.