繁体   English   中英

d3.js 在时间序列尺度上绘制图形

[英]d3.js plotting the graph on time series scale

我的数据采用 d [ Object, Object,.... ] 格式,其中每个对象的格式为 {count:5 time:1352961975 },其中 time 是 Unix Timestamp。 我想根据时间和计数值来绘制它。 计数值绘制在 y 轴上,时间将绘制在 x 轴上为 12:30 AM , 1 AM, 1:30 AM ... 。 到当前时间。
像这样

  var midnight_time = new Date();
  midnight_time.setHours(0,0,0,0);


  x = d3.time.scale()
  .range([0,width])
  .domain([ new Date(midnight_time),new Date()]);

  y = d3.scale.linear()
  .domain([0,ymax]) //by using ajax not shown here getting the value of ymax                     
  .range([height, 0]);

 line = d3.svg.line()
.x(function(d) { return x(d.count); })
.y(function(d) { return y(d.time); });

svg = d3.select("#viz").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");


  svg.append("defs").append("clipPath")
   .attr("id", "clip")
   .append("rect")
   .attr("width", width)
  .attr("height", height);

 svg.append("g")
   .attr("class", "x axis")
   .attr("transform", "translate(0," + height + ")")
   .call(d3.svg.axis().scale(x).orient("bottom")
   .ticks(d3.time.minutes,tick_count));

 svg.append("g")
   .attr("class", "y axis")
   .call(d3.svg.axis().scale(y).orient("left"));

  d3.select(".x.axis")
     .append("text")
      .text("the time span")
   .attr("transform", "translate(360,30)");


  d3.select(".y.axis")
    .append("text")
    .text("Maximum number of active users")
    .attr("transform", "rotate (-90, -35, 0) translate(-210)");

 path = svg.append("g")
   .attr("clip-path", "url(#clip)")
   .append("path")
   .data([data])
   .attr("class", "line")
   .attr("d", line);

我想我没有正确应用时间格式。 我需要将 Unix 时间戳转换为 AM 或 PM 格式的正确时间,然后相应地调用图形绘图。

UNIX 时间戳没有 AM 或 PM 的概念,您需要使用格式化程序来处理它。 d3 提供了用于创建时间格式化程序的函数,它可以让您指定它完全按照您的意愿显示(请参阅此文档)。 然后,您可以使用tickFormat方法将该格式化程序传递给轴。

我认为格式化字符串%I:%M %p会给你你想要的格式。 您可以将其应用于以下内容:

d3.svg.axis().scale(y).orient("left").tickFormat(d3.time.format("%I:%M %p"))

暂无
暂无

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

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