Highrise Test

Read chart data from HTML

by loleg

HTML

<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/data.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>

<!-- Based on Newscoop table -->
<table class="datatable" lang="column" title="" summary="Units" style="width:400px; height:400px">
	<thead>
		<tr>
			<th></th>
			<th>Jane</th>
			<th>John</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<th>Apples</th>
			<td>3</td>
			<td>4</td>
		</tr>
		<tr>
			<th>Pears</th>
			<td>2</td>
			<td>0</td>
		</tr>
		<tr>
			<th>Plums</th>
			<td>5</td>
			<td>11</td>
		</tr>
		<tr>
			<th>Bananas</th>
			<td>1</td>
			<td>1</td>
		</tr>
		<tr>
			<th>Oranges</th>
			<td>2</td>
			<td>4</td>
		</tr>
	</tbody>
</table>

CSS

.datatable th { font-weight: bold; font-style: italic }
.datatable td, .datatable th { margin:0; padding:0.1em; }
.datatable tr { border-bottom: 1px solid #aaa; }

JavaScript

$(function () {
    $('.datatable').each(function(ix) {
        tbl = $(this).hide();
        tbl.before('<div id="datacontainer' + ix + '"></div>').after('<button id="datasource' + ix + '">Quelldaten</button>');
        $('#datasource' + ix).click(function() {
            tbl.fadeIn(); $(this).hide();  
        }).css('margin-left', parseInt(tbl.css('width'))/1.15);
        $('#datacontainer' + ix).css({
            width: tbl.css('width'),
            height: tbl.css('height'),
            margin: '0 auto'
        }).highcharts({
            data: {
                table: this
            },
            chart: {
                type: tbl.attr('lang')
            },
            title: {
                text: tbl.attr('title')
            },
            yAxis: {
                allowDecimals: false,
                title: {
                    text: tbl.attr('summary')
                }
            },
            tooltip: {
                formatter: function() {
                    return '<b>'+ this.series.name +'</b><br/>'+
                        this.y +' '+ this.x.toLowerCase();
                }
            }
        });
        tbl.css('height','auto').css('width','100%');
    });
});