Embed Charts Into Your Documents


Leveraging the Chart API provides a great way to include dynamic charts in your S-Docs.
If you previously added a Google Chart to your template, you can easily update your org and template to use Image-Charts.

Create A Remote Site Setting:

This functionality requires an entry in Salesforce's Remote Site Settings.

  1. Click Your Name > Setup > Security Controls > Remote Site Settings.
  2. Click New Remote Site button.
  3. Set the Remote Site Name field to ImageCharts.
  4. Set the Remote Site URL field to https://image-charts.com/
  5. Make sure Active is checked and then click the Save button.

Using Image-Charts

Image-Charts work by passing your Salesforce data to the site's Chart API, which in turn dynamically builds and returns the chart as a PNG image.

The site offers a tool to help you understand chart URL parameters and build the look of your chart. Be sure to set the API Endpoint to "/chart" before you begin working. You can get started here: https://editor.image-charts.com/

Security Note: Unlike S-Docs, which works entirely within the Salesforce.com platform, Image-Charts images are hosted remotely. Depending on the chart, your data is passed from Salesforce to Image-Charts. Your security team would need to determine if this is an acceptable practice.

Images are rendered by using one URL request per chart. For example, you can manipulate the values in the following URL to render a different pie chart:


The link above will return the following image:

As you may begin to see, using the Chart API is all about creating these URLs where you pass in the data points, legends and chart options. Depending on the chart, some of these URLs can be long and a bit complex.

Option 1: Use Formula Fields For Your Google Charts

Rather than building complex logic within S-Docs to create these URLs, we again leverage Salesforce formula fields to do the heavy lifting.

This example formula field would display a Won/Loss pie chart:

IMAGE("https://image-charts.com/chart?chco=CFECF7,27c9c2&chd=t:" & Text(Won__c/(Won__c + Lost__c)) & "," & Text(Lost__c/(Won__c + Lost__c)) & "&chf=bg,s,F3F3EC&chl=Won|Lost&chs=275x100&cht=p3", "chart text")

Then, we simply drop this formula field into our S-Doc template (be sure to check Contains Rich Text box).

Option 2: Use The Google Chart API URL In Your S-Docs Template

While more complex, another approach is to build the Chart API URL in your S-Doc Template and then mix in field values. This may offer you greater control. Using syntax similar to the following, you could add the image, where ChartLegend__c would just contain the value “Jan|Feb.”

lt#img src="https://image-charts.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl={{!MyObject__c.ChartLegend__c}}"/gt#

You can combine this method with Named Queries or simple unformatted related list queries (<class>none</class>) to pull in data (chd) for your charts.

Tags: ,

Was this helpful?