Reader small image

You're reading from  Visualforce Development Cookbook

Product typeBook
Published inSep 2013
Reading LevelIntermediate
PublisherPackt
ISBN-139781782170808
Edition1st Edition
Languages
Right arrow
Author (1)
Keir Bowden
Keir Bowden
author image
Keir Bowden

Keir Bowden is a 30-year veteran of the IT industry from the United Kingdom. After spending the early part of his career in the defence industry, he moved into investment banking systems, implementing systems for Banque Nationale de Paris, CitiGroup, and Deutsche Bank. In the late 1990s, Keir moved into Internet technologies, leading to a development of the order management and payment handling systems of one of the first European Internet shopping sites. Keir started working with Force.com in late 2008 and has been recognized multiple times by Salesforce as an MVP for his contribution and leadership in the community. In 2012, he became the first certified technical architect outside of Salesforce in EMEA, and he has served as a judge on several EMEA Technical Architect Certification Review Boards. Keir is also a prominent blogger on Apex, Visualforce and Lightning Components solutions; and a regular speaker at events such as Dreamforce, Cloud World Forum, and Salesforce World Tour. Keir is a chief technical officer of BrightGena—a Salesforce.com Platinum Cloud Alliance Partner in the United Kingdom, where he is responsible for the present and future technical strategies. Keir acted as a technical reviewer for the CRM Admin Cookbook before accepting the challenge of authoring this book, which also happens to be his first.
Read more about Keir Bowden

Right arrow

Chapter 6. Visualforce Charts

In this chapter, we will cover the following recipes:

  • Creating a bar chart

  • Creating a line chart

  • Customizing a chart

  • Adding multiple series

  • Creating a stacked bar chart

  • Adding a third axis

  • Embedding a chart in a record view page

  • Multiple charts per page

Introduction


Visualforce charting allows custom charts to be embedded into any Visualforce page using standard components, only server-side code is required. A key difference from the standard charting functionality available in reports and dashboards is that the data is provided by the Visualforce page controller and can be derived from any number of sObjects, regardless of whether any relationships between the sObjects exist.

Note

Visualforce charts became Generally Available in the Winter '13 release of Salesforce. Prior to this, custom charts required use of a JavaScript framework, such as Dojo Charting or Google Charts.

In this chapter, we will create a number of Visualforce charts of increasing complexity, add a chart to a standard Salesforce record view page, and generate a number of charts on a single page, much like a standard Salesforce dashboard.

Creating a bar chart


Bar charts allow easy comparison of groups of data. A typical use in Salesforce is to view performance on a month-by-month basis; for example, to identify the effectiveness of a process improvement.

In this recipe, we will create a Visualforce page containing a bar chart that displays the total value of won opportunities per month for the previous 12 months. This allows a sales manager to view at a glance whether sales are increasing or decreasing, and to identify any problem months that require further analysis.

Getting ready

This recipe makes use of a custom controller, so this must be created before the Visualforce page.

How to do it…

  1. Navigate to the Apex Classes setup page by clicking on Your Name | Setup | Develop | Apex Classes.

  2. Click on the New button.

  3. Paste the contents of the BarChartController.cls Apex class from the code download into the Apex Class area.

  4. Click on the Save button.

  5. Next, create the Visualforce page by navigating to the Visualforce setup page by clicking...

Creating a line chart


Line charts are useful to demonstrate changes in data over time. A typical use in Salesforce is to view the number of records with a particular characteristic over a period of time.

In this recipe, we will create a Visualforce page containing a line chart that displays the total number of closed cases per month for the previous 12 months.

Getting ready

This recipe makes use of a custom controller, so this will need to be created before the Visualforce page.

How to do it…

  1. First, create the custom controller by navigating to the Apex Classes setup page by clicking on Your Name | Setup | Develop | Apex Classes.

  2. Click on the New button.

  3. Paste the contents of the LineChartController.cls Apex class from the code download into the Apex Class area.

  4. Click on the Save button.

  5. Next, create the Visualforce page by navigating to the Visualforce setup page by clicking on Your Name | Setup | Develop | Pages.

  6. Click on the New button.

  7. Enter LineChart in the Label field.

  8. Accept the default LineChart...

Customizing a chart


Visualforce charts are highly customizable; colors, markers, line widths, highlighting, legends, labels, and more are under the control of the developer.

In this recipe we will create a Visualforce page containing a bar chart displaying the total value of won opportunities per month for the last year.

The chart will be customized to display horizontal bars in a custom dark blue color that do not highlight when the user hovers over a bar. Finally, a legend will be displayed to show the user what the bars represent.

Getting ready

This recipe relies on the custom controller from the Creating a bar chart recipe in this chapter. If you have already completed that recipe, you can skip this section.

  1. Navigate to the Apex Classes setup page by clicking on Your Name | Setup | Develop | Apex Classes.

  2. Click on the New button.

  3. Paste the contents of the BarChartController.cls Apex class from the code download into the Apex Class area.

  4. Click on the Save button.

How to do it…

  1. Navigate to the Visualforce...

Adding multiple series


In the previous recipes in this chapter, each chart contained a single series. Visualforce charts are not limited to this and can plot multiple sets of data, regardless of whether there is a relationship between the data sets.

In this recipe we will create a Visualforce page containing a chart that plots two series against the month for the last year. The first is a bar series of the number of opportunities lost in the month, while the second is a line series of the number of opportunities won in the month. This allows a sales director to see if the won/lost ratio is improving over time.

Getting ready

This recipe makes use of a custom controller, so this must be present before the Visualforce page can be created.

How to do it…

  1. Navigate to the Apex Classes setup page by clicking on Your Name | Setup | Develop | Apex Classes.

  2. Click on the New button.

  3. Paste the contents of the MultiSeriesChartController.cls Apex class from the code download into the Apex Class area.

  4. Click on...

Creating a stacked bar chart


Stacked bar charts allow the contributing parts of data to be compared to the whole. An example of this is displaying a bar that represents the total number of opportunities that are currently open, with sections of the bar displaying the count of opportunity records that are in each stage of the sales process.

In this recipe, we will create a Visualforce page containing a stacked bar chart where each bar displays the total opportunity value that closed that month, both won and lost, for the last 12 months. Each bar is divided into two segments: the lower segment shows the total value of opportunities lost in a month, while the upper segment shows the total value won.

Getting ready

This recipe makes use of a custom controller, so this must be present before the Visualforce page can be created.

How to do it…

  1. Navigate to the Apex Classes setup page by clicking on Your Name | Setup | Develop | Apex Classes.

  2. Click on the New button.

  3. Paste the contents of the StackedBarChartController...

Adding a third axis


Plotting multiple series on a single graph can be problematic if the values of the two series vary widely. For example, if the total value of won opportunities were plotted against the record count of won opportunities, the total value number would be likely to be several hundred thousand times the record count number. Plotting these on a single chart would result in the record count plot being as close to zero as to be indistinguishable from zero.

The solution to this problem is to display a third axis. The axis is scaled appropriately to the data set that is plotted against it.

In this recipe we will create a Visualforce page containing a chart that displays the total value of the won and lost opportunities per month for the last year. The won/lost information is displayed as a stacked bar chart. The chart also displays a line series chart where each point on the line series is the number of opportunities that were won/lost in that month. As the number of opportunities...

Embedding a chart in a record view page


Visualforce charts can be generated wherever a Visualforce page can be displayed, including sidebar components, the homepage, and in standard record view pages.

In this recipe, we will create a Visualforce page containing a chart that displays a stacked bar chart containing the total number of activities carried out with a contact per month for the last year. The stacked bars contain a segment for the events and tasks that make up the activity total. This Visualforce page is embedded into the standard contact record view page to allow a sales manager to see at a glance whether a contact is being neglected or receiving more than its fair share of attention.

Getting ready

This recipe makes use of a controller extension that must be present before the Visualforce page can be created.

How to do it…

  1. First, create the controller extension for the Visualforce page by navigating to the Apex Classes setup page by clicking on Your Name | Setup | Develop | Apex Classes...

Multiple charts per page


A common use case for Visualforce charting is producing a number of custom charts arranged into rows and columns, much like a standard dashboard. Simply adding chart components to HTML table cells results in all the charts being displayed in the top-left cell of the table, as shown in the following screenshot:

The solution to this is to use the chart renderTo attribute to specify the DOM component that the chart should be rendered inside.

In this recipe we will create a Visualforce page that displays a table of bar charts. Each bar chart displays the total won opportunity value per month for the last year for a specific account.

Getting ready

This recipe makes use of a custom controller, so this will need to be present before the Visualforce page can be created.

How to do it…

  1. First, create the custom controller for the Visualforce page by navigating to the Apex Classes setup page by clicking on Your Name | Setup | Develop | Apex Classes.

  2. Click on the New button.

  3. Paste the...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Visualforce Development Cookbook
Published in: Sep 2013Publisher: PacktISBN-13: 9781782170808
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Keir Bowden

Keir Bowden is a 30-year veteran of the IT industry from the United Kingdom. After spending the early part of his career in the defence industry, he moved into investment banking systems, implementing systems for Banque Nationale de Paris, CitiGroup, and Deutsche Bank. In the late 1990s, Keir moved into Internet technologies, leading to a development of the order management and payment handling systems of one of the first European Internet shopping sites. Keir started working with Force.com in late 2008 and has been recognized multiple times by Salesforce as an MVP for his contribution and leadership in the community. In 2012, he became the first certified technical architect outside of Salesforce in EMEA, and he has served as a judge on several EMEA Technical Architect Certification Review Boards. Keir is also a prominent blogger on Apex, Visualforce and Lightning Components solutions; and a regular speaker at events such as Dreamforce, Cloud World Forum, and Salesforce World Tour. Keir is a chief technical officer of BrightGena—a Salesforce.com Platinum Cloud Alliance Partner in the United Kingdom, where he is responsible for the present and future technical strategies. Keir acted as a technical reviewer for the CRM Admin Cookbook before accepting the challenge of authoring this book, which also happens to be his first.
Read more about Keir Bowden