user2782405
user2782405

Reputation: 393

Highcharts xAxis formatter export to csv

I have a formatter function in xAxis. When I export the data to csv, it generates 0,1,2 instead of the custom xAxis formatted data.

I'm using highcharts v7.1.2.

 xAxis: {
                title: {
                    text: 'Year'
                },
                labels: {
                    formatter: function () {
                        var months = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.",
                            "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."];

                        var day = this.value + 1;

                        var date= DateFromDay(2019, day);

                        return months[date.getMonth()] + ' ' + date.getDate();
                    }
                }
            }

How to get formatted data in export file. Thanks in advance for your help!

Upvotes: 0

Views: 561

Answers (1)

Wojciech Chmiel
Wojciech Chmiel

Reputation: 7372

By default Highcharts export x and y values of each point. Using xAxis.labels.formatter callback the axis labels can be modified but points values are the same.

This issue can be resolved by wrapping Highcharts.Chart.prototype.getDataRows method and modify the array with rows to export. Check demo and code posted below:

Code:

function formatXAxisLabels (value) {
    return value * 10 + "s";
}

(function(H) {
  H.wrap(H.Chart.prototype, 'getDataRows', function(proceed, multiLevelHeaders) {
    var rows = proceed.call(this, multiLevelHeaders);

    rows = rows.map(row => {
      if (H.defined(row.x)) {
        row[0] = formatXAxisLabels(row.x);
      }
      return row;
    });

    return rows;
  });
}(Highcharts));

Demo:

Upvotes: 1

Related Questions