chartjs-plugin-colorschemes

repository·master·Indexed 43 days ago

https://github.com/nagix/chartjs-plugin-colorschemes

A plugin for Chart.js 2.5.0 or later that provides predefined professional color schemes based on ColorBrewer, Microsoft Office, and Tableau. It allows developers to apply palettes via the plugins.colorschemes.scheme option, create custom schemes from scratch, or extend existing ones using a custom function.

Tokens
2.1K
Snippets
9
Records
10
Agent score
5%

What's inside chartjs-plugin-colorschemes

  1. Override existing color settings

    master

    By default, the plugin does not apply a color scheme if a dataset already has color options specified. If you are using libraries like ng-charts that automatically apply default colors, set override: true to force the plugin's scheme to take precedence.

    options: {
        plugins: {
            colorschemes: {
                scheme: 'brewer.Paired12',
                override: true
            }
        }
    }
  2. Install chartjs-plugin-colorschemes

    master

    You can install the plugin via npm, bower, or use a CDN. This plugin requires Chart.js 2.5.0 or later.

    npm install chartjs-plugin-colorschemes --save
    bower install chartjs-plugin-colorschemes --save
    <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-colorschemes"></script>
    <script src="https://unpkg.com/chartjs-plugin-colorschemes"></script>
  3. Use chartjs-plugin-colorschemes with ES6 modules

    master

    You can import the entire plugin to use all schemes:

    import 'chartjs-plugin-colorschemes';

    To reduce bundle size, you can import only the plugin core and specific color schemes individually:

    // import the plugin core
    import 'chartjs-plugin-colorschemes/src/plugins/plugin.colorschemes';
    
    // import a particular color scheme
    import { Aspect6 } from 'chartjs-plugin-colorschemes/src/colorschemes.office';
    
    // ...
        options: {
            plugins: {
                colorschemes: {
                    scheme: Aspect6
                }
            }
        }
  4. Configure colorscheme plugin options

    master

    Plugin options can be set per chart via options.plugins.colorschemes.* or globally via Chart.defaults.global.plugins.colorschemes.*.

    | Name | Type | Default | Description |
    | ---- | ---- | ------- | ----------- |
    | `fillAlpha` | `number` | `0.5` | The transparency value for the line fill color. Must be a number between `0.0` (fully transparent) and `1.0` (no transparency). |
    | `scheme` | `string`\|`string[]` | `'brewer.Paired12'` | Color scheme name that is any of [Color Chart](https://nagix.github.io/chartjs-plugin-colorschemes/colorchart.html). It also accepts an array of color strings, which is primarily for ES modules. |
    | `reverse` | `boolean` | `false` | If set to `true`, the order of the colors in the selected `scheme` is reversed. |
    | `override` | `boolean` | `false` | If set to `true`, the specified color scheme will override the existing color options. If `false`, it is only applied when no color setting exists. |
    | `custom` | `function` | `undefined` | A function that takes a copy of the color string array for `scheme` in order to extend the predefined scheme colors. |
  5. Install and register the chartjs-plugin-colorschemes plugin

    master

    To use the plugin, import the default export and register it with Chart.js. The plugin attaches a colorschemes object to the Chart constructor, which contains the available color schemes. You can then use these schemes in your chart configurations.

    import Chart from 'chart.js';
    import ColorSchemesPlugin from 'chartjs-plugin-colorschemes';
    
    // Register the plugin
    Chart.register(ColorSchemesPlugin);
    
    // The plugin attaches available schemes to the Chart object
    console.log(Chart.colorschemes);
  6. Create a custom colorscheme from scratch

    master

    Instead of using a named scheme or the custom function, you can pass an array of color strings directly to the scheme option to define your own colorscheme.

    var myColors = ['red', 'green', 'blue', 'orange', 'black', 'yellow'];
    
    // ...
        options: {
            plugins: {
                colorschemes: {
                    scheme: myColors
                }
            }
        }
  7. Use predefined color schemes in Chart.js

    master

    To use a predefined color scheme, specify it within the plugins.colorschemes.scheme option of your Chart.js configuration. You can find available scheme names in the Color Chart.

    Note: If the number of datasets exceeds the number of colors in the scheme, colors will repeat. If a dataset has its own color specified, the plugin will not modify it.

    options: {
        plugins: {
            colorschemes: {
                scheme: 'brewer.Pailed12'
            }
        }
    }
  8. Extend schemes with the `custom` function

    master

    The custom option accepts a function that receives a copy of the current scheme's color array. You can modify this array (e.g., by pushing new colors) and return it to extend the predefined scheme.

    var customColorFunction = function(schemeColors) {
        var myColors = ['#ff0000', '#00ff00', '#0000ff']; // define/generate own colors
    
        // extend the color scheme with own colors
        Array.prototype.push.apply(schemeColors, myColors);
    
        return schemeColors; // optional: this is not needed if the array is modified in place
    };
    
    // ...
        options: {
            plugins: {
                colorschemes: {
                    scheme: 'brewer.Paired12',
                    custom: customColorFunction
                }
            }
        }
  9. Access predefined color schemes via the default export

    master

    The plugin exports a default object containing several categorized collections of color schemes. You can access these collections to provide color palettes for Chart.js datasets. The available categories are:

    • brewer: ColorBrewer schemes.
    • office: Microsoft Office-style color schemes.
    • tableau: Tableau-style color schemes.
    import colorSchemes from 'chartjs-plugin-colorschemes';
    
    // Accessing a specific category
    const mySchemes = colorSchemes.brewer;
    const myOfficeSchemes = colorSchemes.office;
    const myTableauSchemes = colorSchemes.tableau;