Skip to content

Commit

Permalink
fix chartjs#10836 - add publc getLabelItems()
Browse files Browse the repository at this point in the history
  • Loading branch information
cmcnulty committed Nov 2, 2022
1 parent 3fea72e commit eaa0f75
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 17 deletions.
40 changes: 24 additions & 16 deletions src/core/core.scale.js
Expand Up @@ -359,6 +359,14 @@ export default class Scale extends Element {
return this.options.labels || (this.isHorizontal() ? data.xLabels : data.yLabels) || data.labels || [];
}

/**
* @return {LabelItem[]}
*/
getLabelItems(chartArea = this.chart.chartArea) {
const items = this._labelItems || (this._labelItems = this._computeLabelItems(chartArea));
return items;
}

// When a new layout is created, reset the data limits cache
beforeLayout() {
this._cache = {};
Expand Down Expand Up @@ -1292,17 +1300,20 @@ export default class Scale extends Element {
}

items.push({
rotation,
label,
font,
color,
strokeColor,
strokeWidth,
textOffset,
textAlign: tickTextAlign,
textBaseline,
translation: [x, y],
backdrop,
renderTextOptions: {
rotation,
color,
strokeColor,
strokeWidth,
textOffset,
textAlign: tickTextAlign,
textBaseline,
translation: [x, y],
backdrop,
}
});
}

Expand Down Expand Up @@ -1549,16 +1560,13 @@ export default class Scale extends Element {
clipArea(ctx, area);
}

const items = this._labelItems || (this._labelItems = this._computeLabelItems(chartArea));
let i, ilen;

for (i = 0, ilen = items.length; i < ilen; ++i) {
const item = items[i];
const items = this.getLabelItems(chartArea);
for (const item of items) {
const renderTextOptions = item.renderTextOptions;
const tickFont = item.font;
const label = item.label;

let y = item.textOffset;
renderText(ctx, label, 0, y, tickFont, item);
const y = item.textOffset;
renderText(ctx, label, 0, y, tickFont, renderTextOptions);
}

if (area) {
Expand Down
34 changes: 33 additions & 1 deletion types/helpers/helpers.canvas.d.ts
@@ -1,4 +1,4 @@
import { PointStyle } from '..';
import { PointStyle, Scriptable, ScriptableScaleContext } from '..';
import { Color } from '../color';
import { ChartArea, RoundedRect } from '../geometric';
import { CanvasFontSpec } from './helpers.options';
Expand Down Expand Up @@ -89,6 +89,38 @@ export interface RenderTextOpts {
* Underline the text
*/
underline?: boolean;

/**
* Dimensions for drawing the label backdrop
*/
backdrop?: BackdropOptions;
}

export interface BackdropOptions {
/**
* Left position of backdrop as pixel
*/
left: number;

/**
* Top position of backdrop as pixel
*/
top: number;

/**
* Width of backdrop in pixels
*/
width: number;

/**
* Height of backdrop in pixels
*/
height: number;

/**
* Color of label backdrops.
*/
color: Scriptable<Color, ScriptableScaleContext>;
}

export function renderText(
Expand Down
9 changes: 9 additions & 0 deletions types/index.d.ts
Expand Up @@ -10,6 +10,8 @@ import { Color } from './color';
import Element from '../src/core/core.element';
import { ChartArea, Padding, Point } from './geometric';
import { LayoutItem, LayoutPosition } from './layout';
import { RenderTextOpts } from './helpers/helpers.canvas';
import { CanvasFontSpec } from './helpers';

export { EasingFunction } from '../src/helpers/helpers.easing';
export { default as ArcElement, ArcProps } from '../src/elements/element.arc';
Expand Down Expand Up @@ -1312,6 +1314,7 @@ export interface Scale<O extends CoreScaleOptions = CoreScaleOptions> extends El
getMinMax(canStack: boolean): { min: number; max: number };
getTicks(): Tick[];
getLabels(): string[];
getLabelItems(chartArea?: ChartArea): LabelItem[];
beforeUpdate(): void;
configure(): void;
afterUpdate(): void;
Expand Down Expand Up @@ -1355,6 +1358,12 @@ export interface ScriptableScalePointLabelContext {
type: string;
}

export interface LabelItem {
label: string | string[];
font: CanvasFontSpec;
textOffset: number;
renderTextOptions: RenderTextOpts;
}

export declare const Ticks: {
formatters: {
Expand Down

0 comments on commit eaa0f75

Please sign in to comment.