TradingView 轻量级图表价格似乎无法正确缩放

All*_*art 5 javascript charts tradingview-api lightweight-charts

我刚刚将比特币的实时数据添加到我的图表中。已经出现了一个如此之高的峰值,甚至交易视图都无法处理它......至少在我的图表上是这样。它是这样的: 我的代码/图表但放大了 正如你所看到的,6 月 2 日,比特币的涨幅如此之高,以至于它已经走出了屏幕。这应该是可以修复的,因为在实际的交易视图页面本身上,一切看起来都很好并且正常: TradingView.com 另外,如果我缩小图表,它看起来完全正常: 我的代码/图表但缩小了 所以我想要的是我的图表的缩放方式与 Tradingview.com 上的相同。

这是我的代码:

// Predifined variables
var chart, priceArea, fetchedData;

// Faster to write log();
const log = console.log;

// Get data.
fetch('./getData.php', {
    method: 'GET'
}).then(response => response.json()).then(function (data) {
    // Set data to a global variable for global usage.
    fetchedData = data;

    // To make sure the chart is initialized and set after this fetch is done. Else I would get a error for setting data that I do not yet have.
    initChartSettings();
});


/**
 * Initializes the chart and its settings.
 */
function initChartSettings() {
    // Create chart canvas
    chart = LightweightCharts.createChart(document.getElementById('Chart'), {width: 1500, height: 700,});

    // Could also be done in the width and height code line but thought it might work for scaling.
    chart.applyOptions({
        timeScale: {
            // Adds hours and minutes to the chart.
            timeVisible: true,
            secondsVisible: false
        }
    });

    // Init price line
    priceArea = chart.addAreaSeries();

    // This array is needed to make the setData work. It expects an array with objects.
    let arrayWithOldData = [];

    // Get each item in the fetchedData array. Since it gives more data then I need I use this for loop to take the time and value. Used to create chart lines.
    for (let i = 0; i < fetchedData.length; i++) {
        let dataElement = fetchedData[i];

        // Object needed for the arrayWithOldData.
        let oldData = {
            // Timestamp / 1000 to make it a workable timestamp for tradingview chart and + 7200 seconds to make sure I get a timestamp of amsterdam (+2:00).
            time: (dataElement[0] / 1000) + 7200,
            value: dataElement[4]
        };

        // Add the data to the array.
        arrayWithOldData.push(oldData);
    }

    log(arrayWithOldData);

    // Set data
    priceArea.setData(arrayWithOldData);

    // PriceLine options
    priceArea.applyOptions({
        topColor: 'rgba(70, 130, 180, 0.5)',
        bottomColor: 'rgba(70, 130, 180, 0.1)',
        lineColor: '#4682B4',
        lineWidth: 2
    });

    startTime = new Date();
    updateLiveChartData();
}
Run Code Online (Sandbox Code Playgroud)

我尝试过的:

我在代码中尝试了以下操作:https://github.com/tradingview/lightweight-charts/blob/master/docs/customization.md -> 价格轴 ->priceScale() -> autoScale: true 和 scaleMargins 具有不同的值顶部和底部。似乎scaleMargins有效,但是当我回到过去并确保我不再看到峰值时,一切都一样好:(我还尝试放在代码末尾:chart.timeScale().fitContent() ; 但这给出的结果与我现在的结果相同,但随后缩小了。如果我用 timeScale 放大,它看起来仍然相同。

tim*_*cov 7

看来该问题是由无效的数据格式引起的(您已在需要数字的地方传递了字符串)。我们将在https://github.com/tradingview/lightweight-charts/issues/315之后在调试模式下警告无效类型。

编辑: 从里程碑 3.2 开始,启用调试模式时会显示警告。

  • 花了 3 个小时试图弄清楚发生了什么,问题是系列时间顺序。多谢 (2认同)