Class: FunctionPlot

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

Instance Method Summary collapse

Constructor Details

#initialize(function, min, max, params) ⇒ FunctionPlot

  • function - a String function in Clojure Style

  • min - Lower Bound

  • max - Upper Bound

  • parms - an Hash whit parametrs value

Parms

This Hash contain parmas . List of key permitted .

  • :title - (default “A sample Graph”) Graph main title

  • :x_label - (default ‘Categories’) Label of x ass

  • :y_label - (default ‘Value’) Label of y ass

  • :legend - (default false) Prints legend

  • :height - (default 1500) Graph height

  • :width - (default 800) Graph width

  • :file_name - (default sample.jpg) Graph File Name/Path to save plot. You can chose jpg or pdf

Examples

Class Usage:

function = "(defn cubic [x] (+ (* x x x) (* 2 x x) (* 2 x) 3))" 
min = 0
max = 50
params = {"title" => "Plot my Function"}
plot = FunctionPlot.new(function,min,max,params)
#To view
plot.view 
#to save
plot.save


38
39
40
41
42
43
# File 'lib/function_plot/function_plot.rb', line 38

def initialize(function,min,max,params)
	@params=self.check_par(params)
	@function=function
	@min=min
	@max=max
end

Instance Method Details

#check_par(params) ⇒ Object



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

def check_par(params)
       	hash = Hash.new
	permitted = ["title","x_label","y_label","legend","width","height","file_name"]
       	keys = params.keys
       	keys.each{ |key|
               	 if permitted.include?(key)==false
                       	raise "Error : Permitted key are title,x_label,y_label,legend,width,height,file_name"
                	end
       	}
	
	if params["title"]==nil
               	hash["title"]="A sample Graph"
       	else
               	 hash["title"]=params["title"]
       	end

	if params["x_label"]==nil
                       hash["x_label"]="Categories"
               else
                        hash["x_label"]=params["x_label"]
               end

	if params["y_label"]==nil
                       hash["y_label"]="Values"
               else
                       hash["y_label"]=params["y_label"]
               end


 	if params["legend"]==nil
       	        hash["legend"]=false
     		else
               	hash["legend"]=params["legend"]
       	end
	

	if params["width"]==nil
               	hash["width"]=1500
       	else
               	hash["width"]=params["width"]
       	end

	if params["height"]==nil
               	hash["height"]=800
       	else
               	hash["height"]=params["height"]
       	end

       	if params["file_name"]==nil
               	hash["file_name"]="sample.jpg"
       	else
               	hash["file_name"]=params["file_name"]
      		end

	params_map = HashMap.new(hash)
       	return params_map
end

#getFunctionObject



103
104
105
# File 'lib/function_plot/function_plot.rb', line 103

def getFunction
	@function
end

#getMaxObject



111
112
113
# File 'lib/function_plot/function_plot.rb', line 111

def getMax
	@max
end

#getMinObject



107
108
109
# File 'lib/function_plot/function_plot.rb', line 107

def getMin
	@min 
end

#getParamsObject



115
116
117
# File 'lib/function_plot/function_plot.rb', line 115

def getParams
        @params
end

#saveObject



120
121
122
123
124
125
126
# File 'lib/function_plot/function_plot.rb', line 120

def save
	begin
       		a = Java::ComDomain::clocomics
      			a.save_function(self.getFunction,self.getMin,self.getMax,self.getParams)
	rescue Exception => ex
	end
end

#viewObject



127
128
129
130
131
132
133
134
135
# File 'lib/function_plot/function_plot.rb', line 127

def view
	 begin
                       a = Java::ComDomain::clocomics
                       a.view_function(self.getFunction,self.getMin,self.getMax,self.getParams)
               rescue Exception => ex
               end


end