You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@echarts.apache.org by "ggregoire (via GitHub)" <gi...@apache.org> on 2025/05/21 17:19:02 UTC

Re: [I] While renderer as SVG the SaveAsImage png file can't use [echarts]

ggregoire commented on issue #7763:
URL: https://github.com/apache/echarts/issues/7763#issuecomment-2898696843

   If anyone interested:
   
   ```typescript
   import type { ECharts } from 'echarts'
   
   function convertSVGDataURLtoPNGDataURL(SVGDataURL: string): Promise<string> {
     return new Promise(resolve => {
       const canvas = document.createElement('canvas')
       const context = canvas.getContext('2d')!
       const image = new Image()
   
       image.addEventListener(
         'load',
         () => {
           canvas.width = image.width
           canvas.height = image.height
           context.fillStyle = '#fff'
           context.fillRect(0, 0, canvas.width, canvas.height)
           context.drawImage(image, 0, 0, canvas.width, canvas.height)
           resolve(canvas.toDataURL('image/png'))
         },
         false,
       )
   
       image.src = SVGDataURL
     })
   }
   
   export function downloadSVGChartAsPNG(
     echartsInstance: ECharts | undefined,
     filename: string,
   ) {
     if (!echartsInstance) return
   
     const url = echartsInstance.getDataURL({
       type: 'svg',
       pixelRatio: 2,
       backgroundColor: '#fff',
     })
   
     convertSVGDataURLtoPNGDataURL(url).then(PNGDataURL => {
       const link = document.createElement('a')
       link.href = PNGDataURL
       link.download = filename
       document.body.appendChild(link)
       link.click()
       document.body.removeChild(link)
     })
   }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@echarts.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@echarts.apache.org
For additional commands, e-mail: commits-help@echarts.apache.org