1

It's a pretty specific use, but it can come in handy.

Imagine you have a different values to stack, some data varying a lot and some being almost constant. If you use the default order and the variable data is stacked under the constant data, the variable data will make the constant data have a very variable base. So if you stack first at bottom the less variable data, it could help.

Example: These two graphs show how to improve readibility by stacking deeper data that move the less, i.e. has the smaller standard deviation.

Default graphing doesn't help readibility
Default graphing could lead to bad readibility

Improved readibility when sorting by standard deviation
Improved readibility when sorting by standard deviation

lolesque
  • 10,693
  • 5
  • 42
  • 42

1 Answers1

1
  1. use rrdtool graph with PRINT command to get the standard deviation of the data sources (stdev_array)
  2. sort stdev_array
  3. graph by stacking in the order of stdev_array

Here is the code in PHP but any language can do it.
I'm using RRDtool 1.4.5
Don't forget to define $rrd_path (path to rrd file), $img_path (path where to write the image), $data_sources (an array of DS names, depends on how you build your RRD), $rrd_colors (an array of hexa colors).

$rrd_colors_count = count($rrd_colors);
$stdev_command = "rrdtool graph /dev/null ";
foreach ($data_sources as $index => $ds_name)
{
  $stdev_command .= "DEF:serv$index=$rrd_path:$ds_name:AVERAGE ";
  $stdev_command .= "VDEF:stdev$index=serv$index,STDEV PRINT:stdev$index:%lf ";
}
exec($stdev_command, $stdev_order, $ret);
if ($ret === 0)
{
  array_shift($stdev_order); // remove first useless line "0x0" (may depend on your rrdtool version?)
  asort($stdev_order); // sort by standard deviation keeping the indexes
}
else $stdev_order = $data_sources; // backup in case $stdev_command failed

$graph_command = "rrdtool graph $img_path ";
$graph_command .= "AREA:0 ";
foreach ($stdev_order as $index => $useless)
{
  $ds_name = $data_sources[$index];
  $graph_command .= "DEF:line$index=$rrd_path:$ds_name:AVERAGE ";
  $graph_command .= "STACK:line$index" . $rrd_colors[$index%$rrd_colors_count].' ';
}
exec($graph_command, $out, $ret);
// check $ret (and $out) to see if all is good
lolesque
  • 10,693
  • 5
  • 42
  • 42