TradingView Lightweight Charts在Safari中显示无效日期求助
解决TradingView Lightweight Charts在Safari中显示异常的问题
问题根源
你代码里的localization.timeFormatter实现存在浏览器兼容性问题:
- 直接返回
Date(businessDayOrTimestamp)会在Safari中生成不符合预期的日期字符串,导致图表无法识别时间,出现“invalid date”提示,最终只能显示直线而非动态折线。 - Chrome和Firefox对Date对象的隐式toString处理更宽松,所以能正常渲染图表。
修复方案
1. 修正时间格式化函数
把timeFormatter改成显式生成统一格式的日期字符串,避免依赖浏览器的默认处理逻辑:
localization: { timeFormatter: businessDayOrTimestamp => { // 处理时间戳或BusinessDay对象两种情况 const date = typeof businessDayOrTimestamp === 'number' ? new Date(businessDayOrTimestamp * 1000) // 若为秒级时间戳需转为毫秒 : new Date(businessDayOrTimestamp.year, businessDayOrTimestamp.month - 1, businessDayOrTimestamp.day); // 生成标准化的日期时间字符串 return `${date.getFullYear()}-${String(date.getMonth()+1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')} ${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`; }, },
注意:如果你的时间戳是秒级的,必须乘以1000转为毫秒——Safari对秒级时间戳的解析支持很差。
2. 统一数据时间格式
确保传入图表的dates数组里的时间值格式统一,优先使用毫秒级时间戳或ISO标准日期字符串,避免用浏览器解析差异大的自定义格式。
3. 校验数据绑定逻辑
确认chart_data和dates的对应关系完全正确,没有缺失或格式错误的数据点。
内容的提问来源于stack exchange,提问作者Chiel
相关产品推荐
相关产品推荐

