[Chartjs]-How to calculate and show monthly cost in chartjs?

2๐Ÿ‘

โœ…

I think it could be better to use a reduce of the result array instead of the for cycle.

You should remove the j for loop and add the following code to get the position of annotation (where instance):

const where = arr.reduce(function(pre, cur, index){
  if (!pre) {
    if (cur.sum_new_in_months < cur.sum_old_in_months) {
      const diffCur = cur.sum_old_in_months - cur.sum_new_in_months;
      const posInDiff = (cur.sum_new_in_months - diffCur) / cur.sum_new_in_months;
      return index - 1 + posInDiff;
    }
  }
  return pre;
}, 0);

EDIT: here a codepen with annotation: https://codepen.io/stockinail/pen/ExREWrq

Leave a comment