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

v0.3.1

Published

Automatic color generation for Chart.js

Downloads

24,536

Readme

chartjs-plugin-autocolors

Automatic color generation for Chart.js

GitHub Workflow Status

The generation is based on Janus Troelsen's answer at Stack Overflow.

This plugin requires Chart.js 3.0.0 or later. Could work with v2, but it is not supported.

NOTE the plugin does not automatically register.

Example

Example chart

Installation

NPM:

npm i --save chartjs-plugin-autocolors

CDN:

<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-autocolors"></script>

Usage

loading

ESM

import autocolors from 'chartjs-plugin-autocolors';

CDN

const autocolors = window['chartjs-plugin-autocolors'];

Registering

All charts

Chart.register(autocolors);

Single chart

const chart = new Chart(ctx, {
  // ...
  plugins: [
    autocolors
  ]
});

Disabling (when registered for all charts)

If registered globally, it might be desirable to disable the autocolors for some charts

const chart = new Chart(ctx, {
  // ...
  options: {
    plugins: {
      autocolors: {
        enabled: false
      }
    }
  }
});

Mode

NOTE: 'dataset' mode does not work properly for Pie / Doughnut charts, so using 'data' mode with those charts is advised.

There are two modes, 'dataset' (default) 'data' and 'label'

  • In 'dataset' mode, a new color is picked for each dataset.
  • In 'data' mode, an array of colors, equivalent to the length of data is provided for each dataset.
  • In 'label' mode, color is picked for each different dataset label.
const chart = new Chart(ctx, {
  // ...
  options: {
    plugins: {
      autocolors: {
        mode: 'data'
      }
    }
  }
});

Customize

A customize function can be provided to customize the generated colors. The function is expected to return object containing background and border properties, with values acceptable as colors by Chart.js.

const lighten = (color, value) => Chart.helpers.color(color).lighten(value).rgbString();

const chart = new Chart(ctx, {
  // ...
  options: {
    plugins: {
      autocolors: {
        customize(context) {
          const colors = context.colors;
          return {
            background: lighten(colors.background, 0.5),
            border: lighten(colors.border, 0.5)
          };
        }
      }
    }
  }
});

Offset

offset option can be used to offset the color generation by a number of colors.

const chart = new Chart(ctx, {
  // ...
  options: {
    plugins: {
      autocolors: {
        offset: 5
      }
    }
  }
});

Repeat

Sometimes you might need to color multiple adjacent datasets with same color. The repeat option is for that use case.

const chart = new Chart(ctx, {
  // ...
  options: {
    plugins: {
      autocolors: {
        repeat: 2
      }
    }
  }
});

Browser compatibility

This plugin uses a generator function, so it is not compatible with Internet Explorer.

License

chartjs-plugin-autocolors.js is available under the MIT license.