title: How to calculate last months as a period and update to chart by javascript
body: <p>I need to update the chart based on time period.</p>
  
  <p>currently chart is updating all values for 3,6,9,12 months from january to December</p>
  
  <p>Now i need to update the chart by</p>
  
  <ul>
  <li>last 3 months - (means current month is december so i need to get the chart for Dec, nov, october)</li>
  <li>last 6 months (Dec, nov, october, september,August,july)</li>
  <li>last 9 months (Dec, nov, october, september,August, july, june, may, april)</li>
  <li>January to this month (get the chart for january to current month)</li>
  </ul>
  
  <p>I am already calculating the current month, but i dont know how to attach to chart.</p>
  
  <p>one more thing i am calling same chart function, thrice, is there a way to make it less code. i tried but i am not able to figure it out.</p>
  
  <p>Any help is Appreciated</p>
  
  <h2>JSFIDDLE</h2>
  
  <p><strong><a href="http://jsfiddle.net/jygh8obh/22/" rel="nofollow">http://jsfiddle.net/jygh8obh/22/</a></strong></p>
  
  <p>Html</p>
  
  <pre><code>&lt;select id="chartType"&gt;
  &lt;option value="bar"&gt;bar chart&lt;/option&gt;
  &lt;option value="area"&gt;area chart&lt;/option&gt;
  &lt;option value="line"&gt;line chart&lt;/option&gt;
  &lt;option value="spline"&gt;spline chart&lt;/option&gt;
  &lt;/select&gt;
  
  &lt;select id="SelectPeriod" &gt;
  &lt;option value="3"&gt;January to this month&lt;/option&gt;
  &lt;option value="3"&gt;Last 3 months&lt;/option&gt;
  &lt;option value="6"&gt;Last 6 months&lt;/option&gt;
  &lt;option value="9"&gt;Last 9 months&lt;/option&gt;
  &lt;option value="12"&gt;Last 12 months&lt;/option&gt;
  &lt;/select&gt;
  
  &lt;div id="chart"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>Javascript</p>
  
  <pre><code>var data = [
  ['data4', 'data2', 'data3'],
  [90, 120, 300],
  [40, 160, 240],
  [50, 200, 290],
  [120, 160, 230],
  [80, 130, 300],
  [90, 220, 320],
  [90, 120, 300],
  [40, 160, 240],
  [50, 200, 290],
  [120, 160, 230],
  [80, 130, 300],
  [90, 220, 320]
  ];
  
  
  //calculating current month
  var currentMonth = new Date().getMonth();
  console.log(currentMonth);
  
  //c3 customisation
  var chart = c3.generate({
  bindto : '#chart',
  data : {
      rows : data,
      type : 'bar',
  },
  axis : {
      x : {
          tick : {
              format : function (d) {
                  return formatMonth(d);
              }
          }
      }
  }
  });
  
  //select chart type
  $("#chartType").change(function (evt) {
  var chartSelection = $("#chartType").val();
  var timePeriod = $("#SelectPeriod").val();
  var chart = c3.generate({
      data : {
          rows : data.slice(0, parseInt(timePeriod) + 1),
          type : chartSelection
      },
      axis : {
          x : {
              tick : {
                  format : function (d) {
                      return formatMonth(d);
                  }
              }
          }
      }
  });
  
  });
  
  //select time period
  $("#SelectPeriod").change(function (evt) {
  var chartSelection = $("#chartType").val();
  var timePeriod = $("#SelectPeriod").val();
  var chart = c3.generate({
      data : {
          rows : data.slice(0, parseInt(timePeriod) + 1),
          type : chartSelection
      },
      axis : {
          x : {
              tick : {
                  format : function (d) {
                      return formatMonth(d);
                  }
              }
          }
      }
  });
  });
  
  //function for months
  function formatMonth(d) {
  if (!d) {
  return "JAN";
  }
  var months = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
  return month = months[d];
  }
  </code></pre>