nagix / chartjs-plugin-colorschemes

Predefined color schemes for Chart.js
MIT License
263 stars 58 forks source link

Extend the amount of scheme colors through a function #8

Closed JohnArcher closed 5 years ago

JohnArcher commented 5 years ago

Hey @nagix , would you accept this commit as a pull request? -> https://github.com/JohnArcher/chartjs-plugin-colorschemes/commit/707ce90516af10e23e7ded3055f75d9c4a4e9f63

The aim is to (dynamically) add additional colors to a scheme in order to e.g. prevent that the same colors are used again when you have more datasets then colors.

An example using my code would be:

var myColorFunction = function () {
    var myColors = ['#ff00000', '#00ff00'];
    // ... or calculate your colors with some fancy algorithm
    return myColors;
}

...
    plugins: {
        colorschemes: {
            scheme: 'brewer.Paired12',
            colorFunction: myColorFunction
        }
    }

I really need this behaviour. :-) What do you think?

nagix commented 5 years ago

I think this is a great idea. To make it more generalized, how about naming it custom and allowing it to receive the array of color strings and to return the customized color strings?

var myCustomFunction = function(colors) {
    var myColors = ['#ff00000', '#00ff00'];
    Array.prototype.push.apply(colors, myColors);
    // ... or calculate your colors with some fancy algorithm
    return colors;
}

// ...
    plugins: {
        colorschemes: {
            scheme: 'brewer.Paired12',
            custom: myCustomFunction
        }
    }

In order to keep the original color string array untouched, the array should be cloned before it is passed to the custom function.

If this is ok, feel free to create a pull request 🙂

JohnArcher commented 5 years ago

Sorry for my late reply. I had some days off due to Easter holidays.

I am glad you like the idea; thank you! I will incorporate your changes and create the pull request.

JohnArcher commented 5 years ago

Ok, I reworked the code. Have a look: https://github.com/JohnArcher/chartjs-plugin-colorschemes/commit/ee2d23fbb89b50eb77a7a24e797bbfdc7d9bca1f

It is no problem for me to change it further if you don't like pieces of this.

nagix commented 5 years ago

Thank you @JohnArcher. Other than this comment, the code looks good. Can you create a PR for this? It would be great if you also add a description about the custom option in README.

JohnArcher commented 5 years ago

You're welcome, @nagix. Here is the latest commit: https://github.com/JohnArcher/chartjs-plugin-colorschemes/commit/aaf6ac90e3435cf05d017457ae10f0cec18842eb Not sure whether the documentation is ok (language, overall style), that's why I didn't create the PR right away.

nagix commented 5 years ago

Please see my comments in https://github.com/JohnArcher/chartjs-plugin-colorschemes/commit/aaf6ac90e3435cf05d017457ae10f0cec18842eb and go ahead with PR. Thanks!

JohnArcher commented 5 years ago

Thanks for your good comments! PR is there.

nagix commented 5 years ago

Closing as v0.4.0 was released.