Javascript 织女星编辑“;“桌子是空的”;
我试图建立一个基本图,显示各大陆能源的比例。我已经清理了数据并在此处上载了.csv文件: 然而,当我尝试在vega lite编辑器中导入它时,它表示我的csv为空 以下是我编写的代码:Javascript 织女星编辑“;“桌子是空的”;,javascript,vega-lite,Javascript,Vega Lite,我试图建立一个基本图,显示各大陆能源的比例。我已经清理了数据并在此处上载了.csv文件: 然而,当我尝试在vega lite编辑器中导入它时,它表示我的csv为空 以下是我编写的代码: { "data": {"url": "raw.githubusercontent.com/mkeutgen/dataviz_energy/main/prop_source_by_continent_2015"}, "mark"
{
"data": {"url": "raw.githubusercontent.com/mkeutgen/dataviz_energy/main/prop_source_by_continent_2015"},
"mark": "bar",
"encoding": {
"x": {
"field": "continent",
"type": "nominal",
"domain":["Africa","Europe","Asia","North America","South America","Asia"],
"title": "Continent"
},
"y": {
"field":"prop",
"aggregate":"sum",
"type": "quantitative",
"stack": "normalize"
},
"color": {
"field": "share energy",
"type": "nominal",
"scale": {
"domain": ["coal_share_energy","gas_share_energy","nuclear_share_energy", "hydro_share_energy","renewables_share_energy","oil_share_energy"],
"range": ["#e7ba52", "#c7c7c7", "#aec7e8", "#1f77b4", "#9467bd"]
},
"title": "Weather type"
}
}
}
如果查看Javascript控制台,您将看到以下问题:
loader.js:175 GET https://vega.github.io/editor/raw.githubusercontent.com/mkeutgen/dataviz_energy/main/prop_source_by_continent_2015 404
您正试图加载带有相对URL而非绝对URL的数据集,该URL返回404;您可以通过在URL前面添加https://
来解决此问题
此外,由于您的URL不包含文件扩展名,您必须告诉Vega Lite数据是使用格式
规范的CSV格式。通过这两个更改,您的图表可以工作():
{
“数据”:{
“url”:”https://raw.githubusercontent.com/mkeutgen/dataviz_energy/master/prop_source_by_continent_2015",
“格式”:{“类型”:“csv”}
},
“标记”:“条”,
“编码”:{
“x”:{
“场”:“大陆”,
“类型”:“标称”,
“域名”:[“非洲”、“欧洲”、“亚洲”、“北美”、“南美”、“亚洲”],
“标题”:“大陆”
},
“y”:{
“字段”:“道具”,
“总计”:“总和”,
“类型”:“定量”,
“堆栈”:“规范化”
},
“颜色”:{
“场”:“共享能量”,
“类型”:“标称”,
“比例”:{
“领域”:[“煤炭、天然气、核能”,
“水电共享能源”、“可再生能源共享能源”、“石油共享能源”],
“范围”:[“e7ba52”、“c7c7c7”、“aec7e8”、“1f77b4”、“9467bd”]
},
“标题”:“天气类型”
}
}
}
您在
数据
配置中提供的Url还应包含https://
,然后需要指定数据的格式。请参阅以下配置或尝试:
{
"data": {
"url": "https://raw.githubusercontent.com/mkeutgen/dataviz_energy/main/prop_source_by_continent_2015",
"format": {"type": "csv"}
},
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"mark": "bar",
"encoding": {
"x": {
"field": "continent",
"type": "nominal",
"domain": [
"Africa",
"Europe",
"Asia",
"North America",
"South America",
"Asia"
],
"title": "Continent"
},
"y": {
"field": "prop",
"aggregate": "sum",
"type": "quantitative",
"stack": "normalize"
},
"color": {
"field": "share energy",
"type": "nominal",
"scale": {
"domain": [
"coal_share_energy",
"gas_share_energy",
"nuclear_share_energy",
"hydro_share_energy",
"renewables_share_energy",
"oil_share_energy"
],
"range": ["#e7ba52", "#c7c7c7", "#aec7e8", "#1f77b4", "#9467bd"]
},
"title": "Weather type"
}
}
}