简体   繁体   English

如何使用angularjs在visjs中添加缩放按钮?

[英]How to add zoom in zoom out buttons in visjs using angularjs?

Need help in having a zoom in and zoom out button in visjs network graph using angularjs, I could not find any relevant options for this. 需要帮助在使用angularjs的visjs网络图中放大和缩小按钮,我找不到任何相关的选项。 Please help, I am also providing a plunker link as an example 请帮助,我也提供一个plunker链接作为例子

Code

<vis-network data="data" options="options" height="100%"></vis-network>

$scope.options = {
  autoResize: true,
  height: '800',
  width: '100%'
};

Take a look at the interaction, navigationButtons option. 看看交互,navigationButtons选项。 It has built in controls for zoom, pan and reset view. 它内置了缩放,平移和重置视图控件。

You need to change your vis options to include navigationButtons: true and keyboard: true to have keboard shortcuts enabled 您需要更改vis选项以包含navigationButtons: truekeyboard: true以启用keboard快捷方式

$scope.options = {
  autoResize: true,
  height: '600',
  width: '100%',
  interaction: {
      navigationButtons: true,
      keyboard: true
  }
};

Check this plunker 检查这个plunker

I've never worked with plunker, so I can not integrated my solution into your example, but I've created a JSFiddle for it which is based on a simple network example from the visjs.org website. 我从未使用过plunker,所以我无法将我的解决方案集成到您的示例中,但我已经为它创建了一个JSFiddle ,它基于visjs.org网站上的一个简单的网络示例。

Unfortunately there is no setScale(scale) method available right now, but you could extend the network until someone implements it. 遗憾的是,目前还没有可用的setScale(scale)方法,但您可以扩展network直到有人实现它。

var network;
var zoomstep = 0.3;

function zoomin() {
    network.setScale(network.getScale() - zoomstep);
}

function zoomout() {
    network.setScale(network.getScale() + zoomstep);
}

vis.Network.prototype.setScale = function (scale) {
    var options = {
        nodes: []
    };
    var range = this.view._getRange(options.nodes);
    var center = this.view._findCenter(range);
    var animationOptions = {
        position: center,
        scale: scale,
        animation: options.animation
    };
    this.view.moveTo(animationOptions);
};

The vis.Network.setScale code was taken from the Network.js and View.js source code, based on what getScale() did. vis.Network.setScale代码取自Network.js和View.js源代码,基于getScale() I had to redo some things which the methods View.fit , View._getRange and View._findCenter did but it's working good so far. 我不得不重做View.fitView._getRangeView._findCenter方法View.fit一些事情,但到目前为止它还运行良好。

Updated solution for vis.js 4.21.0 更新了vis.js 4.21.0的解决方案

vis.Network.prototype.zoom = function (scale) {
    const animationOptions = {
        scale: this.getScale() + scale,
        animation: { duration: 300 }
    };
    this.view.moveTo(animationOptions);
};

Click handler code: 单击处理程序代码

this.network.zoom(-0.5) // or 0.5 to zoom in

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

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