Using Angular and chartJS to generate a circular bar graph

I am looking to enhance my standard bar chart by creating rounded thin bars, similar to the image below:

https://i.sstatic.net/uugJV.png

While I have come across examples that suggest creating a new chart, I am unsure of how to implement this within the angular framework. Here is an example for reference.

Current bar graph:

const ctx = this.myChart.nativeElement.getContext('2d');
this.myChartBis = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: this.labels,
    datasets: [{
      label: 'test',
      showLine: true,
      lineTension: 0,
      data: this.data,
      fill: false,
      pointBorderColor: '#cd0037',
      pointBackgroundColor: '#ffffff',
      borderColor: [
        '#747678',
      ],
      borderWidth: 0
    }
    ],
  },
  options: {
    showLines: true,
    legend: {
      display: false,
    },
    responsive: true,
    maintainAspectRatio: true,
    tooltips: {
      yPadding: -2,
      xPadding: 10,
      footerMarginTop: 5,
      titleFontColor: 'rgba(0, 0, 255, 0.0)',
      displayColors: false,
      borderWidth: 1,
      bodyFontSize: 16,
      bodyFontFamily: 'Avenir',
      backgroundColor: '#FFF',
      borderColor: '#d7d7d7',
      bodyFontColor: '#0088ce',
    scales: {
      yAxes: [{
        display: false,
        gridLines: {
          drawBorder: false
        },
        ticks: {
          maxTicksLimit: 5,
          display: true,
          autoSkip: false,
          min: 0,
          max: 100,
        }
      }],
      xAxes: [{
        display: false,
        gridLines: {
          display: false
        },
        ticks: {
          beginAtZero: true,
          autoSkip: false,
          callback: (value: any) => {
              return value;
          }
        }
      }]
    }
  }
});

I am hoping to achieve rounded bars similar to the example image, with the additional condition of displaying a grey dot if no value is present (although the priority is to create rounded bars).

Answer №1

If you're looking to create rounded bar charts, you can utilize the resources available at https://github.com/jedtrow/Chart.js-Rounded-Bar-Charts. Simply include both files Chart.roundedBarCharts.js and Chart.roundedBarChart.min.js in your project folder to get started. Once you've added these files, you can customize your charts using the following code:

var options = {
    cornerRadius: 20,
};

If you're working with small bars like the ones in the photo, consider adding barPercentage to your code. Your updated options object may look like this:

var options = {
    cornerRadius: 20,
    scales: {
        xAxes: [{
            barPercentage: 0.4
        }]
    }
};

Answer №2

A guide on creating a rounded Angular 7 chart.js bar chart

HTML 
    <div style="width: 40%">
        <canvas id="barChart"></canvas>
    </div>

.ts

 chartColors = {
    red: 'rgb(255, 99, 132)',
    orange: 'rgb(255, 159, 64)',
    yellow: 'rgb(255, 205, 86)',
    green: 'rgb(75, 192, 192)',
    blue: 'rgb(54, 162, 235)',
    purple: 'rgb(153, 102, 255)',
    grey: 'rgb(231,233,237)'
  };

ngAfterViewInit() {
    this.canvas = document.getElementById('barChart');
    this.ctx = this.canvas.getContext('2d');
      Chart.elements.Rectangle.prototype.draw = function() {
        // implementation details
      };
    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I only replace every second occurrence in a JS string?

Looking for help with JavaScript: "a a a a".replace(/(^|\s)a(\s|$)/g, '$1') I thought the result would be '', but it's showing 'a a' instead. Can someone explain what I'm missing? To clarify, I want to r ...

Guide to successfully setting up a Json server and ReactJS to run simultaneously on the same port

Currently, I am facing an issue while attempting to run my React application with a JSON server as the backend API. The problem arises when trying to run them on different ports due to a CORS error. I am currently seeking advice and looking for a solutio ...

Steps for setting all textareas on a website to "readonly" mode

My website contains numerous textareas but they currently do not have the read-only attribute. It would be very time-consuming to manually add the readonly attribute to each one. Is there a more efficient method to make all textareas read-only? ...

Endless Loop Issue with jQuery AJAX Request

I've been working on a project where I'm trying to display data from a Mysqli database using jQuery ajax. However, no matter how much I tweak the code, I always end up in an infinite loop. Has anyone else encountered a similar situation when maki ...

Challenges in Ensuring Proper Alignment of Connection Line Between Boxes on Left and Right Sides within a React Component

Currently, I am developing a React component that displays two sets of boxes on the left and right sides of the screen. Users can choose one box from each side and click a "Connect" button to draw a line between them. However, I am encountering an issue wh ...

Master the art of using useMutation from react-query: Learn how to handle error messages and success notifications effectively!

Currently in my Next.js project, I have integrated react-query to retrieve data from MongoDB. Within my form, a POST request is sent using the useMutation hook: /*** components ***/ import {Button, InputGroup} from "../index"; import {useMutatio ...

Unable to locate the required conditional template for the 'mdRadioButton' directive due to the absence of the 'mdRadioGroup' controller

I am currently working on developing a custom directive that will help me display questions within a survey. Given the multiple types of questions I have, I decided to create a single directive and dynamically change its template based on the type of quest ...

Unresolved (waiting for response) unidentified

I encountered a perplexing error in Chrome and I am unable to identify its source: https://i.sstatic.net/f9Blt.png The only clue I have is that after refactoring approximately 10,000 lines of code, this error surfaced. It occurred during the middle of the ...

Animate failed due to the appending and adding of class

$('#clickMe').click(function() { $('#ticketsDemosss').append($('<li>').text('my li goes here')).addClass('fadeIn'); }); <link href="http://s.mlcdn.co/animate.css" rel="stylesheet"/> <script ...

What is the method to insert a new <input> element after the last input field has been filled in

I recently started working on a form using StackBlitz, but I've hit a roadblock and need some guidance on how to proceed. My goal is to achieve a similar effect like the one shown in this gif: https://i.stack.imgur.com/76nsY.gif and I'd like to ...

Encountering an Error during the Installation of MapBox SDK in React Native

After creating a React Native project with Expo using expo init MapTry, I encountered an issue while trying to install the MapBox library. Despite following the installation guide at https://github.com/rnmapbox/maps#Installation, I faced an error message w ...

Troubleshooting: How can I ensure my custom scrollbar updates when jQuery niceselect expands?

I am currently utilizing the jquery plugins mCustomScrollbar and niceselect. However, I have encountered an issue when expanding the niceselect dropdown by clicking on it - the mCustomScrollbar does not update accordingly. I suspect this is due to the abso ...

jQuery is unable to locate elements

Here is a JavaScript code snippet that I have: (function($, undefined) { $("a").click(function(event) { //or another elemtnt alert('Hello!'); }) })(jQuery); Also, here is the link being referenced in the code: <a href="http: ...

What is the best way to implement a conditional check before a directive is executed in Angular, aside from using ng-if

I am facing an issue where the directive is being executed before the ng-if directive. Is there a way to ensure that the ng-if directive is executed before the custom directive? Could I be making a mistake somewhere? Should I consider using a different ...

It appears that when filtering data in Angular from Web Api calls, there is a significant amount of data cleaning required

It appears that the current website utilizes asp.net web forms calling web api, but I am looking to convert it to Angular calling Web api. One thing I have noticed is that I need to clean up the data. How should I approach this? Should I use conditional ...

Button fails to display as intended despite meeting conditions

I'm currently using a formData object with useState(). Whenever a field is updated in the form, I update formData like this: setFormData({...formData, [field.id]: field.value}) My goal is to have the button at the end of the form change once all req ...

Tips for launching a colorbox within a specific div container

I am looking to implement a color box that opens without a popup and only shows/hides inside a specific div. Is it possible to target a div to open the colorbox content? <a href='#myGallery' class='group'>click here</a> &l ...

I am having trouble understanding why my JavaScript code is bypassing the if statements

let emptyErr = [] if (!(req.body.title)) { emptyErr[0] = ('A title must be provided for a post!') } else if (!req.body.category) { emptyErr[1] = ('Please select a category for your post.') } else if (!req.body.content) { ...

Injecting services with an abstract class is a common practice in Angular library modules

In my development workflow, I have established an Angular Component Library that I deploy using NPM (via Nexus) to various similar projects. This library includes a PageComponent, which contains a FooterComponent and a NavbarComponent. Within the NavbarCom ...

The jQuery datetimepicker fails to reflect changes made to the minDate property

I have encountered a datetimepicker object that was previously set up with the following configuration: $('#startDate').datetimepicker({ monthNames: DATE_TIME_MONTHS_NAMES, monthNamesShort: DATE_TIME_MONTHS_NAMES_SHORT, dayNames: DAT ...