I am using chart.js to display Bar chart using LWC. I want to open report when we click on Bar chart. I am facing issue to while identifying onclick event.
Parent Component: HtML
<template>
<lightning-card title="Opportunities Report" icon-name="utility:chart">
<template if:true={chartConfiguration}>
<c-childbarchart chart-config={chartConfiguration}></c-childbarchart>
</template>
</lightning-card>
</template>
JS file
import { LightningElement,wire } from 'lwc';
import getOpportunities from '@salesforce/apex/summary2.runReport';
import getReporId from '@salesforce/apex/summary2.runReport3';
import {NavigationMixin} from 'lightning/navigation';
export default class SummaryCount extends NavigationMixin(LightningElement) {
chartConfiguration;
chartLabel = [];
reportId;
@wire(getOpportunities)
getOpportunities({ error, data })
{
if (error) {
this.error = error;
this.chartConfiguration = undefined;
}
else if (data) {
//console.log(data)
let chartdata = [];
let chartLabel =['Year Count','Last Month Count'];
var conts = data;
data.forEach(opp => {
chartdata.push(opp);
})
console.log('chartdata',chartdata);
this.chartConfiguration = {
type: 'bar',
data: {
datasets: [{
axis: 'x',
barPercentage: 1,
barThickness: 2,
label: 'Total Count',
backgroundColor: "green",
//backgroundColor: ["green","green"],
data: chartdata,
},],
labels: chartLabel,
},
options: {
scales: {
xAxes: [{
barThickness: 20, // number (pixels) or 'flex'
// maxBarThickness: 12 // number (pixels)
}],
yAxes: [{
ticks: {
beginAtZero: true
}
}]
},
onClick:this.graphClickEvent,
}
};
console.log('data => ', data);
this.error = undefined;
}
}
@wire(getReporId)getIdd({data,error})
{
if(data){
console.log('Report Id:'+data);
this.reportId=data;
}
}
graphClickEvent(){
console.log('i got clicked');
}
}
Child Component: HTML
<template>
<div class="slds-p-around_small slds-grid slds-grid--vertical-align-center slds-grid--align-center">
<canvas class="barChart" lwc:dom="manual"></canvas>
<div if:false={isChartJsInitialized} class="slds-col--padded slds-size--1-of-1">
<lightning-spinner alternative-text="Loading" size="medium" variant="base"></lightning-spinner>
</div>
</div>
</template>
Js
import { LightningElement, api } from 'lwc';
import chartjs from '@salesforce/resourceUrl/ChartJS';
import { loadScript } from 'lightning/platformResourceLoader';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class childbarchart extends NavigationMixin(LightningElement) {
@api chartConfig;
isChartJsInitialized;
renderedCallback() {
if (this.isChartJsInitialized) {
return;
}
// load chartjs from the static resource
Promise.all([loadScript(this, chartjs)])
.then(() => {
this.isChartJsInitialized = true;
const ctx = this.template.querySelector('canvas.barChart').getContext('2d');
this.chart = new window.Chart(ctx, JSON.parse(JSON.stringify(this.chartConfig)));
})
.catch(error => {
this.dispatchEvent(
new ShowToastEvent({
title: 'Error loading Chart',
message: error.message,
variant: 'error',
})
);
});
}
}