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

279
Views
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 answers
Answer question

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 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!