Skip to content

Commit

Permalink
fix: low label display
Browse files Browse the repository at this point in the history
  • Loading branch information
hokilpet authored and disyakidneyshot committed Dec 11, 2023
1 parent 223f5bf commit 935c2e0
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/chart/components/high_low/high-low.drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { FullChartConfig } from '../../chart.config';
import { ChartModel } from '../chart/chart.model';
import { CanvasModel } from '../../model/canvas.model';
import { getTextLineHeight } from '../../utils/canvas/canvas-text-functions.utils';
import { clipToBounds } from '../../drawers/data-series.drawer';
import { clipToBounds } from '../../utils/canvas/canvas-drawing-functions.utils';

type MarkerType = 'high' | 'low';

Expand Down Expand Up @@ -60,6 +60,7 @@ export class HighLowDrawer implements Drawer {
private drawMarkerLabel(ctx: CanvasRenderingContext2D, candleIdx: number, yValue: number, type: MarkerType): void {
const y = this.getMarkerY(ctx, yValue, type === 'low');
const fontSize = getTextLineHeight(ctx, false);
// we need to measure fit into the bounds for low label by its top point
const yForBoundsTrack = type === 'low' ? y - fontSize : y;
if (!this.checkMarkerInBounds(yForBoundsTrack)) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/chart/components/highlights/highlights.drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { CanvasBoundsContainer, CanvasElement } from '../../canvas/canvas-bounds
import { Drawer } from '../../drawers/drawing-manager';
import { ChartModel } from '../chart/chart.model';
import { unitToPixels } from '../../model/scaling/viewport.model';
import { clipToBounds } from '../../drawers/data-series.drawer';
import { clipToBounds } from '../../utils/canvas/canvas-drawing-functions.utils';

const LABEL_PADDINGS = [20, 10];

Expand Down
2 changes: 1 addition & 1 deletion src/chart/components/volumes/volumes.drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { BarType, FullChartConfig } from '../../chart.config';
import { clipToBounds } from '../../drawers/data-series.drawer';
import { clipToBounds } from '../../utils/canvas/canvas-drawing-functions.utils';
import { PriceMovement } from '../../model/candle-series.model';
import { CanvasModel } from '../../model/canvas.model';
import { Pixel, ViewportModel, unitToPixels } from '../../model/scaling/viewport.model';
Expand Down
2 changes: 1 addition & 1 deletion src/chart/components/y_axis/y-axis.drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { FullChartConfig, YAxisAlign, getFontFromConfig } from '../../chart.config';
import { clipToBounds } from '../../drawers/data-series.drawer';
import { clipToBounds } from '../../utils/canvas/canvas-drawing-functions.utils';
import { Drawer } from '../../drawers/drawing-manager';
import { Bounds } from '../../model/bounds.model';
import { CanvasModel } from '../../model/canvas.model';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import { BarType, FullChartConfig } from '../../chart.config';
import { BoundsProvider } from '../../model/bounds.model';
import { DataSeriesModel, VisualSeriesPoint } from '../../model/data-series.model';
import { ChartDrawerConfig, clipToBounds, SeriesDrawer } from '../data-series.drawer';
import { ChartDrawerConfig, SeriesDrawer } from '../data-series.drawer';
import { clipToBounds } from '../../utils/canvas/canvas-drawing-functions.utils';

export const candleTypesList: BarType[] = [
'candle',
Expand Down
9 changes: 1 addition & 8 deletions src/chart/drawers/data-series.drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
*/
import { DynamicModelDrawer } from '../components/dynamic-objects/dynamic-objects.drawer';
import { PaneManager } from '../components/pane/pane-manager.component';
import { Bounds } from '../model/bounds.model';
import { CanvasModel } from '../model/canvas.model';
import { DataSeriesModel, VisualSeriesPoint } from '../model/data-series.model';
import { clipToBounds } from '../utils/canvas/canvas-drawing-functions.utils';

export interface ChartDrawerConfig {
singleColor?: string;
Expand Down Expand Up @@ -69,13 +69,6 @@ export class DataSeriesDrawer implements DynamicModelDrawer<DataSeriesModel> {
}
}

export const clipToBounds = (ctx: CanvasRenderingContext2D, bounds: Bounds) => {
ctx.beginPath();
ctx.rect(bounds.x, bounds.y, bounds.width, bounds.height);
ctx.clip();
ctx.closePath();
};

export const setLineWidth = (
ctx: CanvasRenderingContext2D,
lineWidth: number,
Expand Down
3 changes: 2 additions & 1 deletion src/chart/drawers/ht-data-series.drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import { PaneManager } from '../components/pane/pane-manager.component';
import { DataSeriesModel } from '../model/data-series.model';
import { HitTestCanvasModel } from '../model/hit-test-canvas.model';
import { ChartDrawerConfig, clipToBounds, SeriesDrawer } from './data-series.drawer';
import { ChartDrawerConfig, SeriesDrawer } from './data-series.drawer';
import { clipToBounds } from '../utils/canvas/canvas-drawing-functions.utils';
import { Drawer } from './drawing-manager';

/***
Expand Down
19 changes: 18 additions & 1 deletion src/chart/utils/canvas/canvas-drawing-functions.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { YAxisAlign } from '../../chart.config';
import { Bounds } from '../../model/bounds.model';

/**
* Draws a rounded rectangle on a canvas context
Expand Down Expand Up @@ -171,7 +172,7 @@ export function avoidAntialiasing(ctx: CanvasRenderingContext2D, draw: () => voi
* @param {{x: number, y: number}} a - The first point of the rectangle.
* @param {{x: number, y: number}} b - The second point of the rectangle.
* @returns {void}
*/
export function fillRect(
ctx: CanvasRenderingContext2D,
Expand All @@ -190,3 +191,19 @@ export function fillRect(
const h = Math.abs(a.y - b.y);
ctx.fillRect(x, y, w, h);
}

/**
* Sets clipping region for a Canvas 2D context according to the provided bounds.
*
* @param {CanvasRenderingContext2D} ctx - The canvas 2D context which will get the new clip region.
* @param {object} bounds - The bounds of the clipping region. Object containing x, y, width and height properties.
*
* @example
* clipToBounds(ctx, {x: 50, y: 50, width: 100, height: 100});
*/
export const clipToBounds = (ctx: CanvasRenderingContext2D, bounds: Bounds) => {
ctx.beginPath();
ctx.rect(bounds.x, bounds.y, bounds.width, bounds.height);
ctx.clip();
ctx.closePath();
};
4 changes: 2 additions & 2 deletions src/chart/utils/canvas/canvas-text-functions.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface CanvasTextProperties {
/**
* Baseline Height in Project
*/
const __BASELINE__ = 1.33;
const BASELINE = 1.33;

/**
* Sets the font, fill style and text alignment of a canvas context based on the provided properties.
Expand Down Expand Up @@ -66,7 +66,7 @@ export function getTextLineHeight(ctx: CanvasRenderingContext2D, includeBaseLine
textSize = textSizeMatch[0];
}
}
return includeBaseLine ? parseInt(textSize, 10) * __BASELINE__ : parseInt(textSize, 10);
return includeBaseLine ? parseInt(textSize, 10) * BASELINE : parseInt(textSize, 10);
}

/**
Expand Down

0 comments on commit 935c2e0

Please sign in to comment.