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

标签 javascript highcharts

我正在使用带面积图的 Highcharts。

如何自定义 xAxis 标签后缀1st, 2nd, 3rd 等...?

Online Demo

enter image description here

当前格式:

第 01 小时、第 02 小时、第 03 小时 ....

这必须更改如下:

第 1 小时、第 2 小时、第 3 小时....

我曾尝试使用ordinal 方法进行上述转换,但没有成功...

可能是序号脚本放错了?

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/

相关文章:

javascript - jQuery:如何隐藏和显示href?

javascript - 减少 Highcharts 中的 x 轴标签密度

javascript - Highcharts 文本气泡

javascript - 如何获取父div内元素的距离(以像素为单位)

javascript - jQuery sortable - 根据输入字段的值排序

javascript - Highcharts 饼图数据格式通过 php

javascript - 使 highcharts 中的标题可点击并使用 bootstrap popover?

javascript - 如何在堆积条形图中设置系列数据元素的最小高度?

javascript - 删除彼此相邻出现多次的字符串 - javascript 或 jquery

javascript - 使用javascript将div标签插入页面顶部