Skip to content

Commit 95935ee

Browse files
committed
A lot of miscellaneous bug fixes.
1 parent b1704cb commit 95935ee

File tree

6 files changed

+62
-23
lines changed

6 files changed

+62
-23
lines changed

lib/plotrb/base.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ def collect_attributes
166166
json_attr = classify(k, :json)
167167
if v.respond_to?(:collect_attributes)
168168
collected[json_attr] = v.collect_attributes
169+
elsif v.is_a?(Array)
170+
collected[json_attr] = [].concat(v.collect{ |va|
171+
va.respond_to?(:collect_attributes) ? va.collect_attributes : va
172+
})
169173
else
170174
collected[json_attr] = v
171175
end

lib/plotrb/data.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ def format(*args, &block)
4444
end
4545

4646
def extra_fields
47-
@extra_fields ||= [:index]
48-
@extra_fields.concat(@transform.collect { |t| t.extra_fields }).uniq!
47+
@extra_fields ||= [:data, :index]
48+
if @transform
49+
@extra_fields.concat(@transform.collect { |t| t.extra_fields }).
50+
flatten!.uniq!
51+
end
52+
@extra_fields
4953
end
5054

5155
def method_missing(method, *args, &block)

lib/plotrb/marks.rb

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,29 +62,37 @@ def properties
6262
end
6363

6464
def enter(&block)
65+
process_from
6566
@properties.merge!(
66-
{ enter: ::Plotrb::Mark::MarkProperty.new(@type, @from, &block) }
67+
{ enter: ::Plotrb::Mark::MarkProperty.
68+
new(@type, @from[:data], &block) }
6769
)
6870
self
6971
end
7072

7173
def exit(&block)
74+
process_from
7275
@properties.merge!(
73-
{ exit: ::Plotrb::Mark::MarkProperty.new(@type, @from, &block) }
76+
{ exit: ::Plotrb::Mark::MarkProperty.
77+
new(@type, @from[:data], &block) }
7478
)
7579
self
7680
end
7781

7882
def update(&block)
83+
process_from
7984
@properties.merge!(
80-
{ update: ::Plotrb::Mark::MarkProperty.new(@type, @from, &block) }
85+
{ update: ::Plotrb::Mark::MarkProperty.
86+
new(@type, @from[:data], &block) }
8187
)
8288
self
8389
end
8490

8591
def hover(&block)
92+
process_from
8693
@properties.merge!(
87-
{ hover: ::Plotrb::Mark::MarkProperty.new(@type, @from, &block) }
94+
{ hover: ::Plotrb::Mark::MarkProperty.
95+
new(@type, @from[:data], &block) }
8896
)
8997
self
9098
end
@@ -105,7 +113,7 @@ def process_name
105113
end
106114

107115
def process_from
108-
return unless @from
116+
return unless @from && !@from_processed
109117
from = {}
110118
@from.each do |f|
111119
case f
@@ -125,6 +133,7 @@ def process_from
125133
end
126134
end
127135
@from = from
136+
@from_processed = true
128137
end
129138

130139
def process_group
@@ -383,7 +392,7 @@ def attribute_post_processing
383392
def process_field
384393
return unless @field
385394
case @field
386-
when String
395+
when String, Symbol
387396
@field = get_full_field_ref(@field)
388397
when Hash
389398
if @field[:group]
@@ -418,10 +427,16 @@ def process_scale
418427
end
419428

420429
def get_full_field_ref(field)
421-
data = ::Plotrb::Kernel.find_data(@data)
422-
if field.start_with?('data.') ||
423-
data.extra_fields.include?(field.to_sym)
430+
data = if @data.is_a?(::Plotrb::Data)
431+
@data
432+
else
433+
::Plotrb::Kernel.find_data(@data)
434+
end
435+
extra_fields = (data.extra_fields if data) || []
436+
if field.to_s.start_with?('data.')
424437
field
438+
elsif extra_fields.include?(field.to_sym)
439+
classify(field, :json)
425440
else
426441
"data.#{field}"
427442
end

lib/plotrb/transforms.rb

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Transform
2424

2525
def initialize(type, &block)
2626
@type = type
27-
@extra_fields = []
27+
@extra_fields = [:index, :data]
2828
self.send(@type)
2929
self.instance_eval(&block) if block_given?
3030
::Plotrb::Kernel.transforms << self
@@ -330,6 +330,7 @@ def pie
330330
add_attributes(:sort, :value)
331331
define_boolean_attribute(:sort)
332332
define_single_val_attribute(:value)
333+
@extra_fields.concat([:start_angle, :end_angle])
333334
end
334335

335336
def stack
@@ -512,8 +513,12 @@ def process_link_target
512513
end
513514

514515
def process_pie_value
515-
return unless @type == :pie && @value
516-
@value = get_full_field_ref(@value)
516+
return unless @type == :pie
517+
if @value
518+
@value = get_full_field_ref(@value)
519+
else
520+
@value = 'data'
521+
end
517522
end
518523

519524
def process_stack_order
@@ -553,10 +558,17 @@ def process_wordcloud_font_size
553558
end
554559

555560
def get_full_field_ref(field)
556-
if field.start_with?('data.') || extra_fields.include?(field.to_sym)
557-
field
558-
else
559-
"data.#{field}"
561+
case field
562+
when String
563+
if field.start_with?('data.') || extra_fields.include?(field.to_sym)
564+
field
565+
else
566+
"data.#{field}"
567+
end
568+
when ::Plotrb::Data
569+
'data'
570+
else
571+
raise ArgumentError, 'Invalid data field'
560572
end
561573
end
562574

lib/plotrb/visualization.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ class Visualization
2828
add_attributes :name, :width, :height, :viewport, :padding, :data, :scales,
2929
:marks, :axes
3030

31-
def initialize(args={}, &block)
32-
default = {width: 500, height: 500}
33-
args.reverse_merge(default).each do |k, v|
34-
self.instance_variable_set("@#{k}", v) if self.attributes.include?(k)
35-
end
31+
def initialize(&block)
3632
define_single_val_attributes(:name, :width, :height, :viewport, :padding)
3733
define_multi_val_attributes(:data, :scales, :marks, :axes)
3834
self.instance_eval(&block) if block_given?
@@ -46,6 +42,12 @@ def generate_spec(format=nil)
4642
end
4743
end
4844

45+
private
46+
47+
def attribute_post_processing
48+
49+
end
50+
4951
end
5052

5153
end

spec/plotrb/marks_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
describe 'properties' do
2222

2323
it 'allows multiple properties' do
24+
::Plotrb::Kernel.stub(:find_data).with('some_data').and_return(true)
25+
subject.from('some_data')
2426
subject.enter
2527
subject.exit
2628
subject.properties.keys.should match_array([:enter, :exit])

0 commit comments

Comments
 (0)