# if no parms are supplied, then use some defaults load_response if {![info exists response(title)]} { set title "Default" } else { regsub -all {\[|\]} $response(title) {} title # set title $response(title) } if {![info exists response(curvalue)]} { set curvalue 78 } else { regsub -all {[^0-9|-|+|.]} $response(curvalue) {} curvalue # set curvalue $response(curvalue) } # initial image and cont definitions set image [gd create 150 150] set white [gd color new $image 255 255 255] set black [gd color new $image 0 0 0] set green [gd color new $image 0 255 0] set yellow [gd color new $image 255 255 0] set red [gd color new $image 255 0 0] set blue [gd color new $image 0 0 255] set radiansPerDegree 0.0174533 #====================== Some Procs ================= proc draw_face {} { global lowest highest warn degreesAround global faceColor warnColor normalColor global dialSize xOrigin yOrigin global fontCharWidth fontCharHeight title global image set warnRatio [expr ($warn -$lowest + 0.0) / ($highest - $lowest)] set warnArcAngle [expr int(180 + $degreesAround * $warnRatio)] loop i 0 3 { gd arc $image $faceColor $xOrigin $yOrigin [expr $dialSize + $i] [expr $dialSize + $i] 180 $warnArcAngle gd arc $image $warnColor $xOrigin $yOrigin [expr $dialSize + $i] [expr $dialSize + $i] $warnArcAngle 0 } gd text $image $faceColor small [expr $xOrigin - $dialSize / 2 - [clength $lowest] * $fontCharWidth / 2 ] [expr $yOrigin + 2] $lowest gd text $image $faceColor small [expr $xOrigin + $dialSize / 2 - [clength $lowest] * $fontCharWidth / 2 ] [expr $yOrigin + 2] $highest if {$title != ""} { gd text $image $faceColor small [expr $xOrigin - ($fontCharWidth * [clength $title] / 2)] [expr $yOrigin - $dialSize / 2 - $fontCharHeight - 4] $title } } proc draw_needle {value} { global lowest highest warn degreesAround global faceColor warnColor normalColor global dialSize xOrigin yOrigin global fontCharWidth fontCharHeight title global radiansPerDegree image set ratio [expr ($value - $lowest + 0.0) / $highest] set angle [expr 180 - ($ratio * $degreesAround)] set warnRatio [expr ($warn -$lowest + 0.0) / ($highest - $lowest)] if {$ratio > $warnRatio} { set needleColor $warnColor } else { set needleColor $normalColor } set needleArcAngle [expr int(180 + $degreesAround * $ratio)] gd arc $image $needleColor $xOrigin $yOrigin [expr $dialSize - 8] [expr $dialSize - 8] 180 $needleArcAngle set cords {} set needleX [expr int($xOrigin + cos($angle * $radiansPerDegree) * ($dialSize - 12) / 2)] set needleY [expr int($yOrigin - sin($angle * $radiansPerDegree) * ($dialSize - 12) / 2)] gd line $image $needleColor $xOrigin $yOrigin $needleX $needleY gd text $image $needleColor small [expr $xOrigin - [clength $value] * $fontCharWidth / 2] [expr $yOrigin + 2] $value } #=================== Some more cont values and vars =================== set lowest 0 set highest 100 set warn 85 if {[info exists response(max)]} { set highest $response(max) set warn [expr $highest - [expr $highest / 5]] } set degreesAround 180 set faceColor $black set warnColor $red set normalColor $blue set dialSize 50 set xOrigin 75 set yOrigin 75 set fontCharWidth 6 set fontCharHeight 12 #===================== Draw the gauge and done ============== draw_face draw_needle $curvalue image_expire 0 gd writeGIF $image $imageFile gd destroy $image