Dynamically lablling arc2d(pie chart)

Can somebody help me .I want to lable a arc2d (pie chart)dynamically.Can somebody guide me how to do this as I am new to java2d.import java.io.*;

import java.util.*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import java.awt.image.*;

import java.util.Random;

import java.sql.*;

import java.math.BigDecimal;

publicclass pie2dextends JPanel{

Connection con;

Statement stmt;

Vector vc,conVector;

int arraincrement,sum,pieMinusAngle,IntegerToInt;

double sumAngle,allAngleSum;

publicvoid pie(Graphics g){

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String connection_string="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=D:\\bhanu\\bhanu.mdb;DriverID=22";

//String connection_string="jdbc:odbc:bhanu";

con=DriverManager.getConnection(connection_string);

stmt=con.createStatement();

System.out.println("connected to contractor database");

}

catch(Exception er){

System.out.println("could not connect to database");

}

vc=new Vector();

conVector=new Vector();

try{

ResultSet res=stmt.executeQuery("select contractor,count(contractor)as totalcon from daily group by contractor");

System.out.println("Result set executed");

while(res.next()){

String tovector=res.getString("totalcon");

String contractor=res.getString("contractor");

conVector.add(contractor);

int stringToInt=Integer.parseInt(tovector);

vc.add(stringToInt);

}

}

catch(Exception e){

System.out.println("Result set error"+e);

}

//Collections.sort(vc);

System.out.println("vc is now :"+vc);

Graphics2D g2d=(Graphics2D)g;

Random generator =new Random();

g2d.setFont(new Font("Arial",Font.BOLD,15));

int vectorsize=vc.size();

for(int i=0;i<vectorsize;i++){

Object getInt=vc.elementAt(i);

Integer objToInt=(Integer)getInt;

int IntegerToInt=objToInt.intValue();

sum=sum+IntegerToInt;

}

int pieAngle=360;

for(int i=0;i<vectorsize;i++){

System.out.println("sum is now :"+sum);

Object getInt=vc.elementAt(i);

Integer objToInt=(Integer)getInt;

IntegerToInt=objToInt.intValue();

//System.out.println("IntegerToInt is now :"+IntegerToInt);

double DivideAngle=(double)((360/sum)*IntegerToInt);

//System.out.println("DivideAngle is now :"+DivideAngle);

allAngleSum=allAngleSum+DivideAngle;

}

if(allAngleSum><360){

double sumCalculate=(360-allAngleSum)/vectorsize;

for(int i=0;i<vectorsize;i++){

System.out.println("sum is now :"+sum);

Object getInt=vc.elementAt(i);

Integer objToInt=(Integer)getInt;

IntegerToInt=objToInt.intValue();

double DivideAngle=(double)((360/sum)*IntegerToInt)+sumCalculate;

//int calulate=DivideAngle*IntegerToInt;

Object contractorObj=conVector.elementAt(i);

String contractor=(String)contractorObj;

//System.out.println("sumAngle is now :"+sumAngle);

int Rnewcolor=generator.nextInt(255);

int Gnewcolor=generator.nextInt(255);

int Bnewcolor=generator.nextInt(255);

g2d.setPaint(new Color(Rnewcolor,Gnewcolor,Bnewcolor));

g2d.fill(new Arc2D.Double( 240, 200, 400, 400, sumAngle,DivideAngle, Arc2D.PIE ) );

sumAngle=sumAngle+DivideAngle;

String intToString=Integer.toString(IntegerToInt);

System.out.println("DivideAngle is now :"+DivideAngle);

if(i><7){

g2d.fill(new Rectangle2D.Double(i*70,50,30,30));

//g2d.fill(new Rectangle2D.Double(700,i*10,30,30));

g2d.drawString(intToString,i*73,100);

g2d.drawString(contractor,i*73,120);//top horizontal

//g2d.drawString(intToString,760,i*82);

}

else{

g2d.fill(new Rectangle2D.Double(20,i*40,30,30));

g2d.drawString(intToString,55,i*42);

g2d.drawString(contractor,75,i*42);

}

}

}

if(allAngleSum>360){

double sumCalculate=(allAngleSum-360)/vectorsize;

for(int i=0;i<vectorsize;i++){

System.out.println("sum is now :"+sum);

Object getInt=vc.elementAt(i);

Integer objToInt=(Integer)getInt;

IntegerToInt=objToInt.intValue();

double DivideAngle=(double)((360/sum)*IntegerToInt)-sumCalculate;

//int calulate=DivideAngle*IntegerToInt;

Object contractorObj=conVector.elementAt(i);

String contractor=(String)contractorObj;

//System.out.println("sumAngle is now :"+sumAngle);

int Rnewcolor=generator.nextInt(255);

int Gnewcolor=generator.nextInt(255);

int Bnewcolor=generator.nextInt(255);

g2d.setPaint(new Color(Rnewcolor,Gnewcolor,Bnewcolor));

g2d.fill(new Arc2D.Double( 240, 200, 400, 400, sumAngle,DivideAngle, Arc2D.PIE ) );

sumAngle=sumAngle+DivideAngle;

String intToString=Integer.toString(IntegerToInt);

System.out.println("DivideAngle is now :"+DivideAngle);

if(i><7){

g2d.fill(new Rectangle2D.Double(i*70,50,30,30));

//g2d.fill(new Rectangle2D.Double(700,i*10,30,30));

g2d.drawString(intToString,i*73,100);

g2d.drawString(contractor,i*73,120);//top horizontal

//g2d.drawString(intToString,760,i*82);

}

else{

g2d.fill(new Rectangle2D.Double(20,i*40,30,30));

g2d.drawString(intToString,55,i*42);

g2d.drawString(contractor,75,i*42);

}

}

}

//g2d.fill(new Ellipse2D.Double(300,150,400,400));

//g2d.setColor(Color.red);

//g2d.fill(new Ellipse2D.Double(300,150,10,400));

//g2d.setColor(Color.yellow);

//g2d.fill(new Arc2D.Double( 240, 200, 400, 400, 0, 270, Arc2D.PIE ) );

sum=0;

sumAngle=0;

allAngleSum=0;

try{

stmt.close();

con.close();

}

catch(Exception e){

System.out.println("Could not close connections"+e);

}

}

publicvoid paintComponent(Graphics g){

super.paintComponent(g);

pie(g);

}

}

[10604 byte] By [sushilsingh27a] at [2007-10-2 6:03:33]
# 1

This works okay now. You can run it as-is to see.

import java.awt.*;

import java.awt.event.*;

import java.awt.font.*;

import java.awt.geom.*;

//import java.awt.image.*;

import java.io.*;

//import java.math.BigDecimal;

import java.sql.*;

import java.util.*;

import javax.swing.*;

public class P2D extends JPanel{

Connection con;

Statement stmt;

Vector vc, conVector;

int arraincrement, sum, pieMinusAngle, IntegerToInt;

double sumAngle, allAngleSum;

Random generator;

Font font;

final int

X= 240,

Y= 200,

DIA= 400,

MARGIN =4;

public P2D() {

// do these things one time only

collectData();

processData();

generator = new Random();

font = new Font("Arial",Font.BOLD,15);

}

public void collectData(){

int[] data = { 75, 32, 90, 64 };

vc = new Vector();

for(int j = 0; j < data.length; j++)

vc.add(new Integer(data[j]));

/*

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String connection_string="jdbc:odbc:Driver={Microsoft Access Driver " +

"(*.mdb)};Dbq=D:\\bhanu\\bhanu.mdb;DriverID=22";

//String connection_string="jdbc:odbc:bhanu";

con=DriverManager.getConnection(connection_string);

stmt=con.createStatement();

System.out.println("connected to contractor database");

} catch(Exception er) {

System.out.println("could not connect to database");

}

vc=new Vector();

conVector=new Vector();

try{

ResultSet res=stmt.executeQuery("select contractor,count(contractor)as " +

"totalcon from daily group by contractor");

System.out.println("Result set executed");

while(res.next()){

String tovector=res.getString("totalcon");

String contractor=res.getString("contractor");

conVector.add(contractor);

int stringToInt=Integer.parseInt(tovector);

vc.add(stringToInt);

}

} catch(Exception e) {

System.out.println("Result set error"+e);

}

//Collections.sort(vc);

System.out.println("vc is now :"+vc);

try{

stmt.close();

con.close();

} catch(Exception e) {

System.out.println("Could not close connections"+e);

}

*/

}

private void processData() {

int vectorsize=vc.size();

for(int i=0;i<vectorsize;i++){

Object getInt=vc.elementAt(i);

Integer objToInt=(Integer)getInt;

int IntegerToInt=objToInt.intValue();

sum=sum+IntegerToInt;

}

// int pieAngle=360;

// all angles must add up to 360 degrees by definition

for(int i=0;i<vectorsize;i++){

System.out.println("sum is now :"+sum);

Object getInt=vc.elementAt(i);

Integer objToInt=(Integer)getInt;

IntegerToInt=objToInt.intValue();

//System.out.println("IntegerToInt is now :"+IntegerToInt);

// integer division rounds down

double DivideAngle=//(double)((360/sum)*IntegerToInt);

(360.0 / sum) * IntegerToInt;

//System.out.println("DivideAngle is now :"+DivideAngle);

allAngleSum=allAngleSum+DivideAngle;

}

}

public void drawPie(Graphics g){

Graphics2D g2d=(Graphics2D)g;

g2d.setFont(font);

int vectorsize=vc.size();

double sumAngle = 0.0;

double sumCalculate = 0.0;

if(allAngleSum >< 360)

sumCalculate=(360-allAngleSum)/vectorsize;

else if(allAngleSum > 360)

sumCalculate=(allAngleSum-360)/vectorsize;

for(int i=0;i<vectorsize;i++){

//System.out.println("sum is now :"+sum);

Object getInt=vc.elementAt(i);

Integer objToInt=(Integer)getInt;

IntegerToInt=objToInt.intValue();

// 360 / 261 = 1

double DivideAngle=//(double)((360/sum)*IntegerToInt)+sumCalculate;

((360.0/sum) * IntegerToInt) + sumCalculate;

//int calulate=DivideAngle*IntegerToInt;

//Object contractorObj=conVector.elementAt(i);

//String contractor=(String)contractorObj;

//System.out.println("sumAngle is now :"+sumAngle);

g2d.setPaint(getColor());

g2d.fill(new Arc2D.Double( 240, 200, 400, 400, sumAngle,

DivideAngle, Arc2D.PIE ) );

sumAngle=sumAngle+DivideAngle;

String intToString=Integer.toString(IntegerToInt);

System.out.println("DivideAngle is now :"+DivideAngle);

if(i >< 7){

g2d.fill(new Rectangle2D.Double(i*70,50,30,30));

//g2d.fill(new Rectangle2D.Double(700,i*10,30,30));

g2d.drawString(intToString,i*73,100);

//g2d.drawString(contractor,i*73,120);//top horizontal

//g2d.drawString(intToString,760,i*82);

} else {

g2d.fill(new Rectangle2D.Double(20,i*40,30,30));

g2d.drawString(intToString,55,i*42);

//g2d.drawString(contractor,75,i*42);

}

}

//sum=0;

//sumAngle=0;

//allAngleSum=0; results in division by zero on next paint call

}

private void drawLabels(Graphics g) {

Graphics2D g2d=(Graphics2D)g;

g2d.setFont(font);

int vectorsize=vc.size();

g2d.setPaint(Color.red);

FontRenderContext frc = g2d.getFontRenderContext();

font = font.deriveFont(24f);

g2d.setFont(font);

double theta = 0.0;

int cx = X + DIA/2;

int cy = Y + DIA/2;

g2d.setPaint(Color.red);

for(int j = 0; j < vectorsize; j++) {

double value = ((Integer)vc.elementAt(j)).doubleValue();

// java counts angles increasing clockwise starting at 3 o'clock

// minus theta goes anti-clockwise which we need

// to match the counting/direction of Arc2D

theta -= (value / sum) * 2*Math.PI;

//System.out.println("value = " + value + "\t" +

//"theta = " + Math.toDegrees(theta));

String s = String.valueOf(value);

float width = (float)font.getStringBounds(s, frc).getWidth();

float height = font.getLineMetrics(s, frc).getAscent();

double diag = Math.sqrt(Math.pow((width/2)*Math.cos(theta), 2) +

Math.pow((height/2)*Math.sin(theta), 2));

float scx = (float)(cx + (DIA/2 + diag + MARGIN) * Math.cos(theta));

float scy = (float)(cy + (DIA/2 + diag + MARGIN) * Math.sin(theta));

float sx = scx - width/2;

float sy = scy + height/2;

g2d.drawString(s, sx, sy);

}

}

private Color getColor() {

int Rnewcolor=generator.nextInt(255);

int Gnewcolor=generator.nextInt(255);

int Bnewcolor=generator.nextInt(255);

return new Color(Rnewcolor,Gnewcolor,Bnewcolor);}

public void paintComponent(Graphics g){

super.paintComponent(g);

drawPie(g);

drawLabels(g);

}

public static void main(String[] args) {

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(new P2D());

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

}

74philipa at 2007-7-16 13:04:00 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks 74philip for your guidence and full code examples regarding dynamically labeling ,its working nice.Once again thanks very much
sushilsingh27a at 2007-7-16 13:04:00 > top of Java-index,Desktop,Core GUI APIs...