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

chartjs-plugin-stacked100

v1.7.0

Published

This plugin for Chart.js that makes your bar chart to 100% stacked bar chart

Downloads

76,506

Readme

chartjs-plugin-stacked100

This plugin for Chart.js that makes your bar chart to 100% stacked bar chart.

Requires Chart.js v3 or higher.

Demo: https://y-takey.github.io/chartjs-plugin-stacked100

Result image

Installation

npm

yarn add chartjs-plugin-stacked100

or

npm install chartjs-plugin-stacked100 --save
import { Chart } from "chart.js";
import ChartjsPluginStacked100 from "chartjs-plugin-stacked100";

Chart.register(ChartjsPluginStacked100);

CDN

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
Chart.register(ChartjsPluginStacked100.default);

Options

| Name | Type | Default | Description | | :------------------ | :---------------------------- | :------ | :------------------------------------------------------------------------------------------------------------------------------------------ | | enable | boolean | false | | | replaceTooltipLabel | boolean | true | replace tooltip label automatically. | | fixNegativeScale | boolean | true | when datasets has negative value and fixNegativeScale is false, the nagative scale is variable. (the range is between -100 and -1) | | individual | boolean | false | scale the highest bar to 100%, and the rest would be proportional to the highest bar of a stack. | | round | "off" | "down" | "up" | "off" | Use "off" to Math.round, "down" to Math.floor, and "up" to Math.ceil. | | precision | number | 1 | precision of percentage. the range is between 0 and 16 | | axisId | string | - | This option allows you to stack only the axis in a chart that includes multiple axes. By default, the plugin will attempt to stack all axes |

Usage

Basic

specify plugin options with { stacked100: { enable: true } }.

Use your tooltip label

specify plugin options with { stacked100: { enable: true, replaceTooltipLabel: false } }. and you can pass tooltip option.

new Chart(document.getElementById("my-chart"), {
  type: "bar",
  data: {},
  options: {
    tooltips: {
      callbacks: {
        label: (tooltipItem) => {
          const data = tooltipItem.chart.data;
          const datasetIndex = tooltipItem.datasetIndex;
          const index = tooltipItem.dataIndex;
          const datasetLabel = data.datasets[datasetIndex].label || "";
          // You can use two type values.
          // `data.originalData` is raw values,
          // `data.calculatedData` is percentage values, e.g. 20.5 (The total value is 100.0)
          const originalValue = data.originalData[datasetIndex][index];
          const rateValue = data.calculatedData[datasetIndex][index];

          return `${datasetLabel}: ${rateValue}% (raw ${originalValue})`;
        },
      },
    },
    plugins: {
      stacked100: { enable: true, replaceTooltipLabel: false },
    },
  },
});

Specify the precision of the percentage value

You can pass precision option. (default value is 1 ) For example, when you pass { stacked100: { enable: true, precision: 3 } }, the result is 0.123% .

Examples

new Chart(document.getElementById("my-chart"), {
  type: "bar",
  data: {
    labels: ["Foo", "Bar"],
    datasets: [
      { label: "bad", data: [5, 25], backgroundColor: "rgba(244, 143, 177, 0.6)" },
      { label: "better", data: [15, 10], backgroundColor: "rgba(255, 235, 59, 0.6)" },
      { label: "good", data: [10, 8], backgroundColor: "rgba(100, 181, 246, 0.6)" },
    ],
  },
  options: {
    indexAxis: "y",
    plugins: {
      stacked100: { enable: true },
    },
  },
});

Datapoints can be Objects

...
// line or bar charts
datasets: [
  { data: [{ y: 5 }, { y: 25 }] },
  { data: [{ y: 15 }, { y: 10 }] },
  { data: [{ y: 10 }, { y: 8 }] }
]

// horizontalBar charts
datasets: [
  { data: [{ x: 5 }, { x: 25 }] },
  { data: [{ x: 15 }, { x: 10 }] },
  { data: [{ x: 10 }, { x: 8 }] }
]
...
How to use datalabels plugin
new Chart(document.getElementById("my-chart"), {
  type: "bar",
  data: {},
  options: {
    plugins: {
      datalabels: {
        formatter: (_value, context) => {
          const data = context.chart.data;
          const { datasetIndex, dataIndex } = context;
          return `${data.calculatedData[datasetIndex][dataIndex]}% (${data.originalData[datasetIndex][dataIndex]})`;
        },
      },
      stacked100: { enable: true },
    },
  },
});

Use with React

npx create-react-app demo-react
cd demo-react
npm install react-chartjs-2 chartjs-plugin-stacked100 --save

Then use it:

import { Chart, Bar } from "react-chartjs-2";
import ChartjsPluginStacked100 from "chartjs-plugin-stacked100";

Chart.register(ChartjsPluginStacked100);

const ChartData = (props: any) => {
  return (
    <>
      {
        <div>
          <Bar
            data={{
              labels: ["Foo", "Bar"],
              datasets: [
                { label: "bad", data: [5, 25], backgroundColor: "rgba(244, 143, 177, 0.6)" },
                { label: "better", data: [15, 10], backgroundColor: "rgba(255, 235, 59, 0.6)" },
                { label: "good", data: [10, 8], backgroundColor: "rgba(100, 181, 246, 0.6)" },
              ],
            }}
            options={{
              //@ts-ignore
              indexAxis: "y",
              plugins: {
                stacked100: { enable: true },
              },
            }}
          />
        </div>
      }
    </>
  );
};
export default ChartData;

You can find a working example in the demo-react folder

Supported chart types

Contributing

Pull requests and issues are always welcome.

For bugs and feature requests, please create an issue.

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request!

Development

  • install: yarn install
  • start dev server: yarn start
  • unit test: yarn test:watch or yarn test
  • publish the plugin: npm version (major|minor|patch) && npm publish
  • check: yarn dev
  • deploy to github pages: yarn gh