繁体   English   中英

tradingview 轻量级图表的日期仅显示 1/1/1970 的错误日期

[英]Date of tradingview lightweight chart showing a wrong date of only 1/1/1970

请帮助我进行 tradingview js 图表定制。 我对图表有严重的疑问。 图表未显示从数据库中拉入图表的准确日期在此处输入图像描述

上图是显示所提供数据准确日期的图表

在此处输入图像描述

从数据库中提取的数据给出了错误的日期,从而给出了错误的图表。 我怎样才能更正图表日期

    var investment_charts = LightweightCharts.createChart(document.getElementById('investment_charts'), {
        width: width,
        height: height,
        priceScale: {
            scaleMargins: {
                top: 0.2,
                bottom: 0.2,
            },
        position: 'left',
            borderVisible: true,
        },
        timeScale: {
            borderVisible: false,
        },
        grid: {
            horzLines: {
                color: '#eee',
            },
            vertLines: {
                color: '#ffffff',
            },
        },
        crosshair: {
                horzLine: {
                visible: false,
                labelVisible: false
            },
            vertLine: {
                visible: true,
                style: 0,
                width: 1,
                color: 'rgba(32, 38, 46, 0.1)',
                labelVisible: false,
            }
        },
    });

    // chart.resize(320, 250);
    var series = investment_charts.addAreaSeries({  
        topColor: 'rgba(0, 120, 255, 0.2)', 
        bottomColor: 'rgba(0, 120, 255, 0.0)',
        lineColor: 'rgba(0, 120, 255, 1)',
        lineWidth: 3
    });

    series.setData([
        <?php
        foreach($user->historychart as $key=>$value){
            if($value->investment_amount != null){
                echo '{ time:'.$value->created_at->format('Y-m-d').', value:'. $value->investment_amount.'},';
            }
        }
        ?>

    ]);

    function businessDayToString(businessDay) {
        return new Date(Date.UTC(businessDay.year, businessDay.month - 1, businessDay.day, 0, 0, 0)).toLocaleDateString();
    }

    var toolTipWidth    = 96;
    var toolTipHeight   = 40;
    var toolTipMargin   = 15;
    var priceScaleWidth = 10;

    var toolTip = document.createElement('div');
    toolTip.className = 'floating-tooltip-1';
    document.getElementById('investment_charts').appendChild(toolTip);

    // update tooltip
    investment_charts.subscribeCrosshairMove(function(param) {
        if (!param.time || param.point.x < 0 || param.point.x > width || param.point.y < 0 || param.point.y > height) {
            toolTip.style.display = 'none';
            return;
        }

        var dateStr = LightweightCharts.isBusinessDay(param.time)
            ? businessDayToString(param.time)
            : new Date(param.time * 1000).toLocaleDateString();

        toolTip.style.display = 'block';
        var price = param.seriesPrices.get(series);
        toolTip.innerHTML = '<div style="color: rgba(0, 120, 255, 0.9)">⬤ Naira</div>' +
            '<div style="font-size: 24px; margin: 4px 0px; color: #20262E">' + (Math.round(price * 100) / 100).toFixed(2) + '</div>' +
            '<div>' + dateStr + '</div>';

        var left = param.point.x;


        if (left > width - toolTipWidth - toolTipMargin) {
            left = width - toolTipWidth;
        } else if (left < toolTipWidth / 2) {
        left = priceScaleWidth;
    }

        toolTip.style.left = left + 'px';
        toolTip.style.top = 80 + 'px';
    });








    var charts = LightweightCharts.createChart(document.getElementById('charts'), {
        width: width,
        height: height,
        priceScale: {
            scaleMargins: {
                top: 0.2,
                bottom: 0.2,
            },
        position: 'left',
            borderVisible: true,
        },
        timeScale: {
            borderVisible: false,
        },
        grid: {
            horzLines: {
                color: '#eee',
            },
            vertLines: {
                color: '#ffffff',
            },
        },
        crosshair: {
                horzLine: {
                visible: false,
                labelVisible: false
            },
            vertLine: {
                visible: true,
                style: 0,
                width: 1,
                color: 'rgba(32, 38, 46, 0.1)',
                labelVisible: false,
            }
        },
    });

    // chart.resize(320, 250);
    var seriesa = charts.addAreaSeries({    
        topColor: 'rgba(0, 120, 255, 0.2)', 
        bottomColor: 'rgba(0, 120, 255, 0.0)',
        lineColor: 'rgba(0, 120, 255, 1)',
        lineWidth: 3
    });

    seriesa.setData([
         <?php
        foreach($user->historychart as $key=>$value){
            if($value->daily_profit != null){
                echo '{ time:'.$value->created_at->format('Y-m-d').', value:'. $value->daily_profit.'},';
            }
        }
        ?>

    ]);

    function businessDayToString1(businessDay) {
        return new Date(Date.UTC(businessDay.year, businessDay.month - 1, businessDay.day, 0, 0, 0)).toLocaleDateString();
    }

    var toolTipWidth1    = 96;
    var toolTipHeight1   = 40;
    var toolTipMargin1   = 15;
    var priceScaleWidth1 = 10;

    var toolTip1 = document.createElement('div');
    toolTip1.className = 'floating-tooltip-2';
    document.getElementById('charts').appendChild(toolTip1);

    // update tooltip
    charts.subscribeCrosshairMove(function(param) {
        if (!param.time || param.point.x < 0 || param.point.x > width || param.point.y < 0 || param.point.y > height) {
            toolTip1.style.display = 'none';
            return;
        }

        var dateStr = LightweightCharts.isBusinessDay(param.time)
            ? businessDayToString1(param.time)
            : new Date(param.time * 1000).toLocaleDateString();

        toolTip1.style.display = 'block';
        var price = param.seriesPrices.get(seriesa);
        toolTip1.innerHTML = '<div style="color: rgba(0, 120, 255, 0.9)">⬤ Naira</div>' +
            '<div style="font-size: 24px; margin: 4px 0px; color: #20262E">' + (Math.round(price * 100) / 100).toFixed(2) + '</div>' +
            '<div>' + dateStr + '</div>';

        var left = param.point.x;


        if (left > width - toolTipWidth1 - toolTipMargin1) {
            left = width - toolTipWidth1;
        } else if (left < toolTipWidth1 / 2) {
        left = priceScaleWidth1;
    }

        toolTip1.style.left = left + 'px';
        toolTip1.style.top = 80 + 'px';
    });

我猜问题出在哪里是我从数据库调用数据循环的地方,但我不知道为什么它显示错误的日期。

<?php
    foreach($user->historychart as $key=>$value){
        if($value->investment_amount != null){
            echo '{ time:'.$value->created_at->format('Y-m-d').', value:'. $value->investment_amount.'},';
        }
    }

?>

我通过将日期传递给双引号以将其转换为字符串来解决此问题。

<?php
foreach($user->historychart as $key=>$value){
    if($value->investment_amount != null){
        echo '{ time:"'.$value->created_at->format('Y-m-d').'", value:'. $value->investment_amount.'},';
    }
}

?>

对我来说,我将代码从正常日期更改为时间戳日期

now = datetime.datetime.now()
timenow = str(now.strftime("%m/%d/%Y %H:%M:%S"))                    
        
date = datetime.datetime.strptime(timenow, "%m/%d/%Y %H:%M:%S")
timestamp = datetime.datetime.timestamp(date)
linechart = {
    "time" : timestamp,
    "value" : row[0]
}

例如,如果你从 Binance 获得时间戳,你必须 /1000

01/01/1970 是第一个时间戳 0000000000 的日期,当你的时间戳太大/错误时返回

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM