Reputation: 1953
I am trying to display Linechart using chartist.js. I have used reduce()
to extract data from props
. I have keys as year
and values as average value
in averages object. But I am not able to display the line chart as I am not able to see any lines on chart I am getting type error. I have created React component and inside componentDidMount()
I have added my line chart code. I think the issue is related to javascript/reactjs and not chartist.js
Code:
class Linechart extends Component {
constructor(props){
super(props);
}
componentDidMount(){
const totals = this.props.bowldata.reduce((a, {season, econ}) => {
if (!a[season]) {
a[season] = {sum: 0, count: 0};
}
if(econ !== null){
a[season].count++;
a[season].sum += parseInt(econ);
}
return a;
}, {});
const averages = {};
Object.keys(totals).forEach(key => {
averages[key] = totals[key].sum / totals[key].count;
});
console.log(averages);
const series = Object.values(averages);
const labels = Object.keys(averages);
const data = {
series:series,
labels:labels
}
this.linechart = new Chartist.Line('.ct-line-chart', data, options);
}
render(){
return(
<div>
<div className="col-md-3 col-xs-6 ct-line-chart">
{this.linechart}
</div>
</div>
)}
}
export default Linechart ;
Using .forEach()
I am trying to create new array of objects with sum/count
as a value
and key
as year
.
Upvotes: 2
Views: 3999