Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chartjs-plugin-autocolors

Package Overview
Dependencies
Maintainers
0
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chartjs-plugin-autocolors

Automatic color generation for Chart.js

  • 0.3.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.8K
increased by6.3%
Maintainers
0
Weekly downloads
 
Created
Source

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.

Keywords

FAQs

Package last updated on 11 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc