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-transform

v1.0.5

Published

addon to d3.js that wraps SVG transforms into an API

Downloads

4,213

Readme

D3-Transform

d3-transform makes it easy to define and reuse functions that produce transform attribute strings for SVG elements. Using d3-transform reduces repetition, allows you to compose multiple transforms, and eliminates ugly string-interpolation from your d3 visualization code.

Installation

Include d3-transform in your web page using a script tag any time after you've included d3:

<script src="http://d3js.org/d3.v4.min.js"></script>
<script src="d3-transform.js"></script>

Usage

d3-transform replaces the manual construction of transform attribute strings for SVG elements. For example, if you want to translate, rotate, and scale a group element depending on the data bound to that element, you'd write something like this without d3-transform:

d3.select('svg').selectAll('g')
    .data([{ size: 5 }, { size: 10 }])
  .enter().append('g')
    .attr('transform', function(d, i) {
      return "translate(20," + d.size * 10 + ") rotate (40) scale(" + ( d.size + 2 ) + ")");
    });

With d3-transform, you can rewrite the above code like this:

var transform = d3.transform()
    .translate(function(d) { return [20, d.size * 10] })
    .rotate(40)
    .scale(function(d) { return d.size + 2 });

var svg = d3.select('svg').selectAll('g')
    .data([{ size: 5 }, { size: 10 }])
    .enter()
    .append('g')
    .attr('transform', transform);

In both cases the resulting document will look the same:

<svg>
  <g transform="translate(20,50) rotate(40) scale(7)"></g>
  <g transform="translate(20,100) rotate(40) scale(12)"></g>
</svg>

You can specify arguments for these operations by either providing positional arguments to the corresponding method of the transform object, or by providing a function that will return an array of arguments that are interpreted as positional arguments. In the special case where an operation only takes one argument, your function can return a number.

All of the SVG 1.1 transform operations are supported: matrix, rotate, translate, scale, skewX, and skewY. See the SVG 1.1 Specification or MDN for further details on the arguments of each operation.

When using node.js, calls to require('d3-transform') return a direct reference to the "transform" method.

var d3 = require('d3');
var d3Transform = require('d3-transform');

var transform = d3Transform()
    .translate(function(d) { return [20, d.size * 10] })
    .rotate(40)
    .scale(function(d) { return d.size + 2 });

var svg = d3.select('svg.example1').selectAll('g')
    .data([{ size: 5 }, { size: 10 }])
    .enter()
    .append('g')
    .attr('transform', transform);

Composition

If you want to extend one transform with another set of operations, pass the initial transform object into the d3.transform() function:

var transform1 = d3.transform()
  .translate(10,20);

var transform2 = d3.transform(transform1)
  .scale(function(d) { return [d.size];})

d3.select('svg.example2').selectAll('g')
    .data([{ size: 5 }, { size: 10 }])
  .enter().append('g')
    .attr('transform', transform2);

Another way to compose multiple transform objects is to use the seq operation:

var transform1 = d3.transform()
  .translate(10,20);

var transform2 = d3.transform()
  .scale(function(d) { return [d.size];})

var transform =
  transform1.seq(transform2);

With either of these approaches, the result is a document that looks like this:

<svg>
  <g transform="translate(10,20) scale(5)"></g>
  <g transform="translate(10,20) scale(10)"></g>
</svg>

Contributors