I do multiple ajax calls and I want to show a chart inside the modal window after clicking on symbol name. Right now I can see the chart only for the last symbol. For the first symbol the modal window is empty. I understand that with each ajax call the new chart function overrides the previous result and display new chart in last modal window. But how can I still show all previous charts and display new one in the lsat window?
var stocks = [];
window.onload = function() {
var symbols = ['AAPL', 'MSFT', 'FB'];
symbols.forEach( symbol => makeAjaxCall(symbol));
}
function makeAjaxCall(param){
$.ajax({
type: "GET",
url: "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=" + param + "&interval=1min&apikey=T6UEJETEQRVGDJS9",
success: function(result){
stocks = result;
getPrices();
}
});
}
function getPrices() {
var metaData = stocks["Meta Data"],
timeSeries = stocks["Time Series (1min)"],
sym = metaData["2. Symbol"];
var mdl1 = '<div id="chartModal" class="modal fade" role="dialog">' +
'<div class="modal-dialog modal-lg" role="content">' +
'<div class="modal-content">' +
'<div class="modal-header">' +
'<h4>',
mdl2 = '</h4>' +
'<button type="button1" class="close" data-dismiss="modal">×</button>' +
'</div>' +
'<div class="modal-body" id="modalBody">' +
'<div class = "container-canvas">' +
'<canvas class = "line-chart" width = "400" height = "250"></canvas>' +
'</div>' +
'</div>' +
'</div>' +
'</div>' +
'</div>',
mdl3 = mdl1 + sym + mdl2;
document.getElementById("loadedStocks").innerHTML += '<div class="eachStock"><span><a onclick="showChart()">' + sym + '</a></span></div><div></div>' + mdl3;
var datasetsValues = Object.values(timeSeries),
datasetsValuesReverse = datasetsValues.reverse();
highPrice = Object.values(datasetsValuesReverse).map(o => o["4. close"]),
dateKeys = Object.keys(timeSeries),
datesReverse = dateKeys.reverse();
var ctx = document.getElementById('loadedStocks').querySelectorAll('.line-chart');
var last = ctx[ctx.length - 1];
new Chart(last, {
type: 'line',
data: {
labels: datesReverse,
datasets: [{
data: highPrice,
borderColor: "#FF4500",
label: "Close",
fillset: "#FFDAB9"
}],
pointStyle: "cross",
},
options: {
title: {
display: true,
text: "Stock's close price changes"
}
}
});
}
function showChart() {
$('#chartModal').modal();
}
My html:
<div id='loadedStocks'></div>
Thanks!