【问题标题】:How to show correctly ticks to time data in a Pie chart label using highcharts?如何使用 highcharts 在饼图标签中正确显示时间数据的刻度?
【发布时间】:2019-08-29 19:06:25
【问题描述】:

我会尽量简明扼要,我尝试渲染一个饼图并设置标签以显示总时间,我得到了刻度,我想在饼图中显示它。

据我所知,饼图没有坐标轴,我找到的所有替代方案都配置在 X 或 Y 轴上。 像这样的:

xAxis: {
    type: 'datetime', //y-axis will be in milliseconds
},

在工具提示中:

formatter: function () {
    return '<b>' + this.series.name + '</b><br/>' +
    Highcharts.dateFormat('%H:%M:%S', new Date(this.y));
},

这是我的文本代码

        Highcharts.chart('container', {
                        chart: {
                            type: 'pie',
                            options3d: {
                                enabled: true,
                                alpha: 45,
                                beta: 0
                            }
                        },
                        credits: {
                            enabled: false
                        },
                        title: {
                            text: ''
                        },
                        subtitle: {
                            text: ''
                        },
                        tooltip: {
                            formatter: function () {
                                return '<b>' + this.series.name + '</b><br/>' +
                                    Highcharts.dateFormat('%H:%M:%S', new Date(this.y));
                            },
                            shared: true,
                            useHTML: true
                        },
                        plotOptions: {
                            pie: {
                                allowPointSelect: true,
                                cursor: 'pointer',
                                depth: 35,
                                dataLabels: {
                                    enabled: true,
                                    format: '{point.name}'
                                }
                            }
                        },
                        series: [{
                            type: 'pie',
                            data: [
                                    [ 'T1',30600000000  ],
                            ]
                        }]
                    });

我尝试了一些在线刻度转换器,它们都将 '30600000000' 转换为 00:51:00 (HH:mm:ss) ut 在我的标签中显示为 04:00:00。

这是一个小提琴 https://jsfiddle.net/lvevano/gp80ckfr/2/ 我做错了什么?

【问题讨论】:

标签: javascript html json highcharts


【解决方案1】:

您有所不同,因为您用于 Date 的构造函数接受 EPOCH 日期,并且您正在向它传递刻度。您需要将其转换为 EPOCH,以便 javascript 可以理解它。

var yourDate = new Date(30600000000); // Your date
console.log(yourDate);
var convertedToEpoch = (yourDate.getTime() - 621355968000000000) / 10000;
console.log(new Date(convertedToEpoch));

用一种新方法更新了您的小提琴,将刻度转换为纪元。

Highcharts.chart('container', {
  chart: {
    type: 'pie',
    options3d: {
      enabled: true,
      alpha: 45,
      beta: 0
    }
  },
  credits: {
    enabled: false
  },
  title: {
    text: ''
  },
  subtitle: {
    text: ''
  },
  tooltip: {
    formatter: function() {
      return '<b>' + this.series.name + '</b><br/>' +
        Highcharts.dateFormat('%H:%M:%S', new Date(convertTicksToEpoc(this.y)));
    },
    shared: true,
    useHTML: true
  },
  plotOptions: {
    pie: {
      allowPointSelect: true,
      cursor: 'pointer',
      depth: 35,
      dataLabels: {
        enabled: true,
        format: '{point.name}'
      }
    }
  },
  series: [{
    type: 'pie',
    data: [
      ['T1', 30600000000],
      ['T2', 20400000000],
    ]
  }]
});

function convertTicksToEpoc(ticks) {
  return (ticks - 621355968000000000) / 10000;
}
<script src="https://code.highcharts.com/highcharts.js"></script>
<div id="container" style="min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto"></div>

您可以在此链接 https://devkimchi.com/2018/11/04/converting-tick-or-epoch-to-timestamp-in-logic-app/ 上了解这两个日期

【讨论】:

    猜你喜欢
    • 2016-11-29
    • 1970-01-01
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多