|
| 1 | +function output = write_image(pfObj, imageFormat, filename, height, width, scale) |
| 2 | + |
| 3 | +% Function to write plotly figures to a supported image format, which are the following: "png", "jpg", "jpeg", "webp", "svg", "pdf", "eps", "json" |
| 4 | + |
| 5 | +debug=1; |
| 6 | +if nargin < 2 |
| 7 | + imageFormat='png'; |
| 8 | + filename='figure.png'; |
| 9 | + height=500; |
| 10 | + width=800; |
| 11 | + scale=1; |
| 12 | +end |
| 13 | + |
| 14 | +wd=fileparts(fileparts(mfilename('fullpath'))); |
| 15 | +output=[]; |
| 16 | + |
| 17 | +if ~isa(pfObj,'plotlyfig') |
| 18 | + fprintf('\nError: Input is not a plotlyfig object.\n\n'); |
| 19 | + return |
| 20 | +end |
| 21 | + |
| 22 | +if isunix() |
| 23 | + kExec = string(fullfile(wd,'kaleido','kaleido')); |
| 24 | + cc="cat"; |
| 25 | +else |
| 26 | + kExec = string(fullfile(wd,'kaleido','kaleido.cmd')); |
| 27 | + cc="type"; |
| 28 | +end |
| 29 | +plyJsLoc = string(fullfile(wd,'kaleido','plotly-latest.min.js')); |
| 30 | + |
| 31 | +if ~isfile(kExec) || ~isfile(plyJsLoc) |
| 32 | + status=getKaleido(); |
| 33 | +else |
| 34 | + status=1; |
| 35 | +end |
| 36 | + |
| 37 | +if status == 0 |
| 38 | + return |
| 39 | +end |
| 40 | + |
| 41 | +mjLoc = replace(string(fullfile(wd,'kaleido','etc','mathjax','MathJax.js')),'\','/'); |
| 42 | +scope="plotly"; |
| 43 | + |
| 44 | + |
| 45 | +% Prepare input plotly object for Kaleido |
| 46 | +q=struct(); |
| 47 | +q.data.data = pfObj.data; |
| 48 | +q.data.layout = pfObj.layout; |
| 49 | +q.data.layout = rmfield(q.data.layout,'height'); |
| 50 | +q.data.layout = rmfield(q.data.layout,'width'); |
| 51 | +q.format = imageFormat; |
| 52 | +q.height = height; |
| 53 | +q.scale = scale; |
| 54 | +q.width = width; |
| 55 | + |
| 56 | +pfJson = native2unicode(jsonencode(q),'UTF-8'); |
| 57 | +tFile = string(fullfile(wd,'kaleido','temp.txt')); |
| 58 | +f=fopen(tFile,'w'); |
| 59 | +fprintf(f,"%s",pfJson); |
| 60 | +fclose(f); |
| 61 | + |
| 62 | +cmd=[cc," ",tFile," | ",kExec," ",scope," --plotlyjs='",plyJsLoc,"' ","--mathjax='file:///",mjLoc,"' --no-sandbox --disable-gpu --allow-file-access-from-files --disable-breakpad --disable-dev-shm-usage"]; |
| 63 | + |
| 64 | +if debug |
| 65 | + inputCmd=char(join(cmd,'')); |
| 66 | + fprintf('\nDebug info:\n%s\n\n',inputCmd); |
| 67 | +end |
| 68 | + |
| 69 | +[code,out]=system(char(join(cmd,''))); |
| 70 | +if debug |
| 71 | + disp(out); |
| 72 | +end |
| 73 | + |
| 74 | +if code ~= 0 |
| 75 | + fprintf('\nFatal: Failed to run Kaleido.\n\n'); |
| 76 | + return; |
| 77 | +else |
| 78 | + a=string(split(out,newline)); |
| 79 | + if a(end)=="" |
| 80 | + a(end)=[]; |
| 81 | + end |
| 82 | + output = jsondecode(a(end)); |
| 83 | +end |
| 84 | + |
| 85 | +if output.code ~= 0 |
| 86 | + fprintf('\nError: %s\n',output.message); |
| 87 | +else |
| 88 | + out=unicode2native(output.result,'UTF-8'); |
| 89 | + out=base64decode(out); |
| 90 | + f=fopen(filename,'wb'); |
| 91 | + fwrite(f,out); |
| 92 | + fclose(f); |
| 93 | +end |
0 commit comments