gpt4 book ai didi

interaction - 在 Vega 中拖动数据点的鼠标交互

转载 作者:行者123 更新时间:2023-12-05 03:03:37 24 4
gpt4 key购买 nike

我正在努力更深入地了解 Vega 图中的交互。目前,我想修改 Voronoi example from the documentation这样您就不会添加和删除点,而是拖动现有的点。我的代码如下所示:

{
"$schema": "https://vega.github.io/schema/vega/v4.json",
"width": 500,
"height": 200,
"autosize": "none",

"signals": [
{
"name": "movePoint",
"on": [
{
"events": "[mousedown, mouseup] > mousemove",
"update": "{u: round(100*invert('xscale', x()))/100, v: round(100*invert('yscale', y()))/100}"
}
]
}
],

"data": [
{
"name": "table",
"values": [
{"u": 0.1, "v": 0.1},
{"u": 0.9, "v": 0.1},
{"u": 0.1, "v": 0.9},
{"u": 0.9, "v": 0.9},
{"u": 0.5, "v": 0.5}
],
"on": [
{"trigger": "movePoint", "modify": "movePoint", "values": "{u: movePoint.u, v: movePoint.v}"}
]
}
],

"scales": [
{
"name": "xscale",
"domain": [0, 1],
"range": "width"
},
{
"name": "yscale",
"domain": [0, 1],
"range": "height"
}
],

"marks": [
{
"name": "points",
"type": "symbol",
"zindex": 1,
"from": {"data": "table"},
"interactive": false,
"encode": {
"enter": {
"fill": {"value": "black"},
"size": {"value": 36},
"x": {"scale": "xscale", "field": "u"},
"y": {"scale": "yscale", "field": "v"}
}
}
},
{
"type": "path",
"from": {"data": "points"},
"encode": {
"enter": {
"stroke": {"value": "firebrick"},
"fill": {"value": "transparent"}
}
},
"transform": [
{
"type": "voronoi",
"x": "datum.x", "y": "datum.y",
"size": [{"signal": "width"}, {"signal": "height"}]
}
]
}
]
}

看来信号已经很好了。我使用 Vega 调试 View 对其进行了测试,它似乎可以正常工作:

  • 仅当鼠标按下期间发生鼠标移动时信号才会改变
  • 信号值位于正确的坐标系中,即数据使用的坐标系,即坐标介于 0 和 1 之间

不起作用的是交互:我无法拖动点,并且数据不会因鼠标交互而改变。我试图将触发器从数据移动到标记,但也没有用。我需要做什么才能拖动点?

最佳答案

我找到了如何为自己做这件事。问题是我使用的信号计算出正确的新坐标,但它不包含被拖来拖去的对象的 ID。我发现为此添加一个单独的信号最简单。因此,我们现在有了“NewPointPosition”(相同的信号,新名称)和“WhichPoint”,而不是一个“MovePoint”信号,其中包含被拖动对象的 ID。可能这可以合并为一个信号,但这样理解起来可能更容易。

我的第一个版本中缺少的另一个重要点是“点”和“路径”标记需要在“编码”中有一个“更新”子句,而不仅仅是一个“输入”子句。否则,数据会发生变化,但可视化不会对此使用react。

工作代码如下所示:

    {
"$schema": "https://vega.github.io/schema/vega/v4.json",
"width": 500,
"height": 200,
"autosize": "none",

"signals": [
{
"name": "whichPoint",
"on": [
{
"events": "path:click, path:mousemove[event.buttons]{20}",
"update": "datum.datum"
}
]
},
{
"name": "newPointPosition",
"on": [
{
"events": "path:click, path:mousemove[event.buttons]{20}",
"update": "{u: invert('xscale', x()), v: invert('yscale', y())}"
}
]
}
],

"data": [
{
"name": "table",
"values": [
{"u": 0.1, "v": 0.1},
{"u": 0.9, "v": 0.1},
{"u": 0.1, "v": 0.9},
{"u": 0.9, "v": 0.9},
{"u": 0.5, "v": 0.5}
],
"on": [
{"trigger": "whichPoint", "modify": "whichPoint", "values": "{u: newPointPosition.u, v:newPointPosition.v}"}
]
}
],

"scales": [
{
"name": "xscale",
"domain": [0, 1],
"range": "width"
},
{
"name": "yscale",
"domain": [0, 1],
"range": "height"
}
],

"marks": [
{
"name": "points",
"type": "symbol",
"zindex": 1,
"from": {"data": "table"},
"encode": {
"enter": {
"fill": {"value": "black"},
"size": {"value": 36},
"x": {"scale": "xscale", "field": "u"},
"y": {"scale": "yscale", "field": "v"}
},
"update": {
"fill": {"value": "black"},
"size": {"value": 36},
"x": {"scale": "xscale", "field": "u"},
"y": {"scale": "yscale", "field": "v"}
}
}
},
{
"type": "path",
"from": {"data": "points"},
"encode": {
"enter": {
"stroke": {"value": "firebrick"},
"fill": {"value": "transparent"}
},
"update": {
"stroke": {"value": "firebrick"},
"fill": {"value": "transparent"}
}
},
"transform": [
{
"type": "voronoi",
"x": "datum.x", "y": "datum.y",
"size": [{"signal": "width"}, {"signal": "height"}]
}
]
}
]
}

关于interaction - 在 Vega 中拖动数据点的鼠标交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53673056/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com