score:4
Accepted answer
You need to change the style of the text before writing it down.
If you take a look at the fillStyle
doc on MDN :
The CanvasRenderingContext2D.fillStyle property of the Canvas 2D API specifies the color or style to use inside shapes.
So it will have an effect on the next shapes (such as text via fillText
).
You need to change the style of the text before writing it down.
Using the same function you put in your question :
var fillText = function(x, y, legendItem, textWidth)
{
// We store the current fillStyle
var prevFillStyle = ctx.fillStyle;
if (legendItem.hidden) {
// If the item is hidden, we change the fillStyle to red
ctx.fillStyle = "red";
}
// The legend label is written here
ctx.fillText(legendItem.text, boxWidth + (fontSize / 2) + x, y);
if (legendItem.hidden) {
// We comment the stroke part -- as you did
//ctx.beginPath();
//ctx.lineWidth = 2;
//ctx.moveTo(boxWidth + (fontSize / 2) + x, y + (fontSize / 2));
//ctx.lineTo(boxWidth + (fontSize / 2) + x + textWidth, y + (fontSize / 2));
//ctx.stroke();
// And then we put the fillStyle back to its previous value
ctx.fillStyle = prevFillStyle;
}
};
score:0
I found a better way doing that:
add onClick to legends and put this :
const index = legendItem.datasetIndex;
const ci = legend.chart;
const isVisible = ci.isDatasetVisible(index);
ci.setDatasetVisibility(index, !isVisible);
// this is where the stroke remove happens
const ci = legend.chart;
const fillTextValue = ci.ctx.fillText;
ci.ctx.fillText = function fillText(x, v, b) {
fillTextValue.bind(ci.ctx, x, v, b)();
this.fillStyle = "rgba(0,0,0,0)"; // whatever color you like
};
ci.update();
Source: stackoverflow.com
Related Query
- How to change color of hidden legend item instead of strike-through in Chart.js
- How to change the color of legend in chartjs and be able to add one more legend?
- How to change color of column in chart js
- change legend item style when dataset is hidden
- How to change background color of labels in line chart from chart.js?
- How to change color by clicking on the chart bar?
- How can I re-distribute values within a charts.js pie chart when a legend item is turned off/on?
- How can i give the full Legend a background color in chart js?
- how to change point style legend to diamond in chart js
- How can i change the every legend label font color using chart.js version 2.8.0 (spacial line chart)?
- How to change label color of ng2 chart in angular
- How to change bar color on a angular-chart and chart js bar graph with multiple datasets?
- How set color family to pie chart in chart.js
- chartjs datalabels change font and color of text displaying inside pie chart
- chart js how to fill legend box with colour
- Chart js. How to change font styles for "labels" array?
- How to change the color of Chart.js points depending on the label
- How do I change the 'months' language displayed on the date axis in Chart JS?
- How to change line segment color of a line graph in Chart.js?
- Chart.js: How can a line series (out of many) change line color and thickness upon mouse hover?
- how to set chart.js grid color for line chart
- Chart js: how can I align the legend and the title
- ChartJS -- How do I change scale color when I have to scales?
- Change border color on legend in Chart.js
- chart.js bar chart color change based on value
- Angular 8 & ChartJs change color in pie chart
- How to show tooltip on legend item in chart.js
- How to set single color on each bar in angular chart js
- How to change Chart.js horizontal bar chart Width?
- How to change the label color in chart.js?
More Query from same tag
- ERROR TypeError: Cannot read property 'nativeElement' of undefined in Ionic 5 and chart.js
- Chart JS not working with date
- Smallest values are invisible in ng2-charts bar charts
- Chartjs - pointColor to follow current color of gradient stroke
- ChartJS ticks values are not correct
- How to filter chart.js by month
- Change labels colors for radar chart js
- Chart.js Doughnut with rounded edges
- Chart.js how to use scriptable options
- Problems hovering over ChartJS bar chart after being redrawn
- ChartJS Line chart causes browser crash
- ChartJS legend background color while using point styles
- function returns undefined in AngularJS
- Issue in implementing ChartJS in my new assignment of ReactJS App, please let me know where I am wrong
- Chart.js line chart does not render data lines on iOS
- Plotting Real Time data using Flask Chart.js
- chartjs - how to set the order in which the different charts are displayed
- ChartJS - Highlight grouped bars
- chart.js version 3 - How to make an overlapping bar chart?
- Long text in x-axis is cut in bar graphs ng2-charts
- How to get the setState reassigned in react
- How to Find the Sum of items Per Year Using Chart Js
- Display values in Pareto chart using Chart.js 2.0.2
- chart.js - link to other page when click on specific section in chart
- Chartjs Polar Area Chart - Data labels shown don't rotate with startAngle
- charts.js chart size it's diferente than container setted size
- How to set 2 y-axis title (and/or label) on chartjs?
- ChartJS: Can interior of polar area chart be hollow?
- How to create a line chart indicating which month a user wrote more or less blogs?
- How to allow zooming in between range only in chartjs-plugin-zoom