[@types/chartjs-plugin-colorschemes] Create type definitions (#44149)

This commit is contained in:
Dan Manastireanu 2020-04-27 15:10:16 +03:00 committed by GitHub
parent e2b8378a90
commit 0dc3b3ce6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 104 additions and 0 deletions

View File

@ -0,0 +1,54 @@
import * as Chart from 'chart.js';
import { ColorSchemesOptions } from 'chartjs-plugin-colorschemes';
// https://github.com/nagix/chartjs-plugin-colorschemes/blob/master/src/plugins/plugin.colorschemes.js#L12
const defaults: ColorSchemesOptions = {
scheme: 'brewer.Paired12',
fillAlpha: 0.5,
reverse: false,
override: false,
};
// Supports global defaults
Chart.defaults.global.plugins = {
colorschemes: defaults,
};
const ctx = new CanvasRenderingContext2D();
const chartData = {};
// Supports chart options
// https://github.com/nagix/chartjs-plugin-colorschemes#usage
let chart = new Chart(ctx, {
type: 'bar',
data: chartData,
options: {
plugins: {
colorschemes: defaults,
},
},
});
// Supports custom function
// https://github.com/nagix/chartjs-plugin-colorschemes#custom-function
const customColorFunction = (schemeColors: string[]) => {
const 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
};
chart = new Chart(ctx, {
type: 'bar',
data: chartData,
options: {
plugins: {
colorschemes: {
scheme: 'brewer.Paired12',
custom: customColorFunction,
},
},
},
});

View File

@ -0,0 +1,25 @@
// Type definitions for chartjs-plugin-colorschemes 0.4
// Project: https://nagix.github.io/chartjs-plugin-colorschemes
// Definitions by: Dan Manastireanu <https://github.com/danmana>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.4
import * as Chart from 'chart.js';
declare module 'chart.js' {
interface ChartPluginsOptions {
colorschemes?: ColorSchemesOptions;
}
}
export interface ColorSchemesOptions {
scheme: string | string[];
fillAlpha?: number;
reverse?: boolean;
override?: boolean;
custom?: (schemeColors: string[]) => string[] | void;
}
declare const ColorSchemesPlugin: Chart.PluginServiceGlobalRegistration & Chart.PluginServiceRegistrationOptions;
export default ColorSchemesPlugin;

View File

@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"chartjs-plugin-colorschemes-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }