/** * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { YAxisType } from '../common/Styles'; import { formatPrecision, formatThousands, formatFoldDecimal, } from '../common/utils/format'; import { isValid } from '../common/utils/typeChecks'; import View from './View'; import type YAxis from '../component/YAxis'; export default class CandleVisibleLastPriceLabelView extends View { override drawImp(ctx: CanvasRenderingContext2D): void { const widget = this.getWidget(); const pane = widget.getPane(); const bounding = widget.getBounding(); const chartStore = pane.getChart().getChartStore(); const priceMarkStyles = chartStore.getStyles().candle.visiblePriceMark; const yAxisColor = chartStore.getStyles().xAxis.axisLine.color; const lastVisiblePriceMarkStyles = priceMarkStyles.last; const lastVisiblePriceMarkTextStyles = lastVisiblePriceMarkStyles.text; if ( priceMarkStyles.show && lastVisiblePriceMarkStyles.show && lastVisiblePriceMarkTextStyles.show ) { const precision = chartStore.getPrecision(); const yAxis = pane.getAxisComponent() as YAxis; const data = chartStore.getVisibleLastData(); if (isValid(data)) { const { close, open } = data; const priceY = yAxis.convertToNicePixel(close); let backgroundColor: string; if (close > open) { backgroundColor = lastVisiblePriceMarkStyles.upColor; } else if (close < open) { backgroundColor = lastVisiblePriceMarkStyles.downColor; } else { backgroundColor = lastVisiblePriceMarkStyles.noChangeColor; } let text: string; if (yAxis.getType() === YAxisType.Percentage) { const fromData = chartStore.getVisibleFirstData(); const fromClose = fromData!.close; text = `${(((close - fromClose) / fromClose) * 100).toFixed(2)}%`; } else { text = formatPrecision(close, precision.price); } text = formatFoldDecimal( formatThousands(text, chartStore.getThousandsSeparator()), chartStore.getDecimalFoldThreshold(), ); let x: number; let textAlgin: CanvasTextAlign; if (yAxis.isFromZero()) { x = 0; textAlgin = 'left'; } else { x = bounding.width; textAlgin = 'right'; } this.createFigure({ name: 'text', attrs: { x, y: priceY, text, align: textAlgin, baseline: 'middle', }, styles: { ...lastVisiblePriceMarkTextStyles, color: backgroundColor, borderColor: backgroundColor, backgroundColor: yAxisColor, }, })?.draw(ctx); } } } }