Cytoscape adds a custom image to a node using clickable elements on it.

I am creating a charting tool in ember using cytoscape js and I can display the chart data, but I do not know how to set each node to display with an image that has other images / buttons that function inside it. Basically, I want it to look like this:

enter image description here

There are two buttons on the image (I’ll most likely add icons), and there are also labels that exist in the node, which I don’t know how to do.

Here is the code I have.

Template:

<div class="container" >
  <div id="cy"></div>
</div>

JS Component:

import Ember from 'ember';


export default Ember.Component.extend({
tagName: '',

map: Ember.computed('model.map_data', function()
{
 if(this.get('model.map_data')){
   return JSON.parse(this.get('model.map_data').data)
  } else {
   return {};
  }
 }),
cytoscape_data: Ember.computed('model.sub_apps.[]',function() {
var ret = {
        nodes: [],
        edges: []
};
var red = 50;//replace with threshold
var green = 25;//replace with threshold
var _this = this;
this.get("model").map_data.forEach(function(node) {
  var y= 0;
  var x = 0;
  var color = 'green';
  if(node.value >= red ){
    color = 'red';
  }else {
    if(node.value > green){
      color = 'orange';
    }
  }
  var position = _this.get("map")["app" + node.id];
  if(position){
    x = parseInt(position.split(',')[0]);
    y = parseInt(position.split(',')[1]);
  }
  ret["nodes"].push({
          data: {
                  id: node.id,
                  label: node.name,
                  node_type: 'app',
                  tooltip: node.description,
                  color: color
          },
          position: {
                  x: x,
                  y: y
          }
  });
  if(node.relations) {
    node.relations.forEach(function(parent) {

      ret["edges"].push({
        data: {
          source: node.id,
          target: parent.app_to_id
        }
      });
    });
  }
});

 return ret;
}),

didInsertElement: function() {
 this._super();
 var cy = cytoscape({
 container: Ember.$('#cy')[0],
 elements: this.get("cytoscape_data"),
 zoom: 1,
 pan: { x: 0, y: 0 },
 fit: true,
 randomize: false,
 layout: {
      name: 'preset'
    },
  style: [
    {
      selector: 'node',
      style: {
        'content': 'data(label)',
        'text-opacity': 0.8,
        'text-valign': 'center',
        'text-halign': 'right',
        'width': '200px',
        'height': '200px',
        'border-color': 'green',
        'border-width': 3,
        'border-opacity': 0.5,
        'background-image': 'url(../assets/images/base_node_image.svg)'
        // 'background-color': 'data(color)'
      }
    },
    {
      selector: 'edge',
      style: {
        'width': 6,
        'border-color': 'green',
        'target-arrow-shape': 'triangle',
        'target-arrow-color': 'red',
        'opacity': 1,
        'curve-style': 'bezier'
      }
    },

    {
      selector: ':selected',
      style: {
        'background-color': 'orange',
        'opacity': 1
      }
    },

    {
      selector: '.faded',
      style: {
        'opacity': 0.0,
        'text-opacity': 0
      }
    },
  ],

});
Ember.run.scheduleOnce('afterRender', this, function(){
  cy;
});
cy.on('click', 'node', function(evt){
  var node = evt.target;
  console.log( 'clicked ' + node.data('label') );
   });
  },
});

The diagram displayed by this code is as follows:

enter image description here

, , , . , , , , ( node ). node, , node.

, !

+4
1

, , . : " node , /, ."; , html ; , cytoscape , HTML canvas.

. @maxfranz ( cytoscape.js) ; : " HTML , , , ".

, html, URL- , . . MDN Web Docs .

; , , ; . cytoscape compound node s. . github.

, , :

enter image description here

, .

+1

Source: https://habr.com/ru/post/1690402/


All Articles