Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some typings issues #9680

Merged
merged 2 commits into from Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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' }
}
};
};