cytoscape.js Cxtmenu圆形菜单篇

很久之前写过一篇cytoscape.js基础篇, 地址是https://blog.csdn.net/dahaiaixiaohai/article/details/89669526. 可以适当的参考一下其内容.

很久之前写过一篇cytoscape.js初级篇, 地址是https://blog.csdn.net/dahaiaixiaohai/article/details/108862390. 可以适当的参考一下其内容.

本篇章, 正式使用cytoscape.js实现一些简单的节点菜单显示(提示)操作.

本文对应的源代码寄托于github, cytoscape.js Cxtmenu圆形菜单篇github源代码, 在此感谢github提供的服务.

系列文章

前言

本实例通过Vue开发测试, 其他开发方式可以参考本篇章, 适当的修改后使用.

cytoscape依赖引用

  • npm : npm install cytoscape --save
  • bower : bower install cytoscape
  • jspm : jspm install npm:cytoscape

cytoscape扩展依赖引用

  • 本篇章主要依赖cytoscape-cxtmenu组件. 所以务必保证该组件安装成功.

npm 布局及扩展依赖引用

# Cytoscape.js的圆形可滑动上下文扩展菜单
npm install cytoscape-cxtmenu --save
# Cytoscape.js的布局方式
npm install cytoscape-avsdf --save
npm install cytoscape-cola --save
npm install cytoscape-cose-bilkent --save

升级Cytoscape组件

扩展Cytoscape实例对象

<script>
  import cytoscape from 'cytoscape';
  import cxtmenu from 'cytoscape-cxtmenu';
  import cola from 'cytoscape-cola';
  import avsdf from 'cytoscape-avsdf';
  import coseBilkent from 'cytoscape-cose-bilkent';

  export default {
   
    // ......
    mounted() {
   
	  // Cxtmenu圆形菜单主要依赖组件
      if (!cytoscape().cxtmenu) {
   
        cytoscape.use(cxtmenu);
        cytoscape.use(cola);
        cytoscape.use(avsdf);
        cytoscape.use(coseBilkent);
      }
      this.$cy = cytoscape({
   ......});
      // Cxtmenu圆形菜单--开始
      this.$cy.cxtmenu({
   
        menuRadius: 80, // the radius of the circular menu in pixels
        selector: 'node', // elements matching this Cytoscape.js selector will trigger cxtmenus
        commands: (element) => {
   
          return [
            {
   
              fillColor: 'rgba(200, 200, 200, 0.75)', // optional: custom background color for item
              content: '<span class="fa fa-flash fa-2x">操作1</span>', // html/text content to be displayed in the menu
              contentStyle: {
   }, // css key:value pairs to set the command's css in js if you want
              select: function (ele) {
    // a function to execute when the command is selected
                alert(ele.id()); // `ele` holds the reference to the active element
              },
              enabled: true, // whether the command is selectable
            },
            {
   
              fillColor: 'rgba(200, 200, 200, 0.75)', // optional: custom background color for item
              content: '<span class="fa fa-flash fa-2x">操作2</span>', // html/text content to be displayed in the menu
              contentStyle: {
   }, // css key:value pairs to set the command's css in js if you want
              select: function (ele) {
    // a function to execute when the command is selected
                alert(ele.id()); // `ele` holds the reference to the active element
              },
              enabled: true, // whether the command is selectable
            },
            {
   
              // fillColor: 'rgba(200, 200, 200, 0.75)', // optional: custom background color for item
              content: '操作3', // html/text content to be displayed in the menu
              // contentStyle: {}, // css key:value pairs to set the command's css in js if you want
              select: (ele) => alert(ele.id()),  // a function to execute when the command is selected
              enabled: true, // whether the command is selectable
            },
            {
   
              // fillColor: 'rgba(200, 200, 200, 0.75)', // optional: custom background color for item
              content: '禁用', // html/text content to be displayed in the menu
              // contentStyle: {}, // css key:value pairs to set the command's css in js if you want
              select: (ele) => alert(ele.id()),  // a function to execute when the command is selected
              enabled: false, // whether the command is selectable
            }
          ]
        },
        fillColor: 'rgba(0, 0, 0, 0.75)', // 指令默认颜色(the background colour of the menu)
        activeFillColor: 'rgba(1, 105, 217, 0.75)', // 所选指令的颜色(the colour used to indicate the selected command)
        activePadding: 10, // additional size in pixels for the active command
        indicatorSize: 14, // the size in pixels of the pointer to the active command
        separatorWidth: 4, //连续命令之间的空白间隔(以像素为单位)
        spotlightPadding: 10, //元素和聚光灯之间的额外间距(以像素为单位)
        minSpotlightRadius: 10, // the minimum radius in pixels of the spotlight
        maxSpotlightRadius: 14, // the maximum radius in pixels of the spotlight
        openMenuEvents: 'cxttapstart taphold', // space-separated cytoscape events that will open the menu; only `cxttapstart` and/or `taphold` work here
        itemColor: 'white', // 各指令元素内字体颜色
        itemTextShadowColor: 'red', // 各指令元素内字体阴影颜色
        zIndex: 9999, // the z-index of the ui div
        atMouse: true, // draw menu at mouse position
      });
      //Cxtmenu圆形菜单--结束
      // 不同节点的样式
      this.$cy.style()......;
    },
  }
</script>

完整的Cytoscape组件代码

<style scoped>
</style>

<template>
  <div style="position: relative; height: 100%; width: 100%; z-index: 0;">
    <div id="cytoscape_id" style="height: 100%; width: 100%; z-index: 1;"></div>
  </div>
</template>

<script>
  import cytoscape from 'cytoscape';
  import cxtmenu from 'cytoscape-cxtmenu';
  import cola from 'cytoscape-cola';
  import avsdf from 'cytoscape-avsdf';
  import coseBilkent from 'cytoscape-cose-bilkent';

  export default {
   
    name: "CJS",
    beforeCreate() {
   
      this.$cy && this.$cy.destroyed() && this.$cy.destroy();
      delete this.$cy;
    },
    beforeDestroy() {
   
      this.$cy && this.$cy.
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
您好,针对您的问题,我理解您想要在 Cytoscape.js 中实现节点的展开和收缩功能,并且想要通过 cxtMenu 来实现。在 Cytoscape.js 中,节点的展开和收缩可以通过 `collapse` 和 `expand` 方法来实现。而在 cxtMenu 中,则需要通过自定义菜单项来触发这些操作。 具体实现方法如下: 1. 在 Cytoscape.js 的配置中添加 cxtmenu 插件,并定义需要显示的菜单项。例如: ```js import cytoscape from 'cytoscape'; import cxtmenu from 'cytoscape-cxtmenu'; cytoscape.use(cxtmenu); const cy = cytoscape({ container: document.getElementById('cy'), elements: yourElements, style: yourStyles, layout: yourLayout, // 添加 cxtmenu 插件并定义菜单cxtmenu: { selector: 'node', commands: [ { content: 'Collapse', select: function(ele){ ele.collapse(); } }, { content: 'Expand', select: function(ele){ ele.expand(); } } ] } }); ``` 2. 在菜单项中定义 `select` 方法,当用户点击菜单项时,调用 `collapse` 或 `expand` 方法来改变节点的状态。例如: ```js { content: 'Collapse', select: function(ele){ ele.collapse(); } }, { content: 'Expand', select: function(ele){ ele.expand(); } } ``` 需要注意的是,`collapse` 和 `expand` 方法需要在 `cola` 布局插件加载后才可用。因此,您需要在 Cytoscape.js 的配置中添加 `layout` 属性,并使用 `cola` 布局插件。例如: ```js import cytoscape from 'cytoscape'; import cola from 'cytoscape-cola'; cytoscape.use(cola); const cy = cytoscape({ container: document.getElementById('cy'), elements: yourElements, style: yourStyles, layout: { name: 'cola' }, // 添加 cxtmenu 插件并定义菜单cxtmenu: { selector: 'node', commands: [ { content: 'Collapse', select: function(ele){ ele.collapse(); } }, { content: 'Expand', select: function(ele){ ele.expand(); } } ] } }); ``` 通过上述方法,您就可以在 Cytoscape.js 中实现节点的展开和收缩功能,并且通过 cxtMenu 来触发这些操作了。希望能对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值