I’m working with TradingView.js
https://github.com/tradingview/lightweight-charts
https://unpkg.com/lightweight-charts/dist/lightweight-charts.standalone.production.js
When I format data with yyyy-mm-dd format it works fine. When I try to use UNIX time, which is supported in their documentation, the data doesn’t format. Can’t figure out why
const chart = LightweightCharts.createChart(document.getElementById('chart'), {
width: 600,
height: 300,
timeScale: {
timeVisible: true,
secondsVisible: true,
},
});
const lineSeries = chart.addLineSeries();
var examplePrices = [{ 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 }];
var fiveDayPrices = [{"time":1725463800,"value":408.76},{"time":1725462000,"value":409.60}];
//lineSeries.setData(examplePrices);
lineSeries.setData(fiveDayPrices);
Here is how I’m generating the fiveDayPrices data with PHP
<?php
$fiveDayPricesAndTimes = array();
foreach($fiveDayStockPrices as $index => $dailyPriceInterval){
$priceTime = strtotime($dailyPriceInterval["datetime"]); // This is already in seconds
$priceValue = $dailyPriceInterval["close"];
$fiveDayPricesAndTimes[$index]["time"] = intval($priceTime); // Now in seconds
$fiveDayPricesAndTimes[$index]["value"] = floatval($priceValue);
}
$fiveDayPricesAndTimes = json_encode($fiveDayPricesAndTimes, true);
?>