// JS
var chart;
JSC.fetch('./resources/appleProductsSales.csv')
  .then(function(response) {
    return response.text();
  })
  .then(function(text) {
    renderChart(JSC.csv2Json(text));
  });
function renderChart(data) {
  var series = [
    {
      name: 'Comulative Devices sold',
      type: 'area',
      points: JSC.nest()
        .key('date')
        .rollup('total_units_sold')
        .points(data)
    },
    {
      name: 'Active Devices',
      type: 'line',
      color: 'lightgray',
      points: JSC.nest()
        .key('date')
        .rollup('active_devices')
        .points(data)
    }
  ];
  chart = JSC.chart('chartDiv', {
    debug: true,
    title: {
      label: {
        text: 'Cumulative Devices (thousands)'
      },
      position: 'center'
    },
    defaultPoint_marker_type: 'none',
    legend_visible: false,
    xAxis: [
      {
        id: 'mainX',
        defaultTick: {
          label_rotate: 90,
          gridLine_visible: false
        }
      },
      {
        // Distance from the chart area edge. Negative values offset the axis towards the middle of the chart area.
        caOffset: -50,
        scale_syncWith: 'mainX',
        defaultTick_enabled: false,
        customTicks: [
          {
            value: ['Q4 2013', 'Q4 2018'],
            rangeMode: 'measure',
            label: {
              text: 'LIFESPAN',
              color: 'white'
            },
            line_color: 'white'
          }
        ]
      }
    ],
    yAxis: [
      {
        defaultTick: {
          enabled: false,
          rangeMode: 'measure',
          gridLine_visible: false
        },
        alternateGridFill: 'none',
        id: 'mainY',
        orientation: 'opposite',
        // Distance from the chart area edge. Negative values offset the axis towards the middle of the chart area.
        caOffset: -55,
        customTicks: [
          {
            value: 1471.63,
            label_text: 'Active'
          },
          {
            value: 2443.54,
            label_text: 'Sold'
          },
          {
            value: [1471.63, 2443.54],
            label_text: 'Retired'
          }
        ]
      },
      {
        scale_syncWith: 'mainY',
        defaultTick_line_visible: false
      }
    ],
    series: series
  });
}
date,total_units_sold,active_devices
Q3 2007,11.58,25.31
Q4 2007,25.06,28.63
Q1 2008,51.82,32.38
Q2 2008,66.45,36.6
Q3 2008,80.68,41.36
Q4 2008,101.23,46.72
Q1 2009,130.84,52.76
Q2 2009,147.86,59.55
Q3 2009,165.89,67.19
Q4 2009,186.49,75.76
Q1 2010,219.56,85.37
Q2 2010,242.14,96.13
Q3 2010,266.69,108.16
Q4 2010,297.92,121.58
Q1 2011,345.07,136.54
Q2 2011,381.19,153.18
Q3 2011,422.27,171.63
Q4 2011,461.97,192.04
Q1 2012,535.04,214.57
Q2 2012,593.19,239.34
Q3 2012,647.03,266.48
Q4 2012,698.24,296.13
Q1 2013,786.17,328.37
Q2 2013,852.66,363.26
Q3 2013,906.84,400.86
Q4 2013,962.79,441.15
Q1 2014,1050.75,484.09
Q2 2014,1117.72,529.59
Q3 2014,1173.54,577.48
Q4 2014,1233.29,627.56
Q1 2015,1334.7,679.57
Q2 2015,1413.05,733.2
Q3 2015,1476.31,788.08
Q4 2015,1539.95,843.82
Q1 2016,1636.16,900
Q2 2016,1701.63,956.18
Q3 2016,1756.23,1011.92
Q4 2016,1815.9,1066.8
Q1 2017,1912.64,1120.43
Q2 2017,1976.52,1172.44
Q3 2017,2033.26,1222.52
Q4 2017,2095.66,1270.41
Q1 2018,2191.26,1315.91
Q2 2018,2256.67,1358.85
Q3 2018,2313.24,1399.14
Q4 2018,2375.14,1436.74
Q1 2019,2443.54,1471.63
Q2 2019,,1503.87
Q3 2019,,1533.52
Q4 2019,,1560.66
Q1 2020,,1585.43