npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-cytoscape

v0.5.25

Published

NGX-Cytoscape is an Angular 5+ wrapper around the CytoscapeJS 3 module based on original work from Michael Knoch.

Downloads

208

Readme

ngx-cytoscape

NGX-Cytoscape is an Angular 5+ wrapper around the CytoscapeJS 3 module based on original work from Michael Knoch.

This library enables you to add Cytoscape-based graph visualizations into your Angular application, with values supplied by instance variables.

This fork adds support for the Angular 5 library formats using RollupJS, supporting UMD and CommonJS formats.

http://js.cytoscape.org

Originally forked from Michael Knoch's repo for Angular 2:

https://github.com/michaelknoch/ng2-cytoscape

Updates also provided to this repo from Nick Bradford - if this fork is out of date check out his:

https://github.com/nmbradford/ngx-cytoscape

Installation

Assuming you have a package.json defining your project (possibly generated from @angular/cli's "ng new" or "npm init"), you can use the regular "npm install" approach to get the current version. For now, you'll also have to manually add CytoscapeJS to your project as well:

$ npm install --save ngx-cytoscape cytoscape

Tracking changes

If you want to track the most recent updates then specifiy the "latest" version

$ npm install --save ngx-cytoscape@latest cytoscape

The project may be mildly fluid, so use extensive Unit/E2E tests if you do and be prepared to rollback with "git" or your version control software if tests fail).

The workflow outline of tracking changes might look something like this:

$ git add . ; git commit -m "Pre-update check-in"
$ ng test   # Store test results for future comparison
$ npm update ngx-cytoscape cytoscape
$ ng test   # Now compare these tests results to the previous
$ git add . ; git commit -m "Passed tests after update" # If the tests looked good
$ git revert # If the tests were worse off and you don't want to update your code at this point

Of course, CI systems like Jenkins or Travis can automate this for you.

Regardless of whether you're tracking changes or locking in a particular version, verify in your package.json that you have lines that look like this in your "dependencies" section (either "latest" or something like "^0.5.20" for ngx-cytoscape depending on which you chose):

"ngx-cytoscape": "latest",
    "cytoscape": "^3.2.8",

For now, you'll also have to add the underlying CytoscapeJS library to your .angular-cli.json. Note we've also assumed that you have the full version of jQuery already installed (Bootstrap, Cytoscape, and other modules need the full version - Angular only supplies the "lite" version as the fallback if you don't supply your own full jQuery).

(Stop and restart any "ng serve" or "ng build --watch" when you make changes to the .angular-cli.json file):

      "scripts": [
        "../node_modules/jquery/dist/jquery.js",
        "../node_modules/cytoscape/dist/cytoscape.js",

and then in your Angular AppModule, import for both TypeScript and Angular:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';

// Import your library
import { CytoscapeModule } from 'ngx-cytoscape'; // <= Add this TS import

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    CytoscapeModule // <= Add this NG import
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

From there, you can invoke

Example Usage

In the component you want to use Cytoscape in, provide an instance property with nodes and edges, then pass that to the [elements] @Input of the tag in your HTML. Here's a condensed version.

import {Component} from '@angular/core';

@Component({
    selector: 'graph',
    template: '<ngx-cytoscape [elements]="graphData"></ngx-cytoscape>',
    styles: [`
        ngx-cytoscape {
          height: 50vh;
          float: left;
          position: relative;
          border: solid #21c0c0;
          width: 50em;
        }
    }`],
})
export class Graph {

    graphData = {
        nodes: [
            {data: {id: 'j', name: 'Jerry', faveColor: '#6FB1FC', faveShape: 'triangle'}},
            {data: {id: 'e', name: 'Elaine', faveColor: '#EDA1ED', faveShape: 'ellipse'}},
            {data: {id: 'k', name: 'Kramer', faveColor: '#86B342', faveShape: 'octagon'}},
            {data: {id: 'g', name: 'George', faveColor: '#F5A45D', faveShape: 'rectangle'}}
        ],
        edges: [
            {data: {source: 'j', target: 'e', faveColor: '#6FB1FC'}},
            {data: {source: 'j', target: 'k', faveColor: '#6FB1FC'}},
            {data: {source: 'j', target: 'g', faveColor: '#6FB1FC'}},

            {data: {source: 'e', target: 'j', faveColor: '#EDA1ED'}},
            {data: {source: 'e', target: 'k', faveColor: '#EDA1ED'}},

            {data: {source: 'k', target: 'j', faveColor: '#86B342'}},
            {data: {source: 'k', target: 'e', faveColor: '#86B342'}},
            {data: {source: 'k', target: 'g', faveColor: '#86B342'}},

            {data: {source: 'g', target: 'j', faveColor: '#F5A45D'}}
        ]
    };

    constructor() {
    }

}

There's a fuller version in the "cytodemo" folder of the GitHub repo.

Future work will include more support for 3rd party Cytoscape plugins. Currently, to use a plugin like canvas, you'll have to get access to the module's exposed "cy" object:

import { Component, OnInit, ViewChild } from '@angular/core';

// ...

export class MyComponent implements OnInit, ngAfterViewInit {

  // Get reference to the native element cytoscape instance 
  @ViewChild('cytoscape') cytoscape: any;

  // ...

  ngAfterViewInit() {
      if (this.cytograph.cy) {
        const cyLayer = this.cytograph.cy.cyCanvas();
        const cnv: HTMLCanvasElement = cyLayer.getCanvas();
        const ctx: CanvasRenderingContext2D = cnv.getContext("2d");
        // ...
          this.cytograph.cy.on("render cyCanvas.resize", function(evt, src) {
              // "this" is now "cy" inside this callback function
              cyLayer.resetTransform(ctx);
              cyLayer.clear(ctx);
              ctx.fillStyle = "#ff00ff";
              //ctx.fillRect(0, 0, 100, 100); // Top left corner
              cyLayer.setTransform(ctx);

              const width = cnv.width;
              const height = cnv.height;
              const data = Array(width * height);

              // Draw model elements
              this.nodes().forEach(function(node) {
                  const pos = node.position();
                  // Do something with canvas at or around the node's position
                  ctx.fillRect(pos.x - 25, pos.y - 25, 50, 50); // At node position (bisection point of 50x50 rectangle)
              });
          });
      }
      // ...
  }

Of course, you'll also have to have added it to the package.json:

$ npm install cytoscape-

Your package.json should have at least these entries in this case:

    "ngx-cytoscape": "latest",
    "cytoscape": "^3.2.8",
    "cytoscape-canvas": "^3.0.1",

As well as to the .angular-cli.json (stop and restart any "ng serve" or "ng build --watch" when you make changes):

      "scripts": [
        "../node_modules/jquery/dist/jquery.js",
        "../node_modules/cytoscape/dist/cytoscape.js",
        "../node_modules/cytoscape-canvas/dist/cytoscape-canvas.js",

Development

You'll probably want to start by cloning the repository:

$ git clone https://github.com/calvinvette/ngx-cytoscape.git

CD into the ngx-cytoscape directory.

To generate all *.js, *.d.ts and *.metadata.json files:

$ npm run build

The actual module distribution is in the "dist/" folder. If you want to develop locally, CD into that folder and do an "npm link"

$ cd dist
$ npm link

This uses either a symbolic link (Unix) or Junction (Windows) to point to the dist folder in your local npm module cache. Any changes you make in the project and rebuild locally will be immediately available in local npm cache. (Just don't forget to re-run the "npm run build" command after making local changes.)

Then in the project you want to use the module in (another project created with @angular/cli's "ng new" or "npm init"), invoke link again with the module name (another symlink/junction to the local npm cache, but this time as a read-only reference):

$ cd ~/workspace/my-angular-project
$ npm link ngx-cytoscape

If you have anything you'd like to contribute or improve, please feel free to make a "pull request".

License

MIT ©2018 [Calvin Vette]