source: trunk/kitgen/8.x/blt/pareto.tcl@ 181

Last change on this file since 181 was 175, checked in by demin, 12 years ago

initial commit

File size: 2.8 KB
Line 
1package 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
22namespace import blt::*
23
24#
25# Example of a pareto chart.
26#
27barchart .b \
28-title "Defects Found During Inspection" \
29-font {Helvetica 12} \
30-plotpady { 12 4 }
31table . .b -fill both
32set 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.
48bitmap define pattern1 { {4 4} {01 02 04 08} }
49# For each process, create a bar element to display the magnitude.
50set count 0
51set sum 0
52set ydata 0
53set xdata 0
54foreach { label value color } $data {
55incr count
56.b element create $label -xdata $count -ydata $value \
57-fg $color -relief solid -bd 1 -stipple pattern1 -bg lightblue
58set labels($count) $label
59# Keep a running total of defects
60set sum [expr $value + $sum]
61lappend ydata $sum
62lappend 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.
69foreach x $xdata y $ydata {
70set percent [expr ($y * 100.0) / $sum]
71if { $x == 0 } {
72set text "0%"
73} else {
74set 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
99proc FormatLabels { widget value } {
100global labels
101set value [expr round($value)]
102if {[info exists labels($value)] } {
103return $labels($value)
104}
105
106return $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
Note: See TracBrowser for help on using the repository browser.