@neabyte/candlestick-cli
Version:
Beautiful terminal candlestick charts with real-time trading data. Create stunning ASCII art charts directly in your terminal with support for live market data, custom colors, and professional trading visualization.
1 lines • 1.95 kB
JavaScript
import{CONSTANTS}from"../constants.js";import{color,roundPrice}from"../utils/core.js";import{ChartData}from"../chart/chart-data.js";export class YAxis{chartData;constructor(e){this.chartData=e}priceToHeights(e){const{height:t}=this.chartData,{visibleCandleSet:r}=this.chartData,a=Math.min(e.close,e.open),i=Math.max(e.close,e.open),c=r.minPrice,s=r.maxPrice-c||1;return[(e.high-c)/s*t,(e.low-c)/s*t,(i-c)/s*t,(a-c)/s*t]}renderLine(e,t={}){return e===this.chartData.height||1===e?this.renderTick(e,t):e%CONSTANTS.Y_AXIS_SPACING?this.renderEmpty(e,t):this.renderTick(e,t)}renderPrice(e,t){const{chartData:r}=this,{visibleCandleSet:a}=r,{minPrice:i,maxPrice:c}=a,{height:s}=r,n=e=>roundPrice(e),o=[];for(let e=1;e<=s;e++){let t;if(e===s)t=n(c);else if(1===e)t=n(i);else{t=n(i+(e-1)*(c-i)/(s-1))}o.push(t)}const h=Math.max(...o.map(e=>e.length));let l;if(e===s)l=n(c);else if(1===e)l=n(i);else{l=n(i+(e-1)*(c-i)/(s-1))}const S=roundPrice(i+e*(c-i)/s);let T=!1,N="";for(const[e,r]of Object.entries(t)){const t=parseFloat(e.replace(/,/g,"")),a=parseFloat(l.replace(/,/g,""));if(Math.abs(t-a)<.01){T=!0,N=r;break}const i=parseFloat(S.replace(/,/g,""));if(a<=t&&t<i){T=!0,N=r;break}}const p=l.padStart(h);return[T,CONSTANTS.Y_AXIS_ON_THE_RIGHT?` ${color(`${CONSTANTS.UNICODE_Y_AXIS_RIGHT} ${p}`,N)}`:`${color(`${p} ${CONSTANTS.UNICODE_Y_AXIS_LEFT}`,N)}${" ".repeat(CONSTANTS.MARGIN_RIGHT)}`]}renderEmpty(e,t={}){if(t&&void 0!==e){const[,r]=this.renderPrice(e,t);return r}const{chartData:r}=this,{visibleCandleSet:a}=r,{minPrice:i,maxPrice:c}=a,{height:s}=r,n=e=>roundPrice(e),o=[];for(let e=1;e<=s;e++){let t;if(e===s)t=n(c);else if(1===e)t=n(i);else{t=n(i+(e-1)*(c-i)/(s-1))}o.push(t)}const h=Math.max(...o.map(e=>e.length));if(CONSTANTS.Y_AXIS_ON_THE_RIGHT)return` ${CONSTANTS.UNICODE_Y_AXIS}`;const l=" ".repeat(h+1),S=" ".repeat(CONSTANTS.MARGIN_RIGHT);return`${l}${CONSTANTS.UNICODE_Y_AXIS}${S}`}renderTick(e,t){const[,r]=this.renderPrice(e,t);return r}}