简体   繁体   English

n3-图表动态更改日期的x轴标签

[英]n3-charts dynamically changing x-axis label for date

I'm trying to create a label function that will dynamically change the x-axis label depending on the date range selected as the graph in this website . 我正在尝试创建一个标签函数,该函数将根据在此网站中选择为图表的日期范围来动态更改x轴标签。

To make things easier, the ticks in the x-axis is statically set to 12 instead of dynamically changing it depending on the date range, which I think could be rather difficult. 为了使事情变得容易,将x轴的刻度线静态设置为12,而不是根据日期范围动态更改它,我认为这可能会很困难。

Say a date range of within 1 month will return the label as: 假设1个月内的日期范围将返回标签为:

A day 一天

6 PM - 9 PM - Sat 25 - 03 AM - 06 AM - 09 AM - 12 PM - 03 PM - 06 PM - 09 PM - Sun 26 - 03 AM 下午6点-晚上9点- 周六25 - 03型AM - 06分-上午09点-下午12点-下午3点-下午6点-下午9时- 26日 -上午03点

A week 一周

Sun 10 - Mon 11 - Tue 12 - ... - Sat 16 周日10-周一11-周二12-...-周六16

A Month 一个月

27Mar - 29Tue - 31Apr- Fri - 03Apr - 05 Tue - 07 Thu 09 Sat... 27Mar-29Tue-31Apr- Fri-03Apr-05 Tue-07 Thu 09 Sat ...

Using the d3.time.format("%I:%M %p") as the labelFunction works, however it is not suitable to change the label dynamically. 使用d3.time.format(“%I:%M%p”)作为labelFunction起作用,但是不适合动态更改标签。 I also tried to create a separate functions formatDateAxis() to return the string depending on the startDate and endDate (in Unix epoch milliseconds) and return the format depending on the range, but fails returning only the format itself, eg %I %p. 我还尝试创建一个单独的函数formatDateAxis()以根据startDate和endDate返回字符串(以Unix纪元毫秒为单位),并根据范围返回格式,但是仅返回格式本身失败,例如%I%p。

  $scope.options = {
    axes: {
      x: {key: 'x', labelFunction: d3.time.format("%I:%M %p"), type: 'date', ticks: 12},
      y: {key: 'y1', type: 'linear', min: 20, max: 60, ticks: 5},
      y2: {key: 'y2', type: 'linear', min: 20, max: 60, ticks: 5}
    },
    series: [
      {y: 'value', color: '#0099FF', thickness: '2px', label: 'Temperature', dotSize: 2},
      {y: 'otherValue', axis: 'y2', color: '#6666FF', thinkness: '2px', visible: true, dotSize: 2, label: 'Humidity'}
    ],
    lineMode: 'linear',
    tension: 0.7,
    tooltip: {mode: 'scrubber', formatter: function(x, y, series) {return moment(x).fromNow() + ' : ' + y;}},
    drawLegend: true,
    drawDots: false,
    columnsHGap: 5
  }  

Function to format the x axis depending on the startDate and endDate of the selected data source. 用于根据所选数据源的startDate和endDate格式化x轴的函数。 Referred from another stackoverflow post here . 此处的另一个stackoverflow帖子引用。

var formatXAxes = function(value) {
  var dateFormat = "";
  if (endDateMillis - startDateMillis > 1468800000) {
    dateFormat = d3.time.format('%a %d');
  }

  else if (endDateMillis - startDateMillis > 172800000) {
    dateFormat = d3.time.format('%a %d');
  }

  else {
    dateFormat = d3.time.format('%I %p');
  }
  return dateFormat;
}

I would appreciate any helps to achieve this task to chart some historical sensor records for my home. 我将不胜感激,可以帮助您完成此任务,以绘制一些我家的历史传感器记录。

You can access the formatter by accessing the n3chart options javascript object say if you want to change the format of your x axis you can do: 您可以通过访问n3chart选项javascript对象来访问格式化程序,例如,如果您想更改x轴的格式,则可以执行以下操作:

$scope.options.axes.x.ticksFormat = '%X'

this will automatically update the ticksFormat and the n3chart will be updated in realtime to show the change. 这将自动更新ticksFormat,并且n3chart将实时更新以显示更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM