谷歌用相对“时间戳”绘制时间线图表

发布于 2025-01-10 05:24:45 字数 1044 浏览 4 评论 0原文

我试图在折线图中可视化每个日期的速度运行时间。我有以下数据:

ID日期时间
12021-10-20 00:00:001:59:11.19
22021-10-21 00:00:001:55:33.43
32021-10-22 00:00 :001:54:52.82
42021-10-23 00:00:001:53:44.15

数据将被转换为数组;

var runs = [
    [1, "2021-10-20 00:00:00", "1:59:11.19"],
    [2, "2021-10-21 00:00:00", "1:55:33.43"],
    [3, "2021-10-22 00:00:00", "1:54:52.82"],
    [4, "2021-10-23 00:00:00", "1:53:44.15"]
];

我尝试遵循开发文档中的示例,以及示例< a href="https://jsfiddle.net/api/post/library/pure/" rel="nofollow noreferrer">JSFiddle 没有任何运气。因为它们仅显示支持数字或绝对日期时间/创建时间的示例。

有没有办法使折线图与相对时间(包括毫秒)一起工作,按日期的时间顺序显示所有时间?

I'm trying to visualise speedrun times in a linechart, per date. I have the following data:

IDDateTime
12021-10-20 00:00:001:59:11.19
22021-10-21 00:00:001:55:33.43
32021-10-22 00:00:001:54:52.82
42021-10-23 00:00:001:53:44.15

The data will be converted to an array, as such;

var runs = [
    [1, "2021-10-20 00:00:00", "1:59:11.19"],
    [2, "2021-10-21 00:00:00", "1:55:33.43"],
    [3, "2021-10-22 00:00:00", "1:54:52.82"],
    [4, "2021-10-23 00:00:00", "1:53:44.15"]
];

I tried following the example from the dev docs, and the example JSFiddle without any luck. As they only show examples that support numbers or absolute datetimes / created times.

Is there a way to make a linechart work with relative times (including ms), showing all times chronologically per date?

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

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

发布评论

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

评论(1

离鸿 2025-01-17 05:24:45

如果您想在 x 轴上显示日期并在 y 轴上显示时间,
您可以使用 google 的 timeofday 数据类型来表示-轴

DataTable timeofday 列数据类型采用 3 或 4 个数字的数组,分别表示小时、分钟、秒和可选的毫秒。
例如,上午 8:30 的时间为:[8, 30, 0, 0]

以下是用于构建数据表的例程

var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('timeofday', 'Time');

for (var i = 0; i < runs.length; i++) {
  // convert date
  var rowDate = new Date(runs[i][1]);
  
  // convert time to timeofday data type [h, m, sec, ms]
  var rowTime = runs[i][2].split(':');
  var ms = rowTime[rowTime.length - 1].split('.');
  rowTime = rowTime.map(function (timeValue, index) {
    if (index === (rowTime.length - 1)) {
      return parseInt(ms[0]);
    } else {
      return parseInt(timeValue);
    }
  });
  rowTime.push(parseInt(ms[1]));
  
  // add data row
  data.addRow([rowDate, rowTime]);
}

,请参阅以下工作片段...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var runs = [
    [1, "2021-10-20 00:00:00", "1:59:11.19"],
    [2, "2021-10-21 00:00:00", "1:55:33.43"],
    [3, "2021-10-22 00:00:00", "1:54:52.82"],
    [4, "2021-10-23 00:00:00", "1:53:44.15"]
  ];

  var data = new google.visualization.DataTable();
  data.addColumn('date', 'Date');
  data.addColumn('timeofday', 'Time');

  for (var i = 0; i < runs.length; i++) {
    // convert date
    var rowDate = new Date(runs[i][1]);
    
    // convert time to timeofday data type [h, m, sec, ms]
    var rowTime = runs[i][2].split(':');
    var ms = rowTime[rowTime.length - 1].split('.');
    rowTime = rowTime.map(function (timeValue, index) {
      if (index === (rowTime.length - 1)) {
        return parseInt(ms[0]);
      } else {
        return parseInt(timeValue);
      }
    });
    rowTime.push(parseInt(ms[1]));
    
    // add data row
    data.addRow([rowDate, rowTime]);
  }

  var chart = new google.visualization.LineChart(document.getElementById('chart'));
  chart.draw(data, {
    chartArea: {
      height: '100%',
      width: '100%',
      top: 40,
      left: 64,
      right: 40,
      bottom: 40
    },
    hAxis: {
      format: 'MM/dd/yyyy',
      ticks: data.getDistinctValues(0)
    },
    height: '100%',
    legend: {
      position: 'top'
    },
    width: '100%'
  });
});
html, body {
  height: 100%;
  margin: 0px 0px 0px 0px;
  padding: 0px 0px 0px 0px;
}

#chart {
  min-height: 400px;
  height: 100%;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>

if you are wanting to display the date on the x-axis and the time on the y-axis,
you can use google's timeofday data type for the y-axis

The DataTable timeofday column data type takes an array of either 3 or 4 numbers, representing hours, minutes, seconds, and optionally milliseconds, respectively.
For example, the time 8:30am would be: [8, 30, 0, 0]

following is routine used to build data table

var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('timeofday', 'Time');

for (var i = 0; i < runs.length; i++) {
  // convert date
  var rowDate = new Date(runs[i][1]);
  
  // convert time to timeofday data type [h, m, sec, ms]
  var rowTime = runs[i][2].split(':');
  var ms = rowTime[rowTime.length - 1].split('.');
  rowTime = rowTime.map(function (timeValue, index) {
    if (index === (rowTime.length - 1)) {
      return parseInt(ms[0]);
    } else {
      return parseInt(timeValue);
    }
  });
  rowTime.push(parseInt(ms[1]));
  
  // add data row
  data.addRow([rowDate, rowTime]);
}

see following working snippet...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var runs = [
    [1, "2021-10-20 00:00:00", "1:59:11.19"],
    [2, "2021-10-21 00:00:00", "1:55:33.43"],
    [3, "2021-10-22 00:00:00", "1:54:52.82"],
    [4, "2021-10-23 00:00:00", "1:53:44.15"]
  ];

  var data = new google.visualization.DataTable();
  data.addColumn('date', 'Date');
  data.addColumn('timeofday', 'Time');

  for (var i = 0; i < runs.length; i++) {
    // convert date
    var rowDate = new Date(runs[i][1]);
    
    // convert time to timeofday data type [h, m, sec, ms]
    var rowTime = runs[i][2].split(':');
    var ms = rowTime[rowTime.length - 1].split('.');
    rowTime = rowTime.map(function (timeValue, index) {
      if (index === (rowTime.length - 1)) {
        return parseInt(ms[0]);
      } else {
        return parseInt(timeValue);
      }
    });
    rowTime.push(parseInt(ms[1]));
    
    // add data row
    data.addRow([rowDate, rowTime]);
  }

  var chart = new google.visualization.LineChart(document.getElementById('chart'));
  chart.draw(data, {
    chartArea: {
      height: '100%',
      width: '100%',
      top: 40,
      left: 64,
      right: 40,
      bottom: 40
    },
    hAxis: {
      format: 'MM/dd/yyyy',
      ticks: data.getDistinctValues(0)
    },
    height: '100%',
    legend: {
      position: 'top'
    },
    width: '100%'
  });
});
html, body {
  height: 100%;
  margin: 0px 0px 0px 0px;
  padding: 0px 0px 0px 0px;
}

#chart {
  min-height: 400px;
  height: 100%;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>

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