Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove strikethrough behavior in chart.js bar chart

i am trying to change the look of the legends by removing the strikethrough effect , wihtout using legendCallback function in chart.js. The reason why i do not want to use the legendCallback function because i have my own custmizations in chart.options.legend.onClick. hence if i use legendCallback i won't be able to use the chart.options.legend.onClick.

after carefully looking at the source of Chart.js i come to know that inside a draw function of Chart.Legend they are applying the strikethrough effect.

Here is the link to plugin.legend.js

and here is the piece of code that applies the styling

    var fillText = function(x, y, legendItem, textWidth) {
            var halfFontSize = fontSize / 2;
            var xLeft = boxWidth + halfFontSize + x;
            var yMiddle = y + halfFontSize;

            ctx.fillText(legendItem.text, xLeft, yMiddle);

            if (legendItem.hidden) {
                // Strikethrough the text if hidden
                ctx.beginPath();
                ctx.lineWidth = 2;
                ctx.moveTo(xLeft, yMiddle);
                ctx.lineTo(xLeft + textWidth, yMiddle);
                ctx.stroke();
            }
        };

i want to know how shall we able to alter the behavior of the strikethrough just apply the fade effect when the legend is not active or hidden.

while searching for solution i came across this codepen where in some has tried to override the functionality but unfortunately it is now working properly with the chart.js version 2.7.3

Link to my fiddle

like image 896
Hunt Avatar asked Dec 09 '25 05:12

Hunt


1 Answers

I know this post is old (I don't think there are rules against posting on older posts) but in case anyone comes across this you can simply set onClick to null like this: (as well as change the legend's text color and size)

        options: {
          plugins: {
            legend: {
              onClick: null,
              labels: {
                color: "white",
                font: {
                  size: 18
                }
              }
            }
          }
        }
      }
like image 155
Lucas Dowlen Avatar answered Dec 10 '25 18:12

Lucas Dowlen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!