Javascript 使用fetch获取图表JS的JSON数据

Javascript 使用fetch获取图表JS的JSON数据,javascript,json,chart.js,fetch,Javascript,Json,Chart.js,Fetch,我不熟悉异步编码,并且正在疯狂地尝试制定一个解决方案,将数据从嵌套的json数组文件加载到图表JS中。通过梳理所有不同的示例,我可以获取fetch调用中的数据,但无法将其导出到全局对象中,因为它是异步的。目标是在图表配置中引用json字段和值。这是当前的迭代 var jsonData= fetch('dataSummary.json') .然后(状态) .then(json) .then(功能(数据){ log('Request successed with JSON response',da

我不熟悉异步编码,并且正在疯狂地尝试制定一个解决方案,将数据从嵌套的json数组文件加载到图表JS中。通过梳理所有不同的示例,我可以获取fetch调用中的数据,但无法将其导出到全局对象中,因为它是异步的。目标是在图表配置中引用json字段和值。这是当前的迭代

var jsonData=
fetch('dataSummary.json')
.然后(状态)
.then(json)
.then(功能(数据){
log('Request successed with JSON response',data[0]);
返回数据[0]
}).catch(函数(错误){
日志('请求失败',错误);
});
var chartData={jsonData:{};
Promise.all([jsonData]).then(函数(值){
chartData=值[0];
console.log(图表数据);
返回图表数据[0];
});
console.log(Object.keys(chartData))
关键是在数据准备就绪时更新(或绘制)图表

fetch('dataSummary.json')
    .then(res => json)
    .then(function(data) {
        console.log('Request succeeded with JSON response', data[0]);
        // rather the returning data use it to update/populate the chart
    }).catch(function(error) {
        console.log('Request failed', error);
    });

谢谢,我将图表定义移到了fetch中,它可以正常工作。