Highcharts column chart using HTML table data

To create a column chart using HTML table data with Highcharts, you can follow these steps:

  • Include the Highcharts library in your HTML file. You can either download it from the Highcharts website or include it from a CDN like this:
<script src="https://code.highcharts.com/highcharts.js"></script>
  • Create a container element for the chart in your HTML file, like this:
<div id="chart-container"></div>
  • Create an HTML table with the data for the chart. The table should have a header row with column labels, and subsequent rows with data values. For example:
<table id="data-table">
  <thead>
    <tr>
      <th>Month</th>
      <th>Apples</th>
      <th>Oranges</th>
      <th>Bananas</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>January</td>
      <td>5</td>
      <td>3</td>
      <td>4</td>
    </tr>
    <tr>
      <td>February</td>
      <td>7</td>
      <td>2</td>
      <td>6</td>
    </tr>
    <tr>
      <td>March</td>
      <td>3</td>
      <td>6</td>
      <td>5</td>
    </tr>
  </tbody>
</table>
  • In your JavaScript code, extract the data from the HTML table and convert it to a format that Highcharts can use. You can use jQuery or plain JavaScript to select the table elements and iterate over the rows and columns. For example:
var categories = [];
var seriesData = [];

$('#data-table tbody tr').each(function(i) {
  var $row = $(this);
  var month = $row.find('td:first-child').text();
  categories.push(month);

  $row.find('td:not(:first-child)').each(function(j) {
    var $cell = $(this);
    var value = parseInt($cell.text(), 10);

    if (seriesData.length <= j) {
      seriesData.push({ name: $cell.attr('data-name'), data: [] });
    }

    seriesData[j].data.push(value);
  });
});

Note that we're using jQuery to select the table elements and iterate over the rows and columns. We're also using the 'data-name' attribute on the table cells to set the series names.

  • Create a Highcharts chart object and configure it with the data and chart options. In this case, we want a column chart, so we'll set the chart type to 'column', and we'll use the extracted data to set the categories and series data, like this:
Highcharts.chart('chart-container', {
  chart: {
    type: 'column'
  },
  title: {
    text: 'Column Chart with HTML Table Data'
  },
  xAxis: {
    categories: categories
  },
  yAxis: {
    title: {
      text: 'Value'
    }
  },
  series: seriesData
});

Note that we're using the extracted data to set the categories and series data for the chart.

  • Finally, add some CSS to style the chart container and make it responsive, like this:
#chart-container {
  width: 100%;
  height: 500px;
  max-width: 800px;
  margin: 0 auto;
}
  1. Highcharts Column Chart with dynamic HTML table data:

    • Create a Highcharts Column Chart dynamically by fetching data from an HTML table.
    <div id="container"></div>
    <table id="dataTable">
        <thead>
            <tr>
                <th>Category</th>
                <th>Data Point</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Category 1</td>
                <td>10</td>
            </tr>
            <tr>
                <td>Category 2</td>
                <td>15</td>
            </tr>
            <!-- Additional rows... -->
        </tbody>
    </table>
    <script>
        // Fetch data from HTML table and create Highcharts Column Chart
        const tableData = [];
        $('#dataTable tbody tr').each(function () {
            const category = $(this).find('td:eq(0)').text();
            const dataPoint = parseInt($(this).find('td:eq(1)').text(), 10);
            tableData.push([category, dataPoint]);
        });
    
        Highcharts.chart('container', {
            chart: {
                type: 'column'
            },
            title: {
                text: 'Column Chart from HTML Table Data'
            },
            xAxis: {
                categories: tableData.map(item => item[0])
            },
            series: [{
                name: 'Data Points',
                data: tableData.map(item => item[1])
            }],
            // Other configuration options...
        });
    </script>
    
  2. Convert HTML table data to Highcharts Column Chart:

    • Utilize JavaScript to convert HTML table data into a Highcharts Column Chart.
    <!-- HTML table and container div -->
    <div id="container"></div>
    <table id="dataTable">
        <!-- Table content... -->
    </table>
    <script>
        // JavaScript code to convert HTML table data to Highcharts Column Chart
        // Refer to the previous example for the conversion logic.
    </script>
    
  3. Highcharts Column Chart with JSON data from HTML table:

    • Transform JSON data obtained from an HTML table into a Highcharts Column Chart.
    <!-- HTML table and container div -->
    <div id="container"></div>
    <table id="dataTable">
        <!-- Table content... -->
    </table>
    <script>
        // JavaScript code to convert HTML table data to JSON
        const jsonData = [];
        $('#dataTable tbody tr').each(function () {
            const category = $(this).find('td:eq(0)').text();
            const dataPoint = parseInt($(this).find('td:eq(1)').text(), 10);
            jsonData.push({ category, dataPoint });
        });
    
        Highcharts.chart('container', {
            chart: {
                type: 'column'
            },
            title: {
                text: 'Column Chart from JSON Data'
            },
            xAxis: {
                categories: jsonData.map(item => item.category)
            },
            series: [{
                name: 'Data Points',
                data: jsonData.map(item => item.dataPoint)
            }],
            // Other configuration options...
        });
    </script>
    
  4. Interactive Highcharts Column Chart with HTML table integration:

    • Integrate interactive features such as tooltips, click events, etc., for a seamless user experience.
    <!-- HTML table and container div -->
    <div id="container"></div>
    <table id="dataTable">
        <!-- Table content... -->
    </table>
    <script>
        // JavaScript code to create an interactive Highcharts Column Chart
        const tableData = [];
        $('#dataTable tbody tr').each(function () {
            const category = $(this).find('td:eq(0)').text();
            const dataPoint = parseInt($(this).find('td:eq(1)').text(), 10);
            tableData.push([category, dataPoint]);
        });
    
        Highcharts.chart('container', {
            chart: {
                type: 'column'
            },
            title: {
                text: 'Interactive Column Chart from HTML Table Data'
            },
            xAxis: {
                categories: tableData.map(item => item[0])
            },
            series: [{
                name: 'Data Points',
                data: tableData.map(item => item[1]),
                point: {
                    events: {
                        click: function () {
                            alert('Clicked on ' + this.category);
                        }
                    }
                }
            }],
            tooltip: {
                formatter: function () {
                    return '<b>' + this.category + '</b><br>' + 'Data Point: ' + this.y;
                }
            },
            // Other configuration options...
        });
    </script>
    
  5. Customizing appearance of Highcharts Column Chart from HTML table:

    • Customize appearance options such as colors, border styles, and background colors.
    <!-- HTML table and container div -->
    <div id="container"></div>
    <table id="dataTable">
        <!-- Table content... -->
    </table>
    <script>
        // JavaScript code to create a custom-styled Highcharts Column Chart
        const tableData = [];
        $('#dataTable tbody tr').each(function () {
            const category = $(this).find('td:eq(0)').text();
            const dataPoint = parseInt($(this).find('td:eq(1)').text(), 10);
            tableData.push([category, dataPoint]);
        });
    
        Highcharts.chart('container', {
            chart: {
                type: 'column',
                backgroundColor: '#f2f2f2'
            },
            title: {
                text: 'Styled Column Chart from HTML Table Data'
            },
            xAxis: {
                categories: tableData.map(item => item[0])
            },
            series: [{
                name: 'Data Points',
                data: tableData.map(item => item[1]),
                color: '#64b4dc' // Column color
            }],
            // Other configuration options...
        });
    </script>
    
  6. Adding tooltips to Highcharts Column Chart from HTML table:

    • Enhance user experience by adding tooltips to the Highcharts Column Chart.
    <!-- HTML table and container div -->
    <div id="container"></div>
    <table id="dataTable">
        <!-- Table content... -->
    </table>
    <script>
        // JavaScript code to create Highcharts Column Chart with tooltips
        const tableData = [];
        $('#dataTable tbody tr').each(function () {
            const category = $(this).find('td:eq(0)').text();
            const dataPoint = parseInt($(this).find('td:eq(1)').text(), 10);
            tableData.push([category, dataPoint]);
        });
    
        Highcharts.chart('container', {
            chart: {
                type: 'column'
            },
            title: {
                text: 'Column Chart with Tooltips from HTML Table Data'
            },
            xAxis: {
                categories: tableData.map(item => item[0])
            },
            series: [{
                name: 'Data Points',
                data: tableData.map(item => item[1])
            }],
            tooltip: {
                formatter: function () {
                    return '<b>' + this.category + '</b><br>' + 'Data Point: ' + this.y;
                }
            },
            // Other configuration options...
        });
    </script>
    
  7. Updating Highcharts Column Chart dynamically with HTML table changes:

    • Enable dynamic updates to the Highcharts Column Chart based on changes in the HTML table.
    <!-- HTML table and container div -->
    <div id="container"></div>
    <table id="dataTable">
        <!-- Table content... -->
    </table>
    <script>
        // JavaScript code to dynamically update Highcharts Column Chart
        function updateChart() {
            const updatedData = [];
            $('#dataTable tbody tr').each(function () {
                const category = $(this).find('td:eq(0)').text();
                const dataPoint = parseInt($(this).find('td:eq(1)').text(), 10);
                updatedData.push([category, dataPoint]);
            });
    
            chart.update({
                xAxis: {
                    categories: updatedData.map(item => item[0])
                },
                series: [{
                    data: updatedData.map(item => item[1])
                }]
            });
        }
    
        // Initial chart creation
        const tableData = [];
        $('#dataTable tbody tr').each(function () {
            const category = $(this).find('td:eq(0)').text();
            const dataPoint = parseInt($(this).find('td:eq(1)').text(), 10);
            tableData.push([category, dataPoint]);
        });
    
        const chart = Highcharts.chart('container', {
            chart: {
                type: 'column'
            },
            title: {
                text: 'Dynamic Column Chart from HTML Table Data'
            },
            xAxis: {
                categories: tableData.map(item => item[0])
            },
            series: [{
                name: 'Data Points',
                data: tableData.map(item => item[1])
            }],
            // Other configuration options...
        });
    
        // Example: Trigger the update on button click
        $('#updateButton').click(function () {
            updateChart();
        });
    </script>
    
  8. Highcharts Column Chart using tabular data in HTML:

    • Integrate tabular data directly from HTML into the Highcharts Column Chart.
    <!-- HTML table and container div -->
    <div id="container"></div>
    <table id="dataTable">
        <!-- Table content... -->
    </table>
    <script>
        // JavaScript code to create Highcharts Column Chart from HTML table
        Highcharts.chart('container', {
            chart: {
                type: 'column'
            },
            title: {
                text: 'Column Chart from HTML Table'
            },
            xAxis: {
                categories: $('#dataTable tbody tr').map(function () {
                    return $(this).find('td:eq(0)').text();
                }).get()
            },
            series: [{
                name: 'Data Points',
                data: $('#dataTable tbody tr').map(function () {
                    return parseInt($(this).find('td:eq(1)').text(), 10);
                }).get()
            }],
            // Other configuration options...
        });
    </script>