주식처럼 그래프를 그리려면?

주식처럼 그래프를 그리려면?

QA

주식처럼 그래프를 그리려면?

본문

실시간 사이버 주식을 나타내려고 하는데요
그래프를 그려야 하는데
어떻게 그리조?
PHP에서 선을 그리는 함수가 있나???
어떻게하면 좋을까요?

 

이 질문에 댓글 쓰기 :

답변 3

배열로 쓰던가라... ^^ 잘 응용해서 해보세염 <br>

< br>
<?php< br>
< br>
function img($f_type, $f_date, $x_string, $y_string) {< br>
< br>
//Header("Content-type: image/gif");    // 헤더를 붙이면 file저장이 안되고 바로 보여주는 방식으로 해야한다.< br>
$fp = fopen($f_type[19], "wb"); < br>
$im = imagecreate($f_type[7], $f_type[8]);< br>
< br>
< br>
$x_font_max = 0;< br>
$y_font_max = 0;< br>
< br>
if (count($y_string) > 0) { < br>
rsort($y_string);< br>
if (strlen($y_string[0]) > strlen(end($y_string))) $y_font_max = strlen($y_string[0]);< br>
else $y_font_max = strlen(end($y_string));< br>
$y_font_max = $y_font_max*5 + 5;< br>
}< br>
< br>
< br>
if (count($x_string) > 0) { < br>
$x_string2 = $x_string;< br>
rsort($x_string2);< br>
if (strlen($x_string2[0]) > strlen(end($x_string2))) $x_font_max = strlen($x_string2[0]);< br>
else $x_font_max = strlen(end($x_string2));< br>
$x_font_max = $x_font_max*5 + 5;< br>
}< br>
< br>
< br>
$backcolor = ImageColorAllocate($im, $f_type[12],$f_type[13],$f_type[14]);< br>
< br>
$black = ImageColorAllocate($im, $f_type[16],$f_type[17],$f_type[18]);< br>
$black2 = ImageColorAllocate($im, 0,0,0);< br>
$y_line =  ImageColorAllocate($im, $f_type[3],$f_type[4],$f_type[5]);< br>
$p_color =  ImageColorAllocate($im, $f_type[9],$f_type[10],$f_type[11]);< br>
< br>
$p_date = $f_date;< br>
rsort($p_date);< br>
< br>
if ($f_type[0] == "line") {< br>
$points[0]=0;< br>
for ($i=0;$i<=count($f_date)-2;$i++) {< br>
< br>
< br>
$p_width = ($f_type[7] - 10 - $y_font_max) / count($f_date);< br>
$p_height = $f_type[8] - 10 - $x_font_max;< br>
$p_x1 = $i*$p_width+5 + ((($i+1)*$p_width+5)-($i*$p_width+5))/2;< br>
$p_x2 = ($i+1)*$p_width+5 + ((($i+2)*$p_width+5)-(($i+1)*$p_width+5))/2;< br>
< br>
$p_y1 = 0;< br>
$p_y2 = 0;< br>
if ($f_type[1] == "center") {< br>
$p_end = $p_date[count($p_date)-1];< br>
if (($p_date[0]-$f_type[2]) > (-1*($p_end-$f_type[2]))) $p_max = ($p_date[0]-$f_type[2]) * 2;< br>
else $p_max = -2*($p_end-$f_type[2]);< br>
< br>
$p_y1 = $p_height/2+5 - ($f_date[$i]-$f_type[2])/$p_max*$p_height;< br>
$p_y2 = $p_height/2+5 - ($f_date[$i+1]-$f_type[2])/$p_max*$p_height;< br>
< br>
<br>
} else {< br>
< br>
if ($f_date[$i] < 0) $p_y1 = $p_height + 5;< br>
else $p_y1 = $p_height-$f_date[$i]/$p_date[0]*$p_height +5;< br>
if ($f_date[$i+1] < 0) $p_y2 = $p_height + 5;< br>
else $p_y2 = $p_height-$f_date[$i+1]/$p_date[0]*$p_height + 5;< br>
< br>
}< br>
$p_x1 += $y_font_max;< br>
$p_x2 += $y_font_max;< br>
< br>
< br>
imageline($im, $p_x1, $p_y1, $p_x2, $p_y2, $p_color);< br>
}< br>
< br>
< br>
}< br>
else if ($f_type[0] == "box") {< br>
< br>
for ($i=0;$i<=count($f_date)-1;$i++) {< br>
$p_width = ($f_type[7] - 10 - $y_font_max) / count($f_date);< br>
$p_height = $f_type[8] - 10 - $x_font_max;< br>
$p_center = $i*$p_width+5 + ((($i+1)*$p_width+5)-($i*$p_width+5))/2;< br>
$p_x1 = $p_center - $p_width/3;< br>
$p_x2 = $p_center + $p_width/3;< br>
< br>
$p_y1 = 0;< br>
$p_y2 = 0;< br>
<br>
if ($f_type[1] == "center") {< br>
$p_end = $p_date[count($p_date)-1];< br>
if (($p_date[0]-$f_type[2]) > (-1*($p_end-$f_type[2]))) $p_max = ($p_date[0]-$f_type[2]) * 2;< br>
else $p_max = -2*($p_end-$f_type[2]);< br>
< br>
$p_y1 = $p_height/2+5 - ($f_date[$i]-$f_type[2])/$p_max*$p_height;< br>
$p_y2 = $p_height/2+5;< br>
if ($p_y1 > $p_y2) {< br>
$temp = $p_y1;< br>
$p_y1 = $p_y2;< br>
$p_y2 = $temp;< br>
}< br>
<br>
} else {< br>
$p_y1 = $p_height-$f_date[$i]/$p_date[0]*$p_height+5;< br>
$p_y2 = $p_height+5;< br>
< br>
if (($p_y2 - $p_y1) <= 0) $p_y1 = $p_y2;< br>
}< br>
$p_x1 += $y_font_max;< br>
$p_x2 += $y_font_max;< br>
imagefilledrectangle($im, $p_x1, $p_y1, $p_x2, $p_y2, $p_color); // 색이 칠해진 직사각형 <br>
< br>
}< br>
< br>
}< br>
else {< br>
< br>
< br>
for ($i=0;$i<=count($f_date)-1;$i++) {< br>
< br>
$p_width = ($f_type[7] - 10 - $y_font_max) / count($f_date);< br>
$p_height = $f_type[8] - 10 - $x_font_max;< br>
< br>
$p_x1 = $i*$p_width+5 + ((($i+1)*$p_width+5)-($i*$p_width+5))/2;< br>
$p_x2 = $p_x1;< br>
$p_y1 = 0;< br>
$p_y2 = 0;< br>
<br>
if ($f_type[1] == "center") {< br>
$p_end = $p_date[count($p_date)-1];< br>
if (($p_date[0]-$f_type[2]) > (-1*($p_end-$f_type[2]))) $p_max = ($p_date[0]-$f_type[2]) * 2;< br>
else $p_max = -2*($p_end-$f_type[2]);< br>
< br>
$p_y1 = $p_height/2+5 - ($f_date[$i]-$f_type[2])/$p_max*$p_height;< br>
$p_y2 = $p_height/2+5;< br>
<br>
} else {< br>
$p_y1 = $p_height-$f_date[$i]/$p_date[0]*$p_height+5;< br>
$p_y2 = $p_height+5;< br>
if (($p_y2 - $p_y1) <= 0) $p_y1 = $p_y2; < br>
}< br>
< br>
$p_x1 += $y_font_max;< br>
$p_x2 += $y_font_max;< br>
< br>
imageline($im, $p_x1, $p_y1, $p_x2, $p_y2, $p_color);< br>
< br>
}< br>
< br>
< br>
}< br>
< br>
< br>
if (count($y_string) > 0) {< br>
for ($i=0;$i<=count($y_string)-1;$i++) {< br>
if ($f_type[1] == "center") $p_y1 = $p_height/2+5 - ($y_string[$i]-$f_type[2])/$p_max*$p_height;< br>
else $p_y1 = $p_height-$y_string[$i]/$p_date[0]*$p_height+5;< br>
if ((($f_type[6] && "line") && ($y_string[$i] > 0)) ||  $f_type[1] == "center") imagestring($im, $font, ($y_font_max - strlen($y_string[$i]) * 5) , $p_y1, $y_string[$i], $black);< br>
< br>
if ((($f_type[6] && "line") && ($y_string[$i] > 0)) ||  $f_type[1] == "center") imageline($im, (5 + $y_font_max), $p_y1, ($f_type[7] - 5), $p_y1, $y_line);< br>
< br>
}< br>
}< br>
< br>
if (count($x_string) > 0) {< br>
for ($i=0;$i<=count($x_string)-1;$i++) {< br>
$p_x1 = $i*$p_width+5 + ((($i+1)*$p_width+5)-($i*$p_width+5))/2-3;< br>
$p_y1 = $f_type[8] - $x_font_max + strlen($x_string[$i])*5;< br>
< br>
ImageStringUp($im, $font, ($p_x1+$y_font_max), $p_y1, $x_string[$i], $black);< br>
< br>
}< br>
}< br>
if ($f_type[1] == "center") {< br>
imageline($im, (5 + $y_font_max), 5 , (5 + $y_font_max), ($f_type[8] - 5 - $x_font_max), $black2);< br>
imageline($im, (5 + $y_font_max), (($f_type[8] - $x_font_max)/2), ($f_type[7] - 5), (($f_type[8] - $x_font_max)/2), $black2);< br>
< br>
}< br>
else {< br>
imageline($im, (5 + $y_font_max), 5, (5 + $y_font_max), ($f_type[8] - 5 - $x_font_max), $black2);< br>
imageline($im, (5 + $y_font_max), ($f_type[8] - 5 - $x_font_max), ($f_type[7] - 5), ($f_type[8] - $x_font_max - 5), $black2);< br>
}< br>
< br>
if ($f_type[15] == "yes") imagecolortransparent($im, $backcolor); // 배경 색깔을 뺀다...< br>
< br>
Imagegif($im,$f_type[19]); < br>
ImageDestroy($im);< br>
< br>
}< br>
< br>
/*< br>
< br>
$f_type[0] = "box"; //"bar", "box", "line"< br>
$f_type[1] = "center"; // "standard", "center"< br>
$f_type[2] = 0; // 표준이 되는 값 <br>
< br>
// y_string 이 있을때 표시줄 색깔을 넣는다.< br>
$f_type[3] = "200"; < br>
$f_type[4] = "200"; < br>
$f_type[5] = "200"; < br>
$f_type[6] = "y_line"; // y_string 이 있을때 체크 <br>
< br>
$f_type[7]=500; // 이미지 사이즈 넓이 <br>
$f_type[8] =500; // 이미지 사이즈 높이 <br>
< br>
// 라인이나 선 박스의 색깔을 구한다.< br>
$f_type[9] = "0"; //red< br>
$f_type[10] = "255"; //green< br>
$f_type[11] = "0"; //blue< br>
< br>
// 배경색을 넣는다.< br>
$f_type[12] = "255"; //red< br>
$f_type[13] = "255"; //green< br>
$f_type[14] = "255"; //blue< br>
< br>
// 배경색깔을 빼고 싶은 색깔을 넣는다.< br>
$f_type[15] = "yes"; // yes 경우에만 <br>
< br>
// 글짜색 <br>
$f_type[16] = "0"; //red< br>
$f_type[17] = "0"; // green< br>
$f_type[18] = "0"; // blue< br>
< br>
// gif file 저장 경로및 file명를 정한다.< br>
$f_type[19] = "./img/0010110.gif"; < br>
< br>
< br>
mt_srand((double)microtime()*100000000000000); < br>
< br>
//for ($i=0;$i<=99;$i++) $f_date[]=mt_rand();< br>
for ($i=0;$i<=20;$i++) $f_date[]=mt_rand()-1000000000;< br>
//for ($i=0;$i<=5;$i++) $f_date[]=$i;< br>
$y_string = $f_date;< br>
$x_string = $f_date;< br>
img($f_type, $f_date, $x_string, $y_string);< br>
< br>
*/< br>

?>< br> 

 

입니다 ~

php만으로 그래프를 그리기에는 다소 많은 어려움이 있습니다.

 

관련 라이브러리가 있긴 하지만 유료로 제공되는것들이 많고

 

추세에 따라 자바스크립트로 된 차트 라이브러리를 이용하는 것이 많이 도움이 되실것 같습니다.

 

http://humblesoftware.com/flotr2/ 

 

이런것들을 활용하시면 될듯합니다.

 

구글에서 자바스크립트 차트 등으로 검색해보시면 많은 결과가 나옵니다.

답변을 작성하시기 전에 로그인 해주세요.
전체 17
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT