如何用值秒格式化flot char yaxis和tooltip?

时间:2015-12-23 14:29:50

标签: javascript jquery flot

我正在使用flot char。我需要显示yaxis H:i:s时间格式(例如yaxis - 00:00:00,00:05:00,00:10:00等),以及工具提示:10小时 - 00: 02:05时间有我的数组数据,第二个元素是秒:

var data = [[1, 0],[2, 0],[3, 0],[4, 0],[5, 0],[6, 0],[7, 0],[8, 0],[9, 7],[10, 123],[11, 47],[12, 0],[13, 0],[14, 0],[15, 0],[16, 0],[17, 0],[18, 0],[19, 0],[20, 0],[21, 0],[22, 0],[23, 0],[24, 0]];

它是flot char的代码:

this.options = {
                xaxis : {
                        mode : null,
                        tickLength : 5,
                        min : 1,
                        max : 24,
                        ticks: [[1, "1 hour"], [2, "2 hour"], [3, "3 hour"], [4, "4 hour"], [5, "5 hour"], [6, "6 hour"], [7, "7 hour"], [8, "8 hour"], [9, "9 hour"], [10, "10 hour"], [11, "11 hour"], [12, "12 hour"], [13, "13 hour"], [14, "14 hour"], [15, "15 hour"], [16, "16 hour"], [17, "17 hour"], [18, "18 hour"], [19, "19 hour"], [20, "20 hour"], [21, "21 hour"], [22, "22 hour"], [23, "23 hour"], [24, "24 hour"]]
                },
                series : {
                        lines : {
                                show : true,
                                lineWidth : 1,
                                fill : true,
                                fillColor : {
                                        colors : [{
                                                opacity : 0.1
                                        }, {
                                                opacity : 0.15
                                        }]
                                }
                        },
                        points: { show: true },
                        shadowSize : 0
                },
                selection : {
                        mode : "x"
                },
                grid : {
                        hoverable : true,
                        clickable : true,
                        tickColor : "#efefef",
                        borderWidth : 0,
                        borderColor : "#efefef"
                },
                tooltip : true,
                colors : ["#3734d5"]

        };

        var opt = {
                tooltipOpts : {
                        content : "<b>%x</b> hour - <span>%y</span> time",
                        defaultTheme : false
                }
        };

$.plot($("#chart"), [data], $.extend(true, opt, this.options));

如何格式化tooltipOpts内容?谢谢

1 个答案:

答案 0 :(得分:1)

您可以将content选项设置为函数而不是字符串,并像这样格式化时间值(此fiddle中的完整代码):

  content: function(label, x, y, datapoint) {
    var seconds = (y % 60).toString();
    if (seconds.length == 1) {
      seconds = "0" + seconds;
    }

    y = Math.floor(y / 60);
    var minutes = (y % 60).toString();
    if (minutes.length == 1) {
      minutes = "0" + minutes;
    }

    y = Math.floor(y / 60);
    var hours = (y % 60).toString();
    if (hours.length == 1) {
      hours = "0" + hours;
    }

    var time = hours + ":" + minutes + ":" + seconds;
    return "<b>" + x + "</b> hour - <span>" + time + "</span> time";
  }