JSFiddle - React, Tailwind, and code Playground

by mark47

HTML

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

<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>

<table id="datatable">
           <thead>
            <tr>
              <th></th>
              <th class="cat">1</th>
              <th class="cat">2</th>
              <th class="cat">3</th>
              <th class="cat">4</th>
              <th class="cat">5</th>
              <th class="cat">6</th>
              <th class="cat">7</th>
              <th class="cat">8</th>
            </tr> 
          </thead> 
          <tbody>
            <tr class="data">
              <td>Suspected</td>
              <td>60</td>
              <td>70</td>
              <td>82</td>
              <td>55</td>
              <td>40</td>
              <td>47</td>
              <td>59</td>
              <td>47</td>
            </tr> 
            <tr>
              <td>Confirmed</td>
              <td>40</td>
              <td>22</td>
              <td>30</td>
              <td>16</td>
              <td>36</td>
              <td>20</td>
              <td>21</td>
              <td>30</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 = [];
            $('thead tr th:first', table).nextAll(function {
                //console.log('boog');
                options.xAxis.categories.push(this.innerHTML);
                console.log(options.xAxis.categories);
            });

            // the data series
            options.series = [];
            $('tbody tr', table).each(function(i) {
                var tr = this;
                $('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));
                        }
                    }
                    console.log(options.series.data);
                });
            });

            var chart = new Highcharts.Chart(options);
        }

        var table = document.getElementById('datatable'),
            options = {
                chart: {
                    renderTo: 'container',
                    type: 'line'
                },
                title: {
                    text: 'Data extracted from a HTML table in the page'
 ...