Echarts 渲染 SVG 图形简单示例 - 带注释
<!-- 引入echarts -->
<script src='https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js'></script>
<!-- 容器,用于渲染echarts图表 -->
<div id='chart' style='width: 400px; height: 400px;'></div>
<script>
// 初始化echarts图表
var myChart = echarts.init(document.getElementById('chart'));
// 定义svg图形的路径
var svgPath = 'M43,113L99,113L99,206L43,206Z';
// 定义echarts系列的配置项
var option = {
series: [{
type: 'custom', // 自定义系列类型
renderItem: function(params, api) {
// 创建一个path元素
var path = new echarts.graphic.Path({
shape: {
// 从api获取svg路径
d: api.coord([[43, 113], [99, 113], [99, 206], [43, 206]])
},
style: {
// 设置边框和填充颜色
stroke: '#000000',
fill: '#800080'
}
});
// 返回path元素
return path;
}
}]
};
// 设置echarts图表的配置项并渲染图表
myChart.setOption(option);
</script>
<!-- svg图形 -->
<p><svg><g name='1'><path d='M43,113L99,113L99,206L43,206Z' stroke-width='0.5' stroke='#000000' fill='#800080'></path></g></svg></p>
AI 优化版
原文地址:https://www.cveoy.top/t/topic/nr1l 著作权归作者所有。请勿转载和采集!