Chartjs-MongoDB based data is not populating in Chart.js

0👍

Value for data property when dealing with datasets should have the following shape

PropTypes.shape({
  datasets: PropTypes.arrayOf(PropTypes.shape({
    data: PropTypes.array
  }))
})

For example,

    {
            datasets: [
              {
                _id: '238940890234809234',
                data: [10, 20, 10, 20, 10, 20, 10]
              },
              {
                _id: '098340598345839455',
                data: [50, 100, 50, 100, 50, 100, 50]
              }
            ]
   }

You have to plot a different bar graph for each question.

  render() {
        return (
          <div>
            {this.state.chartData.map(data => <Bar key={data._id} data={data} /> }
          </div>
        );
  }

The _id has been included to be used for the component key property.

chartData.push({
  _id: element._id,
  labels: element.answer_set,
  datasets:[{
    data: element.count_set
  }]
});

Leave a comment