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

Types: Allow font to be partial scriptable and individually scriptable #10364

Merged
merged 8 commits into from May 25, 2022
15 changes: 12 additions & 3 deletions types/helpers/helpers.options.d.ts
@@ -1,15 +1,24 @@
import { TRBL, TRBLCorners } from '../geometric';
import { FontSpec } from '../index.esm';

export interface CanvasFontSpec extends FontSpec {
// Should be the same as FontSpec from the index.esm.ts
// Duplicate because the default one is scriptable and this one is not
interface NonScriptableFontSpec {
family: string;
size: number;
style: 'normal' | 'italic' | 'oblique' | 'initial' | 'inherit';
weight: string | null;
lineHeight: number | string;
}

export interface CanvasFontSpec extends NonScriptableFontSpec {
string: string;
}
/**
* Parses font options and returns the font object.
* @param {object} options - A object that contains font options to be parsed.
* @return {object} The font object.
*/
export function toFont(options: Partial<FontSpec>): CanvasFontSpec;
export function toFont(options: Partial<NonScriptableFontSpec>): CanvasFontSpec;

/**
* Converts the given line height `value` in pixels for a specific font `size`.
Expand Down
42 changes: 26 additions & 16 deletions types/index.esm.d.ts
Expand Up @@ -1467,7 +1467,7 @@ export interface CoreChartOptions<TType extends ChartType> extends ParsingOption
* base font
* @see Defaults.font
*/
font: Partial<FontSpec>;
font: Partial<FontSpec<ScriptableChartContext | ScriptableCartesianScaleContext>>;
/**
* Resizes the chart canvas when its container does (important note...).
* @default true
Expand Down Expand Up @@ -1637,31 +1637,31 @@ export type AnimationOptions<TType extends ChartType> = {
transitions: TransitionsSpec<TType>;
};

export interface FontSpec {
export interface FontSpec<ScriptableFontContext> {
LeeLenaleee marked this conversation as resolved.
Show resolved Hide resolved
/**
* Default font family for all text, follows CSS font-family options.
* @default "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"
*/
family: string;
family: Scriptable<string, ScriptableFontContext>;
/**
* Default font size (in px) for text. Does not apply to radialLinear scale point labels.
* @default 12
*/
size: number;
size: Scriptable<number, ScriptableFontContext>;
/**
* Default font style. Does not apply to tooltip title or footer. Does not apply to chart title. Follows CSS font-style options (i.e. normal, italic, oblique, initial, inherit)
* @default 'normal'
*/
style: 'normal' | 'italic' | 'oblique' | 'initial' | 'inherit';
style: Scriptable<'normal' | 'italic' | 'oblique' | 'initial' | 'inherit', ScriptableFontContext>;
/**
* Default font weight (boldness). (see MDN).
*/
weight: string | null;
weight: Scriptable<string | null, ScriptableFontContext>;
/**
* Height of an individual line of text (see MDN).
* @default 1.2
*/
lineHeight: number | string;
lineHeight: Scriptable<number | string, ScriptableFontContext>;
}

export type TextAlign = 'left' | 'center' | 'right';
Expand Down Expand Up @@ -2303,7 +2303,7 @@ export interface LegendOptions<TType extends ChartType> {
* Font of label
* @see Defaults.font
*/
font: FontSpec;
font: Partial<FontSpec<ScriptableChartContext>>;
/**
* Padding between rows of colored boxes.
* @default 10
Expand Down Expand Up @@ -2364,7 +2364,7 @@ export interface LegendOptions<TType extends ChartType> {
/**
* see Fonts
*/
font: FontSpec;
font: Partial<FontSpec<ScriptableChartContext>>;
position: 'center' | 'start' | 'end';
padding?: number | ChartArea;
/**
Expand Down Expand Up @@ -2398,7 +2398,7 @@ export interface TitleOptions {
* @see Defaults.color
*/
color: Color;
font: FontSpec;
font: Partial<FontSpec<ScriptableChartContext>>;

/**
* Marks that this box should take the full width/height of the canvas (moving other boxes). If set to `false`, places the box above/beside the
Expand Down Expand Up @@ -2629,7 +2629,7 @@ export interface TooltipOptions<TType extends ChartType = ChartType> extends Cor
* See Fonts
* @default {weight: 'bold'}
*/
titleFont: Scriptable<FontSpec, ScriptableTooltipContext<TType>>;
titleFont: Scriptable<Partial<FontSpec<ScriptableTooltipContext<TType>>>, ScriptableTooltipContext<TType>>;
/**
* Spacing to add to top and bottom of each title line.
* @default 2
Expand Down Expand Up @@ -2659,7 +2659,7 @@ export interface TooltipOptions<TType extends ChartType = ChartType> extends Cor
* See Fonts.
* @default {}
*/
bodyFont: Scriptable<FontSpec, ScriptableTooltipContext<TType>>;
bodyFont: Scriptable<Partial<FontSpec<ScriptableTooltipContext<TType>>>, ScriptableTooltipContext<TType>>;
/**
* Horizontal alignment of the body text lines.
* @default 'left'
Expand All @@ -2684,7 +2684,7 @@ export interface TooltipOptions<TType extends ChartType = ChartType> extends Cor
* See Fonts
* @default {weight: 'bold'}
*/
footerFont: Scriptable<FontSpec, ScriptableTooltipContext<TType>>;
footerFont: Scriptable<Partial<FontSpec<ScriptableTooltipContext<TType>>>, ScriptableTooltipContext<TType>>;
/**
* Horizontal alignment of the footer text lines.
* @default 'left'
Expand Down Expand Up @@ -2919,7 +2919,7 @@ export interface TickOptions {
/**
* see Fonts
*/
font: Scriptable<FontSpec, ScriptableScaleContext>;
font: Scriptable<Partial<FontSpec<ScriptableScaleContext>>, ScriptableScaleContext>;
/**
* Sets the offset of the tick labels from the axis
*/
Expand Down Expand Up @@ -3022,6 +3022,16 @@ export type CartesianTickOptions = TickOptions & {
maxTicksLimit: number;
}

export interface ScriptableCartesianScaleContext {
scale: keyof CartesianScaleTypeRegistry;
type: string;
}

export interface ScriptableChartContext {
chart: Chart;
type: string;
}

export interface CartesianScaleOptions extends CoreScaleOptions {
/**
* Scale boundary strategy (bypassed by min/max time options)
Expand Down Expand Up @@ -3082,7 +3092,7 @@ export interface CartesianScaleOptions extends CoreScaleOptions {
/** Color of the axis label. */
color: Color;
/** Information about the axis title font. */
font: FontSpec;
font: Partial<FontSpec<ScriptableCartesianScaleContext>>;
/** Padding to apply around scale labels. */
padding: number | {
/** Padding on the (relative) top side of this axis label. */
Expand Down Expand Up @@ -3399,7 +3409,7 @@ export type RadialLinearScaleOptions = CoreScaleOptions & {
color: Scriptable<Color, ScriptableScalePointLabelContext>;
/**
*/
font: Scriptable<FontSpec, ScriptableScalePointLabelContext>;
font: Scriptable<Partial<FontSpec<ScriptableScalePointLabelContext>>, ScriptableScalePointLabelContext>;

/**
* Callback function to transform data labels to point labels. The default implementation simply returns the current string.
Expand Down