D3.js x轴时间标度 [英] D3.js x-axis time scale

查看:134
本文介绍了D3.js x轴时间标度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在D3.js做一个散点图,日期在x轴。下面的代码基于d3站点上的散点图示例。我必须在attr中做错事('cx'
区域...

I'm trying to do a scatter plot in D3.js with date on the x-axis. The code below is based on the scatter plot example on the d3 site. I must be doing something wrong in the attr('cx' area...

var data =[

  {
    "title":"SUNY GENESEO COLLEGE STADIUM PHASE 2",
    "stage":"Biddate Set",
    "naples_update_date":"2/9/2014",
    "value":7500000,
    "value_type":"Confirmed",
    "ownership":"State",
    "work_type":"Alteration",
    "record_date":"1/21/2014",
    "floors":null,
    "floor_area":null,
    "floor_units":"",
    "land_area":null,
    "land_units":"",
    "structures":null,
    "units":0,
    "contract_type":"Open Bidding",
    "address":"1 College Cir",
    "city":"Geneseo",
    "state":"NY",
    "county":"Livingston",
    "date":1390911781
  },
  {
    "title":"KENTUCKY FAIR & EXPOSITION CENTER FREEDOM HALL-ROOFING",
    "stage":"Post Bid Results Pending",
    "naples_update_date":"2/10/2014",
    "value":2662903,
    "value_type":"Confirmed",
    "ownership":"State",
    "work_type":"Alteration",
    "record_date":"10/29/2013",
    "floors":2,
    "floor_area":null,
    "floor_units":"",
    "land_area":null,
    "land_units":"",
    "structures":1,
    "units":0,
    "contract_type":"Open Bidding",
    "address":"937 Phillips Ln",
    "city":"Louisville",
    "state":"KY",
    "county":"Jefferson",
    "date":1383132359
  }
];

var format = d3.time.format("%d/%m/%Y");
var dateMin = format.parse("20/03/2001");
var dateMax = format.parse("7/02/2001"); 

var margin = {top: 20, right: 20, bottom: 30, left: 120},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;
var xValue = function(d) { 
  return format.parse(d.record_date);
  }, // data -> value
    xScale = d3.time.scale().domain([dateMin,dateMax]).range([0, width]), // value -> display
    xMap = function(d) { return xScale(xValue(d));}, // data -> display
    xAxis = d3.svg.axis().scale(xScale).orient("bottom");
var yValue = function(d) { return d.value;}, // data -> value
    yScale = d3.scale.linear().range([height, 0]), // value -> display
    yMap = function(d) { return yScale(yValue(d));}, // data -> display
    yAxis = d3.svg.axis().scale(yScale).orient("left");
// setup fill color
var cValue = function(d) { return d.ownership;},
    color = d3.scale.category10();
// add the graph canvas to the body of the webpage
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 + ")");

// add the tooltip area to the webpage
var tooltip = d3.select("body").append("div")
    .attr("class", "tooltip")
    .style("opacity", 0);

// don't want dots overlapping axis, so add in buffer to data domain
  xScale.domain([d3.min(data, xValue)-1, d3.max(data, xValue)+1]);
  yScale.domain([d3.min(data, yValue)-1, d3.max(data, yValue)+1]);

//x-axis
svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis)
    .append("text")
      .attr("class", "label")
      .attr("x", width)
      .attr("y", -6)
      .style("text-anchor", "end")
      .text("Date");

  // y-axis
  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
    .append("text")
      .attr("class", "label")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("Value");

// draw dots
  svg.selectAll(".dot")
      .data(data)
    .enter().append("circle")
      .attr("class", "dot")
      .attr("r", 3.5)
  .attr("cx", xMap)
      .attr("cy", yMap)
      .style("fill", function(d) { return color(cValue(d));}) 
      .on("mouseover", function(d) {
          tooltip.transition()
               .duration(200)
               .style("opacity", 0.9);
          tooltip.html(d.title + "<br/> (" + xValue(d) + ", " + yValue(d) + ")")
               .style("left", (d3.event.pageX + 5) + "px")
               .style("top", (d3.event.pageY - 28) + "px");
      })
      .on("mouseout", function(d) {
          tooltip.transition()
               .duration(500)
               .style("opacity", 0);
      })
  .attr('data-title',function(e){
return e.title;
})
.attr('data-value',function(e){
return e.value;
})
.attr('data-date',function(e){
return e.record_date;
})
.attr('data-sqft',function(e){
return e.floor_area;
});

我已经搜索过,并尝试按照提示,确保的日期。

I've searched around and tried to follow the tips out there, making sure the dates for the .range() are objects of the same format at the dates inside attr(cx).

推荐答案

演示: http://jsfiddle.net/EC6TL/

问题出在line:

xScale.domain([d3.min(data, xValue) - 1, d3.max(data, xValue) + 1]);

您不能从日期中添加和减少 1 。 : - )

You cannot add and subtract 1 from dates. :-)

修复

xScale.domain([d3.min(data, xValue), d3.max(data, xValue)]);

这篇关于D3.js x轴时间标度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆