Skip to content

Commit

Permalink
Low label doesn't show up
Browse files Browse the repository at this point in the history
  • Loading branch information
hokilpet authored and disyakidneyshot committed Dec 4, 2023
1 parent d73ed72 commit 70bb1f5
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 49 deletions.
10 changes: 8 additions & 2 deletions src/chart/components/high_low/high-low.drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +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';

type MarkerType = 'high' | 'low';

Expand Down Expand Up @@ -41,7 +42,10 @@ export class HighLowDrawer implements Drawer {
ctx.font = this.config.components.highLow.font;
this.drawMarkerLabel(ctx, finalHighIdx, high, 'high');
this.drawMarkerLabel(ctx, finalLowIdx, low, 'low');
const chartBounds = this.canvasBoundsContainer.getBounds('PANE_CHART');
ctx.restore();
// We need clip here so lowLabel won't overlap other panes
clipToBounds(ctx, chartBounds);
}
}

Expand All @@ -55,7 +59,9 @@ export class HighLowDrawer implements Drawer {
*/
private drawMarkerLabel(ctx: CanvasRenderingContext2D, candleIdx: number, yValue: number, type: MarkerType): void {
const y = this.getMarkerY(ctx, yValue, type === 'low');
if (!this.checkMarkerInBounds(y)) {
const fontSize = getTextLineHeight(ctx, false);
const yForBoundsTrack = type === 'low' ? y - fontSize : y;
if (!this.checkMarkerInBounds(yForBoundsTrack)) {
return;
}
const text = this.getMarkerText(yValue, type);
Expand Down Expand Up @@ -89,7 +95,7 @@ export class HighLowDrawer implements Drawer {
private getMarkerY(ctx: CanvasRenderingContext2D, yValue: number, offset: boolean = false): number {
const y = this.chartModel.toY(yValue);
if (offset) {
const fontSize = getTextLineHeight(ctx);
const fontSize = getTextLineHeight(ctx, false);
return y + fontSize;
}
return y;
Expand Down
52 changes: 26 additions & 26 deletions src/chart/components/y_axis/price_labels/price-label.drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import {
FullChartColors,
getFontFromConfig,
YAxisAlign,
YAxisLabelAppearanceType,
YAxisLabelAppearanceType, YAxisLabelMode,
} from '../../../chart.config';
import { redrawBackgroundArea } from '../../../drawers/chart-background.drawer';
import { Bounds } from '../../../model/bounds.model';
import { avoidAntialiasing, drawLine } from '../../../utils/canvas/canvas-drawing-functions.utils';
import { calculateSymbolHeight, calculateTextWidth } from '../../../utils/canvas/canvas-font-measure-tool.utils';
import { floor } from '../../../utils/math.utils';
import { drawBadgeLabel, drawPlainLabel, drawRectLabel } from '../y-axis-labels.drawer';
import { drawBadgeLabel, drawPlainLabel, drawRectLabel, checkLabelInBoundaries } from '../y-axis-labels.drawer';
import { VisualYAxisLabel, YAxisVisualLabelType } from './y-axis-labels.model';

type LabelDrawer = typeof drawBadgeLabel | typeof drawRectLabel | typeof drawPlainLabel;
Expand Down Expand Up @@ -96,22 +96,30 @@ export function drawLabel(
showLine && avoidAntialiasing(ctx, () => drawLine(ctx, lineXStart, lineY, lineXEnd, lineY, 1));
const _drawLabel = () => drawLabel(ctx, bounds, text, centralY, visualLabel, config, colors, false, backgroundCtx);

switch (mode) {
case 'line':
_drawLine();
_drawDescription();
break;
case 'line-label':
_drawLine();
_drawDescription();
_drawLabel();
break;
case 'label':
_drawDescription();
_drawLabel();
break;
case 'none':
break;
const drawLineLabel = () => {
_drawLine();
_drawDescription();
}

const drawLineLabelLabel = () => {
_drawLine();
_drawLabel();
_drawDescription();
}

const drawLabelLabel = () => {
_drawDescription();
_drawLabel();
}

const labelDrawerByMode: Record<Exclude<YAxisLabelMode, 'none'>, () => void> = {
'line': drawLineLabel,
'line-label': drawLineLabelLabel,
'label': drawLabelLabel,
}

if (mode !== 'none' && checkLabelInBoundaries(centralY, bounds, labelBoxHeight)) {
labelDrawerByMode[mode]();
}

ctx.restore();
Expand Down Expand Up @@ -143,14 +151,6 @@ function drawDescription(
const labelBoxBottom = centralY + fontHeight / 2 + paddingBottom;
const labelBoxHeight = labelBoxBottom - labelBoxY;

// do not draw, if description is out of bounds
if (
centralY < labelBounds.y + labelBoxHeight / 2 ||
centralY > labelBounds.y + labelBounds.height - labelBoxHeight / 2
) {
return;
}

ctx.save();

// overlay rect
Expand Down
45 changes: 27 additions & 18 deletions src/chart/components/y_axis/y-axis-labels.drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ export const DEFAULT_PRICE_LABEL_PADDING = 4;
* @param text - text to draw
* @param centralY - y
* @param config - label styles config
* @param align
* @param yAxisState
* @param yAxisColors
* @param checkBoundaries
*/
export function drawBadgeLabel(
ctx: CanvasRenderingContext2D,
Expand All @@ -54,7 +55,7 @@ export function drawBadgeLabel(
config: YAxisLabelDrawConfig,
yAxisState: YAxisConfig,
yAxisColors: FullChartColors['yAxis'],
drawOutside: boolean = false,
checkBoundaries: boolean = true,
): void {
const align = yAxisState.align;
const textFont = config.textFont ?? getFontFromConfig(yAxisState);
Expand All @@ -71,12 +72,10 @@ export function drawBadgeLabel(
const labelBoxHeight = labelBoxBottomY - labelBoxTopY;

// do not draw, if label is out of bounds
if (
(centralY < bounds.y + labelBoxHeight / 2 || centralY > bounds.y + bounds.height - labelBoxHeight / 2) &&
!drawOutside
) {
if (checkBoundaries && !checkLabelInBoundaries(centralY, bounds, labelBoxHeight)) {
return;
}

ctx.save();
ctx.fillStyle = bgColor;
ctx.strokeStyle = bgColor;
Expand Down Expand Up @@ -119,8 +118,9 @@ export function drawBadgeLabel(
* @param text - text to draw
* @param centralY - y
* @param config - label styles config
* @param align
* @param yAxisState
* @param yAxisColors
* @param checkBoundaries
*/
export function drawRectLabel(
ctx: CanvasRenderingContext2D,
Expand All @@ -130,7 +130,7 @@ export function drawRectLabel(
config: YAxisLabelDrawConfig,
yAxisState: YAxisConfig,
yAxisColors: FullChartColors['yAxis'],
drawOutside: boolean = false,
checkBoundaries: boolean = true,
) {
const align = yAxisState.align;
const textFont = config.textFont ?? getFontFromConfig(yAxisState);
Expand All @@ -150,12 +150,10 @@ export function drawRectLabel(
const rounded = config.rounded ?? yAxisState.typeConfig.rectangle?.rounded;

// do not draw, if label is out of bounds
if (
(centralY < bounds.y + labelBoxHeight / 2 || centralY > bounds.y + bounds.height - labelBoxHeight / 2) &&
!drawOutside
) {
if (checkBoundaries && !checkLabelInBoundaries(centralY, bounds, labelBoxHeight)) {
return;
}

ctx.save();
ctx.fillStyle = bgColor;
ctx.strokeStyle = bgColor;
Expand Down Expand Up @@ -188,8 +186,10 @@ export function drawRectLabel(
* @param text - text to draw
* @param centralY - y
* @param config - label styles config
* @param align
* @param yAxisState
* @param yAxisColors
* @param checkBoundaries
* @param backgroundCtx
*/
export function drawPlainLabel(
ctx: CanvasRenderingContext2D,
Expand All @@ -199,7 +199,7 @@ export function drawPlainLabel(
config: YAxisLabelDrawConfig,
yAxisState: YAxisConfig,
yAxisColors: FullChartColors['yAxis'],
drawOutside: boolean = false,
checkBoundaries: boolean = true,
backgroundCtx?: CanvasRenderingContext2D,
) {
const align = yAxisState.align;
Expand All @@ -219,12 +219,10 @@ export function drawPlainLabel(
const labelBoxHeight = labelBoxBottomY - labelBoxTopY;

// do not draw, if label is out of bounds
if (
(centralY < bounds.y + labelBoxHeight / 2 || centralY > bounds.y + bounds.height - labelBoxHeight / 2) &&
!drawOutside
) {
if (checkBoundaries && !checkLabelInBoundaries(centralY, bounds, labelBoxHeight)) {
return;
}

ctx.save();
ctx.fillStyle = bgColor;
ctx.strokeStyle = bgColor;
Expand Down Expand Up @@ -261,3 +259,14 @@ export function getLabelYOffset(
const fontHeight = calculateSymbolHeight(font, ctx);
return fontHeight / 2 + paddingTop;
}

/**
* Checks if label fits in chart scale boundaries
* @param centralY
* @param bounds
* @param labelBoxHeight
* returns true if label fits
*/
export function checkLabelInBoundaries(centralY: number, bounds: Bounds, labelBoxHeight: number) {
return !(centralY < bounds.y + labelBoxHeight / 2 || centralY > bounds.y + bounds.height - labelBoxHeight / 2);
}
12 changes: 9 additions & 3 deletions src/chart/utils/canvas/canvas-text-functions.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export interface CanvasTextProperties {
rtl?: boolean;
}

/**
* Baseline Height in Project
*/
const __BASELINE__ = 1.33;

/**
* Sets the font, fill style and text alignment of a canvas context based on the provided properties.
* @param {CanvasRenderingContext2D} ctx - The canvas context to modify.
Expand Down Expand Up @@ -47,9 +52,10 @@ export function prepareTextForFill(ctx: CanvasRenderingContext2D, properties: Ca
/**
* Calculates the line height of a text based on the font size of the provided CanvasRenderingContext2D.
* @param {CanvasRenderingContext2D} ctx - The CanvasRenderingContext2D object used to draw the text.
* @param includeBaseLine
* @returns {number} The calculated line height of the text.
*/
export function getTextLineHeight(ctx: CanvasRenderingContext2D): number {
export function getTextLineHeight(ctx: CanvasRenderingContext2D, includeBaseLine: boolean = true): number {
const textSizeMatch = ctx.font.match(/(\d+.)?\d+(px|pt)/gi);
let textSize = '10px';
if (textSizeMatch && textSizeMatch.length) {
Expand All @@ -60,7 +66,7 @@ export function getTextLineHeight(ctx: CanvasRenderingContext2D): number {
textSize = textSizeMatch[0];
}
}
return parseInt(textSize, 10) * 1.33; // Base Line Height in Project
return includeBaseLine ? parseInt(textSize, 10) * __BASELINE__ : parseInt(textSize, 10);
}

/**
Expand Down Expand Up @@ -106,7 +112,7 @@ export function getTextLines(text: string): string[] {
* @param {number} y - The y-coordinate of the starting position of the text.
* @param {CanvasTextProperties} properties - An object containing properties for the text, such as font size, style, and alignment.
* @returns {void}
*/
export function drawText(
ctx: CanvasRenderingContext2D,
Expand Down

0 comments on commit 70bb1f5

Please sign in to comment.