0

I am seeking urgent help.

I have to show a donut chart on a record. I tried to use chart.js latest library 3.2.0 but still, it doesn't work.

APEX Controller -

public class FeesChartController {
@AuraEnabled
public static List<DataSet> getFees(String QuoteId){
    system.debug('QuoteId'+QuoteId);
    List<SBQQ__Quote__c> result =  [Select Id,Fee1__c, Fee2__c, Fee3__c from SBQQ__Quote__c where Id =:QuoteId ];
    List<DataSet> dataSet = new List<DataSet>();
    
    DataSet GpFees = new DataSet('Fee 1',result[0].Fee1__c);
    dataSet.add(GpFees);
    DataSet Salary = new DataSet('Fee 2',result[0].Fee2__c);
    dataSet.add(Salary);
    DataSet Compliance = new DataSet('Fee 3',result[0].Fee3__c);
    dataSet.add(Compliance);
    
    System.debug('dataSet'+dataSet);
    return dataSet ; 
}

public class DataSet{
    public DataSet(String label ,Decimal Value){
        this.label  = label ; 
        this.value = Value ;
    }
    @AuraEnabled
    public String label {get;set;}
    @AuraEnabled
    public Decimal  value {get;set;}
    
    
}

}

Component

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId" access="global" controller="FeesChartController">
    <aura:attribute name="QuoteId" type="string"/>
{!v.QuoteId}
<ltng:require scripts="{!$Resource.chartjs}"
              afterScriptsLoaded="{!c.scriptsLoaded}" />
<canvas id="pie-chart" width="950" height="250"></canvas></aura:component>

JS Controller

({
scriptsLoaded : function(component, event, helper) {
    var action = component.get("c.getFees");
    var quoteId = component.get("v.recordId");
    component.set("v.QuoteId",component.get("v.recordId"));
    alert('quoteId'+quoteId);
    action.setParams({
        "QuoteId": quoteId
    });
    action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            let val = response.getReturnValue();
            //alert(response.getReturnValue());
            var labelset=[] ;
            var dataset=[] ;
            val.forEach(function(key) {
                alert(key.label+'-->'+key.value);
                labelset.push(key.label) ; 
                dataset.push(key.value) ; 
            });
            new Chart(document.getElementById("pie-chart"), {
                type: 'pie',
                data: {
                    labels:labelset,
                    datasets: [{
                        label: "Count of Task",
                        backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9"],
                        data: dataset
                    }]
                },
                options: {
                    title: {
                        display: true,
                        text: 'Total Tasks by Status'
                    }
                }
            });
        }
    });
    $A.enqueueAction(action);
    
}})

I tried solutions mentioned on other similar questions as well but no luck with that too.

Current Output - It's just showing a blank block. Nothing getting showed up on the record page where I have added this component.

Expected - It should prepare and show a chart.

Nothing showed up as an error.

The 1st tried solution is given by @MohithShrivastava - Radar chart using Chart.js not working

The 2nd tried solution is given by @CarlosNaranjo - Donut chart in lightning component

The 3rd tried solution - Lightning Chart.js example

Any urgent help will be really appreciated. Thanks

2
  • 2
    Welcome to SFSE! Could you please edit your post to describe the specific problem behavior, including any error messages, console logs, observed behaviors...? – David Reed Apr 27 at 21:49
  • @DavidReed - I have edited the original post and mentioned current output, expectations. – Pratik Pawar Apr 28 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.