【问题标题】:How to change tool tip using High Charts如何使用 Highcharts 更改工具提示
【发布时间】:2016-01-20 01:11:41
【问题描述】:

我已经成功地使用 High Chart 创建了一个图表来比较患者的预期时间和到达时间。

但是,我现在遇到了列的label 的问题;在milliseconds 中显示时间。

下面的屏幕截图显示了问题:

如何更改标签以显示时间格式 (H:M:S)

代码:

<script type="text/javascript">
        function drawChart(){
            var chart = new Highcharts.Chart({
                chart: {
                    renderTo: 'divforchart',
                    type:'column',
                },
                xAxis: {
                    name:'patients',
                    categories: [<?php
                    echo "'".$names[0]."'";
                    for($i = 1; $i < sizeof($names); $i++){
                        echo ",'".$names[$i]."'";
                    }
                    ?>]
                },
                yAxis: {
                    type: 'datetime',
                    dateTimeLabelFormats: { 
                        //force all formats to be hour:minute:second
                        second: '%H:%M:%S',
                        minute: '%H:%M:%S',
                        hour: '%H:%M:%S',
                        day: '%H:%M:%S',
                        week: '%H:%M:%S',
                        month: '%H:%M:%S',
                        year: '%H:%M:%S'
                    },
                    min: <?php echo "Date.UTC(".gmdate("Y,m,d,H",strtotime($minDate)).")";?>
                },

                series: [
                    {
                        name: 'Arrival time',
                        data: [<?php
                            echo "['".$names[0]."',Date.UTC(".gmdate("Y,m,d,H,i,s",strtotime($Arrival_time[0])).")]";
                            for($i = 1; $i < sizeof($names); $i++){
                                echo "
                                ,['".$names[$i]."',Date.UTC(".gmdate("Y,m,d,H,i,s",strtotime($Arrival_time[$i])).")]";
                            }
                        ?>]
                    },
                    {
                        name: 'Expected time',
                        data: [<?php
                            echo "['".$names[0]."',Date.UTC(".gmdate("Y,m,d,H,i,s",strtotime($Expected_time[0])).")]";
                            for($i = 1; $i < sizeof($names); $i++){
                                echo "
                                ,['".$names[$i]."',Date.UTC(".gmdate("Y,m,d,H,i,s",strtotime($Expected_time[$i])).")]";
                            }
                        ?>]
                    }
                ]
            });
        }
    </script>
    </head>
    <body onLoad="drawChart()">
     <div id="divforchart" style="height: 400px"></div>
    </body>

【问题讨论】:

    标签: javascript datetime highcharts tooltip


    【解决方案1】:

    尝试:

    Highcharts.dateFormat(values)
    

    请看这里:http://api.highcharts.com/highcharts#Highcharts.dateFormat%28%29

    如果你想在意甲中使用它,或者你可以更改工具提示。

    series: [{
            dataLabels: {
                enabled: true,
                formatter:function() {
                    return Highcharts.dateFormat('%H:%M:%S',this.y);
                },
                style:{color:"black"}
            },
            data: [data]
        }]
    

    工具提示:

    tooltip:{
        formatter:function() {
            return Highcharts.dateFormat('%H:%M:%S',this.y);
        }
    }
    

    您的代码必须如下所示:

    chart: {
                        renderTo: 'divforchart',
                        type:'column',
                    },
    tooltip: { ...
    

    【讨论】:

    • 我按照建议插入了您的代码,但一旦执行,图表根本不显示。
    • 删除颜色:颜色[0],我在代码中将颜色声明为 var,我编辑了我的答案。或者使用 F12 检查您的控制台并查看错误。
    【解决方案2】:

    从外观上看,您指的是工具提示,而不是 dataLabel。 您可以使用 formatter 函数正确格式化日期:

    http://api.highcharts.com/highcharts#tooltip.formatter

    (如果您实际上是在谈论 dataLabel,您仍然可以做同样的事情:http://api.highcharts.com/highcharts#plotOptions.series.dataLabels.formatter)

    我认为默认是对 y 轴值进行 numberFormat,但您会希望在格式化程序中使用 dateFormat 函数:

    http://api.highcharts.com/highcharts#Highcharts.dateFormat%28%29

    如果您在实施方面需要帮助,请创建一个 jsfiddle (http://jsfiddle.net/) 并回发

    【讨论】:

    • 是的,你是对的,它是工具提示,而不是数据标签。虽然,我不确定这个功能在哪里或如何编辑它?
    • 详细信息在我的答案中链接到的示例中。也看看这里:jsfiddle.net/JVNjs/289
    • 在我的代码中,我认为没有指定工具提示,因为我在任何地方都看不到它,但工具提示显然有效?
    • 工具提示默认启用并已建立默认格式。
    • 我已经给你链接到文档,它们链接到图表示例。我给了你一个使用你的确切场景的例子。你所要做的就是看那些例子:) 写得很清楚......
    猜你喜欢
    • 1970-01-01
    • 2022-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多