Chartjs-Change the Colour of the last segment of line chart in chartjs/react-chart2js

0👍

Even though I didn’t use react-chartjs-2 to do this, I think it should help you.

const lastSegment = function(ctx, value){
  if(ctx.p1DataIndex == config.data.datasets[0].data.length-1){
    return value
  }
}
segment: {
  borderColor: ctx => lastSegment(ctx, '#FF0000') 
}

I compare the index of the next point with the last index.
If the 2 are equal, I change the colour of the segment to red.

Here’s the full code to see what it looks like

Leave a comment