From 49352c8e37fa5c3b53b1c4b01ddb9c891a371e6b Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Sun, 4 Jun 2023 15:30:30 -0300 Subject: [PATCH] * Ignore composite names * Show guesses --- c-busqueda/handler.cr | 49 +++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/c-busqueda/handler.cr b/c-busqueda/handler.cr index ee6c82b..c8c4846 100644 --- a/c-busqueda/handler.cr +++ b/c-busqueda/handler.cr @@ -4,16 +4,17 @@ require "ishi/html" require "json" 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 # # buffer is the Ishi output # 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 = html[html.index("")] - html = html.join("\n") + %( + html = "#{title}" + html.join("\n") + %(
@@ -28,7 +29,7 @@ class Handler ) - # This ID needs to be unique in case + # This ID needs to be unique in case # we have 2 charts in the same page html.gsub("gnuplot_canvas", canvas_name) end @@ -190,27 +191,49 @@ class Handler [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 datos = split_por_genero(datos)[genero] end - 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}? ¡Contáme 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 Ishi.new(buffer) do - x = (1..10).to_a - y = (1..10).to_a + x = (0..datos.size - 1).to_a + y = datos.map { |r| + r[0].to_f + } + + xtics = Hash(Float64, String).new + datos.each_with_index { |r, i| + xtics[i.to_f] = r[1].to_s.titleize + } + canvas_size(800, 300) - plot(x,y, style: :boxes, fs: 0.25) - .boxwidth(0.5) - .ylabel("Popularidad") - .xlabel("Nombre") + plot(x, y, style: :boxes, fs: 0.25) + .boxwidth(0.5) + .show_key(false) + .ylabel("Popularidad (miles)") + .xlabel("Nombre") + .xtics(xtics) end { - body: format_buffer(buffer, "busqueda"), + body: format_buffer(buffer, "busqueda", title), status_code: 200, headers: HTTP::Headers{"Content-Type" => "text/html"}, } end -end \ No newline at end of file +end