Current section

Files

Jump to
archeometer lib templates svg dsm_svg.eex
Raw

lib/templates/svg/dsm_svg.eex

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<%# Dark gray %>
<% line_color = "169,169,169" %>
<%# This one is assuming default fonts %>
<% char_width = 8 %>
<% item_size = margin = 20 %>
<% padding = 20 %>
<% txt_offset = 5 %>
<% max_name_chars =
mod_names
|> Map.values()
|> Enum.max_by(&String.length/1)
|> String.length()
%>
<% max_txt_width = char_width * max_name_chars %>
<% mtx_size = length(mtx.nodes) * item_size %>
<% base_x = base_y = margin + max_txt_width %>
<% svg_size = (2 * margin) + max_txt_width + padding + mtx_size %>
<svg xmlns="http://www.w3.org/2000/svg"
height="<%= svg_size %>"
width="<%= svg_size %>">
<%# Main square %>
<rect x="<%= base_x + padding %>"
y="<%= base_y + padding %>"
width="<%= mtx_size %>"
height="<%= mtx_size %>"
style="fill:none;stroke-width:2;stroke:rgb(<%= line_color %>)"
/>
<%# Labels and lines %>
<% names = Enum.map(mtx.nodes, fn n -> Map.get(mod_names, n) end) %>
<%= for {mod_name, idx} <- Enum.with_index(names, 1) do %>
<%# Horizontal %>
<% ypos = base_y + padding + (item_size * idx ) %>
<text id="<%= mod_name %>" x="<%= item_size %>" y="<%= ypos - txt_offset %>"><%= mod_name %></text>
<line x1="<%= margin %>"
y1="<%= ypos %>"
x2="<%= margin + max_txt_width + padding + mtx_size %>"
y2="<%= ypos %>"
style="stroke:rgb(<%= line_color %>);stroke-width:1" />
<%# Vertical %>
<% xpos = base_x + (item_size * idx) %>
<text transform="translate(<%= xpos + padding - txt_offset %>,<%= margin + max_txt_width %>) rotate(-90)">
<%= mod_name %>
</text>
<line x1="<%= xpos + padding %>"
y1="<%= margin %>"
x2="<%= xpos + padding %>"
y2="<%= base_y + padding + mtx_size %>"
style="stroke:rgb(<%= line_color %>);stroke-width:1" />
<% end %>
<%# Dependencies %>
<%= for {row, i} <- Enum.with_index(mtx.nodes) do %>
<%= for {col, j} <- Enum.with_index(mtx.nodes) do %>
<% val = Map.get(mtx.edges, {row, col}) %>
<%= if val do %>
<% xpos = base_x + padding + (item_size * j) + 2 %>
<% ypos = base_y + padding + (item_size * i) + 16 %>
<text x="<%= xpos %>" y="<%= ypos %>">
<%= case val do
:+ -> "🔹"
:* -> "🔸"
end %>
</text>
<% end %>
<% end %>
<% end %>
<%# Groups %>
<%# colors = [red, green, orange, purple, magenta] %>
<% colors = ["255,0,0", "0,153,0", "255,128,0", "128,0,128", "255,0,255"] %>
<%= for {{r, c}, color} <- Stream.zip(group_corners, Stream.cycle(colors)) do %>
<% x1 = base_x + padding + (item_size * r) %>
<% x2 = base_x + padding + (item_size * (c + 1)) %>
<% y1 = base_y + padding + (item_size * r) %>
<% y2 = base_y + padding + (item_size * (c + 1)) %>
<% style = "fill:none;stroke:rgb(#{color});stroke-width:2" %>
<rect x="<%= x1 %>"
y="<%= y1 %>"
width="<%= x2 - x1 %>"
height="<%= y2 - y1 %>"
style="<%= style %>"
/>
<% end %>>
</svg>