|
| 1 | +function response = plotlyoffline(plotlyfig) |
| 2 | + % Generate offline Plotly figure saved as an html file within |
| 3 | + % the current working directory. The file will be saved as: |
| 4 | + % 'plotlyfig.PlotOptions.FileName'.html. |
| 5 | + |
| 6 | + % grab the bundled dependencies |
| 7 | + userhome = getuserdir(); |
| 8 | + plotly_config_folder = fullfile(userhome,'.plotly'); |
| 9 | + plotly_js_folder = fullfile(plotly_config_folder, 'plotlyjs'); |
| 10 | + bundle_name = 'plotly-matlab-offline-bundle.js'; |
| 11 | + bundle_file = fullfile(plotly_js_folder, bundle_name); |
| 12 | + |
| 13 | + % check that the bundle exists |
| 14 | + try |
| 15 | + bundle = fileread(bundle_file); |
| 16 | + catch |
| 17 | + error(['Error reading: %s.\nPlease download the required ', ... |
| 18 | + 'dependencies using: >>getplotloffline \n', ... |
| 19 | + 'or contact [email protected] for assistance.'], ... |
| 20 | + bundle_file); |
| 21 | + end |
| 22 | + |
| 23 | + % handle plot div specs |
| 24 | + id = char(java.util.UUID.randomUUID); |
| 25 | + width = [num2str(plotlyfig.layout.width) 'px']; |
| 26 | + height = [num2str(plotlyfig.layout.height) 'px']; |
| 27 | + |
| 28 | + if plotlyfig.PlotOptions.ShowLinkText |
| 29 | + link_text = plotlyfig.PlotOptions.LinkText; |
| 30 | + else |
| 31 | + link_text = ''; |
| 32 | + end |
| 33 | + |
| 34 | + % format the data and layout |
| 35 | + jdata = m2json(plotlyfig.data); |
| 36 | + jlayout = m2json(plotlyfig.layout); |
| 37 | + clean_jdata = escapechars(jdata); |
| 38 | + clean_jlayout = escapechars(jlayout); |
| 39 | + |
| 40 | + % template dependencies |
| 41 | + dep_script = sprintf('<script type="text/javascript">%s</script>', ... |
| 42 | + bundle); |
| 43 | + |
| 44 | + % template environment vars |
| 45 | + plotly_domain = plotlyfig.UserData.PlotlyDomain; |
| 46 | + env_script = sprintf(['\n<script type="text/javascript">', ... |
| 47 | + 'window.PLOTLYENV=window.PLOTLYENV || {};', ... |
| 48 | + 'window.PLOTLYENV.BASE_URL="%s";', ... |
| 49 | + 'Plotly.LINKTEXT="%s";', ... |
| 50 | + '</script>'], plotly_domain, link_text); |
| 51 | + |
| 52 | + % template Plotly.plot |
| 53 | + script = sprintf(['\n Plotly.plot("%s", %s, %s).then(function(){'... |
| 54 | + '\n $(".%s.loading").remove();' ... |
| 55 | + '\n $(".link--embedview").text("%s");'... |
| 56 | + '\n });'], id, clean_jdata, clean_jlayout, ... |
| 57 | + id, link_text); |
| 58 | + |
| 59 | + plotly_script = sprintf(['\n<div class="%s loading" style=', ... |
| 60 | + 'color: rgb(50,50,50);">Drawing...</div>' ... |
| 61 | + '\n<div id="%s" style="height: %s;',... |
| 62 | + 'width: %s;" class="plotly-graph-div">' ... |
| 63 | + '</div> \n<script type="text/javascript">' ... |
| 64 | + '%s \n</script>'], id, id, height, width, ... |
| 65 | + script); |
| 66 | + |
| 67 | + % template entire script |
| 68 | + offline_script = [dep_script env_script plotly_script]; |
| 69 | + filename = plotlyfig.PlotOptions.FileName; |
| 70 | + |
| 71 | + % remove the whitespace from the filename |
| 72 | + clean_filename = filename(filename~=' '); |
| 73 | + html_filename = [clean_filename '.html']; |
| 74 | + |
| 75 | + % save the html file in the working directory |
| 76 | + plotly_offline_file = fullfile(pwd, html_filename); |
| 77 | + file_id = fopen(plotly_offline_file, 'w'); |
| 78 | + fprintf(file_id, offline_script); |
| 79 | + fclose(file_id); |
| 80 | + |
| 81 | + % return the local file url to be rendered in the browser |
| 82 | + response = ['file://' plotly_offline_file]; |
| 83 | + |
| 84 | +end |
0 commit comments