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

**URL:** <https://discuss.elastic.co/t/how-do-i-constrain-data-in-a-vega-visualization-to-the-visible-space/287131>\
**Category:** Kibana\
**Tags:** vega\
**Created:** [October 19, 2021, 8:05pm UTC](https://discuss.elastic.co/t/how-do-i-constrain-data-in-a-vega-visualization-to-the-visible-space/287131 "2021-10-19T20:05:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nsimi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nsimi/32/39013_2.png) [@nsimi](https://discuss.elastic.co/u/nsimi)\
**Post date:** [October 19, 2021, 8:05pm UTC](https://discuss.elastic.co/t/how-do-i-constrain-data-in-a-vega-visualization-to-the-visible-space/287131/1 "2021-10-19T20:05:57Z")

</div>

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:

```auto
{
  "$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!

---

<div class="post-metadata">

**Author:** ![bhavyarm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bhavyarm/32/22392_2.png) [@bhavyarm](https://discuss.elastic.co/u/bhavyarm)\
**Post date:** [October 25, 2021, 7:02pm UTC](https://discuss.elastic.co/t/how-do-i-constrain-data-in-a-vega-visualization-to-the-visible-space/287131/2 "2021-10-25T19:02:18Z")

</div>

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.](https://github.com/d3/d3-force) and then also ask here - [https://groups.google.com/g/vega-js](https://groups.google.com/g/vega-js)

Thanks,  
Bhavya

---

<div class="post-metadata">

**Author:** ![nsimi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nsimi/32/39013_2.png) [@nsimi](https://discuss.elastic.co/u/nsimi)\
**Post date:** [October 27, 2021, 5:54pm UTC](https://discuss.elastic.co/t/how-do-i-constrain-data-in-a-vega-visualization-to-the-visible-space/287131/3 "2021-10-27T17:54:20Z")

</div>

Hello @bhavyarm,

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 24, 2021, 5:54pm UTC](https://discuss.elastic.co/t/how-do-i-constrain-data-in-a-vega-visualization-to-the-visible-space/287131/4 "2021-11-24T17:54:41Z")

</div>

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