javascript - 绘制点标签的显示未正确显示在每月第一天的 X 范围图表上 - Highcharts

标签 javascript c# css asp.net highcharts

我目前正在制作一个 x 范围图来显示事件的时间线。将光标放在条形图上时,用于显示数据信息的弹出窗口出现问题。

对于任何特定月份的每个第一个日期,我都没有获得所需的准确信息。

这是我的代码:

Highcharts.chart('container', {
    chart: {
       type: 'xrange',
        styledMode: true
    },
    title: {
        text: 'Highcharts X-range'
    },
    xAxis: {
        type: 'datetime'
    },
    yAxis: {
        title: {
            text: ''
        },
        categories: ['Proto', 'Dev', 'Test'],
        reversed: true
        },
    series: [{
        name: 'Project:',

        pointWidth: 20,
        data: [{
            x: Date.UTC(2014, 0, 1),
            x2: Date.UTC(2014, 5, 2),
            y: 0,
            partialFill: 0.25
        },  {
            x: Date.UTC(2014, 2, 1),
            x2: Date.UTC(2014, 3, 19),
            y: 1
        }, {
            x: Date.UTC(2014, 11, 1),
            x2: Date.UTC(2014, 11, 23),
            y: 2
        }],
          dataLabels: {
          enabled: true
        }
       }]

      });

我希望输出的格式如下:

2014 年 1 月 1 日,星期三 - 2014 年 6 月 2 日,星期一 元素:原型(prototype)

但实际输出是:

2014 年 1 月 - 2014 年 6 月 元素:原型(prototype)

如果有人能帮我解决这个问题,我将不胜感激。

谢谢。

最佳答案

您可以使用 tooltip.formatter 回调函数自定义工具提示内容,并使用 Highcharts.dateFormat() 方法根据需要格式化日期。检查下面发布的代码和演示。

HTML:

<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/xrange.js"></script>
<div id="container"></div>

JS:

Highcharts.chart('container', {
  chart: {
    type: 'xrange',
    styledMode: true
  },
  title: {
    text: 'Highcharts X-range'
  },
  xAxis: {
    type: 'datetime'
  },
  yAxis: {
    title: {
      text: ''
    },
    categories: ['Proto', 'Dev', 'Test'],
    reversed: true
  },
  tooltip: {
    formatter: function() {
      var point = this,
        series = point.series,
        color = point.color || series.chart.options.colors[point.colorIndex],
        firstDate = Highcharts.dateFormat('%A, %b %e, %Y', point.x),
        secondDate = Highcharts.dateFormat('%A, %b %e, %Y', point.x2),
        header = '<span style="font-size: 10px">' + firstDate + ' - ' + secondDate + '</span><br/>',
        content = '<span style="color:' + color + '">\u25CF</span> ' + series.name + ': <b>' + point.yCategory + '</b><br/>';

      return header + content;
    }
  },
  series: [{
    name: 'Project',
    pointWidth: 20,
    data: [{
      x: Date.UTC(2014, 0, 1),
      x2: Date.UTC(2014, 5, 2),
      y: 0,
      partialFill: 0.25
    }, {
      x: Date.UTC(2014, 2, 1),
      x2: Date.UTC(2014, 3, 19),
      y: 1
    }, {
      x: Date.UTC(2014, 11, 1),
      x2: Date.UTC(2014, 11, 23),
      y: 2
    }],
    dataLabels: {
      enabled: true
    }
  }]

});

演示:
https://jsfiddle.net/BlackLabel/3wbscy6q/

API 引用:
https://api.highcharts.com/highcharts/tooltip.formatter
https://api.highcharts.com/class-reference/Highcharts#.dateFormat
https://api.highcharts.com/highcharts/tooltip.dateTimeLabelFormats

关于javascript - 绘制点标签的显示未正确显示在每月第一天的 X 范围图表上 - Highcharts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54359597/

相关文章:

javascript - 鼠标在 HTML 5 响应式 Canvas 中的位置

c# - 序列化私有(private)成员数据

C# 主项目和子项目引用

c# - 使用任务并行库构建任务处理管道

html - CSS中绝对定位和相对定位之间是否存在父子关系?

javascript - 你如何使用pdf.js获取客户端存储的pdf的文本内容?

javascript - JS超时不触发

javascript - 如何在 JavaScript 中按照指定的正则表达式模式拆分数组

html - CSS:在保持纵横比的同时居中和放大或缩小图像

css - 如何使用 WinJS 将分割 View Pane 元素与底部对齐