gpt4 book ai didi

reactjs - React-DOM:无法将交易 View Canvas 从 'lightweight-charts' 附加到正文

转载 作者:行者123 更新时间:2023-12-04 04:17:46 27 4
gpt4 key购买 nike

react 的交易 View 带有 document.body,我想在父 div 中插入 Canvas 。但它会附加到 body 的末端。尝试了很多可能的解决方案,但没有任何效果。

Elements Want to move graph within orange background

Graph代码如下:

import { createChart } from "lightweight-charts";  

function Graph() {
var chart = createChart(document.body, {
width: 600,
height: 300,
crosshair: {
mode: "normal"
}
});

var candleSeries = chart.addCandlestickSeries();

var data = [
{ time: "2018-10-19", open: 54.62, high: 55.5, low: 54.52, close: 54.9 },
{ time: "2018-10-22", open: 55.08, high: 55.27, low: 54.61, close: 54.98 },
{ time: "2018-10-23", open: 56.09, high: 57.47, low: 56.09, close: 57.21 },
{ time: "2018-10-24", open: 57.0, high: 58.44, low: 56.41, close: 57.42 },
{ time: "2018-10-25", open: 57.46, high: 57.63, low: 56.17, close: 56.43 },
{ time: "2018-10-26", open: 56.26, high: 56.62, low: 55.19, close: 55.51 },
{ time: "2018-10-29", open: 55.81, high: 57.15, low: 55.72, close: 56.48 },
{ time: "2018-10-30", open: 56.92, high: 58.8, low: 56.92, close: 58.18 },
{ time: "2018-10-31", open: 58.32, high: 58.32, low: 56.76, close: 57.09 },
{ time: "2018-11-01", open: 56.98, high: 57.28, low: 55.55, close: 56.05 },
{ time: "2018-11-02", open: 56.34, high: 57.08, low: 55.92, close: 56.63 },
{ time: "2018-11-05", open: 56.51, high: 57.45, low: 56.51, close: 57.21 },
{ time: "2018-11-06", open: 57.02, high: 57.35, low: 56.65, close: 57.21 },
{ time: "2018-11-07", open: 57.55, high: 57.78, low: 57.03, close: 57.65 },
{ time: "2018-11-08", open: 57.7, high: 58.44, low: 57.66, close: 58.27 },
];

candleSeries.setData(data);

var lastClose = data[data.length - 1].close;
var lastIndex = data.length - 1;

var targetIndex = lastIndex + 105 + Math.round(Math.random() + 30);
var targetPrice = getRandomPrice();

var currentIndex = lastIndex + 1;
var currentBusinessDay = { day: 29, month: 5, year: 2019 };
var ticksInCurrentBar = 0;
var currentBar = {
open: null,
high: null,
low: null,
close: null,
time: currentBusinessDay
};

function mergeTickToBar(price) {
if (currentBar.open === null) {
currentBar.open = price;
currentBar.high = price;
currentBar.low = price;
currentBar.close = price;
} else {
currentBar.close = price;
currentBar.high = Math.max(currentBar.high, price);
currentBar.low = Math.min(currentBar.low, price);
}
candleSeries.update(currentBar);
}

function reset() {
candleSeries.setData(data);
lastClose = data[data.length - 1].close;
lastIndex = data.length - 1;

targetIndex = lastIndex + 5 + Math.round(Math.random() + 30);
targetPrice = getRandomPrice();

currentIndex = lastIndex + 1;
currentBusinessDay = { day: 29, month: 5, year: 2019 };
ticksInCurrentBar = 0;
}

function getRandomPrice() {
return 10 + Math.round(Math.random() * 10000) / 100;
}

function nextBusinessDay(time) {
var d = new Date();
d.setUTCFullYear(time.year);
d.setUTCMonth(time.month - 1);
d.setUTCDate(time.day + 1);
d.setUTCHours(0, 0, 0, 0);
return {
year: d.getUTCFullYear(),
month: d.getUTCMonth() + 1,
day: d.getUTCDate()
};
}

setInterval(function() {
var deltaY = targetPrice - lastClose;
var deltaX = targetIndex - lastIndex;
var angle = deltaY / deltaX;
var basePrice = lastClose + (currentIndex - lastIndex) * angle;
var noise = 0.1 - Math.random() * 0.2 + 1.0;
var noisedPrice = basePrice * noise;
mergeTickToBar(noisedPrice);
if (++ticksInCurrentBar === 5) {
// move to next bar
currentIndex++;
currentBusinessDay = nextBusinessDay(currentBusinessDay);
currentBar = {
open: null,
high: null,
low: null,
close: null,
time: currentBusinessDay
};
ticksInCurrentBar = 0;
if (currentIndex === 5000) {
reset();
return;
}
if (currentIndex === targetIndex) {
// change trend
lastClose = noisedPrice;
lastIndex = currentIndex;
targetIndex = lastIndex + 5 + Math.round(Math.random() + 30);
targetPrice = getRandomPrice();
}
}
}, 200);
return null;
}

export default Graph;

父组件:

import React from "react";
import Graph from "./Graph";

function App() {
return (
<div
style={{
flex: 1,
flexDirection: "column",
background: "orange",
justifyContent: "center",
alignItems: "center"
}}
>
<Graph />
<h1>It is to append graph into body</h1>
</div>
);
}

export default App;

我已经尝试过 refs,不同的 DOM 操作方法,但没有任何效果。如果有人可以帮助我找到解决方案,那就太好了。

最佳答案

我在使用错误逻辑的 useRef 并让它与这些更改一起工作,对于可能遇到相同类型问题的人:

import { createChart } from "lightweight-charts";
import React from 'react';

const Graph = () => {
const chartRef = React.useRef(null);

React.useEffect(()=> {
if(chartRef.current){
const chart = createChart(chartRef.current, {
width: 600,
height: 300,
crosshair: {
mode: "normal"
}
});

prepareChart(chart);
}
}, [])

function prepareChart(chart) {

var candleSeries = chart.addCandlestickSeries();

var data = [
{ time: "2018-10-19", open: 54.62, high: 55.5, low: 54.52, close: 54.9 },
{ time: "2018-10-22", open: 55.08, high: 55.27, low: 54.61, close: 54.98 },
{ time: "2018-10-23", open: 56.09, high: 57.47, low: 56.09, close: 57.21 },
{ time: "2018-10-24", open: 57.0, high: 58.44, low: 56.41, close: 57.42 },
{ time: "2018-10-25", open: 57.46, high: 57.63, low: 56.17, close: 56.43 },
];

candleSeries.setData(data);

var lastClose = data[data.length - 1].close;
var lastIndex = data.length - 1;

var targetIndex = lastIndex + 105 + Math.round(Math.random() + 30);
var targetPrice = getRandomPrice();

var currentIndex = lastIndex + 1;
var currentBusinessDay = { day: 29, month: 5, year: 2019 };
var ticksInCurrentBar = 0;
var currentBar = {
open: null,
high: null,
low: null,
close: null,
time: currentBusinessDay
};

function mergeTickToBar(price) {
if (currentBar.open === null) {
currentBar.open = price;
currentBar.high = price;
currentBar.low = price;
currentBar.close = price;
} else {
currentBar.close = price;
currentBar.high = Math.max(currentBar.high, price);
currentBar.low = Math.min(currentBar.low, price);
}
candleSeries.update(currentBar);
}

function reset() {
candleSeries.setData(data);
lastClose = data[data.length - 1].close;
lastIndex = data.length - 1;

targetIndex = lastIndex + 5 + Math.round(Math.random() + 30);
targetPrice = getRandomPrice();

currentIndex = lastIndex + 1;
currentBusinessDay = { day: 29, month: 5, year: 2019 };
ticksInCurrentBar = 0;
}

function getRandomPrice() {
return 10 + Math.round(Math.random() * 10000) / 100;
}

function nextBusinessDay(time) {
var d = new Date();
d.setUTCFullYear(time.year);
d.setUTCMonth(time.month - 1);
d.setUTCDate(time.day + 1);
d.setUTCHours(0, 0, 0, 0);
return {
year: d.getUTCFullYear(),
month: d.getUTCMonth() + 1,
day: d.getUTCDate()
};
}

setInterval(function() {
var deltaY = targetPrice - lastClose;
var deltaX = targetIndex - lastIndex;
var angle = deltaY / deltaX;
var basePrice = lastClose + (currentIndex - lastIndex) * angle;
var noise = 0.1 - Math.random() * 0.2 + 1.0;
var noisedPrice = basePrice * noise;
mergeTickToBar(noisedPrice);
if (++ticksInCurrentBar === 5) {
// move to next bar
currentIndex++;
currentBusinessDay = nextBusinessDay(currentBusinessDay);
currentBar = {
open: null,
high: null,
low: null,
close: null,
time: currentBusinessDay
};
ticksInCurrentBar = 0;
if (currentIndex === 5000) {
reset();
return;
}
if (currentIndex === targetIndex) {
// change trend
lastClose = noisedPrice;
lastIndex = currentIndex;
targetIndex = lastIndex + 5 + Math.round(Math.random() + 30);
targetPrice = getRandomPrice();
}
}
}, 200);

}
return <div ref={chartRef} />;
}

export default Graph;

关于reactjs - React-DOM:无法将交易 View Canvas 从 'lightweight-charts' 附加到正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60220841/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com