Site icon FusionBrew – The FusionCharts Blog

Generate Chart Images on the Server Using FusionCharts

FusionCharts allows you to create awesome dashboards that can be viewed on a PC, Mac, iPad, iPhone, or a variety of mobile devices. To view these dashboards, launch your preferred browser, navigate to the dashboard’s URL (either by typing in or clicking through), the browser loads the content and FusionCharts, and you are finally presented with your interactive dashboards. Everything is in working order! But what if your boss requests that you send the dashboard via email at the end of each day so that they don’t have to remember to check it? While you try to explain to them that they will lose all of the interactivity in the dashboard, such as drill-down, slicing, and tool-tips, that they are accustomed to in the web version. They will, however, expect to see elements such as tables and Live Charts in their emails. This creates a new issue for you. While tables and other HTML elements can be easily rendered in an email, charts rendered in a browser using JavaScript would not render in an email client for security reasons. As a result, your only option is to convert these charts to images on the server before composing the email. In this post, we’ll look at how to do so with the help of a community plugin called FCImg; how to set it up, and why this plugin is so important when it comes to generating images for your fusion charts to the server.

What is FCImg?

FCImg is a free PHP chart library that allows you to generate PNG and JPEG images of your charts on the server, without even rendering the chart in a browser. It renders the JavaScript version of FusionCharts at the server and then internally uses the open-source wkhtmltoimage shell utility to convert the charts to images. Please see the live generation of chart images at the FCimg gallery. The chart images generated by FCImg could be used in the following scenarios when you need to:

Setting Up FCImg

To get FCImg for your server, choose the right variant from the list below: The system requirements to run FCImg are: After downloading the appropriate variant, extract the package to a path on your server that is accessible by your PHP Scripts. You will find that there is only one folder – fcimg. Inside this folder, you will find a /bin sub-folder, fcimg.php and template.txt. Next, you will need to set the right permission for the /bin folder contained within this folder. On Linux systems, the contents must be executable. If you downloaded and extract the files directly on the server, the file permissions are already set. However, if you transferred the files using FTP, then you need to set the permissions manually using this command from the directory where you extracted fcimg: [cciel lang=”bash”]chmod +x fcimg/bin/*

Using FCImg

Using FCImg is fairly simple. You include the fcimg.php file and invoke fusioncharts_to_image function with a bunch of parameters, as listed and explained below.
require "fcimg/fcimg.php";
fusioncharts_to_image (
	"/var/www/report.png",		// Full path to output image, including name.
	"Column3D.swf",				// SWF Alias of chart. Note: SWF File is NOT required. 
								// It's just a pointer to which chart type to render.
	$inputString,				// The input XML data string for the chart
	400, 500,					// Height and width of the chart
	array(						// Additional options for conversion
		'imageType' => 'jpg',	// Setting image type as JPG
		'quality' => 75			// Increase output quality
)
);
The fusioncharts_to_image method takes the following arguments, listed in order: When you now run this script on your server, you will find the output chart image file [cciel lang=’javascript]report.png under /var/www folder of your server.

What Happened Behind the Scenes?

When we executed this script, the following actions happened behind the scenes: FCImg takes a couple of seconds to export a single chart on a basic server. This delay is intended because the default animations of FusionCharts take up to 1.5 seconds to complete on certain charts. The fusioncharts_to_image function is a blocking call. It means that the PHP script will not continue until the rendering process is complete (unlike event-based callback systems). Therefore, it is best to call this method in PHP scripts that run on the server directly and do not serve output to browsers e.g., in a script that generates emailed reports, or a script that is executed through a cron-job.

Changing the FusionCharts JavaScript Library Used in FCImg

By default, FCImg ships with an evaluation copy of the FusionCharts JavaScript version, which is not the latest as well. To update to the latest, or even point to your licensed copy of FusionCharts JavaScript graphs, you will need to do the following changes: Note that the order of files added is important.

Getting On With It

Do give FCImg a spin. It’s useful. It’s free. And if you encounter some painful situations, give a shout to the developer at contact@fcimg.org. It will help if you include details like the XML/JSON data.
Exit mobile version