Unique canvas name suppor

This commit is contained in:
Roberto Alsina 2023-06-04 11:52:30 -03:00
parent aeb18bf928
commit d2f590665d

View File

@ -3,8 +3,38 @@ require "http/headers"
require "http/request"
require "ishi/html"
require "json"
require "uuid"
class Handler
def format_buffer(buffer, canvas_name)
# 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
html = buffer.to_s.split("\n")
html = html[html.index("<script type=\"text/javascript\">")..html.index("</script>")]
html = html.join("\n") + %(
<div class="gnuplot">
<canvas id="Tile" width="32" height="32" hidden></canvas>
<table class="plot">
<tr><td>
<canvas id="gnuplot_canvas" width="800" height="300" tabindex="0">
Sorry, your browser seems not to support the HTML 5 canvas element
</canvas>
</td></tr>
</table>
<script type="text/javascript" defer>
gnuplot.init(); gnuplot_canvas();
</script>
</div>
)
# This ID needs to be unique in case
# we have 2 charts in the same page
html.gsub("gnuplot_canvas", canvas_name)
end
def query(sql)
params = URI::Params.encode({"q": sql})
response = HTTP::Client.get URI.new(
@ -70,27 +100,8 @@ class Handler
}
end
# This is a whole page, we just need some bits
html = buffer.to_s.split("\n")
html = html[html.index("<script type=\"text/javascript\">")..html.index("</script>")]
html = html.join("\n") + %(
<div class="gnuplot">
<canvas id="Tile" width="32" height="32" hidden></canvas>
<table class="plot">
<tr><td>
<canvas id="gnuplot_canvas" width="800" height="300" tabindex="0">
Sorry, your browser seems not to support the HTML 5 canvas element
</canvas>
</td></tr>
</table>
<script type="text/javascript" defer>
gnuplot.init(); gnuplot_canvas();
</script>
</div>
)
{
body: html,
body: format_buffer(buffer, "historico"),
status_code: 200,
headers: HTTP::Headers{"Content-Type" => "text/html"},
}