带图层与点选择的散点图中保留zoom&pan功能的技术求助
解决Vega-Lite图层图表中选择与Zoom/Pan共存的问题
下面提供两种可行方案,按需选择:
方案一:放弃选择窗口的Zoom/Pan,给画布添加全局Zoom/Pan
把选择参数的窗口缩放/平移配置移除,在顶层添加独立的Zoom/Pan参数来控制整个画布的缩放和平移操作:
{ "$schema": "https://vega.github.io/schema/vega-lite/v5.json", "params": [ { "name": "quiverDefaultBrush", "select": { "type": "interval", "encodings": ["x", "y"] // 移除选择窗口的zoom和translate,仅保留框选功能 } }, { "name": "viewZoomPan", "select": "interval", "type": "interval", "bind": "scales", "zoom": "wheel!", "translate": "[mousedown, window:mouseup] > window:mousemove!" } ], "layer": [ { "data": {"url": "data/cars.json"}, "mark": {"type": "point", "shape": "M-.5,-.5L.5,.5M-.5,.5L.5,-.5", "size":100, "strokeWidth": 0.4}, "encoding": { "x": { "field": "Horsepower", "type": "quantitative" }, "y": { "field": "Miles_per_Gallon", "type": "quantitative" }, "tooltip": [ {"field": "description", "type": "nominal"}, {"field": "name", "type": "nominal"} ], "color": { "condition": { "param": "quiverDefaultBrush", "value": "blue" }, "value": "gray" }, "opacity": { "condition": { "param": "quiverDefaultBrush", "value": 1 }, "value": 0.5 } } }, { "data": {"url": "data/cars.json"}, "mark": "point", "encoding": { "x": { "field": "Horsepower", "type": "quantitative" }, "y": { "field": "Miles_per_Gallon", "type": "quantitative" }, "color": { "condition": { "param": "quiverDefaultBrush", "value": "green" }, "value": "gray" }, "opacity": { "condition": { "param": "quiverDefaultBrush", "value": 1 }, "value": 0.5 }, "size": {"value": 5} } } ] }
- 将选择参数移到顶层,让两个图层共享该选择配置
- 新增
viewZoomPan参数,通过bind: "scales"绑定到x/y轴,实现画布的Zoom/Pan - 移除选择参数的窗口缩放/平移事件,让它仅负责点框选
方案二:用Shift键区分选择与Zoom/Pan
如果想保留选择窗口的操作,同时通过Shift键触发画布的Zoom/Pan,可给两个参数的事件添加按键条件:
{ "$schema": "https://vega.github.io/schema/vega-lite/v5.json", "params": [ { "name": "quiverDefaultBrush", "select": { "type": "interval", "encodings": ["x", "y"], // 不按Shift时,拖拽为框选操作 "translate": "[mousedown, window:mouseup, !shift] > window:mousemove!", // 不按Shift时,滚轮缩放选择窗口 "zoom": "wheel!, !shift" } }, { "name": "viewZoomPan", "select": "interval", "type": "interval", "bind": "scales", // 按住Shift时,滚轮缩放画布 "zoom": "wheel!, shift", // 按住Shift时,拖拽平移画布 "translate": "[mousedown, window:mouseup, shift] > window:mousemove!" } ], "layer": [ { "data": {"url": "data/cars.json"}, "mark": {"type": "point", "shape": "M-.5,-.5L.5,.5M-.5,.5L.5,-.5", "size":100, "strokeWidth": 0.4}, "encoding": { "x": { "field": "Horsepower", "type": "quantitative" }, "y": { "field": "Miles_per_Gallon", "type": "quantitative" }, "tooltip": [ {"field": "description", "type": "nominal"}, {"field": "name", "type": "nominal"} ], "color": { "condition": { "param": "quiverDefaultBrush", "value": "blue" }, "value": "gray" }, "opacity": { "condition": { "param": "quiverDefaultBrush", "value": 1 }, "value": 0.5 } } }, { "data": {"url": "data/cars.json"}, "mark": "point", "encoding": { "x": { "field": "Horsepower", "type": "quantitative" }, "y": { "field": "Miles_per_Gallon", "type": "quantitative" }, "color": { "condition": { "param": "quiverDefaultBrush", "value": "green" }, "value": "gray" }, "opacity": { "condition": { "param": "quiverDefaultBrush", "value": 1 }, "value": 0.5 }, "size": {"value": 5} } } ] }
- 给选择参数的事件添加
!shift条件,仅在不按Shift时触发选择窗口操作 - 给画布Zoom/Pan参数的事件添加
shift条件,仅在按住Shift时触发画布缩放平移
内容的提问来源于stack exchange,提问作者Alexander Fedotov
相关产品推荐
相关产品推荐

