Compare commits

...

2 Commits

Author SHA1 Message Date
Roberto Alsina 823cc24e8a Typo, usar miles de verdad 2023-06-04 17:39:03 -03:00
Roberto Alsina 49352c8e37 * Ignore composite names
* Show guesses
2023-06-04 15:30:30 -03:00
1 changed files with 35 additions and 13 deletions

View File

@ -4,16 +4,17 @@ require "ishi/html"
require "json" require "json"
class Handler class Handler
def format_buffer(buffer, canvas_name) def format_buffer(buffer, canvas_name, title)
# Process the gnuplot output so it works in the page # Process the gnuplot output so it works in the page
# #
# buffer is the Ishi output # buffer is the Ishi output
# name is a string to replace for gnuplot_canvas so # name is a string to replace for gnuplot_canvas so
# we can have multiple charts in a page # we can have multiple charts in a page
# title is added on top of the chart
html = buffer.to_s.split("\n") html = buffer.to_s.split("\n")
html = html[html.index("<script type=\"text/javascript\">")..html.index("</script>")] html = html[html.index("<script type=\"text/javascript\">")..html.index("</script>")]
html = html.join("\n") + %( html = "<b>#{title}</b>" + html.join("\n") + %(
<div class="gnuplot"> <div class="gnuplot">
<canvas id="Tile" width="32" height="32" hidden></canvas> <canvas id="Tile" width="32" height="32" hidden></canvas>
<table class="plot"> <table class="plot">
@ -28,7 +29,7 @@ class Handler
</script> </script>
</div> </div>
) )
# This ID needs to be unique in case # This ID needs to be unique in case
# we have 2 charts in the same page # we have 2 charts in the same page
html.gsub("gnuplot_canvas", canvas_name) html.gsub("gnuplot_canvas", canvas_name)
end end
@ -190,27 +191,48 @@ class Handler
[r[0].as_i, r[1].as_s] [r[0].as_i, r[1].as_s]
} }
# In this context, remove all composite names
datos.reject! { |r|
r[1].to_s.includes? " "
}
if genero if genero
datos = split_por_genero(datos)[genero] datos = split_por_genero(datos)[genero]
end end
datos = datos[..10] datos = datos[..10]
if datos.size > 1
title = "¿Puede ser ... #{datos[0][1].to_s.titleize}? ¿O capaz que #{datos[1][1].to_s.titleize}? ¡Contame más!"
elsif datos.size == 1
title = "Me parece que ... #{datos[0][1].to_s.titleize}!"
else
title = "No tengo idea!"
end
buffer = IO::Memory.new buffer = IO::Memory.new
Ishi.new(buffer) do Ishi.new(buffer) do
x = (1..10).to_a x = (0..datos.size - 1).to_a
y = (1..10).to_a y = datos.map { |r|
r[0].to_f / 1000
}
xtics = Hash(Float64, String).new
datos.each_with_index { |r, i|
xtics[i.to_f] = r[1].to_s.titleize
}
canvas_size(800, 300) canvas_size(800, 300)
plot(x,y, style: :boxes, fs: 0.25) plot(x, y, style: :boxes, fs: 0.25)
.boxwidth(0.5) .boxwidth(0.5)
.ylabel("Popularidad") .show_key(false)
.xlabel("Nombre") .ylabel("Popularidad (miles)")
.xtics(xtics)
end end
{ {
body: format_buffer(buffer, "busqueda"), body: format_buffer(buffer, "busqueda", title),
status_code: 200, status_code: 200,
headers: HTTP::Headers{"Content-Type" => "text/html"}, headers: HTTP::Headers{"Content-Type" => "text/html"},
} }
end end
end end