|
462 | 462 | savedOptions = options; |
463 | 463 | return $.extend({}, options, {chartArea: null}); |
464 | 464 | }); |
465 | | - const img = document.createElement('img'); |
466 | | - img.src = args.getImageURI(); |
467 | | - const temp = document.createElement('canvas'); |
468 | | - temp.width = img.width; |
469 | | - temp.height = img.height; |
470 | | - const ctx = temp.getContext('2d'); |
471 | | - ctx.drawImage(img, 0, 0); |
472 | | - const image = runtime.getField(imageLib, "internal"); |
473 | | - const trimmed = image.trimCanvas(temp); |
474 | | - const download = document.createElement('a'); |
475 | | - download.href = trimmed.toDataURL(); |
476 | | - download.download = 'chart.png'; |
477 | | - // from https://stackoverflow.com/questions/3906142/how-to-save-a-png-from-javascript-variable |
478 | | - function fireEvent(obj, evt){ |
479 | | - const fireOnThis = obj; |
480 | | - if(document.createEvent) { |
481 | | - const evObj = document.createEvent('MouseEvents'); |
482 | | - evObj.initEvent(evt, true, false); |
483 | | - fireOnThis.dispatchEvent(evObj); |
484 | | - } else if(document.createEventObject) { |
485 | | - const evObj = document.createEventObject(); |
486 | | - fireOnThis.fireEvent('on' + evt, evObj); |
487 | | - } |
488 | | - } |
489 | | - fireEvent(download, 'click'); |
490 | | - args.draw(_ => savedOptions); |
| 465 | + // getImageURI may return a data URL synchronously (Google Charts) |
| 466 | + // or a Promise resolving to one (Vega, which renders asynchronously). |
| 467 | + Promise.resolve(args.getImageURI()).then((uri) => { |
| 468 | + const img = document.createElement('img'); |
| 469 | + // Must wait for the image to load before reading its dimensions |
| 470 | + // and drawing it; for the async (Vega) path it isn't ready yet. |
| 471 | + img.onload = () => { |
| 472 | + const temp = document.createElement('canvas'); |
| 473 | + temp.width = img.width; |
| 474 | + temp.height = img.height; |
| 475 | + const ctx = temp.getContext('2d'); |
| 476 | + ctx.drawImage(img, 0, 0); |
| 477 | + const image = runtime.getField(imageLib, "internal"); |
| 478 | + const trimmed = image.trimCanvas(temp); |
| 479 | + const download = document.createElement('a'); |
| 480 | + download.href = trimmed.toDataURL(); |
| 481 | + download.download = 'chart.png'; |
| 482 | + // from https://stackoverflow.com/questions/3906142/how-to-save-a-png-from-javascript-variable |
| 483 | + function fireEvent(obj, evt){ |
| 484 | + const fireOnThis = obj; |
| 485 | + if(document.createEvent) { |
| 486 | + const evObj = document.createEvent('MouseEvents'); |
| 487 | + evObj.initEvent(evt, true, false); |
| 488 | + fireOnThis.dispatchEvent(evObj); |
| 489 | + } else if(document.createEventObject) { |
| 490 | + const evObj = document.createEventObject(); |
| 491 | + fireOnThis.fireEvent('on' + evt, evObj); |
| 492 | + } |
| 493 | + } |
| 494 | + fireEvent(download, 'click'); |
| 495 | + args.draw(_ => savedOptions); |
| 496 | + }; |
| 497 | + img.src = uri; |
| 498 | + }); |
491 | 499 | }); |
492 | 500 | }, |
493 | 501 | resizeStop: (_, ui) => { |
|
0 commit comments