Set null dates on Google Chart Timeline - Javascript Google Chart

Javascript examples for Google Chart:Line Chart

Description

Set null dates on Google Chart Timeline

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*from ww w  .  java  2  s .  c  om*/
  google.charts.load('current', {
    'packages': ['timeline']
  });
  google.charts.setOnLoadCallback(drawChart);
  function drawChart() {
    var container = document.getElementById('timeline');
    var chart = new google.visualization.Timeline(container);
    var dataTable = new google.visualization.DataTable();
    dataTable.addColumn({
      type: 'string',
      id: 'President'
    });
    dataTable.addColumn({
      type: 'string',
      id: 'Category'
    });
    dataTable.addColumn({
      type: 'date',
      id: 'Start'
    });
    dataTable.addColumn({
      type: 'date',
      id: 'End'
    });
    dataTable.addRows([
      ['Washington', 'NTC', new Date(2016, 6, 4), new Date(2016, 6, 7)],
      ['Adams', 'Baseline', new Date(2016, 6, 4), new Date(2016, 6, 14)],
      ['Adams', 'Forecast', new Date(), new Date()],
      ['Adams', 'Actual', new Date(), new Date()],
      ['Jefferson', 'Baseline', new Date(2016, 6, 8), new Date(2016, 6, 14)],
      ['Jefferson', 'Forecast', new Date(), new Date()],
      ['Jefferson', 'Actual', new Date(), new Date()],
      ['Tyson', 'Baseline', new Date(2016, 6, 9), new Date(2016, 6, 12)],
      ['Tyson', 'Forecast', new Date(), new Date()],
      ['Tyson', 'Actual', new Date(), new Date()],
    ]);
    var filterDate = new Date();
    filterDate.setMilliseconds(0);
    filterDate = filterDate.toJSON();
    function testFunc(value) {
      value.setMilliseconds(0);
      return value.toJSON() !== filterDate;
    }
    var someRows = dataTable.getFilteredRows([{
      column: 2,
      test: testFunc
    }, {
      column: 3,
      test: testFunc
    }]);
    var dataView = new google.visualization.DataView(dataTable);
    dataView.setRows(someRows);
    var colors = [];
    var colorMap = {
      Baseline: '#425cfb',
      Forecast: '#f8ac08',
      Actual: '#06af90',
      NTC: '#1b9e2e'
    }
    for (var i = 0; i < dataTable.getNumberOfRows(); i++) {
      colors.push(colorMap[dataTable.getValue(i, 1)]);
    }
    var options = {
      colors: colors,
      timeline: {
        showBarLabels: false,
        groupByRowLabel: true,
        barLabelStyle: {
          fontName: 'Garamond',
          fontSize: 6
        },
      },
    };
    chart.draw(dataView, options);
  }
    }

      </script> 
   </head> 
   <body> 
      <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> 
      <script type="text/javascript" src="https://www.google.com/jsapi"></script> 
      <div id="timeline" style="width:100%; height: auto;"></div>  
   </body>
</html>

Related Tutorials