I know this question has been asking many times, but I still can't find any solution ! Here is my code : I'm programming an array of cryptocurrency's values but I'm stuck when I'm trying to create graphs.
ajaxGet("https://api.coinmarketcap.com/v1/ticker/?limit=300", function (reponse) {
// Transforme la réponse en tableau d'objets JavaScript
var monnaie = JSON.parse(reponse);
$("#tableBody").empty();
var tr;
function page1(event) {
var timestamp = Math.round(new Date().getTime() / 1000);
console.log(timestamp);
for (var i = 0; i < 50; i++) {
ajaxGet("https://min-api.cryptocompare.com/data/histohour?fsym=" + monnaie[i].symbol + "&tsym=USD&limit=28&aggregate=6&toTs=" + timestamp, function (reponse2) {
var monnaieGraph = JSON.parse(reponse2)['Data'];
var points = '';
var maximum = Math.max.apply(Math,monnaieGraph.map(function(o){return o.high;}))
var minimum = Math.min.apply(Math,monnaieGraph.map(function(o){return o.low;}))
for (var j=0; j<=28; j++){
var moyenne = ((monnaieGraph[j].high)+(monnaieGraph[j].low))/2;
echelle = ((-100/(maximum-minimum))*(moyenne-minimum))+100;
points += (j*12) + ',' + echelle + '\n';
}
});
tr = $('<tr/>');
tr.append("<td>" + monnaie[i].rank + "</td>");
tr.append("<td>" + "<img class=imageCrypto src=logos/" + monnaie[i].symbol + ".png" + " " + "style=width:37.5px; height:37.5px;" + ">" + "<div class=nomCurr><a href=http://www.google.com/" + monnaie[i].id +">" + monnaie[i].name + "</a>" + "<div>" + monnaie[i].symbol + "</div></div></td>");
tr.append("<td>" + formatter.format(monnaie[i].price_btc) + " BTC" + "</td>");
tr.append("<td>" + "$ " + formatter.format(monnaie[i].price_usd) + "</td>");
tr.append("<td>" + "$ " + formatter.format(monnaie[i].market_cap_usd) + "</td>");
tr.append("<td>" + formatter.format(monnaie[i].available_supply) + " " + monnaie[i].symbol + "</td>");
if (Number(monnaie[i].percent_change_24h) > 0) {
tr.append("<td class=pourcentPositif>" + Number(monnaie[i].percent_change_24h) + "%" + "<i class='fa fa-caret-up'" + "</td>");
//tr.append("<i class=fa fa-caret-up>" + "</i>");
}
else {
tr.append("<td class=pourcentNegatif>" + Number(monnaie[i].percent_change_24h) + "%" + "<i class='fa fa-caret-down'" + "</td>");
}
tr.append('<td><svg viewBox="0 0 336 100" width=116 height=35><polyline fill=none stroke=#0074d9 stroke-width=6 points="' + points + '"/></svg></div>');
$('#tableBody').append(tr);
}
}
Everything works ! I got an array with all the values I want.
And as I said, I'm trying to add a column of graphs for each currency ! I tried 3 different code but none of them are working ! Here are the 3 codes with an image of each result:
for (var i = 0; i < 50; i++) {
ajaxGet("https://min-api.cryptocompare.com/data/histohour?fsym=" + monnaie[i].symbol + "&tsym=USD&limit=28&aggregate=6&toTs=" + timestamp, function (reponse2) {
var monnaieGraph = JSON.parse(reponse2)['Data'];
var points = '';
var maximum = Math.max.apply(Math,monnaieGraph.map(function(o){return o.high;}))
var minimum = Math.min.apply(Math,monnaieGraph.map(function(o){return o.low;}))
for (var j=0; j<=28; j++){
var moyenne = ((monnaieGraph[j].high)+(monnaieGraph[j].low))/2;
echelle = ((-100/(maximum-minimum))*(moyenne-minimum))+100;
points += (j*12) + ',' + echelle + '\n';
}
});
tr = $('<tr/>');
tr.append("<td>" + monnaie[i].rank + "</td>");
tr.append("<td>" + "<img class=imageCrypto src=logos/" + monnaie[i].symbol + ".png" + " " + "style=width:37.5px; height:37.5px;" + ">" + "<div class=nomCurr><a href=http://www.google.com/" + monnaie[i].id +">" + monnaie[i].name + "</a>" + "<div>" + monnaie[i].symbol + "</div></div></td>");
tr.append("<td>" + formatter.format(monnaie[i].price_btc) + " BTC" + "</td>");
tr.append("<td>" + "$ " + formatter.format(monnaie[i].price_usd) + "</td>");
tr.append("<td>" + "$ " + formatter.format(monnaie[i].market_cap_usd) + "</td>");
tr.append("<td>" + formatter.format(monnaie[i].available_supply) + " " + monnaie[i].symbol + "</td>");
if (Number(monnaie[i].percent_change_24h) > 0) {
tr.append("<td class=pourcentPositif>" + Number(monnaie[i].percent_change_24h) + "%" + "<i class='fa fa-caret-up'" + "</td>");
//tr.append("<i class=fa fa-caret-up>" + "</i>");
}
else {
tr.append("<td class=pourcentNegatif>" + Number(monnaie[i].percent_change_24h) + "%" + "<i class='fa fa-caret-down'" + "</td>");
}
tr.append('<td><svg viewBox="0 0 336 100" width=116 height=35><polyline fill=none stroke=#0074d9 stroke-width=6 points="' + points + '"/></svg></div>');
$('#tableBody').append(tr);
}
Inside my "for" loop, I added the ajaxGet, in which I create a variable "points" which get the values from the API scaled for the graph.
This code doesn't work, I got the error: "points is not defined".
Here is my 2nd try :
for (var i = 0; i < 50; i++) {
tr = $('<tr/>');
tr.append("<td>" + monnaie[i].rank + "</td>");
tr.append("<td>" + "<img class=imageCrypto src=logos/" + monnaie[i].symbol + ".png" + " " + "style=width:37.5px; height:37.5px;" + ">" + "<div class=nomCurr><a href=http://www.google.com/" + monnaie[i].id +">" + monnaie[i].name + "</a>" + "<div>" + monnaie[i].symbol + "</div></div></td>");
tr.append("<td>" + formatter.format(monnaie[i].price_btc) + " BTC" + "</td>");
tr.append("<td>" + "$ " + formatter.format(monnaie[i].price_usd) + "</td>");
tr.append("<td>" + "$ " + formatter.format(monnaie[i].market_cap_usd) + "</td>");
tr.append("<td>" + formatter.format(monnaie[i].available_supply) + " " + monnaie[i].symbol + "</td>");
if (Number(monnaie[i].percent_change_24h) > 0) {
tr.append("<td class=pourcentPositif>" + Number(monnaie[i].percent_change_24h) + "%" + "<i class='fa fa-caret-up'" + "</td>");
//tr.append("<i class=fa fa-caret-up>" + "</i>");
}
else {
tr.append("<td class=pourcentNegatif>" + Number(monnaie[i].percent_change_24h) + "%" + "<i class='fa fa-caret-down'" + "</td>");
}
ajaxGet("https://min-api.cryptocompare.com/data/histohour?fsym=" + monnaie[i].symbol + "&tsym=USD&limit=28&aggregate=6&toTs=" + timestamp, function (reponse2) {
var monnaieGraph = JSON.parse(reponse2)['Data'];
var points = '';
var maximum = Math.max.apply(Math,monnaieGraph.map(function(o){return o.high;}))
var minimum = Math.min.apply(Math,monnaieGraph.map(function(o){return o.low;}))
for (var j=0; j<=28; j++){
var moyenne = ((monnaieGraph[j].high)+(monnaieGraph[j].low))/2;
echelle = ((-100/(maximum-minimum))*(moyenne-minimum))+100;
points += (j*12) + ',' + echelle + '\n';
}
tr.append('<td><svg viewBox="0 0 336 100" width=116 height=35><polyline fill=none stroke=#0074d9 stroke-width=6 points="' + points + '"/></svg></div>');
});
$('#tableBody').append(tr);
}
This time, I added the creation of the column inside the ajaxGet. The graphs are ok, but they only appear as a row, on the last row !
And here is my last try:
for (var i = 0; i < 50; i++) {
ajaxGet("https://min-api.cryptocompare.com/data/histohour?fsym=" + monnaie[i].symbol + "&tsym=USD&limit=28&aggregate=6&toTs=" + timestamp, function (reponse2) {
var monnaieGraph = JSON.parse(reponse2)['Data'];
var points = '';
var maximum = Math.max.apply(Math,monnaieGraph.map(function(o){return o.high;}))
var minimum = Math.min.apply(Math,monnaieGraph.map(function(o){return o.low;}))
for (var j=0; j<=28; j++){
var moyenne = ((monnaieGraph[j].high)+(monnaieGraph[j].low))/2;
echelle = ((-100/(maximum-minimum))*(moyenne-minimum))+100;
points += (j*12) + ',' + echelle + '\n';
}
tr = $('<tr/>');
tr.append("<td>" + monnaie[i].rank + "</td>");
tr.append("<td>" + "<img class=imageCrypto src=logos/" + monnaie[i].symbol + ".png" + " " + "style=width:37.5px; height:37.5px;" + ">" + "<div class=nomCurr><a href=http://www.google.com/" + monnaie[i].id +">" + monnaie[i].name + "</a>" + "<div>" + monnaie[i].symbol + "</div></div></td>");
tr.append("<td>" + formatter.format(monnaie[i].price_btc) + " BTC" + "</td>");
tr.append("<td>" + "$ " + formatter.format(monnaie[i].price_usd) + "</td>");
tr.append("<td>" + "$ " + formatter.format(monnaie[i].market_cap_usd) + "</td>");
tr.append("<td>" + formatter.format(monnaie[i].available_supply) + " " + monnaie[i].symbol + "</td>");
if (Number(monnaie[i].percent_change_24h) > 0) {
tr.append("<td class=pourcentPositif>" + Number(monnaie[i].percent_change_24h) + "%" + "<i class='fa fa-caret-up'" + "</td>");
//tr.append("<i class=fa fa-caret-up>" + "</i>");
}
else {
tr.append("<td class=pourcentNegatif>" + Number(monnaie[i].percent_change_24h) + "%" + "<i class='fa fa-caret-down'" + "</td>");
}
tr.append('<td><svg viewBox="0 0 336 100" width=116 height=35><polyline fill=none stroke=#0074d9 stroke-width=6 points="' + points + '"/></svg></div>');
$('#tableBody').append(tr);
});
}
I added the declaration of the whole table, inside the ajaxGet ! I don't understand the result : This time, the graphs are what I want, but the rest of the array (every columns in every rows) have the value of the 51th coin (but the "graphs" column works), while my variable i is supposed to be 0
Hope my problem is clear, I'm sorry I can't post images... Thank you for your time !