add ohlc legend

This commit is contained in:
c9s 2022-06-27 16:39:46 +08:00
parent a90b6bd1f5
commit 8db6c5e278
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -449,6 +449,14 @@ const TradingViewChart = (props: TradingViewChartProps) => {
series.setData(chartData.klines);
series.setMarkers(chartData.markers);
const ohlcLegend = createLegend(0, 'rgba(0, 0, 0, 1)');
chartContainerRef.current.appendChild(ohlcLegend);
const updateOHLCLegend = createOHLCLegendUpdater(ohlcLegend, "")
chart.current.subscribeCrosshairMove((param: MouseEventParams) => {
updateOHLCLegend(param.seriesPrices.get(series), param.time);
});
[9, 27, 99].forEach((w, i) => {
const emaValues = calculateEMA(chartData.klines, w)
const emaColor = 'rgba(' + w + ', ' + (111 - w) + ', 232, 0.9)'
@ -459,7 +467,7 @@ const TradingViewChart = (props: TradingViewChartProps) => {
});
emaLine.setData(emaValues);
const legend = createLegend(i, emaColor)
const legend = createLegend(i + 1, emaColor)
chartContainerRef.current.appendChild(legend);
const updateLegendText = createLegendUpdater(legend, 'EMA ' + w)
@ -637,4 +645,16 @@ const createLegendUpdater = (legend: HTMLDivElement, prefix: string) => {
}
}
const createOHLCLegendUpdater = (legend: HTMLDivElement, prefix: string) => {
return (param: any, time : any) => {
console.log(param)
if (param) {
legend.innerHTML = prefix + ` O: ${param.open} H: ${param.high} L: ${param.low} C: ${param.close} T: ${new Date(time * 1000)}`;
} else {
legend.innerHTML = prefix + ' -';
}
}
}
export default TradingViewChart;