A short history of web charting
Before diving into Highcharts, it is worth mentioning how web charts evolved from pure HTML with server-side technology to the current client side.
HTML image map (server-side technology)
This technique has been used since the early days of HTML, when server-side operations were the main drive. Charts were only HTML images generated from the web server. Before there were any server-side scripting languages such as PHP, one of the most common approaches was to use Common Gateway Interface (CGI), which executes plotting programs (such as gnuplot) to output images. Later, when PHP became popular, the GD graphic module was used for plotting. One product that uses this technique is JpGraph.
The following is an example of how to include a chart image in an HTML page:
<img src="pie_chart.php" border=0 align="left">
The chart script file pie_chart.php
is embedded in an HTML img
tag. When the page is loaded, the browser sees the img src
attribute and sends an HTTP request for pie_chart.php
. As far as the web browser is concerned, it has no knowledge of whether the .php
file is an image file or not. When the web server (with PHP support) receives the request, it recognizes the .php
extension and executes the PHP scripts. The following is a cut down JpGraph example; the script outputs the image content and streams it back as an HTTP response, in the same way as normal image content would be sent back:
// Create new graph $graph = new Graph(350, 250); // Add data points in array of x-axis and y-axis values $p1 = new LinePlot($datay,$datax); $graph->Add($p1); // Output line chart in image format back to the client $graph->Stroke();
Furthermore, this technology combines with an HTML map
tag for chart navigation, so that, when users click on a certain area of a graph, for example a slice in a pie chart, it can load a new page with another graph.
This technology has the following advantages:
- Ideal for automation tasks, for example scheduled reports or e-mail alerts with the graph attached.
- Doesn't require JavaScript. It is robust, pure HTML, and is light on the client side.
It has the following disadvantages:
- More workload on the server side
- Pure HTML and a limited technology—few interactions can be put on the graphs and none on the animations
Java applet (client-side) and servlet (server-side)
A Java applet enables the web browser to execute multiplatform Java bytecode to achieve what HTML cannot do, such as graphics display, animations, and advanced user interactions. This was the first technology to extend traditional server-based work to the client side. To include a Java applet in an HTML page, HTML's applet
(deprecated) or object
tags are used and require a Java plugin to be installed for the browser.
The following is an example of including a Java applet inside an object
tag. As Java does not run in the same environment in Internet Explorer as in other browsers, the conditional comments for IE were used:
<!--[if !IE]> Non Internet Explorer way of loading applet --> <object classid="Java:chart.class" type="application/x-java-applet" height="300" width="550" > <!--<![endif] Internet way of loading applet --> <object classid="clsid:8AD9C840..." codebase="/classes/"> <param name="code" value="chart.class" /> </object> <!--[if !IE]> --> </object> <!--<![endif]-->
Generally, the Java 2D chart products are built from the java.awt.Graphics2D
class and the java.awt.geom
package from Java
Abstract Window Toolkit (AWT). Then, the main chart library allows the users to utilize it in a browser, extending from the Applet
class, or to run it on the server side, extending from the Servlet
class.
An example of a Java product is JFreeChart. It comes with 2D and 3D solutions and is free for nonprofit use. JFreeChart can be run as an applet, servlet, or standalone application. The following shows part of the code used to plot data points within an applet:
public class AppletGraph extends JApplet { // Create X and Y axis plot dataset and populate // with data. XYPlot xyPlot = new XYPlot(); xyPlot.setDataset(defaultXYDataset); CombinedDomainXYPlot combinedDomainXYPlot = new CombinedDomainXYPlot(); combinedDomainXYPlot.add(xyPlot); // Create a jFreeChart object with the dataset JFreeChart jFreeChart = new JFreeChart(combinedDomainXYPlot); // Put the jFreeChart in a chartPanel ChartPanel chartPanel = new ChartPanel(jFreeChart); chartPanel.setPreferredSize(new Dimension(900,600)); // Add the chart panel into the display getContentPane().add(chartPanel); }
To run a chart application on the server side, a servlet container is needed—for example Apache Tomcat. The standard web.xml
file is defined to bind a URL to a servlet:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="server_charts" version="2.4" xmlns="..." xmlns:xsi="..." xsi:schemaLocation="..."> <servlet> <servlet-name>PieChartServlet</servlet-name> <servlet-class>charts.PieChartServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>PieChartServlet</servlet-name> <url-pattern>/servlets/piechart</url-pattern> </servlet-mapping> </web-app>
When the servlet container, such as Tomcat, receives an HTTP request with the URL http://localhost/servlets/piechart
, it resolves the request into a servlet application. The web server then executes the chart servlet, formats the output into an image, and returns the image content as an HTTP response.
This technology has the following advantages:
- Advanced graphics, animations, and user interfaces
- Reusable core code for different deployment options: client-side, server-side, or standalone applications
It has the following disadvantages:
- Applet security issues
- If the plugin crashes, it can hang or crash the browser
- Very CPU intensive
- Requires a Java plugin
- Long startup time
- Standardization problems
Adobe Shockwave Flash (client-side)
Flash is widely used because it offers audio, graphics, animation, and video capabilities on web browsers. Browsers are required to have the Adobe Flash Player plugin installed. As for plotting graphs, this technique was a common choice (because there weren't many other options) before the HTML5 standard became popular.
Graphing software adopting this technology ships with its own exported Shockwave Flash (SWF) files. These SWF files contain compressed vector-based graphics and compiled ActionScript instructions to create a chart. In order for the Flash Player to display the graphs, the SWF file has to be loaded from an HTML page. To do that, an HTML object
tag is needed. The tag is internally created and injected into the document's DOM by the software's own JavaScript routines.
Inside this object
tag is dimension and SWF path information for plotting the graph. The graph variable data is also passed inside this tag. So, as soon as the browser sees an object
tag with specific parameters, it calls the installed Flash Player to process both the SWF file and the parameters. To pass the graph's plot data from the server side to the client-side Flash Player, flashVars
is embedded inside a param
tag with the data type. The following is an example from Yahoo YUI 2:
<object id="yuiswf1" type="..." data="charts.swf" width="100%" height="100%"> <param name="allowscriptaccess" value="always"> <param name="flashVars" value="param1=value1¶m2=value2"> </object>
This technology has the following advantages:
- Pretty graphics and animations with rich user interactions
It has the following disadvantages:
- If the plugin crashes, it can hang or crash the browser
- Very CPU-intensive
- Long startup time
- Standardization problems