Output image for graphs with no data (#11865)

* Output image for graphs with no data

* add font

* Fix graph error, try to detect rrd missing

* centralize graph_error
use it for graph_text_and_exit()

* Add SVG, right now the text can get a little big...

* fix style
This commit is contained in:
Tony Murray
2021-01-13 07:23:47 -06:00
committed by GitHub
parent ed41e6eedc
commit b07b81021a
7 changed files with 98 additions and 118 deletions

View File

@@ -591,6 +591,62 @@ function generate_port_thumbnail($port)
return generate_port_image($port);
}//end generate_port_thumbnail()
/**
* Create image to output text instead of a graph.
*
* @param string $text
* @param int[] $color
*/
function graph_error($text, $color = [128, 0, 0])
{
global $vars, $debug;
if (! $debug) {
set_image_type();
}
$width = $vars['width'] ?? 150;
$height = $vars['height'] ?? 60;
if (Config::get('webui.graph_type') === 'svg') {
$rgb = implode(', ', $color);
$font_size = 20;
$svg_x = 100;
$svg_y = min($font_size, $width ? (($height / $width) * $svg_x) : 1);
echo "<svg viewBox=\"0 0 $svg_x $svg_y\" xmlns=\"http://www.w3.org/2000/svg\"><text x=\"50%\" y=\"50%\" dominant-baseline=\"middle\" text-anchor=\"middle\" style=\"font-family: sans-serif; fill: rgb($rgb);\">$text</text></svg>";
} else {
$img = imagecreate($width, $height);
imagecolorallocatealpha($img, 255, 255, 255, 127); // transparent background
$px = ((imagesx($img) - 7.5 * strlen($text)) / 2);
$font = $width < 200 ? 3 : 5;
imagestring($img, $font, $px, ($height / 2 - 8), $text, imagecolorallocate($img, ...$color));
// Output the image
imagepng($img);
imagedestroy($img);
}
}
/**
* Output message to user in image format.
*
* @param string $text string to display
*/
function graph_text_and_exit($text)
{
global $vars;
if ($vars['showcommand'] == 'yes') {
echo $text;
return;
}
graph_error($text, [13, 21, 210]);
exit;
}
function print_port_thumbnail($args)
{
echo generate_port_link($args, generate_port_image($args));