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



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

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



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

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”)



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

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”)



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

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



230
231
232
# File 'lib/rdata.rb', line 230

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



234
235
236
# File 'lib/rdata.rb', line 234

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



223
224
225
226
227
# File 'lib/rdata.rb', line 223

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”)



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

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”)



214
215
216
217
218
# File 'lib/rdata.rb', line 214

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”)



130
131
132
# File 'lib/rdata.rb', line 130

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”)



143
144
145
146
147
148
149
# File 'lib/rdata.rb', line 143

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



199
200
201
# File 'lib/rdata.rb', line 199

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



174
175
176
# File 'lib/rdata.rb', line 174

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”)



153
154
155
156
157
158
159
# File 'lib/rdata.rb', line 153

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”)



187
188
189
# File 'lib/rdata.rb', line 187

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



181
182
183
# File 'lib/rdata.rb', line 181

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”)



162
163
164
165
166
167
168
# File 'lib/rdata.rb', line 162

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”)



195
196
197
# File 'lib/rdata.rb', line 195

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