Home > Back-end >  How to use d3.js chart with data from coingeckoapi (json)
How to use d3.js chart with data from coingeckoapi (json)

Time:04-19

I'd like to create a chart with d3.js. Could someone tell me how to work with the jsondata[timestamp,price]. I get the data from coingeckoAPI and it looks like that:

{"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:

 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");
      ?>

CodePudding user response:

Please change HTML line to below

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.

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/

  • Related