如果你是一名Web開(kāi)發(fā)者,那么你一定會(huì)有使用PHP來(lái)寫(xiě)代碼。當(dāng)你需要在你的網(wǎng)站上添加一個(gè)圖表或趨勢(shì)圖的時(shí)候,PHP pChart是一種非常有用的工具。
為了使用pChart,我們需要從他們的官網(wǎng)上下載和安裝這個(gè)項(xiàng)目。然后,在你的代碼中引用pChart:
require_once("class/pData.class.php"); require_once("class/pDraw.class.php"); require_once("class/pImage.class.php");
為了繪制一個(gè)簡(jiǎn)單的柱狀圖,你需要定義數(shù)據(jù)和繪圖的參數(shù)。下面是一個(gè)例子:
$data = new pData(); $data->addPoints(array(1,5,7,9,12,8,9),"ScoreA"); $data->addPoints(array(3,2,5,8,11,9,10),"ScoreB"); $data->setSerieDescription("ScoreA","Student A"); $data->setSerieDescription("ScoreB","Student B"); $data->setXAxisName("Test"); $data->addPoints(array("Test 1","Test 2","Test 3","Test 4","Test 5","Test 6","Test 7"),"Labels"); $data->setSerieOnAxis("ScoreA",0); $data->setSerieOnAxis("ScoreB",0); $myPicture = new pImage(700,230,$data); $myPicture->drawGradientArea(0,0,700,230,DIRECTION_VERTICAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>100)); $myPicture->drawGradientArea(0,0,700,30,DIRECTION_VERTICAL,array("StartR"=>255,"StartG"=>255,"StartB"=>255,"EndR"=>30,"EndG"=>30,"EndB"=>30,"Alpha"=>100)); $myPicture->drawLine(0,30,700,30,array("R"=>255,"G"=>255,"B"=>255)); $myPicture->setFontProperties(array("FontName"=>"fonts/arial.ttf","FontSize"=>8)); $myPicture->drawText(150,18,"Scores",array("R"=>255,"G"=>255,"B"=>255)); $myPicture->drawText(20,220,"Test",array("R"=>255,"G"=>255,"B"=>255)); $myPicture->setGraphArea(60,40,670,190); $myPicture->drawScale(array("AxisR"=>0,"AxisG"=>0,"AxisB"=>0,"DrawSubTicks"=>TRUE,"CycleBackground"=>TRUE,"Mode"=>SCALE_MODE_START0)); $myPicture->drawBarChart(array("DisplayValues"=>TRUE,"DisplayColor"=>DISPLAY_MANUAL,"DisplayR"=>255,"DisplayG"=>0,"DisplayB"=>0)); header("Content-type:image/png"); $myPicture->render();
上述代碼將繪制一個(gè)簡(jiǎn)單的柱狀圖,并在Web瀏覽器上顯示它。
如果你想繪制其他類(lèi)型的圖表,pChart還有許多其他的方法和選項(xiàng)。例如,你可以繪制線圖、餅圖和散點(diǎn)圖。pChart還支持許多細(xì)節(jié)設(shè)置,以幫助你自定義你的圖表。
總的來(lái)說(shuō),如果你需要在你的網(wǎng)站上添加一個(gè)圖表或趨勢(shì)圖,PHP pChart是一個(gè)非常有用的工具,它可以幫助你快速地生成各種類(lèi)型的圖表。