File: area.rb

package info (click to toggle)
ruby-rubyvis 0.6.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,808 kB
  • ctags: 679
  • sloc: ruby: 11,114; makefile: 2
file content (228 lines) | stat: -rw-r--r-- 7,258 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
module Rubyvis
  # Alias for Rubyvis::Area
  def self.Area
    Rubyvis::Area
  end
  # Provides methods pertinents to area like-marks.
  module AreaPrototype  # :nodoc:
    def fixed
      {
        :line_width=> true,
        :line_join=> true,
        :stroke_style=> true,
        :fill_style=> true,
        :segmented=> true,
        :stroke_dasharray => true, # SVG strokeDasharray (Jamie Love protovis mod)
        :interpolate=> true,
        :tension=> true
      }
    end
    def area_build_instance(s)
      binds = self.binds
      # Handle fixed properties on secondary instances. */
      if (self.index!=0)
        fixed = @binds.fixed
        #/* Determine which properties are fixed. */
        if (!fixed)
          binds.fixed=[]
          fixed = binds.fixed
          filter=lambda {|prop|
            if prop.fixed
              fixed.push(prop)
              false
            else
              true
            end
          }
          #      p binds.required
          binds.required = binds.required.find_all(&filter)
          #      p binds.required
          if (!self.scene[0].segmented)
            binds.optional = binds.optional.find_all(&filter)
          end
          
        end
        
        #    p binds.required


        #/* Copy fixed property values from the first instance. */
        fixed.each {|prop|
          name=prop.name
          s[name]=self.scene[0][name]
        }
        
        
        #    p binds.fixed
        #/* Evaluate all properties on the first instance. */
      else
        binds.required = binds._required
        binds.optional = binds._optional
        binds.fixed = nil
      end
      # pp binds
      mark_build_instance(s)
    end



    def area_bind
      mark_bind
      
      binds = self.binds
      required = binds.required
      optional = binds.optional
      
      optional.size.times {|i|
        prop = optional[i]
        prop.fixed = fixed.keys.include? prop.name
        
        if (prop.name == "segmented")
          required.push(prop)
        end
      }
      optional.delete_if {|v| v.name=='segmented'}
      # Cache the original arrays so they can be restored on build. */
      @binds._required = required
      @binds._optional = optional
    end


    def area_anchor(name)
      #scene=nil
      anchor=mark_anchor(name)
      anchor.interpolate(lambda {
        self.scene.target[self.index].interpolate
      }).eccentricity(lambda {
        self.scene.target[self.index].eccentricity
      }).tension(lambda {
        self.scene.target[self.index].tension
      })
      anchor
    end
  end
  
  # Represents an area mark: the solid area between two series of
  # connected line segments. Unsurprisingly, areas are used most frequently for
  # area charts.
  #
  # <p>Just as a line represents a polyline, the <tt>Area</tt> mark type
  # represents a <i>polygon</i>. However, an area is not an arbitrary polygon;
  # vertices are paired either horizontally or vertically into parallel
  # <i>spans</i>, and each span corresponds to an associated datum. Either the
  # width or the height must be specified, but not both; this determines whether
  # the area is horizontally-oriented or vertically-oriented.  Like lines, areas
  # can be stroked and filled with arbitrary colors.

  class Area < Mark
    include AreaPrototype
    @properties=Mark.properties.dup
    
    
  ##
  # :attr: width
  # The width of a given span, in pixels; used for horizontal spans. If the width
  # is specified, the height property should be 0 (the default). Either the top
  # or bottom property should be used to space the spans vertically, typically as
  # a multiple of the index.
  
  
  ##
  # :attr: height  
  # The height of a given span, in pixels; used for vertical spans. If the height
  # is specified, the width property should be 0 (the default). Either the left
  # or right property should be used to space the spans horizontally, typically
  # as a multiple of the index.
  
  
  ##
  # :attr: line_width  
  # The width of stroked lines, in pixels; used in conjunction with
  # <tt>strokeStyle</tt> to stroke the perimeter of the area. Unlike the
  # {@link Line} mark type, the entire perimeter is stroked, rather than just one
  # edge. The default value of this property is 1.5, but since the default stroke
  # style is null, area marks are not stroked by default.
  #
  # <p>This property is <i>fixed</i> for non-segmented areas. See
  # {@link pv.Mark}.
  
  
  ##
  # :attr: stroke_style
  # The style of stroked lines; used in conjunction with <tt>lineWidth</tt> to
  # stroke the perimeter of the area. Unlike the {@link Line} mark type, the
  # entire perimeter is stroked, rather than just one edge. The default value of
  # this property is null, meaning areas are not stroked by default.
  #
  # <p>This property is <i>fixed</i> for non-segmented areas. See
  # {@link pv.Mark}.
  #
  
  
  ##
  # :attr: fill_style  
  # The area fill style; if non-null, the interior of the polygon forming the
  # area is filled with the specified color. The default value of this property
  # is a categorical color.
  #
  # <p>This property is <i>fixed</i> for non-segmented areas. See
  # {@link pv.Mark}.
  
  
  ##
  # :attr: segmented  
  # Whether the area is segmented; whether variations in fill style, stroke
  # style, and the other properties are treated as fixed. Rendering segmented
  # areas is noticeably slower than non-segmented areas.
  #
  # <p>This property is <i>fixed</i>. See {@link pv.Mark}.
  
  
  ##
  # :attr: interpolate  
  # How to interpolate between values. Linear interpolation ("linear") is the
  # default, producing a straight line between points. For piecewise constant
  # functions (i.e., step functions), either "step-before" or "step-after" can
  # be specified. To draw open uniform b-splines, specify "basis".
  # To draw cardinal splines, specify "cardinal"; see also Line.tension()
  #
  # <p>This property is <i>fixed</i>. See {@link pv.Mark}.
  
  
  ##
  # :attr: tension  
  # The tension of cardinal splines; used in conjunction with
  # interpolate("cardinal"). A value between 0 and 1 draws cardinal splines with
  # the given tension. In some sense, the tension can be interpreted as the
  # "length" of the tangent; a tension of 1 will yield all zero tangents (i.e.,
  # linear interpolation), and a tension of 0 yields a Catmull-Rom spline. The
  # default value is 0.7.
  #
  # <p>This property is <i>fixed</i>. See {@link pv.Mark}.
    
    
    
    attr_accessor_dsl :width, :height, :line_width, [:stroke_style, lambda {|d| Rubyvis.color(d)}], [:fill_style, lambda {|d| Rubyvis.color(d)}], :segmented, :stroke_dasharray, :interpolate, :tension
    def type
      'area'
    end
    def bind
      area_bind
    end
    def build_instance(s)
      area_build_instance(s)
    end
    def self.defaults
      a= Rubyvis::Colors.category20
      Area.new.mark_extend(Mark.defaults).line_width(1.5).fill_style(lambda {a.scale(self.parent.index)}).interpolate('linear').tension(0.7)
    end
    def anchor(name)
      area_anchor(name)
    end
    def build_implied(s)
      s.height=0 if s.height.nil?
      s.width=0 if s.width.nil?
      mark_build_implied(s)
    end
  end
end