Showing posts with label Chart. Show all posts
Showing posts with label Chart. Show all posts

Thursday, 20 October 2022

ChartJs - Scatter Chart Points/Dots On click Functionality

SCENARIO


While working on one of the requirements for an Automobile sector projector for a client based out of Dallas, Texas. There was a requirement to open the record page dynamically in a new tab when clicking on scatter chart points/dots in the VF page using Chartjs.

CHALLENGE


Although we can use standard apex:chart tags for representing charts, we had to use ChartJs for this requirement. So we used ChartJs functionality for building custom logic to open multiple record pages in a new tab when clicked on data points.

APPROACH


We have used the apex controller for fetching data dynamically and displaying it using VF Page. Here, the Amount & Month field of the Account Standard object is used, where the Month field will represent the data of X-Axis & Amount field will represent the data of Y-Axis respectively. Below is an example.

EXAMPLE


In this example, we have created an apex controller named 'ChartDataExample'
where we fetch data for plotting the chart and displaying it on VF Page 'ScatterChart'.

Step1:Create an Apex Controller.
Step2:Create a VF Page.

OUTPUT



CONCLUSION


By following the above-mentioned code, we can open the record page dynamically in a new tab when clicking on points/dots in the scatter chart on VF Page.

If you have any questions you can reach out our Salesforce Consulting team here.

Thursday, 1 September 2022

How to use ChartJS to generate a chart in a lightning web component?

The age of Data Science nowadays places a lot of importance on data visualization. Visual representation of the data puts more impact on the user interface. But the issue is to build custom, responsive, reactive charts and graphs in salesforce. Open source is one of the best sources for this kind of solution. The ChartJs is an open source solution that powers, to create charts and graphs that are completely responsive, accessible, and reactive, giving the best performance. By using ChartJs in salesforce, we can create custom charts and graphs.

In this blog, We will see how we can leverage the power of ChartJs to build a responsive Bar chart in LWC. We will see how to pull information from an Opportunity and feed it into the bar chart.

Why use ChartJS?

The charts can be added to the pages using the App Builder’s “Standard Report Chart” component. However, the report filters can be used to display relevant data on the charts but when we have a lot of custom logic and filters to be implemented in a lightning component then the “Standard Report Chart” have filter limitations. ChartJs is a handy solution for this. Let's jump for example.

Example:-

In the example we will draw a bar chart for latest 10 Opportunity amounts whose stage is Closed won.

Step 1:- Download ChartJs library.
  • Click Here to open link and right click on page and save as.
  • Upload file in Static Resource with the name 'ChartJS'.

Step 2:-
 Create a LWC with the name 'chart'.
HTML consist a canvas tag, the Chartjs library uses that canvas to draw the chart.
  
Here we have declared two @api attributes in the chart component
  • loaderVariant - Used to set the loader variant from the parent component.
  • chartConfig - Used to pass the chart settings and data from the parent component, so that we can reuse this component for different charts.


Step 3:- Create an apex class to get Opportunity data.
Retrieving Opportunity data.

Step 4:- Create  parent LWC named 'barChartExample'.
Calling child component chat and set chart configration.

The wire method gets the opportunity data and feeds it into the chart lwc.

The most important thing is chartConfiguration, all the chart behaviour is based on the config that we set to the chart. Below are main three attributes.
  • type:- It defines the type of chart being rendered, here we have 'bar'. 
  • data:- Here we pass the chartLabels and chartData and other settings for the bar chart.
  • options:- Additional settings for chart.


Output:-



If you have any questions you can reach out our Salesforce Consulting team here.