Papaparse, ReferenceError: variable is not defined
Papaparse, ReferenceError: variable is not defined
我正在制作 Geocharts 地图,该地图使用 PapaParse 导入一些数据。我遇到的问题是 temp = CovidData.data.find(element[1] === countries[c]);
.
行的 ReferenceError
我正在使用 PapaParse 库来解析我从 GitHub 抓取的 csv 文件。
google.charts.load('current', {
'packages':['geochart'],
});
google.charts.setOnLoadCallback(drawRegionsMap);
function drawRegionsMap() {
Papa.parse('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv',{ download: true,
complete: function(results) {var CovidData=results;}});
countries = ["France","Germany", "United Kingdom", "US", "Italy"];
var mapData = [{label: 'Country', type: 'string'}, 'Density',{label: 'Infected', type: 'number'}];
for(c in countries) {
var temp = CovidData.data.find(element => element[0] === "" && element[1] === countries[c]);
mapData.push([countries[c],1,temp[temp.length-1]);
}
var data = google.visualization.arrayToDataTable(mapData);
var options = {
colorAxis: {colors: ['green','blue', 'red','black'], maxValue: 1},
datalessRegionColor: 'grey',
defaultColor: '#f5f5f5',
callback: drawRegionsMap,
};
var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));
chart.draw(data);
}
我能够使用 JS Promise 解决这个问题,因此它会等待所有数据被获取并且异步函数到 return,然后再继续。它看起来像这样:
google.charts.load('current', {
'packages':['geochart'],
});
urls = ['https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv']
Promise.all(
urls
.map(
url=>
new Promise(
(resolve,reject)=>
Papa.parse(url,
{
download: true,
complete:resolve,//resolve the promise when complete
error:reject//reject the promise if there is an error
}
)
)
))
.then(
function (results) {
var mapData = [[{label: 'Country', type: 'string'}, 'Density',{label: 'Infected', type: 'number'},{type: 'string', role: 'tooltip', 'p': {'html': true}}]];
// ...add data
function drawRegionsMap() {
CovidInfected= results[0];
}
}
var data = google.visualization.arrayToDataTable(mapData);
// var options = ...;
var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));
chart.draw(data, {
tooltip: { isHtml: true, trigger: 'selection'}
});
}
google.charts.setOnLoadCallback(drawRegionsMap);
)
.catch(
err=>console.warn("Something went wrong:",err)
)
我正在制作 Geocharts 地图,该地图使用 PapaParse 导入一些数据。我遇到的问题是 temp = CovidData.data.find(element[1] === countries[c]);
.
我正在使用 PapaParse 库来解析我从 GitHub 抓取的 csv 文件。
google.charts.load('current', {
'packages':['geochart'],
});
google.charts.setOnLoadCallback(drawRegionsMap);
function drawRegionsMap() {
Papa.parse('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv',{ download: true,
complete: function(results) {var CovidData=results;}});
countries = ["France","Germany", "United Kingdom", "US", "Italy"];
var mapData = [{label: 'Country', type: 'string'}, 'Density',{label: 'Infected', type: 'number'}];
for(c in countries) {
var temp = CovidData.data.find(element => element[0] === "" && element[1] === countries[c]);
mapData.push([countries[c],1,temp[temp.length-1]);
}
var data = google.visualization.arrayToDataTable(mapData);
var options = {
colorAxis: {colors: ['green','blue', 'red','black'], maxValue: 1},
datalessRegionColor: 'grey',
defaultColor: '#f5f5f5',
callback: drawRegionsMap,
};
var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));
chart.draw(data);
}
我能够使用 JS Promise 解决这个问题,因此它会等待所有数据被获取并且异步函数到 return,然后再继续。它看起来像这样:
google.charts.load('current', {
'packages':['geochart'],
});
urls = ['https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv']
Promise.all(
urls
.map(
url=>
new Promise(
(resolve,reject)=>
Papa.parse(url,
{
download: true,
complete:resolve,//resolve the promise when complete
error:reject//reject the promise if there is an error
}
)
)
))
.then(
function (results) {
var mapData = [[{label: 'Country', type: 'string'}, 'Density',{label: 'Infected', type: 'number'},{type: 'string', role: 'tooltip', 'p': {'html': true}}]];
// ...add data
function drawRegionsMap() {
CovidInfected= results[0];
}
}
var data = google.visualization.arrayToDataTable(mapData);
// var options = ...;
var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));
chart.draw(data, {
tooltip: { isHtml: true, trigger: 'selection'}
});
}
google.charts.setOnLoadCallback(drawRegionsMap);
)
.catch(
err=>console.warn("Something went wrong:",err)
)