简体   繁体   English

vega中分组条形图的工具提示(使用组标记)

[英]tooltips on grouped bar charts in vega (using group marks)

I'm using vega to create a grouped bar chart and I would want to make some numbers (in my label data field) appear when I hover my mouse over one bar or a subgroup of bars. 我正在使用vega创建一个分组的条形图,当我将鼠标悬停在一个条形图或一组条形图上时,我想使一些数字出现(在标签数据字段中)。 The json object below seems to work, but it shows all the number of all the bars when I move over a part of the graph. 下面的json对象似乎可以正常工作,但是当我在图形的一部分上移动时,它显示了所有条形的所有数目。

{
  "scales": [
    {
      "padding": 0.2, 
      "range": "height", 
      "type": "ordinal", 
      "domain": {
        "field": "category", 
        "data": "table"
      }, 
      "name": "cat"
    }, 
    {
      "domain": {
        "field": "value", 
        "data": "table"
      }, 
      "name": "val", 
      "range": "width", 
      "type": "linear", 
      "round": "true", 
      "nice": "true"
    }, 
    {
      "range": "category20", 
      "type": "ordinal", 
      "domain": {
        "field": "position", 
        "data": "table"
      }, 
      "name": "color"
    }
  ], 
  "axes": [
    {
      "tickSize": 0, 
      "scale": "cat", 
      "type": "y", 
      "tickPadding": 8
    }, 
    {
      "scale": "val", 
      "type": "x"
    }
  ], 
  "signals": [
    {
      "name": "tooltip",
      "init": {},
      "streams": [
        {"type": "rect:mouseover", "expr": "datum"},
        {"type": "rect:mouseout", "expr": "{}"}
      ]
    }
  ],
  "predicates": [
    {
      "name": "tooltip", "type": "==", 
      "operands": [{"signal": "tooltip._id"}, {"arg": "id"}]
    }
  ],  
  "height": 800, 
  "width": 600, 
  "marks": [
    {
      "from": {
        "data": "table", 
        "transform": [
          {
            "type": "facet", 
            "groupby": [
              "category"
            ]
          }
        ]
      }, 
      "marks": [
        {
          "type": "rect", 
          "name": "bars", 
          "properties": {
            "enter": {
              "y": {
                "field": "position", 
                "scale": "pos"
              }, 
              "x": {
                "field": "value", 
                "scale": "val"
              }, 
              "x2": {
                "scale": "val", 
                "value": 0
              }, 
              "fill": {
                "field": "position", 
                "scale": "color"
              }, 
              "height": {
                "band": "true", 
                "scale": "pos"
              }
            }
          }
        }, 
        {
          "from": {
            "mark": "bars"
          }, 
          "type": "text", 
          "properties": {
            "enter": {
              "align": {"value": "center"},
              "fill": {"value": "#333"}
            },
            "update": {
              "x": { "field": "x2"},
              "dy": {"field":"height", "mult": 0.5},
              "y": {"field":"y"},
              "text": {"field": "datum.label"},
              "align": {"value":"center"},
              "baseline":{"value":"middle"},
              "fillOpacity": {
                "rule": [
                  {
                    "predicate": {"name": "tooltip", "id": {"value": null}},
                    "value": 0
                  },
                  {"value": 1}
                ]
              }
            }
          }
        }
      ], 
      "type": "group", 
      "properties": {
        "enter": {
          "y": {
            "field": "key", 
            "scale": "cat"
          }, 
          "height": {
            "band": "true", 
            "scale": "cat"
          }
        }
      }, 
      "scales": [
        {
          "range": "height", 
          "type": "ordinal", 
          "name": "pos", 
          "domain": {
            "field": "position"
          }
        }
      ]
    }
  ], 
  "data": [
    {
      "values": [
        {
          "category": "A", 
          "position": 1, 
          "value": 1661.0, 
          "label": 40.0
        }, 
        {
          "category": "A", 
          "position": 2, 
          "value": 2928.0, 
          "label": 35.0
        }, 
        {
          "category": "A", 
          "position": 3, 
          "value": 9010.0, 
          "label": 69.0
        }, 
        {
          "category": "A", 
          "position": 4, 
          "value": 6459.0, 
          "label": 97.0
        }, 
        {
          "category": "B", 
          "position": 1, 
          "value": 1022.0, 
          "label": 39.0
        }, 
        {
          "category": "B", 
          "position": 2, 
          "value": 1185.0, 
          "label": 33.0
        }, 
        {
          "category": "B", 
          "position": 3, 
          "value": 567.0, 
          "label": 60.0
        }, 
        {
          "category": "B", 
          "position": 4, 
          "value": 759.0, 
          "label": 84.0
        }
      ], 
      "name": "table"
    }
  ]
} 

I also tried to use signals in the mark block as shown below, but this doens't give me the desired result (all numbers are shown stacked on top of each other, unreadable). 我还尝试在标记块中使用信号,如下所示,但这并不能给我想要的结果(所有数字都显示为堆叠在一起,难以辨认)。

  {
      "from": {
        "mark": "bars"
      }, 
      "type": "text", 
      "properties": {
        "enter": {
          "align": {"value": "center"},
          "fill": {"value": "#333"}
        },
        "update": {
          "x": { "signal": "tooltip.x2"},
          "dy": {"field":"height", "mult": 0.5},
          "y": {"signal":"tooltip.y"},
          "text": {"field": "datum.label"},
          "align": {"value":"center"},
          "baseline":{"value":"middle"},
          "fillOpacity": {
            "rule": [
              {
                "predicate": {"name": "tooltip", "id": {"value": null}},
                "value": 0
              },
              {"value": 1}
            ]
          }
        }
      }
    }

Can anyone help out how to get the desired result and to understand how the data is passed of a (sub)mark to that signal? 谁能帮忙获得期望的结果,并理解如何将(子)标记的数据传递给该信号?

I solved the issue by specifying three predicates, one that checks if datum.position equals the position given by the tooltip's signal datum. 我通过指定三个谓词解决了这个问题,其中一个谓词检查datum.position是否等于工具提示的信号数据给定的位置。 Another predicate for checking the category and a last predicate that checks both these conditions. 用于检查类别的另一个谓词和用于检查这两个条件的最后一个谓词。 When you hoover your mouse over a bar in the chart and the position and category datum of that bar are the same as in the tooltip's signal, the text is made opaque, otherwise it remains transparent. 当您将鼠标悬停在图表中的条形上并且该条形的位置和类别数据与工具提示的信号中的相同时,该文本将变为不透明,否则将保持透明。

{
  "scales": [
    {
      "padding": 0.2, 
      "range": "height", 
      "type": "ordinal", 
      "domain": {
        "field": "category", 
        "data": "table"
      }, 
      "name": "cat"
    }, 
    {
      "domain": {
        "field": "value", 
        "data": "table"
      }, 
      "name": "val", 
      "range": "width", 
      "type": "linear", 
      "round": "true", 
      "nice": "true"
    }, 
    {
      "range": "category20", 
      "type": "ordinal", 
      "domain": {
        "field": "position", 
        "data": "table"
      }, 
      "name": "color"
    }
  ], 
  "axes": [
    {
      "tickSize": 0, 
      "scale": "cat", 
      "type": "y", 
      "tickPadding": 8
    }, 
    {
      "scale": "val", 
      "type": "x"
    }
  ], 
  "signals": [
    {
      "name": "tooltip",
      "init": {},
      "streams": [
        {"type": "rect:mouseover", "expr": "datum"},
        {"type": "rect:mouseout", "expr": "{}"}
      ]
    }
  ],
  "predicates": [
    {
      "name": "isPosition", "type": "==", 
      "operands": [{"signal": "tooltip.position"}, {"arg": "position"}]
    },
    {
      "name": "isCategory", "type": "==", 
      "operands": [{"signal": "tooltip.category"}, {"arg": "category"}]
    },
    {
      "name": "iftooltip", "type": "and", 
      "operands": [{"predicate": "isPosition"}, {"predicate": "isCategory"}]
    }
  ],  
  "height": 800, 
  "width": 600, 
  "marks": [
    {
      "from": {
        "data": "table", 
        "transform": [
          {
            "type": "facet", 
            "groupby": [
              "category"
            ]
          }
        ]
      }, 
      "marks": [
        {
          "type": "rect", 
          "name": "bars", 
          "properties": {
            "enter": {
              "y": {
                "field": "position", 
                "scale": "pos"
              }, 
              "x": {
                "field": "value", 
                "scale": "val"
              }, 
              "x2": {
                "scale": "val", 
                "value": 0
              }, 
              "fill": {
                "field": "position", 
                "scale": "color"
              }, 
              "height": {
                "band": "true", 
                "scale": "pos"
              }
            }
          }
        }, 
        {
          "from": {
            "mark": "bars"
          }, 
          "type": "text", 
          "properties": {
            "enter": {
              "align": {"value": "center"},
              "fill": {"value": "#333"}
            },
            "update": {
              "x": { "field": "x2","offset":-10},
              "y": {"field":"y"},
              "dy": {"field": "height", "mult": 0.5},
              "text": {"signal": "tooltip.label"},
              "align": {"value":"center"},
              "baseline":{"value":"middle"},
              "fillOpacity": {
                "rule": [
                {
                  "predicate": {"name": "iftooltip", "position": {"field": "datum.position"},"category":{"field":"datum.category"}},
                  "value": 1
                },
                {"value": 0}
            ]}
            }
          }
        }
      ], 
      "type": "group", 
      "properties": {
        "enter": {
          "y": {
            "field": "key", 
            "scale": "cat"
          }, 
          "height": {
            "band": "true", 
            "scale": "cat"
          }
        }
      }, 
      "scales": [
        {
          "range": "height", 
          "type": "ordinal", 
          "name": "pos", 
          "domain": {
            "field": "position"
          }
        }
      ]
    }
  ], 
  "data": [
    {
      "values": [
        {
          "category": "A", 
          "position": 1, 
          "value": 1661.0, 
          "label": 40.0
        }, 
        {
          "category": "A", 
          "position": 2, 
          "value": 2928.0, 
          "label": 35.0
        }, 
        {
          "category": "A", 
          "position": 3, 
          "value": 9010.0, 
          "label": 69.0
        }, 
        {
          "category": "A", 
          "position": 4, 
          "value": 6459.0, 
          "label": 97.0
        }, 
        {
          "category": "B", 
          "position": 1, 
          "value": 1022.0, 
          "label": 39.0
        }, 
        {
          "category": "B", 
          "position": 2, 
          "value": 1185.0, 
          "label": 33.0
        }, 
        {
          "category": "B", 
          "position": 3, 
          "value": 567.0, 
          "label": 60.0
        }, 
        {
          "category": "B", 
          "position": 4, 
          "value": 759.0, 
          "label": 84.0
        }
      ], 
      "name": "table"
    }
  ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM