rubyvis 0.1.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rubyvis/mark.rb CHANGED
@@ -1,3 +1,4 @@
1
+
1
2
  module Rubyvis
2
3
  # Constructs a new mark with default properties. Marks, with the exception of
3
4
  # the root panel, are not typically constructed directly; instead, they are
@@ -69,7 +70,7 @@ module Rubyvis
69
70
  # @attr [Panel]
70
71
  attr_accessor :root
71
72
 
72
- # The child index. -1 if the enclosing parent panel is null; otherwise, the
73
+ # The child index. -1 if the enclosing parent panel is nil; otherwise, the
73
74
  # zero-based index of this mark into the parent panel's <tt>children</tt>
74
75
  # array.
75
76
  # @attr [Number]
@@ -118,7 +119,7 @@ module Rubyvis
118
119
  # If a cast function has been assigned to the specified property name, the
119
120
  # property function is wrapped by the cast function, or, if a constant is
120
121
  # specified, the constant is immediately cast. Note, however, that if the
121
- # property value is null, the cast function is not invoked.
122
+ # property value is nil, the cast function is not invoked.
122
123
  #
123
124
  # Parameters:
124
125
  # * @param [String] name the property name.
@@ -128,8 +129,12 @@ module Rubyvis
128
129
 
129
130
  def self.property_method(name, _def, func=nil, klass=nil)
130
131
  return if klass.method_defined? name
131
- klass.send(:define_method, name) do |*arguments|
132
+ klass.send(:define_method, name) do |*arguments,&block|
133
+
132
134
  v,dummy = arguments
135
+ if block
136
+ v=block
137
+ end
133
138
  if _def and self.scene
134
139
  if arguments.size>0
135
140
  defs[name]=OpenStruct.new({:id=>(v.nil?) ? 0 : Rubyvis.id, :value=> v})
@@ -137,7 +142,8 @@ module Rubyvis
137
142
  end
138
143
  return defs[name]
139
144
  end
140
- if arguments.size>0
145
+
146
+ if arguments.size>0 or block
141
147
  v=v.to_proc if v.respond_to? :to_proc
142
148
  type=(!_def).to_i<<1 | (v.is_a? Proc).to_i
143
149
 
@@ -379,7 +385,7 @@ module Rubyvis
379
385
  end
380
386
 
381
387
  # Create a new Mark
382
- def initialize(opts=Hash.new)
388
+ def initialize(opts=Hash.new, &block)
383
389
  @_properties=[]
384
390
  opts.each {|k,v|
385
391
  self.send("#{k}=",v) if self.respond_to? k
@@ -390,9 +396,18 @@ module Rubyvis
390
396
  @index_defined = true
391
397
  @scale=1
392
398
  @scene=nil
399
+ if block
400
+ block.arity<1 ? self.instance_eval(&block) : block.call(self)
401
+ end
393
402
  end
394
403
 
395
404
 
405
+
406
+
407
+
408
+
409
+
410
+
396
411
  # The mark type; a lower name. The type name controls rendering
397
412
  # behavior, and unless the rendering engine is extended, must be one of the
398
413
  # built-in concrete mark types: area, bar, dot, image, label, line, panel,
@@ -454,9 +469,9 @@ module Rubyvis
454
469
 
455
470
 
456
471
  #Returns the previous instance of this mark in the scene graph, or
457
- # null if this is the first instance.
472
+ # nil if this is the first instance.
458
473
  #
459
- # @return a node in the scene graph, or null.
474
+ # @return a node in the scene graph, or nil.
460
475
  def sibling
461
476
  (self.index==0) ? nil: self.scene[self.index-1]
462
477
  end
@@ -464,9 +479,9 @@ module Rubyvis
464
479
 
465
480
  # Returns the current instance in the scene graph of this mark,
466
481
  # in the previous instance of the enclosing parent panel.
467
- # May return null if this instance could not be found.
482
+ # May return nil if this instance could not be found.
468
483
  #
469
- # @return a node in the scene graph, or null.
484
+ # @return a node in the scene graph, or nil.
470
485
  def cousin
471
486
  par=self.parent
472
487
  s= par ? par.sibling : nil
@@ -969,3 +984,5 @@ require 'rubyvis/mark/rule'
969
984
  require 'rubyvis/mark/label'
970
985
  require 'rubyvis/mark/dot'
971
986
  require 'rubyvis/mark/wedge'
987
+ require 'rubyvis/mark/shorcut_methods'
988
+
@@ -56,9 +56,10 @@ module Rubyvis
56
56
  # Create a new Anchor. Use Mark.add instead.
57
57
 
58
58
  def initialize(target)
59
- super()
59
+
60
60
  self.target=target
61
61
  self.parent=target.parent
62
+ super()
62
63
  end
63
64
 
64
65
  # Sets the prototype of this anchor to the specified mark. Any properties not
@@ -4,7 +4,7 @@ module Rubyvis
4
4
  Rubyvis::Area
5
5
  end
6
6
  # Provides methods pertinents to area like-marks.
7
- module AreaPrototype
7
+ module AreaPrototype # :nodoc:
8
8
  def fixed
9
9
  {
10
10
  :line_width=> true,
@@ -99,9 +99,107 @@ module Rubyvis
99
99
  return anchor
100
100
  end
101
101
  end
102
+
103
+ # Represents an area mark: the solid area between two series of
104
+ # connected line segments. Unsurprisingly, areas are used most frequently for
105
+ # area charts.
106
+ #
107
+ # <p>Just as a line represents a polyline, the <tt>Area</tt> mark type
108
+ # represents a <i>polygon</i>. However, an area is not an arbitrary polygon;
109
+ # vertices are paired either horizontally or vertically into parallel
110
+ # <i>spans</i>, and each span corresponds to an associated datum. Either the
111
+ # width or the height must be specified, but not both; this determines whether
112
+ # the area is horizontally-oriented or vertically-oriented. Like lines, areas
113
+ # can be stroked and filled with arbitrary colors.
114
+
102
115
  class Area < Mark
103
116
  include AreaPrototype
104
117
  @properties=Mark.properties.dup
118
+
119
+
120
+ ##
121
+ # :attr: width
122
+ # The width of a given span, in pixels; used for horizontal spans. If the width
123
+ # is specified, the height property should be 0 (the default). Either the top
124
+ # or bottom property should be used to space the spans vertically, typically as
125
+ # a multiple of the index.
126
+
127
+
128
+ ##
129
+ # :attr: height
130
+ # The height of a given span, in pixels; used for vertical spans. If the height
131
+ # is specified, the width property should be 0 (the default). Either the left
132
+ # or right property should be used to space the spans horizontally, typically
133
+ # as a multiple of the index.
134
+
135
+
136
+ ##
137
+ # :attr: line_width
138
+ # The width of stroked lines, in pixels; used in conjunction with
139
+ # <tt>strokeStyle</tt> to stroke the perimeter of the area. Unlike the
140
+ # {@link Line} mark type, the entire perimeter is stroked, rather than just one
141
+ # edge. The default value of this property is 1.5, but since the default stroke
142
+ # style is null, area marks are not stroked by default.
143
+ #
144
+ # <p>This property is <i>fixed</i> for non-segmented areas. See
145
+ # {@link pv.Mark}.
146
+
147
+
148
+ ##
149
+ # :attr: stroke_style
150
+ # The style of stroked lines; used in conjunction with <tt>lineWidth</tt> to
151
+ # stroke the perimeter of the area. Unlike the {@link Line} mark type, the
152
+ # entire perimeter is stroked, rather than just one edge. The default value of
153
+ # this property is null, meaning areas are not stroked by default.
154
+ #
155
+ # <p>This property is <i>fixed</i> for non-segmented areas. See
156
+ # {@link pv.Mark}.
157
+ #
158
+
159
+
160
+ ##
161
+ # :attr: fill_style
162
+ # The area fill style; if non-null, the interior of the polygon forming the
163
+ # area is filled with the specified color. The default value of this property
164
+ # is a categorical color.
165
+ #
166
+ # <p>This property is <i>fixed</i> for non-segmented areas. See
167
+ # {@link pv.Mark}.
168
+
169
+
170
+ ##
171
+ # :attr: segmented
172
+ # Whether the area is segmented; whether variations in fill style, stroke
173
+ # style, and the other properties are treated as fixed. Rendering segmented
174
+ # areas is noticeably slower than non-segmented areas.
175
+ #
176
+ # <p>This property is <i>fixed</i>. See {@link pv.Mark}.
177
+
178
+
179
+ ##
180
+ # :attr: interpolate
181
+ # How to interpolate between values. Linear interpolation ("linear") is the
182
+ # default, producing a straight line between points. For piecewise constant
183
+ # functions (i.e., step functions), either "step-before" or "step-after" can
184
+ # be specified. To draw open uniform b-splines, specify "basis".
185
+ # To draw cardinal splines, specify "cardinal"; see also Line.tension()
186
+ #
187
+ # <p>This property is <i>fixed</i>. See {@link pv.Mark}.
188
+
189
+
190
+ ##
191
+ # :attr: tension
192
+ # The tension of cardinal splines; used in conjunction with
193
+ # interpolate("cardinal"). A value between 0 and 1 draws cardinal splines with
194
+ # the given tension. In some sense, the tension can be interpreted as the
195
+ # "length" of the tangent; a tension of 1 will yield all zero tangents (i.e.,
196
+ # linear interpolation), and a tension of 0 yields a Catmull-Rom spline. The
197
+ # default value is 0.7.
198
+ #
199
+ # <p>This property is <i>fixed</i>. See {@link pv.Mark}.
200
+
201
+
202
+
105
203
  attr_accessor_dsl :width, :height, :line_width, [:stroke_style, lambda {|d| Rubyvis.color(d)}], [:fill_style, lambda {|d| Rubyvis.color(d)}], :segmented, :interpolate, :tension
106
204
  def type
107
205
  'area'
@@ -48,11 +48,11 @@ module Rubyvis
48
48
  ##
49
49
  # :attr: stroke_style
50
50
  # The style of stroked lines; used in conjunction with line_width to
51
- # stroke the bar's border. The default value of this property is null, meaning bars are not stroked by default.
51
+ # stroke the bar's border. The default value of this property is nil, meaning bars are not stroked by default.
52
52
 
53
53
  ##
54
54
  # :attr: fill_style
55
- # The bar fill style; if non-null, the interior of the bar is filled with the
55
+ # The bar fill style; if non-nil, the interior of the bar is filled with the
56
56
  # specified color. The default value of this property is a categorical color.
57
57
 
58
58
  attr_accessor_dsl :width, :height, :line_width, [:stroke_style, lambda {|d| Rubyvis.color(d)}], [:fill_style, lambda {|d| Rubyvis.color(d)}]
@@ -70,8 +70,8 @@ module Rubyvis
70
70
 
71
71
  ##
72
72
  # :attr: fill_style
73
- # The fill style; if non-null, the interior of the dot is filled with the
74
- # specified color. The default value of this property is null, meaning dots are
73
+ # The fill style; if non-nil, the interior of the dot is filled with the
74
+ # specified color. The default value of this property is nil, meaning dots are
75
75
  # not filled by default. See Rubyvis.color()
76
76
 
77
77
  attr_accessor_dsl :shape, :shape_angle, :shape_radius, :shape_size, :line_width, [:stroke_style, lambda {|d| Rubyvis.color(d)}], [:fill_style, lambda {|d| Rubyvis.color(d)}]
@@ -3,7 +3,8 @@ module Rubyvis
3
3
  def self.Line
4
4
  Rubyvis::Line
5
5
  end
6
- module LinePrototype
6
+ # Provides methods pertinents to line like marks.
7
+ module LinePrototype # :nodoc:
7
8
  include AreaPrototype
8
9
  def line_anchor(name)
9
10
  anchor=area_anchor(name).text_align(lambda {|d|
@@ -14,26 +15,137 @@ module Rubyvis
14
15
  return anchor
15
16
  end
16
17
  end
18
+
19
+ # Represents a series of connected line segments, or <i>polyline</i>,
20
+ # that can be stroked with a configurable color and thickness. Each
21
+ # articulation point in the line corresponds to a datum; for <i>n</i> points,
22
+ # <i>n</i>-1 connected line segments are drawn. The point is positioned using
23
+ # the box model. Arbitrary paths are also possible, allowing radar plots and
24
+ # other custom visualizations.
25
+ #
26
+ # <p>Like areas, lines can be stroked and filled with arbitrary colors. In most
27
+ # cases, lines are only stroked, but the fill style can be used to construct
28
+ # arbitrary polygons.
17
29
  class Line < Mark
18
30
  include AreaPrototype
19
31
  include LinePrototype
20
32
  @properties=Mark.properties.dup
33
+
34
+
35
+ ##
36
+ # :attr: line_width
37
+ # The width of stroked lines, in pixels; used in conjunction with
38
+ # +stroke_style+ to stroke the line.
39
+
40
+
41
+ ##
42
+ # :attr: stroke_style
43
+ # The style of stroked lines; used in conjunction with <tt>lineWidth</tt> to
44
+ # stroke the line. The default value of this property is a categorical color.
45
+
46
+
47
+ ##
48
+ # :attr: line_join
49
+ # The type of corners where two lines meet. Accepted values are "bevel",
50
+ # "round" and "miter". The default value is "miter".
51
+ #
52
+ # <p>For segmented lines, only "miter" joins and "linear" interpolation are
53
+ # currently supported. Any other value, including nil, will disable joins,
54
+ # producing disjoint line segments. Note that the miter joins must be computed
55
+ # manually (at least in the current SVG renderer); since this calculation may
56
+ # be expensive and unnecessary for small lines, specifying nil can improve
57
+ # performance significantly.
58
+ #
59
+ # <p>This property is <i>fixed</i>. See Rubyvis.Mark
60
+
61
+ ##
62
+ # :attr: fill_style
63
+ # The line fill style; if non-nil, the interior of the line is closed and
64
+ # filled with the specified color. The default value of this property is a
65
+ # nil, meaning that lines are not filled by default.
66
+ #
67
+ # <p>This property is <i>fixed</i>. See Rubyvis.Mark
68
+
69
+
70
+ ##
71
+ # :attr: segmented
72
+ # Whether the line is segmented; whether variations in stroke style, line width and the other properties are treated as fixed. Rendering segmented lines is noticeably slower than non-segmented lines.
73
+ # <p>This property is <i>fixed</i>. See Rubyvis.Mark
74
+
75
+
76
+ ##
77
+ # :attr: interpolate
78
+ # How to interpolate between values. Linear interpolation ("linear") is the
79
+ # default, producing a straight line between points. For piecewise constant
80
+ # functions (i.e., step functions), either "step-before" or "step-after"
81
+ # can be specified. To draw a clockwise circular arc between points,
82
+ # specify "polar"; to draw a counterclockwise circular arc between points,
83
+ # specify "polar-reverse". To draw open uniform b-splines, specify "basis". # To draw cardinal splines, specify "cardinal"; see also Line.tension()
84
+ #
85
+ # <p>This property is <i>fixed</i>. See Rubyvis.Mark
86
+
87
+
88
+ ##
89
+ # :attr: eccentricity
90
+ # The eccentricity of polar line segments; used in conjunction with
91
+ # interpolate("polar"). The default value of 0 means that line segments are
92
+ # drawn as circular arcs. A value of 1 draws a straight line. A value between 0
93
+ # and 1 draws an elliptical arc with the given eccentricity.
94
+
95
+
96
+ ##
97
+ # :attr: tension
98
+ # The tension of cardinal splines; used in conjunction with
99
+ # interpolate("cardinal"). A value between 0 and 1 draws cardinal splines with
100
+ # the given tension. In some sense, the tension can be interpreted as the
101
+ # "length" of the tangent; a tension of 1 will yield all zero tangents (i.e.,
102
+ # linear interpolation), and a tension of 0 yields a Catmull-Rom spline. The
103
+ # default value is 0.7.
104
+ #
105
+ # <p>This property is <i>fixed</i>. See Rubyvis.Mark
106
+
21
107
  attr_accessor_dsl :line_width, :line_join, [:stroke_style, lambda {|d| Rubyvis.color(d)}], [:fill_style, lambda {|d| Rubyvis.color(d)}], :segmented, :interpolate, :eccentricity, :tension
108
+ # Type of line
22
109
  def type
23
110
  "line"
24
111
  end
112
+
113
+ # Constructs a new line anchor with default properties. Lines support five
114
+ # different anchors:<ul>
115
+ #
116
+ # <li>top
117
+ # <li>left
118
+ # <li>center
119
+ # <li>bottom
120
+ # <li>right
121
+ #
122
+ # </ul>In addition to positioning properties (left, right, top bottom), the
123
+ # anchors support text rendering properties (text-align, text-baseline). Text is
124
+ # rendered to appear outside the line. Note that this behavior is different
125
+ # from other mark anchors, which default to rendering text <i>inside</i> the
126
+ # mark.
127
+ #
128
+ # <p>For consistency with the other mark types, the anchor positions are
129
+ # defined in terms of their opposite edge. For example, the top anchor defines
130
+ # the bottom property, such that a bar added to the top anchor grows upward.
25
131
  def anchor(name)
26
132
  line_anchor(name)
27
133
  end
28
- def bind(*args)
134
+ # Reuse Area's implementation for segmented bind & build.
135
+ def bind(*args) # :nodoc:
29
136
  area_bind(*args)
30
137
  end
31
- def build_instance(*args)
138
+ # Reuse Area's implementation for segmented bind & build.
139
+
140
+ def build_instance(*args) # :nodoc:
32
141
  area_build_instance(*args)
33
142
  end
143
+ # Default properties for lines. By default, there is no fill and the stroke
144
+ # style is a categorical color. The default interpolation is linear.
145
+
34
146
  def self.defaults
35
147
  a=Rubyvis::Colors.category10()
36
- Line.new.extend(Mark.defaults).line_join('miter').line_width(1.5).stroke_style( lambda {return a.scale(self.parent.index)}).interpolate('linear').eccentricity(0).tension(7)
148
+ Line.new.extend(Mark.defaults).line_join('miter').line_width(1.5).stroke_style( lambda {return a.scale(self.parent.index)}).interpolate('linear').eccentricity(0).tension(0.7)
37
149
  end
38
150
  end
39
151
  end
@@ -12,9 +12,10 @@ module Rubyvis
12
12
  attr_accessor_dsl :transform, :overflow, :canvas
13
13
  attr_accessor :children, :root
14
14
  def initialize
15
- super
16
15
  @children=[]
17
16
  @root=self
17
+ super
18
+
18
19
  end
19
20
  def children_inspect(level=0)
20
21
  out=[]
@@ -0,0 +1,58 @@
1
+ class Rubyvis::Mark
2
+ ##
3
+ # :section: Ruby API
4
+ ##
5
+
6
+ # Create
7
+ def self.mark_method(name,mark) #:nodoc:
8
+ define_method(name) do |*args,&block|
9
+ opts=args[0]
10
+ opts||=Hash.new
11
+ if opts[:anchor]
12
+ base=anchor(opts[:anchor])
13
+ else
14
+ base=self
15
+ end
16
+ a=base.add(mark)
17
+ if block
18
+ block.arity<1 ? a.instance_eval(&block) : block.call(a)
19
+ end
20
+ end
21
+ end
22
+ ##
23
+ # :method: area(opts,&block)
24
+ #
25
+ mark_method :area, Rubyvis::Area
26
+ ##
27
+ # :method: bar(opts,&block)
28
+ #
29
+ mark_method :bar, Rubyvis::Bar
30
+ ##
31
+ # :method: dot(opts,&block)
32
+ #
33
+ mark_method :dot, Rubyvis::Dot
34
+ ##
35
+ # :method: _image(opts,&block)
36
+ #
37
+ mark_method :_image, Rubyvis::Image
38
+ ##
39
+ # :method: label(opts,&block)
40
+ #
41
+ mark_method :label, Rubyvis::Label
42
+ ##
43
+ # :method: line(opts,&block)
44
+ #
45
+ mark_method :line, Rubyvis::Line
46
+ ##
47
+ # :method: panel(opts,&block)
48
+ #
49
+ mark_method :panel, Rubyvis::Panel
50
+ ##
51
+ # :method: rule(opts,&block)
52
+ #
53
+ mark_method :rule, Rubyvis::Rule
54
+ ##
55
+ # :method: wedge(opts,&block)
56
+ #
57
+ mark_method :wedge, Rubyvis::Rule
58
+ end