Can someone help me on how to draw a dynamic pie chart in java?
I don't want to use jfreechart. I want to use jtable or other methods if available(but simple one)
I have one but it is static.i need to write it upon user input.how can i do it?

private Font font; 

   public PieChart ()
   {
      super( "Computer" );

      setSize( 600, 400 );
      setLocation( 70, 70 ) ;
      show();
   }

   public void paint( Graphics g )
   {

      font = new Font("Sanserif", Font.BOLD, 14);
      // start at 0 and sweep 360 degrees

      g.setColor( Color.black );

      g.fillArc( 110, 80, 300, 300, 0, 36 );
      g.drawString("Hand Tool : 10%", 420, 80);

      g.setColor( Color.red );
      g.fillArc( 110, 80, 300, 300, 36, 120 );
      g.drawString("Power : 33%", 420, 100);

      g.setColor( Color.blue );
      g.fillArc( 110, 80, 300, 300, 156, 80 );
      g.drawString("Lawn Mower Sales: 22%", 420, 120);

      g.setColor( Color.green );
      g.fillArc( 110, 80, 300, 300, 236, 80 );
      g.drawString("Bench Tools Sales: 22%", 420, 140);

      g.setColor( Color.white );
      g.fillArc( 110, 80, 300, 300, 316, 44 );
      g.drawString("Tool Accessories Sales: 12%", 420, 160);

      g.setColor( Color.black );
      g.drawArc( 110, 80, 300, 300, 0, 360 );
   }

   public static void main( String args[] )
   {
      PieChart app = new PieChart ();

      app.addWindowListener(
         new WindowAdapter() {
            public void windowClosing( WindowEvent e )
            {
               System.exit( 0 );
            }
         }
      );
   }
}

Recommended Answers

your paint method seems prety static to me, where is the dynamic part?
are this values coming somewhere?, also if you are overriding the paint method you will spend some hard time trying to do it dynamically.

Instead I found this nice example from Rose India:

Jump to Post

If you read that example you will see where it declares an array of numbers that are the data for the pie chart. Replace that with whatever code you want to get your dynamic data from whereever.

Jump to Post

All 7 Replies

your paint method seems prety static to me, where is the dynamic part?
are this values coming somewhere?, also if you are overriding the paint method you will spend some hard time trying to do it dynamically.

Instead I found this nice example from Rose India:
http://www.roseindia.net/java/example/java/swing/draw-pie-chart.shtml

hope it helps

yes i have not done it dynamic.i dont know how to do it. but the one from rose india also is static!

If you read that example you will see where it declares an array of numbers that are the data for the pie chart. Replace that with whatever code you want to get your dynamic data from whereever.

it's too complicate!

Which part are you having problems with?

I want to modify my code to make it dynamic is it possible? the one from rose india
I cant understand the following:

for(int j = 0; j < marks.length; j++)
        {
            p = cp + (pie/2) * Math.cos(theta);
            q = cq + (pie/2) * Math.sin(theta);
            g2.draw(new Line2D.Double(cp, cq, p, q));
            phi = (marks[j]/total) * 2 * Math.PI;
            p = cp + (9*pie/24) * Math.cos(theta + phi/2);
            q = cq + (9*pie/24) * Math.sin(theta + phi/2);
            g2.setFont(font);
            String st = String.valueOf(marks[j]);
            FontRenderContext frc = g2.getFontRenderContext();
            float textWidth = (float)font.getStringBounds(st, frc).getWidth();
            LineMetrics lm = font.getLineMetrics(st, frc);
            float sp = (float)(p - textWidth/2);
            float sq = (float)(q + lm.getAscent()/2);
            g2.drawString(st, sp, sq);
            p = cp + (pie/2 + 4*PAD/5) * Math.cos(theta + phi/2);
            q = cq + (pie/2 + 4*PAD/5) * Math.sin(theta+ phi/2);
            st = numberFormat.format(marks[j]/total);
            textWidth = (float)font.getStringBounds(st, frc).getWidth();
            lm = font.getLineMetrics(st, frc);
            sp = (float)(p - textWidth/2);
            sq = (float)(q + lm.getAscent()/2);
            g2.drawString(st, sp, sq);
            theta += phi;
        }
        g2.dispose();
    }

Typical roseIndia - it's not a source I'd recommend to anyone. However if you bother to read the explanatory text, rather than just copy the code, you will see some explanation for this particular code. All thiscode is to do with displaying the numerical percentages nicely laid out in the right place on the pie chart.
It's anyone's guess as to why all the drawing is to g2, which is then disposed of as soon as the drawing is finished.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.