Home > database >  Passing values to Bar chart via JSON array in React js
Passing values to Bar chart via JSON array in React js

Time:12-22

Hello I want to pass my dynamically values like months like SEP,OCT,NOV and its sum value about user creation month vise. My chart working fine with giving manually values but when I give values with JSON map then its not working. My code with Manually which is working fine

   data: {
     labels:[ chartUsers[0]?.Month,chartUsers[1]?.Month,chartUsers[2]?.Month ],
     datasets:[
      {
    label: "2022",
    data: [chartUsers[0]?.totalUsers, chartUsers[1]?.totalUsers, chartUsers[2]?.totalUsers],
    maxBarThickness: 15
      }
          ]
     } 

But When I give values by loop then not working. My cod with loop is

    data: {
     labels:[ chartUsers.map(item => { return(item.Month ',')  }) ],
     datasets:[
      {
    label: "2022",
    data: [chartUsers.map(item => { return(item.totalUsers ',')  })],
    maxBarThickness: 15
      }
          ]
     }

Then this code is not working. Actually I want with values with loop. Can anyone help please with thanks

CodePudding user response:

map function already returns array, so you should remove the brackets:

  data: {
    labels: chartUsers.map(item => { return(item.Month)  }) ,
    datasets:[
            {
          label: "2022",
          data: chartUsers.map(item => { return(item.totalUsers)  }),
          maxBarThickness: 15
            }
         ]
    }
  • Related