0

I'm using Google Charts Gantt to draw several charts in my page, this is the code:

function drawGantt() {

  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Task ID');
  data.addColumn('string', 'Task Name');
  data.addColumn('string', 'Resource');
  data.addColumn('date', 'Start Date');
  data.addColumn('date', 'End Date');
  data.addColumn('number', 'Duration');
  data.addColumn('number', 'Percent Complete');
  data.addColumn('string', 'Dependencies');

  data.addRows([
    ['2014Spring', 'Spring 2014', 'spring',
     new Date(2014, 2, 22), new Date(2014, 5, 20), null, 100, null],
    ['2014Summer', 'Summer 2014', 'summer',
     new Date(2014, 5, 21), new Date(2014, 8, 20), null, 100, null],
    ['2014Autumn', 'Autumn 2014', 'autumn',
     new Date(2014, 8, 21), new Date(2014, 11, 20), null, 100, null],
    ['2014Winter', 'Winter 2014', 'winter',
     new Date(2014, 11, 21), new Date(2015, 2, 21), null, 100, null],
    ['2015Spring', 'Spring 2015', 'spring',
     new Date(2015, 2, 22), new Date(2015, 5, 20), null, 50, null],
    ['2015Summer', 'Summer 2015', 'summer',
     new Date(2015, 5, 21), new Date(2015, 8, 20), null, 0, null],
    ['2015Autumn', 'Autumn 2015', 'autumn',
     new Date(2015, 8, 21), new Date(2015, 11, 20), null, 0, null],
    ['2015Winter', 'Winter 2015', 'winter',
     new Date(2015, 11, 21), new Date(2016, 2, 21), null, 0, null],
    ['Football', 'Football Season', 'sports',
     new Date(2014, 8, 4), new Date(2015, 1, 1), null, 100, null],
    ['Baseball', 'Baseball Season', 'sports',
     new Date(2015, 2, 31), new Date(2015, 9, 20), null, 14, null],
    ['Basketball', 'Basketball Season', 'sports',
     new Date(2014, 9, 28), new Date(2015, 5, 20), null, 86, null],
    ['Hockey', 'Hockey Season', 'sports',
     new Date(2014, 9, 8), new Date(2015, 5, 21), null, 89, null]
  ]);

    var els = document.getElementsByClassName("gantt-chart");

    for(i = 0; i < els.length; i++){

        var el = els[i];

        var options = {
            width: 1100,
            height: 400,
            chartArea: {width: '100%', height: '100%'},
            gantt: {
                trackHeight: 30
            }
        };

        var chart = new google.visualization.Gantt(el);
        chart.draw(data, options);
    }
}

As you can see at the end I'm using a loop over elements in the HTML and I assign a chart to each of them but the result is that all the charts except 1 are always empty, which is rendered randomises everytime.

I've read this article which relates to a similar problem with a different chart but I can't really apply that solution because I can easily have more than 15 charts rendered at the same time in the page.

What can I do to render all these charts at the same time without having conflicts with draw function?

Thanks in advance.

Community
  • 1
  • 1
Aitor Martin
  • 724
  • 1
  • 11
  • 26

1 Answers1

0

code seems to work fine here...

google.charts.load('current', {'packages':['gantt']});
google.charts.setOnLoadCallback(drawGantt);

function drawGantt() {

  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Task ID');
  data.addColumn('string', 'Task Name');
  data.addColumn('string', 'Resource');
  data.addColumn('date', 'Start Date');
  data.addColumn('date', 'End Date');
  data.addColumn('number', 'Duration');
  data.addColumn('number', 'Percent Complete');
  data.addColumn('string', 'Dependencies');

  data.addRows([
    ['2014Spring', 'Spring 2014', 'spring',
     new Date(2014, 2, 22), new Date(2014, 5, 20), null, 100, null],
    ['2014Summer', 'Summer 2014', 'summer',
     new Date(2014, 5, 21), new Date(2014, 8, 20), null, 100, null],
    ['2014Autumn', 'Autumn 2014', 'autumn',
     new Date(2014, 8, 21), new Date(2014, 11, 20), null, 100, null],
    ['2014Winter', 'Winter 2014', 'winter',
     new Date(2014, 11, 21), new Date(2015, 2, 21), null, 100, null],
    ['2015Spring', 'Spring 2015', 'spring',
     new Date(2015, 2, 22), new Date(2015, 5, 20), null, 50, null],
    ['2015Summer', 'Summer 2015', 'summer',
     new Date(2015, 5, 21), new Date(2015, 8, 20), null, 0, null],
    ['2015Autumn', 'Autumn 2015', 'autumn',
     new Date(2015, 8, 21), new Date(2015, 11, 20), null, 0, null],
    ['2015Winter', 'Winter 2015', 'winter',
     new Date(2015, 11, 21), new Date(2016, 2, 21), null, 0, null],
    ['Football', 'Football Season', 'sports',
     new Date(2014, 8, 4), new Date(2015, 1, 1), null, 100, null],
    ['Baseball', 'Baseball Season', 'sports',
     new Date(2015, 2, 31), new Date(2015, 9, 20), null, 14, null],
    ['Basketball', 'Basketball Season', 'sports',
     new Date(2014, 9, 28), new Date(2015, 5, 20), null, 86, null],
    ['Hockey', 'Hockey Season', 'sports',
     new Date(2014, 9, 8), new Date(2015, 5, 21), null, 89, null]
  ]);

  var els = document.getElementsByClassName("gantt-chart");

  for(i = 0; i < els.length; i++){
    var el = els[i];

    var options = {
      width: 1100,
      height: 400,
      chartArea: {width: '100%', height: '100%'},
      gantt: {
        trackHeight: 30
      }
    };

    var chart = new google.visualization.Gantt(el);
    chart.draw(data, options);
  }
}
div {
  border: 1px solid #e0e0e0;
  padding: 6px;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div class="gantt-chart"></div>
<div class="gantt-chart"></div>
<div class="gantt-chart"></div>
<div class="gantt-chart"></div>
<div class="gantt-chart"></div>
<div class="gantt-chart"></div>
WhiteHat
  • 59,912
  • 7
  • 51
  • 133