在 jQuery flot 中在 y 轴上显示小时

发布于 2024-12-12 09:04:15 字数 637 浏览 1 评论 0原文

我制作了一个图表,在 y 轴上显示小时。但现在我必须将时间转换为小数。

2:30 将是 2.5

As it is Looking now

我知道你可以将 y 轴设置为时间模式,但我可以无法让它工作:

        yaxis: {
            mode: "time",
            timeformat: "%H:%M"

        },

但是我应该如何将小时数传递给 jQuery Plot:“1:40”不起作用

我想让它看起来像这样,只需要几个小时而不是几分钟

Analytics example

我在互联网上搜索帮助,jQuery API 没有正确解释它: http://flot.googlecode.com/svn/trunk/API.txt

非常感谢您的帮助!

I have made a chart where i show hours on y axis. But now i have to convert the time to a decimal.

2:30 would be 2.5

As it is looking now

I know that you can set y axis to time mode, but I can not get it to work:

        yaxis: {
            mode: "time",
            timeformat: "%H:%M"

        },

But how should i deliver the hours to jQuery Plot: "1:40" do not work

I would like to make it look this way with just hours instead of minutes

Analytics example

I have search the internet for help and the jQuery API do not explain it properly:
http://flot.googlecode.com/svn/trunk/API.txt

Thank you very much for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

dawn曙光 2024-12-19 09:04:15

您想要做的只是使用轴 tickFormatter 来伪造它。 “时间”模式实际上应该被称为“日期”模式,因为它是关于日期而不是时间。时间真的只是偶然。因此,如果您已经完成了将时间转换为十进制的工作,只需使用 JavaScript 函数将它们转换回您想要的格式:

function timeTickFormatter(val,axis) {
  var hours = Math.floor(val);
  var minutes = (val - hours) * 60;

  return hours+":"+minutes;
}

这显然并没有涵盖所有排列(即,如果您需要的话,可以在前导零) ,但这就是想法。如果您不知道如何将其放入 flot 中,请在绘图选项中如下所示:

$.plot(placeholder, data, {
  ..,
  yaxis: {
    ..,
    tickFormatter:timeTickFormatter,
    ..
  }
});

What you want to do is just fake it up using the axis tickFormatter. The "time" mode should really be called something like "date" mode, because it's about dates not times. The time is just incidental really. So if you're already doing the work to convert the times to a decimal, just use a javascript function to convert them back to the format you want:

function timeTickFormatter(val,axis) {
  var hours = Math.floor(val);
  var minutes = (val - hours) * 60;

  return hours+":"+minutes;
}

That doesn't cover all the permutations obviously (i.e. leading zeros if you want them), but that's the idea. If you don't know how to put this into flot, it's like this in your plot options:

$.plot(placeholder, data, {
  ..,
  yaxis: {
    ..,
    tickFormatter:timeTickFormatter,
    ..
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文