Export Custom data columns

Export custom data columns in CSV export

by Sanjay

HTML

<script src="http://code.highcharts.com/stock/highstock.js"></script>
<script src="http://highcharts.github.io/export-csv/export-csv.js"></script>

<div id="container" style="height: 300px; margin-top: 2em"></div>


<button id="getcsv">Alert CSV</button>

JavaScript

var chart = new Highcharts.StockChart({

    chart: {
        renderTo: 'container'
    },

    navigator: {
        series: {
            includeInCSVExport: false
        }
    },
    series: [{
        data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],
        pointStart: Date.UTC(2013, 0, 1),
        pointInterval: 24 * 36e5
    }],
    
    exporting: {
        csv: {
            dateFormat: '%Y-%m-%d'
        }
    }

});


$('#getcsv').click(function () {
// Store the original series
var original_series = chart.series;
// Create a fake series
var series = [
  {
    name: 'A',
    points: [],
    options: {
      stickyTracking: true
    }
  },
  {
    name: 'A + 10',
    points: [],
    options: {
      stickyTracking: true
    }
  }
];

  // Clone the xAsix data and yAxis data, If you are using more than one yAxis then use:
  //var options = JSON.parse(JSON.stringify(chart.series[0].options));
//var data = options.data;
//console.log(data);
// You can get Y values and X values in data variable.
// Otherwise use simply Points as below
  var xPoints = JSON.parse(JSON.stringify(chart.series[0].xData));
  var yPoints = JSON.parse(JSON.stringify(chart.series[0].yData));
  xPoints.forEach(function(val, i) {
    series[0].points.push({
      x: val,
      y: yPoints[i],
    });
    series[1].points.push({
    	x: val,
      y: yPoints[i] + 10,
    });
  });
  // Replace chart series with new fake series
  	chart.series = series;
    alert(chart.getCSV());
    // Set original series back
    chart.series = original_series;
});