Lightning component to display chart using Chart.Js
In the following example, we are using Chart.Js plugin to implement Chart in our lightning component. I am using Account and it's related Contact for showing how many contact are related to one account graphically.
Screenshots: -
Apex: -
- public class getContact {
- @AuraEnabled
- public static List<Contact> getContactList(){
- List<Contact> con = [SELECT Id, Name, Email, Account.Name FROM Contact];
- return con;
- }
- }
Component: -
- <aura:component implements="flexipage:availableForAllPageTypes" access="global" controller="getContact">
- <!--attributes-->
- <ltng:require scripts="{!$Resource.chartNew}" afterScriptsLoaded="{!c.scriptsLoaded}"/> <!--include chart JS plugin from static resource-->
- <aura:handler name="init" value="{!this}" action="{!c.init}" /> <!--init method for getting contacts and accounts
- and set them into chart-->
- <!--markup-->
- <div class="slds-grid">
- <div class="slds-col slds-p-around_large">
- <canvas aura:id="myChart" class="myChart"></canvas>
- </div>
- </div>
- </aura:component>
- ({
- scriptsLoaded : function(component, event, helper) {
- //create context to instantiate a pre-defined chart
- var ctx = component.find("myChart").getElement();
- var data = {
- labels: ["Family", "Friends", "Relative", "Event Organizer", "Home Renovation And Construction"], //name of accounts
- datasets: [
- {
- label: 'Number of contacts',
- borderColor: 'rgb(245, 66, 138)',
- backgroundColor: 'rgb(245, 66, 138)',
- },
- ]
- }
- component.myChart = new Chart(ctx, {
- type: 'bar',
- data: data,
- options: {
- responsive: true,
- plugins: {
- legend: {
- position: 'top',
- }
- },
- }
- });
- },
- init: function(component, event, helper) {
- helper.getContactAndAccount(component, event, helper);
- },
- })
Helper: -
- ({
- getContactAndAccount : function(component, event, helper) {
- var action = component.get("c.getContactList");
- action.setCallback(this, function(response){
- var state = response.getState();
- if(state === "SUCCESS") {
- var contacts = response.getReturnValue();
- console.log('contacts-->', contacts);
- //go to next helper function to assign value to chart
- this.setData(component, event, contacts);
- }
- });
- $A.enqueueAction(action);
- },
- setData : function(component, event, contacts) {
- if (component.myChart && component.myChart.data && component.myChart.data.datasets[0]) {
- var contacts = contacts;
- if (contacts && Array.isArray(contacts)) {
- var map = {};
- contacts.forEach(function(contact) {
- map[contact.Account.Name.replace(/ /g, '_')] = (map[contact.Account.Name.replace(/ /g, '_')] || 0) +1;})
- //since in map we can't use name with space separated, so we replace the space with '_'
- var data = [
- map.Family || 0,
- map.Friends || 0,
- map.Relative || 0,
- map.Event_Organizer || 0,
- map.Home_Renovation_And_Construction || 0
- ];
- //in this example I am putting the account name which I needed to show, If we want we can show all account or can make them dinamically to be came
- component.myChart.data.datasets[0].data = data;
- component.myChart.update();
- }
- }
- },
- })
App: -
- <aura:application extends="force:slds" access="GLOBAL">
- <c:ChartDemoComponent />
- </aura:application>

Comments
Post a Comment