I have created Barchart component (displays barchart) which is child component. In parent component I am passing props to Barchart component. In Barchart component I am using if/else and to render the Barchart accordingly.
Barchart component:
import React, { Component } from 'react';
class Barchart extends Component {
constructor(props){
super(props);
}
componentDidMount(){
if(this.props.statType === 'batting'){
const data1 = {
labels: ['XS', 'S', 'M', 'L', 'XL', 'XXL', 'XXXL'],
series: [20, 60, 120, 200, 180, 20, 10]
}
const options1 = {
width:300,
height:300,
distributeSeries: true
}
const mychart1 = new Chartist.Bar('.ct-bar-chart', data1,options1);
}else if(this.props.statType === 'bowling'){
const data1 = {
labels: ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
series: [200, 600, 120, 200, 1800, 200, 100]
}
const options1 = {
width:300,
height:300,
distributeSeries: true
}
const mychart1 = new Chartist.Bar('.ct-bar-chart', data1,options1);
}
}
render(){
return(
<div className="ct-bar-chart">
{this.mychart1}
</div>
)}
}
export default Barchart;
Bowling component:
return(
<div><Barchart bowldata={this.props} statType='bowling'/></div>
)
Batting component:
return(
<div><Barchart batdata={this.props} statType='batting'/></div>
)
I am able to see barchart on batting page (i.e batting component) but on bowling page (i.e bowling component) nothing is displayed.
render()method is very large so I cannot put entire code. If possible can you share some working example on jsfiddle or sandbox.