| [175] | 1 | package require BLT
|
|---|
| 2 | # --------------------------------------------------------------------------
|
|---|
| 3 | # Starting with Tcl 8.x, the BLT commands are stored in their own
|
|---|
| 4 | # namespace called "blt". The idea is to prevent name clashes with
|
|---|
| 5 | # Tcl commands and variables from other packages, such as a "table"
|
|---|
| 6 | # command in two different packages.
|
|---|
| 7 | #
|
|---|
| 8 | # You can access the BLT commands in a couple of ways. You can prefix
|
|---|
| 9 | # all the BLT commands with the namespace qualifier "blt::"
|
|---|
| 10 | #
|
|---|
| 11 | # blt::graph .g
|
|---|
| 12 | # blt::table . .g -resize both
|
|---|
| 13 | #
|
|---|
| 14 | # or you can import all the command into the global namespace.
|
|---|
| 15 | #
|
|---|
| 16 | # namespace import blt::*
|
|---|
| 17 | # graph .g
|
|---|
| 18 | # table . .g -resize both
|
|---|
| 19 | #
|
|---|
| 20 | # --------------------------------------------------------------------------
|
|---|
| 21 |
|
|---|
| 22 | namespace import blt::*
|
|---|
| 23 |
|
|---|
| 24 | #
|
|---|
| 25 | # Example of a pareto chart.
|
|---|
| 26 | #
|
|---|
| 27 | barchart .b \
|
|---|
| 28 | -title "Defects Found During Inspection" \
|
|---|
| 29 | -font {Helvetica 12} \
|
|---|
| 30 | -plotpady { 12 4 }
|
|---|
| 31 | table . .b -fill both
|
|---|
| 32 | set data {
|
|---|
| 33 | "Spot Weld" 82 yellow
|
|---|
| 34 | "Lathe" 49 orange
|
|---|
| 35 | "Gear Cut" 38 green
|
|---|
| 36 | "Drill" 24 blue
|
|---|
| 37 | "Grind" 17 red
|
|---|
| 38 | "Lapping" 12 brown
|
|---|
| 39 | "Press" 8 purple
|
|---|
| 40 | "De-burr" 4 pink
|
|---|
| 41 | "Packaging" 3 cyan
|
|---|
| 42 | "Other" 12 magenta
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | # Create an X-Y graph line element to trace the accumulated defects.
|
|---|
| 46 | .b line create accum -label "" -symbol none -color red
|
|---|
| 47 | # Define a bitmap to be used to stipple the background of each bar.
|
|---|
| 48 | bitmap define pattern1 { {4 4} {01 02 04 08} }
|
|---|
| 49 | # For each process, create a bar element to display the magnitude.
|
|---|
| 50 | set count 0
|
|---|
| 51 | set sum 0
|
|---|
| 52 | set ydata 0
|
|---|
| 53 | set xdata 0
|
|---|
| 54 | foreach { label value color } $data {
|
|---|
| 55 | incr count
|
|---|
| 56 | .b element create $label -xdata $count -ydata $value \
|
|---|
| 57 | -fg $color -relief solid -bd 1 -stipple pattern1 -bg lightblue
|
|---|
| 58 | set labels($count) $label
|
|---|
| 59 | # Keep a running total of defects
|
|---|
| 60 | set sum [expr $value + $sum]
|
|---|
| 61 | lappend ydata $sum
|
|---|
| 62 | lappend xdata $count
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | # Configure the coordinates of the accumulated defects,
|
|---|
| 66 | # now that we know what they are.
|
|---|
| 67 | .b line configure accum -xdata $xdata -ydata $ydata
|
|---|
| 68 | # Add text markers to label the percentage of total at each point.
|
|---|
| 69 | foreach x $xdata y $ydata {
|
|---|
| 70 | set percent [expr ($y * 100.0) / $sum]
|
|---|
| 71 | if { $x == 0 } {
|
|---|
| 72 | set text "0%"
|
|---|
| 73 | } else {
|
|---|
| 74 | set text [format %.1f $percent]
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | .b marker create text \
|
|---|
| 78 | -coords "$x $y" \
|
|---|
| 79 | -text $text \
|
|---|
| 80 | -anchor c \
|
|---|
| 81 | -yoffset -5
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | # Display an auxillary y-axis for percentages.
|
|---|
| 85 | .b axis configure y2 \
|
|---|
| 86 | -hide no \
|
|---|
| 87 | -min 0.0 \
|
|---|
| 88 | -max 100.0 \
|
|---|
| 89 | -title "Percentage"
|
|---|
| 90 | # Title the y-axis
|
|---|
| 91 | .b axis configure y -title "Defects"
|
|---|
| 92 | # Configure the x-axis to display the process names, instead of numbers.
|
|---|
| 93 | .b axis configure x \
|
|---|
| 94 | -title "Process" \
|
|---|
| 95 | -command FormatLabels \
|
|---|
| 96 | -rotate 90 \
|
|---|
| 97 | -subdivisions 0
|
|---|
| 98 |
|
|---|
| 99 | proc FormatLabels { widget value } {
|
|---|
| 100 | global labels
|
|---|
| 101 | set value [expr round($value)]
|
|---|
| 102 | if {[info exists labels($value)] } {
|
|---|
| 103 | return $labels($value)
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | return $value
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | # No legend needed.
|
|---|
| 110 | .b legend configure -hide yes
|
|---|
| 111 | # Configure the grid lines.
|
|---|
| 112 | .b grid configure -mapx x -color lightblue
|
|---|