⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class.hist

📁 PHP4_0入门与提高源程序代码
💻 HIST
字号:
<?php

/**************************************
这个类接受一个一维数组的数据,
通过getStatus()和getBins()返回两个数组
***************************************/
//类的定义开始
class Histogram {

	//变量的定义
	var $N,$MIN,$MAX,$AVG,$STDV,$SUM,$SUM2,$NBINS;
	var $BINS = array();
	var $BINGRAPH = array();
	var $STATS = array();

	/*****************************
	function:	类的构造函数
	$param:	$data $nbins $title
	*****************************/
	function Histogram($data="", $nbins=0, $title="") {
		if ($data && $nbins) {
			$this->create($data,$nbins,$title);
		}
	}


/******************************
function:	创建直方图
$param:	$data	保存数据的数组
$param:	$data	数据分组的个数
******************************/
function create($data,$nbins,$title="") {
		
		//检查给出的数组是否可以使用
		(($this->N = count($data)) > 1) or die("Not enough data, number of values: ".$this->N."\n");
		($this->NBINS = $nbins) > 1 or die("Insuficient number of bins\n");
		
		//初始化类的变量
		$this->MIN = (float) min($data);
		$this->MAX = (float) max($data);
		$delta = (float) ($this->MAX - $this->MIN)/$this->NBINS;
		$this->setTitle($title);
		
		//初始化bins数组
		for ($i=0; $i < $this->NBINS; $i++) {
			$bin[$i] = (float) $this->MIN + $delta * $i;
			$this->BINS[ (string) $bin[$i] ] = 0;
		}
		
		//计算各统计数字
		for ($i = 0; $i < $this->N ; $i++) {
			$this->SUM += (float) $data[$i];
			$this->SUM2 += (float) pow($data[$i],2);
		}
		$this->AVG = $this->SUM/$this->N;
		$this->STDV = sqrt(($this->SUM2 - $this->N*pow($this->AVG,2))/(float)($this->N - 1));

		//构造STATUS数组
		$this->STATS =	array (
									MIN=>$this->MIN,
									MAX=>$this->MAX,
									N=>$this->N,
									SUM=>$this->SUM,
									SUM2=>$this->SUM2,
									AVG=>$this->AVG,
									STDV=>$this->STDV,
									NBINS=>$this->NBINS
								);

		//构造BINS数组
		sort($data);
		$tmp = ($this->NBINS - 1);
		for ($i = 0; $i < $this->N; $i++) {
			for ($j = $tmp; $j >= 0; $j--) {
				if ($data[$i] >= $bin[$j]) {
					$this->BINS[ (string) $bin[$j] ]++;
					break;
				}
			}
		}
	}


	//设定标题
	function setTitle($title) {
		$this->TITLE=$title;
	}

	//返回STATUS数组
	function getStats() {
		return $this->STATS;
	}

	//返回BINS数组
	function getBins() {
		return $this->BINS;
	}

	function getGraphBins() {
		while (list($k,$v) = each($this->BINS)) {
			$bin[] = sprintf("%5.2f",$k); $val[] = $v;
		}
		$bingraph = array($bin,$val);
		return $bingraph;
	}

	//输出状态信息
	function printStats() {
		$s = "Statistics for histogram: ".$this->TITLE."\n";
		$s .= sprintf("N = %8d\t\tMin = %-8.4f\tMax = %-8.4f\tAvg = %-8.4f\n",$this->N,$this->MIN,$this->MAX,$this->AVG);
		$s .= sprintf("StDev = %-8.4f\tSum = %-8.4f\tSum^2 = %-8.4f\n",$this->STDV,$this->SUM,$this->SUM2);
		echo $s;
	}

	//输出统计结果
	function printBins() {
		echo "Number of bins: ".count($this->BINS)."\n";
		echo "BIN\tVAL\t\tFREQ\n";
		$maxbin = max($this->BINS);
		reset($this->BINS);
		for ($i = 0; $i < $this->NBINS; $i++) {
			list($key,$val) = each($this->BINS);
			echo sprintf("%d\t%-8.4f\t%-8d |%s\n",$i+1,$key,$val,$this->_printBar($val,$maxbin));
		}
	}

	//绘制*表示的柱状图的函数,内部使用
	function _printBar($val,$maxbin) {
		$fact = (float) ($maxbin > 40) ? 40/$maxbin : 1;
		$niter = (int) $val * $fact;
		$out = "";
		for ($i=0; $i<$niter; $i++) 
			$out .= "*";
		return $out;
	}

} //结束

?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -