HTML Table with Dynamic Update
Uses input fields and an HTML table to generate and dynamicaly update the chart.
by secretgspot
HTML
<script src="http://highcharts.com/js/testing.js"></script>
<div id="container" style="height: 400px; width: 500px"></div>
<table id="datatable">
<thead>
<tr>
<th></th>
<th>Jane</th>
<th>John</th>
</tr>
</thead>
<tbody>
<tr>
<th>Apples</th>
<td><input type="text" value="3" /></td>
<td><input type="text" value="4" /></td>
</tr>
<tr>
<th>Pears</th>
<td><input type="text" value="2" /></td>
<td><input type="text" value="0" /></td>
</tr>
<tr>
<th>Plums</th>
<td><input type="text" value="5" /></td>
<td><input type="text" value="11" /></td>
</tr>
<tr>
<th>Bananas</th>
<td><input type="text" value="1" /></td>
<td><input type="text" value="1" /></td>
</tr>
<tr>
<th>Oranges</th>
<td><input type="text" value="2" /></td>
<td><input type="text" value="4" /></td>
</tr>
</tbody>
</table>
JavaScript
/**
* Visualize an HTML table using Highcharts. The top (horizontal) header
* is used for series names, and the left (vertical) header is used
* for category names. This function is based on jQuery.
* @param {Object} table The reference to the HTML table to visualize
* @param {Object} options Highcharts options
*/
Highcharts.visualize = function(table, options) {
// the categories
options.xAxis.categories = [];
$('tbody th', table).each( function(i) {
options.xAxis.categories.push(this.innerHTML);
});
// the data series
options.series = [];
$('tr', table).each( function(i) {
var tr = this;
$('th, td', tr).each( function(j) {
if (j > 0) { // skip first column
if (i == 0) { // get the name and init the series
options.series[j - 1] = {
name: this.innerHTML,
data: []
};
} else { // add values
options.series[j - 1].data.push(parseFloat($('input', this)[0].value));
}
}
});
});
var chart = new Highcharts.Chart(options, function(chart){
$('table input').change(function(){
var td = $(this).parent(),
i = td.index() - 1,
x = td.parent().index();
chart.series[i].data[x].update( parseFloat(this.value) );
});
});
}
// On document ready, call visualize on the datatable.
$(document).ready(function() {
var table = document.getElementById('datatable'),
options = {
chart: {
renderTo: 'container',
defaultSeriesType: 'column'
},
title: {
text: 'Data extracted from a HTML table in the page'
},
xAxis: {
},
yAxis: {
title: {
text: 'Units'
}
},
plotOptions:{
series:{
shadow:false,
borderWidth:0,
...