Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
How can I arrange according to month using chart Js?

I'm using chart js, I tried to edit the dates created from my MongoDB to check if it will represent according to the given month. As shown in the image, the line is not arranged according to its given month index.

How can I arrange the line chart according to the month created? Is the logic I put in the useEffect wrong?

const Home = () => {
  const [userStats, setUserStats] = useState([])

  const MONTHS = useMemo(
    () => [
      'Jan',
      'Feb',
      'Mar',
      'Apr',
      'May',
      'Jun',
      'Jul',
      'Agu',
      'Sep',
      'Oct',
      'Nov',
      'Dec',
    ],
    []
  )

  useEffect(() => {
    const getStats = async () => {
      try {
        const res = await userRequest.get('/user/stats')
        res.data.map((item) =>
          setUserStats((prev) => [
            ...prev,
            { name: MONTHS[item._id - 1], 'Active User': item.total },
          ])
        )
      } catch {}
    }
    getStats()
  }, [MONTHS])

  console.log(userStats)

  return (
    <div className="home">
      <Featured />
      <Chart
        data={userStats}
        title="user Analaytics"
        grid
        dataKey="Active User"
      />
      <div className="homeWidgets">
        <Widget />
        <WidgetLg />
      </div>
    </div>
  )
}

export default Home

Actual Picture

and each time I refresh the page, the arrangement of it gets rendered random.

2nd Render Image

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Okay I found it, I used sort method, and grabbed the id of it

 const list = res.data.sort((a, b) => {
      return a._id - b._id
    })
    list.map((item) =>
      setUserStats((prev) => [
        ...prev,
        { name: MONTHS[item._id - 1], 'Active User': item.total },
      ])
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!