I've played around a bit with the RRD graphing and implemented a quick gradient support for them. It's based on several other scripts and if someone is cabable of re-writing the "colourBrightness" function to be GPLv2 I would greatly appreciate it. The code changes ALL AREA related graphs dynamically.
Nothing fency and a quick&dirty hack, but maybe someone still likes it
Look here for additional information: http://blog.network-outsourcing.de/2015 ... ti-graphs/
Here are some samples graphs: Here's the code.
You can change the effect of the gradient by changing the following line accoringly:
$end_color = colourBrightness("#" . $graph_item["hex"],-0.4); // End color is a 40% (0.4) darkened (negative number) version of the original color
File: lib/rrd.php, Line: 1375
Code: Select all
if (preg_match("/^AREA$/", $graph_item_types{$graph_item["graph_type_id"]})) {
$graph_item_stack_type = $graph_item_types{$graph_item["graph_type_id"]};
$graph_variables["text_format"][$graph_item_id] = str_replace(":", "\:", $graph_variables["text_format"][$graph_item_id]); /* escape colons */
// $txt_graph_items .= $graph_item_types{$graph_item["graph_type_id"]} . ":" . $data_source_name . $graph_item_color_code . ":" . cacti_escapeshellarg($graph_variables["text_format"][$graph_item_id] . $hardreturn[$graph_item_id]) . " ";
$end_color = colourBrightness("#" . $graph_item["hex"],-0.4); // End color is a 40% (0.4) darkened (negative number) version of the original color
$txt_graph_items .= gradient($data_source_name,$graph_item_color_code,$end_color.$graph_item["alpha"],cacti_escapeshellarg($graph_variables["text_format"][$graph_item_id] . $hardreturn[$graph_item_id]),20,false,$graph_item["alpha"]);
$need_rrd_nl = FALSE;
}elseif (preg_match("/^LINE[123]$/", $graph_item_types{$graph_item["graph_type_id"]})) {
$graph_item_stack_type = $graph_item_types{$graph_item["graph_type_id"]};
$graph_variables["text_format"][$graph_item_id] = str_replace(":", "\:", $graph_variables["text_format"][$graph_item_id]); /* escape colons */
$txt_graph_items .= $graph_item_types{$graph_item["graph_type_id"]} . ":" . $data_source_name . $graph_item_color_code . ":" . cacti_escapeshellarg($graph_variables["text_format"][$graph_item_id] . $hardreturn[$graph_item_id]) . " ";
}elseif ($graph_item_types{$graph_item["graph_type_id"]} == "STACK") {
Code: Select all
// from: http://lab.clearpixel.com.au/2008/06/darken-or-lighten-colours-dynamically-using-php/ License: unknown
// The code is based on the CSSColor project of the same person from here: http://www.barelyfitz.com/projects/csscolor/ License: GPLv2
function colourBrightness($hex, $percent) {
// Work out if hash given
$hash = '';
if (stristr($hex,'#')) {
$hex = str_replace('#','',$hex);
$hash = '#';
}
/// HEX TO RGB
$rgb = array(hexdec(substr($hex,0,2)), hexdec(substr($hex,2,2)), hexdec(substr($hex,4,2)));
//// CALCULATE
for ($i=0; $i<3; $i++) {
// See if brighter or darker
if ($percent > 0) {
// Lighter
$rgb[$i] = round($rgb[$i] * $percent) + round(255 * (1-$percent));
} else {
// Darker
$positivePercent = $percent - ($percent*2);
$rgb[$i] = round($rgb[$i] * (1-$positivePercent)); // round($rgb[$i] * (1-$positivePercent));
}
// In case rounding up causes us to go to 256
if ($rgb[$i] > 255) {
$rgb[$i] = 255;
}
}
//// RBG to Hex
$hex = '';
for($i=0; $i < 3; $i++) {
// Convert the decimal digit to hex
$hexDigit = dechex($rgb[$i]);
// Add a leading zero if necessary
if(strlen($hexDigit) == 1) {
$hexDigit = "0" . $hexDigit;
}
// Append to the hex string
$hex .= $hexDigit;
}
return $hash.$hex;
}
// from https://github.com/lingej/pnp4nagios/blob/master/share/pnp/application/helpers/rrd.php / License: GPLv2
function gradient($vname=FALSE, $start_color='#0000a0', $end_color='#f0f0f0', $label=FALSE, $steps=20, $lower=FALSE, $alpha='FF'){
$label = preg_replace("/'/","",$label);
if($vname === FALSE){
// throw new Kohana_exception("rrd::". __FUNCTION__ . "() First Parameter 'vname' is missing");
}
if(preg_match('/^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i',$start_color,$matches)){
$r1=hexdec($matches[1]);
$g1=hexdec($matches[2]);
$b1=hexdec($matches[3]);
}else{
// throw new Kohana_exception("rrd::". __FUNCTION__ . "() Wrong Color Format: '".$start_color."'");
}
if(preg_match('/^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i',$end_color,$matches)){
$r2=hexdec($matches[1]);
$g2=hexdec($matches[2]);
$b2=hexdec($matches[3]);
}else{
// throw new Kohana_exception("rrd::". __FUNCTION__ . "() Wrong Color Format: '".$end_color."'");
}
$diff_r=$r2-$r1;
$diff_g=$g2-$g1;
$diff_b=$b2-$b1;
$spline = "";
$spline_vname = "var".substr(sha1(rand()),1,4);
$vnamet = $vname.substr(sha1(rand()),1,4);
if(preg_match('/^([0-9]{1,3})%$/', $lower, $matches)){
// if($matches[1] > 100)
// throw new Kohana_exception("rrd::". __FUNCTION__ . "() Lower gradient start > 100% is not allowed: '".$lower."'");
$lower = $matches[1];
$spline .= sprintf("CDEF:%sminimum=%s,100,/,%d,* ".RRD_NL, $vnamet, $vname, $lower);
}elseif(preg_match('/^([0-9]+)$/', $lower, $matches)){
$lower = $matches[1];
$spline .= sprintf("CDEF:%sminimum=%s,%d,- ".RRD_NL, $vnamet, $vname, $lower);
}else{
$lower = 0;
$spline .= sprintf("CDEF:%sminimum=%s,%s,- ".RRD_NL, $vnamet, $vname, $vname);
}
for ($i=$steps; $i>0; $i--){
$spline .= sprintf("CDEF:%s%d=%s,%sminimum,-,%d,/,%d,*,%sminimum,+ ".RRD_NL,$spline_vname,$i,$vname,$vnamet,$steps,$i,$vnamet);
}
// We don't use alpha blending for the area right now
$alpha = 'ff';
for ($i=$steps; $i>0; $i--){
$factor=$i / $steps;
$r=round($r1 + $diff_r * $factor);
$g=round($g1 + $diff_g * $factor);
$b=round($b1 + $diff_b * $factor);
if (($i==$steps) and ($label!=FALSE) and (strlen($label)>2) ){
$spline .= sprintf("AREA:%s%d#%02X%02X%02X%s:\"%s\" ".RRD_NL, $spline_vname,$i,$r,$g,$b,$alpha,$label);
}else{
$spline .= sprintf("AREA:%s%d#%02X%02X%02X%s ".RRD_NL, $spline_vname,$i,$r,$g,$b,$alpha);
}
}
$spline .= sprintf("AREA:%s%d#%02X%02X%02X%s ".RRD_NL, $spline_vname,$steps,$r2,$g2,$b2,'00',$label);
return $spline;
}