// generateChart is a required function which will be called to generate your JS chart
generateChart = function(options) 
{
    // This is the div you draw your chart into
    var $chartDrawDiv = $(options.divSelector);
    //console.log(JSON.stringify(options));
   
   // Use require to load the javascript libraries you need
   require(['http://www.gstatic.com/charts/loader.js'],function() 
   {
       google.charts.load("current", {packages: ["corechart"]});
       google.charts.setOnLoadCallback(function()
       {
           // Process the data for your chart with the data in options.dataset.data if necessary
           var processedData = processData(options.dataset.data);
           
           doDrawing(processedData, $chartDrawDiv, options.dataset.chart_information.height, options.dataset.chart_information.width, options.dataset.attributes.default_colours);
       });

   });
},

processData = function(dataset) 
{
   // Convert the raw data JSON to the format required for the chart if required.
   var ds = [['Demographic', 'Invoiced Amount', 'Invoice Estimate']];
    for (var i = 0; i < dataset['demographic'].length; i++){
      ds.push([
          dataset['demographic'][i].formatted_data,
          parseFloat(dataset['invoiced_amount'][i].raw_data),
          parseFloat(dataset['invoice_estimate'][i].raw_data),
        ]);}
    
  return google.visualization.arrayToDataTable(ds);
},

doDrawing = function(data, $chartDrawDiv, height, width, colors) 
{
   // Do the actual drawing of the chart into $chartDiv.

   var chart = new google.visualization.AreaChart($chartDrawDiv[0]);
 
    var options = {
        title: 'Winter Performance',
        width: width,
        height: height,
        hAxis: {title: 'Demographic',  titleTextStyle: {color: '#333'}},
        vAxis: {minValue: 0}
    };




   chart.draw(data, options);
   
}

