Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

280
Vistas
Counting string to plot a graph using chart.js and angular

I am trying to create a bar chart in angular using chart.js and angular. I want to display the disaters vs area. with the type of disaster ( earthquake, hurricane, storm...etc) on the x-axis and area (as in location) on the y-axis. However, the area data is string and does not work when trying to plot a bar graph as it requires numbers. I am reading the data as follows:

ngOnInit(): void {
this.chartService.noOfEqu().subscribe((res) => {
      let area = res.map((res) => res.area);
      let disaster = res.map((res) => res.disasterNature);
new Chart('disasterCanvas', {
        type: 'bar',

        data: {
          labels: disaster,
          datasets: [
            {
              data: area,
              label: 'area',
              backgroundColor: '#A121D5',
            },
          ],
        },
        options: {
          plugins: {
            legend: {
              display: true,
            },
          },
          scales: {
            x: {
              ticks: {
                display: true,
              },
            },
            y: {
              ticks: {
                display: true,
              },
            },
          },
        },
      });
    });
  }
}

Where let area = res.map((res) => res.area); is the area variable. I want to know what is the best way to structure the data to be entered into the graph. The disaster data that is logged is:

disaster

and the area data :

area

How can i structure the area such that it counts how much areas that a disaster has occured and plot it on the bar chart? Thank you in advance...if there is anything i can clarify please let me know in the comments

Also is there another way to display this data besides tallying the string data?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to add a labels array to the scale and set the type to category to use string data:

const options = {
  type: 'bar',
  data: {
    labels: ["Earthquake", "Tornade", "Flood"],
    datasets: [{
      label: '# of Votes',
      data: ['Paris', 'Berlin', 'London'],
      backgroundColor: 'pink'
    }]
  },
  options: {
    scales: {
      y: {
        type: 'category',
        reverse: true,
        labels: ['', 'London', 'Paris', 'Berlin']
      }
    }
  }
}

const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.js"></script>
</body>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda