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

easy-stacked-bar

v0.0.3

Published

A simple package for generating D3 horizontal stacked barplots

Downloads

3

Readme

easy-stacked-bar

A simple package for generating D3 horizontal stacked barplots

Alt text

Table of Contents

Installation

Install the latest release from npm

npm install easy-stacked-bar

Quick Start

import * as d3 from "d3";
import { stackedBarHorizontal, count, dputJS } from "easy-stacked-bar";

const data = [
    {
      gene: "BRCA2",
      missense: 5,
      nonsense: 0,
      multiple: 0,
    },
    {
      gene: "BRCA1",
      missense: 2,
      nonsense: 2,
      multiple: 1,
    },
    {
      gene: "RAD51",
      missense: 8,
      nonsense: 0,
      multiple: 0,
    },
    {
      gene: "TP53",
      missense: 9,
      nonsense: 0,
      multiple: 0,
    }
  ];


const chart = stackedBarHorizontal()
    .data(data) // Set the data
    .positionTopLeft([50, 50]) // Set the top left position
    .positionBottomRight([800, 700]) // Set the bottom right position
   //.yScale(yScale) // Optionally use a pre-computed yScale

// Create SVG
const svg = d3
   .select("body")
   .append("svg")
   .attr("width", window.innerWidth)
   .attr("height", window.innerHeight)

// Create Chart
const stackedBarChart = chart(svg) // Render the chart

Counting Long Data

Sometimes your data is not summarised in the wide 'count' format stackedBarChart() expects. If your data is in a long-form, use count() to produce the required wide-count format.

How count works is easiest to understand using an example:

// Input Data
const longData = [
   { x: "Patient1", gene: "BRCA1", type: "missense" },
   { x: "Patient2", gene: "BRCA1", type: "missense" },
   { x: "Patient3", gene: "BRCA1", type: "nonsense" },
   { x: "Patient4", gene: "BRCA1", type: "nonsense" },
   { x: "Patient5", gene: "TP53", type: "missense" },
   { x: "Patient6", gene: "TP53", type: "missense" },
   { x: "Patient6", gene: "TP53", type: "nonsense" }
 ];
 const dataCounts = count(longData, 'gene', 'type');

 console.log(dataCounts);

 // Output:
 // [
 //   {"gene": "BRCA1", "missense": 2, "nonsense": 2 },
 //   {"gene": "TP53",  "missense": 2, "nonsense": 1 }
 // ]

Key Methods

stackedBarHorizontal

The stackedBarHorizontal function is used to create a stacked horizontal bar chart. It allows you to customize various aspects of the chart's appearance and behavior.

  • data(data: Array<Object>)

    • Sets the data for the chart. Expects an array of objects where each object represents a data point with categories and subcategories.
  • pixelGapBetweenStacks(pixelGap: number)

    • Sets the gap between stacked bars in pixels.
  • positionTopLeft(position: [number, number])

    • Sets the top left position of the chart relative to the top of the y-axis line.
  • positionBottomRight(position: [number, number])

    • Sets the bottom right position of the chart relative to the right of the x-axis line.
  • yScale(scale: d3.scaleBand)

    • Sets a pre-computed yScale for the chart. Allows you to provide a custom y-axis scale.
  • hideAxisX()

    • Hides the x-axis of the chart.
  • showAxisX()

    • Shows the x-axis of the chart.
  • hideAxisY()

    • Hides the y-axis of the chart.
  • showAxisY()

    • Shows the y-axis of the chart.
  • yTickSize(tickSize: number)

    • Sets the tick size for the y-axis.
  • yTickSizeOuter(tickSizeOuter: number)

    • Sets the outer tick size for the y-axis.
  • yTickPadding(tickPadding: number)

    • Sets the tick padding for the y-axis.
  • fontSizeX(fontSize: number)

    • Sets the font size for the x-axis labels.
  • fontSizeY(fontSize: number)

    • Sets the font size for the y-axis labels.
  • cornerRadius(radius: number)

    • Sets the corner radius for the stacked bars.
  • mouseOverFunction(callback: function)

    • Sets the function to execute on mouseover event.
  • mouseMoveFunction(callback: function)

    • Sets the function to execute on mousemove event.
  • mouseLeaveFunction(callback: function)

    • Sets the function to execute on mouseleave event.
  • rectSecondaryClass(secondaryClass: string)

    • Sets a secondary class of the rect svg elements composing the stacked bar chart