]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
add getLabelItems public method (#10966) v4.1.0
authorDan Onoshko <danon0404@gmail.com>
Fri, 16 Dec 2022 06:36:14 +0000 (10:36 +0400)
committerGitHub <noreply@github.com>
Fri, 16 Dec 2022 06:36:14 +0000 (07:36 +0100)
src/core/core.scale.js
test/specs/core.scale.tests.js
types/helpers/helpers.canvas.d.ts
types/index.d.ts

index e79deb20384bef9424468c25e618266cd6075f6b..562ab21b654e9f8f593765e8d5f47c6eae9ad8cd 100644 (file)
@@ -359,6 +359,14 @@ export default class Scale extends Element {
     return this.options.labels || (this.isHorizontal() ? data.xLabels : data.yLabels) || data.labels || [];
   }
 
+  /**
+   * @return {import('../types.js').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 = {};
@@ -1292,17 +1300,19 @@ export default class Scale extends Element {
       }
 
       items.push({
-        rotation,
         label,
         font,
-        color,
-        strokeColor,
-        strokeWidth,
         textOffset,
-        textAlign: tickTextAlign,
-        textBaseline,
-        translation: [x, y],
-        backdrop,
+        options: {
+          rotation,
+          color,
+          strokeColor,
+          strokeWidth,
+          textAlign: tickTextAlign,
+          textBaseline,
+          translation: [x, y],
+          backdrop,
+        }
       });
     }
 
@@ -1549,16 +1559,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.options;
       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) {
index aedde2a145e048de4bb11c04818de80d0727fc4c..d0aa77f60adee74d8dc44dbe150fa9eb6d3aef88 100644 (file)
@@ -720,9 +720,9 @@ describe('Core.scale', function() {
           }
         }
       });
-      const mapper = item => parseFloat(item.translation[0].toFixed(2));
+      const mapper = item => parseFloat(item.options.translation[0].toFixed(2));
       const expected = [20.15, 113.6, 207.05, 300.5, 393.95, 487.4];
-      const actual = chart.scales.x._labelItems.map(mapper);
+      const actual = chart.scales.x.getLabelItems().map(mapper);
       const len = expected.length;
       for (let i = 0; i < len; ++i) {
         const actualValue = actual[i];
index cda1b5ce804679b5ecaca67520af0de623ad937c..6e7895adb54844cf93016e334365714c411d0198 100644 (file)
@@ -1,4 +1,4 @@
-import { PointStyle } from '../index.js';
+import { PointStyle, Scriptable, ScriptableScaleContext } from '../index.js';
 import { Color } from '../color.js';
 import { ChartArea, RoundedRect } from '../geometric.js';
 import { CanvasFontSpec } from '../../src/helpers/helpers.options.js';
@@ -72,13 +72,13 @@ export interface RenderTextOpts {
    * The text alignment to use. If unset, the existing
    * textAlign property of the context is unchanged
    */
-  textAlign: CanvasTextAlign;
+  textAlign?: CanvasTextAlign;
 
   /**
    * The text baseline to use. If unset, the existing
    * textBaseline property of the context is unchanged
    */
-  textBaseline: CanvasTextBaseline;
+  textBaseline?: CanvasTextBaseline;
 
   /**
    * If specified, a translation to apply to the context
@@ -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(
index 74d7e863515d84e241e39cda95bddd52652a2123..e3b377562b734e8fd4e77aaca8de6b2ea083aea5 100644 (file)
@@ -9,6 +9,8 @@ import { Color } from './color.js';
 import Element from '../src/core/core.element.js';
 import { ChartArea, Padding, Point } from './geometric.js';
 import { LayoutItem, LayoutPosition } from './layout.js';
+import { RenderTextOpts } from './helpers/helpers.canvas.js';
+import { CanvasFontSpec } from '../src/helpers/helpers.options.js';
 
 export { EasingFunction } from '../src/helpers/helpers.easing.js';
 export { default as ArcElement, ArcProps } from '../src/elements/element.arc.js';
@@ -1311,6 +1313,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;
@@ -1354,6 +1357,12 @@ export interface ScriptableScalePointLabelContext {
   type: string;
 }
 
+export interface LabelItem {
+  label: string | string[];
+  font: CanvasFontSpec;
+  textOffset: number;
+  options: RenderTextOpts;
+}
 
 export declare const Ticks: {
   formatters: {