我正在使用带有区域图的Highcharts。

如何按小时自定义xAxis label,后缀为1st, 2nd, 3rd等...?


  Online Demo


javascript - Highcharts-dateTimeLabelFormats的自定义格式(第一小时,第二小时…)-LMLPHP

当前格式:

01th hour, 02th hour, 03th hour ....

必须对此进行如下更改:

1st hour, 2nd hour, 3rd hour...。

我试图通过使用序数方法进行上述转换,但是却无法正常工作...

可能放置的顺序脚本是错误的?

var s=["th","st","nd","rd"],
          v=n%100;
      return n+(s[(v-20)%10]||s[v]||s[0]);


完整代码:

$(function (n) {

  // Everything in common between the charts
  var commonOptions = {
    colors: ['#fe5758', '#99cc03', '#33b5e6', '#fd8f40', '#e7ca60', '#40abaf', '#f6f7f8', '#e9e9eb'],
    chart: {
      style: {
        fontFamily: 'Roboto Light',
        fontWeight: 'normal',
        fontSize: '12px',
        color: '#585858',
      }
    },
    title: {
      text: null
    },
    subtitle: {
      text: null
    },
    credits: {
      enabled: false
    },
    exporting: {
      enabled: false
    },
    xAxis: {
      title: {
        style: {
          fontFamily: 'Roboto',
          color: "#000",
        }
      },
      labels: {
        style:{ color: '#000' }
      },
      lineColor: '#e4e4e4',
      lineWidth: 1,
      tickLength: 0,
    },
    yAxis: {
      title: {
        style: {
          fontFamily: 'Roboto',
          color: "#000",
        }
      },
      offset:-6,
      labels: {
        style:{ color: '#bbb' }
      },
      tickInterval: 100,
      lineColor: '#e4e4e4',
      lineWidth: 1,
      gridLineDashStyle: 'dash',
    },
    series: [{
      backgroundColor: "rgba(0 ,0, 0, 0.5)",
    }],

    // Area Chart
    plotOptions: {
      area: {
        stacking: 'normal',
        lineWidth: 1,
        fillOpacity: 0.1,
        marker: {
          lineWidth: 1.5,
          symbol: 'circle',
          fillColor: 'white',
        },
        legend: {
          radius: 2,
        }
      }
    },
    tooltip: {
      useHTML: true,
      shared: true,
      backgroundColor: '#5f5f5f',
      borderWidth: 0,
      style: {
        padding:10,
        color: '#fefefe',
      }
    },
    legend: {
      itemStyle: {
        fontFamily: 'Roboto Light',
        fontWeight: 'normal',
        fontSize: '12',
        color: '#666666',
      },
      marker: {
        symbol: 'square',
        verticalAlign: 'middle',
        radius: '4',
      },
      symbolHeight: 6,
      symbolWidth: 6,
    },
  };

  $('#areaChartTwoWay-time').highcharts(Highcharts.merge(commonOptions, {
    chart: { type: 'area' },
    xAxis: {
      title: { text: 'Hours', },
      type: 'datetime',
      min:Date.UTC(2016,04,16,1,0,0,0),
      minRange: 12 * 3600 * 500,
      dateTimeLabelFormats: {
        hour: '%Hth hour',
      },
    },
    yAxis: {
      tickInterval: 30,
      title: { text: 'Total views', },
      offset:0,
    },
    series: [{
      name: 'Unique open',
      lineColor: '#fb8b4b',
      marker: { lineColor: '#fb8b4b', fillColor: 'white', },
      data: [88, 68, 79, 39, 48, 69, 69, 18, 40, 62, 91, 61, 39],
      legendIndex:1,
      pointInterval: 3600 * 500,
      pointStart:Date.UTC(2016,04,16,1,0,0,0),
    }, {
      name: 'Forwards',
      lineColor: '#99cc03',
      marker: { lineColor: '#99cc03', fillColor: 'white', },
      data: [3, 8, 15, 12, 61, 32, 43, 18, 9, 12, 22, 18, 12],
      legendIndex:0,
      pointInterval: 3600 * 500,
      pointStart:Date.UTC(2016,04,16,1,0,0,0),
    }]
  }));

  var s=["th","st","nd","rd"],
      v=n%100;
  return n+(s[(v-20)%10]||s[v]||s[0]);

  $('.highcharts-grid > path:last-child').remove();
  $('.highcharts-markers > path:last-child').remove();

});

最佳答案

您可以调用另一个函数来获取序数。一个快速的谷歌发现这种方法:



function getGetOrdinal(n) {
   var s=["th","st","nd","rd"],
       v=n%100;
   return n+(s[(v-20)%10]||s[v]||s[0]);
}





这需要一个数字并返回一个字符串,该字符串由数字加上正确的序号组成。

但是,这不适用于dateTimeLabelFormats。供您使用,您可以使用简单的标签格式化程序。

我也在这里更新了您的在线演示:

http://jsfiddle.net/6nz0sod1/2/

希望这可以帮助

资料来源:https://ecommerce.shopify.com/c/ecommerce-design/t/ordinal-number-in-javascript-1st-2nd-3rd-4th-29259

关于javascript - Highcharts-dateTimeLabelFormats的自定义格式(第一小时,第二小时…),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36565555/

10-17 01:30