stratoflow

Creating charts in Visualforce – tutorial

Visualforce charting is a collection of components that provide a simple and intuitive way to create charts in your Visualforce pages and custom components. It is a very useful tool in when it comes to creating customized business charts.

Introduction

You can create your own chart by following these 3 steps:

  1. Prepare data in controller or JavaScript
  2. Sending data from controller or JavaScript via returning it from method or properties.
  3. Define chart by using Visualforce charting components

Preaparing data

Let’s start by focusing on the first point – preparing data. First of all, we must learn that data to charts can be provided in 3 different ways:

  • As an expression that represents a controller method reference – by returning List of objects from method or properites in controller you can send data to visualforce page
  • As a string representing a JavaScript function – Data is created in controller but is sended to visualforce page by JavaScript that is connecting with controller
  • As a string representing a JavaScript array – data for chart is created directly in JavaScript function and its returned to page.

And now i’ll present you with examples for these 3 points:

First example

Controller : 
@RemoteAction
public static List<Data> getData() {
List<PieWedgeData> data = new List<PieWedgeData>();
data.add(new Data('Jan', 12));
data.add(new Data('Jan', 14));
data.add(new Data('Jan', 15));
data.add(new Data('Jan', 20));
data.add(new Data('Jan', 21));
data.add(new Data('Jan', 23);
return data;
}
Page :
<apex:page>
<script>
function getRemoteData(callback) {
PieChartController.getData(function(result, event) {
if(event.status && result && result.constructor === Array) {
callback(result);
}
});
}
</script>
<apex:chart data="getRemoteData" ...></apex:chart>
</apex:page>

Data in this example is a custom class that we’ve created in different file.

Second example:

Controller : 
@RemoteAction
public static List<Data> getData() {
List<PieWedgeData> data = new List<PieWedgeData>();
data.add(new Data('Jan', 12));
data.add(new Data('Jan', 14));
data.add(new Data('Jan', 15));
data.add(new Data('Jan', 20));
data.add(new Data('Jan', 21));
data.add(new Data('Jan', 23);
return data;
}
Page :
<apex:page>
<script>
function getRemoteData(callback) {
PieChartController.getData(function(result, event) {
if(event.status && result && result.constructor === Array) {
callback(result);
}
});
}
</script>
<apex:chart data="getRemoteData" ...></apex:chart>
</apex:page>

RemoteAction is annotation to method which is working with JavaScript.

Third example:

Page : 
<apex:page>
<script>
var dataArray = new Array();
dataArray.push({'data1':33,'data2':66,'data3':80,'name':'Jan'});
dataArray.push({'data1':33,'data2':66,'data3':80,'name':'Feb'});
// ...
</script>

<apex:chart data="dataArray" ...></apex:chart>
</apex:page>
<apex:chart>

Limitations

And now let’s focus on limitations of Visualforce charting. First of all, charts can only be displayed in browsers supporting SVG. Secondly, charts tags are using Javascript code to draw chart, therefore we won’t be able to display them as PDF. Furthermore, these charts cannot be used in Email messages and templates. Dynamic charting components also aren’t supported.

Summary

Even though there are some obstacles and limitations along the way, Visualforce charts are very useful and neat tool especially when standard Salesforce charts are insufficient. They are also very useful when you wish to compose custom pages that combine charts and data tables in ways that are more useful to your personal work. I hope this quick tutorial will be helpful and resolve all the problems that you’ve had concerning this topic.

Testimonials

The developed software product was built from scratch with solid quality. We have had a long-term engagement with Stratoflow for nearly 10 years. We look at them as partners, rather than contractors. I'm impressed by their team culture and cross-team support.

Nathan Pesin

CTO, Legerity Financials

Stratoflow was a great partner, challenging as well as supporting our customer projects for the best outcome. They have a great pool of talent within the business - all very capability technologists, as well as being business-savvy and suitable for consultancy engagements.

Chris Goodall

Managing Consultant, CG Consultancy (UK) Limited

The bespoke metal exchange platform works great, it is easily accessible and richly functional. Stratoflow managed deadlines capably, meticulously documented their progress, and delivered a complex project at an affordable cost.

Bartlomiej Knichnicki

Vice Chairman, Supervisory Board

We are very pleased with our partnership with Stratoflow and, as we continue to grow, we expect to increase the numbers of developers that work with us on our projects. They have proven to be very skilled and flexible. They're extremely reliable, and they have a very good company culture of their own, which gives them a real edge compared to other providers that serve more as production shops rather than thought partners and creative problem solvers.

Andrew Kennedy

Founder & Managing Director, Tier 2 Consulting

Stratoflow successfully customized the system according to the specific functionalities and without bugs reported. The team was commended for their adaptability in the work process and for their responsiveness.

Joshua Blavins

Tech PM, Digital Agency

The features implemented have received overwhelmingly positive feedback from end-users. Stratoflow has an incredible technical expertise and a high degree of flexibility when it comes to changing project requirements.

Adam Hill

Chief Technology Officer, Legerity

They have impressively good knowledge of AI issues. Very responsive to any amendments and findings. Very good communication. We received a finished project which could be implemented into production shortly after testing.

CO-Founder & CTO

Circular Fashion Company

They provided superb service with seamless communication and a highly professional, technical approach. The team displays impressive technical expertise and are willing to share information and engage in constructive feedback.

Filip Stachnik

Operations Manager, Otwarte Klatki (part of Anima International)

They're very skilled technically and are also able to see the bigger picture. Stratoflow can actually think about solutions, not just the technical task at hand, which they've been assigned.

Arnd Jan Prause

Chief Operating Officer, musQueteer

Stratoflow delivered the website successfully within the timeframe and budget. They assured that the output met the set requirements. Overall, the team's performance was excellent and recommended for their exceptional technical business expertise. They've been able to deliver all of their work on time and within budget, which has been very impressive.

Lars Andersen

Founder & CEO, My Nametags

Travel sector rebound after the pandemic is complete. We have fantastic global coverage of travel data distribution due to mutual agreements and data exchange between aggregators. Competition for the best price of limited resources degradates margins.

How to win? Provide personalized experience and build your own products in the front-office. The missing bits: a traveller golden record collecting past activities and a AI/ML recommendation technology.

Michał Głomba

CEO at Stratoflow