Debian Codegraph @

examples/pareto.tcl

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