Sự kiện

Vẽ đồ thị bằng css và php

Danh mục: PHP & MySQL
 

bar graphs, lập trình php, php code, thu thuat php, đồ thị

Nếu bạn cần một ví dụ minh họa về việc vẽ đồ thị bằng php và css thì hàm chức năng mà mình chia sẻ trong bài viết này sẽ giúp ích cho bạn. Các bạn chỉ cần copy đoạn code sau đây :

function drawCSSGraph($data, $total, $settings='height=20 width=300 color=#c0c0c0'){

//Emulate the symfony style of using settings

if(is_array($settings)){

$width = (isset($settings['width']))?$settings['width']:300;

$height = (isset($settings['height']))?$settings['height']:20;

$color = (isset($settings['color']))?$settings['color']:'#c0c0c0';

} else {

$settings = explode(' ', $settings);

foreach($settings as $setting){

$tmp = explode('=', $setting);

$$tmp[0] = $tmp[1];

if(!isset($width)) $width = 300;

if(!isset($height)) $height = 20;

if(!isset($color)) $color = '#c0c0c0';

}

}

if(count($data) > 1){

$HTMLoutput = '';

foreach($data as $label=>$var){

$labelv = preg_replace('/\[var\]/', $var, $label);

$HTMLoutput .= drawCSSGraph(array($labelv=>$var), $total, $settings);

}

return $HTMLoutput;

} else {

$variable = $data[key($data)];

$label = preg_replace('/\[var\]/', $variable, key($data));

return

'<div><span>'.$label.'</span>

<div style="width:'.$width.'px;height:'.$height.'px;border:1px solid black;padding:1px;">

<div class=\'bargraph\' style=\''.

(($width > $height)?'width':'height').':'.

(($variable/$total)*($width > $height?$width:$height)).'px;background-color:'.$color.';\'></div>

</div>

</div>'."\n";

}

}

Và sau đây là ví dụ minh họa :

$someData = array('Oranges'=>4, 'Apples'=>10);

$total = 14;

echo drawCSSGraph($someData, $total);

 



BÀI VIẾT KHÁC
CODE GỢI Ý CHO BẠN
BÌNH LUẬN

BẢN QUYỀN ®
MANGUON.COM đã đăng kí quyền tác giả: AR1682/GP-STTTP
Mọi chia sẻ nội dung thuộc tác quyền của MANGUON.COM phải ghi rõ "Nguồn MANGUON.COM"