Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highchart show string on x-axis

I am trying to show my x-axis values as a string on highcharts for a series but instead get (0,1,2....) which is the xaxis array index position. How do I show the xaxis value formatted as a string on higcharts?

Here is what I have

Highcharts.chart('container', {
  title: {
    text: 'Chart with time'
  },
  xAxis: {
    type: 'datetime',
                "labels": {
                "formatter": function() {
                        console.log(this);
                        console.log(this.value.toString());
                    return this.value
                }                    
            },
  },
  series: [{
    data: [
      ['Jan-1', 100],
      ['Jan-2', 120],
      ['Jan-3', 130]
    ]
  }]
});

Would like to see 'Jan-1', 'Jan-2' as the labels on the x-axis. Here is a link to the fiddle below https://jsfiddle.net/dLfv2sbd/2/

like image 622
lboyel Avatar asked Mar 09 '23 05:03

lboyel


2 Answers

Remove labels and use type: 'category' instead of type: 'datetime':

Highcharts.chart('container', {
  title: {
    text: 'Chart with time'
  },
  xAxis: {
    type: 'category',
  },
  series: [{
    data: [
      ['Jan-1', 100],
      ['Jan-2', 120],
      ['Jan-3', 130]
    ]
  }]
});

Check the fiddle.

like image 115
Gerry Avatar answered Mar 19 '23 10:03

Gerry


You can still use datetime this way.

Highcharts.chart('container', {
  title: {
    text: 'Chart with time'
  },
  xAxis: {
    type: 'datetime',
                "labels": {
                "formatter": function() {
                    return Highcharts.dateFormat('%b-%e', this.value)
                }                    
            },
  },
  series: [{
    data: [
      [Date.UTC(2017,0,1), 100],
      [Date.UTC(2017,0,2), 120],
      [Date.UTC(2017,0,3), 130]
    ]
  }]
});

https://jsfiddle.net/dLfv2sbd/4/

like image 21
NovaPenguin Avatar answered Mar 19 '23 10:03

NovaPenguin