2005-03-29 05:27:32 -05:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require 'tk'
|
|
|
|
require 'tkextlib/blt'
|
|
|
|
|
|
|
|
# Example of a pareto chart.
|
|
|
|
#
|
|
|
|
# The pareto chart mixes line and bar elements in the same graph.
|
|
|
|
# Each processing operating is represented by a bar element. The
|
|
|
|
# total accumulated defects is displayed with a single line element.
|
2009-03-05 22:56:38 -05:00
|
|
|
b = Tk::BLT::Barchart.new(:title=>'Defects Found During Inspection',
|
|
|
|
:font=>'Helvetica 12', :plotpady=>[12, 4],
|
2005-03-29 05:27:32 -05:00
|
|
|
:width=>'6i', :height=>'5i')
|
|
|
|
Tk::BLT::Table.add(Tk.root, b, :fill=>:both)
|
|
|
|
|
|
|
|
data = [
|
2009-03-05 22:56:38 -05:00
|
|
|
["Spot Weld", 82, 'yellow'],
|
|
|
|
["Lathe", 49, 'orange'],
|
|
|
|
["Gear Cut", 38, 'green'],
|
|
|
|
["Drill", 24, 'blue'],
|
|
|
|
["Grind", 17, 'red'],
|
|
|
|
["Lapping", 12, 'brown'],
|
|
|
|
["Press", 8, 'purple'],
|
|
|
|
["De-burr", 4, 'pink'],
|
|
|
|
["Packaging", 3, 'cyan'],
|
2005-03-29 05:27:32 -05:00
|
|
|
["Other", 12, 'magenta']
|
|
|
|
]
|
|
|
|
|
|
|
|
# Create an X-Y graph line element to trace the accumulated defects.
|
|
|
|
b.line_create('accum', :label=>'', :symbol=>:none, :color=>'red')
|
|
|
|
|
|
|
|
# Define a bitmap to be used to stipple the background of each bar.
|
|
|
|
pattern1 = Tk::BLT::Bitmap.define([ [4, 4], [1, 2, 4, 8] ])
|
|
|
|
|
|
|
|
# For each process, create a bar element to display the magnitude.
|
|
|
|
count = 0
|
|
|
|
sum = 0
|
|
|
|
ydata = [0]
|
|
|
|
xdata = [0]
|
|
|
|
labels = []
|
|
|
|
|
|
|
|
data.each{|label, value, color|
|
|
|
|
count += 1
|
2009-03-05 22:56:38 -05:00
|
|
|
b.element_create(label, :xdata=>count, :ydata=>value, :foreground=>color,
|
|
|
|
:relief=>:solid, :borderwidth=>1, :stipple=>pattern1,
|
2005-03-29 05:27:32 -05:00
|
|
|
:background=>'lightblue')
|
|
|
|
labels[count] = label
|
|
|
|
# Get the total number of defects.
|
|
|
|
sum += value
|
|
|
|
ydata << sum
|
|
|
|
xdata << count
|
|
|
|
}
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
# Configure the coordinates of the accumulated defects,
|
2005-03-29 05:27:32 -05:00
|
|
|
# now that we know what they are.
|
|
|
|
b.element_configure('accum', :xdata=>xdata, :ydata=>ydata)
|
|
|
|
|
|
|
|
# Add text markers to label the percentage of total at each point.
|
|
|
|
xdata.zip(ydata){|x, y|
|
|
|
|
percent = (y * 100.0) / sum
|
|
|
|
if x == 0
|
|
|
|
text = ' 0%'
|
|
|
|
else
|
|
|
|
text = '%.1f' % percent
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
b.marker_create(:text, :coords=>[x, y], :text=>text, :font=>'Helvetica 10',
|
2005-03-29 05:27:32 -05:00
|
|
|
:foreground=>'red4', :anchor=>:center, :yoffset=>-5)
|
|
|
|
}
|
|
|
|
|
|
|
|
# Display an auxillary y-axis for percentages.
|
2009-03-05 22:56:38 -05:00
|
|
|
b.axis_configure('y2', :hide=>false, :min=>0.0, :max=>100.0,
|
2005-03-29 05:27:32 -05:00
|
|
|
:title=>'Percentage')
|
|
|
|
|
|
|
|
# Title the y-axis
|
|
|
|
b.axis_configure('y', :title=>'Defects')
|
|
|
|
|
|
|
|
# Configure the x-axis to display the process names, instead of numbers.
|
2009-03-05 22:56:38 -05:00
|
|
|
b.axis_configure('x', :title=>'Process', :rotate=>90, :subdivisions=>0,
|
2005-03-29 05:27:32 -05:00
|
|
|
:command=>proc{|w, val|
|
|
|
|
val = val.round
|
|
|
|
labels[val]? labels[val]: val
|
|
|
|
})
|
|
|
|
|
|
|
|
# No legend needed.
|
|
|
|
b.legend_configure(:hide=>true)
|
|
|
|
|
|
|
|
# Configure the grid lines.
|
|
|
|
b.gridline_configure(:mapx=>:x, :color=>'lightblue')
|
|
|
|
|
|
|
|
Tk.mainloop
|