diff --git a/types/chart.js/chart.js-tests.ts b/types/chart.js/chart.js-tests.ts index 4766242a0c..6e598e26ae 100644 --- a/types/chart.js/chart.js-tests.ts +++ b/types/chart.js/chart.js-tests.ts @@ -1,11 +1,11 @@ -import { Chart, ChartData, Point } from 'chart.js'; +import { Chart, ChartData, Point, ChartColor } from 'chart.js'; // alternative: // import chartjs = require('chart.js'); // => chartjs.Chart const plugin = { - afterDraw: (chartInstance: Chart, easing: Chart.Easing, options?: any) => {}, + afterDraw: (chartInstance: Chart, easing: Chart.Easing, options?: any) => { }, }; const ctx = new CanvasRenderingContext2D(); @@ -180,3 +180,24 @@ if (radialChart.aspectRatio !== null) { console.log(radialChart.aspectRatio * 2); } console.log(radialChart.options === radialChart.config.options); + +const chartWithScriptedOptions = new Chart(new CanvasRenderingContext2D(), { + type: "bar", + data: { + labels: ["a", "b", "c", "d", "e"], + datasets: [{ + label: "test", + data: [1, 3, 5, 4, 2], + backgroundColor: ({ dataset, dataIndex }): ChartColor => { + if (dataset === undefined || dataset.data === undefined || dataIndex === undefined) { + return "black"; + } + const value = dataset.data[dataIndex]; + if (typeof value !== "number") { + return "black"; + } + return value > 3 ? "red" : "green"; + } + }], + } +}); diff --git a/types/chart.js/index.d.ts b/types/chart.js/index.d.ts index fa554ba918..7e7ab6af7c 100644 --- a/types/chart.js/index.d.ts +++ b/types/chart.js/index.d.ts @@ -135,6 +135,8 @@ declare namespace Chart { type TextAlignment = 'left' | 'center' | 'right'; + type BorderAlignment = 'center' | 'inner'; + interface ChartArea { top: number; right: number; @@ -522,34 +524,46 @@ declare namespace Chart { type ChartColor = string | CanvasGradient | CanvasPattern | string[]; + type Scriptable = (ctx: { + chart?: Chart; + dataIndex?: number; + dataset?: ChartDataSets + datasetIndex?: number; + }) => T; + interface ChartDataSets { cubicInterpolationMode?: 'default' | 'monotone'; - backgroundColor?: ChartColor | ChartColor[]; - borderWidth?: number | number[]; - borderColor?: ChartColor | ChartColor[]; + backgroundColor?: ChartColor | ChartColor[] | Scriptable; + borderAlign?: BorderAlignment | BorderAlignment[] | Scriptable; + borderWidth?: number | number[] | Scriptable; + borderColor?: ChartColor | ChartColor[] | Scriptable; borderCapStyle?: 'butt' | 'round' | 'square'; borderDash?: number[]; borderDashOffset?: number; borderJoinStyle?: 'bevel' | 'round' | 'miter'; - borderSkipped?: PositionType; + borderSkipped?: PositionType | PositionType[] | Scriptable; data?: Array | ChartPoint[]; fill?: boolean | number | string; - hoverBackgroundColor?: ChartColor | ChartColor[]; - hoverBorderColor?: ChartColor | ChartColor[]; - hoverBorderWidth?: number | number[]; + hitRadius?: number | number[] | Scriptable; + hoverBackgroundColor?: ChartColor | ChartColor[] | Scriptable; + hoverBorderColor?: ChartColor | ChartColor[] | Scriptable; + hoverBorderWidth?: number | number[] | Scriptable; label?: string; lineTension?: number; steppedLine?: 'before' | 'after' | 'middle' | boolean; - pointBorderColor?: ChartColor | ChartColor[]; - pointBackgroundColor?: ChartColor | ChartColor[]; - pointBorderWidth?: number | number[]; - pointRadius?: number | number[]; - pointHoverRadius?: number | number[]; - pointHitRadius?: number | number[]; - pointHoverBackgroundColor?: ChartColor | ChartColor[]; - pointHoverBorderColor?: ChartColor | ChartColor[]; - pointHoverBorderWidth?: number | number[]; - pointStyle?: PointStyle | HTMLImageElement | HTMLCanvasElement | Array; + pointBorderColor?: ChartColor | ChartColor[] | Scriptable; + pointBackgroundColor?: ChartColor | ChartColor[] | Scriptable; + pointBorderWidth?: number | number[] | Scriptable; + pointRadius?: number | number[] | Scriptable; + pointRotation?: number | number[] | Scriptable; + pointHoverRadius?: number | number[] | Scriptable; + pointHitRadius?: number | number[] | Scriptable; + pointHoverBackgroundColor?: ChartColor | ChartColor[] | Scriptable; + pointHoverBorderColor?: ChartColor | ChartColor[] | Scriptable; + pointHoverBorderWidth?: number | number[] | Scriptable; + pointStyle?: PointStyle | HTMLImageElement | HTMLCanvasElement | Array | Scriptable; + radius?: number | number[] | Scriptable; + rotation?: number | number[] | Scriptable; xAxisID?: string; yAxisID?: string; type?: ChartType | string;