JAVA APPLET BAR+LINE GRAPH(SOURCE CODE) 2
ปรับแก้โปรแกรมให้แสดง สีหลังภาพเป็น %
---------------------------- JAVA SORCE ------------------------------
import java.awt.*; import java.applet.*; import java.util.*; import java.lang.*;
public class GraphMix extends Applet { private Double fgWidth ; private Double fgHeight ;
public void paint(Graphics g) { Double max,min,rate,num_split,step; String licence = getParameter("COPYRIGHT") ; int grap = Integer.parseInt(getParameter("GRAP")); // ตัวแปรนี้ใช้กำหนดระยะขอบโดยประมาณ X px. int num_x; // จำนวนชุดข้อมูล fgWidth = new Double(getParameter("WIDTH")) ; // ความกว้างซึ่งเป็นตัวเดียวกับที่ Applete CODE ใน HTML fgHeight = new Double(getParameter("HEIGHT")); // เช่นเดียวกับความกว้าง แต่เป็นความสูง int showValue = Integer.parseInt(getParameter("SHOWVALUE")); fgWidth =new Double( fgWidth.doubleValue() - 50); // กำหนดระยะขอบลดลงเบื้องต้นก่อน 50 px fgHeight = new Double(fgHeight.doubleValue() - 70); max = new Double(getParameter("MAX-Y")); // ค่าแกน Y สูงสุด min = new Double(getParameter("MIN-Y")); // ค่าแกน Y ต่ำสุด num_split =new Double(getParameter("SET-Y")); // จำนวน Y ที่ถูกแบ่งเป็นกี่ส่วน num_x = Integer.parseInt(getParameter("NUM-X")); // จำนวน ข้อมูลใน 1 ชุด String label_x = getParameter("DATA-X"); // label ที่แกน X StringTokenizer label_x_data= new StringTokenizer(label_x,"|"); // ข้อมูลกราฟเส้น Y รับได้สูงสุด 4 ชุด ใช้เครื่องหมาย pipe แบ่งข้อมูล และจำนวนข้อมูลต้องเท่ากับ num_x ต.ย. ข้อมูลเช่น // // ถ้าไม่ใช่ให้กำหนดค่า NONE ไว้ เช่น // StringTokenizer DATA; String LINE1 = getParameter("LINE1"); String LINE2 = getParameter("LINE2"); String LINE3 = getParameter("LINE3"); String LINE4 = getParameter("LINE4"); if ( LINE1.equals("NONE") ) { LINE1 = ""; } if ( LINE2.equals("NONE") ) { LINE2 = ""; } if ( LINE3.equals("NONE") ) { LINE3 = ""; } if ( LINE4.equals("NONE") ) { LINE4 = ""; }
// ส่วนของกราฟแท่ง String BOX1 = getParameter("BOX1"); String BOX2 = getParameter("BOX2"); String BOX3 = getParameter("BOX3"); String BOX4 = getParameter("BOX4"); if ( BOX1.equals("NONE") ) { BOX1 = ""; } if ( BOX2.equals("NONE") ) { BOX2 = ""; } if ( BOX3.equals("NONE") ) { BOX3 = ""; } if ( BOX4.equals("NONE") ) { BOX4 = ""; } // กำหนดสี background/font and set default color Color background; Color color,color1,color2,color3,color4,colorFont; color1 = new Color(0,0,255); color2 = new Color(255,0,0); color3 = new Color(128,0,128); color4 = new Color(0,100,0);
color1 = convertColor(getParameter("COLOR1")); color2 = convertColor(getParameter("COLOR2")); color3 = convertColor(getParameter("COLOR3")); color4 = convertColor(getParameter("COLOR4")); colorFont = convertColor(getParameter("FONT-COLOR"));
// color = new Color(173,216,230); // กำหนด background color background = new Color(220,240,255); background = convertColor(getParameter("BACKGROUND")); setBackground(background); int fontSize = Integer.parseInt(getParameter("FONT-SIZE")); Font font = new Font(getParameter("FONT-NAME"),Font.PLAIN,fontSize); background = convertColor(getParameter("BACKGROUND")); g.setFont(font);
// เป็นไปได้ที่เป็น กราฟค่าที่ระยะแกน Y มีค่าน้อยกว่า diff max กับ min if ( ( max.doubleValue() - min.doubleValue() ) > fgHeight.doubleValue() ) { rate = new Double( (fgHeight.doubleValue() - 20) / ( max.doubleValue() - min.doubleValue() ) ) ; } else { rate = new Double( (fgHeight.doubleValue() - 20 )/ ( max.doubleValue() - min.doubleValue() )) ; } step = new Double( (( max.doubleValue() - min.doubleValue() ) / num_split.doubleValue()) ) ;
// แสดงข้อมูลข้อความที่แกน x และ แกน y g.setColor(colorFont); g.drawString(getParameter("NAME-Y"),10,10+(grap/2)); g.drawString(getParameter("NAME-X"),fgWidth.intValue() +(grap/2) ,fgHeight.intValue()+25);
// gan - X gan(g,50,20+grap,50,fgHeight.intValue()+grap,colorFont); // gan - Y gan(g,50,fgHeight.intValue()+grap,fgWidth.intValue(),fgHeight.intValue()+grap,colorFont);
// scale - Y for (int i = 0 ; i < num_split.intValue() ; i++) { Integer x,tmp_i; String x_label; Double tmp_d; tmp_i = new Integer(i); tmp_d = new Double( min.doubleValue() + step.doubleValue() * tmp_i.doubleValue() ); x_label = new String(tmp_d.toString() ); x_label = formatNumber(x_label,2); tmp_d = new Double( fgHeight.doubleValue() - step.doubleValue() * rate.doubleValue() * tmp_i.doubleValue() ); x = new Integer ( tmp_d.intValue() ); g.drawString(x_label.toString(),10,x.intValue()+grap+2); g.drawLine(40,x.intValue()+grap,50,x.intValue() +grap); } g.drawLine(40,20+grap,50,20 +grap); g.drawString(max.toString(),10,20+grap+2);
// scale - X int x_step = ( fgWidth.intValue() - 50 ) / (num_x + 1) ; for (int i = 0 ; i <= num_x ; i++) { int xx = 50 + ( i * x_step ) ; if ( i > 0 ) { g.drawLine( xx,fgHeight.intValue()+grap,xx,fgHeight.intValue()+grap+10); g.drawString(label_x_data.nextToken(),xx-10,fgHeight.intValue()+grap+30); } }
String num_area = getParameter("NUM_AREA") ; if ( num_area != null ) { if ( ! num_area.equals("NONE") ) { int int_area = Integer.parseInt(num_area); for ( int i = 1 ; i < int_area + 1 ; i++) { String readArea = "AREA" + Integer.toString(i); String TEXT = getParameter(readArea); String datas[] = TEXT.split("|"); String str_htmlColor = datas[0]; Color color_htmlColor = convertColor(str_htmlColor); String str_htmlVal = datas[1] ; Double dbl_htmlVal = new Double(str_htmlVal); Double tmp_d = new Double( (fgHeight.doubleValue() + grap) - ( rate.doubleValue() * dbl_htmlVal.doubleValue()) ); int h =( fgHeight.intValue() + grap ) - tmp_d.intValue() ; g.setColor(color_htmlColor); g.fillRect(50 + 1,fgHeight.intValue()+grap-h, fgWidth.intValue() - (grap * 2) - 10 ,h); } } // end if num_area == NONE } // end if num_area is null
// LINE 1 int fg = 0; int preX = 0; int preY= 0; if ( LINE1.length() != 0 ) { DATA= new StringTokenizer(LINE1,"|"); g.setColor(color1); for (int i = 0 ; i <= num_x ; i++) { int xx = 50 + ( i * x_step ) ; if ( i > 0 ) { //g.drawLine( xx,fgHeight+grap,xx,fgHeight+grap+10); String yy = DATA.nextToken(); Double yyy = new Double(yy); Double tmp_d = new Double( (fgHeight.doubleValue() + grap) - ( rate.doubleValue() * yyy.doubleValue()) ); if ( showValue == 1 ) { g.drawString(yy,xx,tmp_d.intValue()); } if ( fg == 1 ) { g.drawLine(preX,preY,xx,tmp_d.intValue()); } preX = xx; preY =tmp_d.intValue(); fg = 1; } } }
// LINE 2 if ( LINE2.length() != 0 ) { DATA= new StringTokenizer(LINE2,"|"); g.setColor(color2); fg = 0; preX = 0; preY= 0; for (int i = 0 ; i <= num_x ; i++) { int xx = 50 + ( i * x_step ) ; if ( i > 0 ) { String yy = DATA.nextToken(); Double yyy = new Double(yy); Double tmp_d = new Double( (fgHeight.doubleValue() + grap) - ( rate.doubleValue() * yyy.doubleValue()) ); if ( showValue == 1 ) { g.drawString(yy,xx,tmp_d.intValue()); } if ( fg == 1 ) { g.drawLine(preX,preY,xx,tmp_d.intValue()); } preX = xx; preY =tmp_d.intValue(); fg = 1; } } }
// LINE 3 if ( LINE3.length() != 0 ) { DATA= new StringTokenizer(LINE3,"|"); g.setColor(color3); fg = 0; preX = 0; preY= 0; for (int i = 0 ; i <= num_x ; i++) { int xx = 50 + ( i * x_step ) ; if ( i > 0 ) { String yy = DATA.nextToken(); Double yyy = new Double(yy); Double tmp_d = new Double( (fgHeight.doubleValue() + grap) - ( rate.doubleValue() * yyy.doubleValue()) ); if ( showValue == 1 ) { g.drawString(yy,xx,tmp_d.intValue()); } if ( fg == 1 ) { g.drawLine(preX,preY,xx,tmp_d.intValue()); } preX = xx; preY =tmp_d.intValue(); fg = 1; } } }
// LINE 4 if ( LINE4.length() != 0 ) { DATA= new StringTokenizer(LINE4,"|"); g.setColor(color4); fg = 0; preX = 0; preY= 0; for (int i = 0 ; i <= num_x ; i++) { int xx = 50 + ( i * x_step ) ; if ( i > 0 ) { String yy = DATA.nextToken(); Double yyy = new Double(yy); Double tmp_d = new Double( (fgHeight.doubleValue() + grap) - ( rate.doubleValue() * yyy.doubleValue()) ); if ( showValue == 1 ) { g.drawString(yy,xx,tmp_d.intValue()); } if ( fg == 1 ) { g.drawLine(preX,preY,xx,tmp_d.intValue()); } preX = xx; preY =tmp_d.intValue(); fg = 1; } } }
int countBox = 0; if ( BOX1.length() != 0 ) { countBox++; } if ( BOX2.length() != 0 ) { countBox++; } if ( BOX3.length() != 0 ) { countBox++; } if ( BOX4.length() != 0 ) { countBox++; } int boxFULL = ( x_step * 80 / 100) ; int boxWidth = 0; if ( countBox != 0 ) { boxWidth = boxFULL / countBox ; } // BOX1 int idxBox = 0; if ( BOX1.length() != 0 ) { DATA= new StringTokenizer(BOX1,"|"); g.setColor(color1); for (int i = 0 ; i <= num_x ; i++) { int xx = 50 + ( i * x_step ) ; if ( i > 0 ) { String yy = DATA.nextToken(); Double yyy = new Double(yy); Double tmp_d = new Double( (fgHeight.doubleValue() + grap) - ( rate.doubleValue() * yyy.doubleValue()) ); if ( showValue == 1 ) { g.drawString(yy,xx,tmp_d.intValue()); } int h =( fgHeight.intValue() + grap ) - tmp_d.intValue() ; xx = xx - ( boxFULL / 2 ) - (boxWidth * idxBox ); g.fillRect(xx,tmp_d.intValue(),boxWidth,h); } } } if ( BOX2.length() != 0 ) { idxBox++; DATA= new StringTokenizer(BOX2,"|"); g.setColor(color2); for (int i = 0 ; i <= num_x ; i++) { int xx = 50 + ( i * x_step ) ; if ( i > 0 ) { String yy = DATA.nextToken(); Double yyy = new Double(yy); Double tmp_d = new Double( (fgHeight.doubleValue() + grap) - ( rate.doubleValue() * yyy.doubleValue()) ); if ( showValue == 1 ) { g.drawString(yy,xx,tmp_d.intValue()); } int h =( fgHeight.intValue() + grap ) - tmp_d.intValue() ; xx = xx - ( (boxFULL/2) - (boxWidth * idxBox)) ; g.fillRect(xx,tmp_d.intValue(),boxWidth,h); } } }
if ( BOX3.length() != 0 ) { idxBox++; DATA= new StringTokenizer(BOX3,"|"); g.setColor(color3); for (int i = 0 ; i <= num_x ; i++) { int xx = 50 + ( i * x_step ) ; if ( i > 0 ) { String yy = DATA.nextToken(); Double yyy = new Double(yy); Double tmp_d = new Double( (fgHeight.doubleValue() + grap) - ( rate.doubleValue() * yyy.doubleValue()) ); if ( showValue == 1 ) { g.drawString(yy,xx,tmp_d.intValue()); } int h =( fgHeight.intValue() + grap ) - tmp_d.intValue() ; xx = xx - ( (boxFULL/2) - (boxWidth*idxBox) ) ; g.fillRect(xx,tmp_d.intValue(),boxWidth,h); } } }
if ( BOX4.length() != 0 ) { idxBox++; DATA= new StringTokenizer(BOX4,"|"); g.setColor(color4); for (int i = 0 ; i <= num_x ; i++) { int xx = 50 + ( i * x_step ) ; if ( i > 0 ) { String yy = DATA.nextToken(); Double yyy = new Double(yy); Double tmp_d = new Double( (fgHeight.doubleValue() + grap) - ( rate.doubleValue() * yyy.doubleValue()) ); if ( showValue == 1 ) { g.drawString(yy,xx,tmp_d.intValue()); } int h =( fgHeight.intValue() + grap ) - tmp_d.intValue() ; xx = xx - ( (boxFULL/2) - (boxWidth*idxBox) ) ; g.fillRect(xx,tmp_d.intValue(),boxWidth,h); } } } String NUM_TEXT = getParameter("NUM_TEXT"); if ( NUM_TEXT != null ) { if ( ! NUM_TEXT.equals("NONE") ) { int num_text = Integer.parseInt(NUM_TEXT); for ( int i = 1 ; i < num_text + 1 ; i++) { String txt = ""; String w = "TXT" + Integer.toString(i); String TEXT = getParameter(w); String lines[] = TEXT.split("|"); String txtFontType = lines[0]; int txtFontSize = Integer.parseInt(lines[1]); Color txtColor = convertColor(lines[2]); int a = Integer.parseInt(lines[3]); int b = Integer.parseInt(lines[4]); txt = lines[5]; Font txtFont = new Font(txtFontType,Font.PLAIN,txtFontSize); g.setFont(txtFont); g.setColor(txtColor); g.drawString(txt,a,b); } // end for i } //end if NUM_TXT == NONE } // end if NUM_TEXT != null
}
private String formatNumber(String val,int digit ) { String ret; int idxOfDot,end; idxOfDot = val.indexOf("."); if ( idxOfDot == 0 ) { ret = val; } else { StringTokenizer tmp; tmp = new StringTokenizer(val,"."); String a1 = tmp.nextToken(); String a2 = tmp.nextToken(); if ( digit > a2.length() - 1 ) { ret = a1 + "." + a2.substring(0); } else { ret = a1 + "." + a2.substring(0,digit); } } return ret; } private void gan(Graphics g,int x1,int y1,int x2,int y2,Color colorFont) { g.setColor(colorFont); g.drawLine(x1,y1,x2,y2); }
private Color convertColor(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) ; sum = sum + ( d * power(16,j)); ret = sum; } return ret; } private int power(int num,int p) { int ret; if ( p < 0 ) { ret = 0; } else if ( p == 0) { ret = 1; } else if ( p == 1) { ret = num; } else { int sum = 0; for ( int i = 1 ; i < p ; i++ ) { sum = sum + ( num * num ); } ret = sum; } return ret ; }
}
--------------------------- END JAVA SOURCE ----------------------- -------------------------- HTML SOURCE ---------------------------- <HTML> <HEAD> <="Content-type" content="text/html;charset=TIS-620" /> <="CACHE-CONTROL" content="NO-CACHE" /> <="PRAGMA" content="NO-CACHE" /> <="EXPIRES" content="Sun ,18 Oct 1987 12:00:00 GMT" /> <TITLE>Yeremiah : Graph Mix</TITLE> </HEAD> <BODY> <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%"> <TR> <TD style="font-family:MS Sans Serif;font-size:12pt;color:gold;background:#0000a0;border:10px inset #0000a0;" >กราฟทดสอบแบบผสม</TD><TD> </TD> </TR> <TR> <TD WIDTH="730" style="border:4px outset blue;background:blue;"> <APPLET code="GraphMix.class" style="width:721px;height:100%;border:5px inset blue;"> <PARAM NAME="SHOWVALUE" VALUE="1"/><!-- แสดงตัวเลขหรือไม่ 1 แสดง,0 ไม่แสดง --> <PARAM NAME="GRAP" VALUE="20"/><!-- ช่องวางจากจุดขอบ --> <PARAM NAME="NAME-Y" VALUE="มูลค่า(บาท)"/> <!-- หน่วยแกนตั้ง --> <PARAM NAME="NAME-X" VALUE="เดือน"/><!-- หน่วยแกนนอน --> <PARAM NAME="MAX-Y" VALUE="100"/> <!-- แกนตั้ง --> <PARAM NAME="MIN-Y" VALUE="0"/> <PARAM NAME="SET-Y" VALUE="10"/> <PARAM NAME="BACKGROUND" VALUE="#DCF0FF" /> <PARAM NAME="COLOR1" VALUE="NAVY" /> <PARAM NAME="COLOR2" VALUE="tomato" /> <PARAM NAME="COLOR3" VALUE="YELLOW" /> <PARAM NAME="COLOR4" VALUE="#00a000" /> <PARAM NAME="NUM-X" VALUE="12" /> <PARAM NAME="DATA-X" VALUE="JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC" /> <PARAM NAME="LINE1" VALUE="30|50|43|14|53|64|57|88|19|40|21|12" /> <PARAM NAME="LINE2" VALUE="33|65|24|51|31|62|70|80|35|60|22|32" /> <PARAM NAME="LINE3" VALUE="13|35|44|11|31|42|50|30|45|50|62|72" /> <PARAM NAME="LINE4" VALUE="33|45|74|31|11|22|30|30|65|80|12|12" /> <PARAM NAME="BOX1" VALUE="53|35|44|41|51|42|50|40|35|20|32|42" /> <PARAM NAME="BOX2" VALUE="33|65|24|51|31|62|70|80|35|60|22|32" /> <PARAM NAME="BOX3" VALUE="13|35|44|11|31|42|50|30|45|50|62|72" /> <PARAM NAME="BOX4" VALUE="33|45|74|31|11|22|30|30|65|80|12|12" /> <PARAM NAME="FONT-NAME" VALUE="MS Sans Serif" /> <!-- Default Font --> <PARAM NAME="FONT-SIZE" VALUE="12" /> <PARAM NAME="FONT-COLOR" VALUE="BLUE" /> <PARAM name="NUM_TEXT" value="2" /> <PARAM name="TXT1" value="MS Sans Serif|20|BLUE|170|30|ความสามารถในการแข่งขันของสินค้าไทย" /> <PARAM name="TXT2" value="MS Sans Serif|10|#FF0000|210|460|Copyright @ 2011 by Solution Provider Network." />
<PARAM name="NUM_AREA" value="3" /> <PARAM name="AREA1" value="#33FFFF|100" /> <PARAM name="AREA2" value="#FFFFC0|80" /> <PARAM name="AREA3" value="#FFBBBB|60" />
</APPLET> </TD>
<TD ALIGN="CENTER" VALIGN="TOP"> <div style="border:5px inset skyblue;width:80%;background:#ADD8E6;"> <font color="BLUE"> -- พ.ศ. 2544</font><BR/> <font COLOR="RED" > -- พ.ศ. 2543</font><BR/> <font COLOR="#FF4993" > -- พ.ศ. 2543</font><BR/> <font COLOR="#FFFFC0" > -- พ.ศ. 2543</font><BR/> </div> <table border="0" width="100%"><tr height="400px"><td align="center" valign="bottom"> <img src="/images/back.gif" width="100px" onclick="history.back();" /> </td></tr></table> </TD> </TR> </TABLE>
</BODY> </HTML>
Create Date : 10 เมษายน 2554 |
|
16 comments |
Last Update : 10 เมษายน 2554 13:06:52 น. |
Counter : 3498 Pageviews. |
|
|
|