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

d3-tribin

v1.0.6

Published

Group two-dimensional points into triangular bins.

Downloads

11

Readme

d3-tribin

This D3 plugin is largely inspired by the d3-hexbin plugin but instead of using hexagons to bin points it uses triangles. It is therefore used to group two-dimensional points into (equilateral) triangular bins. It supports color encoding, area encoding, or both.

Installing

If you use NPM, npm install d3-tribin. Otherwise, download the latest release.

API Reference

# tribin()

Constructs a new default tribin generator.

# tribin(points)

Bins the specified array of points, returning an array of triangular bins. For each point in the specified points array, the x- and y-accessors are invoked to compute the x- and y-coordinates of the point, which is then used to assign the point to a triangular bin. If either the x- or y-coordinate is NaN, the point is ignored and will not be in any of the returned bins.

Each bin in the returned array is an array containing the bin’s points. Only non-empty bins are returned; empty bins without points are not included in the returned array. Each bin has these additional properties:

  • x - the x-coordinate of the center of the associated bin’s triangle
  • y - the y-coordinate of the center of the associated bin’s triangle
  • rotation - the rotation (in gradients) with respect to the vertical axe of the associated bin’s triangle

The x- and y-coordinates of the triangle center and its rotation can be used to render the triangle at the appropriate location in conjunction with tribin.triangle or tribin.triangleFromBin or . For example, given a tribin generator:

var tribin = d3.tribin();

You could display a triangle for each non-empty bin as follows:

svg.selectAll("path")
  .data(tribin(points))
  .enter().append("path")
    .attr("d", function(d) { return tribin.triangleFromBin(d); });

Which is equivalent to:

svg.selectAll("path")
  .data(tribin(points))
  .enter().append("path")
    .attr("d", function(d) { return tribin.triangle(1, d.rotation, [d.x, d.y]); });

And also equivalent to:

svg.selectAll("path")
  .data(tribin(points))
  .enter().append("path")
    .attr("d", function(d) { return "M" + d.x + "," + d.y + tribin.triangle(1, d.rotation); });

# tribin.triangleFromBin(bin, [side])

Returns the SVG path string for the (equilateral) triangle with default size. The center and the rotation of the triangle are automatically set by using the bin properties x, y and rotation. If side is not specified, the tribin’s current side is used. If side is specified, a triangle with the specified side is returned, this is useful for area-encoded bivariate tribins.

# tribin.triangle([side], [rotation], [center])

Returns the SVG path string for the (equilateral) triangle with default size, zero rotation and centered at the origin ⟨0,0⟩. The path string is defined with relative coordinates such that you can easily translate the triangle to the desired position. If side is not specified, the tribin’s current side is used. If side is specified, a triangle with the specified side is returned. If rotation is not specified, zero is used. If rotation is specified, a triangle with the specified rotation is returned. The rotation is applied clockwise and specified in radians (e.g. 180 degrees correspond to Math.PI rad). If center is not specified, the triangle is centered at ⟨0,0⟩. If center is specified, the triangle will be centered at ⟨center[0],center[1]⟩.

# tribin.x([x])

If x is specified, sets the x-coordinate accessor to the specified function and returns this tribin generator. If x is not specified, returns the current x-coordinate accessor, which defaults to:

function x(d) {
  return d[0];
}

The x-coordinate accessor is used by tribin to compute the x-coordinate of each point. The default value assumes each point is specified as a two-element array of numbers [x, y].

# tribin.y([y])

If y is specified, sets the y-coordinate accessor to the specified function and returns this tribin generator. If y is not specified, returns the current y-coordinate accessor, which defaults to:

function y(d) {
  return d[1];
}

The y-coordinate accessor is used by tribin to compute the y-coordinate of each point. The default value assumes each point is specified as a two-element array of numbers [x, y].

# tribin.side([side])

If side is specified, sets the side of the triangle to the specified number. If side is not specified, returns the current side, which defaults to 1.