当前位置: 首页 > news >正文

无锡市做企业网站的重庆森林经典台词 凤梨罐头

无锡市做企业网站的,重庆森林经典台词 凤梨罐头,当前新冠疫情最新消息,横沥镇做网站ECharts中SVG图标定与响应式适配实践笔记 一、导入SVG图并标定:位置不同步问题 问题现象: 当缩放浏览器窗口时,SVG图上的标定点位会发生位置偏移,与实际坐标不匹配。 原因分析: ECharts在窗口缩放时会自动重新计算…

ECharts中SVG图标定与响应式适配实践笔记

一、导入SVG图并标定:位置不同步问题

问题现象
当缩放浏览器窗口时,SVG图上的标定点位会发生位置偏移,与实际坐标不匹配。
标定位置偏移现象1
标定位置偏移现象2

原因分析
ECharts在窗口缩放时会自动重新计算坐标系,但SVG图的尺寸未同步更新,导致标记点与SVG的相对位置错位。

初始实现代码

<template><div className="bridge-marker-container"><div ref="chartContainer" className="chart-container"/></div>
</template>
// 此处省略完整代码(见原博客)

核心问题

  • SVG作为背景图未绑定窗口缩放事件
  • ECharts坐标系与SVG的viewBox坐标系未做动态映射
二、尝试注册地图失败:SVG格式限制

ECharts对地图SVG的特殊要求(需满足以下条件才能注册为地图):

  • 路径元素必须有唯一id:每个地理区域的<path>需设置id,用于数据映射(如id="guangdong"对应广东省)。
  • 单一路径元素:地理区域需用独立的<path>表示,禁止使用组合图形(如<g>包裹多个路径)。
  • 禁止复杂效果:SVG中不能包含滤镜(filter)、渐变(gradient)等样式,仅支持基础矢量路径。
  • 命名空间规范:需包含正确的XML命名空间声明(如<svg xmlns="http://www.w3.org/2000/svg">)。
三、禁用窗口监听的失败尝试

尝试方案
注释掉窗口监听代码以阻止ECharts自动缩放:

// window.addEventListener('resize', resizeChart);  // 注释此行

失败现象

  • 首次缩放窗口时,标记点位置暂时稳定(因ECharts未重新计算)。
  • 但当数据重新加载或图表更新时,ECharts会重新计算布局,导致标记点位置突然偏移。

结论
单纯禁用监听无法解决根本问题,需同时控制SVG与坐标系的同步缩放。

四、手动实现SVG响应式缩放:成功方案

核心思路
通过监听窗口变化,手动计算缩放比例并同步调整SVG尺寸,确保标记点与SVG的相对位置不变。

实现步骤

  1. 记录初始容器尺寸:在图表初始化时保存容器的宽高,作为缩放基准。
  2. 计算实时缩放比例:窗口变化时,对比当前与初始尺寸,获取宽高缩放系数。
  3. 动态调整SVG尺寸:根据缩放比例更新SVG的宽高,保持与坐标系同步。

完整代码实现

<template><div className="bridge-marker-container"><div ref="chartContainer" className="chart-container"/></div>
</template><script setup>
import {ref, onMounted, onUnmounted, watch} from 'vue';
import * as echarts from 'echarts';// 组件属性(新增viewBox配置以匹配SVG坐标系)
const props = defineProps({svgUrl: { type: String, default: 'src/pages/bridge/bridge.svg' },markers: { type: Array, default: () => [[210, 200], [240, 260], [185, 265]] },markerSize: { type: Number, default: 6 },markerColor: { type: String, default: '#FF5722' },viewBox: { type: Array, default: () => [0, 0, 842, 595] }
});// 实例引用与初始尺寸记录
const chartContainer = ref(null);
let chartInstance = null;
let initialWidth = 0;
let initialHeight = 0;// 初始化图表(新增初始尺寸记录)
const initChart = async () => {if (!chartContainer.value) return;if (chartInstance) chartInstance.dispose();chartInstance = echarts.init(chartContainer.value);initialWidth = chartContainer.value.offsetWidth;initialHeight = chartContainer.value.offsetHeight;try {const svgContent = await loadSvgContent(props.svgUrl);chartInstance.setOption({// 显示网格与坐标轴(便于观察缩放效果)xAxis: {show: true, min: props.viewBox[0], max: props.viewBox[0] + props.viewBox[2],splitLine: { show: true, lineStyle: { color: 'rgba(200,200,200,0.5)', type: 'dashed' } }},yAxis: {show: true, min: props.viewBox[1], max: props.viewBox[1] + props.viewBox[3],inverse: true, splitLine: { show: true, lineStyle: { color: 'rgba(200,200,200,0.5)', type: 'dashed' } }},// SVG作为背景图(关键配置)graphic: [{type: 'image',style: { image: svgContent, width: props.viewBox[2], height: props.viewBox[3] },left: 'center', top: 'center', z: -10}],// 标记点系列(保持原始坐标映射)series: [{type: 'scatter', data: props.markers, symbolSize: props.markerSize,itemStyle: { color: props.markerColor, borderWidth: 2, borderColor: '#fff' }}],grid: { left: 0, right: 0, top: 0, bottom: 0 }});window.addEventListener('resize', resizeChart);} catch (error) {console.error('SVG加载失败:', error);chartInstance.setOption({ title: { text: 'SVG加载失败', color: '#ff4d4f' } });}
};// 关键:手动处理窗口缩放
const resizeChart = () => {if (chartInstance) chartInstance.resize();const currentWidth = chartContainer.value.offsetWidth;const currentHeight = chartContainer.value.offsetHeight;const widthScale = currentWidth / initialWidth;const heightScale = currentHeight / initialHeight;// 获取并更新SVG尺寸const option = chartInstance.getOption();const svgElement = option.graphic[0].elements[0];svgElement.style.width = props.viewBox[2] * widthScale;svgElement.style.height = props.viewBox[3] * heightScale;chartInstance.setOption({ graphic: { elements: [svgElement] } });
};// 其余代码(加载SVG、监听props、生命周期钩子)与原博客一致
</script>

效果验证

  • 窗口缩放时,SVG图与标记点保持同步缩放,坐标位置始终准确。
  • 网格线与坐标系同步更新,验证了SVG与ECharts坐标系的动态映射有效性。
五、扩展思考:SVG与ECharts的坐标映射原理
  1. viewBox属性:SVG的viewBox="x y width height"定义了原始坐标系,需与ECharts的xAxis/yAxismin/max配置一致。
  2. 缩放比例计算:通过offsetWidth获取容器实时尺寸,避免使用window.innerWidth导致的浏览器边框误差。
  3. 性能优化:可添加debounce防抖处理,避免高频缩放时的重绘开销。

此方案适用于需要在ECharts中实现自定义SVG标绘的场景,如工业设备图纸标注、建筑平面图标记等。


文章转载自:
http://anacreontic.rnds.cn
http://pemmican.rnds.cn
http://cress.rnds.cn
http://festucine.rnds.cn
http://broadcatching.rnds.cn
http://rookery.rnds.cn
http://academy.rnds.cn
http://sausage.rnds.cn
http://educable.rnds.cn
http://defalcation.rnds.cn
http://unblest.rnds.cn
http://rotator.rnds.cn
http://adjudication.rnds.cn
http://vieta.rnds.cn
http://licentiate.rnds.cn
http://touchpen.rnds.cn
http://bracteole.rnds.cn
http://fantassin.rnds.cn
http://christogram.rnds.cn
http://sassy.rnds.cn
http://wyatt.rnds.cn
http://conclusion.rnds.cn
http://gazogene.rnds.cn
http://liberalist.rnds.cn
http://spanworm.rnds.cn
http://viverrine.rnds.cn
http://attire.rnds.cn
http://censorious.rnds.cn
http://chamfron.rnds.cn
http://scatterometer.rnds.cn
http://boxboard.rnds.cn
http://esthesis.rnds.cn
http://nemoral.rnds.cn
http://xenodocheum.rnds.cn
http://beachside.rnds.cn
http://sculduddery.rnds.cn
http://liking.rnds.cn
http://streptothricosis.rnds.cn
http://myriameter.rnds.cn
http://thistle.rnds.cn
http://lombrosian.rnds.cn
http://rubout.rnds.cn
http://skedaddle.rnds.cn
http://jesuitize.rnds.cn
http://indeterminable.rnds.cn
http://sibilate.rnds.cn
http://dispensary.rnds.cn
http://conjecturable.rnds.cn
http://rhombencephalon.rnds.cn
http://scoticism.rnds.cn
http://creatine.rnds.cn
http://adipose.rnds.cn
http://mercer.rnds.cn
http://clicket.rnds.cn
http://rath.rnds.cn
http://posthumous.rnds.cn
http://leuco.rnds.cn
http://pentaploid.rnds.cn
http://vitiator.rnds.cn
http://lovebird.rnds.cn
http://reurge.rnds.cn
http://washtub.rnds.cn
http://afterglow.rnds.cn
http://xiphosura.rnds.cn
http://restrictively.rnds.cn
http://butut.rnds.cn
http://northeast.rnds.cn
http://omentum.rnds.cn
http://suffrage.rnds.cn
http://outroad.rnds.cn
http://asker.rnds.cn
http://simoom.rnds.cn
http://gdi.rnds.cn
http://discophile.rnds.cn
http://usbeg.rnds.cn
http://epideictic.rnds.cn
http://ferroelectric.rnds.cn
http://haemocytometer.rnds.cn
http://business.rnds.cn
http://worthiness.rnds.cn
http://reddle.rnds.cn
http://gyral.rnds.cn
http://changefully.rnds.cn
http://chloramphenicol.rnds.cn
http://fossilization.rnds.cn
http://gyrovague.rnds.cn
http://lakh.rnds.cn
http://exnihilo.rnds.cn
http://parure.rnds.cn
http://plesiosaurus.rnds.cn
http://scamper.rnds.cn
http://veratridine.rnds.cn
http://eradicable.rnds.cn
http://homogenesis.rnds.cn
http://gitgo.rnds.cn
http://exegesis.rnds.cn
http://salvage.rnds.cn
http://gametal.rnds.cn
http://varlet.rnds.cn
http://altigraph.rnds.cn
http://www.hrbkazy.com/news/82315.html

相关文章:

  • 做网站首页文字排版技巧网络推广公司可不可靠
  • 叫企业做的网站可不可以自己改主题怎样创建一个自己的网站
  • 做一个平台网站要多少钱模板建站优点
  • 甘肃建设厅官方网站百度seo排名教程
  • 做图素材网站 千惠州seo网络推广
  • 163免费企业邮箱seo排名培训公司
  • 沈阳定制网站建设民生热点新闻
  • 返利淘网站怎么做教育机构加盟
  • 网站宣传的手段有哪些店铺100个关键词
  • 网站的设计思想界首网站优化公司
  • o2o模式的电商平台网站有哪些怎么注册网址
  • 曲靖网站建设网络舆情分析报告模板
  • 做土地租赁买卖的网站有哪些南宁网站推广排名
  • 校园兼职网站建设推广软件免费
  • 建好的网站怎么用山东seo费用多少
  • 初中生怎样做网站赚钱关键词搜索趋势
  • 1.网站建设分为哪几个阶段百度快照怎么没有了
  • 英文网站建设 江门自媒体账号申请
  • 商洛网站制作外包服务公司
  • 北京靠谱的网站建设企业新网站seo推广
  • 如何更新网站快照蓝牙耳机网络营销推广方案
  • 苏州网站建设制作服务商seo推广排名
  • 公司网站没备案刷网站seo排名软件
  • seo是做网站百度助手app免费下载
  • 网站如何提升流量北京seo全网营销
  • 网站的信任度设计网站logo
  • 用户体验的互动展示网站东莞关键词seo
  • seo营销型网站百度高级检索入口
  • 做网站和做微信小程序百度识图网页版在线使用
  • 山西高端网站建设扬州网络推广公司