Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 'BucketizedDimension' allowing buckets to be created from bucke…
…tizing or categorizing the value of a selected measure.
- Loading branch information
Nathan
committed
Apr 8, 2010
1 parent
49a46a4
commit f5ce37c
Showing
13 changed files
with
150 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
module Cubicle | ||
|
||
class BucketizedDimension < Dimension | ||
|
||
attr_accessor :step, :bump, :value_expression, :bucket_range, :formatter | ||
|
||
def initialize(dimension_name, value_expression, bucket_range, options={}, &block) | ||
super(dimension_name, options) | ||
@value_expression = value_expression | ||
@bucket_range = bucket_range | ||
@formatter = block | ||
@step = options(:bucket_size, :step) || 1 | ||
@bump = options(:range_start_bump, :bump) || 1 | ||
end | ||
|
||
def to_js_value | ||
generate_buckets | ||
"(function(val){if(val==null || isNaN(val))return null; #{@buckets.join(';')}})(#{value_expression})" | ||
end | ||
|
||
private | ||
def generate_buckets | ||
@buckets = [] | ||
prev = :begin | ||
@bucket_range.step(step) do |next_val| | ||
@buckets << bucket_javascript(prev,next_val) | ||
prev = next_val | ||
end | ||
@buckets << bucket_javascript(prev, :end) | ||
end | ||
|
||
def bucket_javascript(min,max) | ||
min += bump unless min.is_a?(Symbol) || max.is_a?(Symbol) || min==@bucket_range.begin | ||
if (min == :begin) | ||
"if (val < #{max}) return '#{label_for(min,max)}'" | ||
elsif (max == :end) | ||
"if (val > #{min}) return '#{label_for(min,max)}'" | ||
else | ||
"if (val >= #{min} && val <= #{max}) return '#{label_for(min,max)}'" | ||
end | ||
end | ||
|
||
def label_for(min,max) | ||
return formatter.call(min,max) if formatter | ||
if min == :begin | ||
"< #{max}" | ||
elsif max == :end | ||
"> #{min}" | ||
else | ||
"#{min} - #{max}" | ||
end | ||
end | ||
|
||
|
||
end | ||
|
||
<<BUCKETIZE | ||
(function(val){ | ||
if(val == null || isNaN(val)) return null; | ||
if(val < 5) return '< 5'; | ||
if (val >= 6 && val <= 10) return '6 - 10' | ||
if (val >= 11 && val <= 15) return '11 - 15' | ||
return '> 10'; | ||
})(val) | ||
BUCKETIZE | ||
|
||
|
||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Cubicle | ||
VERSION = '0.1.7' | ||
VERSION = '0.1.8' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require "test_helper" | ||
|
||
class BucketizedDimensionTest < ActiveSupport::TestCase | ||
context "BucketizedDimension.to_js_value" do | ||
should "generate an appropriately structured bucketizing function" do | ||
dim = Cubicle::BucketizedDimension.new(:test, "this.value", 0..10, :step=>5, :bump=>1) | ||
assert_equal "(function(val){if(val==null || isNaN(val))return null; if (val < 0) return '< 0';if (val >= 0 && val <= 5) return '0 - 5';if (val >= 6 && val <= 10) return '6 - 10';if (val > 10) return '> 10'})(this.value)", | ||
dim.to_js_value | ||
end | ||
should "use the block to generate the bucket labels if provided" do | ||
dim = Cubicle::BucketizedDimension.new(:test, "this.value", 0..10, :step=>5, :bump=>1) do |min,max| | ||
"#{min}:#{max}" | ||
end | ||
assert_equal "(function(val){if(val==null || isNaN(val))return null; if (val < 0) return 'begin:0';if (val >= 0 && val <= 5) return '0:5';if (val >= 6 && val <= 10) return '6:10';if (val > 10) return '10:end'})(this.value)", | ||
dim.to_js_value | ||
end | ||
should "handle decimal steps and bumps with an integer range" do | ||
dim = Cubicle::BucketizedDimension.new(:test, "this.value", 1..2, :step=>0.5, :bump=>0.01) | ||
puts dim.to_js_value | ||
assert_equal "(function(val){if(val==null || isNaN(val))return null; if (val < 1.0) return '< 1.0';if (val >= 1.0 && val <= 1.5) return '1.0 - 1.5';if (val >= 1.51 && val <= 2.0) return '1.51 - 2.0';if (val > 2.0) return '> 2.0'})(this.value)", | ||
dim.to_js_value | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters