Commit aa049705 authored by SOLIMAN Sylvain's avatar SOLIMAN Sylvain

working gnuplot > canvas, but not good if several plots

parent 551b2101
# very much inspired form the example bash_kernel
# https://raw.githubusercontent.com/takluyver/bash_kernel/
import base64
import imghdr
import os
......@@ -10,19 +9,71 @@ def display_data_for_image(filename):
image = f.read()
os.unlink(filename)
image_type = imghdr.what(None, image)
if image_type is None:
if filename.endswith('.svg'):
image_type = 'svg+xml'
image_data = image.decode('utf-8')
else:
raise ValueError("Not a valid image: %s" % image)
image_type = filename.split('.')[-1]
if image_type == 'svg':
image_type = 'image/svg+xml'
image_data = image.decode('utf-8')
elif image_type == 'js':
image_type = 'text/html'
image = image.decode('utf-8')
# FIXME use local URLs and use mouse CSS
jsurl = 'http://www.gnuplot.info/demo_canvas'
jsid = filename[:-3]
image_data = '''
<script src="{jsurl}/canvastext.js"></script>
<script src="{jsurl}/gnuplot_common.js"></script>
<script src="{jsurl}/gnuplot_dashedlines.js"></script>
<script src="{jsurl}/gnuplot_mouse.js"></script>
<script>'''.format(jsurl=jsurl) + image + '</script>'
image_data = image_data + '''
<canvas id="{jsid}" width=600 height=400
oncontextmenu="return false;"
onkeypress=gnuplot.do_hotkey()></canvas>
<table id="gnuplot_mousebox" class="mbunder">
<tr>
<td class="icon" style="padding: 0px;"
onclick="gnuplot.toggle_grid();"><img
src="{jsurl}/grid.png" id="gnuplot_grid_icon" alt="#"
title="toggle grid"></td>
<td class="icon" style="padding: 0px;"
onclick="gnuplot.unzoom();"><img
src="{jsurl}/previouszoom.png" id="gnuplot_unzoom_icon"
alt="unzoom" title="unzoom"></td>
<td class="icon" style="padding: 0px;"
onclick="gnuplot.rezoom();"><img
src="{jsurl}/nextzoom.png" id="gnuplot_rezoom_icon"
alt="rezoom" title="rezoom"></td>
<td class="icon" style="padding: 0px;"><a
href="{jsurl}/canvas_help.html"><img
src="{jsurl}/help.png" align=right alt="help"
title="help"></a></td>
'''.format(jsid=jsid, jsurl=jsurl)
for i in range(image.count('End {}_plot_'.format(jsid))):
image_data = image_data + '''
<td class="icon" style="padding: 0px;"
onclick='gnuplot.toggle_plot("{jsid}_plot_{nb}")'>{nb}</td>
'''.format(jsid=jsid, nb=i+1)
image_data = image_data + '''
<td class="mb0">x</td>
<td class="mb1"><span id="{jsid}_x">&nbsp;</span></td>
<td class="mb0">y</td>
<td class="mb1"><span id="{jsid}_y">&nbsp;</span></td>
</table>
<script>var cssNode = document.createElement('link');
cssNode.type = 'text/css';
cssNode.rel = 'stylesheet';
cssNode.href = '{jsurl}/gnuplot_mouse.css';
document.head.appendChild(cssNode);</script>
<script>var gnuplot_canvas = {jsid};
{jsid}();</script>
'''.format(jsid=jsid, jsurl=jsurl)
else:
image_type = 'image/' + image_type
image_data = base64.b64encode(image).decode('ascii')
content = {
'data': {
'image/' + image_type: image_data
image_type: image_data
},
'metadata': {}
}
......
......@@ -64,9 +64,9 @@ option('', '--version', Arg, Arg) :-
quit.
option('', '--jupyter', Arg, Arg) :-
set_counter(plot_png, 0),
set_counter(plot_canvas, 0),
set_counter(graph_svg, 0),
set_plot_driver(gnu_plot_png),
set_plot_driver(gnu_plot_canvas),
set_image_viewer_driver(img_tag),
set_draw_graph_driver(graph_svg).
......
......@@ -37,6 +37,14 @@ gnu_plot_png :-
view_image(Filename).
gnu_plot_canvas :-
count(plot_canvas, Index),
format(atom(Filename), 'plot~d.js', [Index]),
format(atom(BaseFilename), 'plot~d', [Index]),
export_plot_to_canvas(Filename, BaseFilename),
view_image(Filename).
execute_plot(Options) :-
process_create(path(gnuplot), Options, []).
......@@ -58,12 +66,19 @@ export_plot_to_png(OutputFile) :-
execute_plot(['plot.plot']).
export_plot_to_canvas(OutputFile, BaseOutputFile) :-
biocham_command,
type(OutputFile, output_file),
doc('plots the current trace as a canvas element in an HTML file'),
export_plot(plot, [canvas(OutputFile, BaseOutputFile)]),
execute_plot(['plot.plot']).
export_plot(FileTemplate, Options) :-
format(atom(PlotFile), '~a.plot', [FileTemplate]),
format(atom(CsvFile), '~a.csv', [FileTemplate]),
export_table(CsvFile),
get_table_data([HeaderRow | _ ]),
HeaderRow =.. [row, _TimeHeader | Headers],
get_table_headers([_Time | Headers]),
absolute_file_name(PlotFile, AbsolutePlotFile),
setup_call_cleanup(
open(AbsolutePlotFile, write, Stream),
......@@ -79,6 +94,13 @@ export_plot_stream(Stream, CsvFile, Headers, Options) :-
set term png
set output "~a"
', [Filename])
;
member(canvas(Filename, BaseFilename), Options)
->
format(Stream, '\c
set term canvas name "~a"
set output "~a"
', [BaseFilename, Filename])
;
true
),
......
......@@ -16,6 +16,7 @@
add_table/2,
get_table_data/1,
get_current_table/1,
get_table_headers/1,
columns/3
]
).
......@@ -79,6 +80,22 @@ export_table(OutputFile) :-
csv_write_file(AbsoluteFilenameCsv, Table).
get_table_headers(Headers) :-
get_table_data(Data),
Data = [FirstRow | _],
FirstRow =.. [row | Columns],
length(Columns, ColumnCount),
ColumnMax is ColumnCount - 1,
findall(
ColumnName,
(
between(0, ColumnMax, ColumnIndex),
get_column_name(ColumnIndex, ColumnName)
),
Headers
).
list_tables :-
biocham_command,
doc('lists the current set of tables.'),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment