HTML tables to Open Flash Chart using jQuery

This came in kind of useful on a client's site recently - allows you to display data in a standard HTML table and then use jQuery to read the data in the table and display a graph using Open Flash Charts in its place. It's all a bit rough but meets my requirements so I probably won't give it any more attention. I'm making it available here in case anyone else finds it useful - feel free to take it and extend it. Here's the jQuery plugin code (ofcplugin.js):



jQuery.fn.createChart = function(settings) {
    this.props = {
        title: ""
    };

    settings = jQuery.extend({
        type: "",
        width: 600,
        height: 320,
        html_title: false,
        tool_tip: "",
        x_col: 1,
        y_col: 2,
        y_max: 100,
        y_steps: 10,
        x_label_orient: 0,
        c_class: ""
    }, settings);
    var xlabels = new Array();
    var values = new Array();

    $(this).find("tbody tr td:nth-child("+settings.x_col+")").each(function() {
        xlabels[xlabels.length] = $(this).text();
    });

    $(this).find("tbody tr td:nth-child("+settings.y_col+")").each(function() {
        values[values.length] = $(this).text();
    });

    var url = "chart-data.php?";
    this.props.title = $(this).find("caption").text();

    if (!settings.html_title) {
        url += "title="+escape(this.props.title+",{font-size:10px; color:#000000}");
    }

    url += "&x_legend="+escape($(this).find("thead th:nth-child(1)").text()+",10,#AAAAAA");
    url += "&y_legend="+escape($(this).find("thead th:nth-child(2)").text()+",10,#AAAAAA");
    url += "&y_label_size="+escape("15");
    url += "amp;&y_steps="+escape(settings.y_steps);
    url += "&values="+escape(values.join(","));
    url += "&x_labels="+escape(xlabels.join(","));
    url += "&y_max="+escape(settings.y_max);
    url += "&bg_colour="+escape("#FFFFFF");
    url += "&x_axis_colour="+escape("#000000");
    url += "&x_grid_colour="+escape("#AAAAAA");
    url += "&y_axis_colour="+escape("#000000");
    url += "&y_grid_colour="+escape("#AAAAAA");
    url += "&x_label_style="+escape("8,#000000,"+settings.x_label_orient+",1,#000000");
    url += "&type="+escape(settings.type);

    if (settings.tool_tip.length > 0) {
        url += "&tool_tip="+escape(settings.tool_tip);
    }

    var outer = $("
 
"); var inner = $("
 
"); outer.append(inner); if (settings.html_title) { outer.prepend("

"+this.props.title+"

"); } $(this).after(outer); $(inner).flash( { src: "open-flash-chart.swf", width: settings.width, height: settings.height, flashvars: {data: url} },{version:8} ); return this; }

Here's the PHP file which creates the chart data (chart-data.php):


<?php

// use the chart class to build the chart:
include_once( 'ofc-library/open-flash-chart.php' );
$g = new graph();

$x_labels = explode(',', $_GET['x_labels']);

for ($i=0; $i

And here's an example of usage:


<table id="table1">
	<caption>Some data</caption>
	<thead>
		<tr>
			<th>Labels</th>
			<th>Values</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>Label 1</td>
			<td>65</td>
		</tr>
		<tr>
			<td>Label 2</td>
			<td>22</td>
		</tr>
		<tr>
			<td>Label 3</td>
			<td>18</td>
		</tr>
		<tr>
			<td>Label 4</td>
			<td>18</td>
		</tr>
	</tbody>
</table>

<script type="text/javascript" src="jquery-1.1.3.js"></script>
<script type="text/javascript" src="jquery.flash.js"></script>
<script type="text/javascript" src="ofcplugin.js"></script>
<script type="text/javascript">

$(document).ready(function() {
    $("#table1").createChart(
        {type:"bar", c_class:"graph centercontent", html_title:true, tool_tip:"#x_label#
#val#%25"}).hide();
});

</script>

Also requires:

See it in action: http://www.playlink.org.uk/articles/?p=20.

shamasis on March 21st 2010

Hi there. You can do the same using the jQuery plugin for FusionCharts v3 and FusionCharts free. See: http://www.fusioncharts.com/jquery/

The jQuery plugin allows users to select a table in jQuery and run convertToFusionCharts() function!

 

"All it takes is humanity ~ http://www.shamasis.net/"

Stu (not verified) on December 19th 2007

Good job! Personally I prefer the flash chart route but the real benefit of both of these approaches is that the HTML itself remains unchanged whichever we choose. I do like the way your solution is self-contained whereas mine currently uses the "script in the middle" to provide data to the chart.

Rebecca Murphey (not verified) on December 19th 2007

Along these same lines, I just came up with a jQuery plugin to solve a similar problem. It will take a table and turn it into a graph using the flot plugin, which lets you skip the Flash part. You can read more about it here: http://blog.rebeccamurphey.com/2007/12/17/graph-table-data-jquery-flot/

Stuart Metcalfe (not verified) on October 22nd 2007

So I see ;) As long as the comments are relevant, I have no problem with a little self-promotion.

monk.e.boy (not verified) on October 22nd 2007

FlashTech and monk.e.boy are both SEOing and linking hard :-) haha, fun :-)

Stuart Metcalfe (not verified) on October 04th 2007

Hey, thanks! I noticed the js interface in your documentation when I downloaded the latest version but decided to go with the current implementation for the time being. Certainly, I look forward to hearing about the improvements when they happen and may revisit my code once it becomes available (and stable). Excellent software by the way :)

monk.e.boy (not verified) on October 04th 2007

Hey, nice post. I wrote Open flash Chart, so thanks for the link love. I am currently implementing a way of passing variables to the chart using swfobject. This should make ajax and stuff easier. I'll drop you a line when it is finished (I am without internet for a few weeks, so please be patient!)

FlashTech (not verified) on October 02nd 2007

That's great! I will surely inform you all once I make use of the code above. Good blog btw :D

Stuart Metcalfe (not verified) on October 01st 2007

Certainly, this method could be used by many charting/graphing products. FusionCharts looks good from the demos although I chose Open Flash Charts because of its use of the 'free as in speech' GPL licence. Thanks for the post though, and feel free to make use of my code and publish any changes if you make any. Consider the code on this page published under a GPL licence for that purpose.

FlashTech (not verified) on October 01st 2007

brilliant post...but you could also create interactive charts with FusionCharts. a free version is also available. it offers a lot of chart type and can be used with any scripting language. the online documentation clearly shows how to use FusionCharts with PHP. you can get more info at: http://www.fusioncharts.com/free/

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

  • Allowed HTML tags: <a> <p> <span> <div> <h1> <h2> <h3> <h4> <h5> <h6> <img> <hr> <br> <br /> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <em> <b> <u> <i> <strong> <font> <del> <ins> <sub> <sup> <quote> <blockquote> <pre> <code> <cite> <strike> <caption>
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.