dimanche 21 mars 2021

React.js Chart Axios (I want to Chart the data whose userId equals "number".)

I want to Chart the data whose userId equals number. I doubt that I wrote if in the right place. I also doubt the term of the number

example: If number 3, let the data with userId 3 come in the graph.

function Chart() {
  const [data, setData] = useState([])
  const [todos, setTodos] = useState([])

  let title = []
  let id = []
  let number = 3  
  useEffect(() => {
    axios.get('https://jsonplaceholder.typicode.com/todos').then(res => {
      const ipl = res.data
      setTodos(ipl)
      ipl.forEach(user => {
        if (todos.userId == number) {
          title.push(user.completed)
        id.push(user.id)
        }
      })
      setData({
        Data: {
          labels: title,
          datasets: [
            {
              label: 'USER LIST',
              data: id,
              backgroundColor: [
                '#3cb371',
                '#0000FF',
                '#9966FF',
                '#4C4CFF',
                '#00FFFF',
                '#f990a7',
                '#aad2ed',
                '#FF00FF',
                'Blue',
                'Red',
              ],
            },
          ],
        },
      })
    })
  }, [])
 

view of graphics

I would be glad if you help

Aucun commentaire:

Enregistrer un commentaire