Search code examples
graphvisualizationvega-litevega

Vega lite add an Interactive Point Highlight


Could anyone helps me to add interactive highlight point on hover? I tried to add props from this example, but it did not work for me.

Here is my spec at Vega Lite Editor


Solution

  • The example is Vega-Lite but your spec is Vega. Try this - it gives a point and a tooltip for you.

    {
      "description": "Total Count line chart.",
      "width": 1200,
      "height": 450,
      "padding": 5,
      "signals": [{"name": "interpolate", "value": "linear"}],
      "legends": [
        {
          "fill": "color",
          "orient": "bottom",
          "direction": "horizontal",
          "symbolType": "square"
        }
      ],
      "data": [
        {
          "name": "table",
          "values": [
            {"x": 0, "y": 145, "c": "All"},
            {"x": 1, "y": 153, "c": "All"},
            {"x": 2, "y": 280, "c": "All"},
            {"x": 3, "y": 150, "c": "All"},
            {"x": 4, "y": 280, "c": "All"},
            {"x": 5, "y": 140, "c": "All"},
            {"x": 6, "y": 90, "c": "All"},
            {"x": 0, "y": 30, "c": "Bulk carrier"},
            {"x": 1, "y": 20, "c": "Bulk carrier"},
            {"x": 2, "y": 90, "c": "Bulk carrier"},
            {"x": 3, "y": 60, "c": "Bulk carrier"},
            {"x": 4, "y": 50, "c": "Bulk carrier"},
            {"x": 5, "y": 40, "c": "Bulk carrier"},
            {"x": 6, "y": 10, "c": "Bulk carrier"},
            {"x": 0, "y": 50, "c": "Tanker ship"},
            {"x": 1, "y": 10, "c": "Tanker ship"},
            {"x": 2, "y": 50, "c": "Tanker ship"},
            {"x": 3, "y": 40, "c": "Tanker ship"},
            {"x": 4, "y": 110, "c": "Tanker ship"},
            {"x": 5, "y": 40, "c": "Tanker ship"},
            {"x": 6, "y": 20, "c": "Tanker ship"},
            {"x": 0, "y": 5, "c": "Dingy"},
            {"x": 1, "y": 23, "c": "Dingy"},
            {"x": 2, "y": 20, "c": "Dingy"},
            {"x": 3, "y": 0, "c": "Dingy"},
            {"x": 4, "y": 30, "c": "Dingy"},
            {"x": 5, "y": 20, "c": "Dingy"},
            {"x": 6, "y": 0, "c": "Dingy"},
            {"x": 0, "y": 50, "c": "Carrier ship"},
            {"x": 1, "y": 60, "c": "Carrier ship"},
            {"x": 2, "y": 90, "c": "Carrier ship"},
            {"x": 3, "y": 40, "c": "Carrier ship"},
            {"x": 4, "y": 50, "c": "Carrier ship"},
            {"x": 5, "y": 20, "c": "Carrier ship"},
            {"x": 6, "y": 40, "c": "Carrier ship"},
            {"x": 0, "y": 10, "c": "Other"},
            {"x": 1, "y": 40, "c": "Other"},
            {"x": 2, "y": 30, "c": "Other"},
            {"x": 3, "y": 10, "c": "Other"},
            {"x": 4, "y": 40, "c": "Other"},
            {"x": 5, "y": 20, "c": "Other"},
            {"x": 6, "y": 20, "c": "Other"}
          ]
        }
      ],
      "scales": [
        {
          "name": "x",
          "type": "point",
          "range": "width",
          "domain": {"data": "table", "field": "x"}
        },
        {
          "name": "y",
          "type": "linear",
          "range": "height",
          "nice": true,
          "zero": true,
          "domain": {"data": "table", "field": "y"}
        },
        {
          "name": "color",
          "type": "ordinal",
          "range": [
            "#61c7f7",
            "#BA20CE",
            "#60cf85",
            "#cd2c4f",
            "#ceae39",
            "#ffffff"
          ],
          "domain": {"data": "table", "field": "c"}
        }
      ],
      "axes": [
        {"orient": "bottom", "scale": "x"},
        {"orient": "left", "scale": "y"}
      ],
      "config": {
        "style": {
          "guide-label": {"fontSize": 14, "fill": "#cccccc", "fontWeight": 800}
        },
        "axis": {"grid": true, "gridColor": "#333333"}
      },
      "marks": [
        {
          "type": "group",
          "from": {"facet": {"name": "series", "data": "table", "groupby": "c"}},
          "marks": [
            {
              "type": "line",
              "from": {"data": "series"},
              "encode": {
                "enter": {
                  "x": {"scale": "x", "field": "x"},
                  "y": {"scale": "y", "field": "y"},
                  "stroke": {"scale": "color", "field": "c"},
                  "strokeWidth": {"value": 2}
                },
                "update": {
                  "interpolate": {"signal": "interpolate"},
                  "strokeOpacity": {"value": 1}
                },
                "hover": {"strokeOpacity": {"value": 0.5}}
              }
            },
    
            {
              "type": "symbol",
              "from": {"data": "series"},
              "encode": {
                "update": {
                  "x": {"scale": "x", "field": "x"},
                  "y": {"scale": "y", "field": "y"},
                  "fillOpacity": {"value": 0}
                },
                
                "hover": {"fillOpacity": {"value": 1},
                "tooltip": {
                            "signal": "{'c':datum.c }"
                        }
                }
              }
            }
          ]
        }
      ]
    }