Vega-Lite中point mark可用的SVG路径格式问题
问题描述
我简化了Vega-Lite的同构条形图示例,使用某SVG路径作为point mark的shape可正常显示:
但替换为圣诞树SVG路径后显示异常:
预期应显示圣诞树:
疑似问题出在两个SVG路径的坐标范围差异,以下是包含两个路径的代码:
{ "$schema": "https://vega.github.io/schema/vega-lite/v5.json", "config": {"view": {"stroke": ""}}, "width": 800, "height": 200, "data": { "values": [ {"country": "Great Britain", "animal": "pic", "col": 3} ] }, "mark": {"type": "point", "filled": true}, "encoding": { "x": {"field": "col", "type": "ordinal", "axis": null}, "y": {"field": "animal", "type": "ordinal", "axis": null}, "row": {"field": "country", "header": {"title": ""}}, "shape": { "field": "animal", "type": "nominal", "scale": { "domain": ["pic", "pic2"], "range": [ "M1.7 -1.7h-0.8c0.3 -0.2 0.6 -0.5 0.6 -0.9c0 -0.6 -0.4 -1 -1 -1c-0.6 0 -1 0.4 -1 1c0 0.4 0.2 0.7 0.6 0.9h-0.8c-0.4 0 -0.7 0.3 -0.7 0.6v1.9c0 0.3 0.3 0.6 0.6 0.6h0.2c0 0 0 0.1 0 0.1v1.9c0 0.3 0.2 0.6 0.3 0.6h1.3c0.2 0 0.3 -0.3 0.3 -0.6v-1.8c0 0 0 -0.1 0 -0.1h0.2c0.3 0 0.6 -0.3 0.6 -0.6v-2c0.2 -0.3 -0.1 -0.6 -0.4 -0.6z", "M256 0L266.969 30.766L299.641 31.703L273.766 51.656L282.969 83L256 64.563L229.031 83L238.234 51.656L212.359 31.703L245.016 30.766z M387.281 405.297L327.781 313.344L361.016 313.344L301.203 220.875L334.094 220.875L256 100.156L177.906 220.875L210.781 220.875L150.984 313.344L184.219 313.344L124.719 405.297z M229.328 430.313h53.344v81.688H229.328Z" ] }, "legend": null }, "opacity": {"value": 1}, "size": {"value": 200} } }
解决方案
问题根源是两个SVG路径的坐标范围差距过大:
- 第一个路径的坐标集中在±2的小范围内
- 圣诞树路径的坐标范围是0到387(横向)、0到512(纵向),导致Vega-Lite按相同尺寸缩放后,圣诞树图形要么过大溢出显示区域,要么被压缩成极小的点。
解决步骤:
- 归一化圣诞树路径坐标:将所有坐标平移并缩放,把整体尺寸缩小到和第一个路径相近的范围(比如±2或0-4区间)。
处理后的圣诞树路径示例(已适配小范围坐标):"M0.96 -2L1.2 -1.52L1.88 -1.5L1.36 -1.16L1.56 -0.6L0.96 -0.92L0.36 -0.6L0.56 -1.16L0.04 -1.5L0.72 -1.52z M3.52 0.2L2.32 -1.4L2.96 -1.4L1.72 -3.08L2.36 -3.08L0.96 -5L-0.44 -3.08L0.2 -3.08L-1.04 -1.4L0.28 -1.4L-1.04 0.2z M0.36 0.6h0.96v1.44H0.36Z" - 替换路径并调整尺寸:将上述路径替换到代码的
shape.scale.range中,可适当调整size参数(比如设为400),就能正常显示圣诞树形状。
内容的提问来源于stack exchange,提问作者Vlad
相关产品推荐
相关产品推荐

