This repository was archived by the owner on Apr 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathplotfunction.jl
More file actions
executable file
·239 lines (196 loc) · 6.46 KB
/
plotfunction.jl
File metadata and controls
executable file
·239 lines (196 loc) · 6.46 KB
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
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env julia
using Gtk, Gtk.ShortNames, Plots, Random
gr(legend=false)
outputdir = joinpath(tempdir(),"juliaplot" * randstring())
mkdir(outputdir)
filename = joinpath(outputdir, "plot.png")
ui = Builder(filename=joinpath(@__DIR__, "ui.glade"))
if isinteractive()
set_gtk_property!(ui["preview"], :file, "Julia_prog_language_logo.svg")
else
set_gtk_property!(ui["preview"], :file, joinpath(dirname(PROGRAM_FILE), "Julia_prog_language_logo.svg"))
end
showall(ui["win"])
mutable struct Variables
eqn::String
x::String
xs::Float64
xf::Float64
y::String
ys::Float64
yf::Float64
t::String
ts::Float64
tf::Float64
xlog::Bool
ylog::Bool
end
variables = Variables(get_gtk_property(ui["eqn"], :text, String),
get_gtk_property(ui["x"], :text, String),
eval(Meta.parse(get_gtk_property(ui["xs"], :text, String))),
eval(Meta.parse(get_gtk_property(ui["xf"], :text, String))),
get_gtk_property(ui["y"], :text, String),
eval(Meta.parse(get_gtk_property(ui["ys"], :text, String))),
eval(Meta.parse(get_gtk_property(ui["yf"], :text, String))),
get_gtk_property(ui["t"], :text, String),
eval(Meta.parse(get_gtk_property(ui["ts"], :text, String))),
eval(Meta.parse(get_gtk_property(ui["tf"], :text, String))),
get_gtk_property(ui["xlog"], :active, Bool),
get_gtk_property(ui["ylog"], :active, Bool))
function update_variables()
variables.eqn = get_gtk_property(ui["eqn"], :text, String)
variables.x = get_gtk_property(ui["x"], :text, String)
variables.xs = eval(Meta.parse(get_gtk_property(ui["xs"], :text, String)))
variables.xf = eval(Meta.parse(get_gtk_property(ui["xf"], :text, String)))
variables.y = get_gtk_property(ui["y"], :text, String)
variables.ys = eval(Meta.parse(get_gtk_property(ui["ys"], :text, String)))
variables.yf = eval(Meta.parse(get_gtk_property(ui["yf"], :text, String)))
variables.t = get_gtk_property(ui["t"], :text, String)
variables.ts = eval(Meta.parse(get_gtk_property(ui["ts"], :text, String)))
variables.tf = eval(Meta.parse(get_gtk_property(ui["tf"], :text, String)))
variables.xlog = get_gtk_property(ui["xlog"], :active, Bool)
variables.ylog = get_gtk_property(ui["ylog"], :active, Bool)
return nothing
end
function plot_option!()
plot!(title=get_gtk_property(ui["title"], :text, String))
plot!(xlabel=get_gtk_property(ui["xlabel"], :text, String))
plot!(ylabel=get_gtk_property(ui["ylabel"], :text, String))
plot!(zlabel=get_gtk_property(ui["zlabel"], :text, String))
if variables.xlog
plot!(xscale=:log10)
end
if variables.ylog
plot!(yscale=:log10)
end
end
function genfn_single()
if occursin("=", variables.eqn)
eqn = split(variables.eqn, "=")[2]
else
eqn = variables.eqn
end
f = eval(Meta.parse(variables.x * "->" * eqn))
return f
end
function genfn_double()
x = variables.xs:variables.xf
y = variables.ys:variables.yf
if occursin("=", variables.eqn)
eqn = split(variables.eqn, "=")[2]
else
eqn = variables.eqn
end
f = eval(Meta.parse( "(" * variables.x * "," * variables.y * ")" * "->" * eqn))
return x, y, f
end
function genfn_para()
if length(split(variables.eqn, ",")) == 2
de = split(variables.eqn, ",")
if occursin("=", de[1])
e1 = split(de[1], "=")[2]
else
e1 = de[1]
end
if occursin("=", de[2])
e2 = split(de[2], "=")[2]
else
e2 = de[2]
end
f = eval(Meta.parse(variables.t * "->" * e1))
g = eval(Meta.parse(variables.t * "->" * e2))
return f, g
else
de = split(variables.eqn, ",")
if occursin("=", de[1])
e1 = split(de[1], "=")[2]
else
e1 = de[1]
end
if occursin("=", de[2])
e2 = split(de[2], "=")[2]
else
e2 = de[2]
end
if occursin("=", de[3])
e3 = split(de[3], "=")[2]
else
e3 = de[3]
end
f = eval(Meta.parse(variables.t * "->" * e1))
g = eval(Meta.parse(variables.t * "->" * e2))
h = eval(Meta.parse(variables.t * "->" * e3))
return f, g, h
end
end
function plot2d_button()
update_variables()
f = genfn_single()
plot(x-> Base.invokelatest(f,x), variables.xs, variables.xf)
plot_option!()
savefig(filename)
set_gtk_property!(ui["preview"], :file, filename)
println("plot 2d function")
return nothing
end
function contour_button()
update_variables()
x, y, f = genfn_double()
contour(x, y, (x,y) -> Base.invokelatest(f, x, y), fill=true, colorbar=true)
plot_option!()
savefig(filename)
set_gtk_property!(ui["preview"], :file, filename)
println("contour")
return nothing
end
function para2d_button()
update_variables()
f, g = genfn_para()
plot(t1 -> Base.invokelatest(f,t1), t2 -> Base.invokelatest(g,t2), variables.ts, variables.tf)
plot_option!()
savefig(filename)
set_gtk_property!(ui["preview"], :file, filename)
println("parametric function 2d")
return nothing
end
function plot3d_button()
update_variables()
x, y, f = genfn_double()
surface(x, y, (x,y) -> Base.invokelatest(f, x, y))
plot_option!()
savefig(filename)
set_gtk_property!(ui["preview"], :file, filename)
println("plot 3d function")
return nothing
end
function para3d_button()
update_variables()
f, g, h = genfn_para()
plot3d(t -> Base.invokelatest(f,t),
t -> Base.invokelatest(g,t),
t -> Base.invokelatest(h,t),
variables.ts, variables.tf)
plot_option!()
savefig(filename)
set_gtk_property!(ui["preview"], :file, filename)
println("parametric function 3d")
return nothing
end
function savefig_button()
dstname = save_dialog("Save figure")
cp(filename, dstname, remove_destination=true)
return nothing
end
signal_connect(x -> plot2d_button(), ui["plot2d"], "clicked")
signal_connect(x -> contour_button(), ui["contour"], "clicked")
signal_connect(x -> para2d_button(), ui["para2d"], "clicked")
signal_connect(x -> plot3d_button(), ui["plot3d"], "clicked")
signal_connect(x -> para3d_button(), ui["para3d"], "clicked")
signal_connect(x -> savefig_button(), ui["savefig"], "clicked")
if !isinteractive()
c = Condition()
signal_connect(ui["win"], :destroy) do widget
notify(c)
end
wait(c)
end