Git Product home page Git Product logo

ivis-at-bilkent / cytoscape.js-grid-guide Goto Github PK

View Code? Open in Web Editor NEW
58.0 16.0 9.0 563 KB

A Cytsocape.js extension to provide a framework for grid interactions such as grid lines and snapping to grid, and guidelines and snap support for alignment of nodes.

License: MIT License

JavaScript 93.28% HTML 6.72%
cytoscapejs cytoscapejs-extension network-visualization diagramming alignment-guidelines grid graph-drawing graphviz

cytoscape.js-grid-guide's Introduction

cytoscape-grid-guide

Description

Guideline example Grid example

A sophisticated and highly customizable Cytoscape.js extension for grid and guideline interactions, distributed under The MIT License. The following features are provided:

  • Grid: Shows a grid in the canvas (with specified size).
  • Auto resizing nodes: Nodes are resized to fit perfectly to grid.
  • Alignment guidelines on drag: As a node is dragged different types of guidelines are drawn to indicate both horizontal and vertical alignment of the dragged node with others. Two nodes are considered aligned when the difference between their x or y coordinates are within the specified tolerance:
    • Reference guidelines: When the dragged node aligns horizontally or vertically with the position of the mouse when the node is picked for drag, this guideline is drawn.
    • Geometric guidelines: When the dragged node (center or edge) aligns horizontally or vertically with another node (center or edge) within specified range, this guideline is drawn.
    • Distribution guidelines: This type of guideline is drawn in one of these two different cases:
      • when the center of the dragged node is in the middle of the closer edges of two other nodes horizontally or vertically (i.e. dragged node is in the middle of two other nodes)
      • when the distance between the dragged node A to node C is exactly twice the distance to node B horizontally or vertically (i.e. node B is in the middle of the dragged node A and another node C)
  • Snapping nodes: Nodes are snapped either to the grid or alignment location. The following options are available:
    • Snap to grid on mouse release: Upon release of the mouse during drag, the node snaps to theclosest grid location.
    • Snap to grid during drag: During drag the node snaps to the closest grid location; dragging is discrete.
    • Snap to alignment location on mouse release: Note that guidelines are drawn when the node is within a certain 'tolerance' amount to perfect alignment. Upon release of the mouse during drag, the node snaps to shown guideline to form perfect alignment.
    • Snap to alignment location during drag: Note that guidelines are drawn when the node is within a certain 'tolerance' amount to perfect alignment. During drag, the node snaps to shown guideline to form perfect alignment.

The users are allowed to customize various things about the functionality including:

  • grid size and color
  • snap type
  • alignment guideline tolerance
  • colors and line styles of each type of guidelines
  • range of geometric and distribution guidelines (i.e. do not consider nodes that are outside this range for alignment)
  • whether or not edge to center alignment should be ignored
  • whether to snap nodes to the center of the grid or to the lines of the grid

Please cite the following paper when using this extension:

U. Dogrusoz , A. Karacelik, I. Safarli, H. Balci, L. Dervishi, and M.C. Siper, "Efficient methods and readily customizable libraries for managing complexity of large networks", PLoS ONE, 13(5): e0197238, 2018.

Demo

Here is a demo:

API

  • cy.gridGuide(options) Sets stated options any time wanted.

  • eles.align(horizontal, vertical, alignTo) Aligns vertically/horizontally dimensions of eles to first element of eles ( or if alignTo is specified aligns to it). horizontal param may get top, center, bottom and vertical param may get left, center, right and horizontal.

For example the code below aligns selected nodes to top left of first selected node.

cy.nodes(":selected").align("top", "left")

Default Undo/Redo Actions

ur.do("align", {
    nodes: cy.nodes(":selected"),
    vertical: "left",
    horizontal: "top",
    alignTo: cy.nodes(":selected")[0],
})

Default Options

var options = {
    // On/Off Modules
    /* From the following four snap options, at most one should be true at a given time */
    snapToGridOnRelease: true, // Snap to grid on release
    snapToGridDuringDrag: false, // Snap to grid during drag
    snapToAlignmentLocationOnRelease: false, // Snap to alignment location on release
    snapToAlignmentLocationDuringDrag: false, // Snap to alignment location during drag
    distributionGuidelines: false, // Distribution guidelines
    geometricGuideline: false, // Geometric guidelines
    initPosAlignment: false, // Guideline to initial mouse position
    centerToEdgeAlignment: false, // Center to edge alignment
    resize: false, // Adjust node sizes to cell sizes
    parentPadding: false, // Adjust parent sizes to cell sizes by padding
    drawGrid: true, // Draw grid background

    // General
    gridSpacing: 20, // Distance between the lines of the grid.
    snapToGridCenter: true, // Snaps nodes to center of gridlines. When false, snaps to gridlines themselves. Note that either snapToGridOnRelease or snapToGridDuringDrag must be true.

    // Draw Grid
    zoomDash: true, // Determines whether the size of the dashes should change when the drawing is zoomed in and out if grid is drawn.
    panGrid: false, // Determines whether the grid should move then the user moves the graph if grid is drawn.
    gridStackOrder: -1, // Namely z-index
    gridColor: '#dedede', // Color of grid lines
    lineWidth: 1.0, // Width of grid lines

    // Guidelines
    guidelinesStackOrder: 4, // z-index of guidelines
    guidelinesTolerance: 2.00, // Tolerance distance for rendered positions of nodes' interaction.
    guidelinesStyle: { // Set ctx properties of line. Properties are here:
        strokeStyle: "#8b7d6b", // color of geometric guidelines
        geometricGuidelineRange: 400, // range of geometric guidelines
        range: 100, // max range of distribution guidelines
        minDistRange: 10, // min range for distribution guidelines
        distGuidelineOffset: 10, // shift amount of distribution guidelines
        horizontalDistColor: "#ff0000", // color of horizontal distribution alignment
        verticalDistColor: "#00ff00", // color of vertical distribution alignment
        initPosAlignmentColor: "#0000ff", // color of alignment to initial mouse location
        lineDash: [0, 0], // line style of geometric guidelines
        horizontalDistLine: [0, 0], // line style of horizontal distribution guidelines
        verticalDistLine: [0, 0], // line style of vertical distribution guidelines
        initPosAlignmentLine: [0, 0], // line style of alignment to initial mouse position
    },

    // Parent Padding
    parentSpacing: -1 // -1 to set paddings of parents to gridSpacing
};

Dependencies

  • Cytoscape.js ^3.3.0

Usage instructions

Download the library:

  • via npm: npm install cytoscape-grid-guide,
  • via bower: bower install cytoscape-grid-guide, or
  • via direct download in the repository (probably from a tag).

require() the library as appropriate for your project:

CommonJS:

var cytoscape = require('cytoscape');
var gridGuide = require('cytoscape-grid-guide');

gridGuide( cytoscape ); // register extension

AMD:

require(['cytoscape', 'cytoscape-grid-guide'], function( cytoscape, gridGuide ){
    gridGuide( cytoscape ); // register extension
});

Plain HTML/JS has the extension registered for you automatically, because no require() is needed.

Publishing instructions

This project is set up to automatically be published to npm and bower. To publish:

  1. Set the version number environment variable: export VERSION=1.2.3
  2. Publish: gulp publish
  3. If publishing to bower for the first time, you'll need to run bower register cytoscape-grid-guide https://github.com/iVis-at-Bilkent/cytoscape.js-grid-guide.git

Team

cytoscape.js-grid-guide's People

Contributors

gregroyal avatar hasanbalci avatar kinimesi avatar lukethacoder avatar metincansiper avatar mrsfy avatar msalihaltun avatar nasimsaleh avatar royludo avatar ugurdogrusoz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cytoscape.js-grid-guide's Issues

Parent Nodes undraggable

Dragging via parent nodes is not possible. Need to modify applyToCyTarget on events_controller module.

Snap during drag

Current we snap on mouse release. Can we (optionally if not complicated) make it snap during drag?

Be compliant with JS strict mode

I'm using a framework enforcing the JS strict mode and I encountering some errors due to some uninitialized variables like

  • calculateOffset in guideline.js
  • padding in parentPadding.js

Is it possible to ensure the respect of the strict mode ?

Guidelines performance

Performance on drag is in state of concern due to guidelines espesically in bigger graphs.

Grid isn't drawn in latest Firefox

Although i think the issue is related to the latest Firefox version i'd like to point out that the grid isn't drawn in Firefx 59.0.1
Older versions, before Firefox Quantum, seems to work fine.

Automove Support?

i'm trying to use the automove extension together with grid-guide. while the grabbed nodes are correctly aligned to the grid, it seems the nodes automatically moved by automove are not. the effect can be seen if you move the upper node in this fiddle.

please let me know if i missed something obvious! :-)

Typescript / Angular 14 support

I have trouble to import the grud guide module to my angular project.

I get the following error:

Could not find a declaration file for module 'cytoscape-grid-guide'. '/home/user/xxx/node_modules/cytoscape-grid-guide/src/index.js' implicitly has an 'any' type. Try npm i --save-dev @types/cytoscape-grid-guideif it exists or add a new declaration (.d.ts) file containingdeclare module 'cytoscape-grid-guide';``

Is there a typescript type definition that i can use?
Maybe you can create one for npm?

TypeError $ is not a function

Initialization of the plugin does not work in my setting due to the above error. In order to make it work I had to adjust the plugin as given below, maybe someone finds this helpful.

},{"functional-red-black-tree":1}],7:[function(require,module,exports){
;(function($){ 'use strict';
// registers the extension on a cytoscape lib ref
[ ... ]
})(jQuery);

Meet UglifyJs error while building my web code

Have you ever made some uglifyjs related plugins config in your code?

I tried to compile my web code, but met the error:

ERROR in static/js/vendor.c2fa7ee7f34202a87f40.js from UglifyJs
Unexpected token name «of», expected punc «;» [./node_modules/cytoscape-grid-guide/src/guidelines.js:248,0][static/js/vendor.c2fa7ee7f34202a87f40.js:74852,13]

Build failed with errors.

npm ERR! code ELIFECYCLE

Test performance for big graphs

We should test the performance of linear time guideline and alignment operations for large graphs (e.g. a few hundred nodes and a few hundred edges) to see if the performance is good enough for interactive use on such graphs.

Improving guideline usability

I think the guidelines would be more usable (better user experience) if

  • a node is not aligned (no guidelines are drawn for) with a node which overlaps this one (e.g. two nodes whose bounds are are currently overlapping or a child node with its parent node)
  • a node is not aligned with a node which is too far from it (more than 5 times the ideal edge length or default average node size?)

Support change background

Hello! I want to upgrade your wonderful library so that it allows you to change the background. just pass the background setting (color)(change naming, this color is GridLinesColor):

gridColor: '#dedede', // Color of grid lines

Change as:

gridColor: '#fff', // Color of grid background
gridLinesColor: '#dedede' // Color of grid lines

, and after need add in svg rendering

<rect width="100%" height="100%" fill="url(#horizontalLines)" transform="translate('+ 0 + ', ' + initialValueY + ')" />\n\
<rect width="100%" height="100%" fill="url(#verticalLines)" transform="translate('+ initialValueX + ', ' + 0 + ')" />\n\

following code:

<svg width="'+ canvasWidth + '" height="'+ canvasHeight + '" xmlns="http://www.w3.org/2000/svg">\n\
            <defs>\n\
                <pattern id="horizontalLines" width="' + increment + '" height="' + increment + '" patternUnits="userSpaceOnUse">\n\
                    <path d="M ' + increment + ' 0 L 0 0 0 ' + 0 + '" fill="none" stroke="' + options.gridLinesColor + '" stroke-width="' + options.lineWidth + '" />\n\
                </pattern>\n\
                <pattern id="verticalLines" width="' + increment + '" height="' + increment + '" patternUnits="userSpaceOnUse">\n\
                    <path d="M ' + 0 + ' 0 L 0 0 0 ' + increment + '" fill="none" stroke="' + options.gridLinesColor + '" stroke-width="' + options.lineWidth + '" />\n\
                </pattern>\n\
            </defs>\n\
<rect width="100%" height="100%"  fill="' + options.gridColor + '" />\n\
<rect width="100%" height="100%" fill="url(#horizontalLines)" transform="translate('+ 0 + ', ' + initialValueY + ')" />\n\
<rect width="100%" height="100%" fill="url(#verticalLines)" transform="translate('+ initialValueX + ', ' + 0 + ')" />\n\

This result i showed on attached image:
image

Show only points

How could we make it show only one point at each intersection in the grid?

Uninitialized cy variable

Hi guys,

I'm getting an error, while trying to include this plugin, registered with "Plain JS" method.

The problem is at line 1025 (probably somewhere else as well). 6fe4bfe

Uncaught ReferenceError: cy is not defined

I've added a temporary fix by assigning window.cy to an instance of cytoscape, but I'd say it has to be changed inside the plugin itself, as not everyone is assigning cytoscape to a window object directly.

Thanks

Canvas isn't resized on browser resizing

Hello,

grid-guide creates two additional canvas elements above of the cytoscape.js canvas elements. One of the both elements doesn't have any attributes, the other got an absolute position, top, left and z-index attributes. So far, no problem.
When the browser is resized and due to the layout, several HTML elements wrap, top and left attributes aren't updated like they were on other canvas elements from other cytoscape plugins.
This results in a problematically layout where you may drag and drop the canvas outside of the borders of the desired area. Additionally node's touchpoints are postponed.

Before resizing:
before

After resizing:
after

Guidelines and tolerance

  • When the guidelinesTolerance option is zero (0), I still expect to see the guidelines when there is perfect alignment but I don't!
  • When the tolerance is non-zero (e.g. 2), guidelines should still be drawn horizontal or vertical to show where there would be perfect alignment.

Guidelines to initial mouse location

Initial mouse location (the location where we clicked on the node to be dragged) should also be remembered and taken into consideration during drag. So the user knows how the drag is proceeding w.r.t. initial mouse position.

Snap to guidelines

There should be a way to snap to guidelines (similar to snapping to grid). We should choose the closest guideline for this.

bug with geometric align on drag

When using the following options
geometricGuideline: true,
snapToAlignmentLocationDuringDrag: true,
If I select multiple nodes and move them together, they lose their alignment with one another.

ezgif com-video-to-gif (5)

Issue with cytoscape-edgehandles , wrong handlePositions

Dear Devs,
I am using cytoscape-grid-guide && cytoscape-edgehandles together.
Even though I have set handlePosition: "middle top", in cy.edgehandles but
handles are displayed at wrong position on hover, node selected.

  1. If i disable cy.gridGuide() , cy.edgehandles seem to work fine
  2. Node size and grid spacing seem to effect this behaviour too. Node size width,height 80,80 and grid spacing 100 would yield different handle position
  3. For very first time when i hover on node, handlePosition, appears at node center while i set it as handlePosition: "middle top", in cy.edgehandles

Code Pen Demo:
https://codepen.io/mafar/pen/JjYqvVp?editors=0010

ISSUE (Animated GIF):
See how handlePosition, offsets
bug
**ISSUE , First Hover node , handlePosition, appears at node center **

sss

Guidelines messed up when window resized

To reproduce (on Windows 10 with latest Chrome):

  • Start ChiSE with a full screen window,
  • Then resize the window to be smaller. Now the guidelines seem to be messed up (displayed in incorrect locations).

Compound nodes positioned off grid

With default options, a compound node's children will all nicely align with the grid and the compound node margins will also be modified to nicely align with the grid. See here:

before

And when you drag child nodes of the compound, they keep nicely aligning with the grid. However, when you drag the compound, sometimes, the compound gets positioned off the grid (resulting in children not properly aligning with the grid). See below:

after

Why was Cytoscape.js dependency updated?

I realized that Cytoscape.js dependency was updated to 3.3.0 4d36ff8 As far as I know, we have maintained backward compatibility pretty well and it should work with all versions >=1.6.0

If backward compatibility will be removed, then it should be removed from the source code as well. There are a couple of checks and conditions that we use to provide backward compatibility.

Initialization pattern if used together with cytoscape.js-context-menus

While trying to use grid-guide and context-menus together I ran into the issue that the order of initialization of these extensions is crucial. To be more concrete:

  • First initializing context-menus and then grid-guide results in "TypeError: e is null" in cytoscape.js.
  • Initializing grid-guide first does not throw that error.

I suspect this is not intentional? :-)

How to force a grid resize/rerender

I'm using this library with ReactJS and my page has the cytoscape graph in the mainview, and a sliding panel on the right. When the panel is closed, I would like the grid to re-calculate the size and update. Is there a way to force the grid to resize?

TypeError: cy.gridGuide is not a function

After #33 there is now the above issue on initialization. After minimizing our project it turns out that the following line called in another external library causes the issue:

if (window.jQuery) { jQuery.noConflict(); }

Since other plugins, like edgehandles etc., don't have the above issue I wonder if initialization of all plugins should not follow the same pattern, preferably that of plugins without the above issue?!

However, since the hack in #33 still works well for us, this is more a question of general curiosity for now :-)

Not enough room for guidelines

When there is not enough room for guidelines, it's probably not worth drawing guidelines. See example below (where there isn't enough room for vertical distribution guidelines):
not-enough-room-for-guideline
So, below a certain length (not long enough to accommodate a line with arrows), they shouldn't be drawn at all (would be best to let the user define this as an extension option).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.