JSFiddle - React, Tailwind, and code Playground

by Emmanuel Umozurike

HTML

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

<input type="hidden" id="hdn_Data" value="Categories,LEADERSHIP,SERVICE,EVANGELISM,Mercy,Apostleship,Shepherding,Hospitality,Prophecy,Administration,Teaching,Faith,Wisdom,Giving,Knowledge,Discernment
Total Points,6,5,5,4,4,4,4,4,3,3,3,2,2,2,1" />


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

JavaScript

$(document).ready(function() {

    var options = {
        chart: {
            renderTo: 'container',
            defaultSeriesType: 'bar'
        },
        title: {
            text: 'Spiritual Gifts Results'
        },
        colors: [
            'green'
            ],
        xAxis: {
            categories: []
        },

        yAxis: {
            title: {
                text: 'Service'
            }
        },
        series: []
    };

    var data = document.getElementById("hdn_Data");
    // Split the lines
    if (data.value != "") {
        var lines = data.value.split('\n');

        // Iterate over the lines and add categories or series
        $.each(lines, function(lineNo, line) {
            var items = line.split(',');
            // header line containes categories
            if (lineNo == 0) {
                $.each(items, function(itemNo, item) {
                    if (itemNo > 0) options.xAxis.categories.push(item);
                });
            }
            // the rest of the lines contain data with their name in the first position
            else {
                var series = {
                    data: []
                };
                $.each(items, function(itemNo, item) {
                    var data = {};
                    if (itemNo == 0) {
                        series.name = item;
                    } else {
                        data.y = parseFloat(item);
                        if (item < 4) {
                            data.color = 'red';
                        }
                        else {
                            data.color = 'green';
                        }
                        series.data.push(data);
                    }
                });
                options.series.push(series);
              }
            });

            // Create the chart
            var chart1 = new Highcharts.Chart(options);
        }
    });