javascript - highcharts:如何正确地将 float 显示为 xAxis 中的日期时间

标签 javascript highcharts

我正在尝试绘制以下示例数据。我想将 x 轴显示为 float 中的日期时间。但它继续显示为 1970-01-01。有人可以帮助我了解如何正确获取它吗?

我复制了如下的 Highcharts 示例。但我想将 x 轴显示为日期,例如“2017-03-20”,而不是原始 float 。

非常感谢您的帮助!

谢谢。

    Highcharts.chart('container', {
        chart: {
            zoomType: 'xy'
        },
        title: {
            text: 'Average Monthly Temperature and Rainfall in Tokyo'
        },
        credits: {
            text: 'Songhuiming',
            href: 'http://www.songhuiming.com'
        },
        subtitle: {
            text: 'Source: WorldClimate.com'
        },
        xAxis: [{
            type: 'datetime',
            dateTimeLabelFormats: {day: '%Y-%b-%d'},
            categories: [1489298400000.0, 1492923600000.0, 1492318800000.0, 1480226400000.0, 1494133200000.0, 1490504400000.0, 1488088800000.0, 1475384400000.0, 1493528400000.0, 1491109200000.0, 1480831200000.0, 1471755600000.0],
            crosshair: true,
            labels: {rotation: 90, step: 2}
        }],
        yAxis: [{ // Primary yAxis
            labels: {
                format: '{value}°C',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            title: {
                text: 'Temperature',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            }
        }, { // Secondary yAxis
            title: {
                text: 'Rainfall',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            labels: {
                format: '{value} mm',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            opposite: true
        }],
        tooltip: {
            shared: true
        },
        legend: {
            layout: 'vertical',
            align: 'left',
            x: 120,
            verticalAlign: 'top',
            y: 100,
            floating: true,
            backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
        },
        series: [{
            name: 'Rainfall',
            type: 'column',
            yAxis: 1,
            data: [1, 7, 2, 1, 4, 1, 1, 1, 4, 6, 1, 1],
            tooltip: {
                valueSuffix: ' mm'
            }

        }, {
            name: 'Temperature',
            type: 'spline',
            data: [110, 105, 104, 96, 102, 93, 93, 105, 118, 119, 101, 94],
            tooltip: {
                valueSuffix: '°C'
            }
        }]
    });

最佳答案

似乎属性 dateTimeLabelFormats 无法正常工作,因此您可以将浮点时间戳转换为格式化日期字符串:

new Date(1489298400000.0).toLocaleDateString()

这里是完整的例子:

Highcharts.chart('container', {
        chart: {
            zoomType: 'xy'
        },
        title: {
            text: 'Average Monthly Temperature and Rainfall in Tokyo'
        },
        credits: {
            text: 'Songhuiming',
            href: 'http://www.songhuiming.com'
        },
        subtitle: {
            text: 'Source: WorldClimate.com'
        },
        xAxis: [{
            categories: [new Date(1489298400000.0).toLocaleDateString(), new Date(1492923600000.0).toLocaleDateString(), new Date(1492318800000.0).toLocaleDateString(), new Date(1480226400000.0).toLocaleDateString(), new Date(1494133200000.0).toLocaleDateString(), new Date(1490504400000.0).toLocaleDateString(), new Date(1488088800000.0).toLocaleDateString(), new Date(1475384400000.0).toLocaleDateString(), new Date(1493528400000.0).toLocaleDateString(), new Date(1491109200000.0).toLocaleDateString(), new Date(1480831200000.0).toLocaleDateString(), new Date(1471755600000.0).toLocaleDateString()],
            crosshair: true,
            labels: {rotation: 90, step: 2}
        }],
        yAxis: [{ // Primary yAxis
            labels: {
                format: '{value}°C',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            title: {
                text: 'Temperature',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            }
        }, { // Secondary yAxis
            title: {
                text: 'Rainfall',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            labels: {
                format: '{value} mm',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            opposite: true
        }],
        tooltip: {
            shared: true
        },
        legend: {
            layout: 'vertical',
            align: 'left',
            x: 120,
            verticalAlign: 'top',
            y: 100,
            floating: true,
            backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
        },
        series: [{
            name: 'Rainfall',
            type: 'column',
            yAxis: 1,
            data: [1, 7, 2, 1, 4, 1, 1, 1, 4, 6, 1, 1],
            tooltip: {
                valueSuffix: ' mm'
            }

        }, {
            name: 'Temperature',
            type: 'spline',
            data: [110, 105, 104, 96, 102, 93, 93, 105, 118, 119, 101, 94],
            tooltip: {
                valueSuffix: '°C'
            }
        }]
    });
<script src="https://code.highcharts.com/highcharts.js"></script>

<div id="container" style="min-width: 310px; max-width: 800px; height: 400px; margin: 0 auto"></div>

关于javascript - highcharts:如何正确地将 float 显示为 xAxis 中的日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48837966/

相关文章:

javascript - window.opener 在弹出窗口中重定向后无用 (JavaScript)

javascript - 选择“选择选项”时显示文本。怎么做?

javascript - 使用经典 ASP 上传

javascript - HighCharts 图表中的单引号未正确显示

javascript - Highcharts 中的反向轴从表中获取数据

javascript - 获取倒数第二个元素 MongoDB

javascript - 尝试使用 jQuery 的 get 方法填充 html 选项列表

javascript - Highcharts 系列格式问题(AngularJs)

javascript - Highcharts 中样条图表中的标记/圆圈

Highcharts:动态设置 y 轴最大值和最小值,而不是在创建时