Class: Rust::Plots::GGPlot::Theme

Inherits:
Layer show all
Defined in:
lib/rust/external/ggplot2/themes.rb

Defined Under Namespace

Classes: BlankElement, Element, LineElement, RectElement, TextElement

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Layer

#option

Constructor Details

#initialize(starting, **options) ⇒ Theme

Returns a new instance of Theme.



33
34
35
36
37
38
# File 'lib/rust/external/ggplot2/themes.rb', line 33

def initialize(starting, **options)
    super("theme", **options)
    if starting
        @starting = "theme_" + starting
    end
end

Class Method Details

.from_h(options) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rust/external/ggplot2/themes.rb', line 6

def self.from_h(options)            
    starting = options.delete('_starting')
    options = options.map do |key, value|
        if value.is_a?(Hash)
            case value.delete('_type').split("::").last
            when 'TextElement'
                [key, TextElement.new(**value)]
            when 'RectElement'
                [key, RectElement.new(**value)]
            when 'TextElement'
                [key, TextElement.new(**value)]
            when 'LineElement'
                [key, LineElement.new(**value)]
            end
        else
            [key, value]
        end
    end.to_h
    
    return Theme.new(starting, **options)
end

.load(filename) ⇒ Object



28
29
30
31
# File 'lib/rust/external/ggplot2/themes.rb', line 28

def self.load(filename)
    json = JSON.parse(File.read(filename))
    return self.from_h(json.to_h)
end

Instance Method Details

#save(path, force = false) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rust/external/ggplot2/themes.rb', line 64

def save(path, force = false)
    if !force && FileTest.exist?(path)
        raise "File already existing."
    end
    
    File.open(path, "w") do |f|
        f.write(
            JSON.pretty_generate(
                self.to_h
            )
        )
    end
    
    return true
end

#to_hObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/rust/external/ggplot2/themes.rb', line 53

def to_h
    options = @options.clone
    
    options['_starting'] = @starting.sub("theme_", "")
    options = options.map do |key, value|
        [key, value.is_a?(Theme::Element) ? value.to_h : value]
    end.to_h
    
    return options
end

#to_RObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rust/external/ggplot2/themes.rb', line 40

def to_R
    result = super do |options, arguments| 
        [
            options.map { |k, v| [k.to_s.gsub("_", "."), v] }.to_h,
            arguments
        ] 
    end
    
    result = Rust::Function.new(@starting).to_R + " + " +  result if @starting
    
    return result
end