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

force-horse

v1.0.12

Published

A web component wrapper for d3.js force layout

Downloads

3

Readme

force-horse

Usage

Installing

npm install --save force-horse

Loading:

With a modules bundler (recommended):

import 'force-horse'; // Imports the web component, not compiled
// OR
import 'force-horse/dist/main.bundle.js'; // Compiled. Less recommended because can't dedupe
@import 'force-horse/src/index.scss'; // Imports the SCSS index file, not compiled
// OR
@import 'force-horse/dist/style.bundle.css'; // Imports the bundle as CSS, compiled

Directly from HTML:

<link rel="stylesheet" type="text/css" href="node_modules/force-horse/dist/style.bundle.css" />
<script type="application/javascript" src="node_modules/force-horse/dist/main.bundle.js"></script>

Using:

From the template

<!-- Web component -->
<force-horse data='{"nodes": [], "links": []}' config='{"anything": true}'></force-horse>
<!-- Angular: Note that you have to add CUSTOM_ELEMENTS_SCHEMA -->
<force-horse [attr.data]="data | json" [attr.config]="config | json"></force-horse>

From Javascript:

const forceHorse = document.createElement("force-horse");
forceHorse.setConfig(someConfig);
forceHorse.setData(someData);
someContainer.appendChild(forceHorse);

To be rendered, force-horse must first get data, which can be passed as an attribute, or directly using setData

A force-horse element does not have an intrinsic size. It adapts itself to the size that is set by its parent. force-horse is implementing CSS flex-box display logic.

Attributes

Data

The graph's data (nodes and links):

  {
	"nodes": [{
	  "id": "string",
	  "label": "string",
	  "svg": "string|optional"
	}],
	"links": [{
	  "sourceId": "string",
	  "targetId": "string"
	}]
  }

Config

Configure options in the component. Defaults to:

{
  "showButtons": true,
  "showLabels": true,
  "numOfLabelsToShow": 10,
  "showNodeWeight": true,
  "showEdgeWeight": true,
  "hideOrphanNodes": false,
  "showFilterButton": true,
  "showLabelsButton": true,
  "showNodeWeightButton": true,
  "showEdgeWeightButton": true,
  "showOrphanNodesButton": true,
  "forceParameters": {
    "friction": 0.5,
    "charge": -100,
    "linkStrength": 1,
    "gravity": 0.3,
    "linkDistance": 10
  }
}

API

You can use the API by querying the force-horse element, and accessing viewer.

// In JS
const viewer = document.qeurySelector("force-horse").viewer;

// In Angular
// Template: `<force-horse #fh></force-horse>`
@ViewChild('fh') forceHorse: ElementRef;
ngAfterViewInit() {
  const viewer = this.forceHorse.viewer;
}

Outputs

Outputs are event emitters. You can subscribe and unsubscribe in the following way:

const subscription = viewer.hoverEvent.subscribe(() => { /* some callback */);
subscription.unsubscribe();

These are the current events. PR's and suggestions for more are welcome: ready (readyEvent): an SVG is drawn and ready

hover (hoverEvent): a node/link is hovered upon

select (selectEvent): a node/link is selected (selectedItems: Array, isMultiple: boolean, currentSelected, currentData)

dblclick (doubleClickEvent): a node/link is double-clicked upon

filter (filterEvent): remove the selected nodes/links from the graph

The forceHorse project contains a complete demo application. You can see it in action here

VISUAL INTERFACE

On activation, force-horse shows the given graph. The graph stabilizes after a short force simulation. Then, if needed, it zooms out, so that the whole graph can be seen at a glance.

Beside showing the graph itself, force-horse provides visual indication of hovering over a node/link, and of selecting a node/link. It supports node dragging, and also pan and zoom.

In addition, force-horse is providing the following buttons:

  • filter: remove selected nodes/links from the graph
  • pause/play: allows to “freeze” the graph, to locate nodes without subsequent changes by the force-layout simulation
  • home: automatically pan/zoom so that the whole graph can be seen at a glance
  • labels: show/hide node labels
  • node weight: show/hide node weight (weightier nodes will be larger)
  • link weight: show/hide link weight (weightier links will be thicker)

DEPENDENCIES

The only dependency of force-horse is:

  • d3.js v4

PERFORMANCE

force-horse was designed for high performance. Some of the measures in use are:

  • minimal use of DOM manipulations
  • large graphs are not rendered continuously, but only a few times during the force simulation. The screen rendering is controlled programmatically, implementing javascriptrequestAnimationFrame(). A thin progress bar is displayed, during a force simulation, so that even if rendering is held, the user can know the stage and the pace of the current force simulation.

MULTIPLE LINKS BETWEEN TWO NODES

The ability to compute multiple, parallel link between two nodes is not currently supported intrinsically by d3.js. It was also not yet developed by the users community. Therefore, we developed this ability especially for force-horse. For the mathematical and implementation details, see here.

CONSOLE MESSAGES

Set localStorage.setItem('force-horse', true) to get informative console messages from force-horse. To turn it off, remove that item by doing localStorage.removeItem('force-horse')