tk-parallel-coordinates 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Chris Lee, PhD
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = tk-parallel-coordinates
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to tk-parallel-coordinates
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Chris Lee, PhD. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1 @@
1
+ require 'tk-parallel-coordinates/base.rb'
@@ -0,0 +1,278 @@
1
+ # DESCRIPTION: provides a TK widget of a parallel coordinate plot.
2
+
3
+ # Chris' note: My memory tells me that I lifted the basic design of this from somewhere on the web, but I don't remember where. My guess is that I added more than 80% new functionality.
4
+
5
+ # This program is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ module Tk
18
+ class ParallelCoordinates
19
+ STATE_NORMAL = 0
20
+ STATE_FILTERED = 1
21
+ STATE_SELECTED = 2
22
+ STATE_CURRENT = 3
23
+ STATES = [ 'normal', 'filtered', 'selected', 'current' ]
24
+
25
+ @@default_colors = {
26
+ 'labels' => 'grey75',
27
+ 'axis' => 'steelblue',
28
+ 'labels' => 'steelblue',
29
+ 'selector' => 'red',
30
+ 'state' => [ 'grey70', 'grey40', '#999900', 'purple' ]
31
+ }
32
+ @@scales_in = {
33
+ 'lin' => proc { |x| x },
34
+ 'log' => proc { |x| (x<=0) ? 0 : Math.log(x) },
35
+ 'log2' => proc { |x| (x<=0) ? 0 : Math.log(x)/Math.log(2) },
36
+ 'log10' => proc { |x| Math.log10(x) },
37
+ 'sqrt' => proc { |x| Math.sqrt(x) },
38
+ '3rt' => proc { |x| x ** (1/3.0) },
39
+ }
40
+ @@scales_out = {
41
+ 'lin' => proc { |x| x },
42
+ 'log' => proc { |x| Math.exp(x) },
43
+ 'log2' => proc { |x| 2 ** x },
44
+ 'log10' => proc { |x| 10 ** x },
45
+ 'sqrt' => proc { |x| x ** 2 },
46
+ '3rt' => proc { |x| x ** 3 },
47
+ }
48
+
49
+ def pack(*args)
50
+ @canvas.pack(*args)
51
+ end
52
+
53
+ def update
54
+ @tuples.each do |k,t|
55
+ @canvas.itemconfigure k, :fill => @colors['state'][t[0]]
56
+ end
57
+ end
58
+
59
+ def set_tuple_state(key,state)
60
+ @canvas.itemconfigure key, :fill => @colors['state'][state]
61
+ @tuples[key][0] = state
62
+ @canvas.raise key, 'all'
63
+ end
64
+
65
+ def addtuple(key,state,tuple)
66
+ if tuple.length != @model.length
67
+ puts "grr... what do I do now? the tuple you gave me is a different length than the number of axes I have"
68
+ return
69
+ end
70
+ axis = 0
71
+ cx = cy = 0
72
+ tuple.each do |item|
73
+ x = axis2x(axis)
74
+ y = item2y(axis, item)
75
+ if cx > 0
76
+ TkcLine.new(@canvas, cx, cy, x, y, :tags => [ key ], :fill => @colors['state'][state] )
77
+ end
78
+ cx = x
79
+ cy = y
80
+ axis += 1
81
+ end
82
+ @tuples[key] = [state,tuple]
83
+ end
84
+
85
+ def addtuples(tuples)
86
+ tuples.each do |tuple|
87
+ addtuple(tuple)
88
+ end
89
+ end
90
+
91
+ def axis2x(axis)
92
+ @model[axis][:x]
93
+ end
94
+
95
+ def item2y(axis, item)
96
+ m = @model[axis]
97
+ if m[:type] == 'range'
98
+ scale = m[:scale]
99
+ s_in = @@scales_in[scale]
100
+ s_out = @@scales_out[scale]
101
+ min = s_in.call m[:min]
102
+ max = s_in.call m[:max]
103
+ del = max - min
104
+ y = @top_margin + (@draw_height * ((s_in.call(item) - min)/(del - min)))
105
+ return y
106
+ elsif m[:type] == 'list'
107
+ c = 0
108
+ m[:list].each do |i|
109
+ break if item == i
110
+ c += 1
111
+ end
112
+ y = @top_margin + (@draw_height * (c.to_f/m[:list].length))
113
+ return y
114
+ elsif m[:type] == 'date'
115
+ raise "I must have fmt_in and fmt_out for a time axis. Use the strftime codes for formatting. E.g., %s => unix timestamp, %Y-%m-%d => SQL date (2007-12-12)" if m[:ifmt] == nil
116
+ min = Date.strptime(m[:min],m[:ifmt])
117
+ max = Date.strptime(m[:max],m[:ifmt])
118
+ del = (max - min).to_i
119
+ y = @top_margin + (@draw_height * ((Date.strptime(item,m[:ifmt]) - min)/(del - min)))
120
+ return y
121
+ end
122
+ end
123
+
124
+ def draw_axis
125
+ axes = @model.length
126
+ axes_spacing = @draw_width / (axes - 1.0)
127
+ offset = @left_margin
128
+ axis = 0
129
+ @model.each do |m|
130
+ m[:x] = offset
131
+ #puts "#{offset} #{@top_margin} #{offset} #{@bottom_margin}"
132
+ TkcLine.new(@canvas, offset, @top_margin, offset, @bottom_margin, :tags => ['axis'], :fill => @colors['axis'])
133
+ offset += axes_spacing
134
+ # lin, log, log10, sqrt, 3rt
135
+ if m[:type] == 'range'
136
+ scale = m[:scale]
137
+ s_in = @@scales_in[scale]
138
+ s_out = @@scales_out[scale]
139
+ min = s_in.call m[:min]
140
+ max = s_in.call m[:max]
141
+ del = max - min
142
+ if m[:items]
143
+ m[:items].each do |itemx|
144
+ item = s_in.call itemx
145
+ y = @top_margin + (@draw_height * ((item - min)/(del - min)))
146
+ text = itemx
147
+ text = sprintf m[:ofmt], text if m[:ofmt]
148
+ TkcText.new(@canvas, m[:x], y, :text => text, :anchor => (axis==0) ? 'e' : 'w', :fill => @colors['labels'], :tags => ['axislabel'])
149
+ end
150
+ else
151
+ points = m[:points] || 20.0
152
+ step = del / points.to_f
153
+ item = min
154
+ items = []
155
+ ctext = nil
156
+ (0..points).each do |i|
157
+ y = @top_margin + (@draw_height * ((item - min)/(del - min)))
158
+ text = s_out.call(item)
159
+ text = sprintf m[:ofmt], text if m[:ofmt]
160
+ unless text == ctext
161
+ TkcText.new(@canvas, m[:x], y, :text => text, :anchor => (axis==0) ? 'e' : 'w', :fill => @colors['labels'], :tags => ['axislabel'])
162
+ end
163
+ ctext = text
164
+ items.push item
165
+ item += step
166
+ end
167
+ m[:items] = items
168
+ end
169
+ elsif m[:type] == 'list'
170
+ # skip helps to mitigate overlap. 10.0 is what I assume the font size to be
171
+ skip = (((m[:list].length * 10.0)/@draw_height) + 1).to_i
172
+ item = 0
173
+ items = []
174
+ m[:list].each do |i|
175
+ if item % skip == 0
176
+ y = @top_margin + (@draw_height * (item.to_f/m[:list].length))
177
+ i = sprintf m[:ofmt], i if m[:ofmt]
178
+ TkcText.new(@canvas, m[:x], y, :text => i, :anchor => (axis==0) ? 'e' : 'w', :fill => @colors['labels'], :tags => ['axislabel'])
179
+ items.push i
180
+ end
181
+ item += 1
182
+ end
183
+ elsif m[:type] == 'date'
184
+ min = Date.strptime(m[:min],m[:ifmt])
185
+ max = Date.strptime(m[:max],m[:ifmt])
186
+ item = min
187
+ del = (max - min).to_i
188
+ points = m[:points] || 20.0
189
+ step = del / points.to_f
190
+ (0..points).each do |i|
191
+ y = @top_margin + (@draw_height * (item - min)/(del - min))
192
+ text = item.strftime m[:ofmt]
193
+ TkcText.new(@canvas, m[:x], y, :text => text, :anchor => (axis==0) ? 'e' : 'w', :fill => @colors['labels'], :tags => ['axislabel'])
194
+ item += step
195
+ end
196
+ end
197
+ axis += 1
198
+ end
199
+ end
200
+
201
+ def set_select_cb(callback)
202
+ @select_cb = callback
203
+ end
204
+
205
+ def do_press(x, y)
206
+ @start_x = x
207
+ @start_y = y
208
+ @current_rect = TkcRectangle.new(@canvas, x, y, x, y, :dash => '-', :outline => @colors['selector'])
209
+ end
210
+
211
+ def do_motion(x, y)
212
+ if @current_rect
213
+ @current_rect.coords @start_x, @start_y, x, y
214
+ @canvas.itemconfigure 'selected', :fill => @colors['state'][STATE_NORMAL]
215
+ @canvas.dtag 'selected'
216
+ @canvas.addtag_overlapping 'selected', @start_x, @start_y, x, y
217
+ @canvas.itemconfigure 'selected', :fill => @colors['state'][STATE_SELECTED]
218
+ @current_rect.fill = ''
219
+ @canvas.itemconfigure 'axis', :fill => @colors['axis']
220
+ @canvas.itemconfigure 'axislabel', :fill => @colors['labels']
221
+ # the below is slow, we need to find a faster way
222
+ @tuples.each do |k,t|
223
+ if t[0] == STATE_FILTERED
224
+ @canvas.itemconfigure k, :fill => @colors['state'][STATE_FILTERED]
225
+ end
226
+ end
227
+ end
228
+ end
229
+
230
+ def do_release(x, y)
231
+ if @current_rect
232
+ #@canvas.addtag_overlapping 'selected', @start_x, @start_y, x, y
233
+ #@canvas.itemconfigure 'selected', :fill => @colors['state'][STATE_SELECTED]
234
+ @current_rect.delete
235
+ @current_rect = nil
236
+ tuples = []
237
+ # stopped here BOOKMARK
238
+ items = @canvas.find_withtag 'selected'
239
+ items.each do |item|
240
+ item.gettags.each do |tag|
241
+ if tag =~ /\|/ and @tuples[tag][0] != STATE_FILTERED
242
+ tuples.push( tag.split(/\|/) )
243
+ end
244
+ end
245
+ end
246
+ @select_cb.call(tuples) if @select_cb and tuples.length > 0
247
+ end
248
+ end
249
+
250
+ def setPalette(*args)
251
+ return unless args.length > 0
252
+ args[0].each do |k,v|
253
+ @colors[k] = v
254
+ end
255
+ end
256
+
257
+ def initialize(parent, h, w, model)
258
+ @model = model
259
+ @canvas = TkCanvas.new(parent) {
260
+ height h
261
+ width w
262
+ }
263
+ @canvas.pack
264
+ @left_margin = 50
265
+ @right_margin = w - 90
266
+ @top_margin = 10
267
+ @bottom_margin = h - 10
268
+ @draw_width = @right_margin - @left_margin
269
+ @draw_height = @bottom_margin - @top_margin
270
+ @canvas.bind("1", proc{|e| do_press(e.x, e.y)})
271
+ @canvas.bind("B1-Motion", proc{|x, y| do_motion(x, y)}, "%x %y")
272
+ @canvas.bind("ButtonRelease-1", proc{|x, y| do_release(x, y)}, "%x %y")
273
+ @colors = @@default_colors
274
+ @tuples = {}
275
+ draw_axis
276
+ end
277
+ end
278
+ end
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ require 'tk'
14
+
15
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
16
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
17
+ require 'tk-parallel-coordinates'
18
+
19
+ class Test::Unit::TestCase
20
+ end
@@ -0,0 +1,38 @@
1
+ require 'helper'
2
+
3
+ class TestTkParallelCoordinates < Test::Unit::TestCase
4
+ should "instantiate and display a parallel coordinate plot with nominal (on a cube root axis) and ordinal data" do
5
+ root = TkRoot.new() {
6
+ title "Tk::ParallelCoordinates Test"
7
+ protocol('WM_DELETE_WINDOW', proc{ exit })
8
+ }
9
+ root.bind('Control-c', proc{ exit })
10
+ left_frame = TkFrame.new(root)
11
+ left_frame.grid(:row=>0,:column=>0,:sticky=>'new')
12
+ time_min = Time.now.to_i - (7*24*60*60)
13
+ time_max = Time.now.to_i
14
+ model = [
15
+ {
16
+ :name => 'Port',
17
+ :type => 'range',
18
+ :scale => '3rt',
19
+ :min => 0,
20
+ :max => 65535,
21
+ :ofmt => '%d',
22
+ #:items => [1,22,80,137,443,1024,5900,6667,31337,65335]
23
+ },
24
+ {
25
+ :name => 'Host',
26
+ :type => 'list',
27
+ :list => ['192.168.0.1','192.168.0.2','192.168.0.2','192.168.0.3']
28
+ }
29
+ ]
30
+ pcp = Tk::ParallelCoordinates.new(left_frame, 500, 360, model)
31
+ [[53,'192.168.0.1'],[53,'192.168.0.2'],[80,'192.168.0.2'],[43099,'192.168.0.3']].each do |t|
32
+ key = t[0].to_s+":"+t[1]
33
+ pcp.addtuple(key,Tk::ParallelCoordinates::STATE_NORMAL,t)
34
+ end
35
+ Tk.mainloop()
36
+ assert(true)
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tk-parallel-coordinates
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
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 W/in8Mm/R2fkVziSdzqChtw/4Z4bW3c+RF7TgX6SP5cKxNAfKmAPuItcs2Y+7bdS
34
+ hr/FktVtT2iAmISRnlEbdaTpfl6N2ZWNT83khV6iOs5xRkX/+0e+GgAv9mE6nqr1
35
+ AkuDXMhposxcnFZUrZ3UtMPEe/JnyP7Vv6pvr3qtZm8FidFZU91+rX/fwdyBU8RP
36
+ /5l8uLWXXNt1wEbtu4N1I66LwTK2iRrQZE8XtlgZGbxYDFUkiurq3OafF2YwRs6W
37
+ 6yhklP75
38
+ -----END CERTIFICATE-----
39
+
40
+ date: 2011-03-06 00:00:00 -05:00
41
+ default_executable:
42
+ dependencies:
43
+ - !ruby/object:Gem::Dependency
44
+ version_requirements: &id001 !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ hash: 3
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ requirement: *id001
54
+ prerelease: false
55
+ name: shoulda
56
+ type: :development
57
+ - !ruby/object:Gem::Dependency
58
+ version_requirements: &id002 !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ~>
62
+ - !ruby/object:Gem::Version
63
+ hash: 23
64
+ segments:
65
+ - 1
66
+ - 0
67
+ - 0
68
+ version: 1.0.0
69
+ requirement: *id002
70
+ prerelease: false
71
+ name: bundler
72
+ type: :development
73
+ - !ruby/object:Gem::Dependency
74
+ version_requirements: &id003 !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ~>
78
+ - !ruby/object:Gem::Version
79
+ hash: 7
80
+ segments:
81
+ - 1
82
+ - 5
83
+ - 2
84
+ version: 1.5.2
85
+ requirement: *id003
86
+ prerelease: false
87
+ name: jeweler
88
+ type: :development
89
+ - !ruby/object:Gem::Dependency
90
+ version_requirements: &id004 !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 3
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ requirement: *id004
100
+ prerelease: false
101
+ name: rcov
102
+ type: :development
103
+ description: This provides a rich Tk widget for displaying data across multiple axis with scaling.
104
+ email: rubygems@chrislee.dhs.org
105
+ executables: []
106
+
107
+ extensions: []
108
+
109
+ extra_rdoc_files:
110
+ - LICENSE.txt
111
+ - README.rdoc
112
+ files:
113
+ - lib/tk-parallel-coordinates.rb
114
+ - lib/tk-parallel-coordinates/base.rb
115
+ - LICENSE.txt
116
+ - README.rdoc
117
+ - test/helper.rb
118
+ - test/test_tk-parallel-coordinates.rb
119
+ has_rdoc: true
120
+ homepage: https://rubygems.org/gems/tk-parallel-coordinates
121
+ licenses:
122
+ - MIT
123
+ post_install_message:
124
+ rdoc_options: []
125
+
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ hash: 3
134
+ segments:
135
+ - 0
136
+ version: "0"
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ hash: 3
143
+ segments:
144
+ - 0
145
+ version: "0"
146
+ requirements: []
147
+
148
+ rubyforge_project:
149
+ rubygems_version: 1.6.1
150
+ signing_key:
151
+ specification_version: 3
152
+ summary: A Parallel Coordinates widget for Tk
153
+ test_files:
154
+ - test/helper.rb
155
+ - test/test_tk-parallel-coordinates.rb
@@ -0,0 +1,3 @@
1
+ ++
2
+ %�ݦ�Ljm(\�_���=��>�������5=irQ��[3�R/�:CY�
3
+ %��� ��;F���W۫�܅��_�k�����GJ�!r�.�p�,˝��F~�a�tHE�D?�@E&����*_�@v��C�"2�2���6�s��N� e 9U�˽�>�~ ��R�b��&���UզNP�|pD\I��̋(�Lj/�w��