Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Highcharts

You're reading from   Learning Highcharts Whether you're a novice or an advanced web developer, this practical tutorial will enable you to produce stunning interactive charts using Highcharts. With a foreword by the creator, it's the only guide you'll need to get started.

Arrow left icon
Product type Paperback
Published in Dec 2012
Publisher Packt
ISBN-13 9781849519083
Length 362 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

Table of Contents (19) Chapters Close

Learning Highcharts
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Web Charts 2. Highcharts Configurations FREE CHAPTER 3. Line, Area, and Scatter Charts 4. Bar and Column Charts 5. Pie Charts 6. Gauge, Polar, and Range Charts 7. Highcharts APIs 8. Highcharts Events 9. Highcharts and jQuery Mobile 10. Highcharts and Ext JS 11. Running Highcharts on the Server Side Index

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 was any server-side scripting language such as PHP, one of the common approaches was to use Common Gateway Interface (CGI), which executes plotting programs (such as gnuplot) to output the 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 inside 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 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 the 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:

  • Server-side technology, which means chart creation does not necessarily require user interaction to initiate.

  • 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.

It has the following disadvantages:

  • More workload on the server side

  • Pure HTML, a limited technology—little interactions can be put on the graphs and no animations

Java applet (client side) and servlet (server side)

Java applet enables the web browser to execute multiplatform Java Byte Code 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 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 on the same environment in Internet Explorer as 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 have the option of using it on a browser extending from the Applet class or running 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 Java plugin

  • Long startup time

  • Standardization problem

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 is the common choice (because there weren't many other options) before the HTML5 standard became popular.

Graphing software adopting this technology basically ship with their 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 is needed 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, it contains dimension and SWF path information for plotting the graph, and 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's 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&param2=value2">
</object>

This technology has the following advantage:

  • Pretty graphics and animations with rich user interactions

It has the following disadvantage:

  • Similar to applets

lock icon The rest of the chapter is locked
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.
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 $19.99/month. Cancel anytime
Banner background image