0

I am trying to chartjs dashboard page.I am got some issues in bottom label color problem.I get the solution but if condtion not working to me.Please any one help me.

My Code.....

generateLabels: function(chart) {
   var get_label = _.pluck(chart.data.datasets, 'label');
   var join_label = _.flatten(get_label);
   var get_color = _.pluck(chart.data.datasets, 'backgroundColor');
   var join_color = _.flatten(get_color);
   var get_data = {
        label: join_label,
        backgroundColor: join_color
  }
  for (k = 0; k < get_data.label.length; k++) {
     return get_data.label.map(function(label, i) {
        if(label != 'undefined'){
           return {
               text: label,
               fillStyle: get_data.backgroundColor[i]
           };
        }
      });
   }

}

if(label != 'undefined') this condition not working...

2
  • 1
    label != 'undefined' is testing if label is string with value 'undefined'. Try value === undefined or !value.
    – marekful
    Jan 30, 2018 at 5:08
  • Also the for loop doesn't make sense. The loop variable k isn't used and the first statement of the loop body is a return. That's a quite convoluted and misleading way to express a simple if (get_data.label.length > 0). Additionally I would explicitly return undefined or even better null in the cases where the function(s) don't run into a return if there is at least one code path ending at a return in a function. Otherwise it's not clear to the reader if this is on purpose or actually an error/oversight.
    – BlackJack
    Feb 1, 2018 at 13:49

2 Answers 2

0

Replace if(label != 'undefined') for if(label). It should works

1
  • if(label) condition is working but no any return value.I got one issues in chartjs bundle js file. Uncaught TypeError: Cannot read property 'text' of undefined at Chart.bundle.min.js:14 at Object.r.each (Chart.bundle.min.js:13) at n.fit (Chart.bundle.min.js:14) at n.update (Chart.bundle.min.js:14) at a (Chart.bundle.min.js:14)
    – Ananth
    Jan 30, 2018 at 5:21
0

I think your problem is that undefined is a keyword so you don't have to put it between quotes. Try with if (label != null && label != undefined) { ... }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.