前幾天突然很想寫些東西,畢竟現在很少自己做開發(主要是現在的工作以維護為主,很少獨立開發新的模組),剛好有個同事在練筆做一個資料統計趨勢的頁面,就按著同樣的要求,自己寫了一個資料統計趨勢圖。還有一個原因是想玩玩CSS。也不知道哪裡冒出來的想頭,用CSS做圖。這樣就產生了下面的代碼。當然,發費了我好久才調試成功(7天左右時間,主要是定位問題,太亂了),到現在還是沒搞清楚,為什麼在表格的cell裡定位是這麼不一樣。!!!
(在ie裡通過,主要是javascript 沒有改進去符合 w3c的標準,如果有人要在firefox下用,自己改javascript,這個應該是方便的:)
附上PHP 的代碼:其中d4x4green是一個4*4像素的圖片,用來標識資料在座標軸的位置
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>chart</TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<style>
.cell{
padding-left: 0px;
padding-right: 1px;
padding-top: 1px;
padding-bottom: 0px;
border-top-width: 1px;
border-right-width: 1px;
border-bottom-width: 1px;
border-left-width: 1px;
border-top-style: dashed;
border-right-style: dashed;
border-bottom-style: none;
border-left-style: none;
border-top-color: #999;
border-right-color: #999;
border-bottom-color: #999;
border-left-color: #999;
position: relative;
}
.polar_y {
border-right-width: 1px;
border-right-style: solid;
border-right-color: #000000;
}
.polar_x {
border-top-width: 1px;
border-top-style: solid;
border-top-color: #000000;
position: relative;
}
.polar_y_value {
clear: right;
float: right;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
position: relative;
top: -8px;
background-color: #FFFFCC;
border: 1px solid #CCCCCC;
}
.polar_x_value {
clear: right;
float: right;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
position: relative;
top:0px;
right:-8px;
}
</style>
<BODY onload="numberCells();">
<?
/*
* 目標:給出一組數,繪出資料的趨勢走向圖
* 1.得到資料個數,得到最大值,最大值/個數(+1) 得到Y組每格增量值
* 2.在640x480的html表格對象中,繪出底圖 , 行是rows+2,列是 col+2 ,多出的一行分別用來增加座標軸,另一行是放資料的
* 3.定位每個資料在底圖中的位置
* 4.繪出每個資料在格子中的位置
*/
$flux="5,0,11,18,9,30,40,50,43,30,32,60";
//$flux="1,35,37";
$months="01,02,03,04,05,06,07,08,09,10,11,12";
$arrflux=split(",",$flux);
$arrmonths=split(",",$months);
$rows=count($arrflux);
$cols=$rows;//count($arrmonths);
$maxflux=0;
for($i=0;$i<$rows;$i++){
if ( $maxflux<intval($arrflux[$i]) ) {
$maxflux=intval($arrflux[$i]);
}
}
$fluxperrow=intval($maxflux/ $rows);
if ($maxflux % $rows >0){
$fluxperrow=$fluxperrow+1;
}
?>
<table width="640" height="480" align="center" cellspacing="0" cellpadding="0" id="tableID">
<?
echo ("<tr>");
echo("<td align=/"right/" valign=/"top/" class=/"polar_y/"><div>");
echo(" ");
echo("</div></td>");
for ($c = 0; $c <= $cols; $c++) {
echo("<td class=/"cell/"> </td>");
}
echo ("</tr>");
// draw polar_y and cell
for ($r = $rows; $r >0; $r--) {
echo ("<tr>");
echo("<td align=/"right/" valign=/"top/" class=/"polar_y/"><div class=/"polar_y_value/">");
echo($r*$fluxperrow);
echo("</div></td>");
for ($c = 0; $c <= $cols; $c++) {
echo("<td class=/"cell/"> </td>");
}
echo ("</tr>");
}
// draw polar_x
echo ("<tr>");
echo("<td align=/"right/" valign=/"top/" class=/"polar_y/"></td>");
for ($c = 0; $c < $cols; $c++) {
echo("<td class=/"polar_x/"><div class=/"polar_x_value/">".$arrmonths[$c]."</div></td>");
}
echo("<td class=/"polar_x/"> </td>");
echo ("</tr>");
?>
</table>
<span id="debug"></sapn>
<SCRIPT LANGUAGE="JavaScript">
<!--
var arrayflux=new Array(
<?
for($i=0;$i<count($arrflux);$i++)
{
echo "'$arrflux[$i]'";
if($i<>(count($arrflux)-1))
{
echo ",";
}
}
?>
);
var fluxperrow=<?=$fluxperrow?>;
var zi=0;
var sAdditional=" position: relative; float:right; right : -6px ; ";
//最大行號=表格行數-1,從最大行數-1開始定位
//表格最小號=0
//表格行數 資料行數+2
function numberCells() {
var tablerows=tableID.rows.length;
for (i=0 ;i< arrayflux.length ;i++){
zi=parseInt(arrayflux[i] / fluxperrow);
//set cell width && height
cellheight=parseInt(480/tablerows );
document.all.tableID.rows(tablerows-2-zi).cells(i+1).style.pixelHeight=cellheight;
cellwidth=parseInt(640/tableID.rows(tablerows-2-zi).cells.length );
document.all.tableID.rows(tablerows-2-zi).cells(i+1).style.pixelWidth=cellwidth;
pos=arrayflux[i] % fluxperrow;
pos=Math.round(cellheight/fluxperrow * pos)+3 - cellheight ;
mystyle=sAdditional+"bottom: "+pos+"px;";
document.all.tableID.rows(tablerows-2-zi).cells(i+1).innerHTML ="<img src=/"./d4x4green.jpg/" style=/""+mystyle+"/" />";
//style=/""+mystyle+"/"
//alert(tablerows+"-2-"+zi+"="+(tablerows-2-zi));
//document.all.debug.innerText=cellwidth+":"+cellheight + " "+document.all.tableID.rows(arrayflux.length-zi).cells(i+1).innerHTML+ " Pos= "+pos;
//break;
}
//設定最後一列的寬度
document.all.tableID.rows(0).cells(arrayflux.length+1).style.pixelWidth=cellwidth;
}
//-->
</SCRIPT>
</BODY>
</HTML>