Skip to content

Commit b155d2d

Browse files
committed
Dictionary syntax
1 parent 44c12e0 commit b155d2d

File tree

3 files changed

+79
-79
lines changed

3 files changed

+79
-79
lines changed

src/Plotly.jl

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ include("plot.jl")
66
include("utils.jl")
77

88
type CurrentPlot
9-
filename::ASCIIASCIIString
9+
filename::ASCIIString
1010
fileopt::ASCIIString
1111
url::ASCIIString
1212
end
1313

1414
api_version = "v2"
1515

16-
default_options = {"filename"=>"Plot from Julia API",
17-
"world_readable"=> true,
18-
"layout"=>{""=>""}}
16+
default_options = Dict("filename"=>"Plot from Julia API",
17+
"world_readable"=> true,
18+
"layout"=>Dict(""=>""))
1919

2020
## Taken from https://github.com/johnmyleswhite/Vega.jl/blob/master/src/Vega.jl#L51
2121
# Open a URL in a browser
@@ -25,10 +25,10 @@ function openurl(url::ASCIIString)
2525
@linux_only run(`xdg-open $url`)
2626
end
2727

28-
default_opts = {
29-
"origin" => "plot",
30-
"platform" => "Julia",
31-
"version" => "0.2"}
28+
default_opts = Dict(
29+
"origin" => "plot",
30+
"platform" => "Julia",
31+
"version" => "0.2")
3232

3333
function get_plot_endpoint()
3434
config = get_config()
@@ -51,12 +51,12 @@ function plot(data::Array,options=Dict())
5151
opt = merge(default_options,options)
5252
r = post(endpoint,
5353
merge(default_opts,
54-
{
54+
Dict(
5555
"un" => creds.username,
5656
"key" => creds.api_key,
5757
"args" => json(data),
5858
"kwargs" => json(opt)
59-
})
59+
))
6060
)
6161
body=JSON.parse(bytestring(r.body))
6262

@@ -79,11 +79,11 @@ function layout(layout_opts::Dict,meta_opts=Dict())
7979

8080
r = post(endpoint,
8181
merge(default_opts,
82-
{"un" => creds.username,
82+
Dict("un" => creds.username,
8383
"key" => creds.api_key,
8484
"args" => json(layout_opts),
8585
"origin" => "layout",
86-
"kwargs" => json(meta_opts)}))
86+
"kwargs" => json(meta_opts))))
8787
__parseresponse(r)
8888
end
8989

@@ -95,11 +95,11 @@ function style(style_opts,meta_opts=Dict())
9595

9696
r = post(endpoint,
9797
merge(default_opts,
98-
{"un" => creds.username,
98+
Dict("un" => creds.username,
9999
"key" => creds.api_key,
100100
"args" => json([style_opts]),
101101
"origin" => "style",
102-
"kwargs" => json(meta_opts)}))
102+
"kwargs" => json(meta_opts))))
103103
__parseresponse(r)
104104
end
105105

@@ -161,9 +161,9 @@ end
161161

162162
function get_template(format_type::ASCIIString)
163163
if format_type == "layout"
164-
return {
164+
return Dict(
165165
"title"=>"Click to enter Plot title",
166-
"xaxis"=>{
166+
"xaxis"=>Dict(
167167
"range"=>[-1,6],
168168
"type"=>"-",
169169
"mirror"=>true,
@@ -190,9 +190,9 @@ function get_template(format_type::ASCIIString)
190190
"zerolinewidth"=>1,
191191
"title"=>"Click to enter X axis title",
192192
"unit"=>"",
193-
"titlefont"=>{"family"=>"","size"=>0,"color"=>""},
194-
"tickfont"=>{"family"=>"","size"=>0,"color"=>""}},
195-
"yaxis"=>{
193+
"titlefont"=>Dict("family"=>"","size"=>0,"color"=>""),
194+
"tickfont"=>Dict("family"=>"","size"=>0,"color"=>"")),
195+
"yaxis"=>Dict(
196196
"range"=>[-1,4],
197197
"type"=>"-",
198198
"mirror"=>true,
@@ -219,18 +219,18 @@ function get_template(format_type::ASCIIString)
219219
"zerolinewidth"=>1,
220220
"title"=>"Click to enter Y axis title",
221221
"unit"=>"",
222-
"titlefont"=>{"family"=>"","size"=>0,"color"=>""},
223-
"tickfont"=>{"family"=>"","size"=>0,"color"=>""}},
224-
"legend"=>{
222+
"titlefont"=>Dict("family"=>"","size"=>0,"color"=>""),
223+
"tickfont"=>Dict("family"=>"","size"=>0,"color"=>"")),
224+
"legend"=>Dict(
225225
"bgcolor"=>"#fff",
226226
"bordercolor"=>"#000",
227227
"borderwidth"=>1,
228-
"font"=>{"family"=>"","size"=>0,"color"=>""},
229-
"traceorder"=>"normal"},
228+
"font"=>Dict("family"=>"","size"=>0,"color"=>""),
229+
"traceorder"=>"normal"),
230230
"width"=>700,
231231
"height"=>450,
232232
"autosize"=>"initial",
233-
"margin"=>{"l"=>80,"r"=>80,"t"=>80,"b"=>80,"pad"=>2},
233+
"margin"=>Dict("l"=>80,"r"=>80,"t"=>80,"b"=>80,"pad"=>2),
234234
"paper_bgcolor"=>"#fff",
235235
"plot_bgcolor"=>"#fff",
236236
"barmode"=>"stack",
@@ -239,10 +239,10 @@ function get_template(format_type::ASCIIString)
239239
"boxmode"=>"overlay",
240240
"boxgap"=>0.3,
241241
"boxgroupgap"=>0.3,
242-
"font"=>{"family"=>"Arial, sans-serif;","size"=>12,"color"=>"#000"},
243-
"titlefont"=>{"family"=>"","size"=>0,"color"=>""},
242+
"font"=>Dict("family"=>"Arial, sans-serif;","size"=>12,"color"=>"#000"),
243+
"titlefont"=>Dict("family"=>"","size"=>0,"color"=>""),
244244
"dragmode"=>"zoom",
245-
"hovermode"=>"x"}
245+
"hovermode"=>"x")
246246
end
247247
end
248248

src/plot.jl

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function get_points(f::Function, options=Dict())
2-
default = {"left"=>-10, "right"=>10, "step"=>0.5, "name"=>"$f", "type"=>"scatter", "mode"=>"lines"}
2+
default = Dict("left"=>-10, "right"=>10, "step"=>0.5, "name"=>"$f", "type"=>"scatter", "mode"=>"lines")
33
opt = merge(default, options)
44
n::Int = (opt["right"] - opt["left"]) / opt["step"] + 1
55
X = Float64[0 for i in 1:n]
@@ -12,31 +12,31 @@ function get_points(f::Function, options=Dict())
1212
end
1313

1414
if opt["type"] == "histogram"
15-
return {"x"=>Y, "type"=>opt["type"], "mode"=>opt["mode"], "name"=>opt["name"]}
15+
return Dict("x"=>Y, "type"=>opt["type"], "mode"=>opt["mode"], "name"=>opt["name"])
1616
elseif opt["type"] == "box"
17-
return {"y"=>Y, "type"=>opt["type"], "mode"=>opt["mode"], "name"=>opt["name"]}
17+
return Dict("y"=>Y, "type"=>opt["type"], "mode"=>opt["mode"], "name"=>opt["name"])
1818
else
19-
return {"x"=>X, "y"=>Y, "type"=>opt["type"], "mode"=>opt["mode"], "name"=>opt["name"]}
19+
return Dict("x"=>X, "y"=>Y, "type"=>opt["type"], "mode"=>opt["mode"], "name"=>opt["name"])
2020
end
2121
end
2222

23-
scatter(f::Array, options=Dict()) = merge({"type"=>"scatter","mode"=>"markers", "x"=>[1:length(f)], "y"=>f}, options)
24-
line(f::Array, options=Dict()) = merge({"type"=>"scatter","mode"=>"lines", "x"=>[1:length(f)], "y"=>f}, options)
25-
box(f::Array, options=Dict()) = merge({"type"=>"box", "x"=>[1:length(f)], "y"=>f}, options)
26-
histogram(f::Array, options=Dict()) = merge({"type"=>"histogram", "y"=>[1:length(f)], "x"=>f}, options)
27-
bar(f::Array, options=Dict()) = merge({"type"=>"bar", "x"=>[1:length(f)], "y"=>f}, options)
23+
scatter(f::Array, options=Dict()) = merge(Dict("type"=>"scatter","mode"=>"markers", "x"=>[1:length(f)], "y"=>f), options)
24+
line(f::Array, options=Dict()) = merge(Dict("type"=>"scatter","mode"=>"lines", "x"=>[1:length(f)], "y"=>f), options)
25+
box(f::Array, options=Dict()) = merge(Dict("type"=>"box", "x"=>[1:length(f)], "y"=>f), options)
26+
histogram(f::Array, options=Dict()) = merge(Dict("type"=>"histogram", "y"=>[1:length(f)], "x"=>f), options)
27+
bar(f::Array, options=Dict()) = merge(Dict("type"=>"bar", "x"=>[1:length(f)], "y"=>f), options)
2828

29-
scatter(f::Dict, options=Dict()) = merge({"type"=>"scatter","mode"=>"markers", "x"=>[k for k in sort(collect(keys(f)))], "y"=>[f[k] for k in sort(collect(keys(f)))]}, options)
30-
line(f::Dict, options=Dict()) = merge({"type"=>"scatter","mode"=>"lines", "x"=>[k for k in sort(collect(keys(f)))], "y"=>[f[k] for k in sort(collect(keys(f)))]}, options)
31-
box(f::Dict, options=Dict()) = merge({"type"=>"box", "x"=>[k for k in sort(collect(keys(f)))], "y"=>[f[k] for k in sort(collect(keys(f)))]}, options)
32-
histogram(f::Dict, options=Dict()) = merge({"type"=>"histogram", "x"=>[k for k in sort(collect(keys(f)))], "y"=>[f[k] for k in sort(collect(keys(f)))]}, options)
33-
bar(f::Dict, options=Dict()) = merge({"type"=>"bar", "x"=>[k for k in sort(collect(keys(f)))], "y"=>[f[k] for k in sort(collect(keys(f)))]}, options)
29+
scatter(f::Dict, options=Dict()) = merge(Dict("type"=>"scatter","mode"=>"markers", "x"=>[k for k in sort(collect(keys(f)))], "y"=>[f[k] for k in sort(collect(keys(f)))]), options)
30+
line(f::Dict, options=Dict()) = merge(Dict("type"=>"scatter","mode"=>"lines", "x"=>[k for k in sort(collect(keys(f)))], "y"=>[f[k] for k in sort(collect(keys(f)))]), options)
31+
box(f::Dict, options=Dict()) = merge(Dict("type"=>"box", "x"=>[k for k in sort(collect(keys(f)))], "y"=>[f[k] for k in sort(collect(keys(f)))]), options)
32+
histogram(f::Dict, options=Dict()) = merge(Dict("type"=>"histogram", "x"=>[k for k in sort(collect(keys(f)))], "y"=>[f[k] for k in sort(collect(keys(f)))]), options)
33+
bar(f::Dict, options=Dict()) = merge(Dict("type"=>"bar", "x"=>[k for k in sort(collect(keys(f)))], "y"=>[f[k] for k in sort(collect(keys(f)))]), options)
3434

35-
scatter(f::Function, options=Dict()) = get_points(f, merge({"type"=>"scatter","mode"=>"markers"}, options))
36-
line(f::Function, options=Dict()) = get_points(f, merge({"type"=>"scatter","mode"=>"lines"}, options))
37-
box(f::Function, options=Dict()) = get_points(f, merge({"type"=>"box"}, options))
38-
histogram(f::Function, options=Dict()) = get_points(f, merge({"type"=>"histogram"}, options))
39-
bar(f::Function, options=Dict()) = get_points(f, merge({"type"=>"bar"}, options))
35+
scatter(f::Function, options=Dict()) = get_points(f, merge(Dict("type"=>"scatter","mode"=>"markers"), options))
36+
line(f::Function, options=Dict()) = get_points(f, merge(Dict("type"=>"scatter","mode"=>"lines"), options))
37+
box(f::Function, options=Dict()) = get_points(f, merge(Dict("type"=>"box"), options))
38+
histogram(f::Function, options=Dict()) = get_points(f, merge(Dict("type"=>"histogram"), options))
39+
bar(f::Function, options=Dict()) = get_points(f, merge(Dict("type"=>"bar"), options))
4040

4141
plot(f::Function, options=Dict()) = plot([line(f, options)])
4242
plot(fs::Array{Function,1}, options=Dict()) = plot([line(f, options) for f in fs])
@@ -51,7 +51,7 @@ if Pkg.installed("Polynomial") !== nothing
5151
bar(p::Poly, options=Dict()) = bar(x->polyval(p,x), options)
5252

5353
function plot{T<:Number}(ps::Array{Poly{T},1}, options=Dict())
54-
data = [get_points(x->polyval(p,x), merge({"name"=>"$p"}, options)) for p in ps]
54+
data = [get_points(x->polyval(p,x), merge(Dict("name"=>"$p"), options)) for p in ps]
5555
return plot([data], options)
5656
end
5757

@@ -64,14 +64,14 @@ if Pkg.installed("TimeSeries") !== nothing
6464
import TimeSeries: TimeArray, timestamp, values, colnames
6565

6666
scatter(ts::TimeArray, options=Dict()) = [
67-
{"x"=>map(t->"$t", timestamp(ts[col])), "y"=>values(ts[col]), "type"=>"scatter", "mode"=>"markers", "name"=>col}
67+
Dict("x"=>map(t->"$t", timestamp(ts[col])), "y"=>values(ts[col]), "type"=>"scatter", "mode"=>"markers", "name"=>col)
6868
for col in colnames(ts)
6969
]
7070

71-
line(ts::TimeArray, options=Dict()) = [merge(x,{"type"=>"line","mode"=>"lines"}) for x in scatter(ts)]
72-
box(ts::TimeArray, options=Dict()) = [merge(x,{"type"=>"box"}) for x in scatter(ts)]
73-
histogram(ts::TimeArray, options=Dict()) = [merge(x,{"type"=>"histogram"}) for x in scatter(ts)]
74-
bar(ts::TimeArray, options=Dict()) = [merge(x,{"type"=>"bar"}) for x in scatter(ts)]
71+
line(ts::TimeArray, options=Dict()) = [merge(x,Dict("type"=>"line","mode"=>"lines")) for x in scatter(ts)]
72+
box(ts::TimeArray, options=Dict()) = [merge(x,Dict("type"=>"box")) for x in scatter(ts)]
73+
histogram(ts::TimeArray, options=Dict()) = [merge(x,Dict("type"=>"histogram")) for x in scatter(ts)]
74+
bar(ts::TimeArray, options=Dict()) = [merge(x,Dict("type"=>"bar")) for x in scatter(ts)]
7575
plot(ts::TimeArray, options=Dict()) = plot([line(ts)], options)
7676
end
7777

@@ -80,30 +80,30 @@ if Pkg.installed("WAV") !== nothing
8080
w, Fs = wav
8181
X = [f/Fs for f in 1.0:length(w)]
8282
Y = [round(y,8) for y in w]
83-
return {"x"=>X, "y"=>Y, "type"=>"scatter", "mode"=>"lines", "name"=>"WAV data"}
83+
return Dict("x"=>X, "y"=>Y, "type"=>"scatter", "mode"=>"lines", "name"=>"WAV data")
8484
end
8585

86-
scatter{T<:Number,U<:Number,V<:Number}(wav::(Array{T,2},U,V,UnionType), options=Dict()) = merge(line(wav),{"type"=>"scatter","mode"=>"markers"})
87-
box{T<:Number,U<:Number,V<:Number}(wav::(Array{T,2},U,V,UnionType), options=Dict()) = merge(line(wav),{"type"=>"box"})
88-
histogram{T<:Number,U<:Number,V<:Number}(wav::(Array{T,2},U,V,UnionType), options=Dict()) = merge(line(wav),{"type"=>"histogram"})
89-
bar{T<:Number,U<:Number,V<:Number}(wav::(Array{T,2},U,V,UnionType), options=Dict()) = merge(line(wav),{"type"=>"bar"})
86+
scatter{T<:Number,U<:Number,V<:Number}(wav::(Array{T,2},U,V,UnionType), options=Dict()) = merge(line(wav),Dict("type"=>"scatter","mode"=>"markers"))
87+
box{T<:Number,U<:Number,V<:Number}(wav::(Array{T,2},U,V,UnionType), options=Dict()) = merge(line(wav),Dict("type"=>"box"))
88+
histogram{T<:Number,U<:Number,V<:Number}(wav::(Array{T,2},U,V,UnionType), options=Dict()) = merge(line(wav),Dict("type"=>"histogram"))
89+
bar{T<:Number,U<:Number,V<:Number}(wav::(Array{T,2},U,V,UnionType), options=Dict()) = merge(line(wav),Dict("type"=>"bar"))
9090

9191
function plot{T<:Number,U<:Number,V<:Number}(wav::(Array{T,2},U,V,UnionType), options=Dict())
92-
opt = merge({"layout"=>{"xaxis"=>{"title"=>"seconds","dtick"=>1,"tick0"=>0,"autotick"=>false}}}, options)
92+
opt = merge(Dict("layout"=>Dict("xaxis"=>Dict("title"=>"seconds","dtick"=>1,"tick0"=>0,"autotick"=>false))), options)
9393
return plot([line(wav)], opt)
9494
end
9595
end
9696

9797
if Pkg.installed("DataFrames") !== nothing
9898
import DataFrames: DataFrame
9999

100-
scatter(df::DataFrame, options=Dict()) = get_points(df, merge({"type"=>"scatter","mode"=>"markers"}, options))
101-
line(df::DataFrame, options=Dict()) = get_points(df, merge({"type"=>"scatter","mode"=>"lines"}, options))
102-
box(df::DataFrame, options=Dict()) = get_points(df, merge({"type"=>"box"}, options))
103-
histogram(df::DataFrame, options=Dict()) = get_points(df, merge({"type"=>"histogram"}, options))
104-
bar(df::DataFrame, options=Dict()) = get_points(df, merge({"type"=>"bar"}, options))
100+
scatter(df::DataFrame, options=Dict()) = get_points(df, merge(Dict("type"=>"scatter","mode"=>"markers"), options))
101+
line(df::DataFrame, options=Dict()) = get_points(df, merge(Dict("type"=>"scatter","mode"=>"lines"), options))
102+
box(df::DataFrame, options=Dict()) = get_points(df, merge(Dict("type"=>"box"), options))
103+
histogram(df::DataFrame, options=Dict()) = get_points(df, merge(Dict("type"=>"histogram"), options))
104+
bar(df::DataFrame, options=Dict()) = get_points(df, merge(Dict("type"=>"bar"), options))
105105
function get_points(df::DataFrame, options=Dict())
106-
default = {"type"=>"scatter", "mode"=>"lines"}
106+
default = Dict("type"=>"scatter", "mode"=>"lines")
107107
opt = merge(default, options)
108108
for axis in ["xs", "ys"]
109109
if haskey(opt, axis) && typeof(opt[axis]) <: Symbol
@@ -114,27 +114,27 @@ if Pkg.installed("DataFrames") !== nothing
114114
if haskey(opt, "xs") && haskey(opt, "ys")
115115
if length(opt["xs"]) == length(opt["ys"])
116116
return [
117-
{"x"=>df[opt["xs"][i]], "y"=>df[opt["ys"][i]], "type"=>opt["type"], "mode"=>opt["mode"]}
117+
Dict("x"=>df[opt["xs"][i]], "y"=>df[opt["ys"][i]], "type"=>opt["type"], "mode"=>opt["mode"])
118118
for i in 1:length(opt["xs"])
119119
]
120120
else
121121
return [
122-
{"x"=>df[x], "y"=>df[y], "type"=>opt["type"], "mode"=>opt["mode"]}
122+
Dict("x"=>df[x], "y"=>df[y], "type"=>opt["type"], "mode"=>opt["mode"])
123123
for x in opt["xs"], y in opt["ys"]
124124
]
125125
end
126126
elseif haskey(opt, "xs")
127127
return [
128-
{"x"=>df[x], "type"=>opt["type"], "mode"=>opt["mode"]}
128+
Dict("x"=>df[x], "type"=>opt["type"], "mode"=>opt["mode"])
129129
for x in opt["xs"]
130130
]
131131
elseif haskey(opt, "ys")
132132
return [
133-
{"y"=>df[y], "type"=>opt["type"], "mode"=>opt["mode"]}
133+
Dict("y"=>df[y], "type"=>opt["type"], "mode"=>opt["mode"])
134134
for y in opt["ys"]
135135
]
136136
else
137-
return {"error"=>"Please set the xs and/or ys options."}
137+
return Dict("error"=>"Please set the xs and/or ys options.")
138138
end
139139
end
140140

@@ -146,7 +146,7 @@ if Pkg.installed("DataFrames") !== nothing
146146
elseif haskey(options, "ys")
147147
return plot([box(df)], options)
148148
else
149-
return {"error"=>"Please set the xs and/or ys options."}
149+
return Dict("error"=>"Please set the xs and/or ys options.")
150150
end
151151
end
152-
end
152+
end

src/utils.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using JSON
22

3-
default_endpoints = {
4-
"base" => "https://plot.ly",
5-
"api" => "https://api.plot.ly/v2"}
3+
default_endpoints = Dict(
4+
"base" => "https://plot.ly",
5+
"api" => "https://api.plot.ly/v2")
66

77
type PlotlyCredentials
88
username::ASCIIString
@@ -148,13 +148,13 @@ function get_credentials_file()
148148
plotly_credentials_file = joinpath(plotly_credentials_folder, ".credentials")
149149

150150
if !isfile(plotly_credentials_file)
151-
creds = {}
151+
creds = Dict()
152152
else
153153
creds_file = open(plotly_credentials_file)
154154
creds = JSON.parse(creds_file)
155155

156156
if creds == nothing
157-
creds = {}
157+
creds = Dict()
158158
end
159159

160160
end
@@ -172,13 +172,13 @@ function get_config_file()
172172
plotly_config_file = joinpath(plotly_config_folder, ".config")
173173

174174
if !isfile(plotly_config_file)
175-
config = {}
175+
config = Dict()
176176
else
177177
config_file = open(plotly_config_file)
178178
config = JSON.parse(config_file)
179179

180180
if config == nothing
181-
config = {}
181+
config = Dict()
182182
end
183183

184184
end

0 commit comments

Comments
 (0)