Class: Rdata

Inherits:
Object
  • Object
show all
Defined in:
lib/rdata.rb

Instance Method Summary collapse

Constructor Details

#initializeRdata

Data will be extracted from this object using get_data and get_data_description



7
8
9
10
11
12
13
14
# File 'lib/rdata.rb', line 7

def initialize
	@data                          = []
	@data_description              = {}
	@data_description["position"]  = "name"
	@data_description["format"]    = {"x"=>"number","y" => "number"}
	@data_description["unit"]      = {"x" => "","y"=>""}
	@data_description["symbol"] = {}
end

Instance Method Details

#add_all_seriesObject

This function can be used to set all data series as graphable. They’ll all be graphed when calling a chart function of the Rchart class. There is no change on Data, only the “Graphable” attribute is modified Generate some data…

This will mark both Serie1 & Serie2 as “graphable”

  • chart_data.add_all_series



93
94
95
96
97
98
99
100
101
102
# File 'lib/rdata.rb', line 93

def add_all_series
	@data_description["values"] = []
	if(!@data[0].nil?)
		@data[0].each do |k,v|
			if (k != "name" )
				@data_description["values"].push(k)
			end
		end
	end
end

#add_point(value, serie = "Serie1", description = "") ⇒ Object

This function can be used to add one or multiple points to a data serie. By default points are added to Serie1. This will the value 25 as the last point of Serie1

  • chart_data.add_point(25)

This will the value 2,4,9,5,1,0 as the last point of Serie1

This will the value 12 as the last point of Serie2

  • chart_data.add_point(12,“Serie2”)

add the desciption “March” to the Serie2

  • chart_data.add_point(12,“Serie2”,“March”)



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

def add_point(value,serie="Serie1",description="")
	if ((value.is_a?(Array)) && value.count == 1)
		value = value[0]
	end
	id = 0
	@data.each_with_index do |v,index|
		id = index+1  unless @data[index][serie].nil?
	end
	if value.is_a?(Numeric)
		if @data[id].nil?
			@data[id]={serie => value}
		else
			@data[id]= @data[id].merge(serie => value)
		end
		if description != ""
			@data[id]["name"] = description
		elsif @data[id]["name"].nil?
			@data[id]["name"] = id
		end
	else

		value.each do |k|
			if @data[id].nil?
				@data[id] = {serie=>k}#TODO check k
			else
				@data[id] = @data[id].merge({serie=>k})
			end

			@data[id]["name"] = id  if @data[id]["name"].nil?
			id = id+1
		end

	end

end

#add_serie(serie_name = "Serie1") ⇒ Object

This function can be used to add a new data serie to the data_description. All the series declared in this object will be graphed when calling a chart function of the Rchart class. There is no change on Data, only the “Graphable” attribute is modified. Generate some data…

This will mark both Serie1 & Serie2 as “graphable” but not Serie3

  • chart_data.add_serie(“Serie1”)

  • chart_data.add_serie(“Serie2”)



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rdata.rb', line 71

def add_serie(serie_name="Serie1")

	if (@data_description["values"].nil?)
		@data_description["values"]  = [serie_name]
	else
		found = false
		@data_description["values"].each do |k|
			found = true if ( k == serie_name )  #TODO check
		end
		@data_description["values"] << serie_name if (!found )
	end

end

#get_dataObject

This function is used everytime you want to retrieve the Data stored in the Rdata structure



232
233
234
# File 'lib/rdata.rb', line 232

def get_data
	@data
end

#get_data_descriptionObject

This function is used everytime you want to retrieve the Data description stored in the Rdata structure



236
237
238
# File 'lib/rdata.rb', line 236

def get_data_description
	@data_description
end

#remove_all_seriesObject

This function can be used to remove the description of a serie. This description will be written on the graph when calling the drawLegend function. Removing it’s name using this function can be usefull to hide previously used series



225
226
227
228
229
# File 'lib/rdata.rb', line 225

def remove_all_series
	@data_description["values"].each do |v|
		@data_description["values"] = []
	end
end

#remove_serie(serie_name = "Serie1") ⇒ Object

This function can be used to remove a data series from the graphable ones. They’ll all be graphed when calling a chart function of the Rchart class. There is no change on Data, only the “Graphable” attribute is modified. Generate some data…

This will mark both Serie1 & Serie2 as “graphable”

  • chart_data.add_all_series

This will remove the “graphable” status of Serie2

  • chart_data.remove_serie(“Serie2”)



114
115
116
117
118
119
120
121
# File 'lib/rdata.rb', line 114

def remove_serie(serie_name="Serie1")
	if (!@data_description["values"].nil?)
		found = false
		@data_description["values"].each do |v|
			@data_description["values"].delete(v)  if (v == serie_name )
		end
	end
end

#remove_serie_name(serie_name) ⇒ Object

This function can be used to remove the description of a serie. This description will be written on the graph when calling the drawLegend function. Removing it’s name using this function can be usefull to hide previously used series Generate some data…

This will set the name of Serie1 to “January”

  • chart_data.set_serie_name(“January”)

This will set the name of Serie2 to “February”

  • chart_data.set_serie_name(“February”,“Serie2”)

Ths will remove name of Serie1

  • chart_data.remove_serie_name(“Serie1”)



216
217
218
219
220
# File 'lib/rdata.rb', line 216

def remove_serie_name(serie_name)
	if(!@data_description["description"][serie_name].nil?)
		@data_description["description"].delete(serie_name)
	end
end

#set_abscise_label_serie(serie_name = "name") ⇒ Object

his function can be used to set which serie is used (if any) as abcisse value Generate some data…

  • chart_data.add_point([“Jan”,“Feb”,“Mar”),“Serie1”)

  • chart_data.add_point([2,4,9),“Serie2”)

  • chart_data.add_point([1,1,2),“Serie3”)

This will mark both Serie1 & Serie2 as “graphable”

  • chart_data.add_serie(“Serie2”)

  • chart_data.add_serie(“Serie3”)

Set Serie as abcisse label

  • chart_data.set_abscise_label_serie(“Serie1”)



132
133
134
# File 'lib/rdata.rb', line 132

def set_abscise_label_serie(serie_name = "name")
	@data_description["position"] = serie_name
end

#set_serie_name(name, serie_name = "Serie1") ⇒ Object

This function can be used to set the description of a serie. This description will be written on the graph when calling the draw_legend function Generate some data…

  • chart_data.add_point([2,4,9),“Serie1”)

  • chart_data.add_point([1,1,2),“Serie2”)

This will set the name of Serie1 to “January”

  • chart_data.set_serie_name(“January”)

This will set the name of Serie2 to “February”

  • chart_data.set_serie_name(“February”,“Serie2”)



145
146
147
148
149
150
151
# File 'lib/rdata.rb', line 145

def set_serie_name(name,serie_name="Serie1")
	if @data_description["description"].nil?
		@data_description["description"]={serie_name => name}
	else
		@data_description["description"] = @data_description["description"].merge(serie_name => name)
	end
end

#set_serie_symbol(name, symbol) ⇒ Object



201
202
203
# File 'lib/rdata.rb', line 201

def set_serie_symbol(name,symbol)
	@data_description["symbol"][name] = symbol
end

#set_x_axis_format(format = "number") ⇒ Object

With this function you can set the format of the X axis values. Todays formats are the following :

  • number used by defaults

  • metric number that will be displayed with k/m/g units



176
177
178
# File 'lib/rdata.rb', line 176

def set_x_axis_format(format="number")
	@data_description["format"]["x"] = format
end

#set_x_axis_name(name = "X Axis") ⇒ Object

This will give a name to the X axis, writting it horizontally behind the chart

  • chart_data.set_x_axis_name(“Samples”)



155
156
157
158
159
160
161
# File 'lib/rdata.rb', line 155

def set_x_axis_name(name="X Axis")
	if @data_description["axis"].nil?
		@data_description["axis"]={"x" => name}
	else
		@data_description["axis"]=@data_description["axis"].merge("x" => name)
	end
end

#set_x_axis_unit(unit = "") ⇒ Object

  • chart_data.set_x_axis_unit(“km”)



189
190
191
# File 'lib/rdata.rb', line 189

def set_x_axis_unit(unit="")
	@data_description["unit"]["x"] = unit
end

#set_y_axis_format(format = "number") ⇒ Object

  • metric number that will be displayed with k/m/g units



183
184
185
# File 'lib/rdata.rb', line 183

def set_y_axis_format(format="number")
	@data_description["format"]["y"] = format
end

#set_y_axis_name(name = "Y Axis") ⇒ Object

This will give a name to the Y axis, writting it horizontally behind the chart

  • chart_data.set_y_axis_name(“Temperature”)



164
165
166
167
168
169
170
# File 'lib/rdata.rb', line 164

def set_y_axis_name(name="Y Axis")
	if @data_description["axis"].nil?
		@data_description["axis"]= {"y" => name}
	else
		@data_description["axis"]=@data_description["axis"].merge("y" => name)
	end
end

#set_y_axis_unit(unit = "") ⇒ Object

Set the axis unit. This will be appended to the axis value Give the “m/s” unit to the Y axis

  • chart_data.set_x_axis_unit(“m/s”)



197
198
199
# File 'lib/rdata.rb', line 197

def set_y_axis_unit(unit="")
	@data_description["unit"]["y"] = unit
end