Skip to content

Commit

Permalink
more title fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
montyvesselinov committed Jan 1, 2025
1 parent 07bd718 commit c98af1a
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/NMFkPlotMap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,17 @@ function plotmap(lon::AbstractVector{T1}, lat::AbstractVector{T1}, color::Abstra
return p
end

function plotly_title_length(title::AbstractString, length::Number)
if length <= 0
return title
else
title_vector = split(title, ' ')
pushfirst!(title_vector, repeat("&nbsp;", length)) # adding nonbreaking spaces to control the colorbar size/position
title_adjusted = join(title_vector, "<br>")
return title_adjusted
end
end

function mapbox(df::DataFrames.DataFrame; column::Union{Symbol,AbstractString}="", filename::AbstractString="", title_length::Number=0, kw...)
regex_lon = r"^[Xx]$|^[Ll]on" # regex for longitude
regex_lat = r"^[Yy]$|^[Ll]at" # regex for latitude
Expand All @@ -252,10 +263,7 @@ function mapbox(df::DataFrames.DataFrame; column::Union{Symbol,AbstractString}="
else
f = ""
end
tv = split(string(a), ' ')
pushfirst!(tv, repeat("&nbsp;", title_length)) # adding nonbreaking spaces to control the colorbar size/position
aa = join(tv, "<br>")
p = mapbox(lon, lat, df[!, a]; filename=f, title=aa, kw...)
p = mapbox(lon, lat, df[!, a]; filename=f, title=plotly_title_length(a, title_length), kw...)
display(p)
end
end
Expand All @@ -265,7 +273,7 @@ function mapbox(df::DataFrames.DataFrame; column::Union{Symbol,AbstractString}="
end
end

function mapbox(lon::AbstractVector{T1}, lat::AbstractVector{T1}, M::AbstractMatrix{T2}, names::AbstractVector=["Column $i" for i = eachcol(M)]; filename::AbstractString="", kw...) where {T1 <: AbstractFloat, T2 <: AbstractFloat}
function mapbox(lon::AbstractVector{T1}, lat::AbstractVector{T1}, M::AbstractMatrix{T2}, names::AbstractVector=["Column $i" for i = eachcol(M)]; filename::AbstractString="", title_length::Number=0, kw...) where {T1 <: AbstractFloat, T2 <: AbstractFloat}
fileroot, fileext = splitext(filename)
for i in eachindex(names)
println("Ploting $(names[i]) ...")
Expand All @@ -275,13 +283,12 @@ function mapbox(lon::AbstractVector{T1}, lat::AbstractVector{T1}, M::AbstractMat
else
f = ""
end
aa = replace(string(names[i]), ' ' => "<br>")
p = mapbox(lon, lat, M[:,i]; filename=f, title=aa, kw...)
p = mapbox(lon, lat, M[:,i]; filename=f, title=plotly_title_length(string(names[i]), title_length), kw...)
display(p)
end
end

function mapbox(lon::AbstractVector{T1}, lat::AbstractVector{T1}, color::AbstractVector{T2}; title::AbstractString="", text::AbstractVector=repeat([""], length(lon)), dot_size::Number=3, dot_size_fig::Number=dot_size, lonc::AbstractFloat=minimum(lon)+(maximum(lon)-minimum(lon))/2, font_size::Number=14, font_size_fig::Number=font_size, font_color_fig::AbstractString="black", line_color::AbstractString="black", latc::AbstractFloat=minimum(lat)+(maximum(lat)-minimum(lat))/2, zoom::Number=4, zoom_fig::Number=zoom, style="mapbox://styles/mapbox/satellite-streets-v12", mapbox_token=NMFk.mapbox_token, filename::AbstractString="", figuredir::AbstractString=".", format::AbstractString=splitext(filename)[end][2:end], width::Union{Nothing,Int}=nothing, height::Union{Nothing,Int}=nothing, scale::Real=1, legend::Bool=true, colorscale::Symbol=:rainbow, paper_bgcolor::AbstractString="#FFF", showcount::Bool=true) where {T1 <: AbstractFloat, T2 <: AbstractFloat}
function mapbox(lon::AbstractVector{T1}, lat::AbstractVector{T1}, color::AbstractVector{T2}; title::AbstractString="", text::AbstractVector=repeat([""], length(lon)), dot_size::Number=3, dot_size_fig::Number=dot_size, lonc::AbstractFloat=minimum(lon)+(maximum(lon)-minimum(lon))/2, font_size::Number=14, font_size_fig::Number=font_size, font_color_fig::AbstractString="black", line_color::AbstractString="black", latc::AbstractFloat=minimum(lat)+(maximum(lat)-minimum(lat))/2, zoom::Number=4, zoom_fig::Number=zoom, style="mapbox://styles/mapbox/satellite-streets-v12", mapbox_token=NMFk.mapbox_token, filename::AbstractString="", figuredir::AbstractString=".", format::AbstractString=splitext(filename)[end][2:end], width::Union{Nothing,Int}=nothing, height::Union{Nothing,Int}=nothing, scale::Real=1, legend::Bool=true, colorscale::Symbol=:rainbow, paper_bgcolor::AbstractString="#FFF", showcount::Bool=true, title_length::Number=0) where {T1 <: AbstractFloat, T2 <: AbstractFloat}
@assert length(lon) == length(lat)
@assert length(lon) == length(color)
@assert length(lon) == length(text)
Expand All @@ -293,7 +300,7 @@ function mapbox(lon::AbstractVector{T1}, lat::AbstractVector{T1}, color::Abstrac
line_color=line_color,
color=color,
colorscale=NMFk.colorscale(colorscale),
colorbar=PlotlyJS.attr(; thicknessmode="pixels", thickness=30, len=0.5, title=title, titlefont=PlotlyJS.attr(size=font_size_fig, color=font_color_fig), tickfont=PlotlyJS.attr(size=font_size_fig, color=font_color_fig))
colorbar=PlotlyJS.attr(; thicknessmode="pixels", thickness=30, len=0.5, title=plotly_title_length(title, title_length), titlefont=PlotlyJS.attr(size=font_size_fig, color=font_color_fig), tickfont=PlotlyJS.attr(size=font_size_fig, color=font_color_fig))
)
else
marker = PlotlyJS.attr(; size=dot_size_fig, color=color)
Expand All @@ -319,7 +326,7 @@ function mapbox(lon::AbstractVector{T1}, lat::AbstractVector{T1}, color::Abstrac
line_color=line_color,
color=color,
colorscale=NMFk.colorscale(colorscale),
colorbar=PlotlyJS.attr(; thicknessmode="pixels", thickness=30, len=0.5, title=title, titlefont=PlotlyJS.attr(size=font_size, color=font_color_fig), tickfont=PlotlyJS.attr(size=font_size, color=font_color_fig))
colorbar=PlotlyJS.attr(; thicknessmode="pixels", thickness=30, len=0.5, title=plotly_title_length(title, title_length), titlefont=PlotlyJS.attr(size=font_size, color=font_color_fig), tickfont=PlotlyJS.attr(size=font_size, color=font_color_fig))
)
else
marker = PlotlyJS.attr(; size=dot_size, color=color)
Expand All @@ -338,7 +345,7 @@ function mapbox(lon::AbstractVector{T1}, lat::AbstractVector{T1}, color::Abstrac
return p
end

function mapbox(lon::AbstractVector{T1}, lat::AbstractVector{T1}, color::AbstractVector{T2}; title::AbstractString="", text::AbstractVector=repeat([""], length(lon)), dot_size::Number=3, dot_size_fig::Number=dot_size, lonc::AbstractFloat=minimum(lon)+(maximum(lon)-minimum(lon))/2, font_size::Number=14, font_size_fig::Number=font_size, font_color_fig::AbstractString="black", line_color::AbstractString="black", latc::AbstractFloat=minimum(lat)+(maximum(lat)-minimum(lat))/2, zoom::Number=4, zoom_fig::Number=zoom, style="mapbox://styles/mapbox/satellite-streets-v12", mapbox_token=NMFk.mapbox_token, filename::AbstractString="", figuredir::AbstractString=".", format::AbstractString=splitext(filename)[end][2:end], width::Union{Nothing,Int}=nothing, height::Union{Nothing,Int}=nothing, scale::Real=1, legend::Bool=true, colorscale::Symbol=:rainbow, paper_bgcolor::AbstractString="#FFF", showcount::Bool=true) where {T1 <: AbstractFloat, T2 <: Union{Number,Symbol,AbstractString,AbstractChar}}
function mapbox(lon::AbstractVector{T1}, lat::AbstractVector{T1}, color::AbstractVector{T2}; title::AbstractString="", text::AbstractVector=repeat([""], length(lon)), dot_size::Number=3, dot_size_fig::Number=dot_size, lonc::AbstractFloat=minimum(lon)+(maximum(lon)-minimum(lon))/2, font_size::Number=14, font_size_fig::Number=font_size, font_color_fig::AbstractString="black", line_color::AbstractString="black", latc::AbstractFloat=minimum(lat)+(maximum(lat)-minimum(lat))/2, zoom::Number=4, zoom_fig::Number=zoom, style="mapbox://styles/mapbox/satellite-streets-v12", mapbox_token=NMFk.mapbox_token, filename::AbstractString="", figuredir::AbstractString=".", format::AbstractString=splitext(filename)[end][2:end], width::Union{Nothing,Int}=nothing, height::Union{Nothing,Int}=nothing, scale::Real=1, legend::Bool=true, colorscale::Symbol=:rainbow, paper_bgcolor::AbstractString="#FFF", showcount::Bool=true, title_length::Number=0) where {T1 <: AbstractFloat, T2 <: Union{Number,Symbol,AbstractString,AbstractChar}}
@assert length(lon) == length(lat)
@assert length(lon) == length(color)
@assert length(lon) == length(text)
Expand All @@ -348,7 +355,7 @@ function mapbox(lon::AbstractVector{T1}, lat::AbstractVector{T1}, color::Abstrac
jj = j % length(NMFk.colors)
k = jj == 0 ? length(NMFk.colors) : jj
marker = PlotlyJS.attr(; size=dot_size, color=NMFk.colors[k])
name = showcount ? "$(string(i)) [$(sum(iz))]" : "$(string(i))"
name = plotly_title_length(showcount ? "$(string(i)) [$(sum(iz))]" : "$(string(i))", title_length)
trace = PlotlyJS.scattermapbox(;
lon=lon[iz],
lat=lat[iz],
Expand Down

0 comments on commit c98af1a

Please sign in to comment.