简体   繁体   中英

Plotly.js traces not showing

I'm trying to use Plotly.js to create some graphs of historical cryptocurrency prices, and am running into the problem that my data is not showing up on the graph created. I'm building my code from the sample code at https://plot.ly/javascript/ajax-call/ but tooling it for my own data source and a local copy of plotly-latest.min.js. I'm using a small subset of my data and only one trace to get the code functional, and I've placed console.log statements after the processing of the data and the creation of the trace that show me my data is properly formatted judging by the sample code and its dataset. I've set the range of the chart to the range of my data, but I still see nothing on the chart when its created despite modeling it after working sample code. Where am I going wrong?

My code:

<!DOCTYPE html>
<html>
<head>
<script src="plotly-latest.min.js"></script>
</head>
<body>
<div id="myDiv" style="width: 480px; height: 400px;"></div>
<script>
  function makePlot() {
    Plotly.d3.csv("bitcoin.csv", function(data){ processData(data) } );
  }

  function processData(allRows) {

    var Date = [], Open = [], High = [], Low = [], Volume = [], MarketCap = [];

    for (var i=0; i<allRows.length; i++) {
        row = allRows[i];
        tmpDate = row['Date;Open;High;Low;Close;Volume;MarketCap'].split(';')[0]
        Date.unshift( tmpDate.split('/')[2] + '-' + tmpDate.split('/')[1] + '-' + tmpDate.split('/')[0]);
        Open.unshift( row['Date;Open;High;Low;Close;Volume;MarketCap'].split(';')[1]);
        High.unshift( row['Date;Open;High;Low;Close;Volume;MarketCap'].split(';')[2]);
        Low.unshift( row['Date;Open;High;Low;Close;Volume;MarketCap'].split(';')[3]);
        Volume.unshift( row['Date;Open;High;Low;Close;Volume;MarketCap'].split(';')[4]);
        MarketCap.unshift( row['Date;Open;High;Low;Close;Volume;MarketCap'].split(';')[5]);
    };
    makePlotly(Date, Open);
    }

    function makePlotly(Date, Open) {
        var plotDiv = document.getElementById("plot");
        var traces = [{
            Date: Date,
            Open: Open}
        ];
        console.log(traces);

        var layout = {
        xaxis: {
            type: 'date',
            title: 'Date',
            range: ['2017-11-12', '2017-11-22']
        },
        yaxis: {
            title: 'Price (USD)',
            range: [4000, 10000]
        },
        title: 'Cryptocurrency Historical Prices'
        }
        Plotly.newPlot('myDiv', traces, layout);
    }


  makePlot();
</script>

</body>
</html>

bitcoin.csv (1 column)

Date;Open;High;Low;Close;Volume;MarketCap
22/11/2017;8077.95;8302.26;8075.47;8253.55;3633530000;134851000000
21/11/2017;8205.74;8348.66;7762.71;8071.26;4277610000;136967000000
20/11/2017;8039.07;8336.86;7949.36;8200.64;3488450000;134167000000
19/11/2017;7766.03;8101.91;7694.10;8036.49;3149320000;129595000000
18/11/2017;7697.21;7884.99;7463.44;7790.15;3667190000;128425000000
17/11/2017;7853.57;8004.59;7561.09;7708.99;4651670000;131026000000
16/11/2017;7323.24;7967.38;7176.58;7871.69;5123810000;122164000000
15/11/2017;6634.76;7342.25;6634.76;7315.54;4200880000;110667000000
14/11/2017;6561.48;6764.98;6461.75;6635.75;3197110000;109434000000
13/11/2017;5938.25;6811.19;5844.29;6559.49;6263250000;99029000000
12/11/2017;6295.45;6625.05;5519.01;5950.07;8957350000;104980000000

I guess it should be your variable traces causes the problem

var traces = [{
    x: Date, //not Date: Date
    y: Open  //not Open: Open
}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM