我正在尝试使用.map()
javascript从 JSON 数据中提取数据。数据提取需要注入stacked bar chart
.
我正在尝试与这个问题类似:Chart.js 中的堆叠条形图和 JSON 数据,但在我的情况下,我希望每个季节的总运行次数为条形图的高度,而堆叠条形图的总高度将分为 3堆栈。
检查堆积条形图:https : //gionkunz.github.io/chartist-js/examples.html#stacked-bar
我的 JSON 数据:
const batdata=[
{"season":2008,
"runs":25,
"tournamentName":"XYZ"
},
{"season":2008,
"runs":125,
"tournamentName":"ABC"
},
{"season":2008,
"runs":825,
"tournamentName":"PQR"
},
{"season":2009,
"runs":425,
"tournamentName":"XYZ"
},
{"season":2009,
"runs":255,
"tournamentName":"ABC"
},
{"season":2010,
"runs":275,
"tournamentName":"XYZ"
},
{"season":2010,
"runs":675,
"tournamentName":"ABC"
}
];
export default batdata;
在chart.js中:
import React, { Component } from 'react';
import batdata from './batdata';
const uniq = a => [...new Set(a)]
const uniqueseason = uniq(
batdata.map( newdata => newdata.season)
)
const runsperseason = batdata.map( newdata => {
})
class Chart extends Component {
render(){
return(
<div>
</div>
)}
}
export default Chart;
我正在使用.map()
获得独特的赛季,然后再次嵌套map()
以获得特定赛季的特定比赛名称的运行。如何解析数据外部 json 文件。最终数据应如下所示:
labels = [2008,2009 2010]
Chartdata = [
[25, 125, 825] <--- Height of barchart = 825+25+125 and it will have 3 stacks because there are 3 tournamentName
[425, 255, 0] <-- tournamentName: PQR not present in season 2009 so 3rd element zero
[275, 675, 0] <---tournamentName: PQR not present in season 2010 so 3rd element zero
]
我想提取数据并将其以3个数组的形式存储在3个数据中:
1)Runs in season 2008 for tournamentName: XYZ,ABC, PQR
2)Runs in season 2009 for tournamentName: XYZ, ABC, PQR
3)Runs in season 2010 for tournamentName: XYZ, ABC, PQR
将数据存储在 3 个不同的数组中后,我们可以使用解构运算符...
来解包数组元素并填充图表数据。