如何使用Javascript轻量级图表API获取自定义图表的当前价格?
如何获得图表右侧用蓝色标记的价格。 这是我目前的代码:如何使用Javascript轻量级图表API获取自定义图表的当前价格?,javascript,tradingview-api,Javascript,Tradingview Api,如何获得图表右侧用蓝色标记的价格。 这是我目前的代码: const chart = LightweightCharts.createChart(document.body, { width: 1500, height: 700 }); const lineSeries = chart.addLineSeries(); const log = console.log; lineSeries.setData([ { time: '2019-04-11', value: 80.01 },
const chart = LightweightCharts.createChart(document.body, { width: 1500, height: 700 });
const lineSeries = chart.addLineSeries();
const log = console.log;
lineSeries.setData([
{ time: '2019-04-11', value: 80.01 },
{ time: '2019-04-12', value: 96.63 },
{ time: '2019-04-13', value: 76.64 },
{ time: '2019-04-14', value: 81.89 },
{ time: '2019-04-15', value: 74.43 },
{ time: '2019-04-16', value: 80.01 },
{ time: '2019-04-17', value: 96.63 },
{ time: '2019-04-18', value: 76.64 },
{ time: '2019-04-19', value: 81.89 },
{ time: '2019-04-20', value: 74.43 },
]);
function randomIntFromInterval(min, max) {
return Math.round((Math.random() * (max - min + 1) + min) * 100) / 100;
}
var startDate = new Date();
var endDate = new Date(2020, 5, 1);
log(lineSeries);
// lineSeries.applyOptions({
// priceFormat: {
// type: 'custom',
// minMove: 0.02,
// formatter: function(price) {
// log(price); //Gives me the price in a very bad way. Also gives it when i hover over chart.
// return '$' + price;
// },
// }
// });
/**
* Updates the chart its lines randomly.
*/
function updateChartStatic() {
setTimeout(() => {
//For updating the date.
let newDate = new Date(startDate);
//Creates a random int.
let randomInt = randomIntFromInterval(randomIntFromInterval(50, 100), randomIntFromInterval(75, 125));
// log(randomInt);
log(lineSeries._series._priceScale.rn.ct);
//Updates the line of the chart.
lineSeries.update({
time: newDate.getFullYear() + '-' + (newDate.getMonth() + 1) + '-' + newDate.getDate(),
value: randomInt,
});
//Makes sure the loop can actually end by adding a day to the startDate.
startDate.setDate(startDate.getDate() + 1);
//Make sure the function will be called again if startDate and endDate date not matches with each other. If they do, the loop will end and a console log will be shown.
startDate <= endDate ? updateChartStatic() : log("END LOOP");
}, 1000);
}
updateChartStatic();
好的,我在lineSeries变量中做了一些探索。我找到了一种方法来获取图表中最后添加的行的当前价格。这就是为什么:
//Get price of last line
let currentPrice = Array.from(Array.from(lineSeries._dataUpdatesConsumer.de.ee)[lineSeries._dataUpdatesConsumer.de.ee.size - 1][1].mapping)[0][1].value;
//Code below ends up the same but is splitted a bit more. Might look bit more understandable :P
let map = lineSeries._dataUpdatesConsumer.de.ee;
let getLastItemInMap = Array.from(map)[map.size - 1];
let secondMap = getLastItemInMap[1].mapping;
let getItemInMap = Array.from(secondMap)[0];
// Returns the price
log(getItemInMap[1].value);
所以我看到有人投了反对票,总是很好,他们也解释了为什么他们投了反对票?如果我的回答不起作用,那就告诉我。对我来说是这样,但对其他人来说可能不是这样。如果是的话就说出来。
//Get price of last line
let currentPrice = Array.from(Array.from(lineSeries._dataUpdatesConsumer.de.ee)[lineSeries._dataUpdatesConsumer.de.ee.size - 1][1].mapping)[0][1].value;
//Code below ends up the same but is splitted a bit more. Might look bit more understandable :P
let map = lineSeries._dataUpdatesConsumer.de.ee;
let getLastItemInMap = Array.from(map)[map.size - 1];
let secondMap = getLastItemInMap[1].mapping;
let getItemInMap = Array.from(secondMap)[0];
// Returns the price
log(getItemInMap[1].value);