Set Zoom Level in Vis.js
Asked Answered
O

1

10

I have a network that I'm building with vis.js, but it is too large width-wise to fit in the pages' container. The network runs left to right and contains steps about a specific process. As a person completes tasks, a server feeds new JSON records to update the colors.

I can't change the container size due to the layout. When the network loads, it causes the font to be incredibly small and unreadable. Is there a way I could set the zoom level as an option so that only the current step in the process will show?

function draw() {

test = null;

// create a network
var testContainer = document.getElementById('testing');

// create some nodes
var width = testing.clientWidth;

var nodes = [
  {id: 1, x: 0, y: 1, label: 'Start', color: 'green', shape: 'circle'},
  {id: 2, label: 'Step 2', shape: 'square'},
  {id: 3, label: 'Step 3', shape: 'square'},
  {id: 4, label: 'Step 4', shape: 'square'},
  {id: 5, label: 'Step 5', shape: 'square'},
  {id: 6, label: 'Step 6', shape: 'square'},
  {id: 7, label: 'Step 7', shape: 'square'},
  {id: 8, label: 'Step 8', shape: 'square'},
  {id: 9, label: 'Step 9', shape: 'square'},
  {id: 10, label: 'Step 10', shape: 'square'},
  {id: 11, label: 'Step 11', shape: 'square'},
  {id: 12, x: width * 2, y: 1, label: 'Success!', shape: 'circle'}
];

// create some edges
var edges = [
  {from: 1, to: 2, style: 'arrow', color: 'red', width: 1, length: 200}, // individual length definition is possible
  {from: 2, to: 3, style: 'arrow', width: 1, length: 200},
  {from: 3, to: 4, style: 'arrow', width: 1, length: 200},
  {from: 4, to: 5, style: 'arrow', width: 1, length: 200},
  {from: 5, to: 6, style: 'arrow', width: 1, length: 200},
  {from: 6, to: 7, style: 'arrow', width: 1, length: 200},
  {from: 7, to: 8, style: 'arrow', width: 1, length: 200},
  {from: 8, to: 9, style: 'arrow', width: 1, length: 200},
  {from: 9, to: 10, style: 'arrow', width: 1, length: 200},
  {from: 10, to: 11, style: 'arrow', width: 1, length: 200},
  {from: 11, to: 12, style: 'arrow', width: 1, length: 200}
];

var testData = {
  nodes: nodes,
  edges: edges
};
var testOptions = {
        width: '100%'
};
var test = new vis.Network(testContainer, testData, testOptions);
}
Oeo answered 12/12, 2014 at 18:58 Comment(0)
W
12

You can use functions like fit, focus, and moveTo change the zoom level.

Example: https://visjs.github.io/vis-network/examples/network/other/animationShowcase.html

Docs: https://visjs.github.io/vis-network/docs/network/

Wool answered 19/12, 2014 at 10:29 Comment(3)
Thanks! I don't know how I overlooked that. The syntax is a bit odd, but this ended working for me: test.focusOnNode(1,{scale: 1, offset:{x: -(width/3)}});Oeo
Can you please take a look with my problem with vis timeline?Herbarium
Please update the links, they no longer works.Abeyta

© 2022 - 2024 — McMap. All rights reserved.