Skip to content

Commit a06c052

Browse files
committed
Whole rewrite
1 parent 646a5e6 commit a06c052

30 files changed

+569
-1039
lines changed

lib/nyaplot.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
require_relative 'nyaplot/version'
2+
require_relative 'nyaplot/data'
3+
require_relative 'nyaplot/color'
24
require_relative 'nyaplot/monkeys'
3-
require_relative 'nyaplot/base'
4-
require_relative 'nyaplot/position'
5-
require_relative 'nyaplot/scale'
6-
require_relative 'nyaplot/sheet'
75
require_relative 'nyaplot/core'
8-
require_relative 'nyaplot/data'
9-
require_relative 'nyaplot/glyph'
106
require_relative 'nyaplot/plot'
11-
require_relative 'nyaplot/pane'
12-
require_relative 'nyaplot/color'
13-
require_relative 'nyaplot/stage2d'
14-
require_relative 'nyaplot/interactive'

lib/nyaplot/base.rb

Lines changed: 0 additions & 105 deletions
This file was deleted.

lib/nyaplot/chart_method.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require_relative './charts/bar'
2+
3+
module Nyaplot
4+
module ChartMethods
5+
def bar(**opts)
6+
if @df.nil?
7+
data = @df
8+
else
9+
if opts[:data].nil?
10+
data = opts[:data]
11+
elsif opts[:df].nil?
12+
data = opts[:df]
13+
else
14+
raise ""
15+
end
16+
end
17+
18+
opts = {
19+
data: data,
20+
xscale: @xscale,
21+
yscale: @yscale,
22+
position: @position,
23+
}.merge(opts)
24+
25+
chart = Bar.new(opts)
26+
@deps.concat(chart.deps)
27+
@glyphs.concat(chart.glyphs)
28+
self
29+
end
30+
31+
## short-cut methods for adding primitives
32+
def add_annotation()
33+
add_text
34+
add_arrow
35+
end
36+
37+
def add_rect()
38+
end
39+
40+
def add_circle()
41+
end
42+
43+
def add_text()
44+
end
45+
46+
def add_arrow()
47+
end
48+
end
49+
end

lib/nyaplot/charts/bar.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module Nyaplot
2+
module Charts
3+
class Bar
4+
allow :ordinal, :linear
5+
need :data, :x, :y, :xscale, :yscale
6+
7+
## data: {ylabel: [1,2,3], xlabel: [:a, :b. :c]} or equal dataframe
8+
def create(**opts)
9+
data = opts[:data]
10+
ad Layers::Data.new({data: data})
11+
12+
data[opts[:x]].each_with_index do |str, i|
13+
pos = create_ordinal_position(opts[:xscale], opts[:yscale], str)
14+
args = {
15+
data: data,
16+
x1: -0.8,
17+
x2: 0.8,
18+
y1: 0,
19+
y2: data[opts[:y]][i],
20+
position: ad(create_ordinal_position(opts[:xscale], opts[:yscale], str))
21+
}
22+
ag Layers::Rect.new(args)
23+
end
24+
25+
@ydomain = [0, data[opts[:y]].max]
26+
end
27+
end
28+
end
29+
end

lib/nyaplot/charts/base.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module Nyaplot
2+
module Charts
3+
class ChartBase
4+
class << self
5+
def allow(x, y)
6+
@@allow_x = x
7+
@@allow_y = y
8+
end
9+
10+
def need(*args)
11+
@@needs = args
12+
end
13+
end
14+
15+
attr_reader :glyphs, :deps
16+
def initialize(**opts)
17+
@glyphs = []
18+
@deps = []
19+
20+
@@needs.each do |sym|
21+
raise "lack argument" if opts[sym].nil?
22+
end
23+
24+
create(**opts)
25+
end
26+
27+
def create
28+
## over-write this
29+
end
30+
31+
private
32+
def add_dependency(layer)
33+
@deps.push(layer)
34+
layer
35+
end
36+
alias :add_dependency :ad
37+
38+
private
39+
def add_glyph(glyph)
40+
@glyphs.push(glyph)
41+
glyph
42+
end
43+
alias :add_glyph :ag
44+
45+
private
46+
def create_ordinal_position(xscale, yscale, label, xy=:x)
47+
scale = xy==:x ? xscale : yscale
48+
d2c = ad Layers::D2c.new({scale: scale, label: label})
49+
arg = {x: xscale, y: yscale}
50+
arg[xy] = d2c
51+
ad Layers::Position2d.new(arg)
52+
end
53+
end
54+
end
55+
end

lib/nyaplot/charts/box.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Nyaplot
2+
module Charts
3+
end
4+
end

lib/nyaplot/charts/line.rb

Whitespace-only changes.

lib/nyaplot/charts/scatter.rb

Whitespace-only changes.

lib/nyaplot/core.rb

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,6 @@
11
require 'erb'
22

33
module Nyaplot
4-
5-
@@dep_libraries = {d3:'http://d3js.org/d3.v3.min'}
6-
@@additional_libraries = {}
7-
@@extension_lists = []
8-
9-
def self.extension_lists
10-
@@extension_lists
11-
end
12-
13-
# Tell JavaScript back-end library to load some extension libraries
14-
# @param [String] name The name of JavaScript extension library to load
15-
def self.add_extension(name)
16-
@@extension_lists.push(name)
17-
end
18-
19-
# Load extension library to IRuby notebook after Nyaplotjs is loaded
20-
def self.add_dependency(name, url)
21-
@@dep_libraries[name]=url;
22-
end
23-
24-
# Load extension library to IRuby notebook before Nyaplotjs is loaded
25-
def self.add_additional_library(name, url)
26-
@@additional_libraries[name]=url
27-
end
28-
29-
# generate initializing code
30-
def self.generate_init_code
31-
path = File.expand_path("../templates/init.js.erb", __FILE__)
32-
template = File.read(path)
33-
dep_libraries = @@dep_libraries
34-
additional_libraries = @@additional_libraries
35-
js = ERB.new(template).result(binding)
36-
js
37-
end
38-
39-
def self.start_debug(port=9996)
40-
require 'webrick'
41-
path = File.expand_path("../../../nyaplotjs/release", __FILE__)
42-
`ruby -e httpd #{path} -p #{port}`
43-
44-
js = self.generate_init_code
45-
js.gsub!("http.+nyaplot.js", "http://localhost:" + port.to_s + "/nyaplot.js")
46-
IRuby.display(IRuby.javascript(js))
47-
end
48-
49-
# Enable to show plots on IRuby notebook
50-
def self.init_iruby
51-
js = self.generate_init_code
52-
IRuby.display(IRuby.javascript(js))
53-
end
54-
554
# Create multi-column layout
565
# @example
576
# include Nyaplot
@@ -80,10 +29,4 @@ def rows(*plots)
8029
plot.pane = Pane.rows(*panes)
8130
plot
8231
end
83-
84-
if $DEBUG_NYAPLOT == true
85-
start_debug
86-
else
87-
init_iruby if defined? IRuby
88-
end
8932
end

lib/nyaplot/data.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
require 'csv'
55

66
module Nyaplot
7-
begin
8-
require "mikon"
9-
rescue
10-
end
11-
127
if defined? Mikon
138
class DataFrame
149
include Nyaplot::Base

0 commit comments

Comments
 (0)