XForms/Google Charts

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Motivation[edit | edit source]

You want to use a web service to create charts.

Method[edit | edit source]

In this example we will use the Google Chart web service: *. Users are each allowed to generate up to 50,000 charts per day.

Parameters[edit | edit source]

The Google Chart application takes several parameters from the URL. For a simple Pie Chart these might include:



http://chart.apis.google.com/chart? - is the Chart API's base URL
The ampersand (&) separates parameters.
cht=p is a code for the chart's type. For example p=2D Pie Chart and p3-3D Pie Chart
chd=t:10,20,30,40 is the chart's data using t format (t:10,20,30) or s format where (s:) a=1 and z=26
chs=400x300 - is the chart's size in pixels.
chl=Amount|Indicator|Code|Date|Text are the labels for the Pie Chart.

Our next step is to put these REST parameters into an XForms instance and hook the instance up to input controls.

Pie Chart Parameters in XML instance[edit | edit source]

Here are the parameters for a piechart type, data, label and size information.

<xf:instance id="chart-params" xmlns="">

Chart Submission[edit | edit source]

We will submit our XForms data to the server using the following submission statement.

<xf:submission id="get-chart"
   method="get" action="http://chart.apis.google.com/chart"  
   separator="&amp;" ref="instance('chart-params')" replace="all"/>

Binding Rules[edit | edit source]

Here is a sample input form for chart parameters:

Input to Chart Generated by Google Charts

Here is a sample output chart generated form this application.

Chart Generate by Google Charts

Sample XForms Application on Google Code[edit | edit source]


Sample Program[edit | edit source]

<html xmlns:xf="http://www.w3.org/2002/xforms"
        <title>Google Pie Chart Demo</title>
        <style type="text/css">
            @namespace xf url("http://www.w3.org/2002/xforms");
            body {font-family: Helvetica, sans-serif}
            /* This line ensures all the separate input controls appear on their own lines */
            xf|input, xf|select1 {display:block; margin:5px 0;}
            /* this puts the labels in 200px columns and right aligns them */
            xf|input > xf|label, xf|select1 > xf|label
            {text-align:right; padding-right:10px; width:200px; float:left; text-align:right;}
            .xf-value {width: 250px}
            <xf:instance id="chart-params" xmlns="">
            <xf:submission id="get-chart" action="http://chart.apis.google.com/chart" method="get" 
                separator="&amp;" ref="instance('chart-params')" replace="all"/>
            <!-- put the cursor in the first field when the form becomes ready -->
            <xf:action ev:event="xforms-ready">
                <xf:setfocus control="field-1"/>
        <h3>Google PieChart Demo</h3>

        <xf:select1 ref="cht" id="field-1">
            <xf:label>Chart Type: </xf:label>
                <xf:label>Pie Chart - flat</xf:label>
                <xf:label>Pie Chart - 3D</xf:label>
        <xf:input ref="chd">
            <xf:label>Data: (t:5,10,20): </xf:label>
        <xf:input ref="chl">
            <xf:label>Labels: (A|B) </xf:label>
        <xf:submit submission="get-chart">
            <xf:label>Create Chart</xf:label>

Discussion[edit | edit source]

This is actually one of the most simple applications. Google Charts has five chart types and hundreds of combinations of parameters. The Chart Types are (cht)

  1. Line charts
  2. Bar charts
  3. Pie charts
  4. Venn diagrams
  5. Scatter plots

One way to test these is to generate a variety of charts with some sample XForms.

Next Page: Venn Diagram | Previous Page: Pie Chart
Home: XForms