meter.php 10.3 KB
Newer Older
Nahla Shiri's avatar
Nahla Shiri committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
<?php

class meter {


function __construct() {

}

function makeSVG($tag, $type, $value, $max, $min, $optimum, $low, $high) {
  $svg = '';
  if ($tag == 'meter') {

    if ($type=='2') {
	/////////////////////////////////////////////////////////////////////////////////////
	///////// CUSTOM <meter type="2">
	/////////////////////////////////////////////////////////////////////////////////////
	$h = 10;
	$w = 160;
	$border_radius = 0.143;		// Factor of Height

	$svg = '<?xml version="1.0" encoding="UTF-8"?>
	<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
	<svg width="'.$w.'px" height="'.$h.'px" viewBox="0 0 '.$w.' '.$h.'" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" ><g>


	<defs>
	<linearGradient id="GrGRAY" x1="0" y1="0" x2="0" y2="1" gradientUnits="boundingBox">
	<stop offset="0%" stop-color="rgb(222, 222, 222)" />
	<stop offset="20%" stop-color="rgb(232, 232, 232)" />
	<stop offset="25%" stop-color="rgb(232, 232, 232)" />
	<stop offset="100%" stop-color="rgb(182, 182, 182)" />
	</linearGradient>

	</defs>
';
	$svg .= '<rect x="0" y="0" width="'.$w.'" height="'.$h.'" fill="#f4f4f4" stroke="none" />';

	// LOW to HIGH region
	//if ($low && $high && ($low != $min || $high != $max)) {
	if ($low && $high) {
	  $barx = (($low-$min) / ($max-$min) ) * $w;
	  $barw = (($high-$low) / ($max-$min) ) * $w;
	  $svg .= '<rect x="'.$barx.'" y="0" width="'.$barw.'" height="'.$h.'" fill="url(#GrGRAY)" stroke="#888888" stroke-width="0.5px" />';
	}

	// OPTIMUM Marker (? AVERAGE)
	if ($optimum) {
	  $barx = (($optimum-$min) / ($max-$min) ) * $w;
	  $barw = $h/2;
	  $barcol = '#888888';
	  $svg .= '<rect x="'.$barx.'" y="0" rx="'.($h*$border_radius).'px" ry="'.($h*$border_radius).'px" width="'.$barw.'" height="'.$h.'" fill="'.$barcol.'" stroke="none" />';
	}

	// VALUE Marker
	if ($value) {
	  if ($min != $low && $value < $low) { $col = 'orange'; }
	  else if ($max != $high && $value > $high) { $col = 'orange'; }
	  else { $col = '#008800'; }
	  $cx = (($value-$min) / ($max-$min) ) * $w;
	  $cy = $h/2;
	  $rx = $h/3.5;
	  $ry = $h/2.2;
	  $svg .= '<ellipse fill="'.$col.'" stroke="#000000" stroke-width="0.5px" cx="'.$cx.'" cy="'.$cy.'" rx="'.$rx.'" ry="'.$ry.'"/>';
	}

	// BoRDER
	$svg .= '<rect x="0" y="0" width="'.$w.'" height="'.$h.'" fill="none" stroke="#888888" stroke-width="0.5px" />';

	$svg .= '</g></svg>';
    }
    else if ($type=='3') {
	/////////////////////////////////////////////////////////////////////////////////////
	///////// CUSTOM <meter type="2">
	/////////////////////////////////////////////////////////////////////////////////////
	$h = 10;
	$w = 100;
	$border_radius = 0.143;		// Factor of Height

	$svg = '<?xml version="1.0" encoding="UTF-8"?>
	<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
	<svg width="'.$w.'px" height="'.$h.'px" viewBox="0 0 '.$w.' '.$h.'" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" ><g>


	<defs>
	<linearGradient id="GrGRAY" x1="0" y1="0" x2="0" y2="1" gradientUnits="boundingBox">
	<stop offset="0%" stop-color="rgb(222, 222, 222)" />
	<stop offset="20%" stop-color="rgb(232, 232, 232)" />
	<stop offset="25%" stop-color="rgb(232, 232, 232)" />
	<stop offset="100%" stop-color="rgb(182, 182, 182)" />
	</linearGradient>

	</defs>
';
	$svg .= '<rect x="0" y="0" width="'.$w.'" height="'.$h.'" fill="#f4f4f4" stroke="none" />';

	// LOW to HIGH region
	if ($low && $high && ($low != $min || $high != $max)) {
	//if ($low && $high) {
	  $barx = (($low-$min) / ($max-$min) ) * $w;
	  $barw = (($high-$low) / ($max-$min) ) * $w;
	  $svg .= '<rect x="'.$barx.'" y="0" width="'.$barw.'" height="'.$h.'" fill="url(#GrGRAY)" stroke="#888888" stroke-width="0.5px" />';
	}

	// OPTIMUM Marker (? AVERAGE)
	if ($optimum) {
	  $barx = (($optimum-$min) / ($max-$min) ) * $w;
	  $barw = $h/2;
	  $barcol = '#888888';
	  $svg .= '<rect x="'.$barx.'" y="0" rx="'.($h*$border_radius).'px" ry="'.($h*$border_radius).'px" width="'.$barw.'" height="'.$h.'" fill="'.$barcol.'" stroke="none" />';
	}

	// VALUE Marker
	if ($value) {
	  if ($min != $low && $value < $low) { $col = 'orange'; }
	  else if ($max != $high && $value > $high) { $col = 'orange'; }
	  else { $col = 'orange'; }
	  $cx = (($value-$min) / ($max-$min) ) * $w;
	  $cy = $h/2;
	  $rx = $h/2.2;
	  $ry = $h/2.2;
	  $svg .= '<ellipse fill="'.$col.'" stroke="#000000" stroke-width="0.5px" cx="'.$cx.'" cy="'.$cy.'" rx="'.$rx.'" ry="'.$ry.'"/>';
	}

	// BoRDER
	$svg .= '<rect x="0" y="0" width="'.$w.'" height="'.$h.'" fill="none" stroke="#888888" stroke-width="0.5px" />';

	$svg .= '</g></svg>';
    }
    else {
	/////////////////////////////////////////////////////////////////////////////////////
	///////// DEFAULT <meter>
	/////////////////////////////////////////////////////////////////////////////////////
	$h = 10;
	$w = 50;
	$border_radius = 0.143;		// Factor of Height

	$svg = '<?xml version="1.0" encoding="UTF-8"?>
	<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
	<svg width="'.$w.'px" height="'.$h.'px" viewBox="0 0 '.$w.' '.$h.'" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" ><g>

	<defs>
	<linearGradient id="GrGRAY" x1="0" y1="0" x2="0" y2="1" gradientUnits="boundingBox">
	<stop offset="0%" stop-color="rgb(222, 222, 222)" />
	<stop offset="20%" stop-color="rgb(232, 232, 232)" />
	<stop offset="25%" stop-color="rgb(232, 232, 232)" />
	<stop offset="100%" stop-color="rgb(182, 182, 182)" />
	</linearGradient>

	<linearGradient id="GrRED" x1="0" y1="0" x2="0" y2="1" gradientUnits="boundingBox">
	<stop offset="0%" stop-color="rgb(255, 162, 162)" />
	<stop offset="20%" stop-color="rgb(255, 218, 218)" />
	<stop offset="25%" stop-color="rgb(255, 218, 218)" />
	<stop offset="100%" stop-color="rgb(255, 0, 0)" />
	</linearGradient>

	<linearGradient id="GrGREEN" x1="0" y1="0" x2="0" y2="1" gradientUnits="boundingBox">
	<stop offset="0%" stop-color="rgb(102, 230, 102)" />
	<stop offset="20%" stop-color="rgb(218, 255, 218)" />
	<stop offset="25%" stop-color="rgb(218, 255, 218)" />
	<stop offset="100%" stop-color="rgb(0, 148, 0)" />
	</linearGradient>

	<linearGradient id="GrBLUE" x1="0" y1="0" x2="0" y2="1" gradientUnits="boundingBox">
	<stop offset="0%" stop-color="rgb(102, 102, 230)" />
	<stop offset="20%" stop-color="rgb(238, 238, 238)" />
	<stop offset="25%" stop-color="rgb(238, 238, 238)" />
	<stop offset="100%" stop-color="rgb(0, 0, 128)" />
	</linearGradient>

	<linearGradient id="GrORANGE" x1="0" y1="0" x2="0" y2="1" gradientUnits="boundingBox">
	<stop offset="0%" stop-color="rgb(255, 186, 0)" />
	<stop offset="20%" stop-color="rgb(255, 238, 168)" />
	<stop offset="25%" stop-color="rgb(255, 238, 168)" />
	<stop offset="100%" stop-color="rgb(255, 155, 0)" />
	</linearGradient>
	</defs>

	<rect x="0" y="0" rx="'.($h*$border_radius).'px" ry="'.($h*$border_radius).'px" width="'.$w.'" height="'.$h.'" fill="url(#GrGRAY)" stroke="none" />
';

	if ($value) {
	  $barw = (($value-$min) / ($max-$min) ) * $w;
	  if ($optimum < $low) {
		if ($value < $low) { $barcol = 'url(#GrGREEN)'; }
		else if ($value > $high) { $barcol = 'url(#GrRED)'; }
		else { $barcol = 'url(#GrORANGE)'; }
	  }
	  else if ($optimum > $high) {
		if ($value < $low) { $barcol = 'url(#GrRED)'; }
		else if ($value > $high) { $barcol = 'url(#GrGREEN)'; }
		else { $barcol = 'url(#GrORANGE)'; }
	  }
	  else {
		if ($value < $low) { $barcol = 'url(#GrORANGE)'; }
		else if ($value > $high) { $barcol = 'url(#GrORANGE)'; }
		else { $barcol = 'url(#GrGREEN)'; }
	  }
	  $svg .= '<rect x="0" y="0" rx="'.($h*$border_radius).'px" ry="'.($h*$border_radius).'px" width="'.$barw.'" height="'.$h.'" fill="'.$barcol.'" stroke="none" />';
	}


	// Borders
	//$svg .= '<rect x="0" y="0" rx="'.($h*$border_radius).'px" ry="'.($h*$border_radius).'px" width="'.$w.'" height="'.$h.'" fill="none" stroke="#888888" stroke-width="0.5px" />';
	if ($value) {
	//  $svg .= '<rect x="0" y="0" rx="'.($h*$border_radius).'px" ry="'.($h*$border_radius).'px" width="'.$barw.'" height="'.$h.'" fill="none" stroke="#888888" stroke-width="0.5px" />';
	}


	$svg .= '</g></svg>';
    }
  }
  else {	// $tag == 'progress'

    if ($type=='2') {
	/////////////////////////////////////////////////////////////////////////////////////
	///////// CUSTOM <progress type="2">
	/////////////////////////////////////////////////////////////////////////////////////
    }
    else {
	/////////////////////////////////////////////////////////////////////////////////////
	///////// DEFAULT <progress>
	/////////////////////////////////////////////////////////////////////////////////////
	$h = 10;
	$w = 100;
	$border_radius = 0.143;		// Factor of Height

	if ($value or $value==='0') {
		$fill = 'url(#GrGRAY)';
	}
	else {
		$fill = '#f8f8f8';
	}

	$svg = '<svg width="'.$w.'px" height="'.$h.'px" viewBox="0 0 '.$w.' '.$h.'"><g>

	<defs>
	<linearGradient id="GrGRAY" x1="0" y1="0" x2="0" y2="1" gradientUnits="boundingBox">
	<stop offset="0%" stop-color="rgb(222, 222, 222)" />
	<stop offset="20%" stop-color="rgb(232, 232, 232)" />
	<stop offset="25%" stop-color="rgb(232, 232, 232)" />
	<stop offset="100%" stop-color="rgb(182, 182, 182)" />
	</linearGradient>

	<linearGradient id="GrGREEN" x1="0" y1="0" x2="0" y2="1" gradientUnits="boundingBox">
	<stop offset="0%" stop-color="rgb(102, 230, 102)" />
	<stop offset="20%" stop-color="rgb(218, 255, 218)" />
	<stop offset="25%" stop-color="rgb(218, 255, 218)" />
	<stop offset="100%" stop-color="rgb(0, 148, 0)" />
	</linearGradient>

	</defs>

	<rect x="0" y="0" rx="'.($h*$border_radius).'px" ry="'.($h*$border_radius).'px" width="'.$w.'" height="'.$h.'" fill="'.$fill.'" stroke="none" />
';

	if ($value) {
	  $barw = (($value-$min) / ($max-$min) ) * $w;
	  $barcol = 'url(#GrGREEN)';
	  $svg .= '<rect x="0" y="0" rx="'.($h*$border_radius).'px" ry="'.($h*$border_radius).'px" width="'.$barw.'" height="'.$h.'" fill="'.$barcol.'" stroke="none" />';
	}


	// Borders
	$svg .= '<rect x="0" y="0" rx="'.($h*$border_radius).'px" ry="'.($h*$border_radius).'px" width="'.$w.'" height="'.$h.'" fill="none" stroke="#888888" stroke-width="0.5px" />';
	if ($value) {
	//  $svg .= '<rect x="0" y="0" rx="'.($h*$border_radius).'px" ry="'.($h*$border_radius).'px" width="'.$barw.'" height="'.$h.'" fill="none" stroke="#888888" stroke-width="0.5px" />';
	}


	$svg .= '</g></svg>';

    }
  }

  return $svg;
}


}	// end of class

?>