Class: Plot::Mark

Inherits:
Object
  • Object
show all
Defined in:
lib/plot/mark.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, **options) ⇒ Mark

Returns a new instance of Mark.



68
69
70
71
# File 'lib/plot/mark.rb', line 68

def initialize(type, **options)
    @type = type
    @options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



66
67
68
# File 'lib/plot/mark.rb', line 66

def options
  @options
end

#typeObject

Returns the value of attribute type.



66
67
68
# File 'lib/plot/mark.rb', line 66

def type
  @type
end

Instance Method Details

#add_option(k, v) ⇒ Object



73
74
75
# File 'lib/plot/mark.rb', line 73

def add_option(k, v)
    @options[k.to_s] = v
end

#render(data) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/plot/mark.rb', line 78

def render(data)
    @options.values do |o|
        if o.try(:value).try(:lambda?)
            output = []
            data.each? do |obj|
                output.push(o.call(obj))
            end

            o.value = output
        end

        if o.try(:transform).try(:lambda?)
            o.value = o.transform.call(data).to_a
            o.transform = nil
        end
    end

    data.each do |d|
        d.each do |k,v|
            if v.class == Date then
                d[k] = {
                    date: true,
                    string: v.to_s
                }
            end
        end
    end

    "Plot['#{@type}'](JSON.parse(`#{JSON.generate(data)}`).map((obj) => { Object.keys(obj).forEach((k) => { if (obj[k].date) { obj[k] = new Date(Date.parse(obj[k].string)) } } ); return obj; }), JSON.parse('#{JSON.generate(@options)}'))"
end