使用 Echarts 渲染 SVG 图形并修改颜色 - 代码示例
// 引入echarts/nimport echarts from 'echarts';/n/n// 获取svg字符串/nconst svgStr = '';/n/n// 将svg字符串转为dom对象/nconst parser = new DOMParser();/nconst svgDom = parser.parseFromString(svgStr, 'image/svg+xml').querySelector('svg');/n/n// 定义option/nconst option = {/n // 使用svg作为背景/n graphic: {/n elements: [{/n type: 'svg',/n left: 'center',/n top: 'center',/n style: {/n width: '100%',/n height: '100%',/n },/n children: [svgDom],/n }],/n },/n};/n/n// 渲染echarts/nconst chart = echarts.init(document.getElementById('chart-container'));/nchart.setOption(option);/n/n// 修改svg图形颜色/nconst svgG = chart.getConnectedDataURL().match(/<g.*<//g>/)[0];/nconst modifiedSvgG = svgG.replace(/fill=/'#/w+/'/, 'fill=/'#0000ff/'');/nconst modifiedSvg = chart.getConnectedDataURL().replace(svgG, modifiedSvgG);/n/n// 更新图形/nchart.setOption({/n graphic: {/n elements: [{/n type: 'svg',/n left: 'center',/n top: 'center',/n style: {/n width: '100%',/n height: '100%',/n },/n children: [parser.parseFromString(modifiedSvg, 'image/svg+xml').querySelector('svg')],/n }],/n },/n});/n
AI 优化版
原文地址:https://www.cveoy.top/t/topic/nr2A 著作权归作者所有。请勿转载和采集!