Plotly Javascript 不使用本地 CSV

Plotly Javascript Not Working w Local CSV

我正在练习尝试复制美国 Chorleth 地图 (https://plot.ly/javascript/choropleth-maps/)

当我 运行 他们的代码片段链接到 CSV 文件 (https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv) 时它起作用了。

但是,当我尝试下载该 CSV 文件并运行将其与本地文件路径连接时,该图不起作用。我很难尝试检查,因为 Plotly 没有明确记录所有内容(我知道这在某种程度上使用了 D3 协议)。

我所做的只是更改文件路径。

<head>
<!-- Plotly.js -->
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<!-- Plotly chart will be drawn inside this DIV -->
<div id="myDiv"></div>
<script>
    Plotly.d3.csv("C:/Users/Me/Desktop/JavaScript Practice/2011_us_ag_exports.csv", function (err, rows) {
        function unpack(rows, key) {
            return rows.map(function (row) { return row[key]; });
        }

        var data = [{
            type: 'choropleth',
            locationmode: 'USA-states',
            locations: unpack(rows, 'code'),
            z: unpack(rows, 'total exports'),
            text: unpack(rows, 'state'),
            zmin: 0,
            zmax: 17000,
            colorscale: [
              [0, 'rgb(242,240,247)'], [0.2, 'rgb(218,218,235)'],
              [0.4, 'rgb(188,189,220)'], [0.6, 'rgb(158,154,200)'],
              [0.8, 'rgb(117,107,177)'], [1, 'rgb(84,39,143)']
            ],
            colorbar: {
                title: 'Millions USD',
                thickness: 0.2
            },
            marker: {
                line: {
                    color: 'rgb(255,255,255)',
                    width: 2
                }
            }
        }];

        console.log(data.locations);
        var layout = {
            title: '2011 US Agriculture Exports by State',
            geo: {
                scope: 'usa',
                showlakes: true,
                lakecolor: 'rgb(255,255,255)'
            }
        };
        Plotly.plot(myDiv, data, layout, { showLink: false });
    });
</script>

将您的路径更改为相对路径Plotly.d3.csv("2011_us_ag_exports.csv",......

问题不在于剧情,而是local file access with javascript