Skip to content Skip to sidebar Skip to footer

Jquery Datatable To Highcharts

'Hi everyone. I was wondering how I could pass, dynamically, jquery datatable values to highcharts from the code below. Variables like xAxis and the 2 yAxis'. I do have more than 3

Solution 1:

This is example with static datatables. This will give some idea to get started. Check comment in snippet, if some doubt please comment

$(document).ready(function() {
  var table = $('#example').DataTable();
  var data = table.rows().data();

  var categories = []; //creating array for storing browser type in array.
  for (var i = 0; i < data.length; i++) {
    categories.push(data[i][0])
  }
  var count = {}; //creating object for getting categories with count
  categories.forEach(function(i) {
    count[i] = (count[i] || 0) + 1;
  });
  //console.log(count)
  var series_data = []; //creating empty array for highcharts series data
  var categores = []; //creating empty array for highcharts categories
  Object.keys(count).map(function(item, key) {
    series_data.push(count[item]);
    categores.push(item)
  });
  //console.log(series_data)
  plotChart(series_data, categores)
});

function plotChart(series_data, categores) {
  Highcharts.chart('container', {
    chart: {
      type: 'column'
    },
    xAxis: {
      categories: categores
    },
    yAxis: {

      title: {
        text: 'Count'
      }
    },
    series: [{
      name: 'Browser',
      data: series_data
    }]

  });
}

Fiddle Demonstration

Update This is server side sample which populate the chart after loading data in datatables.

JS code

$(document).ready(function() {

  var url = 'http://www.json-generator.com/api/json/get/cbEfqLwFaq?indent=2';

  var table = $('#example').DataTable({
    'processing': true,
    'serverSide': true,
    'ajax': {
      type: 'POST',
      'url': url,
      'data': function(d) {
        console.log(d.order);
        return JSON.stringify(d);
      }
    },
    "initComplete": function(settings, json) {
      data = table.rows().data()

      var categories = []; //creating array for storing browser type in array.
      var series_data = [];
      for (var i = 0; i < data.length; i++) {
        categories.push(data[i][0])
        series_data.push(Number([data[i][5].match(/\d/g).join("")]))
      }

      plotChart(categories, series_data)
    }
  });

  //
  //
  $('#reload').click(function(e) {
    table.ajax.reload();
  });

  //    
});

function plotChart(categories, series_data) {
  Highcharts.chart('container', {
    chart: {
      type: 'column'
    },
    xAxis: {
      categories: categories
    },
    yAxis: {

      title: {
        text: 'Count'
      }
    },
    series: [{
      name: 'person',
      data: series_data
    }]

  });
}

HTML

<button id="reload">reload table</button>
<button class="toggleCols" data-column="0">First Name</button>

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

<div id="container"></div>
<br>
<table id="example" class="display" width="100%" cellspacing="0">
  <thead>
    <tr>
      <th>First name</th>
      <th>Last name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
  </thead>
  <tfoot>
    <tr>
      <th>First name</th>
      <th>Last name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
  </tfoot>
</table>

Fiddle Demo


Post a Comment for "Jquery Datatable To Highcharts"