I am trying to create a historical stock data graph and have followed a tutorial but that uses a json file that is already set meaning it only goes up to a certain time. I want to use an api so that it can be up until it is currently. How do I make the format the it to make it the same or to make it parse the json and know how to use the data.
This is the code so far, I have defined ticker and TDKEY as variables.
var dps = [];
var chart = new CanvasJS.Chart("chartContainer", {
zoomEnabled: true,
exportEnabled: true,
title: {
text: (ticker+" Stock Price")
},
axisX: {
valueFormatString: "DD MMM"
},
axisY: {
title: "Price",
interval: 5,
prefix: "$"
},
data: [{
type: "candlestick",
name: "Qualcomm Incorporated Stock Price",
showInLegend: true,
yValueFormatString: "$##0.00",
xValueType: "dateTime",
dataPoints: dps
}]
});
$.getJSON("https://api.twelvedata.com/time_series?
start_date=2020-05-06&outputsize=5&symbol="+ticker+"&interval=1day&
apikey="+TDKEY, parseData
);
function parseData(result) {
console.log(result.values);
for (var i = 0; i < result.length; i++) {
dps.push({
x: result[i].datetime,
y: result[i].open
});
}
chart.render();
}
The format from my api call this retrieves historical stock data for aapl:
{
"meta": {
"symbol": "AAPL",
"interval": "1day",
"currency": "USD",
"exchange_timezone": "America/New_York",
"exchange": "NASDAQ",
"mic_code": "XNGS",
"type": "Common Stock"
},
"values": [
{
"datetime": "2023-04-19",
"open": "165.80000",
"high": "168.16000",
"low": "165.53999",
"close": "167.63000",
"volume": "47644400"
},
{
"datetime": "2023-04-18",
"open": "166.10001",
"high": "167.41000",
"low": "165.64999",
"close": "166.47000",
"volume": "49923000"
},
{
"datetime": "2023-04-17",
"open": "165.09000",
"high": "165.39000",
"low": "164.03000",
"close": "165.23000",
"volume": "41516200"
},
{
"datetime": "2023-04-14",
"open": "164.59000",
"high": "166.32001",
"low": "163.82001",
"close": "165.21001",
"volume": "49337200"
},
{
"datetime": "2023-04-13",
"open": "161.63000",
"high": "165.80000",
"low": "161.42000",
"close": "165.56000",
"volume": "68445600"
}
],
"status": "ok"
}
the format of what the [original] (https://canvasjs.com/data/gallery/php/qualcomm-stock-price.json) tutorial had, this is the historical stock data for qualcomm inc in 2017:
`[
{
"x": 1483381800000,
"y": [
65.860001,
66.139999,
64.599998,
65.400002
]
},
{
"x": 1483468200000,
"y": [
65.669998,
65.949997,
65.260002,
65.470001
]
},
{
"x": 1483554600000,
"y": [
65.220001,
65.980003,
65.050003,
65.550003
]
}
]`