Skip to content

Commit

Permalink
Fix some typings issues (#9680)
Browse files Browse the repository at this point in the history
* Types: Add scope to ticks.callback

* Fix some typings issues
  • Loading branch information
kurkle committed Sep 24, 2021
1 parent 5587738 commit 990a289
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
4 changes: 2 additions & 2 deletions docs/samples/advanced/linear-gradient.md
Expand Up @@ -47,7 +47,7 @@ let width, height, gradient;
function getGradient(ctx, chartArea) {
const chartWidth = chartArea.right - chartArea.left;
const chartHeight = chartArea.bottom - chartArea.top;
if (gradient === null || width !== chartWidth || height !== chartHeight) {
if (!gradient || width !== chartWidth || height !== chartHeight) {
// Create the gradient because this is either the first render
// or the size of the chart has changed
width = chartWidth;
Expand Down Expand Up @@ -79,7 +79,7 @@ const data = {

if (!chartArea) {
// This case happens on initial chart load
return null;
return;
}
return getGradient(ctx, chartArea);
},
Expand Down
2 changes: 1 addition & 1 deletion docs/samples/advanced/radial-gradient.md
Expand Up @@ -30,7 +30,7 @@ function createRadialGradient3(context, c1, c2, c3) {
const chartArea = context.chart.chartArea;
if (!chartArea) {
// This case happens on initial chart load
return null;
return;
}

const chartWidth = chartArea.right - chartArea.left;
Expand Down
12 changes: 6 additions & 6 deletions types/index.esm.d.ts
Expand Up @@ -34,7 +34,7 @@ export interface ScriptableLineSegmentContext {
datasetIndex: number
}

export type Scriptable<T, TContext> = T | ((ctx: TContext, options: AnyObject) => T);
export type Scriptable<T, TContext> = T | ((ctx: TContext, options: AnyObject) => T | undefined);
export type ScriptableOptions<T, TContext> = { [P in keyof T]: Scriptable<T[P], TContext> };
export type ScriptableAndArray<T, TContext> = readonly T[] | Scriptable<T, TContext>;
export type ScriptableAndArrayOptions<T, TContext> = { [P in keyof T]: ScriptableAndArray<T[P], TContext> };
Expand Down Expand Up @@ -1402,22 +1402,22 @@ export interface CoreChartOptions<TType extends ChartType> extends ParsingOption
* base color
* @see Defaults.color
*/
color: Color;
color: Scriptable<Color, ScriptableContext<TType>>;
/**
* base background color
* @see Defaults.backgroundColor
*/
backgroundColor: Color;
backgroundColor: Scriptable<Color, ScriptableContext<TType>>;
/**
* base border color
* @see Defaults.borderColor
*/
borderColor: Color;
borderColor: Scriptable<Color, ScriptableContext<TType>>;
/**
* base font
* @see Defaults.font
*/
font: FontSpec;
font: Scriptable<Partial<FontSpec>, ScriptableContext<TType>>;
/**
* Resizes the chart canvas when its container does (important note...).
* @default true
Expand Down Expand Up @@ -2805,7 +2805,7 @@ export interface TickOptions {
/**
* Returns the string representation of the tick value as it should be displayed on the chart. See callback.
*/
callback: (tickValue: number | string, index: number, ticks: Tick[]) => string | number | null | undefined;
callback: (this: Scale, tickValue: number | string, index: number, ticks: Tick[]) => string | number | null | undefined;
/**
* If true, show tick labels.
* @default true
Expand Down
8 changes: 8 additions & 0 deletions types/tests/scales/options.ts
Expand Up @@ -26,6 +26,14 @@ const chart = new Chart('test', {
// @ts-expect-error Type 'string' is not assignable to type 'false | "millisecond" | "second" | "minute" | "hour" | "day" | "week" | "month" | "quarter" | "year" | undefined'.
unit: 'year'
}
},
y: {
ticks: {
callback(tickValue) {
const value = this.getLabelForValue(tickValue as number);
return '$' + value;
}
}
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions types/tests/scriptable_core_chart_options.ts
@@ -0,0 +1,15 @@
import { ChartConfiguration } from '../index.esm';

const getConfig = (): ChartConfiguration<'bar'> => {
return {
type: 'bar',
data: {
datasets: []
},
options: {
backgroundColor: (context) => context.active ? '#fff' : undefined,
font: (context) => context.datasetIndex === 1 ? { size: 10 } : { size: 12, family: 'arial' }
}
};
};

0 comments on commit 990a289

Please sign in to comment.