Alter the color of the text labels on the bar graph using the chartsjs-plugin-data-labels

The bar chart below shows the number of students. Here are the custom options for formatting it:

  public chartOptions = {
    responsive: true,
    maintainAspectRatio: false,
    legend: {
      display: true,
      labels: {
        fontColor: "black",
        fontSize: 12,
      },
    },
    scales: {
      yAxes: [
        {
          display: true,
          stacked: false,
          scaleLabel: {
            display: true,
            labelString: "Number of Students",
          },
          ticks: {
            stepSize: 5,
            min: 0,
            max: 30,
            fontSize: 12,
            fontColor: "black",
          },
        },
      ],
      xAxes: [
        {
          display: true,
          stacked: false,
          ticks: {
            fontSize: 12,
            fontColor: "black",
            precision: 0,
          },
        },
      ],
    },
  };

This is how you embed the chart in your HTML file:

          <canvas
            baseChart
            [datasets]="chartData"
            [labels]="chartLabels"
            [options]="chartOptions"
            [colors]="chartColors"
            [plugins]="[]"
            [legend]="true"
            [chartType]="'bar'"
            height="300px"
          >
          </canvas>

I'm having trouble changing the color of the data labels to white as shown in the picture below. Any suggestions on how to achieve that?

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

UPDATE:

Even with updated chartOptions, the color of the data labels remains unchanged:

  chartOptions = {
    responsive: true,
    maintainAspectRatio: false,
    legend: {
      display: true,
    },
    scales: {
      yAxes: [
        {
          display: true,
          stacked: false,
          scaleLabel: {
            display: true,
            labelString: "Number of Students",
          },
          ticks: {
            stepSize: 5,
            min: 0,
            max: 30,
            fontSize: 12,
            fontColor: "black",
          },
        },
      ],
      xAxes: [
        {
          display: true,
          stacked: false,
          ticks: {
            fontSize: 12,
            fontColor: "black",
            precision: 0,
          },
        },
      ],
    },
    plugins: {
      labels: {
        fontColor: "white",
        fontSize: 12,
      },
    },
  };

Answer №1

In my opinion, the problem arises when inserting the baels plugin configuration within the legend configuration.

The correct structure should be as follows:

  options: {
  ...
    plugins: {
      labels: {
        fontColor: "black",
        fontSize: 12,
      },
    }
  ...
  }

Answer №2

After troubleshooting, I discovered that the issue lay with the 'chartsjs-plugin-data-labels' plugin that I was using. I managed to resolve it by including the following code within the datasets array.

        dataLabels: {
          colors: ["white"],
        },

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

Is there a sorting data accessor available in swimlane ngx-datatable similar to the one found in the angular material table?

I am currently working on implementing ngx-datatable in my code. The row data consists of key value pairs, with the value being of type object. Unfortunately, the default sorting is not functioning as expected. In Angular Mat-Table, there is a property cal ...

React 15 is found to be incompatible with React-redux due to certain compatibility

I'm currently working on upgrading to the newest version of [email protected] in my project, which also utilizes the react-redux@^4.4.0 package. However, I encountered issues when attempting to follow the upgrade instructions provided in the documenta ...

What can be done to address the problem of background-image opacity making text difficult to read?

I am having an issue with the background. I am aiming for something similar to what is shown in screenshot 1, but instead, I am getting the result shown in screenshot 2. Where could the problem lie? html, body, .bg-for-1 { height: 100%; } .bg-for-1 ...

"Migration Troubles: Migrating from Bing Map V7 to V8 and Facing Script

After searching for a solution to this issue, I still can't figure out why it's not working even though I've seen similar topics. The code I'm using is quite basic and here's an example of what's causing the problem: main.htm ...

Setting the css height to 100% won't be effective if the elements are floated either to the right or left

I'm currently working on a web project and I need to create an HTML page. I'd like to adjust the height of an element to a percentage to ensure it fits the page well. However, when I try to use the float property in CSS and set it as follows: bo ...

Do HTML attributes that are customized without the data-* prefix considered valid?

When it comes to AngularJS, the data- prefix is an option but not a requirement in their examples. Should I decide to include the data- prefix in my project, and what are the benefits of doing so? Will my HTML still be valid even if I choose not to make u ...

Displaying the :before attribute while concealing the element solely with CSS

I have encountered a situation where I am unable to edit the HTML of a document, specifically a payment page on Shopify, for security reasons. Unfortunately, editing JavaScript is also restricted in this scenario. However, there is a workaround available ...

What is the best way to display DT elements next to each other?

Here is a link to the code snippet: http://jsfiddle.net/ZcdkT/1/ The code renders as: DT-nameA DD-definitionA 1 DT-nameB DD-definitionB 1 DD-definitionB 2 I would like it to be formatted like this: DT-nameA DT-nameB DD-definitionA 1 ...

What is causing this JSON to malfunction in Internet Explorer?

Everything is functioning well on Chrome and other browsers except for IE. Below is an example to illustrate: (specifically referring to IE 8, unsure about compatibility with IE 9) Upon running the JSON request, I encountered an error stating "Object exp ...

Set a variable in MongoDB to retrieve the results of the find() method and convert them

Struggling to make this code function properly var data = db.collection('mycollection', function(er, collection) { return collection.find().toArray(); }); The variable data is not returning an array as expected. Uncle ...

Display the current date in a formatted way using Angular 2

I am struggling with displaying the current date in the format I desire. The code snippet I currently have is: this.whatTime = Observable.interval(1000).map(x => new Date()).share(); When I use this code in my template like so: {{whatTime | async}} ...

Encountering an issue when trying to submit the form - "Cannot POST /public/register-controller

I am currently working on developing a login/signup system using Express.js, MySQL, and Node.js. However, I encountered an error when I clicked on the submit button: Cannot POST /public/register-controller This is my index.html: <!DOCTYPE html> ...

What measures can I take to store data for a website user?

Check out my code on this website: If not, here is the HTML and JavaScript snippets: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <meta name="viewport" content="width=device-width, initial-scale=1.0"> ...

Tips for creating collapsible panel groups that close automatically when another panel is opened?

Looking at the code snippet provided, it appears that both panel-groups can be simultaneously open. Is there a way to make one collapse when the other is clicked? <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1 ...

What is the optimal method for fetching JSON information with Angular?

I have data that I organized in a JSON-like structure, as shown below: { "Id": { "1": [ { "Item1": "Item One", "Item2": "Item Two", "Item3": "Item Three" ...

Encountered an issue while trying to read properties of undefined (specifically 'meta') within a Vue 3 single-spa application

I've been working on a micro-frontend project, utilizing vue 3 and single-spa 5.9.3. Recently, I attempted to update one of the npm packages for a Micro-frontend to the latest release. The build process went smoothly, but it resulted in the following ...

Creating a visually appealing layout by positioning two images in opposite corners of a single div

As someone who's not well-versed in CSS, I could really use some assistance on creating a div with two images, similar to the diagram below. ...

Utilize nested JSON Response as row data for ag-Grid in Angular 4

Just starting out with angular and working on a project where I need to display JSON data in a grid. I've opted for ag-grid. Here's the sample JSON response I'm receiving from a rest API: [ { "id": 64, "name": "Utopia", "language": ...

Navigating on Angular 2 without refreshing the page

Seeking advice for my Angular 2 single-page application project which heavily uses maps. The main requirement is that when a point on the map is clicked, the URL should update so that upon refresh, the map zooms into that specific point (and also to allow ...

Is it possible to assign the string variable to "</a>" in PHP without it being interpreted as a command?

I am currently working on a simple web page project, where I want to include a clickable link using HTML. While I have figured out the initial steps, I'm facing an issue with the following code snippet: $URLString = "<"; $URLString .= "/a"; $URLS ...