如何使用来自 coingeckoapi (json) 的数据的 d3.js 图表

How to use d3.js chart with data from coingeckoapi (json)

我想用 d3.js 创建一个图表。谁能告诉我如何使用 jsondata[timestamp,price]。我从 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] ,

那是我的 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");
      ?>

请将 HTML 行更改为以下

var data = <?php echo history("https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=eur&days=1");?>;

如果 JSON.parse 在 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");?>);

您阅读下文linkhttps://www.geeksforgeeks.org/how-to-pass-variables-and-data-from-php-to-javascript/