From 0dc3b3ce6f150f8413ecd3694edc7c8ca331908c Mon Sep 17 00:00:00 2001 From: Dan Manastireanu <498419+danmana@users.noreply.github.com> Date: Mon, 27 Apr 2020 15:10:16 +0300 Subject: [PATCH] [@types/chartjs-plugin-colorschemes] Create type definitions (#44149) --- .../chartjs-plugin-colorschemes-tests.ts | 54 +++++++++++++++++++ types/chartjs-plugin-colorschemes/index.d.ts | 25 +++++++++ .../chartjs-plugin-colorschemes/tsconfig.json | 24 +++++++++ types/chartjs-plugin-colorschemes/tslint.json | 1 + 4 files changed, 104 insertions(+) create mode 100644 types/chartjs-plugin-colorschemes/chartjs-plugin-colorschemes-tests.ts create mode 100644 types/chartjs-plugin-colorschemes/index.d.ts create mode 100644 types/chartjs-plugin-colorschemes/tsconfig.json create mode 100644 types/chartjs-plugin-colorschemes/tslint.json diff --git a/types/chartjs-plugin-colorschemes/chartjs-plugin-colorschemes-tests.ts b/types/chartjs-plugin-colorschemes/chartjs-plugin-colorschemes-tests.ts new file mode 100644 index 0000000000..6c74dceb08 --- /dev/null +++ b/types/chartjs-plugin-colorschemes/chartjs-plugin-colorschemes-tests.ts @@ -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, + }, + }, + }, +}); diff --git a/types/chartjs-plugin-colorschemes/index.d.ts b/types/chartjs-plugin-colorschemes/index.d.ts new file mode 100644 index 0000000000..ef5520a808 --- /dev/null +++ b/types/chartjs-plugin-colorschemes/index.d.ts @@ -0,0 +1,25 @@ +// Type definitions for chartjs-plugin-colorschemes 0.4 +// Project: https://nagix.github.io/chartjs-plugin-colorschemes +// Definitions by: Dan Manastireanu +// 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; diff --git a/types/chartjs-plugin-colorschemes/tsconfig.json b/types/chartjs-plugin-colorschemes/tsconfig.json new file mode 100644 index 0000000000..82c241a0ab --- /dev/null +++ b/types/chartjs-plugin-colorschemes/tsconfig.json @@ -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" + ] +} diff --git a/types/chartjs-plugin-colorschemes/tslint.json b/types/chartjs-plugin-colorschemes/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/chartjs-plugin-colorschemes/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }