JAVA APPLET PIE GRAPH (SOURCE CODE)
เช่นกัน นี่คือ source code กราฟวงกลม ขอมอบให้เป็นสาธารณะสมบัติ ทุกทานสามารถ copy ไปแก้ไขเพื่อใช้งานได้ตามใจครับ
---------------------------- SOURCE JAVA --------------------------- import java.awt.*; import java.applet.*; import java.util.*; import java.lang.*; import java.text.*;
public class GraphPie extends Applet { private Double Width ; private Double Height ; private static final double CONVERT_DEGREES_TO_RADIANS = Math.PI / 180 ;
private double cos(int x) { Integer a = new Integer(x); double y = a.doubleValue(); double z = Math.cos(y * CONVERT_DEGREES_TO_RADIANS ); return z; }
private double sin(int x) { Integer a = new Integer(x); double y = a.doubleValue(); double z = Math.sin(y * CONVERT_DEGREES_TO_RADIANS ); return z; }
public void paint(Graphics g) { Color background,colorFont,color; String licence = getParameter("COPYRIGHT") ; int grap = Integer.parseInt(getParameter("GRAP")); // ตัวแปรนี้ใช้กำหนดระยะขอบโดยประมาณ X px. Width = new Double(getParameter("WIDTH")) ; // ความกว้างซึ่งเป็นตัวเดียวกับที่ Applete CODE ใน HTML Height = new Double(getParameter("HEIGHT")); // เช่นเดียวกับความกว้าง แต่เป็นความสูง
int x = grap; int y = grap; int w; if ( Width.intValue() >= Height.intValue() ) { w = Height.intValue() - ( grap * 2 ) ; } else { w = Width.intValue() - ( grap * 2 ) ; } int h = w; int x_c = x + ( w/2); int y_c = x_c; Double r = new Double(w / 2); background = converColor(getParameter("BACKGROUND")); setBackground(background); colorFont = converColor(getParameter("FONT-COLOR"));
int fontSize = Integer.parseInt(getParameter("FONT-SIZE")); Font font = new Font(getParameter("FONT-NAME"),Font.PLAIN,fontSize); background = converColor(getParameter("BACKGROUND")); g.setFont(font);
int num_x = Integer.parseInt(getParameter("NUM-X")); // จำนวน ข้อมูลใน 1 ชุด
StringTokenizer DATA,COLOR; String DATA1 = getParameter("DATA"); String COLOR1 = getParameter("COLOR"); DATA= new StringTokenizer(DATA1,"|"); COLOR= new StringTokenizer(COLOR1,"|"); Double sum = new Double(0); Double [] val ; val = new Double[100] ; for (int i = 0 ; i <= num_x - 1 ; i++) { String yy = DATA.nextToken(); val[i] = new Double(yy); sum = new Double( sum.doubleValue() + val[i].doubleValue() ); }
Double idx = new Double(0); Double pre = new Double(0); int t; Double diff = new Double(0); NumberFormat form; Locale[] locales = NumberFormat.getAvailableLocales(); form = NumberFormat.getInstance(locales[0]); for (int i = 0 ; i <= num_x -1 ; i++) { String zz = COLOR.nextToken(); color = converColor(zz); g.setColor(color); Double v = new Double(val[i].doubleValue()); //Double v1 = new Double(val[i].doubleValue()); v = new Double( v.doubleValue() * 360 / sum.doubleValue()) ; diff = new Double( idx.intValue() - pre.intValue() ); g.fillArc(x,y,w,h,pre.intValue(),v.intValue()+diff.intValue()); t = ( pre.intValue() + (v.intValue()/2)); Double mm = cal_x(r,t,x_c); Double nn = cal_y(r,t,y_c); g.drawString( form.format(v.doubleValue() * 100 / 360 )+"%", mm.intValue(),nn.intValue()); pre = new Double( idx.intValue() + v.intValue() ); idx = new Double( idx.doubleValue() + v.doubleValue()); } if ( pre.doubleValue() < 360 ) { diff = new Double( 360 - pre.doubleValue() ); g.fillArc(x,y,w,h,pre.intValue(),diff.intValue()); } }
private Double cal_x(Double r,int x,int center) { Double mm; int x_grap; x_grap = 40; if ( x >= 270 && x < 360 ) { x = x - 270; mm = new Double( r.doubleValue() * sin(x) ) ; mm = new Double(center + mm.intValue()); mm = new Double(mm.intValue() + (x_grap/4)); } else if ( x >= 180 && x < 270 ) { x = x - 180; mm = new Double( r.doubleValue() * cos(x) ) ; mm = new Double(center - mm.intValue()); mm = new Double(mm.intValue() - x_grap); } else if ( x >= 90 && x < 180 ) { x = x - 90 ; mm = new Double( r.doubleValue() * sin(x) ) ; mm = new Double(center - mm.intValue()); mm = new Double(mm.intValue() - x_grap); } else { mm = new Double( r.doubleValue() * cos(x) ) ; mm = new Double(center + mm.intValue()); mm = new Double(mm.intValue() + (x_grap/4)); } return mm; }
private Double cal_y(Double r,int x,int center) { Double nn; int x_grap; x_grap = 10; if ( x >= 270 && x < 360 ) { x = 270 - x; nn = new Double( r.doubleValue() * cos(x) ) ; nn = new Double(center + nn.intValue()); nn = new Double( nn.intValue() + x_grap); } else if ( x >= 180 && x < 270 ) { x = x - 180; nn = new Double( r.doubleValue() * sin(x) ) ; nn = new Double(center + nn.intValue()); nn = new Double(nn.intValue() + x_grap); } else if ( x >= 90 && x < 180 ) { x = x - 90 ; nn = new Double( r.doubleValue() * cos(x) ) ; nn = new Double(center - nn.intValue()); nn = new Double(nn.intValue() - x_grap); } else { nn = new Double( r.doubleValue() * sin(x) ) ; nn = new Double(center - nn.intValue()); nn = new Double(nn.intValue() - x_grap); } return nn; }
private Color converColor(String strIn) { Color ret; ret = new Color(0,0,255); // chk reserve word strIn = strIn.toLowerCase(); if ( strIn.equals("blue") ) { ret = new Color(0,0,255); } else if ( strIn.equals("red") ) { ret = new Color(255,0,0); } else if ( strIn.equals("green") ) { ret = new Color(0,255,0); } else if ( strIn.equals("tomato") ) { int num1 = hex2oct("FF"); int num2 = hex2oct("63"); int num3 = hex2oct("47"); ret = new Color(num1,num2,num3); } else if ( strIn.equals("yellow") ) { int num1 = hex2oct("FF"); int num2 = hex2oct("FF"); int num3 = hex2oct("00"); ret = new Color(num1,num2,num3); } else if ( strIn.equals("navy") ) { int num1 = hex2oct("00"); int num2 = hex2oct("00"); int num3 = hex2oct("80"); ret = new Color(num1,num2,num3); } else if ( strIn.equals("black") ) { int num1 = hex2oct("00"); int num2 = hex2oct("00"); int num3 = hex2oct("00"); ret = new Color(num1,num2,num3); } else if ( strIn.equals("white") ) { int num1 = hex2oct("ff"); int num2 = hex2oct("ff"); int num3 = hex2oct("ff"); ret = new Color(num1,num2,num3); } else { // chk format #aabbcc String tmp = strIn.substring(0,1); if ( tmp.equals("#" ) && strIn.length() == 7 ) { tmp = strIn.substring(1,3); int num1 = hex2oct(tmp); tmp = strIn.substring(3,5); int num2 = hex2oct(tmp); tmp = strIn.substring(5,7); int num3 = hex2oct(tmp); ret = new Color(num1,num2,num3); } else { // ถ้า error return blue color ret = new Color(0,0,255); } } return ret; } private int hex2oct(String strIn) { int ret = 0; int d,j; int sum = 0; for ( int i = 0 ; i < strIn.length() ; i++ ) { String tmp = strIn.substring(i,i+1) ; tmp = tmp.toUpperCase(); if ( tmp.equals("A") ) { d = 10; } else if ( tmp.equals("B") ) { d = 11; } else if ( tmp.equals("C") ) { d = 12; } else if ( tmp.equals("D") ) { d = 13; } else if ( tmp.equals("E") ) { d = 14; } else if ( tmp.equals("F") ) { d = 15; } else { if ( tmp.compareTo("0") >= 0 && tmp.compareTo("9") <= 0 ) { d = Integer.parseInt(tmp); } else { d = 0; } } j = ( strIn.length() - i - 1) ; Double x = new Double(Math.pow(16,j)); sum = sum + ( d * x.intValue()) ; ret = sum; } return ret; } } -------------------- END PROGRAM JAVA ------------------------- ------------------ SOURCE HTML ---------------------------------- <HTML> <BODY>
<APPLET code="GraphPie.class" WIDTH="200" HEIGHT="200">
<PARAM NAME="SHOWVALUE" VALUE="1"/> <PARAM NAME="GRAP" VALUE="20"/> <PARAM NAME="BACKGROUND" VALUE="#DCF0FF" /> <PARAM NAME="NUM-X" VALUE="3" /> <PARAM NAME="DATA" VALUE="30|50|20" /> <PARAM NAME="COLOR" VALUE="BLUE|GREEN|RED" /> <PARAM NAME="FONT-NAME" VALUE="MS Sans Serif" /> <PARAM NAME="FONT-SIZE" VALUE="12" /> <PARAM NAME="FONT-COLOR" VALUE="BLUE" /> <PARAM NAME="COPYRIGHT" VALUE="www.pcd.co.th" />
</APPLET></TD>
</BODY> </HTML>
Create Date : 02 เมษายน 2554 |
|
2 comments |
Last Update : 2 เมษายน 2554 9:50:37 น. |
Counter : 1411 Pageviews. |
|
|
|
๑ ช่างกลึงพึ่งช่างชักเป็นหลักก่อ
เป็นช่างหล่อพึ่งช่างหุ่นคอยจุนเสริม
ช่างสลักพึ่งช่างเขียนคอยเจียนเจิม
ช่างติเติมไม่ต้องหมายพึ่งใครเลย
(อยู่ในบทที่ 9 ใน 100 บทครับ)
อีกบทหนึ่งก็คือ
๑ โง่ไม่เป็นเป็นใหญ่ยากฝากให้คิด
ทางชีวิตจะรุ่งโรจน์โสตถิผล
ต้องรู้โง่รู้ฉลาดปราดเปรื่องตน
โง่สิบหนดีกว่าเบ่งเก่งเดี๋ยวเดียว
(อยู่ในบทที่ 70 ครับ)
ยินดีที่มีโอกาสได้รู้จักกันครับ