Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
Getting started with Java on the Raspberry Pi
this week in the
Raspberry Pi
forum!
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Ron McLeod
Paul Clapham
Tim Cooke
Jeanne Boyarsky
Sheriffs:
Rob Spoor
Devaka Cooray
Liutauras Vilda
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Tim Moores
Mikalai Zaikin
Bartenders:
Piet Souris
Forum:
Swing / AWT / SWT
pie chart
hari thatikonda
Greenhorn
Posts: 11
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
any one can tell me how to draw a pie chart
Eddie Vanda
Ranch Hand
Posts: 283
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
http://javaalmanac.com/egs/java.awt/Pie.html?l=rel
The nice thing about Standards is that there are so many to choose from!
Craig Wood
Ranch Hand
Posts: 1535
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.awt.*; import java.awt.event.*; import java.awt.font.*; import java.awt.geom.*; import java.awt.image.BufferedImage; import java.text.NumberFormat; import javax.swing.*; public class PieChart { public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new PieChartPanel()); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } } class PieChartPanel extends JPanel { BufferedImage image; final int PAD = 30; Font font; NumberFormat nf; boolean showConstructionMarkers; public PieChartPanel() { font = new Font("lucida sans regular", Font.PLAIN, 20); nf = NumberFormat.getPercentInstance(); showConstructionMarkers = true; addMouseListener(new Visibility(this)); addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent e) { image = null; } }); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); if(image == null) createChartImage(); g2.drawImage(image, 0, 0, this); } private void createChartImage() { int[] data = { 320, 490, 100, 612 }; int w = getWidth(); int h = getHeight(); int cx = w/2; int cy = h/2; image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setPaint(Color.white); g2.fillRect(0, 0, w, h); g2.setPaint(Color.black); // draw circle int dia = Math.min(w,h) - 2*PAD; g2.draw(new Ellipse2D.Double(cx - dia/2, cy - dia/2, dia, dia)); double total = 0; for(int j = 0; j < data.length; j++) total += data[j]; double theta = 0, phi = 0; double x, y; for(int j = 0; j < data.length; j++) { // draw leading edge of pie slice x = cx + (dia/2) * Math.cos(theta); y = cy + (dia/2) * Math.sin(theta); g2.draw(new Line2D.Double(cx, cy, x, y)); // angle of pie slice phi = (data[j]/total) * 2 * Math.PI; if(showConstructionMarkers) { // draw centerline of slice g2.setXORMode(Color.cyan); x = cx + (dia/2) * Math.cos(theta + phi/2); y = cy + (dia/2) * Math.sin(theta + phi/2); g2.draw(new Line2D.Double(cx, cy, x, y)); g2.setPaintMode(); } // mark text center on centerline x = cx + (9*dia/24) * Math.cos(theta + phi/2); y = cy + (9*dia/24) * Math.sin(theta + phi/2); if(showConstructionMarkers) { g2.setPaint(Color.blue); g2.fill(new Ellipse2D.Double(x - 2, y - 2, 4, 4)); g2.setPaint(Color.black); } // draw text for data value inside pie slice g2.setFont(font); String s = String.valueOf(data[j]); FontRenderContext frc = g2.getFontRenderContext(); float textWidth = (float)font.getStringBounds(s, frc).getWidth(); LineMetrics lm = font.getLineMetrics(s, frc); float sx = (float)(x - textWidth/2); float sy = (float)(y + lm.getAscent()/2); g2.drawString(s, sx, sy); // locate text center for outer, percent values x = cx + (dia/2 + 4*PAD/5) * Math.cos(theta + phi/2); y = cy + (dia/2 + 4*PAD/5) * Math.sin(theta+ phi/2); if(showConstructionMarkers) { g2.setPaint(Color.red); g2.fill(new Ellipse2D.Double(x - 2, y - 2, 4, 4)); g2.setPaint(Color.black); } // show percent values on outside of circle s = nf.format(data[j]/total); textWidth = (float)font.getStringBounds(s, frc).getWidth(); lm = font.getLineMetrics(s, frc); sx = (float)(x - textWidth/2); sy = (float)(y + lm.getAscent()/2); g2.drawString(s, sx, sy); theta += phi; } g2.dispose(); } public void toggleVisibility() { showConstructionMarkers = !showConstructionMarkers; image = null; repaint(); } } /** * double-click PieChartPanel to reveal/hide construction lines */ class Visibility extends MouseAdapter { PieChartPanel pie; public Visibility(PieChartPanel pcp) { pie = pcp; } public void mousePressed(MouseEvent e) { if(e.getClickCount() > 1) pie.toggleVisibility(); } }
He baked a muffin that stole my car! And this tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Changing color for the parts of piechart
Graphs support in Java.
Graphs support in Java.
get a region of a pie chart
Generating line/pie/column /bar charts
More...