question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding multiple datalabels types on chart

See original GitHub issue

Hello ! I was wondering if it was possible to use different datalabels types on a graph. Here is what I currently have : sans titre 1 I am adding the black datalabels with the plugin but didn’t find a way to display my blue datalabels by also using the plugin so I’m currently using onComplete from chart.js which is not optimal at all.

If a similar feature or workaround is not already available, it would be great to be able to do something like this :

datalabels: [{ // datalabelsType1 },{ // datalabelsType2 }]

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
simonbrunelcommented, Sep 3, 2019

@Lenophie @Tajlang @mrVers @redoper1 @omarsedki multiple labels per data element is now implemented in e1347fc28fc8f8d890b33acb1e06011e7fdb54c9. I know it’s been a long time but can some of you test this feature before I release a new version?

chartjs-plugin-datalabels.min.js | documentation | sample

2reactions
simonbrunelcommented, Jun 24, 2018

There is no built-in feature that enables multiple labels for a single value, though I agree that could be useful. However, I’m not fan of the datalabels: [] array approach because it’s a pain to make it mergeable easily, for example if you define multiple labels at the plugin level and want to override options for a specific label at the dataset level:

options: {
  plugins: {
    datalabels: [{
      // first label options
      color: 'green'
    }, {
      // second label options
      color: 'red'
    }]
  }
},
data: {
  datasets: [{
    // how to make the second label color 'blue' instead of 'red'?
    datalabels: ??? 
  },
  // ...
},

That’s a recurrent issue we have in Chart.js with scale options. Also, it doesn’t work well with default options since the array would override it. So ideally we should support a map of options instead of an array, but in this case, we have no way to differentiate an object of options (multiple labels) from the actual options object (single label), that we still need to support.

One solution could be to introduce a new option named labels:

options: {
  plugins: {
    datalabels: {
      color: 'blue',      // labels will be 'blue' by default
      labels: {           // 2 labels for each values
        l1: {             // first label options
          ...             // color fallback to 'blue'
        },
        l2: {             // second label options
          color: 'red',   // override the default color
          ...
        }
      }
    }
  }
},
data: {
  datasets: [{
    datalabels: {           // options for the first dataset
      labels: {
        l1: {
          color: 'yellow'   // override the first label color
        }
                            // l2 still displayed 'red'
      }
    }
  }, {
    datalabels: {
        color: 'pink'       // override the default color
        labels: {
            l2: false|null  // disable the second label
        }
    }
  },
  // ...
},

Thoughts?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple Labels - chartjs-plugin-datalabels
Use multiple labels configuration to display 3 labels per data, one for the index , one for the label and one for the...
Read more >
How to Add Two Data Labels in Excel Chart (with Easy Steps)
In this article, you will find how to add two data labels in excel chart. There are 4 steps in total in this...
Read more >
Add or remove data labels in a chart - Microsoft Support
Right-click the data series or data label to display more data for, and then click Format Data Labels. Click Label Options and under...
Read more >
How to add data labels from different column in an Excel chart?
1. Right click the data series in the chart, and select Add Data Labels > Add Data Labels from the context menu to...
Read more >
How to Add Data Labels within Bars and Total Sum On Top of ...
All the charts are always coded in the latest Chartjs version which is as of this recording Chart JS 3.7.1. What type of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found