diff --git a/classes/Visualizer/D3Renderer/src/index.js b/classes/Visualizer/D3Renderer/src/index.js index 56feb4728..6cba2190c 100644 --- a/classes/Visualizer/D3Renderer/src/index.js +++ b/classes/Visualizer/D3Renderer/src/index.js @@ -45,13 +45,17 @@ function downloadDataUrl( dataUrl, name ) { /** * Render a single D3 chart into its container element via a sandboxed iframe. * - * @param {string} id - DOM element ID of the container - * @param {object} chart - chart entry from visualizer.charts + * @param {string} id - DOM element ID of the container + * @param {object} chart - chart entry from visualizer.charts + * @param {?HTMLElement} [element] - container element, needed when it lives in another document such as the editor iframe */ -function renderD3Chart( id, chart ) { - const container = document.getElementById( id ); +function renderD3Chart( id, chart, element ) { + const container = element || document.getElementById( id ); if ( ! container ) return; + // The sandbox posts to its parent window, which is the container's window. + const containerWindow = container.ownerDocument.defaultView; + const code = typeof chart.code === 'string' ? chart.code : null; if ( ! code ) { @@ -83,7 +87,7 @@ function renderD3Chart( id, chart ) { '
' + '