如何使用d3.js以包含毫秒的时间格式缩放图形的x轴?

时间:2016-08-11 07:19:45

标签: javascript d3.js graph axis

我想使用csv数据使用d3.js生成热图。对于热图绘制,我试图设置x轴,y轴和z轴。但是在设置图表的x轴时我遇到了问题。

示例csv数据:

var csv_data = "date,bucket,count\n07:31:01 001,10000,12\n07:31:01 119,50000,13\n07:31:01 128,60000,53\n07:31:01 200,90000,24\n07:31:01 555,75000,56";

程序代码 -

    <script>
var margin = {top: 20, right: 90, bottom: 30, left: 50},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;


var parseDate = d3.time.format("%Y-%m-%d").parse,
    formatDate = d3.time.format("%b %d");

//
var x = d3.time.scale().range([0, width]),
    y = d3.scale.linear().range([height, 0]),
    z = d3.scale.linear().range(["white", "steelblue"]);

// The size of the buckets in the CSV data file.
// This could be inferred from the data if it weren't sparse.
var xStep = 864e5,
    yStep = 100;

var svg = d3.select("body").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 + ")");

    var csv_data = "date,bucket,count\n07:31:01 001,10000,12 005\n07:31:01 119,50000,13\n07:31:01 128,60000,53";

    var buckets = d3.csv.parse(csv_data);
    buckets.forEach(function(d) {
        d.date = d.date;
        d.bucket = +d.bucket;
        d.count = +d.count;
    });

    x.domain(d3.extent(buckets, function(d) { return d.date; }));
    y.domain(d3.extent(buckets, function(d) { return d.bucket; }));
    z.domain([0, d3.max(buckets, function(d) { return d.count; })]);

    // Extend the x- and y-domain to fit the last bucket.
    // For example, the y-bucket 3200 corresponds to values [3200, 3300].
    x.domain([x.domain()[0], +x.domain()[1] + xStep]);
    y.domain([y.domain()[0], y.domain()[1] + yStep]);

    // Display the tiles for each non-zero bucket.
    svg.selectAll(".tile")
      .data(buckets)
      .enter().append("rect")
      .attr("class", "tile")
      .attr("x", function(d) { return x(d.date); })
      .attr("y", function(d) { return y(d.bucket + yStep); })
      .attr("width", x(xStep) - x(0))
      .attr("height",  y(0) - y(yStep))
      .style("fill", function(d) { return z(d.count); });

  // Add a legend for the color values.
  var legend = svg.selectAll(".legend")
      .data(z.ticks(6).slice(1).reverse())
    .enter().append("g")
      .attr("class", "legend")
      .attr("transform", function(d, i) { return "translate(" + (width + 20) + "," + (20 + i * 20) + ")"; });

  legend.append("rect")
      .attr("width", 20)
      .attr("height", 20)
      .style("fill", z);

  legend.append("text")
      .attr("x", 26)
      .attr("y", 10)
      .attr("dy", ".35em")
      .text(String);

  svg.append("text")
      .attr("class", "label")
      .attr("x", width + 20)
      .attr("y", 10)
      .attr("dy", ".35em")
      .text("Count");

  // Add an x-axis with label.
  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.svg.axis().scale(x).orient("bottom"))
    .append("text")
      .attr("class", "label")
      .attr("x", width)
      .attr("y", -6)
      .attr("text-anchor", "end")
      .text("Time");

  // Add a y-axis with label.
  svg.append("g")
      .attr("class", "y axis")
      .call(d3.svg.axis().scale(y).orient("left"))
    .append("text")
      .attr("class", "label")
      .attr("y", 6)
      .attr("dy", ".71em")
      .attr("text-anchor", "end")
      .attr("transform", "rotate(-90)")
      .text("Latency");

    </script>

目前图表显示如下:

enter image description here

如何将数据的x轴设置为字符串中的数据?我在这里失踪了什么?

1 个答案:

答案 0 :(得分:1)

x axis表示时间刻度。您需要传入有效的日期对象而不是字符串。在您的特定示例中,这将变为:

buckets.forEach(function(d) {
  d.date = timeFormat.parse(d.date);
  d.bucket = +d.bucket;
  d.count = +d.count;
});

其中timeFormat是d3.time.format格式化程序:

var timeFormat = d3.time.format('%H:%M:%S %L');

要解析毫秒,您可以使用the following directive

  

%L - 毫秒作为十进制数[000,999]。

在您的CSV数据中,日期应遵循指定的格式。例如:

07:31:01 001,10000,12005
13:34:01 119,50000,13
17:39:01 128,60000,53

这是一个有效的jsfiddle

enter image description here