Module: CubicChart

Included in:
Graph
Defined in:
lib/cubic_chart.rb

Instance Method Summary collapse

Instance Method Details

#draw_cubic_curve(data, data_description, accuracy = 0.1, serie_name = "") ⇒ Object

This curve is using a cubic algorithm to process the average values between two points. You have to specify the accuracy between two points, typically a 0.1 value is acceptable. the smaller the value is, the longer it will take to process the graph.



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
# File 'lib/cubic_chart.rb', line 5

def draw_cubic_curve(data,data_description,accuracy=0.1,serie_name="")

  data_description = self.validate_data_description("draw_cubic_curve",data_description)
  self.validate_data("draw_cubic_curve",data)
  graph_id = 0
  id = 0
  color_id =0

  data_description["values"].each do |col_name|
    if ( serie_name == "" || serie_name == col_name )
      x_in = []
      y_in =[]
      y_t = []
      u = []
      x_in[0] = 0
      y_in[0] = 0

      data_description["description"].each do |key_i,value_i|
        if ( key_i == col_name )
          color_id = id
          id = id+1
        end
      end
      index = 1
      x_last = -1
      missing = []
      data.each do |key|
        if(!key[col_name].nil?)
          val = key[col_name]

          x_in[index]  = index
          #y_in[index] =  val
          #my hack TODO "" convet missing values to zero
          y_in[index] =  val if ((val).is_a?(Numeric))
          y_in[index] = 0 if (!(val).is_a?(Numeric))
          ######
          missing[index]=true if (!(val).is_a?(Numeric))
          index=index+1
        end
      end
      index= index-1
      y_t[0] = 0
      y_t[1] = 0
      u[0] = 0
      u[1]  = 0
      i =2
      y_last =0

      while(i<=index-1)
        sig = (x_in[i]-x_in[i-1])*1.0/(x_in[i+1]-x_in[i-1]) #rescue 0
        p=sig*y_t[i-1]+2
        y_t[i]=(sig-1)/p
        u[i]=(y_in[i+1]-y_in[i])*1.0/(x_in[i+1]-x_in[i])-(y_in[i]-y_in[i-1])*1.0/(x_in[i]-x_in[i-1]) #rescue 0
        u[i]=(6*u[i]/(x_in[i+1]-x_in[i-1])-sig*u[i-1])/p #rescue 0
        i=i+1
      end
      qn = 0
      un = 0
      y_t[index] = (un - qn * u[index-1]) / (qn * y_t[index-1] + 1)
      k = index-1
      while k>=1
        y_t[k]=y_t[k]*	y_t[k+1]+u[k]
        k=k-1
      end
      x_pos  = @g_area_x1 + @g_area_x_offset
      x =1
      while x<=index
        klo=1
        khi=index
        k = khi-klo
        while k>1
          k=khi-klo
          if x_in[k]>=x
            khi=k
          else
            klo=k
          end
        end
        klo=khi-1
        h = x_in[khi]-x_in[klo]
        a = (x_in[khi]-x)/h rescue 1
        b = (x-x_in[klo])/h rescue 1
        value = a*y_in[klo]+b*y_in[khi]+((a*a*a-a)*y_t[klo]+(b*b*b-b)*y_t[khi])*(h*h)/6
        y_pos = @g_area_y2-((value-@vmin)*@division_ratio)
        #TODO Check(x_last!=-1 && !missing[x.floor].nil? && !missing[(x+1).floor].nil? )
        #UPDATED
        if (x_last!=-1 && missing[x.floor].nil? && missing[(x+1).floor].nil? )
          self.draw_line(x_last,y_last,x_pos,y_pos, @palette[id]["r"],@palette[id]["g"],@palette[id]["b"],true)
        end
        x_last = x_pos
        y_last = y_pos
        x_pos = x_pos +@division_width*accuracy
        x=x+accuracy
      end
      #Add potentialy missing values
      x_pos  = x_pos - @division_width * accuracy
      if ( x_pos < (@g_area_x2 - @g_area_x_offset) )
        y_pos = @g_area_y2 - ((y_in[index]-@vmin) * @division_ratio)
        self.draw_line(x_last,y_last,@g_area_x2-@g_area_x_offset,y_pos,@palette[id]["r"],@palette[id]["g"],@palette[id]["b"],true)
      end
      graph_id += 1
    end
  end
end

#draw_filled_cubic_curve(data, data_description, accuracy = 0.1, alpha = 100, around_zero = false) ⇒ Object

This function will draw a filled curved line graph using all the registered series. This curve is using a cubic algorythm to process the average values between two points. You have to specify the accuracy between two points, typicaly a 0.1 value is acceptable. the smaller the value is, the longer it will take to process the graph. You can provide the alpha value used when merging all series layers. If around_zero is set to true, the area drawn will be between the 0 axis and the line graph value.



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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/cubic_chart.rb', line 115

def draw_filled_cubic_curve(data,data_description,accuracy=0.1,alpha=100,around_zero=false)
  data_description = self.validate_data_description("draw_filled_cubic_curve",data_description)
  self.validate_data("draw_filled_cubic_curve",data)
  layer_width  = @g_area_x2-@g_area_x1
  layer_height = @g_area_y2-@g_area_y1
  y_zero = layer_height - ((0-@vmin) * @division_ratio)
  y_zero = layer_height if ( y_zero > layer_height )
  graph_id = 0
  id = 0
  color_id =0
  data_description["values"].each do |col_name|
    x_in = []
    y_in =[]
    y_t = []
    u = []
    x_in[0] = 0
    y_in[0] = 0
    data_description["description"].each do |key_i,value_i|
      if ( key_i == col_name )
        color_id = id
        id = id+1
      end
    end
    index = 1
    x_last = -1
    missing = []
    data.each do |key|
      if(!key[col_name].nil?)
        val = key[col_name]
        x_in[index]  = index
        y_in[index] =  val
        missing[index]=true if ((val).is_a?(Numeric))
        index=index+1
      end
    end
    index= index-1
    y_t[0] = 0
    y_t[1] = 0
    u[1]  = 0
    i =2
    y_last =0

    while(i<index)
      sig = (x_in[i]-x_in[i-1])*1.0/(x_in[i+1]-x_in[i-1]) #rescue 0
      p=sig*y_t[i-1]+2
      y_t[i]=(sig-1)/p
      u[i]=(y_in[i+1]-y_in[i])*1.0/(x_in[i+1]-x_in[i])-(y_in[i]-y_in[i-1])*1.0/(x_in[i]-x_in[i-1]) #rescue 0
      u[i]=(6*u[i]/(x_in[i+1]-x_in[i-1])-sig*u[i-1])/p #rescue 0
      i=i+1
    end
    qn = 0
    un = 0
    y_t[index] = (un - qn * u[index-1]) / (qn * y_t[index-1] + 1)
    k = index-1
    while k>=1
      y_t[k]=y_t[k]*	y_t[k+1]+u[k]
      k=k-1
    end
    points = []
    points << @g_area_x_offset
    points << layer_height
    @layers[0] = image_create_true_color(layer_width,layer_height)
    image_filled_rectangle(@layers[0],0,0,layer_width,layer_height, 255,255,255)
    image_color_transparent(@layers[0], 255,255,255)
    y_last = nil
    x_pos  = @g_area_x_offset
    points_count= 2
    x=1
    while(x<=index)
      klo=1
      khi=index
      k = khi-klo
      while k>1
        k=khi-klo
        if x_in[k]>=x
          khi=k
        else
          klo=k
        end
      end
      klo=khi-1
      h = x_in[khi]-x_in[klo]
      a = (x_in[khi]-x)/h rescue 1
      b = (x-x_in[klo])/h rescue 1
      value = a*y_in[klo]+b*y_in[khi]+((a*a*a-a)*y_t[klo]+(b*b*b-b)*y_t[khi])*(h*h)/6
      y_pos = layer_height - ((value-@vmin) * @division_ratio)

      a_points  = []
      if ( !y_last.nil? && around_zero && (missing[x.floor].nil?) && (missing[(x+1).floor].nil?))

        a_points << x_last
        a_points << y_last
        a_points << x_pos
        a_points << y_pos
        a_points << x_pos
        a_points << y_zero
        a_points << x_last
        a_points << y_zero
        #check No of points here 4 is pass check in image filled_polygon
        image_filled_polygon(@layers[0], a_points, @palette[color_id]["r"],@palette[color_id]["g"],@palette[color_id]["b"],4)
      end
      if ( missing[(x.floor)].nil? || y_last.nil?)
        points_count = points_count+1
        points << x_pos
        points << y_pos
      else
        points_count = points_count+1
        points << x_last
        points << y_last
      end
      y_last = y_pos
      x_last = x_pos
      x_pos  = x_pos + @division_width * accuracy
      x=x+accuracy
    end

    #// Add potentialy missing values
    # a_points  = []
    x_pos  = x_pos - @division_width * accuracy
    if ( x_pos < (layer_width-@g_area_x_offset) )
      y_pos = layer_height - ((y_in[index]-@vmin) * @division_ratio)
      if ( !y_last.nil? && around_zero )
        a_points << x_last
        a_points <<  y_last
        a_points << (layer_width-@g_area_x_offset)
        a_points << y_pos
        a_points << (layer_width-@g_area_x_offset)
        a_points << y_zero
        a_points <<  x_last
        a_points << y_zero
        #  imagefilledpolygon(@layers[0],a_points,4,$C_Graph)
        image_filled_polygon(@layers[0], a_points, @palette[color_id]["r"],@palette[color_id]["g"],@palette[color_id]["b"],4)
      end

      if ( y_in[klo] != "" && y_in[khi] != "" || y_last.nil? )

        points_count +=1
        points << (layer_width-@g_area_x_offset).floor
        points << (y_pos).floor
      end
    end

    points << (layer_width-@g_area_x_offset).floor
    points << layer_height.floor

    if ( !around_zero )
      image_filled_polygon(@layers[0], points, @palette[color_id]["r"],@palette[color_id]["g"],@palette[color_id]["b"],points_count)
    end

    image_copy_merge(@layers[0],@picture,@g_area_x1,@g_area_y1,0,0,layer_width,layer_height,alpha)
    image_destroy(@layers[0])

    	draw_cubic_curve(data, data_description,accuracy,col_name)
    graph_id+=1
  end
end