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

263
Views
Chart.js suma y valores según la unidad de tiempo

Nuevo en Chart.js, me preguntaba si hay un método integrado para sumar valores y dependiendo de la unidad de tiempo.
Preguntando aquí después de mucha búsqueda en los documentos.

Por ahora, la barra de marzo muestra 20 y la barra de junio muestra 10.
Quiero tener 25 para marzo y 15 para junio, para sumar y para el mismo mes.

 const ctx = document.getElementById("tests-chart") const testsChart = new Chart(ctx, { type: "bar", data: { datasets: [ { label: 'Dataset 1', data: [ { x: "24/03/2022", y: 20 }, { x: "25/03/2022", y: 5 }, { x: "24/06/2022", y: 10 }, { x: "25/06/2022", y: 5 }, ], backgroundColor: 'rgb(255, 99, 132)', }, ], }, options: { scales: { x: { type: "time", time: { parser: "dd/MM/yyyy", unit: "month", // Luxon format string tooltipFormat: "MM", round: 'month' }, }, }, }, })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/luxon@2.3.1/build/global/luxon.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-luxon@1.1.0/dist/chartjs-adapter-luxon.min.js"></script> <div> <canvas id="tests-chart"></canvas> </div>

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

0

Puede manipular su conjunto de datos como se menciona a continuación y lograr el resultado esperado.

 const ctx = document.getElementById("tests-chart"); const data =[ { x: "24/03/2022", y: 20 }, { x: "25/03/2022", y: 5 }, { x: "24/06/2022", y: 10 }, { x: "25/06/2022", y: 5 } ]; let updatedData =[]; let tempDateCollection =[]; data.map((yData , index)=> { const formatedDate = moment(yData.x, "DD/MM/YYYY").format('MMM/YYYY'); if(tempDateCollection.includes(formatedDate)){ const index = tempDateCollection.indexOf(formatedDate); const element = updatedData[index]; updatedData[index] = {...element, y: element.y + yData.y} } else{ tempDateCollection.push(formatedDate); updatedData.push(yData); } }) const testsChart = new Chart(ctx, { type: "bar", data: { datasets: [ { label: 'Dataset 1', data:updatedData, backgroundColor: 'rgb(255, 99, 132)', }, ], }, options: { scales: { x: { type: "time", time: { parser: "dd/MM/yyyy", unit: "month", // Luxon format string tooltipFormat: "MM", round: 'month' }, }, }, }, })
 <script type = "text/JavaScript" src = " https://MomentJS.com/downloads/moment.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/luxon@2.3.1/build/global/luxon.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-luxon@1.1.0/dist/chartjs-adapter-luxon.min.js"></script> <div> <canvas id="tests-chart"></canvas> </div>

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!