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

neustar-venn

v0.0.1

Published

venn.js =======

Downloads

1

Readme

venn.js

A javascript library for laying out area proportional venn and euler diagrams.

Details of how this library works can be found on the blog post I wrote about this. There are also more examples on that page.

Usage

This library depends on d3.js to display the venn diagrams.

Simple layout

To lay out a simple diagram, just define the sets and their sizes along with the sizes of all the set intersection. Calling 'venn.venn' will position the sets such that the areas of each region are proportional to the sizes, and 'venn.drawD3Diagram' will display this diagram:

// define sets and set set intersections
var sets = [{label: "A", size: 10}, {label: "B", size: 10}],
    overlaps = [{sets: [0,1], size: 2}];

// get positions for each set
sets = venn.venn(sets, overlaps);

// draw the diagram in the 'simple_example' div
venn.drawD3Diagram(d3.select(".simple_example"), sets, 300, 300);

View this example

Changing the Style

To change the style of the venn diagram, use D3 to set attributes on the 'text' and 'circle' objects that are returned from the drawD3Diagram call:

var diagram = venn.drawD3Diagram(d3.select("#rings"),
                                 venn.venn(sets, overlaps), 
                                 500, 500);

// change the colours, add a thick border, remove fill
var colours = ['black', 'red', 'blue', 'green']
diagram.circles.style("fill-opacity", 0)
               .style("stroke-width", 10)
               .style("stroke-opacity", .5)
                .style("stroke", function(d,i) { return colours[i]; });

// make the font big and light
diagram.text.style("fill", function(d,i) { return colours[i]})
            .style("font-size", "24px")
            .style("font-weight", "100");

View this example, along with other possible styles

Dynamic layout

To have a layout that reacts to a change in input, you just need to recompute the areas and call updateD3Diagram to do the transition:

// draw the initial set
var sets = venn.venn(getSets(), getSetIntersections());
var diagram = venn.drawD3Diagram(d3.select(".dynamic"), sets, w, h);

// redraw the sets on any change in input
d3.selectAll("input").on("change", function() {
    var sets = venn.venn(getSets(), getSetIntersections());
    venn.updateD3Diagram(diagram, sets);
});

View this example

Making the diagram interactive

Making the diagram interactive is basically the same idea as changing the style: just add event listeners to the returned elements. To change the text size and circle colours on mouseover:

diagram.nodes
    .on("mouseover", function(d, i) {
        var node = d3.select(this).transition();
        node.select("circle").style("fill-opacity", .1);
        node.select("text").style("font-size", "36px");
    })
    .on("mouseout", function(d, i) {
        var node = d3.select(this).transition();
        node.select("circle").style("fill-opacity", 0);
        node.select("text").style("font-size", "24px");
    });

View this example

Adding tooltips to the intersection areas

The intersection areas aren't drawn by default, but there is some code included here to draw a svg path element around the intersection areas. To add a tooltip to the intersection area, use the 'intersectionAreaPath' method to define the area, and then add appropiate events to handle the tooltip:

diagram.svg.select("g").selectAll("path")
    .data(overlaps)
    .enter()
    .append("path")
    .attr("d", function(d) { 
        return venn.intersectionAreaPath(d.sets.map(function(j) { return sets[j]; })); 
    })
    .style("fill-opacity","0")
    .style("fill", "black")
    .style("stroke-opacity", 0)
    .style("stroke", "white")
    .style("stroke-width", "2")
    .on("mouseover", function(d, i) {
        d3.select(this).transition()
            .style("fill-opacity", .1)
            .style("stroke-opacity", 1);
        tooltip.transition().style("opacity", .9);
        tooltip.text(d.size + " users");
    })
    .on("mouseout", function(d, i) {
        d3.select(this).transition()
            .style("fill-opacity", 0)
            .style("stroke-opacity", 0);
        tooltip.transition().style("opacity", 0);
    })
    .on("mousemove", function() {
        tooltip.style("left", (d3.event.pageX) + "px")
               .style("top", (d3.event.pageY - 28) + "px");
    })

View this example

MDS Layout

In most cases the greedy initial layout does a good job of positioning the sets, but there are cases where it breaks down. One case is detailed in this blog post, and it can be better laid out using multidimensional scaling to generate the initial layout.

To enable this just include the mds.js and numeric.js libraries first, and then generate the venn positions by calling:

sets = venn.venn(sets, overlaps, {layoutFunction: venn.classicMDSLayout});

View this example

Released under the MIT License.