Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.x Plugin Legend onClick not working properly #1399

Open
kbabraj opened this issue Apr 13, 2022 · 0 comments
Open

3.x Plugin Legend onClick not working properly #1399

kbabraj opened this issue Apr 13, 2022 · 0 comments

Comments

@kbabraj
Copy link

kbabraj commented Apr 13, 2022

I am using Angular 12 ,"ng2-charts": "^3.0.9", and "chart.js": "^3.7.1", Legend onClick not working properly, somtimes works by click the legend and moving the cursor somewhere or mouse over from the menu.

the same workins in chart.js 2.9.4 and ng2-charts 2.4.2
Example code is provided below and my requirement is when I click the Legend it will display the "Show Text".

Html code

Total New Parameters
Show Text

Angular code

public smeChartOptions: ChartOptions = {
	responsive: true,
	maintainAspectRatio: false,
	
	plugins: {
		/*stacked100: {enable: true},*/
		legend: {
			onClick: (e, item) => {
				console.log('I am in');
				this.isTransDocumentation = true;
			}
		}

	}
};

To simulate the issue go to https://stackblitz.com/edit/ng2-charts-line-template-v3?file=src%2Fapp%2Fapp.component.ts

And replace the below code,

  1. app.components.ts
    import { Component } from '@angular/core';
    import { ChartConfiguration, ChartOptions, ChartType } from "chart.js";

@component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
title = 'ng2-charts-demo';

public lineChartData: ChartConfiguration<'line'>['data'] = {
labels: [
'January',
'February',
'March',
'April',
'May',
'June',
'July'
],
datasets: [
{
data: [ 65, 59, 80, 81, 56, 55, 40 ],
label: 'Series A',
fill: true,
tension: 0.5,
borderColor: 'black',
backgroundColor: 'rgba(255,0,0,0.3)'
}
]
};
public lineChartOptions: ChartOptions<'bar'> = {
responsive: false,
plugins: {
legend: {
display: true,
onClick: (e, legendItem, legend) => {
if (legendItem.text === 'Series A') {
this.isTransDocumentation = true;
} else {
this.isTransDocumentation = false;
}
console.log('I am in');
}
}
}
};
public lineChartLegend = true;
public lineChartPlugins = [];
isTransDocumentation: boolean;

constructor() {
}

ngOnInit() {
}

}

  1. app.component.html
    `

to check ngif and display the below text

Some Text

`

When I onClick the Legend it should show the "Some Text" on the html page

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant