It is working after I refresh the entire page but I need the child component to reload.
Parent Component :
<template if:true={chartConfigurationSubjective}>
<c-acc-child-healthscorechart chart-config={chartConfigurationSubjective}></c-acc-child-healthscorechart>
</template>
<input id="myinput" min="25" max="70" type="range" value={val} oninput={handleChange} />
<p>Selected Health Score : {score}</p> <lightning-button label="Save" onclick={refreshComponent}></lightning-button></p>
Parent JS : Wired method to get the value and create the chart configuration(chartConfig used in child js). ...
handleChange(event) {
let slidervalue = event.target.value;
if (slidervalue > 50) {
this.score = 'Green';
} else if (slidervalue > 25 && slidervalue <= 50) {
this.score = 'Yellow';
} else {
this.score = 'Red';
}
const fields = {};
fields[ID_FIELD.fieldApiName] = this.recordId;
fields[SCORE_FIELD.fieldApiName] = this.score;
const selectedRecordInput = {
fields
};
updateRecord(selectedRecordInput)
.then(() => {
console.log('calculatedScore' + this.score);
return refreshApex(this.wiredAccResult);
}).catch(error => {
console.error(error);
});
}
Child Component :
<template>
<div class="slds-p-around_small slds-grid slds-grid--vertical-align-center slds-grid--align-center">
<canvas class="chart" 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>
Child JS:
@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.chart').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',
})
);
});
}
}