How do I constrain data in a vega visualization to the visible space?

Hello All,

is there a way to constrain all the data in a visualization to the area with in the visualization?

Here is an example:

{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "width": 700,
  "height": 500,
  "padding": 0,
  "autosize": "none",

  "signals": [
    { "name": "cx", "update": "width / 2" },    
    { "name": "cy", "update": "height / 2" },
    { "name": "nodeRadius", "value": 8 },
    { "name": "nodeCharge", "value": -100 },
    { "name": "linkDistance", "value": 30 },
    { "name": "static", "value": true },
    {
      "description": "State variable for active node fix status.",
      "name": "fix", "value": false,
      "on": [
        {
          "events": "symbol:mouseout[!event.buttons], window:mouseup",
          "update": "false"
        },
        {
          "events": "symbol:mouseover",
          "update": "fix || true"
        },
        {
          "events": "[symbol:mousedown, window:mouseup] > window:mousemove!",
          "update": "xy()",
          "force": true
        }
      ]
    },
    {
      "description": "Graph node most recently interacted with.",
      "name": "node", "value": null,
      "on": [
        {
          "events": "symbol:mouseover",
          "update": "fix === true ? item() : node"
        }
      ]
    },
    {
      "description": "Flag to restart Force simulation upon data changes.",
      "name": "restart", "value": false,
      "on": [
        {"events": {"signal": "fix"}, "update": "fix && fix.length"}
      ]
    }
  ],

  "data": [
    {
      "name": "node-data",
      "values":[
        { 
          "names":"abe",
          "index":0,
          "group":1
        },
        { 
          "names":"josh",
          "index":1,
          "group":1
        },
        { 
          "names":"sean",
          "index":2,
          "group":1
        },
        { 
          "names":"tony",
          "index":3,
          "group":1
        },
        { 
          "names":"tom",
          "index":4,
          "group":1
        },
        { 
          "names":"jonathan",
          "index":5,
          "group":1
        },
        { 
          "names":"brandon",
          "index":6,
          "group":1
        },
        { 
          "names":"matt",
          "index":7,
          "group":1
        },
        { 
          "names":"morten",
          "index":8,
          "group":1
        },
        { 
          "names":"todd",
          "index":9,
          "group":1
        },
        { 
          "names":"lars",
          "index":10,
          "group":1
        }
      ]
    },
    {
      "name": "link-data",
      "values":[
        {
          "source":0,
          "target":1
        },
        {
          "source":2,
          "target":3
        }
      ]
    }
  ],

  "scales": [
    {
      "name": "color",
      "type": "ordinal",
      "domain": {"data": "node-data", "field": "group"},
      "range": {"scheme": "category20c"}
    }
  ],

  "marks": [
    {
      "name": "nodes",
      "type": "symbol",
      "zindex": 1,

      "from": {"data": "node-data"},
      "on": [
        {
          "trigger": "fix",
          "modify": "node",
          "values": "fix === true ? {fx: node.x, fy: node.y} : {fx: fix[0], fy: fix[1]}"
        },
        {
          "trigger": "!fix",
          "modify": "node", "values": "{fx: null, fy: null}"
        }
      ],

      "encode": {
        "enter": {
          "fill": {"scale": "color", "field": "group"},
          "stroke": {"value": "white"}
        },
        "update": {
          "size": {"signal": "2 * nodeRadius * nodeRadius"},
          "cursor": {"value": "pointer"}
        }
      },

      "transform": [
        {
          "type": "force",
          "iterations": 300,
          "restart": {"signal": "restart"},
          "static": {"signal": "static"},
          "signal": "force",
          "forces": [
            {"force": "center", "x": {"signal": "cx"}, "y": {"signal": "cy"}},
            {"force": "collide", "radius": {"signal": "nodeRadius"}},
            {"force": "nbody", "strength": {"signal": "nodeCharge"}},
            {"force": "link", "links": "link-data", "distance": {"signal": "linkDistance"}}
          ]
        }
      ]
    },
    {
      "type": "path",
      "from": {"data": "link-data"},
      "interactive": false,
      "encode": {
        "update": {
          "stroke": {"value": "#ccc"},
          "strokeWidth": {"value": 0.5}
        }
      },
      "transform": [
        {
          "type": "linkpath",
          "require": {"signal": "force"},
          "shape": "line",
          "sourceX": "datum.source.x", "sourceY": "datum.source.y",
          "targetX": "datum.target.x", "targetY": "datum.target.y"
        }
      ]
    }
  ]
}

What I want to, is to make it so that all 10 data nodes are visible, but they aren't. Is there a simple way to force this that I have missed or some more complicated way to do it?

Thank you!

Hello,

It looks like we don't know. So the suggestion from our vega viz expert is to check here - GitHub - d3/d3-force: Force-directed graph layout using velocity Verlet integration. and then also ask here - https://groups.google.com/g/vega-js

Thanks,
Bhavya

Hello @bhavyarm,

Thank you for the reply I will give that a try.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.