nyaplot 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/nyaplot/base.rb CHANGED
@@ -41,6 +41,7 @@ module Nyaplot
41
41
  define_method(symbol) {|val=nil|
42
42
  return @properties[symbol] if val.nil?
43
43
  @properties[symbol] = val
44
+ return self
44
45
  }
45
46
  end
46
47
  end
@@ -61,6 +62,7 @@ module Nyaplot
61
62
  define_method(symbol) {|val=nil|
62
63
  return @properties[name][symbol] if val.nil?
63
64
  @properties[name][symbol] = val
65
+ return self
64
66
  }
65
67
  end
66
68
  end
data/lib/nyaplot/color.rb CHANGED
@@ -51,6 +51,12 @@ module Nyaplot
51
51
 
52
52
  # The html interface to access Colorset
53
53
  class Color
54
+ include Enumerable
55
+
56
+ def each(&bl)
57
+ @source.each(&bl)
58
+ end
59
+
54
60
  def initialize(arr)
55
61
  @source = arr
56
62
  end
@@ -73,5 +79,19 @@ module Nyaplot
73
79
  def to_json(*args)
74
80
  @source.to_json
75
81
  end
82
+
83
+ def method_missing(meth, *args, &block)
84
+ if [:size, :length, :[]].include?(meth)
85
+ @source.send(meth, *args, &block)
86
+ else
87
+ super
88
+ end
89
+ end
90
+
91
+ def respond_to?(meth)
92
+ return true if [:size, :length, :[]].include?(meth)
93
+ super
94
+ end
95
+
76
96
  end
77
97
  end
data/lib/nyaplot/data.rb CHANGED
@@ -170,38 +170,29 @@ module Nyaplot
170
170
  @rows[0].keys
171
171
  end
172
172
 
173
- def method_missing(name, *args)
173
+ def method_missing(name, *args, &block)
174
174
  if md = name.match(/(.+)\=/)
175
175
  self.insert_column(name[/(.+)\=/].delete("="), args[0])
176
176
  return
177
- else
177
+ elsif column_labels.include?(name)
178
178
  return self.column(name)
179
+ else
180
+ super(name, *args, &block)
179
181
  end
180
182
  end
181
183
  end
182
184
 
183
185
  class Series
184
186
  include Enumerable
187
+ def each(&block)
188
+ @arr.each(&block)
189
+ end
185
190
 
186
191
  def initialize(label, arr)
187
192
  @arr = arr
188
193
  @label = label
189
194
  end
190
195
 
191
- def each
192
- @arr.each do |item|
193
- yield item
194
- end
195
- end
196
-
197
- def min
198
- @arr.min
199
- end
200
-
201
- def max
202
- @arr.max
203
- end
204
-
205
196
  def to_html(threshold=15)
206
197
  html = '<table><tr><th>' + label.to_s + '</th></tr>>'
207
198
  @arr.each_with_index do |el,i|
@@ -223,5 +214,19 @@ module Nyaplot
223
214
  def label
224
215
  @label
225
216
  end
217
+
218
+ def method_missing(meth, *args, &block)
219
+ if @arr.respond_to?(meth)
220
+ @arr.send(meth, *args, &block)
221
+ else
222
+ super(meth, *args, &block)
223
+ end
224
+ end
225
+
226
+ def respond_to?(meth)
227
+ return true if @arr.respond_to?(meth)
228
+ super(meth)
229
+ end
230
+
226
231
  end
227
232
  end
@@ -49,7 +49,9 @@ module Nyaplot
49
49
  # @return [Numeric] the width of each bar. The specified value should be in the range 0 to 1.
50
50
  # @!attribute color
51
51
  # @return [Array<String>] array of color codes
52
- define_group_properties(:options, [:value, :x, :y, :width, :color])
52
+ # @!attribute legend
53
+ # @return [Bool] decide if the diagram prepare legend
54
+ define_group_properties(:options, [:value, :x, :y, :width, :color, :legend])
53
55
 
54
56
  # calcurate xrange and yrange from recieved data
55
57
  def process_data(df, labels)
@@ -88,7 +90,9 @@ module Nyaplot
88
90
  # @return [String] color code
89
91
  # @!attribute stroke_width
90
92
  # @return [Numeric] the width of stroke
91
- define_group_properties(:options, [:title, :value, :bin_num, :width, :color, :stroke_color, :stroke_width])
93
+ # @!attribute legend
94
+ # @return [Bool] decide if the diagram prepare legend
95
+ define_group_properties(:options, [:title, :value, :bin_num, :width, :color, :stroke_color, :stroke_width, :legend])
92
96
 
93
97
  def process_data(df, labels)
94
98
  label = labels[0]
@@ -162,7 +166,9 @@ module Nyaplot
162
166
  # @return [Numeric] the width of stroke
163
167
  # @!attribute tooltip_contents
164
168
  # @return [Array<Symbol>] column labels to display in tool-tip box
165
- define_group_properties(:options, [:title, :x, :y, :fill_by, :shape_by, :size_by, :color, :shape, :size, :stroke_color, :stroke_width, :tooltip_contents])
169
+ # @!attribute legend
170
+ # @return [Bool] decide if the diagram prepare legend
171
+ define_group_properties(:options, [:title, :x, :y, :fill_by, :shape_by, :size_by, :color, :shape, :size, :stroke_color, :stroke_width, :tooltip_contents, :legend])
166
172
 
167
173
  def process_data(df, labels)
168
174
  label_x = labels[0]
@@ -230,7 +236,9 @@ module Nyaplot
230
236
  # @return [String] the color code
231
237
  # @!attribute stroke_width
232
238
  # @return [Numeric] the width of stroke
233
- define_group_properties(:options, [:title, :x, :y, :color, :stroke_width])
239
+ # @!attribute legend
240
+ # @return [Bool] decide if the diagram prepare legend
241
+ define_group_properties(:options, [:title, :x, :y, :color, :stroke_width, :legend])
234
242
 
235
243
  def process_data(df, labels)
236
244
  label_x = labels[0]
@@ -0,0 +1,53 @@
1
+ class Array
2
+ # Find the numeric mean. nils are ignored.
3
+ def mean
4
+ ary = self.to_a.compact
5
+ if defined?(NMatrix) # Prefer the C version.
6
+ ary.to_nm.mean[0]
7
+ else # Fall back to Ruby.
8
+ ary.inject(0) { |x,i| x + i } / ary.size.to_f
9
+ end
10
+ end
11
+
12
+
13
+ # Find the single median or the mean of medians. nils are ignored.
14
+ def median
15
+ ary = self.to_a.compact.sort.uniq
16
+ return nil if ary.empty?
17
+ if ary.size % 2 == 1 # Even number of entries
18
+ ary[(ary.size-1) / 2]
19
+ else # Odd number of entries
20
+ idx = (ary.size-1) / 2
21
+ (ary[idx] + ary[idx+1]) / 2.0
22
+ end
23
+ end
24
+
25
+
26
+ # Note: Not guaranteed to be the fastest way to find the modes, but I didn't see this function
27
+ # as a bottleneck worth a ton of effort. --JW
28
+ def modes
29
+ ary = self.sort.compact
30
+ return nil if ary.empty?
31
+ h = {}
32
+ ary.each do |k|
33
+ if h.has_key?(k)
34
+ h[k] += 1
35
+ else
36
+ h[k] = 1
37
+ end
38
+ end
39
+
40
+ return nil if h.keys.empty?
41
+
42
+ max_key = h.keys.first
43
+
44
+ h.each_pair do |k,count|
45
+ next if count.nil? || h[max_key].nil?
46
+
47
+ max_key = k if count > h[max_key]
48
+ end
49
+
50
+ mode_count = h[max_key]
51
+ h.select { |k,v| v == mode_count }.keys.sort
52
+ end
53
+ end
data/lib/nyaplot/plot.rb CHANGED
@@ -37,12 +37,15 @@ module Nyaplot
37
37
  define_properties(:diagrams, :filter)
38
38
  define_group_properties(:options, [:width, :height, :margin, :xrange, :yrange, :x_label, :y_label, :bg_color, :grid_color, :legend, :legend_width, :legend_options, :zoom, :rotate_x_label, :rotate_y_label])
39
39
 
40
- def initialize
40
+ def initialize(&block)
41
41
  init_properties
42
42
  set_property(:diagrams, [])
43
43
  set_property(:options, {})
44
44
  set_property(:width, nil)
45
45
  set_property(:legend, nil)
46
+ set_property(:zoom, nil)
47
+
48
+ yield if block_given?
46
49
  end
47
50
 
48
51
  # Add diagram with Array
@@ -102,7 +105,7 @@ module Nyaplot
102
105
  return if diagrams.length == 0
103
106
 
104
107
  # set default values when not specified by users
105
- zoom(true) if diagrams.all?{|d| d.zoom?}
108
+ zoom(true) if zoom.nil? && diagrams.all?{|d| d.zoom?}
106
109
 
107
110
  if width.nil?
108
111
  if legend == true
@@ -1,3 +1,3 @@
1
1
  module Nyaplot
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/nyaplot.gemspec CHANGED
@@ -8,15 +8,47 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Nyaplot::VERSION
9
9
  spec.authors = ["Naoki Nishida"]
10
10
  spec.email = ["domitry@gmail.com"]
11
- spec.summary = %q{interactive plots generator for Ruby users}
12
- spec.description = %q{interactive plots generator for Ruby users. Visit the GitHub repository to get more information.}
11
+ spec.summary = %q{Nyaplot is an interactive plots generator based on Web technology like SVG, WebGL, and JavaScript.}
12
+ spec.description = %q{Nyaplot is an Interactive plots generator based on Web technology like SVG, WebGL, and JavaScript. It enables us to create interactive plots interactively on IRuby notebook, a web-based Ruby environment. Nyaplot is totally web-based gem and plots can be embedded into Rails or Sinatra seemlesly. Supported charts include basic 2D plot, 3D plot, Map plot and plot for Biology. See nbviewer (http://nbviewer.ipython.org/github/domitry/nyaplot/blob/master/examples/notebook/Index.ipynb) to overview what plots can be done with nyaplot and how to create them.}
13
13
  spec.homepage = "https://www.github.com/domitry/nyaplot"
14
14
  spec.license = "MIT"
15
+ spec.post_install_message = <<-EOF
16
+ ************************************************************************
17
+ Welcome to Nyaplot
18
+
19
+ ___/|
20
+ \o.O|
21
+ (___)
22
+ U
23
+
24
+ Thank you for installing Nyaplot gem.
25
+
26
+ We strongly recommend you to install IRuby, an interactive
27
+ Ruby environment on web browser at the same time.
28
+
29
+ $ gem install iruby
30
+
31
+ If you wonder how to use Nyaplot, see /path/to/nyaplot/examples/notebook
32
+ and run `iruby notebook` in the directory.
33
+ You can find these notebook on your browser:
34
+ http://nbviewer.ipython.org/github/domitry/nyaplot/blob/master/examples/notebook/Index.ipynb
35
+
36
+ You can also use nyaplot without IRuby like /path/to/nyaplot/examples/rb
37
+ or on your browser:
38
+ https://github.com/domitry/nyaplot/tree/master/examples/rb
39
+
40
+ Feel free to raise Issue or Pull-request on GitHub.
41
+ Most pull-request might be accepted unless it is broken or too destructive.
42
+
43
+ Enjoy Nyaplot!
44
+ ************************************************************************
45
+ EOF
15
46
 
16
47
  spec.files = `git ls-files -z`.split("\x0")
17
48
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
49
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
50
  spec.require_paths = ["lib"]
51
+ spec.required_ruby_version = ">= 1.9"
20
52
 
21
53
  spec.add_development_dependency "bundler", "~> 1.5"
22
54
  spec.add_development_dependency "rake"
@@ -1,11 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Nyaplot do
4
- context ".init_iruby" do
5
- it "should raise error if IRuby is not loaded." do
6
- expect{Nyaplot.init_iruby}.to raise_error(RuntimeError)
7
- end
8
- end
9
4
 
10
5
  # Tell the name of new JS extension library to Nyaplotjs
11
6
  context ".add_extension" do
@@ -139,35 +139,41 @@ describe Nyaplot::Series do
139
139
  @series = df.a
140
140
  end
141
141
 
142
- context ".each" do
143
- it "should excute recieved block" do
142
+ context "#each" do
143
+ it "should execute received block" do
144
144
  sum = 0
145
145
  @series.each {|val| sum+=val}
146
146
  expect(sum).to eq(40)
147
147
  end
148
148
  end
149
149
 
150
- context ".min" do
151
- it "should calcurate minimam value" do
150
+ context "#min" do
151
+ it "should calculate minimum value" do
152
152
  expect(@series.min).to eq(10)
153
153
  end
154
154
  end
155
155
 
156
- context ".max" do
157
- it "should calcurate maximam value" do
156
+ context "#max" do
157
+ it "should calculate maximum value" do
158
158
  expect(@series.max).to eq(30)
159
159
  end
160
160
  end
161
161
 
162
- context ".to_a" do
162
+ context "#to_a" do
163
163
  it "should return Array" do
164
164
  expect(@series.to_a).to eq([10,30])
165
165
  end
166
166
  end
167
167
 
168
- context ".label" do
168
+ context "#label" do
169
169
  it "should return String" do
170
170
  expect(@series.label).to eq(:a)
171
171
  end
172
172
  end
173
+
174
+ context "#size" do
175
+ it "should delegate to the internal array storage" do
176
+ expect(@series.size).to eq(@series.to_a.size)
177
+ end
178
+ end
173
179
  end
@@ -15,10 +15,9 @@ describe Nyaplot::Frame do
15
15
  end
16
16
  end
17
17
 
18
- context ".export_html" do
18
+ context ".generate_body" do
19
19
  it "should return correct html" do
20
- html = @frame.export_html
21
-
20
+ html = @frame.generate_body
22
21
  if_brackets_is_same_number = [[/<html(.*?)>/,"</html>"],[/<script(.*?)>/,"</script>"],[/<body(.*?)>/,"</body>"]].all? do |pair|
23
22
  html.scan(pair[0]).length == html.scan(pair[1]).length
24
23
  end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Array do
4
+ context "#mean" do
5
+ it "should calculate the mean of its entries" do
6
+ expect([1,2,3,4,5].mean).to eq 3.0
7
+ end
8
+ end
9
+
10
+ context "#median" do
11
+ it "should calculate the median of an odd number of entries" do
12
+ expect([1,2,3,4,5].median).to eq 3
13
+ end
14
+
15
+ it "should calculate the median of an even number of entries" do
16
+ expect([1,2,3,4,5,6].median).to eq 3.5
17
+ end
18
+ end
19
+
20
+ context "#modes" do
21
+ it "should find the only mode (where only one exists)" do
22
+ expect([1,3,2,3,4,5].modes.size).to eq 1
23
+ expect([1,3,2,3,4,5].modes.first).to eq 3
24
+ end
25
+
26
+ it "should find both modes (where two exist) in sorted order" do
27
+ expect([3,3,1,3,1,2,1,4,4].modes.size).to eq 2
28
+ expect([3,3,1,3,1,2,1,4,4].modes).to eq([1,3])
29
+ end
30
+ end
31
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nyaplot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naoki Nishida
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-22 00:00:00.000000000 Z
11
+ date: 2014-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,8 +66,12 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- description: interactive plots generator for Ruby users. Visit the GitHub repository
70
- to get more information.
69
+ description: Nyaplot is an Interactive plots generator based on Web technology like
70
+ SVG, WebGL, and JavaScript. It enables us to create interactive plots interactively
71
+ on IRuby notebook, a web-based Ruby environment. Nyaplot is totally web-based gem
72
+ and plots can be embedded into Rails or Sinatra seemlesly. Supported charts include
73
+ basic 2D plot, 3D plot, Map plot and plot for Biology. See nbviewer (http://nbviewer.ipython.org/github/domitry/nyaplot/blob/master/examples/notebook/Index.ipynb)
74
+ to overview what plots can be done with nyaplot and how to create them.
71
75
  email:
72
76
  - domitry@gmail.com
73
77
  executables: []
@@ -685,6 +689,7 @@ files:
685
689
  - lib/nyaplot/database.rb
686
690
  - lib/nyaplot/diagram.rb
687
691
  - lib/nyaplot/frame.rb
692
+ - lib/nyaplot/monkeys.rb
688
693
  - lib/nyaplot/plot.rb
689
694
  - lib/nyaplot/templates/init.js.erb
690
695
  - lib/nyaplot/templates/iruby.erb
@@ -706,6 +711,7 @@ files:
706
711
  - spec/nyaplot/diagram_spec.rb
707
712
  - spec/nyaplot/frame_spec.rb
708
713
  - spec/nyaplot/matrix_test.csv
714
+ - spec/nyaplot/monkeys_spec.rb
709
715
  - spec/nyaplot/plot_spec.rb
710
716
  - spec/nyaplot3d_spec.rb
711
717
  - spec/nyaplot_spec.rb
@@ -714,7 +720,16 @@ homepage: https://www.github.com/domitry/nyaplot
714
720
  licenses:
715
721
  - MIT
716
722
  metadata: {}
717
- post_install_message:
723
+ post_install_message: "************************************************************************\nWelcome
724
+ to Nyaplot\n\n ___/|\n o.O| \n (___)\n U\n\nThank you for installing Nyaplot gem.\n\nWe
725
+ strongly recommend you to install IRuby, an interactive\nRuby environment on web
726
+ browser at the same time.\n\n$ gem install iruby\n\nIf you wonder how to use Nyaplot,
727
+ see /path/to/nyaplot/examples/notebook\nand run `iruby notebook` in the directory.\nYou
728
+ can find these notebook on your browser:\nhttp://nbviewer.ipython.org/github/domitry/nyaplot/blob/master/examples/notebook/Index.ipynb\n\nYou
729
+ can also use nyaplot without IRuby like /path/to/nyaplot/examples/rb\nor on your
730
+ browser:\nhttps://github.com/domitry/nyaplot/tree/master/examples/rb\n\nFeel free
731
+ to raise Issue or Pull-request on GitHub.\nMost pull-request might be accepted unless
732
+ it is broken or too destructive.\n\nEnjoy Nyaplot!\n************************************************************************\n"
718
733
  rdoc_options: []
719
734
  require_paths:
720
735
  - lib
@@ -722,7 +737,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
722
737
  requirements:
723
738
  - - ">="
724
739
  - !ruby/object:Gem::Version
725
- version: '0'
740
+ version: '1.9'
726
741
  required_rubygems_version: !ruby/object:Gem::Requirement
727
742
  requirements:
728
743
  - - ">="
@@ -733,7 +748,8 @@ rubyforge_project:
733
748
  rubygems_version: 2.2.2
734
749
  signing_key:
735
750
  specification_version: 4
736
- summary: interactive plots generator for Ruby users
751
+ summary: Nyaplot is an interactive plots generator based on Web technology like SVG,
752
+ WebGL, and JavaScript.
737
753
  test_files:
738
754
  - spec/bionya_spec.rb
739
755
  - spec/mapnya_spec.rb
@@ -745,6 +761,7 @@ test_files:
745
761
  - spec/nyaplot/diagram_spec.rb
746
762
  - spec/nyaplot/frame_spec.rb
747
763
  - spec/nyaplot/matrix_test.csv
764
+ - spec/nyaplot/monkeys_spec.rb
748
765
  - spec/nyaplot/plot_spec.rb
749
766
  - spec/nyaplot3d_spec.rb
750
767
  - spec/nyaplot_spec.rb