[英]How to use d3.js chart with data from coingeckoapi (json)
I'd like to create a chart with d3.js. Could someone tell me how to work with the jsondata[timestamp,price].我想用 d3.js 创建一个图表。有人可以告诉我如何使用 jsondata[timestamp,price]。 I get the data from coingeckoAPI and it looks like that:我从 coingeckoAPI 获取数据,它看起来像这样:
{"prices":[[1649667011317,38721.07051511258],[1649667168163,38726.36780848938],[1649667622285,38750.30201896313],[1649667926510,38715.36968177588],[1649668246571,38705.597785934006],[1649668432287,38690.34512542588],[1649668897715,38620.57305041674],[1649669050953,38613.10740572825],[1649669284813,38568.32503183882],[1649669697192,38518.76279413846],[1649669982557,38491.21941297744],[1649670258121,38460.7219359208],[1649670606639,38417.38270710583],[1649670978757,38349.85248699985],[1649671244134,38336.437837571124], {"prices":[[1649667011317,38721.07051511258],[1649667168163,38726.36780848938],[1649667622285,38750.30201896313],[1649667926510,38715.36968177588],[1649668246571,38705.597785934006],[1649668432287,38690.34512542588],[1649668897715,38620.57305041674],[1649669050953 ,38613.10740572825],[1649669284813,38568.32503183882],[1649669697192,38518.76279413846],[1649669982557,38491.21941297744],[1649670258121,38460.7219359208],[1649670606639,38417.38270710583],[1649670978757,38349.85248699985],[1649671244134,38336.437837571124],
thats my Php Code:那是我的 Php 代码:
function history($url) {
$ch= curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$resp = curl_exec($ch);
if ($e= curl_error($ch)) {
echo $e;
}
else {
return $resp;
}
curl_close($ch);
}
and HTML:
<?php echo history("https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=eur&days=1");
?>
Please change HTML line to below请将HTML线路改成下面
var data = <?php echo history("https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=eur&days=1");?>;
You may use JSON.parse if it gives any parsing error in javascript code.如果 javascript 代码中出现任何解析错误,您可以使用 JSON.parse。
var data = JSON.parse(<?php echo history("https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=eur&days=1");?>);
You read below link https://www.geeksforgeeks.org/how-to-pass-variables-and-data-from-php-to-javascript/您阅读以下链接https://www.geeksforgeeks.org/how-to-pass-variables-and-data-from-php-to-javascript/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.