Current section
Files
Jump to
Current section
Files
lib/templates/svg/treemap.svg.eex
<svg version="1.1"
width="<%= 1000 %>" height="<%= 1000 %>"
xmlns="http://www.w3.org/2000/svg">
<% palette = Enum.map(["#2B3A67", "#ADFCF9", "#89023E", "#FFD5C2", "#4B644A"], &CssColors.parse!/1) |> Stream.cycle() %>
<%= for {rectangle, rectangle_index} <- Enum.with_index(rectangles) do %>
<% mix_weight = 1 / length(rectangle.areas) %>
<%= for {area, node_index} <- Enum.with_index(rectangle.areas) do %>
<% {width, height} = area.size %>
<% {x, y} = area.begin %>
<% {absolute_x, absolute_y} = {x * 100, y * 100} %>
<% {absolute_w, absolute_h} = {width * 100, height * 100} %>
<% fill_color = CssColors.mix(Enum.at(palette, rectangle_index), Enum.at(palette, rectangle_index + 1), mix_weight * node_index) %>
<g>
<rect
width="<%= absolute_w %>"
height="<%= absolute_h %>"
x="<%= absolute_x %>"
y="<%= absolute_y %>"
fill="<%= to_string(fill_color) %>"
stroke="white">
<title><%= area.label %> - <%= Float.round(area.pct, 2) %>% - value: <%= area.value %></title>
</rect>
<% %{blue: blue, green: green, red: red, alpha: _} = fill_color %>
<% yiq = ((blue * 114) + (green * 587) + (red * 299)) / 1000 %>
<% text_color = if yiq >= 128 do CssColors.parse!("#000000") else CssColors.parse!("#ffffff") end %>
<% padding = min(absolute_h, absolute_w) * 0.08 %>
<% text_size = String.split(area.label, ".") |> Enum.max_by(&String.length/1) |> String.length() %>
<% text_size = ceil(absolute_w / text_size) %>
<%= if text_size > 4 and absolute_h >= (length(String.split(area.label, ".")) * text_size) + (2 * padding )do %>
<text
x="<%= absolute_x + padding %>"
y="<%= absolute_y + padding %>"
font-size="<%= text_size * 1.2 %>px"
fill="<%= to_string(text_color) %>">
<%= for line <- String.split(area.label, ".") do %>
<tspan x="<%= absolute_x + padding %>" dy="<%= text_size * 1 %>px"><%= line %></tspan>
<% end %>
</text>
<% end %>
</g>
<% end %>
<% end %>
</svg>