JSFiddle - React, Tailwind, and code Playground
by oakley808
HTML
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<style type="text/css">
#datatable td {border:1px solid #ccc; text-align:center;}
#datatable th {background-color:#f1f1f1;}
#datatable th,
#datatable td { padding:3px;}
</style>
<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
<table id="datatable">
<thead>
<tr>
<th></th>
<th>Michael</th>
<th>Amy</th>
</tr>
</thead>
<tbody>
<tr>
<th>IPAs</th>
<td>14</td>
<td>14</td>
</tr>
<tr>
<th>Pilsners</th>
<td>1</td>
<td>1</td>
</tr>
<tr>
<th>Lagers</th>
<td>1</td>
<td>1</td>
</tr>
</tbody>
</table>
JavaScript
$(function () {
// On document ready, call visualize on the datatable.
$(document).ready(function() {
/**
* 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(this.innerHTML));
}
}
});
});
var chart = new Highcharts.Chart(options);
}
var table = document.getElementById('datatable'),
options = {
chart: {
renderTo: 'container',
type: 'column'
},
title: {
text: 'Data extracted from a HTML table in the page'
},
xAxis: {
},
yAxis: {
title: {
text: 'Units'
}
},
...